blob: 2716f841f2732b5bbd8a3de371351740eae85e91 [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 Tarreau3d6ee402021-05-08 20:28:07 +020048#include <haproxy/proxy.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 Tarreau69c66e32021-07-14 19:41:25 +0200127static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; }
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
Willy Tarreau3eb2e472021-08-20 15:47:25 +0200146/* This function takes the Lua global lock. Keep this function's visibility
147 * global so that it can appear in stack dumps and performance profiles!
148 */
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +0100149static inline void lua_take_global_lock()
Willy Tarreau3eb2e472021-08-20 15:47:25 +0200150{
151 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
152}
153
154static inline void lua_drop_global_lock()
155{
156 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
157}
158
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +0100159/* lua lock helpers: only lock when required
160 *
161 * state_id == 0: we're operating on the main lua stack (shared between
162 * os threads), so we need to acquire the main lock
163 *
164 * If the thread already owns the lock (_hlua_locked != 0), skip the lock
165 * attempt. This could happen if we run under protected lua environment.
166 * Not doing this could result in deadlocks because of nested locking
167 * attempts from the same thread
168 */
169static THREAD_LOCAL int _hlua_locked = 0;
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +0100170static inline void hlua_lock(struct hlua *hlua)
171{
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +0100172 if (hlua->state_id != 0)
173 return;
174 if (!_hlua_locked)
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +0100175 lua_take_global_lock();
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +0100176 _hlua_locked += 1;
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +0100177}
178static inline void hlua_unlock(struct hlua *hlua)
179{
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +0100180 if (hlua->state_id != 0)
181 return;
182 BUG_ON(_hlua_locked <= 0);
183 _hlua_locked--;
184 /* drop the lock once the lock count reaches 0 */
185 if (!_hlua_locked)
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +0100186 lua_drop_global_lock();
187}
188
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100189#define SET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200190 ({ \
191 int ret; \
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +0100192 hlua_lock(__HLUA); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200193 if (setjmp(safe_ljmp_env) != 0) { \
194 lua_atpanic(__L, hlua_panic_safe); \
195 ret = 0; \
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +0100196 hlua_unlock(__HLUA); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200197 } else { \
198 lua_atpanic(__L, hlua_panic_ljmp); \
199 ret = 1; \
200 } \
201 ret; \
202 })
203
204/* If we are the last function catching Lua errors, we
205 * must reset the panic function.
206 */
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100207#define RESET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200208 do { \
209 lua_atpanic(__L, hlua_panic_safe); \
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +0100210 hlua_unlock(__HLUA); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200211 } while(0)
212
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100213#define SET_SAFE_LJMP(__HLUA) \
214 SET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
215
216#define RESET_SAFE_LJMP(__HLUA) \
217 RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
218
219#define SET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100220 SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100221
222#define RESET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100223 RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100224
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200225/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200226#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100227/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200228#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200229/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100230#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100231#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200232
Thierry Fournierafc63e22020-11-28 17:06:51 +0100233/* The main Lua execution context. The 0 index is the
234 * common state shared by all threads.
235 */
Willy Tarreau186f3762020-12-04 11:48:12 +0100236static lua_State *hlua_states[MAX_THREADS + 1];
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100237
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100238/* This is the memory pool containing struct lua for applets
239 * (including cli).
240 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100241DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100242
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100243/* Used for Socket connection. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +0100244static struct proxy *socket_proxy;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100245static struct server *socket_tcp;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100246#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100247static struct server *socket_ssl;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100248#endif
249
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100250/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100251struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100252
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100253/* The following variables contains the reference of the different
254 * Lua classes. These references are useful for identify metadata
255 * associated with an object.
256 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100257static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100258static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100259static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100260static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100261static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100262static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200263static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200264static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200265static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100266static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100267
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100268/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200269 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100270 * short timeout. Lua linked with tasks doesn't have a timeout
271 * because a task may remain alive during all the haproxy execution.
272 */
273static unsigned int hlua_timeout_session = 4000; /* session timeout. */
274static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200275static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100276
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100277/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
278 * it is used for preventing infinite loops.
279 *
280 * I test the scheer with an infinite loop containing one incrementation
281 * and one test. I run this loop between 10 seconds, I raise a ceil of
282 * 710M loops from one interrupt each 9000 instructions, so I fix the value
283 * to one interrupt each 10 000 instructions.
284 *
285 * configured | Number of
286 * instructions | loops executed
287 * between two | in milions
288 * forced yields |
289 * ---------------+---------------
290 * 10 | 160
291 * 500 | 670
292 * 1000 | 680
293 * 5000 | 700
294 * 7000 | 700
295 * 8000 | 700
296 * 9000 | 710 <- ceil
297 * 10000 | 710
298 * 100000 | 710
299 * 1000000 | 710
300 *
301 */
302static unsigned int hlua_nb_instruction = 10000;
303
Willy Tarreaucdb53462020-12-02 12:12:00 +0100304/* Descriptor for the memory allocation state. The limit is pre-initialised to
305 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
306 * is replaced with ~0 during post_init after everything was loaded. This way
307 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
308 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100309 */
310struct hlua_mem_allocator {
311 size_t allocated;
312 size_t limit;
313};
314
Willy Tarreaucdb53462020-12-02 12:12:00 +0100315static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100316
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100317/* These functions converts types between HAProxy internal args or
318 * sample and LUA types. Another function permits to check if the
319 * LUA stack contains arguments according with an required ARG_T
320 * format.
321 */
322static int hlua_arg2lua(lua_State *L, const struct arg *arg);
323static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100324__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100325 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100326static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100327static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100328static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
329
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100330__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200331
Thierry Fournier59f11be2020-11-29 00:37:41 +0100332struct prepend_path {
333 struct list l;
334 char *type;
335 char *path;
336};
337
338static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
339
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200340#define SEND_ERR(__be, __fmt, __args...) \
341 do { \
342 send_log(__be, LOG_ERR, __fmt, ## __args); \
343 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100344 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200345 } while (0)
346
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100347static inline struct hlua_function *new_hlua_function()
348{
349 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100350 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100351
352 fcn = calloc(1, sizeof(*fcn));
353 if (!fcn)
354 return NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200355 LIST_APPEND(&referenced_functions, &fcn->l);
Thierry Fournierc7492592020-11-28 23:57:24 +0100356 for (i = 0; i < MAX_THREADS + 1; i++)
357 fcn->function_ref[i] = -1;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100358 return fcn;
359}
360
Christopher Fauletdda44442021-04-12 14:05:43 +0200361static inline void release_hlua_function(struct hlua_function *fcn)
362{
363 if (!fcn)
364 return;
365 if (fcn->name)
366 ha_free(&fcn->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200367 LIST_DELETE(&fcn->l);
Christopher Fauletdda44442021-04-12 14:05:43 +0200368 ha_free(&fcn);
369}
370
Thierry Fournierc7492592020-11-28 23:57:24 +0100371/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
372static inline int fcn_ref_to_stack_id(struct hlua_function *fcn)
373{
374 if (fcn->function_ref[0] == -1)
375 return tid + 1;
376 return 0;
377}
378
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100379/* Used to check an Lua function type in the stack. It creates and
380 * returns a reference of the function. This function throws an
Aurelien DARRAGON5ce11522023-03-02 17:50:49 +0100381 * error if the argument is not a "function".
382 * When no longer used, the ref must be released with hlua_unref()
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100383 */
384__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
385{
386 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100387 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100388 WILL_LJMP(luaL_argerror(L, argno, msg));
389 }
390 lua_pushvalue(L, argno);
391 return luaL_ref(L, LUA_REGISTRYINDEX);
392}
393
Christopher Fauletddc90322020-02-25 10:20:04 +0100394/* Used to check an Lua table type in the stack. It creates and
395 * returns a reference of the table. This function throws an
Aurelien DARRAGON5ce11522023-03-02 17:50:49 +0100396 * error if the argument is not a "table".
397 * When no longer used, the ref must be released with hlua_unref()
Christopher Fauletddc90322020-02-25 10:20:04 +0100398 */
399__LJMP unsigned int hlua_checktable(lua_State *L, int argno)
400{
401 if (!lua_istable(L, argno)) {
402 const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno));
403 WILL_LJMP(luaL_argerror(L, argno, msg));
404 }
405 lua_pushvalue(L, argno);
406 return luaL_ref(L, LUA_REGISTRYINDEX);
407}
408
Aurelien DARRAGON5ce11522023-03-02 17:50:49 +0100409/* Get a reference to the object that is at the top of the stack
410 * The referenced object will be popped from the stack
411 *
412 * The function returns the reference to the object which must
413 * be cleared using hlua_unref() when no longer used
414 */
415__LJMP int hlua_ref(lua_State *L)
416{
417 return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX));
418}
419
420/* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX)
421 * on <L> stack
422 * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref())
423 *
424 * When the reference is no longer used, it should be released by calling
425 * hlua_unref()
426 *
427 * <L> can be from any co-routine as long as it belongs to the same lua
428 * parent state that the one used to get the reference.
429 */
430void hlua_pushref(lua_State *L, int ref)
431{
432 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
433}
434
435/* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX)
436 * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref())
437 *
438 * This will allow the reference to be reused and the referred object
439 * to be garbage collected.
440 *
441 * <L> can be from any co-routine as long as it belongs to the same lua
442 * parent state that the one used to get the reference.
443 */
444void hlua_unref(lua_State *L, int ref)
445{
446 luaL_unref(L, LUA_REGISTRYINDEX, ref);
447}
448
Christopher Fauletd09cc512021-03-24 14:48:45 +0100449__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200450{
451 lua_Debug ar;
452 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200453 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200454
455 while (lua_getstack(L, level++, &ar)) {
456
457 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100458 if (b_data(msg))
459 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200460
461 /* Fill fields:
462 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
463 * 'l': fills in the field currentline;
464 * 'n': fills in the field name and namewhat;
465 * 't': fills in the field istailcall;
466 */
467 lua_getinfo(L, "Slnt", &ar);
468
469 /* Append code localisation */
470 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100471 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200472 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100473 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200474
475 /*
476 * Get function name
477 *
478 * if namewhat is no empty, name is defined.
479 * what contains "Lua" for Lua function, "C" for C function,
480 * or "main" for main code.
481 */
482 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100483 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200484
485 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100486 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200487
488 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100489 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200490
491 else /* nothing left... */
492 chunk_appendf(msg, "?");
493
494
495 /* Display tailed call */
496 if (ar.istailcall)
497 chunk_appendf(msg, " ...");
498 }
499
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200501}
502
503
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100504/* This function check the number of arguments available in the
505 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500506 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100507 */
508__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
509{
510 if (lua_gettop(L) == nb)
511 return;
512 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
513}
514
Mark Lakes22154b42018-01-29 14:38:40 -0800515/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100516 * and the line number where the error is encountered.
517 */
518static int hlua_pusherror(lua_State *L, const char *fmt, ...)
519{
520 va_list argp;
521 va_start(argp, fmt);
522 luaL_where(L, 1);
523 lua_pushvfstring(L, fmt, argp);
524 va_end(argp);
525 lua_concat(L, 2);
526 return 1;
527}
528
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100529/* This functions is used with sample fetch and converters. It
530 * converts the HAProxy configuration argument in a lua stack
531 * values.
532 *
533 * It takes an array of "arg", and each entry of the array is
534 * converted and pushed in the LUA stack.
535 */
536static int hlua_arg2lua(lua_State *L, const struct arg *arg)
537{
538 switch (arg->type) {
539 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540 case ARGT_TIME:
541 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100542 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100543 break;
544
545 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200546 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100547 break;
548
549 case ARGT_IPV4:
550 case ARGT_IPV6:
551 case ARGT_MSK4:
552 case ARGT_MSK6:
553 case ARGT_FE:
554 case ARGT_BE:
555 case ARGT_TAB:
556 case ARGT_SRV:
557 case ARGT_USR:
558 case ARGT_MAP:
559 default:
560 lua_pushnil(L);
561 break;
562 }
563 return 1;
564}
565
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500566/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500568 * with sample fetch wrappers. The input arguments are given to the
569 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 */
571static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
572{
573 switch (lua_type(L, ud)) {
574
575 case LUA_TNUMBER:
576 case LUA_TBOOLEAN:
577 arg->type = ARGT_SINT;
578 arg->data.sint = lua_tointeger(L, ud);
579 break;
580
581 case LUA_TSTRING:
582 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200583 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200584 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200585 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200586 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100587 break;
588
589 case LUA_TUSERDATA:
590 case LUA_TNIL:
591 case LUA_TTABLE:
592 case LUA_TFUNCTION:
593 case LUA_TTHREAD:
594 case LUA_TLIGHTUSERDATA:
595 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200596 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100597 break;
598 }
599 return 1;
600}
601
602/* the following functions are used to convert a struct sample
603 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500604 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100605 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100606static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100607{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200608 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100609 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200611 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100612 break;
613
614 case SMP_T_BIN:
615 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100617 break;
618
619 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200620 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100621 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
622 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
623 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
624 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
625 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
626 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
627 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
628 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
629 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200630 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100631 break;
632 default:
633 lua_pushnil(L);
634 break;
635 }
636 break;
637
638 case SMP_T_IPV4:
639 case SMP_T_IPV6:
640 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200641 if (sample_casts[smp->data.type][SMP_T_STR] &&
642 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200643 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100644 else
645 lua_pushnil(L);
646 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100647 default:
648 lua_pushnil(L);
649 break;
650 }
651 return 1;
652}
653
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100654/* the following functions are used to convert a struct sample
655 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500656 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100657 */
658static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
659{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200660 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100661
662 case SMP_T_BIN:
663 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200664 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100665 break;
666
667 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200668 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100669 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
670 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
671 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
672 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
673 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
674 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
675 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
676 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
677 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200678 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100679 break;
680 default:
681 lua_pushstring(L, "");
682 break;
683 }
684 break;
685
686 case SMP_T_SINT:
687 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100688 case SMP_T_IPV4:
689 case SMP_T_IPV6:
690 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200691 if (sample_casts[smp->data.type][SMP_T_STR] &&
692 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200693 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100694 else
695 lua_pushstring(L, "");
696 break;
697 default:
698 lua_pushstring(L, "");
699 break;
700 }
701 return 1;
702}
703
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100704/* the following functions are used to convert an Lua type in a
705 * struct sample. This is useful to provide data from a converter
706 * to the LUA code.
707 */
708static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
709{
710 switch (lua_type(L, ud)) {
711
712 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200713 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200714 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100715 break;
716
717
718 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200719 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200720 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100721 break;
722
723 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200724 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100725 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200726 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200727 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200728 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200729 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100730 break;
731
732 case LUA_TUSERDATA:
733 case LUA_TNIL:
734 case LUA_TTABLE:
735 case LUA_TFUNCTION:
736 case LUA_TTHREAD:
737 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200738 case LUA_TNONE:
739 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200740 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200741 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100742 break;
743 }
744 return 1;
745}
746
Ilya Shipitsind4259502020-04-08 01:07:56 +0500747/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800748 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100749 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100750 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500751 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100752 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100753 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100754__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100755 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100756{
757 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200758 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100759 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200760 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200761 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200762 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200763 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100764
765 idx = 0;
766 min_arg = ARGM(mask);
767 mask >>= ARGM_BITS;
768
769 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200770 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100771
772 /* Check oversize. */
773 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200774 msg = "Malformed argument mask";
775 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100776 }
777
778 /* Check for mandatory arguments. */
779 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100780 if (idx < min_arg) {
781
782 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200783 if (idx > 0) {
784 msg = "Mandatory argument expected";
785 goto error;
786 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100787
788 /* If first argument have a certain type, some default values
789 * may be used. See the function smp_resolve_args().
790 */
791 switch (mask & ARGT_MASK) {
792
793 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200794 if (!(p->cap & PR_CAP_FE)) {
795 msg = "Mandatory argument expected";
796 goto error;
797 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100798 argp[idx].data.prx = p;
799 argp[idx].type = ARGT_FE;
800 argp[idx+1].type = ARGT_STOP;
801 break;
802
803 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200804 if (!(p->cap & PR_CAP_BE)) {
805 msg = "Mandatory argument expected";
806 goto error;
807 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100808 argp[idx].data.prx = p;
809 argp[idx].type = ARGT_BE;
810 argp[idx+1].type = ARGT_STOP;
811 break;
812
813 case ARGT_TAB:
Olivier Houcharda2658272022-09-13 00:35:53 +0200814 if (!p->table) {
815 msg = "Mandatory argument expected";
816 goto error;
817 }
818 argp[idx].data.t = p->table;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100819 argp[idx].type = ARGT_TAB;
820 argp[idx+1].type = ARGT_STOP;
821 break;
822
823 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200824 msg = "Mandatory argument expected";
825 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100826 break;
827 }
828 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200829 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100830 }
831
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500832 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100833 if ((mask & ARGT_MASK) == ARGT_STOP &&
834 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200835 msg = "Last argument expected";
836 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100837 }
838
839 if ((mask & ARGT_MASK) == ARGT_STOP &&
840 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200841 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100842 }
843
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200844 /* Convert some argument types. All string in argp[] are for not
845 * duplicated yet.
846 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100847 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100848 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200849 if (argp[idx].type != ARGT_SINT) {
850 msg = "integer expected";
851 goto error;
852 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100853 argp[idx].type = ARGT_SINT;
854 break;
855
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100856 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200857 if (argp[idx].type != ARGT_SINT) {
858 msg = "integer expected";
859 goto error;
860 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200861 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100862 break;
863
864 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200865 if (argp[idx].type != ARGT_SINT) {
866 msg = "integer expected";
867 goto error;
868 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200869 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100870 break;
871
872 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200873 if (argp[idx].type != ARGT_STR) {
874 msg = "string expected";
875 goto error;
876 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200877 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200878 if (!argp[idx].data.prx) {
879 msg = "frontend doesn't exist";
880 goto error;
881 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100882 argp[idx].type = ARGT_FE;
883 break;
884
885 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200886 if (argp[idx].type != ARGT_STR) {
887 msg = "string expected";
888 goto error;
889 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200890 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200891 if (!argp[idx].data.prx) {
892 msg = "backend doesn't exist";
893 goto error;
894 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100895 argp[idx].type = ARGT_BE;
896 break;
897
898 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200899 if (argp[idx].type != ARGT_STR) {
900 msg = "string expected";
901 goto error;
902 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200903 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200904 if (!argp[idx].data.t) {
905 msg = "table doesn't exist";
906 goto error;
907 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100908 argp[idx].type = ARGT_TAB;
909 break;
910
911 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200912 if (argp[idx].type != ARGT_STR) {
913 msg = "string expected";
914 goto error;
915 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200916 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100917 if (sname) {
918 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200919 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200920 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200921 if (!px) {
922 msg = "backend doesn't exist";
923 goto error;
924 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100925 }
926 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200927 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100928 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100929 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100930 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200931 if (!argp[idx].data.srv) {
932 msg = "server doesn't exist";
933 goto error;
934 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100935 argp[idx].type = ARGT_SRV;
936 break;
937
938 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200939 if (argp[idx].type != ARGT_STR) {
940 msg = "string expected";
941 goto error;
942 }
943 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
944 msg = "invalid IPv4 address";
945 goto error;
946 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100947 argp[idx].type = ARGT_IPV4;
948 break;
949
950 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200951 if (argp[idx].type == ARGT_SINT)
952 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
953 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200954 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
955 msg = "invalid IPv4 mask";
956 goto error;
957 }
958 }
959 else {
960 msg = "integer or string expected";
961 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200962 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100963 argp[idx].type = ARGT_MSK4;
964 break;
965
966 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200967 if (argp[idx].type != ARGT_STR) {
968 msg = "string expected";
969 goto error;
970 }
971 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
972 msg = "invalid IPv6 address";
973 goto error;
974 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100975 argp[idx].type = ARGT_IPV6;
976 break;
977
978 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200979 if (argp[idx].type == ARGT_SINT)
980 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
981 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200982 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
983 msg = "invalid IPv6 mask";
984 goto error;
985 }
986 }
987 else {
988 msg = "integer or string expected";
989 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200990 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100991 argp[idx].type = ARGT_MSK6;
992 break;
993
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200994 case ARGT_REG:
995 if (argp[idx].type != ARGT_STR) {
996 msg = "string expected";
997 goto error;
998 }
999 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
1000 if (!reg) {
1001 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
1002 argp[idx].data.str.area, err);
1003 free(err);
1004 goto error;
1005 }
1006 argp[idx].type = ARGT_REG;
1007 argp[idx].data.reg = reg;
1008 break;
1009
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +01001010 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +02001011 if (argp[idx].type != ARGT_STR) {
1012 msg = "string expected";
1013 goto error;
1014 }
1015 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001016 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +02001017 ul = p->uri_auth->userlist;
1018 else
1019 ul = auth_find_userlist(argp[idx].data.str.area);
1020
1021 if (!ul) {
1022 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
1023 goto error;
1024 }
1025 argp[idx].type = ARGT_USR;
1026 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001027 break;
1028
1029 case ARGT_STR:
1030 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
1031 msg = "unable to duplicate string arg";
1032 goto error;
1033 }
1034 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001035 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001036
Christopher Fauletd25d9262020-08-06 11:04:46 +02001037 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +02001038 msg = "type not yet supported";
1039 goto error;
1040 break;
1041
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001042 }
1043
1044 /* Check for type of argument. */
1045 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001046 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
1047 arg_type_names[(mask & ARGT_MASK)],
1048 arg_type_names[argp[idx].type & ARGT_MASK]);
1049 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001050 }
1051
1052 /* Next argument. */
1053 mask >>= ARGT_BITS;
1054 idx++;
1055 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001056 return 0;
1057
1058 error:
Olivier Houchardda25dac2022-09-13 00:31:17 +02001059 argp[idx].type = ARGT_STOP;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001060 for (i = 0; i < idx; i++) {
1061 if (argp[i].type == ARGT_STR)
1062 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02001063 else if (argp[i].type == ARGT_REG)
1064 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001065 }
1066 WILL_LJMP(luaL_argerror(L, first + idx, msg));
1067 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001068}
1069
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001071 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001072 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001073 *
1074 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001075 * - hlua_sethlua : create the association between hlua context and lua_state.
1076 */
1077static inline struct hlua *hlua_gethlua(lua_State *L)
1078{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001079 struct hlua **hlua = lua_getextraspace(L);
1080 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001081}
1082static inline void hlua_sethlua(struct hlua *hlua)
1083{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001084 struct hlua **hlua_store = lua_getextraspace(hlua->T);
1085 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001086}
1087
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001088/* This function is used to send logs. It try to send on screen (stderr)
1089 * and on the default syslog server.
1090 */
1091static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
1092{
1093 struct tm tm;
1094 char *p;
1095
1096 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001097 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001098 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001099 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001100 /* Break the message if exceed the buffer size. */
1101 *(p-4) = ' ';
1102 *(p-3) = '.';
1103 *(p-2) = '.';
1104 *(p-1) = '.';
1105 break;
1106 }
Willy Tarreau90807112020-02-25 08:16:33 +01001107 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001108 *p = *msg;
1109 else
1110 *p = '.';
1111 }
1112 *p = '\0';
1113
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001114 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001115 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001116 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1117 return;
1118
Willy Tarreaua678b432015-08-28 10:14:59 +02001119 get_localtime(date.tv_sec, &tm);
1120 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001121 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001122 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001123 fflush(stderr);
1124 }
1125}
1126
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001127/* This function just ensure that the yield will be always
1128 * returned with a timeout and permit to set some flags
1129 */
Aurelien DARRAGONf5d3b312023-07-13 10:18:04 +02001130__LJMP void hlua_yieldk(lua_State *L, int nresults, lua_KContext ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001131 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001132{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001133 struct hlua *hlua;
1134
1135 /* Get hlua struct, or NULL if we execute from main lua state */
1136 hlua = hlua_gethlua(L);
1137 if (!hlua) {
1138 return;
1139 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001140
1141 /* Set the wake timeout. If timeout is required, we set
1142 * the expiration time.
1143 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001144 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001145
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001146 hlua->flags |= flags;
1147
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001148 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001149 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001150}
1151
Willy Tarreau87b09662015-04-03 00:22:06 +02001152/* This function initialises the Lua environment stored in the stream.
1153 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001154 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001155 *
1156 * This function is particular. it initialises a new Lua thread. If the
1157 * initialisation fails (example: out of memory error), the lua function
1158 * throws an error (longjmp).
1159 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001160 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001161 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001162 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001163 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 */
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01001165int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166{
1167 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001168 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001169 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001170 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001171 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001172 LIST_INIT(&lua->com);
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01001173 if (!SET_SAFE_LJMP_PARENT(lua)) {
1174 lua->Tref = LUA_REFNIL;
1175 return 0;
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001176 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001177 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001178 if (!lua->T) {
1179 lua->Tref = LUA_REFNIL;
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01001180 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001181 return 0;
1182 }
1183 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001184 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001185 lua->task = task;
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01001186 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001187 return 1;
1188}
1189
Willy Tarreau87b09662015-04-03 00:22:06 +02001190/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001191 * is destroyed. The destroy also the memory context. The struct "lua"
Aurelien DARRAGONd5c8a102023-03-01 16:45:50 +01001192 * will be freed.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193 */
1194void hlua_ctx_destroy(struct hlua *lua)
1195{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001196 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001197 return;
1198
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001199 if (!lua->T)
1200 goto end;
1201
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001202 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001203 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001204
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001205 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001206 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001208 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001209
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001210 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001211 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001212 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001213 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001214 /* Forces a garbage collecting process. If the Lua program is finished
1215 * without error, we run the GC on the thread pointer. Its freed all
1216 * the unused memory.
1217 * If the thread is finnish with an error or is currently yielded,
1218 * it seems that the GC applied on the thread doesn't clean anything,
1219 * so e run the GC on the main thread.
1220 * NOTE: maybe this action locks all the Lua threads untiml the en of
1221 * the garbage collection.
1222 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001223 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001224 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001225 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001226 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001227 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001228 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001229
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001230 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001231
1232end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001233 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001234}
1235
1236/* This function is used to restore the Lua context when a coroutine
1237 * fails. This function copy the common memory between old coroutine
1238 * and the new coroutine. The old coroutine is destroyed, and its
1239 * replaced by the new coroutine.
1240 * If the flag "keep_msg" is set, the last entry of the old is assumed
1241 * as string error message and it is copied in the new stack.
1242 */
1243static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1244{
1245 lua_State *T;
1246 int new_ref;
1247
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001248 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001249 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001250 if (!T)
1251 return 0;
1252
1253 /* Copy last error message. */
1254 if (keep_msg)
1255 lua_xmove(lua->T, T, 1);
1256
1257 /* Copy data between the coroutines. */
1258 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1259 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001260 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001261
1262 /* Destroy old data. */
1263 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1264
1265 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001266 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001267
1268 /* Fill the struct with the new coroutine values. */
1269 lua->Mref = new_ref;
1270 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001271 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001272
1273 /* Set context. */
1274 hlua_sethlua(lua);
1275
1276 return 1;
1277}
1278
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001279void hlua_hook(lua_State *L, lua_Debug *ar)
1280{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001281 struct hlua *hlua;
1282
1283 /* Get hlua struct, or NULL if we execute from main lua state */
1284 hlua = hlua_gethlua(L);
1285 if (!hlua)
1286 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001287
1288 /* Lua cannot yield when its returning from a function,
1289 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001290 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001291 */
1292 if (lua_gethookmask(L) & LUA_MASKRET) {
1293 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1294 return;
1295 }
1296
1297 /* restore the interrupt condition. */
1298 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1299
1300 /* If we interrupt the Lua processing in yieldable state, we yield.
1301 * If the state is not yieldable, trying yield causes an error.
1302 */
1303 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001304 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001305
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001306 /* If we cannot yield, update the clock and check the timeout. */
1307 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001308 hlua->run_time += now_ms - hlua->start_time;
1309 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001310 lua_pushfstring(L, "execution timeout");
1311 WILL_LJMP(lua_error(L));
1312 }
1313
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001314 /* Update the start time. */
1315 hlua->start_time = now_ms;
1316
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001317 /* Try to interrupt the process at the end of the current
1318 * unyieldable function.
1319 */
1320 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001321}
1322
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001323/* This function start or resumes the Lua stack execution. If the flag
1324 * "yield_allowed" if no set and the LUA stack execution returns a yield
1325 * The function return an error.
1326 *
1327 * The function can returns 4 values:
1328 * - HLUA_E_OK : The execution is terminated without any errors.
1329 * - HLUA_E_AGAIN : The execution must continue at the next associated
1330 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001331 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001332 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001333 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001334 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001335 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001336 * LUA code.
1337 */
1338static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1339{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001340#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1341 int nres;
1342#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001343 int ret;
1344 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001345 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001346
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001347 /* Initialise run time counter. */
1348 if (!HLUA_IS_RUNNING(lua))
1349 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001350
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001351 /* Lock the whole Lua execution. This lock must be before the
1352 * label "resume_execution".
1353 */
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +01001354 hlua_lock(lua);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001355
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001356resume_execution:
1357
1358 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1359 * instructions. it is used for preventing infinite loops.
1360 */
1361 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1362
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001363 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001364 HLUA_SET_RUN(lua);
1365 HLUA_CLR_CTRLYIELD(lua);
1366 HLUA_CLR_WAKERESWR(lua);
1367 HLUA_CLR_WAKEREQWR(lua);
Christopher Fauleta6e792f2021-08-04 17:58:21 +02001368 HLUA_CLR_NOYIELD(lua);
1369 if (!yield_allowed)
1370 HLUA_SET_NOYIELD(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001371
Christopher Fauletbc275a92020-02-26 14:55:16 +01001372 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001373 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001374 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001375
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001376 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001377#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001378 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001379#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001380 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001381#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001382 switch (ret) {
1383
1384 case LUA_OK:
1385 ret = HLUA_E_OK;
1386 break;
1387
1388 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001389 /* Check if the execution timeout is expired. It it is the case, we
1390 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001391 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001392 tv_update_date(0, 1);
1393 lua->run_time += now_ms - lua->start_time;
1394 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001395 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001396 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001397 break;
1398 }
1399 /* Process the forced yield. if the general yield is not allowed or
1400 * if no task were associated this the current Lua execution
1401 * coroutine, we resume the execution. Else we want to return in the
1402 * scheduler and we want to be waked up again, to continue the
1403 * current Lua execution. So we schedule our own task.
1404 */
1405 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001406 if (!yield_allowed || !lua->task)
1407 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001408 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001409 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001410 if (!yield_allowed) {
1411 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001412 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001413 break;
1414 }
1415 ret = HLUA_E_AGAIN;
1416 break;
1417
1418 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001419
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001420 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001421 * because the errors ares the only one mean to return immediately
1422 * from and lua execution.
1423 */
1424 if (lua->flags & HLUA_EXIT) {
1425 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001426 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001427 break;
1428 }
1429
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001430 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001431 if (!lua_checkstack(lua->T, 1)) {
1432 ret = HLUA_E_ERR;
1433 break;
1434 }
1435 msg = lua_tostring(lua->T, -1);
1436 lua_settop(lua->T, 0); /* Empty the stack. */
1437 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001438 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001439 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001440 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001441 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001442 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001443 ret = HLUA_E_ERRMSG;
1444 break;
1445
1446 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001447 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001448 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001449 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001450 break;
1451
1452 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001453 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001454 if (!lua_checkstack(lua->T, 1)) {
1455 ret = HLUA_E_ERR;
1456 break;
1457 }
1458 msg = lua_tostring(lua->T, -1);
1459 lua_settop(lua->T, 0); /* Empty the stack. */
1460 lua_pop(lua->T, 1);
1461 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001462 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001463 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001464 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001465 ret = HLUA_E_ERRMSG;
1466 break;
1467
1468 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001469 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001470 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001471 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001472 break;
1473 }
1474
1475 switch (ret) {
1476 case HLUA_E_AGAIN:
1477 break;
1478
1479 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001480 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001481 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001482 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001483 break;
1484
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001485 case HLUA_E_ETMOUT:
1486 case HLUA_E_NOMEM:
1487 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001488 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001489 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001490 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001491 hlua_ctx_renew(lua, 0);
1492 break;
1493
1494 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001495 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001496 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001497 break;
1498 }
1499
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001500 /* This is the main exit point, remove the Lua lock. */
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +01001501 hlua_unlock(lua);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001502
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001503 return ret;
1504}
1505
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001506/* This function exit the current code. */
1507__LJMP static int hlua_done(lua_State *L)
1508{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001509 struct hlua *hlua;
1510
1511 /* Get hlua struct, or NULL if we execute from main lua state */
1512 hlua = hlua_gethlua(L);
1513 if (!hlua)
1514 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001515
1516 hlua->flags |= HLUA_EXIT;
1517 WILL_LJMP(lua_error(L));
1518
1519 return 0;
1520}
1521
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001522/* This function is an LUA binding. It provides a function
1523 * for deleting ACL from a referenced ACL file.
1524 */
1525__LJMP static int hlua_del_acl(lua_State *L)
1526{
1527 const char *name;
1528 const char *key;
1529 struct pat_ref *ref;
1530
1531 MAY_LJMP(check_args(L, 2, "del_acl"));
1532
1533 name = MAY_LJMP(luaL_checkstring(L, 1));
1534 key = MAY_LJMP(luaL_checkstring(L, 2));
1535
1536 ref = pat_ref_lookup(name);
1537 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001538 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001539
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001540 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001541 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001542 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001543 return 0;
1544}
1545
1546/* This function is an LUA binding. It provides a function
1547 * for deleting map entry from a referenced map file.
1548 */
1549static int hlua_del_map(lua_State *L)
1550{
1551 const char *name;
1552 const char *key;
1553 struct pat_ref *ref;
1554
1555 MAY_LJMP(check_args(L, 2, "del_map"));
1556
1557 name = MAY_LJMP(luaL_checkstring(L, 1));
1558 key = MAY_LJMP(luaL_checkstring(L, 2));
1559
1560 ref = pat_ref_lookup(name);
1561 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001562 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001563
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001564 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001565 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001566 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001567 return 0;
1568}
1569
1570/* This function is an LUA binding. It provides a function
1571 * for adding ACL pattern from a referenced ACL file.
1572 */
1573static int hlua_add_acl(lua_State *L)
1574{
1575 const char *name;
1576 const char *key;
1577 struct pat_ref *ref;
1578
1579 MAY_LJMP(check_args(L, 2, "add_acl"));
1580
1581 name = MAY_LJMP(luaL_checkstring(L, 1));
1582 key = MAY_LJMP(luaL_checkstring(L, 2));
1583
1584 ref = pat_ref_lookup(name);
1585 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001586 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001587
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001588 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001589 if (pat_ref_find_elt(ref, key) == NULL)
1590 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001591 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001592 return 0;
1593}
1594
1595/* This function is an LUA binding. It provides a function
1596 * for setting map pattern and sample from a referenced map
1597 * file.
1598 */
1599static int hlua_set_map(lua_State *L)
1600{
1601 const char *name;
1602 const char *key;
1603 const char *value;
1604 struct pat_ref *ref;
1605
1606 MAY_LJMP(check_args(L, 3, "set_map"));
1607
1608 name = MAY_LJMP(luaL_checkstring(L, 1));
1609 key = MAY_LJMP(luaL_checkstring(L, 2));
1610 value = MAY_LJMP(luaL_checkstring(L, 3));
1611
1612 ref = pat_ref_lookup(name);
1613 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001614 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001615
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001616 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001617 if (pat_ref_find_elt(ref, key) != NULL)
1618 pat_ref_set(ref, key, value, NULL);
1619 else
1620 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001621 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001622 return 0;
1623}
1624
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001625/* A class is a lot of memory that contain data. This data can be a table,
1626 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001627 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001628 * the name of the object (_G[<name>] = <metable> ).
1629 *
1630 * A metable is a table that modify the standard behavior of a standard
1631 * access to the associated data. The entries of this new metatable are
1632 * defined as is:
1633 *
1634 * http://lua-users.org/wiki/MetatableEvents
1635 *
1636 * __index
1637 *
1638 * we access an absent field in a table, the result is nil. This is
1639 * true, but it is not the whole truth. Actually, such access triggers
1640 * the interpreter to look for an __index metamethod: If there is no
1641 * such method, as usually happens, then the access results in nil;
1642 * otherwise, the metamethod will provide the result.
1643 *
1644 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1645 * the key does not appear in the table, but the metatable has an __index
1646 * property:
1647 *
1648 * - if the value is a function, the function is called, passing in the
1649 * table and the key; the return value of that function is returned as
1650 * the result.
1651 *
1652 * - if the value is another table, the value of the key in that table is
1653 * asked for and returned (and if it doesn't exist in that table, but that
1654 * table's metatable has an __index property, then it continues on up)
1655 *
1656 * - Use "rawget(myTable,key)" to skip this metamethod.
1657 *
1658 * http://www.lua.org/pil/13.4.1.html
1659 *
1660 * __newindex
1661 *
1662 * Like __index, but control property assignment.
1663 *
1664 * __mode - Control weak references. A string value with one or both
1665 * of the characters 'k' and 'v' which specifies that the the
1666 * keys and/or values in the table are weak references.
1667 *
1668 * __call - Treat a table like a function. When a table is followed by
1669 * parenthesis such as "myTable( 'foo' )" and the metatable has
1670 * a __call key pointing to a function, that function is invoked
1671 * (passing any specified arguments) and the return value is
1672 * returned.
1673 *
1674 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1675 * called, if the metatable for myTable has a __metatable
1676 * key, the value of that key is returned instead of the
1677 * actual metatable.
1678 *
1679 * __tostring - Control string representation. When the builtin
1680 * "tostring( myTable )" function is called, if the metatable
1681 * for myTable has a __tostring property set to a function,
1682 * that function is invoked (passing myTable to it) and the
1683 * return value is used as the string representation.
1684 *
1685 * __len - Control table length. When the table length is requested using
1686 * the length operator ( '#' ), if the metatable for myTable has
1687 * a __len key pointing to a function, that function is invoked
1688 * (passing myTable to it) and the return value used as the value
1689 * of "#myTable".
1690 *
1691 * __gc - Userdata finalizer code. When userdata is set to be garbage
1692 * collected, if the metatable has a __gc field pointing to a
1693 * function, that function is first invoked, passing the userdata
1694 * to it. The __gc metamethod is not called for tables.
1695 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1696 *
1697 * Special metamethods for redefining standard operators:
1698 * http://www.lua.org/pil/13.1.html
1699 *
1700 * __add "+"
1701 * __sub "-"
1702 * __mul "*"
1703 * __div "/"
1704 * __unm "!"
1705 * __pow "^"
1706 * __concat ".."
1707 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001708 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001709 * http://www.lua.org/pil/13.2.html
1710 *
1711 * __eq "=="
1712 * __lt "<"
1713 * __le "<="
1714 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001715
1716/*
1717 *
1718 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001719 * Class Map
1720 *
1721 *
1722 */
1723
1724/* Returns a struct hlua_map if the stack entry "ud" is
1725 * a class session, otherwise it throws an error.
1726 */
1727__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1728{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001729 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001730}
1731
1732/* This function is the map constructor. It don't need
1733 * the class Map object. It creates and return a new Map
1734 * object. It must be called only during "body" or "init"
1735 * context because it process some filesystem accesses.
1736 */
1737__LJMP static int hlua_map_new(struct lua_State *L)
1738{
1739 const char *fn;
1740 int match = PAT_MATCH_STR;
1741 struct sample_conv conv;
1742 const char *file = "";
1743 int line = 0;
1744 lua_Debug ar;
1745 char *err = NULL;
1746 struct arg args[2];
1747
1748 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1749 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1750
1751 fn = MAY_LJMP(luaL_checkstring(L, 1));
1752
1753 if (lua_gettop(L) >= 2) {
1754 match = MAY_LJMP(luaL_checkinteger(L, 2));
1755 if (match < 0 || match >= PAT_MATCH_NUM)
1756 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1757 }
1758
1759 /* Get Lua filename and line number. */
1760 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1761 lua_getinfo(L, "Sl", &ar); /* get info about it */
1762 if (ar.currentline > 0) { /* is there info? */
1763 file = ar.short_src;
1764 line = ar.currentline;
1765 }
1766 }
1767
1768 /* fill fake sample_conv struct. */
1769 conv.kw = ""; /* unused. */
1770 conv.process = NULL; /* unused. */
1771 conv.arg_mask = 0; /* unused. */
1772 conv.val_args = NULL; /* unused. */
1773 conv.out_type = SMP_T_STR;
1774 conv.private = (void *)(long)match;
1775 switch (match) {
1776 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1777 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1778 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1779 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1780 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1781 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1782 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001783 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001784 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1785 default:
1786 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1787 }
1788
1789 /* fill fake args. */
1790 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001791 args[0].data.str.area = strdup(fn);
1792 args[0].data.str.data = strlen(fn);
1793 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001794 args[1].type = ARGT_STOP;
1795
1796 /* load the map. */
1797 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001798 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001799 * free the err variable.
1800 */
1801 luaL_where(L, 1);
1802 lua_pushfstring(L, "'new': %s.", err);
1803 lua_concat(L, 2);
1804 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001805 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001806 WILL_LJMP(lua_error(L));
1807 }
1808
1809 /* create the lua object. */
1810 lua_newtable(L);
1811 lua_pushlightuserdata(L, args[0].data.map);
1812 lua_rawseti(L, -2, 0);
1813
1814 /* Pop a class Map metatable and affect it to the userdata. */
1815 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1816 lua_setmetatable(L, -2);
1817
1818
1819 return 1;
1820}
1821
1822__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1823{
1824 struct map_descriptor *desc;
1825 struct pattern *pat;
1826 struct sample smp;
1827
1828 MAY_LJMP(check_args(L, 2, "lookup"));
1829 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001830 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001831 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001832 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001833 }
1834 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001835 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001836 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001837 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 +01001838 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001839 }
1840
1841 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001842 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001843 if (str)
1844 lua_pushstring(L, "");
1845 else
1846 lua_pushnil(L);
1847 return 1;
1848 }
1849
1850 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001851 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001852 return 1;
1853}
1854
1855__LJMP static int hlua_map_lookup(struct lua_State *L)
1856{
1857 return _hlua_map_lookup(L, 0);
1858}
1859
1860__LJMP static int hlua_map_slookup(struct lua_State *L)
1861{
1862 return _hlua_map_lookup(L, 1);
1863}
1864
1865/*
1866 *
1867 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868 * Class Socket
1869 *
1870 *
1871 */
1872
1873__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1874{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001875 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001876}
1877
1878/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001879 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 * received.
1881 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001882static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001884 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001886 if (appctx->ctx.hlua_cosocket.die) {
1887 si_shutw(si);
1888 si_shutr(si);
1889 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001890 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1891 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001892 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1893 }
1894
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001895 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001896 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001897 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001898
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001899 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001900 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001901 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001902
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001903 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001904 * to be notified whenever the connection completes.
1905 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001906 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001907 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001908 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001909 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001910 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001911 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001912
1913 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001914 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001915
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001916 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001917 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001918 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001919
1920 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001921 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001922 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001923
1924 /* Some data were injected in the buffer, notify the stream
1925 * interface.
1926 */
1927 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001928 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001929
1930 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001931 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001932 */
1933 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001934 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001935}
1936
Willy Tarreau87b09662015-04-03 00:22:06 +02001937/* This function is called when the "struct stream" is destroyed.
1938 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001939 * Wake all the pending signals.
1940 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001941static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001942{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001943 struct xref *peer;
1944
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001945 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001946 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1947 if (peer)
1948 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001949
1950 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001951 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1952 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001953}
1954
1955/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001956 * uses this object. If the stream does not exists, just quit.
1957 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958 * pending signal can rest in the read and write lists. destroy
1959 * it.
1960 */
1961__LJMP static int hlua_socket_gc(lua_State *L)
1962{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001963 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001964 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001965 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001966
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001967 MAY_LJMP(check_args(L, 1, "__gc"));
1968
1969 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001970 peer = xref_get_peer_and_lock(&socket->xref);
1971 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001972 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001973 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001975 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001976 appctx->ctx.hlua_cosocket.die = 1;
1977 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001978
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001979 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001980 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001981 return 0;
1982}
1983
1984/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001985 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986 */
sada05ed3302018-05-11 11:48:18 -07001987__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001989 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001990 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001991 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001992 struct hlua *hlua;
1993
1994 /* Get hlua struct, or NULL if we execute from main lua state */
1995 hlua = hlua_gethlua(L);
1996 if (!hlua)
1997 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001998
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001999 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002000
2001 /* Check if we run on the same thread than the xreator thread.
2002 * We cannot access to the socket if the thread is different.
2003 */
2004 if (socket->tid != tid)
2005 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2006
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002007 peer = xref_get_peer_and_lock(&socket->xref);
2008 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002009 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01002010
2011 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002012 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002013
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002014 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002015 appctx->ctx.hlua_cosocket.die = 1;
2016 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002018 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002019 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002020 return 0;
2021}
2022
sada05ed3302018-05-11 11:48:18 -07002023/* The close function calls close_helper.
2024 */
2025__LJMP static int hlua_socket_close(lua_State *L)
2026{
2027 MAY_LJMP(check_args(L, 1, "close"));
2028 return hlua_socket_close_helper(L);
2029}
2030
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002031/* This Lua function assumes that the stack contain three parameters.
2032 * 1 - USERDATA containing a struct socket
2033 * 2 - INTEGER with values of the macro defined below
2034 * If the integer is -1, we must read at most one line.
2035 * If the integer is -2, we ust read all the data until the
2036 * end of the stream.
2037 * If the integer is positive value, we must read a number of
2038 * bytes corresponding to this value.
2039 */
2040#define HLSR_READ_LINE (-1)
2041#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002042__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043{
2044 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2045 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002046 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002047 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002048 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02002050 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002051 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02002052 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002053 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002054 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01002055 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002056 struct stream_interface *si;
2057 struct stream *s;
2058 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002059 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002061 /* Get hlua struct, or NULL if we execute from main lua state */
2062 hlua = hlua_gethlua(L);
2063
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002064 /* Check if this lua stack is schedulable. */
2065 if (!hlua || !hlua->task)
2066 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
2067 "'frontend', 'backend' or 'task'"));
2068
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002069 /* Check if we run on the same thread than the xreator thread.
2070 * We cannot access to the socket if the thread is different.
2071 */
2072 if (socket->tid != tid)
2073 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2074
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002075 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002076 peer = xref_get_peer_and_lock(&socket->xref);
2077 if (!peer)
2078 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002079 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2080 si = appctx->owner;
2081 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002083 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002085 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002086 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087 if (nblk < 0) /* Connection close. */
2088 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002089 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002091
2092 /* remove final \r\n. */
2093 if (nblk == 1) {
2094 if (blk1[len1-1] == '\n') {
2095 len1--;
2096 skip_at_end++;
2097 if (blk1[len1-1] == '\r') {
2098 len1--;
2099 skip_at_end++;
2100 }
2101 }
2102 }
2103 else {
2104 if (blk2[len2-1] == '\n') {
2105 len2--;
2106 skip_at_end++;
2107 if (blk2[len2-1] == '\r') {
2108 len2--;
2109 skip_at_end++;
2110 }
2111 }
2112 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002113 }
2114
2115 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002117 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002118 if (nblk < 0) /* Connection close. */
2119 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002120 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002121 goto connection_empty;
2122 }
2123
2124 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002125 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002126 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127 if (nblk < 0) /* Connection close. */
2128 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002129 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002130 goto connection_empty;
2131
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002132 missing_bytes = wanted - socket->b.n;
2133 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002134 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002135 len1 = missing_bytes;
2136 } if (nblk == 2 && len1 + len2 > missing_bytes)
2137 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002138 }
2139
2140 len = len1;
2141
2142 luaL_addlstring(&socket->b, blk1, len1);
2143 if (nblk == 2) {
2144 len += len2;
2145 luaL_addlstring(&socket->b, blk2, len2);
2146 }
2147
2148 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002149 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002150
2151 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002152 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002153
2154 /* If the pattern reclaim to read all the data
2155 * in the connection, got out.
2156 */
2157 if (wanted == HLSR_READ_ALL)
2158 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002159 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002160 goto connection_empty;
2161
2162 /* Return result. */
2163 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002164 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002165 return 1;
2166
2167connection_closed:
2168
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002169 xref_unlock(&socket->xref, peer);
2170
2171no_peer:
2172
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173 /* If the buffer containds data. */
2174 if (socket->b.n > 0) {
2175 luaL_pushresult(&socket->b);
2176 return 1;
2177 }
2178 lua_pushnil(L);
2179 lua_pushstring(L, "connection closed.");
2180 return 2;
2181
2182connection_empty:
2183
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002184 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2185 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002186 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002187 }
2188 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002189 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002190 return 0;
2191}
2192
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002193/* This Lua function gets two parameters. The first one can be string
2194 * or a number. If the string is "*l", the user requires one line. If
2195 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002196 * If the value is a number, the user require a number of bytes equal
2197 * to the value. The default value is "*l" (a line).
2198 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002199 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002200 * integer takes this values:
2201 * -1 : read a line
2202 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002203 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002205 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002206 * concatenated with the read data.
2207 */
2208__LJMP static int hlua_socket_receive(struct lua_State *L)
2209{
2210 int wanted = HLSR_READ_LINE;
2211 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002212 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002213 char *error;
2214 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002215 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002216
2217 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2218 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2219
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002220 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002221
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002222 /* Check if we run on the same thread than the xreator thread.
2223 * We cannot access to the socket if the thread is different.
2224 */
2225 if (socket->tid != tid)
2226 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2227
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 /* check for pattern. */
2229 if (lua_gettop(L) >= 2) {
2230 type = lua_type(L, 2);
2231 if (type == LUA_TSTRING) {
2232 pattern = lua_tostring(L, 2);
2233 if (strcmp(pattern, "*a") == 0)
2234 wanted = HLSR_READ_ALL;
2235 else if (strcmp(pattern, "*l") == 0)
2236 wanted = HLSR_READ_LINE;
2237 else {
2238 wanted = strtoll(pattern, &error, 10);
2239 if (*error != '\0')
2240 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2241 }
2242 }
2243 else if (type == LUA_TNUMBER) {
2244 wanted = lua_tointeger(L, 2);
2245 if (wanted < 0)
2246 WILL_LJMP(luaL_error(L, "Unsupported size."));
2247 }
2248 }
2249
2250 /* Set pattern. */
2251 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002252
2253 /* Check if we would replace the top by itself. */
2254 if (lua_gettop(L) != 2)
2255 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256
Christopher Fauletc31b2002021-05-03 10:11:13 +02002257 /* Save index of the top of the stack because since buffers are used, it
2258 * may change
2259 */
2260 lastarg = lua_gettop(L);
2261
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002262 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263 luaL_buffinit(L, &socket->b);
2264
2265 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002266 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267 if (lua_type(L, 3) != LUA_TSTRING)
2268 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2269 pattern = lua_tolstring(L, 3, &len);
2270 luaL_addlstring(&socket->b, pattern, len);
2271 }
2272
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002273 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002274}
2275
2276/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002277 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002279static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280{
2281 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002282 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002283 struct appctx *appctx;
2284 size_t buf_len;
2285 const char *buf;
2286 int len;
2287 int send_len;
2288 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002289 struct xref *peer;
2290 struct stream_interface *si;
2291 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002293 /* Get hlua struct, or NULL if we execute from main lua state */
2294 hlua = hlua_gethlua(L);
2295
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002296 /* Check if this lua stack is schedulable. */
2297 if (!hlua || !hlua->task)
2298 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2299 "'frontend', 'backend' or 'task'"));
2300
2301 /* Get object */
2302 socket = MAY_LJMP(hlua_checksocket(L, 1));
2303 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002304 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002306 /* Check if we run on the same thread than the xreator thread.
2307 * We cannot access to the socket if the thread is different.
2308 */
2309 if (socket->tid != tid)
2310 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2311
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002312 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002313 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002314 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002315 lua_pushinteger(L, -1);
2316 return 1;
2317 }
2318 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2319 si = appctx->owner;
2320 s = si_strm(si);
2321
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002323 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002324 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002325 lua_pushinteger(L, -1);
2326 return 1;
2327 }
2328
2329 /* Update the input buffer data. */
2330 buf += sent;
2331 send_len = buf_len - sent;
2332
2333 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002334 if (sent >= buf_len) {
2335 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002336 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002337 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002338
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002339 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002340 * the request buffer if its not required.
2341 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002342 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002343 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002344 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002345 }
2346
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002347 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002348 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002349 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002351 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352
2353 /* send data */
2354 if (len < send_len)
2355 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002356 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002357
2358 /* "Not enough space" (-1), "Buffer too little to contain
2359 * the data" (-2) are not expected because the available length
2360 * is tested.
2361 * Other unknown error are also not expected.
2362 */
2363 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002364 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002365 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002366
sada05ed3302018-05-11 11:48:18 -07002367 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002369 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002370 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 return 1;
2372 }
2373
2374 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002375 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002376
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002377 s->req.rex = TICK_ETERNITY;
2378 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002379
2380 /* Update length sent. */
2381 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002382 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002383
2384 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002385 if (sent + len >= buf_len) {
2386 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002388 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389
2390hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002391 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2392 xref_unlock(&socket->xref, peer);
2393 WILL_LJMP(luaL_error(L, "out of memory"));
2394 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002395 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002396 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002397 return 0;
2398}
2399
2400/* This function initiate the send of data. It just check the input
2401 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002402 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403 * "hlua_socket_write_yield" that can yield.
2404 *
2405 * The Lua function gets between 3 and 4 parameters. The first one is
2406 * the associated object. The second is a string buffer. The third is
2407 * a facultative integer that represents where is the buffer position
2408 * of the start of the data that can send. The first byte is the
2409 * position "1". The default value is "1". The fourth argument is a
2410 * facultative integer that represents where is the buffer position
2411 * of the end of the data that can send. The default is the last byte.
2412 */
2413static int hlua_socket_send(struct lua_State *L)
2414{
2415 int i;
2416 int j;
2417 const char *buf;
2418 size_t buf_len;
2419
2420 /* Check number of arguments. */
2421 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2422 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2423
2424 /* Get the string. */
2425 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2426
2427 /* Get and check j. */
2428 if (lua_gettop(L) == 4) {
2429 j = MAY_LJMP(luaL_checkinteger(L, 4));
2430 if (j < 0)
2431 j = buf_len + j + 1;
2432 if (j > buf_len)
2433 j = buf_len + 1;
2434 lua_pop(L, 1);
2435 }
2436 else
2437 j = buf_len;
2438
2439 /* Get and check i. */
2440 if (lua_gettop(L) == 3) {
2441 i = MAY_LJMP(luaL_checkinteger(L, 3));
2442 if (i < 0)
2443 i = buf_len + i + 1;
2444 if (i > buf_len)
2445 i = buf_len + 1;
2446 lua_pop(L, 1);
2447 } else
2448 i = 1;
2449
2450 /* Check bth i and j. */
2451 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002452 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002453 return 1;
2454 }
2455 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002456 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002457 return 1;
2458 }
2459 if (i == 0)
2460 i = 1;
2461 if (j == 0)
2462 j = 1;
2463
2464 /* Pop the string. */
2465 lua_pop(L, 1);
2466
2467 /* Update the buffer length. */
2468 buf += i - 1;
2469 buf_len = j - i + 1;
2470 lua_pushlstring(L, buf, buf_len);
2471
2472 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002473 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002474
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002475 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002476}
2477
Willy Tarreau22b0a682015-06-17 19:43:49 +02002478#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002479__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2480{
2481 static char buffer[SOCKET_INFO_MAX_LEN];
2482 int ret;
2483 int len;
2484 char *p;
2485
2486 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2487 if (ret <= 0) {
2488 lua_pushnil(L);
2489 return 1;
2490 }
2491
2492 if (ret == AF_UNIX) {
2493 lua_pushstring(L, buffer+1);
2494 return 1;
2495 }
2496 else if (ret == AF_INET6) {
2497 buffer[0] = '[';
2498 len = strlen(buffer);
2499 buffer[len] = ']';
2500 len++;
2501 buffer[len] = ':';
2502 len++;
2503 p = buffer;
2504 }
2505 else if (ret == AF_INET) {
2506 p = buffer + 1;
2507 len = strlen(p);
2508 p[len] = ':';
2509 len++;
2510 }
2511 else {
2512 lua_pushnil(L);
2513 return 1;
2514 }
2515
2516 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2517 lua_pushnil(L);
2518 return 1;
2519 }
2520
2521 lua_pushstring(L, p);
2522 return 1;
2523}
2524
2525/* Returns information about the peer of the connection. */
2526__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2527{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002528 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002529 struct xref *peer;
2530 struct appctx *appctx;
2531 struct stream_interface *si;
2532 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002533 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002534
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002535 MAY_LJMP(check_args(L, 1, "getpeername"));
2536
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002537 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002539 /* Check if we run on the same thread than the xreator thread.
2540 * We cannot access to the socket if the thread is different.
2541 */
2542 if (socket->tid != tid)
2543 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2544
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002545 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002546 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002547 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002548 lua_pushnil(L);
2549 return 1;
2550 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002551 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2552 si = appctx->owner;
2553 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002554
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002555 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002556 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002557 lua_pushnil(L);
2558 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002559 }
2560
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002561 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002562 xref_unlock(&socket->xref, peer);
2563 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002564}
2565
2566/* Returns information about my connection side. */
2567static int hlua_socket_getsockname(struct lua_State *L)
2568{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002569 struct hlua_socket *socket;
2570 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002571 struct appctx *appctx;
2572 struct xref *peer;
2573 struct stream_interface *si;
2574 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002575 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002576
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002577 MAY_LJMP(check_args(L, 1, "getsockname"));
2578
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002579 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002580
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002581 /* Check if we run on the same thread than the xreator thread.
2582 * We cannot access to the socket if the thread is different.
2583 */
2584 if (socket->tid != tid)
2585 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2586
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002587 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002588 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002589 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002590 lua_pushnil(L);
2591 return 1;
2592 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002593 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2594 si = appctx->owner;
2595 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002597 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002598 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002599 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002600 lua_pushnil(L);
2601 return 1;
2602 }
2603
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002604 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002605 xref_unlock(&socket->xref, peer);
2606 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002607}
2608
2609/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002610static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002611 .obj_type = OBJ_TYPE_APPLET,
2612 .name = "<LUA_TCP>",
2613 .fct = hlua_socket_handler,
2614 .release = hlua_socket_release,
2615};
2616
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002617__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618{
2619 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002620 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002621 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002623 struct stream_interface *si;
2624 struct stream *s;
2625
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002626 /* Get hlua struct, or NULL if we execute from main lua state */
2627 hlua = hlua_gethlua(L);
2628 if (!hlua)
2629 return 0;
2630
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002631 /* Check if we run on the same thread than the xreator thread.
2632 * We cannot access to the socket if the thread is different.
2633 */
2634 if (socket->tid != tid)
2635 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2636
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002637 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002638 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002639 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002640 lua_pushnil(L);
2641 lua_pushstring(L, "Can't connect");
2642 return 2;
2643 }
2644 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2645 si = appctx->owner;
2646 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002648 /* Check if we run on the same thread than the xreator thread.
2649 * We cannot access to the socket if the thread is different.
2650 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002651 if (socket->tid != tid) {
2652 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002653 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002654 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002655
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002656 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002657 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002658 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659 lua_pushnil(L);
2660 lua_pushstring(L, "Can't connect");
2661 return 2;
2662 }
2663
Willy Tarreaue09101e2018-10-16 17:37:12 +02002664 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002665
2666 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002667 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002668 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002669 lua_pushinteger(L, 1);
2670 return 1;
2671 }
2672
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002673 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2674 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002675 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002676 }
2677 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002678 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002679 return 0;
2680}
2681
2682/* This function fail or initite the connection. */
2683__LJMP static int hlua_socket_connect(struct lua_State *L)
2684{
2685 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002686 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002687 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002688 struct hlua *hlua;
2689 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002690 int low, high;
2691 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002692 struct xref *peer;
2693 struct stream_interface *si;
2694 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002695
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002696 if (lua_gettop(L) < 2)
2697 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002698
2699 /* Get args. */
2700 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002701
2702 /* Check if we run on the same thread than the xreator thread.
2703 * We cannot access to the socket if the thread is different.
2704 */
2705 if (socket->tid != tid)
2706 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2707
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002708 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002709 if (lua_gettop(L) >= 3) {
2710 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002711 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002712
Tim Duesterhus6edab862018-01-06 19:04:45 +01002713 /* Force the ip to end with a colon, to support IPv6 addresses
2714 * that are not enclosed within square brackets.
2715 */
2716 if (port > 0) {
2717 luaL_buffinit(L, &b);
2718 luaL_addstring(&b, ip);
2719 luaL_addchar(&b, ':');
2720 luaL_pushresult(&b);
2721 ip = lua_tolstring(L, lua_gettop(L), NULL);
2722 }
2723 }
2724
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002725 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002726 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002727 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002728 lua_pushnil(L);
2729 return 1;
2730 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002731
2732 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002733 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 +02002734 if (!addr) {
2735 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002736 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002737 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002738
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002739 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002740 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002741 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002742 if (port == -1) {
2743 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002744 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002745 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002746 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2747 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002748 if (port == -1) {
2749 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002750 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002751 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002752 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002753 }
2754 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002755
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002756 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2757 si = appctx->owner;
2758 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002759
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002760 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002761 xref_unlock(&socket->xref, peer);
2762 WILL_LJMP(luaL_error(L, "connect: internal error"));
2763 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002764 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002765
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002766 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002767 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002768 if (!hlua)
2769 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002770
2771 /* inform the stream that we want to be notified whenever the
2772 * connection completes.
2773 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002774 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002775 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002776 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002777
Willy Tarreauf31af932020-01-14 09:59:38 +01002778 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002779
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002780 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2781 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002782 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002783 }
2784 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002785
2786 task_wakeup(s->task, TASK_WOKEN_INIT);
2787 /* Return yield waiting for connection. */
2788
Willy Tarreau9635e032018-10-16 17:52:55 +02002789 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002790
2791 return 0;
2792}
2793
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002794#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002795__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2796{
2797 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002798 struct xref *peer;
2799 struct appctx *appctx;
2800 struct stream_interface *si;
2801 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002802
2803 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2804 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002805
2806 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002807 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002808 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002809 lua_pushnil(L);
2810 return 1;
2811 }
2812 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2813 si = appctx->owner;
2814 s = si_strm(si);
2815
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002816 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002817 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002818 return MAY_LJMP(hlua_socket_connect(L));
2819}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002820#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002821
2822__LJMP static int hlua_socket_setoption(struct lua_State *L)
2823{
2824 return 0;
2825}
2826
2827__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2828{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002829 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002830 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002831 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002832 struct xref *peer;
2833 struct appctx *appctx;
2834 struct stream_interface *si;
2835 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002836
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002837 MAY_LJMP(check_args(L, 2, "settimeout"));
2838
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002839 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002840
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002841 /* convert the timeout to millis */
2842 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002843
Thierry Fournier17a921b2018-03-08 09:59:02 +01002844 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002845 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002846 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2847
Mark Lakes56cc1252018-03-27 09:48:06 +02002848 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002849 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002850
2851 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002852 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002853 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002854
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002855 /* Check if we run on the same thread than the xreator thread.
2856 * We cannot access to the socket if the thread is different.
2857 */
2858 if (socket->tid != tid)
2859 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2860
Mark Lakes56cc1252018-03-27 09:48:06 +02002861 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002862 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002863 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002864 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2865 WILL_LJMP(lua_error(L));
2866 return 0;
2867 }
2868 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2869 si = appctx->owner;
2870 s = si_strm(si);
2871
Cyril Bonté7bb63452018-08-17 23:51:02 +02002872 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002873 s->req.rto = tmout;
2874 s->req.wto = tmout;
2875 s->res.rto = tmout;
2876 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002877 s->req.rex = tick_add_ifset(now_ms, tmout);
2878 s->req.wex = tick_add_ifset(now_ms, tmout);
2879 s->res.rex = tick_add_ifset(now_ms, tmout);
2880 s->res.wex = tick_add_ifset(now_ms, tmout);
2881
2882 s->task->expire = tick_add_ifset(now_ms, tmout);
2883 task_queue(s->task);
2884
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002885 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002886
Thierry Fourniere9636f12018-03-08 09:54:32 +01002887 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002888 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002889}
2890
2891__LJMP static int hlua_socket_new(lua_State *L)
2892{
2893 struct hlua_socket *socket;
2894 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002895 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002896 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002897
2898 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002899 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002900 hlua_pusherror(L, "socket: full stack");
2901 goto out_fail_conf;
2902 }
2903
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002904 /* Create the object: obj[0] = userdata. */
2905 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002906 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002907 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002908 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002909 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002910
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002911 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002912 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002913 hlua_pusherror(L, "socket: uninitialized pools.");
2914 goto out_fail_conf;
2915 }
2916
Willy Tarreau87b09662015-04-03 00:22:06 +02002917 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002918 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2919 lua_setmetatable(L, -2);
2920
Willy Tarreaud420a972015-04-06 00:39:18 +02002921 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002922 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002923 if (!appctx) {
2924 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002925 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002926 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002927
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002928 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002929 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002930 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2931 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002932
Willy Tarreaud420a972015-04-06 00:39:18 +02002933 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002934 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002935 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002936 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002937 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002938 }
2939
Christopher Faulet26256f82020-09-14 11:40:13 +02002940 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002941 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002942 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002943 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002944 }
2945
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002946 /* Initialise cross reference between stream and Lua socket object. */
2947 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002948
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002949 /* Configure "right" stream interface. this "si" is used to connect
2950 * and retrieve data from the server. The connection is initialized
2951 * with the "struct server".
2952 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002953 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002954
2955 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002956 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002957 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002958
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002959 return 1;
2960
Willy Tarreaud420a972015-04-06 00:39:18 +02002961 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002962 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002963 out_fail_sess:
2964 appctx_free(appctx);
2965 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002966 WILL_LJMP(lua_error(L));
2967 return 0;
2968}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002969
2970/*
2971 *
2972 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002973 * Class Channel
2974 *
2975 *
2976 */
2977
2978/* Returns the struct hlua_channel join to the class channel in the
2979 * stack entry "ud" or throws an argument error.
2980 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002981__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002982{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002983 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002984}
2985
Willy Tarreau47860ed2015-03-10 14:07:50 +01002986/* Pushes the channel onto the top of the stack. If the stask does not have a
2987 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002988 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002989static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002991 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002992 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993 return 0;
2994
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002995 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002996 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002997 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998
2999 /* Pop a class sesison metatable and affect it to the userdata. */
3000 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
3001 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002 return 1;
3003}
3004
3005/* Duplicate all the data present in the input channel and put it
3006 * in a string LUA variables. Returns -1 and push a nil value in
3007 * the stack if the channel is closed and all the data are consumed,
3008 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05003009 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003011static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003012{
3013 char *blk1;
3014 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003015 size_t len1;
3016 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003017 int ret;
3018 luaL_Buffer b;
3019
Willy Tarreau06d80a92017-10-19 14:32:15 +02003020 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02003021 if (unlikely(ret <= 0)) {
3022 if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) {
3023 lua_pushnil(L);
3024 return -1;
3025 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 return 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 }
3028
3029 luaL_buffinit(L, &b);
3030 luaL_addlstring(&b, blk1, len1);
3031 if (unlikely(ret == 2))
3032 luaL_addlstring(&b, blk2, len2);
3033 luaL_pushresult(&b);
3034
3035 if (unlikely(ret == 2))
3036 return len1 + len2;
3037 return len1;
3038}
3039
3040/* "_hlua_channel_dup" wrapper. If no data are available, it returns
3041 * a yield. This function keep the data in the buffer.
3042 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003043__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003044{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003045 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003046
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003047 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3048
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003049 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003050 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003051 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003052 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003053
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02003055 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003056 return 1;
3057}
3058
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003059/* Check arguments for the function "hlua_channel_dup_yield". */
3060__LJMP static int hlua_channel_dup(lua_State *L)
3061{
3062 MAY_LJMP(check_args(L, 1, "dup"));
3063 MAY_LJMP(hlua_checkchannel(L, 1));
3064 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
3065}
3066
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003067/* "_hlua_channel_dup" wrapper. If no data are available, it returns
3068 * a yield. This function consumes the data in the buffer. It returns
3069 * a string containing the data or a nil pointer if no data are available
3070 * and the channel is closed.
3071 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003072__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003074 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003075 int ret;
3076
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003077 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003078
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003079 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003080 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003081 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003082 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003083
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003084 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003085 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02003086 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003087
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003088 if (unlikely(ret == -1))
3089 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003090
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003091 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003092 return 1;
3093}
3094
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003095/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003096__LJMP static int hlua_channel_get(lua_State *L)
3097{
3098 MAY_LJMP(check_args(L, 1, "get"));
3099 MAY_LJMP(hlua_checkchannel(L, 1));
3100 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3101}
3102
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103/* This functions consumes and returns one line. If the channel is closed,
3104 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003105 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003106 * value.
3107 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003108__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003109{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003110 char *blk1;
3111 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003112 size_t len1;
3113 size_t len2;
3114 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003115 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003116 int ret;
3117 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003118
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003119 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3120
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003121 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003122 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003123 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003124 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003125
Willy Tarreau06d80a92017-10-19 14:32:15 +02003126 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02003127 if (ret == 0) {
3128 if (HLUA_CANT_YIELD(hlua_gethlua(L))) {
3129 _hlua_channel_dup(chn, L);
3130 return 1;
3131 }
Willy Tarreau9635e032018-10-16 17:52:55 +02003132 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet055310f2021-08-05 11:58:37 +02003133 }
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003134
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003135 if (ret == -1) {
3136 lua_pushnil(L);
3137 return 1;
3138 }
3139
3140 luaL_buffinit(L, &b);
3141 luaL_addlstring(&b, blk1, len1);
3142 len = len1;
3143 if (unlikely(ret == 2)) {
3144 luaL_addlstring(&b, blk2, len2);
3145 len += len2;
3146 }
3147 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003148 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003149 return 1;
3150}
3151
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003152/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003153__LJMP static int hlua_channel_getline(lua_State *L)
3154{
3155 MAY_LJMP(check_args(L, 1, "getline"));
3156 MAY_LJMP(hlua_checkchannel(L, 1));
3157 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3158}
3159
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003160/* This function takes a string as argument, and append it at the input side of
3161 * channel. If data cannot be copied, because there is not enough space to do so
3162 * or because the channel is closed, it returns -1. Otherwise, it returns the
3163 * amount of data written (the string length). This function does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164 */
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003165__LJMP static int hlua_channel_append(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003166{
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003167 struct channel *chn;
3168 const char *str;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003169 size_t len;
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003170
3171 MAY_LJMP(check_args(L, 2, "append"));
3172 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3173 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003174
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003175 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003176 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003177 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003178 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003179
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003180 if (len > c_room(chn) || ci_putblk(chn, str, len) < 0)
3181 lua_pushinteger(L, -1);
3182 else
3183 lua_pushinteger(L, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003184 return 1;
3185}
3186
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003187/* The function replaces input data from the channel by the string passed as
3188 * argument. Before clearing the buffer, we take care the copy will be
3189 * possible. It returns the amount of data written (the string length) on
3190 * success or -1 if the copy is not performed. This function does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003191 */
3192__LJMP static int hlua_channel_set(lua_State *L)
3193{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003194 struct channel *chn;
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003195 const char *str;
3196 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003197
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003198 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003199 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003200 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003201
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003202 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003203 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003204 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003205 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003206
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003207 /* Be sure we can copied the string once input data will be removed. */
3208 if (len > ci_data(chn) || channel_input_closed(chn))
3209 lua_pushinteger(L, -1);
3210 else {
3211 b_set_data(&chn->buf, co_data(chn));
3212 if (ci_putblk(chn, str, len) < 0)
3213 lua_pushinteger(L, -1);
3214 else
3215 lua_pushinteger(L, len);
3216 }
3217 return -1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003218}
3219
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003220/* Append data in the output side of the buffer. This data is immediately
3221 * sent. The function returns the amount of data written. If the buffer
3222 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003223 * if the channel is closed.
3224 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003225__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003226{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003227 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003228 size_t len;
3229 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3230 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3231 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003232 struct hlua *hlua;
3233
3234 /* Get hlua struct, or NULL if we execute from main lua state */
3235 hlua = hlua_gethlua(L);
3236 if (!hlua) {
3237 lua_pushnil(L);
3238 return 1;
3239 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003240
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003241 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003242 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003243 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003244 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003245
Willy Tarreau47860ed2015-03-10 14:07:50 +01003246 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003247 lua_pushinteger(L, -1);
3248 return 1;
3249 }
3250
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003251 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003252 * the request buffer if its not required.
3253 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003254 if (chn->buf.size == 0) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003255 if (HLUA_CANT_YIELD(hlua_gethlua(L)))
3256 return 1;
Willy Tarreau4b962a42018-11-15 11:03:21 +01003257 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003258 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003259 }
3260
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003261 /* The written data will be immediately sent, so we can check
3262 * the available space without taking in account the reserve.
3263 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003264 * data, because the buffer will be flushed.
3265 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003266 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003267
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003268 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003269 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003270 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003271 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003272 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003273 return 1;
3274
3275 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003276 if (max > len - l)
3277 max = len - l;
3278
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003279 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003280 * detects a non contiguous buffer and realign it.
3281 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003282 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003283 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003284
3285 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003286 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003287
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003288 /* buffer replace considers that the input part is filled.
3289 * so, I must forward these new data in the output part.
3290 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003291 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003292
3293 l += max;
3294 lua_pop(L, 1);
3295 lua_pushinteger(L, l);
3296
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003297 if (l < len) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003298 /* If there is no space available, and the output buffer is empty.
3299 * in this case, we cannot add more data, so we cannot yield,
3300 * we return the amount of copied data.
3301 */
3302 max = b_room(&chn->buf);
3303 if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L)))
3304 return 1;
3305
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003306 /* If we are waiting for space in the response buffer, we
3307 * must set the flag WAKERESWR. This flag required the task
3308 * wake up if any activity is detected on the response buffer.
3309 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003310 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003311 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003312 else
3313 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003314 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003315 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003316
3317 return 1;
3318}
3319
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003320/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003321 * yield the LUA process, and resume it without checking the
3322 * input arguments.
3323 */
3324__LJMP static int hlua_channel_send(lua_State *L)
3325{
3326 MAY_LJMP(check_args(L, 2, "send"));
3327 lua_pushinteger(L, 0);
3328
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003329 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003330}
3331
3332/* This function forward and amount of butes. The data pass from
3333 * the input side of the buffer to the output side, and can be
3334 * forwarded. This function never fails.
3335 *
3336 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003337 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003338 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003339__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003340{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003341 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003342 int len;
3343 int l;
3344 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003345 struct hlua *hlua;
3346
3347 /* Get hlua struct, or NULL if we execute from main lua state */
3348 hlua = hlua_gethlua(L);
3349 if (!hlua)
3350 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003351
3352 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003353
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003354 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003355 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003356 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003357 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003358
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003359 len = MAY_LJMP(luaL_checkinteger(L, 2));
3360 l = MAY_LJMP(luaL_checkinteger(L, -1));
3361
3362 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003363 if (max > ci_data(chn))
3364 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003365 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003366 l += max;
3367
3368 lua_pop(L, 1);
3369 lua_pushinteger(L, l);
3370
3371 /* Check if it miss bytes to forward. */
3372 if (l < len) {
3373 /* The the input channel or the output channel are closed, we
3374 * must return the amount of data forwarded.
3375 */
Christopher Faulet055310f2021-08-05 11:58:37 +02003376 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003377 return 1;
3378
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003379 /* If we are waiting for space data in the response buffer, we
3380 * must set the flag WAKERESWR. This flag required the task
3381 * wake up if any activity is detected on the response buffer.
3382 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003383 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003384 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003385 else
3386 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003387
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003388 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003389 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003390 }
3391
3392 return 1;
3393}
3394
3395/* Just check the input and prepare the stack for the previous
3396 * function "hlua_channel_forward_yield"
3397 */
3398__LJMP static int hlua_channel_forward(lua_State *L)
3399{
3400 MAY_LJMP(check_args(L, 2, "forward"));
3401 MAY_LJMP(hlua_checkchannel(L, 1));
3402 MAY_LJMP(luaL_checkinteger(L, 2));
3403
3404 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003405 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003406}
3407
3408/* Just returns the number of bytes available in the input
3409 * side of the buffer. This function never fails.
3410 */
3411__LJMP static int hlua_channel_get_in_len(lua_State *L)
3412{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003413 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003414
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003415 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003416 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003417 if (IS_HTX_STRM(chn_strm(chn))) {
3418 struct htx *htx = htxbuf(&chn->buf);
3419 lua_pushinteger(L, htx->data - co_data(chn));
3420 }
3421 else
3422 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003423 return 1;
3424}
3425
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003426/* Returns true if the channel is full. */
3427__LJMP static int hlua_channel_is_full(lua_State *L)
3428{
3429 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003430
3431 MAY_LJMP(check_args(L, 1, "is_full"));
3432 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003433 /* ignore the reserve, we are not on a producer side (ie in an
3434 * applet).
3435 */
3436 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003437 return 1;
3438}
3439
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003440/* Returns true if the channel is the response channel. */
3441__LJMP static int hlua_channel_is_resp(lua_State *L)
3442{
3443 struct channel *chn;
3444
3445 MAY_LJMP(check_args(L, 1, "is_resp"));
3446 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3447
3448 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3449 return 1;
3450}
3451
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003452/* Just returns the number of bytes available in the output
3453 * side of the buffer. This function never fails.
3454 */
3455__LJMP static int hlua_channel_get_out_len(lua_State *L)
3456{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003457 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003458
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003459 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003460 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003461 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003462 return 1;
3463}
3464
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003465/*
3466 *
3467 *
3468 * Class Fetches
3469 *
3470 *
3471 */
3472
3473/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003474 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003475 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003476__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003477{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003478 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003479}
3480
3481/* This function creates and push in the stack a fetch object according
3482 * with a current TXN.
3483 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003484static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003485{
Willy Tarreau7073c472015-04-06 11:15:40 +02003486 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003487
3488 /* Check stack size. */
3489 if (!lua_checkstack(L, 3))
3490 return 0;
3491
3492 /* Create the object: obj[0] = userdata.
3493 * Note that the base of the Fetches object is the
3494 * transaction object.
3495 */
3496 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003497 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003498 lua_rawseti(L, -2, 0);
3499
Willy Tarreau7073c472015-04-06 11:15:40 +02003500 hsmp->s = txn->s;
3501 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003502 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003503 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003504
3505 /* Pop a class sesison metatable and affect it to the userdata. */
3506 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3507 lua_setmetatable(L, -2);
3508
3509 return 1;
3510}
3511
3512/* This function is an LUA binding. It is called with each sample-fetch.
3513 * It uses closure argument to store the associated sample-fetch. It
3514 * returns only one argument or throws an error. An error is thrown
3515 * only if an error is encountered during the argument parsing. If
3516 * the "sample-fetch" function fails, nil is returned.
3517 */
3518__LJMP static int hlua_run_sample_fetch(lua_State *L)
3519{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003520 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003521 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003522 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003523 int i;
3524 struct sample smp;
3525
3526 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003527 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003528
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003529 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003530 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003531
Thierry FOURNIERca988662015-12-20 18:43:03 +01003532 /* Check execution authorization. */
3533 if (f->use & SMP_USE_HTTP_ANY &&
3534 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3535 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3536 "is not available in Lua services", f->kw);
3537 WILL_LJMP(lua_error(L));
3538 }
3539
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003540 /* Get extra arguments. */
3541 for (i = 0; i < lua_gettop(L) - 1; i++) {
3542 if (i >= ARGM_NBARGS)
3543 break;
3544 hlua_lua2arg(L, i + 2, &args[i]);
3545 }
3546 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003547 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003548
3549 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003550 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003551
3552 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003553 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003554 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003555 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003556 }
3557
3558 /* Initialise the sample. */
3559 memset(&smp, 0, sizeof(smp));
3560
3561 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003562 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003563 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003564 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003565 lua_pushstring(L, "");
3566 else
3567 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003568 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003569 }
3570
3571 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003572 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003573 hlua_smp2lua_str(L, &smp);
3574 else
3575 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003576
3577 end:
3578 for (i = 0; args[i].type != ARGT_STOP; i++) {
3579 if (args[i].type == ARGT_STR)
3580 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003581 else if (args[i].type == ARGT_REG)
3582 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003583 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003584 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003585
3586 error:
3587 for (i = 0; args[i].type != ARGT_STOP; i++) {
3588 if (args[i].type == ARGT_STR)
3589 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003590 else if (args[i].type == ARGT_REG)
3591 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003592 }
3593 WILL_LJMP(lua_error(L));
3594 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003595}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003596
3597/*
3598 *
3599 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003600 * Class Converters
3601 *
3602 *
3603 */
3604
3605/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003606 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003607 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003608__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003609{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003610 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003611}
3612
3613/* This function creates and push in the stack a Converters object
3614 * according with a current TXN.
3615 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003616static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003617{
Willy Tarreau7073c472015-04-06 11:15:40 +02003618 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003619
3620 /* Check stack size. */
3621 if (!lua_checkstack(L, 3))
3622 return 0;
3623
3624 /* Create the object: obj[0] = userdata.
3625 * Note that the base of the Converters object is the
3626 * same than the TXN object.
3627 */
3628 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003629 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003630 lua_rawseti(L, -2, 0);
3631
Willy Tarreau7073c472015-04-06 11:15:40 +02003632 hsmp->s = txn->s;
3633 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003634 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003635 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003636
Willy Tarreau87b09662015-04-03 00:22:06 +02003637 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003638 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3639 lua_setmetatable(L, -2);
3640
3641 return 1;
3642}
3643
3644/* This function is an LUA binding. It is called with each converter.
3645 * It uses closure argument to store the associated converter. It
3646 * returns only one argument or throws an error. An error is thrown
3647 * only if an error is encountered during the argument parsing. If
3648 * the converter function function fails, nil is returned.
3649 */
3650__LJMP static int hlua_run_sample_conv(lua_State *L)
3651{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003652 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003653 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003654 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003655 int i;
3656 struct sample smp;
3657
3658 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003659 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003660
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003661 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003662 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003663
3664 /* Get extra arguments. */
3665 for (i = 0; i < lua_gettop(L) - 2; i++) {
3666 if (i >= ARGM_NBARGS)
3667 break;
3668 hlua_lua2arg(L, i + 3, &args[i]);
3669 }
3670 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003671 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003672
3673 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003674 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003675
3676 /* Run the special args checker. */
3677 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3678 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003679 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003680 }
3681
3682 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003683 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003684 if (!hlua_lua2smp(L, 2, &smp)) {
3685 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003686 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003687 }
3688
Willy Tarreau1777ea62016-03-10 16:15:46 +01003689 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3690
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003691 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003692 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003693 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003694 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003695 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003696 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003697 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3698 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003699 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003700 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003701 }
3702
3703 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003704 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003705 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003706 lua_pushstring(L, "");
3707 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003708 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003709 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003710 }
3711
3712 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003713 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003714 hlua_smp2lua_str(L, &smp);
3715 else
3716 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003717 end:
3718 for (i = 0; args[i].type != ARGT_STOP; i++) {
3719 if (args[i].type == ARGT_STR)
3720 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003721 else if (args[i].type == ARGT_REG)
3722 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003723 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003724 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003725
3726 error:
3727 for (i = 0; args[i].type != ARGT_STOP; i++) {
3728 if (args[i].type == ARGT_STR)
3729 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003730 else if (args[i].type == ARGT_REG)
3731 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003732 }
3733 WILL_LJMP(lua_error(L));
3734 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003735}
3736
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003737/*
3738 *
3739 *
3740 * Class AppletTCP
3741 *
3742 *
3743 */
3744
3745/* Returns a struct hlua_txn if the stack entry "ud" is
3746 * a class stream, otherwise it throws an error.
3747 */
3748__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3749{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003750 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003751}
3752
3753/* This function creates and push in the stack an Applet object
3754 * according with a current TXN.
3755 */
3756static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3757{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003758 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003759 struct stream_interface *si = ctx->owner;
3760 struct stream *s = si_strm(si);
3761 struct proxy *p = s->be;
3762
3763 /* Check stack size. */
3764 if (!lua_checkstack(L, 3))
3765 return 0;
3766
3767 /* Create the object: obj[0] = userdata.
3768 * Note that the base of the Converters object is the
3769 * same than the TXN object.
3770 */
3771 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003772 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003773 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003774 luactx->appctx = ctx;
3775 luactx->htxn.s = s;
3776 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003777
3778 /* Create the "f" field that contains a list of fetches. */
3779 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003780 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003781 return 0;
3782 lua_settable(L, -3);
3783
3784 /* Create the "sf" field that contains a list of stringsafe fetches. */
3785 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003786 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003787 return 0;
3788 lua_settable(L, -3);
3789
3790 /* Create the "c" field that contains a list of converters. */
3791 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003792 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003793 return 0;
3794 lua_settable(L, -3);
3795
3796 /* Create the "sc" field that contains a list of stringsafe converters. */
3797 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003798 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003799 return 0;
3800 lua_settable(L, -3);
3801
3802 /* Pop a class stream metatable and affect it to the table. */
3803 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3804 lua_setmetatable(L, -2);
3805
3806 return 1;
3807}
3808
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003809__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3810{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003811 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003812 struct stream *s;
3813 const char *name;
3814 size_t len;
3815 struct sample smp;
3816
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003817 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3818 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003819
3820 /* It is useles to retrieve the stream, but this function
3821 * runs only in a stream context.
3822 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003823 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003824 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003825 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003826
3827 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003828 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003829 hlua_lua2smp(L, 3, &smp);
3830
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02003831 /* Store the sample in a variable. We don't need to dup the smp, vars API
3832 * already takes care of duplicating dynamic var data.
3833 */
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003834 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003835
3836 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3837 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3838 else
3839 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3840
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003841 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003842}
3843
3844__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3845{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003846 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003847 struct stream *s;
3848 const char *name;
3849 size_t len;
3850 struct sample smp;
3851
3852 MAY_LJMP(check_args(L, 2, "unset_var"));
3853
3854 /* It is useles to retrieve the stream, but this function
3855 * runs only in a stream context.
3856 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003857 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003858 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003859 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003860
3861 /* Unset the variable. */
3862 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003863 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3864 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003865}
3866
3867__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3868{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003869 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003870 struct stream *s;
3871 const char *name;
3872 size_t len;
3873 struct sample smp;
3874
3875 MAY_LJMP(check_args(L, 2, "get_var"));
3876
3877 /* It is useles to retrieve the stream, but this function
3878 * runs only in a stream context.
3879 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003880 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003881 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003882 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003883
3884 smp_set_owner(&smp, s->be, s->sess, s, 0);
3885 if (!vars_get_by_name(name, len, &smp)) {
3886 lua_pushnil(L);
3887 return 1;
3888 }
3889
3890 return hlua_smp2lua(L, &smp);
3891}
3892
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003893__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3894{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003895 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3896 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003897 struct hlua *hlua;
3898
3899 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003900 if (!s->hlua)
3901 return 0;
3902 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003903
3904 MAY_LJMP(check_args(L, 2, "set_priv"));
3905
3906 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003907 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003908
3909 /* Get and store new value. */
3910 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3911 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3912
3913 return 0;
3914}
3915
3916__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3917{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003918 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3919 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003920 struct hlua *hlua;
3921
3922 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003923 if (!s->hlua) {
3924 lua_pushnil(L);
3925 return 1;
3926 }
3927 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003928
3929 /* Push configuration index in the stack. */
3930 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3931
3932 return 1;
3933}
3934
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003935/* If expected data not yet available, it returns a yield. This function
3936 * consumes the data in the buffer. It returns a string containing the
3937 * data. This string can be empty.
3938 */
3939__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3940{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003941 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3942 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003943 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003944 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003945 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003946 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003947 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003948
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003949 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003950 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003951
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003952 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003953 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003954 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003955 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003956 }
3957
3958 /* End of data: commit the total strings and return. */
3959 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003960 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003961 return 1;
3962 }
3963
3964 /* Ensure that the block 2 length is usable. */
3965 if (ret == 1)
3966 len2 = 0;
3967
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003968 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003969 luaL_addlstring(&luactx->b, blk1, len1);
3970 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003971
3972 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003973 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003974 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003975 return 1;
3976}
3977
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003978/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003979__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3980{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003981 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003982
3983 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003984 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003985
3986 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3987}
3988
3989/* If expected data not yet available, it returns a yield. This function
3990 * consumes the data in the buffer. It returns a string containing the
3991 * data. This string can be empty.
3992 */
3993__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3994{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003995 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3996 struct stream_interface *si = luactx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003997 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003998 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003999 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004000 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004001 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004002 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004004 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004005 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004006
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004007 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004008 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004009 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004010 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004011 }
4012
4013 /* End of data: commit the total strings and return. */
4014 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004015 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004016 return 1;
4017 }
4018
4019 /* Ensure that the block 2 length is usable. */
4020 if (ret == 1)
4021 len2 = 0;
4022
4023 if (len == -1) {
4024
4025 /* If len == -1, catenate all the data avalaile and
4026 * yield because we want to get all the data until
4027 * the end of data stream.
4028 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004029 luaL_addlstring(&luactx->b, blk1, len1);
4030 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004031 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004032 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004033 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004034
4035 } else {
4036
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004037 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004038 if (len1 > len)
4039 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004040 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004041 len -= len1;
4042
4043 /* Copy the second block. */
4044 if (len2 > len)
4045 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004046 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004047 len -= len2;
4048
4049 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004050 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004051
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004052 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004053 if (len > 0) {
4054 lua_pushinteger(L, len);
4055 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004056 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004057 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004058 }
4059
4060 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004061 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004062 return 1;
4063 }
4064
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004065 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004066 hlua_pusherror(L, "Lua: internal error");
4067 WILL_LJMP(lua_error(L));
4068 return 0;
4069}
4070
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004071/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004072__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4073{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004074 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004075 int len = -1;
4076
4077 if (lua_gettop(L) > 2)
4078 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4079 if (lua_gettop(L) >= 2) {
4080 len = MAY_LJMP(luaL_checkinteger(L, 2));
4081 lua_pop(L, 1);
4082 }
4083
4084 /* Confirm or set the required length */
4085 lua_pushinteger(L, len);
4086
4087 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004088 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004089
4090 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4091}
4092
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004093/* Append data in the output side of the buffer. This data is immediately
4094 * sent. The function returns the amount of data written. If the buffer
4095 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004096 * if the channel is closed.
4097 */
4098__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4099{
4100 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004101 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004102 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4103 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004104 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004105 struct channel *chn = si_ic(si);
4106 int max;
4107
4108 /* Get the max amount of data which can write as input in the channel. */
4109 max = channel_recv_max(chn);
4110 if (max > (len - l))
4111 max = len - l;
4112
4113 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004114 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004115
4116 /* update counters. */
4117 l += max;
4118 lua_pop(L, 1);
4119 lua_pushinteger(L, l);
4120
4121 /* If some data is not send, declares the situation to the
4122 * applet, and returns a yield.
4123 */
4124 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004125 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004126 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004127 }
4128
4129 return 1;
4130}
4131
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004132/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004133 * yield the LUA process, and resume it without checking the
4134 * input arguments.
4135 */
4136__LJMP static int hlua_applet_tcp_send(lua_State *L)
4137{
4138 MAY_LJMP(check_args(L, 2, "send"));
4139 lua_pushinteger(L, 0);
4140
4141 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4142}
4143
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004144/*
4145 *
4146 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004147 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004148 *
4149 *
4150 */
4151
4152/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004153 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004154 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004155__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004156{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004157 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004158}
4159
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004160/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004161 * according with a current TXN.
4162 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004163static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004164{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004165 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004166 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004167 struct stream_interface *si = ctx->owner;
4168 struct stream *s = si_strm(si);
4169 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004170 struct htx *htx;
4171 struct htx_blk *blk;
4172 struct htx_sl *sl;
4173 struct ist path;
4174 unsigned long long len = 0;
4175 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004176
4177 /* Check stack size. */
4178 if (!lua_checkstack(L, 3))
4179 return 0;
4180
4181 /* Create the object: obj[0] = userdata.
4182 * Note that the base of the Converters object is the
4183 * same than the TXN object.
4184 */
4185 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004186 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004187 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004188 luactx->appctx = ctx;
4189 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4190 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4191 luactx->htxn.s = s;
4192 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004193
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004194 /* Create the "f" field that contains a list of fetches. */
4195 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004196 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004197 return 0;
4198 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004199
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004200 /* Create the "sf" field that contains a list of stringsafe fetches. */
4201 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004202 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004203 return 0;
4204 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004205
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004206 /* Create the "c" field that contains a list of converters. */
4207 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004208 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004209 return 0;
4210 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004211
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004212 /* Create the "sc" field that contains a list of stringsafe converters. */
4213 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004214 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004215 return 0;
4216 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004217
Christopher Fauleta2097962019-07-15 16:25:33 +02004218 htx = htxbuf(&s->req.buf);
4219 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004220 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004221 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004222
Christopher Fauleta2097962019-07-15 16:25:33 +02004223 /* Stores the request method. */
4224 lua_pushstring(L, "method");
4225 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4226 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004227
Christopher Fauleta2097962019-07-15 16:25:33 +02004228 /* Stores the http version. */
4229 lua_pushstring(L, "version");
4230 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4231 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004232
Christopher Fauleta2097962019-07-15 16:25:33 +02004233 /* creates an array of headers. hlua_http_get_headers() crates and push
4234 * the array on the top of the stack.
4235 */
4236 lua_pushstring(L, "headers");
4237 htxn.s = s;
4238 htxn.p = px;
4239 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004240 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004241 return 0;
4242 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004243
Christopher Fauleta2097962019-07-15 16:25:33 +02004244 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004245 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004246 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004247
Christopher Fauleta2097962019-07-15 16:25:33 +02004248 p = path.ptr;
4249 end = path.ptr + path.len;
4250 q = p;
4251 while (q < end && *q != '?')
4252 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004253
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004254 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004255 lua_pushstring(L, "path");
4256 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004257 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004258
Christopher Fauleta2097962019-07-15 16:25:33 +02004259 /* Stores the query string. */
4260 lua_pushstring(L, "qs");
4261 if (*q == '?')
4262 q++;
4263 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004264 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004265 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004266
Christopher Fauleta2097962019-07-15 16:25:33 +02004267 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4268 struct htx_blk *blk = htx_get_blk(htx, pos);
4269 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004270
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004271 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004272 break;
4273 if (type == HTX_BLK_DATA)
4274 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004275 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004276 if (htx->extra != ULLONG_MAX)
4277 len += htx->extra;
4278
4279 /* Stores the request path. */
4280 lua_pushstring(L, "length");
4281 lua_pushinteger(L, len);
4282 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004283
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004284 /* Create an empty array of HTTP request headers. */
4285 lua_pushstring(L, "response");
4286 lua_newtable(L);
4287 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004288
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004289 /* Pop a class stream metatable and affect it to the table. */
4290 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4291 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004292
4293 return 1;
4294}
4295
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004296__LJMP static int hlua_applet_http_set_var(lua_State *L)
4297{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004298 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004299 struct stream *s;
4300 const char *name;
4301 size_t len;
4302 struct sample smp;
4303
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004304 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4305 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004306
4307 /* It is useles to retrieve the stream, but this function
4308 * runs only in a stream context.
4309 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004310 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004311 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004312 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004313
4314 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004315 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004316 hlua_lua2smp(L, 3, &smp);
4317
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02004318 /* Store the sample in a variable. We don't need to dup the smp, vars API
4319 * already takes care of duplicating dynamic var data.
4320 */
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004321 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004322
4323 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4324 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4325 else
4326 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4327
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004328 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004329}
4330
4331__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4332{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004333 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004334 struct stream *s;
4335 const char *name;
4336 size_t len;
4337 struct sample smp;
4338
4339 MAY_LJMP(check_args(L, 2, "unset_var"));
4340
4341 /* It is useles to retrieve the stream, but this function
4342 * runs only in a stream context.
4343 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004344 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004345 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004346 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004347
4348 /* Unset the variable. */
4349 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004350 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4351 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004352}
4353
4354__LJMP static int hlua_applet_http_get_var(lua_State *L)
4355{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004356 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004357 struct stream *s;
4358 const char *name;
4359 size_t len;
4360 struct sample smp;
4361
4362 MAY_LJMP(check_args(L, 2, "get_var"));
4363
4364 /* It is useles to retrieve the stream, but this function
4365 * runs only in a stream context.
4366 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004367 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004368 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004369 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004370
4371 smp_set_owner(&smp, s->be, s->sess, s, 0);
4372 if (!vars_get_by_name(name, len, &smp)) {
4373 lua_pushnil(L);
4374 return 1;
4375 }
4376
4377 return hlua_smp2lua(L, &smp);
4378}
4379
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004380__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4381{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004382 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4383 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004384 struct hlua *hlua;
4385
4386 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004387 if (!s->hlua)
4388 return 0;
4389 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004390
4391 MAY_LJMP(check_args(L, 2, "set_priv"));
4392
4393 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004394 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004395
4396 /* Get and store new value. */
4397 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4398 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4399
4400 return 0;
4401}
4402
4403__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4404{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004405 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4406 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004407 struct hlua *hlua;
4408
4409 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004410 if (!s->hlua) {
4411 lua_pushnil(L);
4412 return 1;
4413 }
4414 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004415
4416 /* Push configuration index in the stack. */
4417 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4418
4419 return 1;
4420}
4421
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004422/* If expected data not yet available, it returns a yield. This function
4423 * consumes the data in the buffer. It returns a string containing the
4424 * data. This string can be empty.
4425 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004426__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004427{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004428 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4429 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004430 struct channel *req = si_oc(si);
4431 struct htx *htx;
4432 struct htx_blk *blk;
4433 size_t count;
4434 int stop = 0;
4435
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004436 htx = htx_from_buf(&req->buf);
4437 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004438 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004439
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004440 while (count && !stop && blk) {
4441 enum htx_blk_type type = htx_get_blk_type(blk);
4442 uint32_t sz = htx_get_blksz(blk);
4443 struct ist v;
4444 uint32_t vlen;
4445 char *nl;
4446
4447 vlen = sz;
4448 if (vlen > count) {
4449 if (type != HTX_BLK_DATA)
4450 break;
4451 vlen = count;
4452 }
4453
4454 switch (type) {
4455 case HTX_BLK_UNUSED:
4456 break;
4457
4458 case HTX_BLK_DATA:
4459 v = htx_get_blk_value(htx, blk);
4460 v.len = vlen;
4461 nl = istchr(v, '\n');
4462 if (nl != NULL) {
4463 stop = 1;
4464 vlen = nl - v.ptr + 1;
4465 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004466 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004467 break;
4468
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004469 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004470 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004471 stop = 1;
4472 break;
4473
4474 default:
4475 break;
4476 }
4477
4478 co_set_data(req, co_data(req) - vlen);
4479 count -= vlen;
4480 if (sz == vlen)
4481 blk = htx_remove_blk(htx, blk);
4482 else {
4483 htx_cut_data_blk(htx, blk, vlen);
4484 break;
4485 }
4486 }
4487
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004488 /* The message was fully consumed and no more data are expected
4489 * (EOM flag set).
4490 */
Christopher Fauleta5a25b12022-08-29 15:37:16 +02004491 if (htx_is_empty(htx) && (req->flags & CF_EOI))
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004492 stop = 1;
4493
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004494 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004495 if (!stop) {
4496 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004497 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004498 }
4499
4500 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004501 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004502 return 1;
4503}
4504
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004505
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004506/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004507__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004508{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004509 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004510
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004511 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004512 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004513
Christopher Fauleta2097962019-07-15 16:25:33 +02004514 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004515}
4516
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004517/* If expected data not yet available, it returns a yield. This function
4518 * consumes the data in the buffer. It returns a string containing the
4519 * data. This string can be empty.
4520 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004521__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004522{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004523 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4524 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004525 struct channel *req = si_oc(si);
4526 struct htx *htx;
4527 struct htx_blk *blk;
4528 size_t count;
4529 int len;
4530
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004531 htx = htx_from_buf(&req->buf);
4532 len = MAY_LJMP(luaL_checkinteger(L, 2));
4533 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004534 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004535 while (count && len && blk) {
4536 enum htx_blk_type type = htx_get_blk_type(blk);
4537 uint32_t sz = htx_get_blksz(blk);
4538 struct ist v;
4539 uint32_t vlen;
4540
4541 vlen = sz;
4542 if (len > 0 && vlen > len)
4543 vlen = len;
4544 if (vlen > count) {
4545 if (type != HTX_BLK_DATA)
4546 break;
4547 vlen = count;
4548 }
4549
4550 switch (type) {
4551 case HTX_BLK_UNUSED:
4552 break;
4553
4554 case HTX_BLK_DATA:
4555 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004556 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004557 break;
4558
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004559 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004560 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004561 len = 0;
4562 break;
4563
4564 default:
4565 break;
4566 }
4567
4568 co_set_data(req, co_data(req) - vlen);
4569 count -= vlen;
4570 if (len > 0)
4571 len -= vlen;
4572 if (sz == vlen)
4573 blk = htx_remove_blk(htx, blk);
4574 else {
4575 htx_cut_data_blk(htx, blk, vlen);
4576 break;
4577 }
4578 }
4579
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004580 /* The message was fully consumed and no more data are expected
4581 * (EOM flag set).
4582 */
Christopher Fauleta5a25b12022-08-29 15:37:16 +02004583 if (htx_is_empty(htx) && (req->flags & CF_EOI))
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004584 len = 0;
4585
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004586 htx_to_buf(htx, &req->buf);
4587
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004588 /* If we are no other data available, yield waiting for new data. */
4589 if (len) {
4590 if (len > 0) {
4591 lua_pushinteger(L, len);
4592 lua_replace(L, 2);
4593 }
4594 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004595 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004596 }
4597
4598 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004599 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004600 return 1;
4601}
4602
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004603/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004604__LJMP static int hlua_applet_http_recv(lua_State *L)
4605{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004606 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004607 int len = -1;
4608
4609 /* Check arguments. */
4610 if (lua_gettop(L) > 2)
4611 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4612 if (lua_gettop(L) >= 2) {
4613 len = MAY_LJMP(luaL_checkinteger(L, 2));
4614 lua_pop(L, 1);
4615 }
4616
Christopher Fauleta2097962019-07-15 16:25:33 +02004617 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004618
Christopher Fauleta2097962019-07-15 16:25:33 +02004619 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004620 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004621
Christopher Fauleta2097962019-07-15 16:25:33 +02004622 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004623}
4624
4625/* Append data in the output side of the buffer. This data is immediately
4626 * sent. The function returns the amount of data written. If the buffer
4627 * cannot contain the data, the function yields. The function returns -1
4628 * if the channel is closed.
4629 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004630__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004631{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004632 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4633 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004634 struct channel *res = si_ic(si);
4635 struct htx *htx = htx_from_buf(&res->buf);
4636 const char *data;
4637 size_t len;
4638 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4639 int max;
4640
Christopher Faulet9060fc02019-07-03 11:39:30 +02004641 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004642 if (!max)
4643 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004644
4645 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4646
4647 /* Get the max amount of data which can write as input in the channel. */
4648 if (max > (len - l))
4649 max = len - l;
4650
4651 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004652 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004653 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004654
4655 /* update counters. */
4656 l += max;
4657 lua_pop(L, 1);
4658 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004659
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004660 /* If some data is not send, declares the situation to the
4661 * applet, and returns a yield.
4662 */
4663 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004664 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004665 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004666 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004667 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004668 }
4669
Christopher Fauleta2097962019-07-15 16:25:33 +02004670 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004671 return 1;
4672}
4673
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004674/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004675 * yield the LUA process, and resume it without checking the
4676 * input arguments.
4677 */
4678__LJMP static int hlua_applet_http_send(lua_State *L)
4679{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004680 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004681
4682 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004683 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004684 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4685 WILL_LJMP(lua_error(L));
4686 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004687
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004688 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004689 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004690
Christopher Fauleta2097962019-07-15 16:25:33 +02004691 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004692}
4693
4694__LJMP static int hlua_applet_http_addheader(lua_State *L)
4695{
4696 const char *name;
4697 int ret;
4698
4699 MAY_LJMP(hlua_checkapplet_http(L, 1));
4700 name = MAY_LJMP(luaL_checkstring(L, 2));
4701 MAY_LJMP(luaL_checkstring(L, 3));
4702
4703 /* Push in the stack the "response" entry. */
4704 ret = lua_getfield(L, 1, "response");
4705 if (ret != LUA_TTABLE) {
4706 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4707 "is expected as an array. %s found", lua_typename(L, ret));
4708 WILL_LJMP(lua_error(L));
4709 }
4710
4711 /* check if the header is already registered if it is not
4712 * the case, register it.
4713 */
4714 ret = lua_getfield(L, -1, name);
4715 if (ret == LUA_TNIL) {
4716
4717 /* Entry not found. */
4718 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4719
4720 /* Insert the new header name in the array in the top of the stack.
4721 * It left the new array in the top of the stack.
4722 */
4723 lua_newtable(L);
4724 lua_pushvalue(L, 2);
4725 lua_pushvalue(L, -2);
4726 lua_settable(L, -4);
4727
4728 } else if (ret != LUA_TTABLE) {
4729
4730 /* corruption error. */
4731 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4732 "is expected as an array. %s found", name, lua_typename(L, ret));
4733 WILL_LJMP(lua_error(L));
4734 }
4735
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004736 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004737 * the header value as new entry.
4738 */
4739 lua_pushvalue(L, 3);
4740 ret = lua_rawlen(L, -2);
4741 lua_rawseti(L, -2, ret + 1);
4742 lua_pushboolean(L, 1);
4743 return 1;
4744}
4745
4746__LJMP static int hlua_applet_http_status(lua_State *L)
4747{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004748 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004749 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004750 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004751
4752 if (status < 100 || status > 599) {
4753 lua_pushboolean(L, 0);
4754 return 1;
4755 }
4756
Willy Tarreau7e702d12021-04-28 17:59:21 +02004757 luactx->appctx->ctx.hlua_apphttp.status = status;
4758 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004759 lua_pushboolean(L, 1);
4760 return 1;
4761}
4762
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004763
Christopher Fauleta2097962019-07-15 16:25:33 +02004764__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004765{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004766 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4767 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004768 struct channel *res = si_ic(si);
4769 struct htx *htx;
4770 struct htx_sl *sl;
4771 struct h1m h1m;
4772 const char *status, *reason;
4773 const char *name, *value;
4774 size_t nlen, vlen;
4775 unsigned int flags;
4776
4777 /* Send the message at once. */
4778 htx = htx_from_buf(&res->buf);
4779 h1m_init_res(&h1m);
4780
4781 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004782 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4783 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004784 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02004785 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
4786 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004787 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4788 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4789 }
4790 else {
4791 flags = HTX_SL_F_IS_RESP;
4792 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4793 }
4794 if (!sl) {
4795 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004796 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004797 WILL_LJMP(lua_error(L));
4798 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004799 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004800
4801 /* Get the array associated to the field "response" in the object AppletHTTP. */
4802 lua_pushvalue(L, 0);
4803 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4804 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004805 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004806 WILL_LJMP(lua_error(L));
4807 }
4808
4809 /* Browse the list of headers. */
4810 lua_pushnil(L);
4811 while(lua_next(L, -2) != 0) {
4812 /* We expect a string as -2. */
4813 if (lua_type(L, -2) != LUA_TSTRING) {
4814 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004815 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004816 lua_typename(L, lua_type(L, -2)));
4817 WILL_LJMP(lua_error(L));
4818 }
4819 name = lua_tolstring(L, -2, &nlen);
4820
4821 /* We expect an array as -1. */
4822 if (lua_type(L, -1) != LUA_TTABLE) {
4823 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004824 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004825 name,
4826 lua_typename(L, lua_type(L, -1)));
4827 WILL_LJMP(lua_error(L));
4828 }
4829
4830 /* Browse the table who is on the top of the stack. */
4831 lua_pushnil(L);
4832 while(lua_next(L, -2) != 0) {
4833 int id;
4834
4835 /* We expect a number as -2. */
4836 if (lua_type(L, -2) != LUA_TNUMBER) {
4837 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004838 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004839 name,
4840 lua_typename(L, lua_type(L, -2)));
4841 WILL_LJMP(lua_error(L));
4842 }
4843 id = lua_tointeger(L, -2);
4844
4845 /* We expect a string as -2. */
4846 if (lua_type(L, -1) != LUA_TSTRING) {
4847 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004848 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004849 name, id,
4850 lua_typename(L, lua_type(L, -1)));
4851 WILL_LJMP(lua_error(L));
4852 }
4853 value = lua_tolstring(L, -1, &vlen);
4854
4855 /* Simple Protocol checks. */
4856 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004857 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004858 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4859 struct ist v = ist2(value, vlen);
4860 int ret;
4861
4862 ret = h1_parse_cont_len_header(&h1m, &v);
4863 if (ret < 0) {
4864 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004865 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004866 name);
4867 WILL_LJMP(lua_error(L));
4868 }
4869 else if (ret == 0)
4870 goto next; /* Skip it */
4871 }
4872
4873 /* Add a new header */
4874 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4875 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004876 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004877 name);
4878 WILL_LJMP(lua_error(L));
4879 }
4880 next:
4881 /* Remove the array from the stack, and get next element with a remaining string. */
4882 lua_pop(L, 1);
4883 }
4884
4885 /* Remove the array from the stack, and get next element with a remaining string. */
4886 lua_pop(L, 1);
4887 }
4888
4889 if (h1m.flags & H1_MF_CHNK)
4890 h1m.flags &= ~H1_MF_CLEN;
4891 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4892 h1m.flags |= H1_MF_XFER_LEN;
4893
4894 /* Uset HTX start-line flags */
4895 if (h1m.flags & H1_MF_XFER_ENC)
4896 flags |= HTX_SL_F_XFER_ENC;
4897 if (h1m.flags & H1_MF_XFER_LEN) {
4898 flags |= HTX_SL_F_XFER_LEN;
4899 if (h1m.flags & H1_MF_CHNK)
4900 flags |= HTX_SL_F_CHNK;
4901 else if (h1m.flags & H1_MF_CLEN)
4902 flags |= HTX_SL_F_CLEN;
4903 if (h1m.body_len == 0)
4904 flags |= HTX_SL_F_BODYLESS;
4905 }
4906 sl->flags |= flags;
4907
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004908 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004909 * and the status code implies the presence of a message body, we must
4910 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004911 * for the keepalive compliance. If the applet announces a transfer-encoding
4912 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004913 */
4914 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02004915 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
4916 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
4917 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004918 /* Add a new header */
4919 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4920 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4921 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004922 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004923 WILL_LJMP(lua_error(L));
4924 }
4925 }
4926
4927 /* Finalize headers. */
4928 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4929 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004930 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004931 WILL_LJMP(lua_error(L));
4932 }
4933
4934 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4935 b_reset(&res->buf);
4936 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4937 WILL_LJMP(lua_error(L));
4938 }
4939
4940 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004941 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004942
4943 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004944 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004945 return 0;
4946
4947}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004948/* We will build the status line and the headers of the HTTP response.
4949 * We will try send at once if its not possible, we give back the hand
4950 * waiting for more room.
4951 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004952__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004953{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004954 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4955 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004956 struct channel *res = si_ic(si);
4957
4958 if (co_data(res)) {
4959 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004960 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004961 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004962 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004963}
4964
4965
Christopher Fauleta2097962019-07-15 16:25:33 +02004966__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004967{
Christopher Fauleta2097962019-07-15 16:25:33 +02004968 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004969}
4970
Christopher Fauleta2097962019-07-15 16:25:33 +02004971/*
4972 *
4973 *
4974 * Class HTTP
4975 *
4976 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004977 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004978
4979/* Returns a struct hlua_txn if the stack entry "ud" is
4980 * a class stream, otherwise it throws an error.
4981 */
4982__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004983{
Christopher Fauleta2097962019-07-15 16:25:33 +02004984 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4985}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004986
Christopher Fauleta2097962019-07-15 16:25:33 +02004987/* This function creates and push in the stack a HTTP object
4988 * according with a current TXN.
4989 */
4990static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4991{
4992 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004993
Christopher Fauleta2097962019-07-15 16:25:33 +02004994 /* Check stack size. */
4995 if (!lua_checkstack(L, 3))
4996 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004997
Christopher Fauleta2097962019-07-15 16:25:33 +02004998 /* Create the object: obj[0] = userdata.
4999 * Note that the base of the Converters object is the
5000 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005001 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005002 lua_newtable(L);
5003 htxn = lua_newuserdata(L, sizeof(*htxn));
5004 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005005
5006 htxn->s = txn->s;
5007 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005008 htxn->dir = txn->dir;
5009 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005010
5011 /* Pop a class stream metatable and affect it to the table. */
5012 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5013 lua_setmetatable(L, -2);
5014
5015 return 1;
5016}
5017
5018/* This function creates ans returns an array of HTTP headers.
5019 * This function does not fails. It is used as wrapper with the
5020 * 2 following functions.
5021 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005022__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005023{
Christopher Fauleta2097962019-07-15 16:25:33 +02005024 struct htx *htx;
5025 int32_t pos;
5026
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005027 /* Create the table. */
5028 lua_newtable(L);
5029
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005030
Christopher Fauleta2097962019-07-15 16:25:33 +02005031 htx = htxbuf(&msg->chn->buf);
5032 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5033 struct htx_blk *blk = htx_get_blk(htx, pos);
5034 enum htx_blk_type type = htx_get_blk_type(blk);
5035 struct ist n, v;
5036 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005037
Christopher Fauleta2097962019-07-15 16:25:33 +02005038 if (type == HTX_BLK_HDR) {
5039 n = htx_get_blk_name(htx,blk);
5040 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005041 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005042 else if (type == HTX_BLK_EOH)
5043 break;
5044 else
5045 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005046
Christopher Fauleta2097962019-07-15 16:25:33 +02005047 /* Check for existing entry:
5048 * assume that the table is on the top of the stack, and
5049 * push the key in the stack, the function lua_gettable()
5050 * perform the lookup.
5051 */
5052 lua_pushlstring(L, n.ptr, n.len);
5053 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005054
Christopher Fauleta2097962019-07-15 16:25:33 +02005055 switch (lua_type(L, -1)) {
5056 case LUA_TNIL:
5057 /* Table not found, create it. */
5058 lua_pop(L, 1); /* remove the nil value. */
5059 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5060 lua_newtable(L); /* create and push empty table. */
5061 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5062 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5063 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005064 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005065
Christopher Fauleta2097962019-07-15 16:25:33 +02005066 case LUA_TTABLE:
5067 /* Entry found: push the value in the table. */
5068 len = lua_rawlen(L, -1);
5069 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5070 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5071 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5072 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005073
Christopher Fauleta2097962019-07-15 16:25:33 +02005074 default:
5075 /* Other cases are errors. */
5076 hlua_pusherror(L, "internal error during the parsing of headers.");
5077 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005078 }
5079 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005080 return 1;
5081}
5082
5083__LJMP static int hlua_http_req_get_headers(lua_State *L)
5084{
5085 struct hlua_txn *htxn;
5086
5087 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5088 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5089
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005090 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005091 WILL_LJMP(lua_error(L));
5092
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005093 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005094}
5095
5096__LJMP static int hlua_http_res_get_headers(lua_State *L)
5097{
5098 struct hlua_txn *htxn;
5099
5100 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5101 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5102
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005103 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005104 WILL_LJMP(lua_error(L));
5105
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005106 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005107}
5108
5109/* This function replace full header, or just a value in
5110 * the request or in the response. It is a wrapper fir the
5111 * 4 following functions.
5112 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005113__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005114{
5115 size_t name_len;
5116 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5117 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5118 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005119 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005120 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005121
Dragan Dosen26743032019-04-30 15:54:36 +02005122 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005123 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5124
Christopher Fauleta2097962019-07-15 16:25:33 +02005125 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005126 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005127 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005128 return 0;
5129}
5130
5131__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5132{
5133 struct hlua_txn *htxn;
5134
5135 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5136 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5137
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005138 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005139 WILL_LJMP(lua_error(L));
5140
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005141 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005142}
5143
5144__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5145{
5146 struct hlua_txn *htxn;
5147
5148 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5149 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5150
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005151 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005152 WILL_LJMP(lua_error(L));
5153
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005154 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005155}
5156
5157__LJMP static int hlua_http_req_rep_val(lua_State *L)
5158{
5159 struct hlua_txn *htxn;
5160
5161 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5162 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5163
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005164 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005165 WILL_LJMP(lua_error(L));
5166
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005167 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005168}
5169
5170__LJMP static int hlua_http_res_rep_val(lua_State *L)
5171{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005172 struct hlua_txn *htxn;
5173
5174 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5175 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5176
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005177 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005178 WILL_LJMP(lua_error(L));
5179
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005180 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005181}
5182
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005183/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005184 * It is a wrapper for the 2 following functions.
5185 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005186__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005187{
5188 size_t len;
5189 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005190 struct htx *htx = htxbuf(&msg->chn->buf);
5191 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005192
Christopher Fauleta2097962019-07-15 16:25:33 +02005193 ctx.blk = NULL;
5194 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5195 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005196 return 0;
5197}
5198
5199__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5200{
5201 struct hlua_txn *htxn;
5202
5203 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5204 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5205
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005206 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005207 WILL_LJMP(lua_error(L));
5208
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005209 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005210}
5211
5212__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5213{
5214 struct hlua_txn *htxn;
5215
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005216 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005217 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5218
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005219 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005220 WILL_LJMP(lua_error(L));
5221
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005222 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005223}
5224
5225/* This function adds an header. It is a wrapper used by
5226 * the 2 following functions.
5227 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005228__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005229{
5230 size_t name_len;
5231 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5232 size_t value_len;
5233 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005234 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005235
Christopher Fauleta2097962019-07-15 16:25:33 +02005236 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5237 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005238 return 0;
5239}
5240
5241__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5242{
5243 struct hlua_txn *htxn;
5244
5245 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5246 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5247
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005248 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005249 WILL_LJMP(lua_error(L));
5250
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005251 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005252}
5253
5254__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5255{
5256 struct hlua_txn *htxn;
5257
5258 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5259 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5260
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005261 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005262 WILL_LJMP(lua_error(L));
5263
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005264 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005265}
5266
5267static int hlua_http_req_set_hdr(lua_State *L)
5268{
5269 struct hlua_txn *htxn;
5270
5271 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5272 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5273
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005274 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005275 WILL_LJMP(lua_error(L));
5276
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005277 hlua_http_del_hdr(L, &htxn->s->txn->req);
5278 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005279}
5280
5281static int hlua_http_res_set_hdr(lua_State *L)
5282{
5283 struct hlua_txn *htxn;
5284
5285 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5286 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5287
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005288 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005289 WILL_LJMP(lua_error(L));
5290
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005291 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5292 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005293}
5294
5295/* This function set the method. */
5296static int hlua_http_req_set_meth(lua_State *L)
5297{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005298 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005299 size_t name_len;
5300 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005301
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005302 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005303 WILL_LJMP(lua_error(L));
5304
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005305 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005306 return 1;
5307}
5308
5309/* This function set the method. */
5310static int hlua_http_req_set_path(lua_State *L)
5311{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005312 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005313 size_t name_len;
5314 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005315
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005316 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005317 WILL_LJMP(lua_error(L));
5318
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005319 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005320 return 1;
5321}
5322
5323/* This function set the query-string. */
5324static int hlua_http_req_set_query(lua_State *L)
5325{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005326 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005327 size_t name_len;
5328 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005329
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005330 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005331 WILL_LJMP(lua_error(L));
5332
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005333 /* Check length. */
5334 if (name_len > trash.size - 1) {
5335 lua_pushboolean(L, 0);
5336 return 1;
5337 }
5338
5339 /* Add the mark question as prefix. */
5340 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005341 trash.area[trash.data++] = '?';
5342 memcpy(trash.area + trash.data, name, name_len);
5343 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005344
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005345 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005346 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005347 return 1;
5348}
5349
5350/* This function set the uri. */
5351static int hlua_http_req_set_uri(lua_State *L)
5352{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005353 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005354 size_t name_len;
5355 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005356
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005357 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005358 WILL_LJMP(lua_error(L));
5359
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005360 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005361 return 1;
5362}
5363
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005364/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005365static int hlua_http_res_set_status(lua_State *L)
5366{
5367 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5368 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005369 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5370 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005371
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005372 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005373 WILL_LJMP(lua_error(L));
5374
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005375 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005376 return 0;
5377}
5378
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005379/*
5380 *
5381 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005382 * Class TXN
5383 *
5384 *
5385 */
5386
5387/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005388 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005389 */
5390__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5391{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005392 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005393}
5394
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005395__LJMP static int hlua_set_var(lua_State *L)
5396{
5397 struct hlua_txn *htxn;
5398 const char *name;
5399 size_t len;
5400 struct sample smp;
5401
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005402 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5403 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005404
5405 /* It is useles to retrieve the stream, but this function
5406 * runs only in a stream context.
5407 */
5408 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5409 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5410
5411 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005412 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005413 hlua_lua2smp(L, 3, &smp);
5414
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02005415 /* Store the sample in a variable. We don't need to dup the smp, vars API
5416 * already takes care of duplicating dynamic var data.
5417 */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005418 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005419
5420 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5421 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5422 else
5423 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5424
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005425 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005426}
5427
Christopher Faulet85d79c92016-11-09 16:54:56 +01005428__LJMP static int hlua_unset_var(lua_State *L)
5429{
5430 struct hlua_txn *htxn;
5431 const char *name;
5432 size_t len;
5433 struct sample smp;
5434
5435 MAY_LJMP(check_args(L, 2, "unset_var"));
5436
5437 /* It is useles to retrieve the stream, but this function
5438 * runs only in a stream context.
5439 */
5440 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5441 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5442
5443 /* Unset the variable. */
5444 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005445 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5446 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005447}
5448
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005449__LJMP static int hlua_get_var(lua_State *L)
5450{
5451 struct hlua_txn *htxn;
5452 const char *name;
5453 size_t len;
5454 struct sample smp;
5455
5456 MAY_LJMP(check_args(L, 2, "get_var"));
5457
5458 /* It is useles to retrieve the stream, but this function
5459 * runs only in a stream context.
5460 */
5461 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5462 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5463
Willy Tarreau7560dd42016-03-10 16:28:58 +01005464 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005465 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005466 lua_pushnil(L);
5467 return 1;
5468 }
5469
5470 return hlua_smp2lua(L, &smp);
5471}
5472
Willy Tarreau59551662015-03-10 14:23:13 +01005473__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005474{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005475 struct hlua *hlua;
5476
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005477 MAY_LJMP(check_args(L, 2, "set_priv"));
5478
Willy Tarreau87b09662015-04-03 00:22:06 +02005479 /* It is useles to retrieve the stream, but this function
5480 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005481 */
5482 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005483
5484 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005485 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005486 if (!hlua)
5487 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005488
5489 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005490 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005491
5492 /* Get and store new value. */
5493 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5494 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5495
5496 return 0;
5497}
5498
Willy Tarreau59551662015-03-10 14:23:13 +01005499__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005500{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005501 struct hlua *hlua;
5502
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005503 MAY_LJMP(check_args(L, 1, "get_priv"));
5504
Willy Tarreau87b09662015-04-03 00:22:06 +02005505 /* It is useles to retrieve the stream, but this function
5506 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005507 */
5508 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005509
5510 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005511 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005512 if (!hlua) {
5513 lua_pushnil(L);
5514 return 1;
5515 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005516
5517 /* Push configuration index in the stack. */
5518 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5519
5520 return 1;
5521}
5522
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005523/* Create stack entry containing a class TXN. This function
5524 * return 0 if the stack does not contains free slots,
5525 * otherwise it returns 1.
5526 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005527static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005528{
Willy Tarreaude491382015-04-06 11:04:28 +02005529 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005530
5531 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005532 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005533 return 0;
5534
5535 /* NOTE: The allocation never fails. The failure
5536 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005537 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005538 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005539 /* Create the object: obj[0] = userdata. */
5540 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005541 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005542 lua_rawseti(L, -2, 0);
5543
Willy Tarreaude491382015-04-06 11:04:28 +02005544 htxn->s = s;
5545 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005546 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005547 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005548
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005549 /* Create the "f" field that contains a list of fetches. */
5550 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005551 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005552 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005553 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005554
5555 /* Create the "sf" field that contains a list of stringsafe fetches. */
5556 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005557 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005558 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005559 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005560
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005561 /* Create the "c" field that contains a list of converters. */
5562 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005563 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005564 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005565 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005566
5567 /* Create the "sc" field that contains a list of stringsafe converters. */
5568 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005569 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005570 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005571 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005572
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005573 /* Create the "req" field that contains the request channel object. */
5574 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005575 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005576 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005577 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005578
5579 /* Create the "res" field that contains the response channel object. */
5580 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005581 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005582 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005583 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005584
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005585 /* Creates the HTTP object is the current proxy allows http. */
5586 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005587 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005588 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005589 return 0;
5590 }
5591 else
5592 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005593 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005594
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005595 /* Pop a class sesison metatable and affect it to the userdata. */
5596 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5597 lua_setmetatable(L, -2);
5598
5599 return 1;
5600}
5601
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005602__LJMP static int hlua_txn_deflog(lua_State *L)
5603{
5604 const char *msg;
5605 struct hlua_txn *htxn;
5606
5607 MAY_LJMP(check_args(L, 2, "deflog"));
5608 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5609 msg = MAY_LJMP(luaL_checkstring(L, 2));
5610
5611 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5612 return 0;
5613}
5614
5615__LJMP static int hlua_txn_log(lua_State *L)
5616{
5617 int level;
5618 const char *msg;
5619 struct hlua_txn *htxn;
5620
5621 MAY_LJMP(check_args(L, 3, "log"));
5622 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5623 level = MAY_LJMP(luaL_checkinteger(L, 2));
5624 msg = MAY_LJMP(luaL_checkstring(L, 3));
5625
5626 if (level < 0 || level >= NB_LOG_LEVELS)
5627 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5628
5629 hlua_sendlog(htxn->s->be, level, msg);
5630 return 0;
5631}
5632
5633__LJMP static int hlua_txn_log_debug(lua_State *L)
5634{
5635 const char *msg;
5636 struct hlua_txn *htxn;
5637
5638 MAY_LJMP(check_args(L, 2, "Debug"));
5639 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5640 msg = MAY_LJMP(luaL_checkstring(L, 2));
5641 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5642 return 0;
5643}
5644
5645__LJMP static int hlua_txn_log_info(lua_State *L)
5646{
5647 const char *msg;
5648 struct hlua_txn *htxn;
5649
5650 MAY_LJMP(check_args(L, 2, "Info"));
5651 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5652 msg = MAY_LJMP(luaL_checkstring(L, 2));
5653 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5654 return 0;
5655}
5656
5657__LJMP static int hlua_txn_log_warning(lua_State *L)
5658{
5659 const char *msg;
5660 struct hlua_txn *htxn;
5661
5662 MAY_LJMP(check_args(L, 2, "Warning"));
5663 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5664 msg = MAY_LJMP(luaL_checkstring(L, 2));
5665 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5666 return 0;
5667}
5668
5669__LJMP static int hlua_txn_log_alert(lua_State *L)
5670{
5671 const char *msg;
5672 struct hlua_txn *htxn;
5673
5674 MAY_LJMP(check_args(L, 2, "Alert"));
5675 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5676 msg = MAY_LJMP(luaL_checkstring(L, 2));
5677 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5678 return 0;
5679}
5680
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005681__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5682{
5683 struct hlua_txn *htxn;
5684 int ll;
5685
5686 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5687 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5688 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5689
5690 if (ll < 0 || ll > 7)
5691 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5692
5693 htxn->s->logs.level = ll;
5694 return 0;
5695}
5696
5697__LJMP static int hlua_txn_set_tos(lua_State *L)
5698{
5699 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005700 int tos;
5701
5702 MAY_LJMP(check_args(L, 2, "set_tos"));
5703 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5704 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5705
Willy Tarreau1a18b542018-12-11 16:37:42 +01005706 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005707 return 0;
5708}
5709
5710__LJMP static int hlua_txn_set_mark(lua_State *L)
5711{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005712 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005713 int mark;
5714
5715 MAY_LJMP(check_args(L, 2, "set_mark"));
5716 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5717 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5718
Lukas Tribus579e3e32019-08-11 18:03:45 +02005719 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005720 return 0;
5721}
5722
Patrick Hemmer268a7072018-05-11 12:52:31 -04005723__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5724{
5725 struct hlua_txn *htxn;
5726
5727 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5728 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5729 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5730 return 0;
5731}
5732
5733__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5734{
5735 struct hlua_txn *htxn;
5736
5737 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5738 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5739 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5740 return 0;
5741}
5742
Christopher Faulet700d9e82020-01-31 12:21:52 +01005743/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005744 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005745 * message and terminate the transaction. It returns 1 on success and 0 on
5746 * error. The Reply must be on top of the stack.
5747 */
5748__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5749{
5750 struct htx *htx;
5751 struct htx_sl *sl;
5752 struct h1m h1m;
5753 const char *status, *reason, *body;
5754 size_t status_len, reason_len, body_len;
5755 int ret, code, flags;
5756
5757 code = 200;
5758 status = "200";
5759 status_len = 3;
5760 ret = lua_getfield(L, -1, "status");
5761 if (ret == LUA_TNUMBER) {
5762 code = lua_tointeger(L, -1);
5763 status = lua_tolstring(L, -1, &status_len);
5764 }
5765 lua_pop(L, 1);
5766
5767 reason = http_get_reason(code);
5768 reason_len = strlen(reason);
5769 ret = lua_getfield(L, -1, "reason");
5770 if (ret == LUA_TSTRING)
5771 reason = lua_tolstring(L, -1, &reason_len);
5772 lua_pop(L, 1);
5773
5774 body = NULL;
5775 body_len = 0;
5776 ret = lua_getfield(L, -1, "body");
5777 if (ret == LUA_TSTRING)
5778 body = lua_tolstring(L, -1, &body_len);
5779 lua_pop(L, 1);
5780
5781 /* Prepare the response before inserting the headers */
5782 h1m_init_res(&h1m);
5783 htx = htx_from_buf(&s->res.buf);
5784 channel_htx_truncate(&s->res, htx);
5785 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5786 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5787 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5788 ist2(status, status_len), ist2(reason, reason_len));
5789 }
5790 else {
5791 flags = HTX_SL_F_IS_RESP;
5792 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5793 ist2(status, status_len), ist2(reason, reason_len));
5794 }
5795 if (!sl)
5796 goto fail;
5797 sl->info.res.status = code;
5798
5799 /* Push in the stack the "headers" entry. */
5800 ret = lua_getfield(L, -1, "headers");
5801 if (ret != LUA_TTABLE)
5802 goto skip_headers;
5803
5804 lua_pushnil(L);
5805 while (lua_next(L, -2) != 0) {
5806 struct ist name, value;
5807 const char *n, *v;
5808 size_t nlen, vlen;
5809
5810 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5811 /* Skip element if the key is not a string or if the value is not a table */
5812 goto next_hdr;
5813 }
5814
5815 n = lua_tolstring(L, -2, &nlen);
5816 name = ist2(n, nlen);
5817 if (isteqi(name, ist("content-length"))) {
5818 /* Always skip content-length header. It will be added
5819 * later with the correct len
5820 */
5821 goto next_hdr;
5822 }
5823
5824 /* Loop on header's values */
5825 lua_pushnil(L);
5826 while (lua_next(L, -2)) {
5827 if (!lua_isstring(L, -1)) {
5828 /* Skip the value if it is not a string */
5829 goto next_value;
5830 }
5831
5832 v = lua_tolstring(L, -1, &vlen);
5833 value = ist2(v, vlen);
5834
5835 if (isteqi(name, ist("transfer-encoding")))
5836 h1_parse_xfer_enc_header(&h1m, value);
5837 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5838 goto fail;
5839
5840 next_value:
5841 lua_pop(L, 1);
5842 }
5843
5844 next_hdr:
5845 lua_pop(L, 1);
5846 }
5847 skip_headers:
5848 lua_pop(L, 1);
5849
5850 /* Update h1m flags: CLEN is set if CHNK is not present */
5851 if (!(h1m.flags & H1_MF_CHNK)) {
5852 const char *clen = ultoa(body_len);
5853
5854 h1m.flags |= H1_MF_CLEN;
5855 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5856 goto fail;
5857 }
5858 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5859 h1m.flags |= H1_MF_XFER_LEN;
5860
5861 /* Update HTX start-line flags */
5862 if (h1m.flags & H1_MF_XFER_ENC)
5863 flags |= HTX_SL_F_XFER_ENC;
5864 if (h1m.flags & H1_MF_XFER_LEN) {
5865 flags |= HTX_SL_F_XFER_LEN;
5866 if (h1m.flags & H1_MF_CHNK)
5867 flags |= HTX_SL_F_CHNK;
5868 else if (h1m.flags & H1_MF_CLEN)
5869 flags |= HTX_SL_F_CLEN;
5870 if (h1m.body_len == 0)
5871 flags |= HTX_SL_F_BODYLESS;
5872 }
5873 sl->flags |= flags;
5874
5875
5876 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005877 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005878 goto fail;
5879
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005880 htx->flags |= HTX_FL_EOM;
5881
Christopher Faulet700d9e82020-01-31 12:21:52 +01005882 /* Now, forward the response and terminate the transaction */
5883 s->txn->status = code;
5884 htx_to_buf(htx, &s->res.buf);
5885 if (!http_forward_proxy_resp(s, 1))
5886 goto fail;
5887
5888 return 1;
5889
5890 fail:
5891 channel_htx_truncate(&s->res, htx);
5892 return 0;
5893}
5894
5895/* Terminate a transaction if called from a lua action. For TCP streams,
5896 * processing is just aborted. Nothing is returned to the client and all
5897 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5898 * is forwarded to the client before terminating the transaction. On success,
5899 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5900 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5901 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005902 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005903__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005904{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005905 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005906 struct stream *s;
5907 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005908
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005909 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005910
Christopher Faulet700d9e82020-01-31 12:21:52 +01005911 /* If the flags NOTERM is set, we cannot terminate the session, so we
5912 * just end the execution of the current lua code. */
5913 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005914 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005915
Christopher Faulet700d9e82020-01-31 12:21:52 +01005916 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005917 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005918 struct channel *req = &s->req;
5919 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005920
Christopher Faulet700d9e82020-01-31 12:21:52 +01005921 channel_auto_read(req);
5922 channel_abort(req);
5923 channel_auto_close(req);
5924 channel_erase(req);
5925
5926 res->wex = tick_add_ifset(now_ms, res->wto);
5927 channel_auto_read(res);
5928 channel_auto_close(res);
5929 channel_shutr_now(res);
5930
5931 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5932 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005933 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005934
Christopher Faulet700d9e82020-01-31 12:21:52 +01005935 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5936 /* No reply or invalid reply */
5937 s->txn->status = 0;
5938 http_reply_and_close(s, 0, NULL);
5939 }
5940 else {
5941 /* Remove extra args to have the reply on top of the stack */
5942 if (lua_gettop(L) > 2)
5943 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005944
Christopher Faulet700d9e82020-01-31 12:21:52 +01005945 if (!hlua_txn_forward_reply(L, s)) {
5946 if (!(s->flags & SF_ERR_MASK))
5947 s->flags |= SF_ERR_PRXCOND;
5948 lua_pushinteger(L, ACT_RET_ERR);
5949 WILL_LJMP(hlua_done(L));
5950 return 0; /* Never reached */
5951 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005952 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005953
Christopher Faulet700d9e82020-01-31 12:21:52 +01005954 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5955 if (htxn->dir == SMP_OPT_DIR_REQ) {
5956 /* let's log the request time */
5957 s->logs.tv_request = now;
5958 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02005959 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01005960 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005961
Christopher Faulet700d9e82020-01-31 12:21:52 +01005962 done:
5963 if (!(s->flags & SF_ERR_MASK))
5964 s->flags |= SF_ERR_LOCAL;
5965 if (!(s->flags & SF_FINST_MASK))
5966 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005967
Christopher Faulet4ad73102020-03-05 11:07:31 +01005968 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005969 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005970 return 0;
5971}
5972
Christopher Faulet700d9e82020-01-31 12:21:52 +01005973/*
5974 *
5975 *
5976 * Class REPLY
5977 *
5978 *
5979 */
5980
5981/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5982 * free slots, the function fails and returns 0;
5983 */
5984static int hlua_txn_reply_new(lua_State *L)
5985{
5986 struct hlua_txn *htxn;
5987 const char *reason, *body = NULL;
5988 int ret, status;
5989
5990 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005991 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005992 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5993 WILL_LJMP(lua_error(L));
5994 }
5995
5996 /* Default value */
5997 status = 200;
5998 reason = http_get_reason(status);
5999
6000 if (lua_istable(L, 2)) {
6001 /* load status and reason from the table argument at index 2 */
6002 ret = lua_getfield(L, 2, "status");
6003 if (ret == LUA_TNIL)
6004 goto reason;
6005 else if (ret != LUA_TNUMBER) {
6006 /* invalid status: ignore the reason */
6007 goto body;
6008 }
6009 status = lua_tointeger(L, -1);
6010
6011 reason:
6012 lua_pop(L, 1); /* restore the stack: remove status */
6013 ret = lua_getfield(L, 2, "reason");
6014 if (ret == LUA_TSTRING)
6015 reason = lua_tostring(L, -1);
6016
6017 body:
6018 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
6019 ret = lua_getfield(L, 2, "body");
6020 if (ret == LUA_TSTRING)
6021 body = lua_tostring(L, -1);
6022 lua_pop(L, 1); /* restore the stack: remove body */
6023 }
6024
6025 /* Create the Reply table */
6026 lua_newtable(L);
6027
6028 /* Add status element */
6029 lua_pushstring(L, "status");
6030 lua_pushinteger(L, status);
6031 lua_settable(L, -3);
6032
6033 /* Add reason element */
6034 reason = http_get_reason(status);
6035 lua_pushstring(L, "reason");
6036 lua_pushstring(L, reason);
6037 lua_settable(L, -3);
6038
6039 /* Add body element, nil if undefined */
6040 lua_pushstring(L, "body");
6041 if (body)
6042 lua_pushstring(L, body);
6043 else
6044 lua_pushnil(L);
6045 lua_settable(L, -3);
6046
6047 /* Add headers element */
6048 lua_pushstring(L, "headers");
6049 lua_newtable(L);
6050
6051 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6052 if (lua_istable(L, 2)) {
6053 /* load headers from the table argument at index 2. If it is a table, copy it. */
6054 ret = lua_getfield(L, 2, "headers");
6055 if (ret == LUA_TTABLE) {
6056 /* stack: [ ... <headers:table>, <table> ] */
6057 lua_pushnil(L);
6058 while (lua_next(L, -2) != 0) {
6059 /* stack: [ ... <headers:table>, <table>, k, v] */
6060 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
6061 /* invalid value type, skip it */
6062 lua_pop(L, 1);
6063 continue;
6064 }
6065
6066
6067 /* Duplicate the key and swap it with the value. */
6068 lua_pushvalue(L, -2);
6069 lua_insert(L, -2);
6070 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6071
6072 lua_newtable(L);
6073 lua_insert(L, -2);
6074 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6075
6076 if (lua_isstring(L, -1)) {
6077 /* push the value in the inner table */
6078 lua_rawseti(L, -2, 1);
6079 }
6080 else { /* table */
6081 lua_pushnil(L);
6082 while (lua_next(L, -2) != 0) {
6083 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6084 if (!lua_isstring(L, -1)) {
6085 /* invalid value type, skip it*/
6086 lua_pop(L, 1);
6087 continue;
6088 }
6089 /* push the value in the inner table */
6090 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6091 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6092 }
6093 lua_pop(L, 1);
6094 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6095 }
6096
6097 /* push (k,v) on the stack in the headers table:
6098 * stack: [ ... <headers:table>, <table>, k, k, v ]
6099 */
6100 lua_settable(L, -5);
6101 /* stack: [ ... <headers:table>, <table>, k ] */
6102 }
6103 }
6104 lua_pop(L, 1);
6105 }
6106 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6107 lua_settable(L, -3);
6108 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6109
6110 /* Pop a class sesison metatable and affect it to the userdata. */
6111 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6112 lua_setmetatable(L, -2);
6113 return 1;
6114}
6115
6116/* Set the reply status code, and optionally the reason. If no reason is
6117 * provided, the default one corresponding to the status code is used.
6118 */
6119__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6120{
6121 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6122 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6123
6124 /* First argument (self) must be a table */
6125 luaL_checktype(L, 1, LUA_TTABLE);
6126
6127 if (status < 100 || status > 599) {
6128 lua_pushboolean(L, 0);
6129 return 1;
6130 }
6131 if (!reason)
6132 reason = http_get_reason(status);
6133
6134 lua_pushinteger(L, status);
6135 lua_setfield(L, 1, "status");
6136
6137 lua_pushstring(L, reason);
6138 lua_setfield(L, 1, "reason");
6139
6140 lua_pushboolean(L, 1);
6141 return 1;
6142}
6143
6144/* Add a header into the reply object. Each header name is associated to an
6145 * array of values in the "headers" table. If the header name is not found, a
6146 * new entry is created.
6147 */
6148__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6149{
6150 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6151 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6152 int ret;
6153
6154 /* First argument (self) must be a table */
6155 luaL_checktype(L, 1, LUA_TTABLE);
6156
6157 /* Push in the stack the "headers" entry. */
6158 ret = lua_getfield(L, 1, "headers");
6159 if (ret != LUA_TTABLE) {
6160 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6161 WILL_LJMP(lua_error(L));
6162 }
6163
6164 /* check if the header is already registered. If not, register it. */
6165 ret = lua_getfield(L, -1, name);
6166 if (ret == LUA_TNIL) {
6167 /* Entry not found. */
6168 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6169
6170 /* Insert the new header name in the array in the top of the stack.
6171 * It left the new array in the top of the stack.
6172 */
6173 lua_newtable(L);
6174 lua_pushstring(L, name);
6175 lua_pushvalue(L, -2);
6176 lua_settable(L, -4);
6177 }
6178 else if (ret != LUA_TTABLE) {
6179 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6180 WILL_LJMP(lua_error(L));
6181 }
6182
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006183 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006184 * the header value as new entry.
6185 */
6186 lua_pushstring(L, value);
6187 ret = lua_rawlen(L, -2);
6188 lua_rawseti(L, -2, ret + 1);
6189
6190 lua_pushboolean(L, 1);
6191 return 1;
6192}
6193
6194/* Remove all occurrences of a given header name. */
6195__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6196{
6197 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6198 int ret;
6199
6200 /* First argument (self) must be a table */
6201 luaL_checktype(L, 1, LUA_TTABLE);
6202
6203 /* Push in the stack the "headers" entry. */
6204 ret = lua_getfield(L, 1, "headers");
6205 if (ret != LUA_TTABLE) {
6206 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6207 WILL_LJMP(lua_error(L));
6208 }
6209
6210 lua_pushstring(L, name);
6211 lua_pushnil(L);
6212 lua_settable(L, -3);
6213
6214 lua_pushboolean(L, 1);
6215 return 1;
6216}
6217
6218/* Set the reply's body. Overwrite any existing entry. */
6219__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6220{
6221 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6222
6223 /* First argument (self) must be a table */
6224 luaL_checktype(L, 1, LUA_TTABLE);
6225
6226 lua_pushstring(L, payload);
6227 lua_setfield(L, 1, "body");
6228
6229 lua_pushboolean(L, 1);
6230 return 1;
6231}
6232
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006233__LJMP static int hlua_log(lua_State *L)
6234{
6235 int level;
6236 const char *msg;
6237
6238 MAY_LJMP(check_args(L, 2, "log"));
6239 level = MAY_LJMP(luaL_checkinteger(L, 1));
6240 msg = MAY_LJMP(luaL_checkstring(L, 2));
6241
6242 if (level < 0 || level >= NB_LOG_LEVELS)
6243 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6244
6245 hlua_sendlog(NULL, level, msg);
6246 return 0;
6247}
6248
6249__LJMP static int hlua_log_debug(lua_State *L)
6250{
6251 const char *msg;
6252
6253 MAY_LJMP(check_args(L, 1, "debug"));
6254 msg = MAY_LJMP(luaL_checkstring(L, 1));
6255 hlua_sendlog(NULL, LOG_DEBUG, msg);
6256 return 0;
6257}
6258
6259__LJMP static int hlua_log_info(lua_State *L)
6260{
6261 const char *msg;
6262
6263 MAY_LJMP(check_args(L, 1, "info"));
6264 msg = MAY_LJMP(luaL_checkstring(L, 1));
6265 hlua_sendlog(NULL, LOG_INFO, msg);
6266 return 0;
6267}
6268
6269__LJMP static int hlua_log_warning(lua_State *L)
6270{
6271 const char *msg;
6272
6273 MAY_LJMP(check_args(L, 1, "warning"));
6274 msg = MAY_LJMP(luaL_checkstring(L, 1));
6275 hlua_sendlog(NULL, LOG_WARNING, msg);
6276 return 0;
6277}
6278
6279__LJMP static int hlua_log_alert(lua_State *L)
6280{
6281 const char *msg;
6282
6283 MAY_LJMP(check_args(L, 1, "alert"));
6284 msg = MAY_LJMP(luaL_checkstring(L, 1));
6285 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006286 return 0;
6287}
6288
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006289__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006290{
6291 int wakeup_ms = lua_tointeger(L, -1);
Willy Tarreau9cc2e4c2021-09-30 16:12:31 +02006292 if (!tick_is_expired(wakeup_ms, now_ms))
Willy Tarreau9635e032018-10-16 17:52:55 +02006293 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006294 return 0;
6295}
6296
6297__LJMP static int hlua_sleep(lua_State *L)
6298{
6299 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006300 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006301
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006302 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006303
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006304 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006305 wakeup_ms = tick_add(now_ms, delay);
6306 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006307
Willy Tarreau9635e032018-10-16 17:52:55 +02006308 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006309 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006310}
6311
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006312__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006313{
6314 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006315 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006316
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006317 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006318
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006319 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006320 wakeup_ms = tick_add(now_ms, delay);
6321 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006322
Willy Tarreau9635e032018-10-16 17:52:55 +02006323 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006324 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006325}
6326
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006327/* This functionis an LUA binding. it permits to give back
6328 * the hand at the HAProxy scheduler. It is used when the
6329 * LUA processing consumes a lot of time.
6330 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006331__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006332{
6333 return 0;
6334}
6335
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006336__LJMP static int hlua_yield(lua_State *L)
6337{
Willy Tarreau9635e032018-10-16 17:52:55 +02006338 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006339 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006340}
6341
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006342/* This function change the nice of the currently executed
6343 * task. It is used set low or high priority at the current
6344 * task.
6345 */
Willy Tarreau59551662015-03-10 14:23:13 +01006346__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006347{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006348 struct hlua *hlua;
6349 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006350
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006351 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006352 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006353
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006354 /* Get hlua struct, or NULL if we execute from main lua state */
6355 hlua = hlua_gethlua(L);
6356
6357 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006358 if (!hlua || !hlua->task)
6359 return 0;
6360
6361 if (nice < -1024)
6362 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006363 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006364 nice = 1024;
6365
6366 hlua->task->nice = nice;
6367 return 0;
6368}
6369
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006370/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006371 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6372 * return an E_AGAIN signal, the emmiter of this signal must set a
6373 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006374 *
6375 * Task wrapper are longjmp safe because the only one Lua code
6376 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006377 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006378struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006379{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006380 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006381 enum hlua_exec status;
6382
Christopher Faulet5bc99722018-04-25 10:34:45 +02006383 if (task->thread_mask == MAX_THREADS_MASK)
6384 task_set_affinity(task, tid_bit);
6385
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006386 /* If it is the first call to the task, we must initialize the
6387 * execution timeouts.
6388 */
6389 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006390 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006391
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006392 /* Execute the Lua code. */
6393 status = hlua_ctx_resume(hlua, 1);
6394
6395 switch (status) {
6396 /* finished or yield */
6397 case HLUA_E_OK:
6398 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006399 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006400 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006401 break;
6402
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006403 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006404 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006405 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006406 break;
6407
6408 /* finished with error. */
6409 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006410 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006411 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006412 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006413 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006414 break;
6415
6416 case HLUA_E_ERR:
6417 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006418 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006419 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006420 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006421 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006422 break;
6423 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006424 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006425}
6426
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006427/* This function is an LUA binding that register LUA function to be
6428 * executed after the HAProxy configuration parsing and before the
6429 * HAProxy scheduler starts. This function expect only one LUA
6430 * argument that is a function. This function returns nothing, but
6431 * throws if an error is encountered.
6432 */
6433__LJMP static int hlua_register_init(lua_State *L)
6434{
6435 struct hlua_init_function *init;
6436 int ref;
6437
6438 MAY_LJMP(check_args(L, 1, "register_init"));
6439
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006440 if (hlua_gethlua(L)) {
6441 /* runtime processing */
6442 WILL_LJMP(luaL_error(L, "register_init: not available outside of body context"));
6443 }
6444
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006445 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6446
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006447 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006448 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006449 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006450
6451 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006452 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006453 return 0;
6454}
6455
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006456/* This functio is an LUA binding. It permits to register a task
6457 * executed in parallel of the main HAroxy activity. The task is
6458 * created and it is set in the HAProxy scheduler. It can be called
6459 * from the "init" section, "post init" or during the runtime.
6460 *
6461 * Lua prototype:
6462 *
6463 * <none> core.register_task(<function>)
6464 */
6465static int hlua_register_task(lua_State *L)
6466{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006467 struct hlua *hlua = NULL;
6468 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006469 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006470 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006471
6472 MAY_LJMP(check_args(L, 1, "register_task"));
6473
6474 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6475
Thierry Fournier75fc0292020-11-28 13:18:56 +01006476 /* Get the reference state. If the reference is NULL, L is the master
6477 * state, otherwise hlua->T is.
6478 */
6479 hlua = hlua_gethlua(L);
6480 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006481 /* we are in runtime processing */
6482 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006483 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006484 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006485 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006486
Willy Tarreaubafbe012017-11-24 17:34:44 +01006487 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006488 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006489 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006490 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006491
Thierry Fournier59f11be2020-11-29 00:37:41 +01006492 /* We are in the common lua state, execute the task anywhere,
6493 * otherwise, inherit the current thread identifier
6494 */
6495 if (state_id == 0)
6496 task = task_new(MAX_THREADS_MASK);
6497 else
6498 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006499 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006500 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006501
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006502 task->context = hlua;
6503 task->process = hlua_process_task;
6504
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01006505 if (!hlua_ctx_init(hlua, state_id, task))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006506 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006507
6508 /* Restore the function in the stack. */
6509 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
Aurelien DARRAGONb6aade32023-03-13 14:09:21 +01006510 /* function ref not needed anymore since it was pushed to the substack */
6511 hlua_unref(L, ref);
6512
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006513 hlua->nargs = 0;
6514
6515 /* Schedule task. */
Willy Tarreau790810f2021-09-30 16:17:37 +02006516 task_wakeup(task, TASK_WOKEN_INIT);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006517
6518 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006519
6520 alloc_error:
6521 task_destroy(task);
6522 hlua_ctx_destroy(hlua);
6523 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6524 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006525}
6526
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006527/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6528 * doesn't allow "yield" functions because the HAProxy engine cannot
6529 * resume converters.
6530 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006531static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006532{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006533 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006534 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006535 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006536
Willy Tarreaube508f12016-03-10 11:47:01 +01006537 if (!stream)
6538 return 0;
6539
Willy Tarreau87b09662015-04-03 00:22:06 +02006540 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006541 * Lua context can be not initialized. This behavior
6542 * permits to save performances because a systematic
6543 * Lua initialization cause 5% performances loss.
6544 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006545 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006546 struct hlua *hlua;
6547
6548 hlua = pool_alloc(pool_head_hlua);
6549 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006550 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6551 return 0;
6552 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006553 HLUA_INIT(hlua);
6554 stream->hlua = hlua;
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01006555 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006556 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6557 return 0;
6558 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006559 }
6560
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006561 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006562 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006563
6564 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006565 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006566 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6567 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006568 else
6569 error = "critical error";
6570 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006571 return 0;
6572 }
6573
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006574 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006576 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006577 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006578 return 0;
6579 }
6580
6581 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006582 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006583
6584 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006585 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006586 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006587 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006588 return 0;
6589 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006590 hlua_smp2lua(stream->hlua->T, smp);
6591 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006592
6593 /* push keywords in the stack. */
6594 if (arg_p) {
6595 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006596 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006597 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006598 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006599 return 0;
6600 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006601 hlua_arg2lua(stream->hlua->T, arg_p);
6602 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006603 }
6604 }
6605
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006606 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006607 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006608
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006609 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006610 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006611 }
6612
6613 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006614 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006615 /* finished. */
6616 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006617 /* If the stack is empty, the function fails. */
6618 if (lua_gettop(stream->hlua->T) <= 0)
6619 return 0;
6620
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006621 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006622 hlua_lua2smp(stream->hlua->T, -1, smp);
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02006623 /* dup the smp before popping the related lua value and
6624 * returning it to haproxy
6625 */
6626 smp_dup(smp);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006627 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006628 return 1;
6629
6630 /* yield. */
6631 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006632 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006633 return 0;
6634
6635 /* finished with error. */
6636 case HLUA_E_ERRMSG:
6637 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006638 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006639 fcn->name, lua_tostring(stream->hlua->T, -1));
6640 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006641 return 0;
6642
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006643 case HLUA_E_ETMOUT:
6644 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6645 return 0;
6646
6647 case HLUA_E_NOMEM:
6648 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6649 return 0;
6650
6651 case HLUA_E_YIELD:
6652 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6653 return 0;
6654
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006655 case HLUA_E_ERR:
6656 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006657 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006658 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006659
6660 default:
6661 return 0;
6662 }
6663}
6664
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006665/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6666 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006667 * resume sample-fetches. This function will be called by the sample
6668 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006669 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006670static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6671 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006672{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006673 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006674 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006675 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006676 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006677
Willy Tarreaube508f12016-03-10 11:47:01 +01006678 if (!stream)
6679 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006680
Willy Tarreau87b09662015-04-03 00:22:06 +02006681 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006682 * Lua context can be not initialized. This behavior
6683 * permits to save performances because a systematic
6684 * Lua initialization cause 5% performances loss.
6685 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006686 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006687 struct hlua *hlua;
6688
6689 hlua = pool_alloc(pool_head_hlua);
6690 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006691 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6692 return 0;
6693 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006694 hlua->T = NULL;
6695 stream->hlua = hlua;
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01006696 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006697 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6698 return 0;
6699 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006700 }
6701
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006702 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006703 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006704
6705 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006706 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006707 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6708 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006709 else
6710 error = "critical error";
6711 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006712 return 0;
6713 }
6714
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006715 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006716 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006717 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006718 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006719 return 0;
6720 }
6721
6722 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006723 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006724
6725 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006726 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006727 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006728 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006729 return 0;
6730 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006731 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006732
6733 /* push keywords in the stack. */
6734 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6735 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006736 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006737 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006738 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006739 return 0;
6740 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006741 hlua_arg2lua(stream->hlua->T, arg_p);
6742 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006743 }
6744
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006745 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006746 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006747
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006748 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006749 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006750 }
6751
6752 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006753 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006754 /* finished. */
6755 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006756 /* If the stack is empty, the function fails. */
6757 if (lua_gettop(stream->hlua->T) <= 0)
6758 return 0;
6759
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006760 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006761 hlua_lua2smp(stream->hlua->T, -1, smp);
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02006762 /* dup the smp before popping the related lua value and
6763 * returning it to haproxy
6764 */
6765 smp_dup(smp);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006766 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006767
6768 /* Set the end of execution flag. */
6769 smp->flags &= ~SMP_F_MAY_CHANGE;
6770 return 1;
6771
6772 /* yield. */
6773 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006774 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006775 return 0;
6776
6777 /* finished with error. */
6778 case HLUA_E_ERRMSG:
6779 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006780 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006781 fcn->name, lua_tostring(stream->hlua->T, -1));
6782 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006783 return 0;
6784
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006785 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006786 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6787 return 0;
6788
6789 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006790 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6791 return 0;
6792
6793 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006794 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6795 return 0;
6796
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006797 case HLUA_E_ERR:
6798 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006799 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006800 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006801
6802 default:
6803 return 0;
6804 }
6805}
6806
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006807/* This function is an LUA binding used for registering
6808 * "sample-conv" functions. It expects a converter name used
6809 * in the haproxy configuration file, and an LUA function.
6810 */
6811__LJMP static int hlua_register_converters(lua_State *L)
6812{
6813 struct sample_conv_kw_list *sck;
6814 const char *name;
6815 int ref;
6816 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02006817 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006818 struct sample_conv *sc;
6819 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006820
6821 MAY_LJMP(check_args(L, 2, "register_converters"));
6822
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006823 if (hlua_gethlua(L)) {
6824 /* runtime processing */
6825 WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context"));
6826 }
6827
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006828 /* First argument : converter name. */
6829 name = MAY_LJMP(luaL_checkstring(L, 1));
6830
6831 /* Second argument : lua function. */
6832 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6833
Thierry Fournierf67442e2020-11-28 20:41:07 +01006834 /* Check if the converter is already registered */
6835 trash = get_trash_chunk();
6836 chunk_printf(trash, "lua.%s", name);
6837 sc = find_sample_conv(trash->area, trash->data);
6838 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006839 fcn = sc->private;
6840 if (fcn->function_ref[hlua_state_id] != -1) {
6841 ha_warning("Trying to register converter 'lua.%s' more than once. "
6842 "This will become a hard error in version 2.5.\n", name);
6843 }
6844 fcn->function_ref[hlua_state_id] = ref;
6845 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006846 }
6847
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006848 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006849 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006850 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02006851 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006852 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006853 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02006854 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006855
6856 /* Fill fcn. */
6857 fcn->name = strdup(name);
6858 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02006859 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006860 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006861
6862 /* List head */
6863 sck->list.n = sck->list.p = NULL;
6864
6865 /* converter keyword. */
6866 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006867 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006868 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02006869 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006870
6871 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6872 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006873 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 +01006874 sck->kw[0].val_args = NULL;
6875 sck->kw[0].in_type = SMP_T_STR;
6876 sck->kw[0].out_type = SMP_T_STR;
6877 sck->kw[0].private = fcn;
6878
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006879 /* Register this new converter */
6880 sample_register_convs(sck);
6881
6882 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02006883
6884 alloc_error:
6885 release_hlua_function(fcn);
6886 ha_free(&sck);
6887 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6888 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006889}
6890
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006891/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006892 * "sample-fetch" functions. It expects a converter name used
6893 * in the haproxy configuration file, and an LUA function.
6894 */
6895__LJMP static int hlua_register_fetches(lua_State *L)
6896{
6897 const char *name;
6898 int ref;
6899 int len;
6900 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02006901 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006902 struct sample_fetch *sf;
6903 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006904
6905 MAY_LJMP(check_args(L, 2, "register_fetches"));
6906
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006907 if (hlua_gethlua(L)) {
6908 /* runtime processing */
6909 WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context"));
6910 }
6911
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006912 /* First argument : sample-fetch name. */
6913 name = MAY_LJMP(luaL_checkstring(L, 1));
6914
6915 /* Second argument : lua function. */
6916 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6917
Thierry Fournierf67442e2020-11-28 20:41:07 +01006918 /* Check if the sample-fetch is already registered */
6919 trash = get_trash_chunk();
6920 chunk_printf(trash, "lua.%s", name);
6921 sf = find_sample_fetch(trash->area, trash->data);
6922 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006923 fcn = sf->private;
6924 if (fcn->function_ref[hlua_state_id] != -1) {
6925 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6926 "This will become a hard error in version 2.5.\n", name);
6927 }
6928 fcn->function_ref[hlua_state_id] = ref;
6929 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006930 }
6931
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006932 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006933 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006934 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02006935 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006936 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006937 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02006938 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006939
6940 /* Fill fcn. */
6941 fcn->name = strdup(name);
6942 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02006943 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006944 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006945
6946 /* List head */
6947 sfk->list.n = sfk->list.p = NULL;
6948
6949 /* sample-fetch keyword. */
6950 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006951 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006952 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02006953 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006954
6955 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6956 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006957 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 +01006958 sfk->kw[0].val_args = NULL;
6959 sfk->kw[0].out_type = SMP_T_STR;
6960 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6961 sfk->kw[0].val = 0;
6962 sfk->kw[0].private = fcn;
6963
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006964 /* Register this new fetch. */
6965 sample_register_fetches(sfk);
6966
6967 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02006968
6969 alloc_error:
6970 release_hlua_function(fcn);
6971 ha_free(&sfk);
6972 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6973 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006974}
6975
Christopher Faulet501465d2020-02-26 14:54:16 +01006976/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006977 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006978__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006979{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006980 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006981 unsigned int delay;
6982 unsigned int wakeup_ms;
6983
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006984 /* Get hlua struct, or NULL if we execute from main lua state */
6985 hlua = hlua_gethlua(L);
6986 if (!hlua) {
6987 return 0;
6988 }
6989
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006990 MAY_LJMP(check_args(L, 1, "wake_time"));
6991
6992 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6993 wakeup_ms = tick_add(now_ms, delay);
6994 hlua->wake_time = wakeup_ms;
6995 return 0;
6996}
6997
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006998/* This function is a wrapper to execute each LUA function declared as an action
6999 * wrapper during the initialisation period. This function may return any
7000 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
7001 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
7002 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007003 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007004static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02007005 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007006{
7007 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007008 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007009 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007010 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007011
7012 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01007013 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
7014 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
7015 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
7016 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007017 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007018 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007019 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007020 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007021
Willy Tarreau87b09662015-04-03 00:22:06 +02007022 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007023 * Lua context can be not initialized. This behavior
7024 * permits to save performances because a systematic
7025 * Lua initialization cause 5% performances loss.
7026 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007027 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007028 struct hlua *hlua;
7029
7030 hlua = pool_alloc(pool_head_hlua);
7031 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007032 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007033 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007034 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007035 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007036 HLUA_INIT(hlua);
7037 s->hlua = hlua;
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01007038 if (!hlua_ctx_init(s->hlua, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn), s->task)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007039 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007040 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007041 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007042 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007043 }
7044
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007045 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007046 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007047
7048 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007049 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007050 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
7051 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01007052 else
7053 error = "critical error";
7054 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007055 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007056 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007057 }
7058
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007059 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007060 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007061 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007062 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007063 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007064 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007065 }
7066
7067 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007068 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 +01007069
Willy Tarreau87b09662015-04-03 00:22:06 +02007070 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007071 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007072 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007073 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007074 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007075 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007076 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007077 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007078
7079 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007080 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007081 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007082 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007083 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007084 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007085 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007086 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007087 lua_pushstring(s->hlua->T, *arg);
7088 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007089 }
7090
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007091 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007092 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007093
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007094 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007095 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007096 }
7097
7098 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01007099 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007100 /* finished. */
7101 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007102 /* Catch the return value */
7103 if (lua_gettop(s->hlua->T) > 0)
7104 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007105
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007106 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007107 if (act_ret == ACT_RET_YIELD) {
7108 if (flags & ACT_OPT_FINAL)
7109 goto err_yield;
7110
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007111 if (dir == SMP_OPT_DIR_REQ)
7112 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7113 s->hlua->wake_time);
7114 else
7115 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7116 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007117 }
7118 goto end;
7119
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007120 /* yield. */
7121 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007122 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007123 if (dir == SMP_OPT_DIR_REQ)
7124 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7125 s->hlua->wake_time);
7126 else
7127 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7128 s->hlua->wake_time);
7129
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007130 /* Some actions can be wake up when a "write" event
7131 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007132 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007133 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007134 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007135 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007136 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007137 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007138 act_ret = ACT_RET_YIELD;
7139 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007140
7141 /* finished with error. */
7142 case HLUA_E_ERRMSG:
7143 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007144 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007145 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007146 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007147 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007148
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007149 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007150 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007151 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007152
7153 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007154 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007155 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007156
7157 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007158 err_yield:
7159 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007160 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007161 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007162 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007163
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007164 case HLUA_E_ERR:
7165 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007166 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007167 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007168
7169 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007170 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007171 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007172
7173 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007174 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007175 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007176 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007177}
7178
Willy Tarreau144f84a2021-03-02 16:09:26 +01007179struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007180{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007181 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007182
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007183 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007184 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007185 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007186}
7187
7188static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7189{
7190 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007191 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007192 struct task *task;
7193 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007194 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007195
Willy Tarreaubafbe012017-11-24 17:34:44 +01007196 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007197 if (!hlua) {
7198 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007199 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007200 return 0;
7201 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007202 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007203 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007204 ctx->ctx.hlua_apptcp.flags = 0;
7205
7206 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007207 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007208 if (!task) {
7209 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007210 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007211 return 0;
7212 }
7213 task->nice = 0;
7214 task->context = ctx;
7215 task->process = hlua_applet_wakeup;
7216 ctx->ctx.hlua_apptcp.task = task;
7217
7218 /* In the execution wrappers linked with a stream, the
7219 * Lua context can be not initialized. This behavior
7220 * permits to save performances because a systematic
7221 * Lua initialization cause 5% performances loss.
7222 */
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01007223 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007224 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007225 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007226 return 0;
7227 }
7228
7229 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007230 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007231
7232 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007233 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007234 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7235 error = lua_tostring(hlua->T, -1);
7236 else
7237 error = "critical error";
7238 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007239 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007240 return 0;
7241 }
7242
7243 /* Check stack available size. */
7244 if (!lua_checkstack(hlua->T, 1)) {
7245 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007246 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007247 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007248 return 0;
7249 }
7250
7251 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007252 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007253
7254 /* Create and and push object stream in the stack. */
7255 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7256 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007257 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007258 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007259 return 0;
7260 }
7261 hlua->nargs = 1;
7262
7263 /* push keywords in the stack. */
7264 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7265 if (!lua_checkstack(hlua->T, 1)) {
7266 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007267 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007268 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007269 return 0;
7270 }
7271 lua_pushstring(hlua->T, *arg);
7272 hlua->nargs++;
7273 }
7274
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007275 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007276
7277 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007278 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007279 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007280
7281 return 1;
7282}
7283
Willy Tarreau60409db2019-08-21 14:14:50 +02007284void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007285{
7286 struct stream_interface *si = ctx->owner;
7287 struct stream *strm = si_strm(si);
7288 struct channel *res = si_ic(si);
7289 struct act_rule *rule = ctx->rule;
7290 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007291 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007292
7293 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007294 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7295 /* eat the whole request */
7296 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007297 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007298 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007299
7300 /* If the stream is disconnect or closed, ldo nothing. */
7301 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7302 return;
7303
7304 /* Execute the function. */
7305 switch (hlua_ctx_resume(hlua, 1)) {
7306 /* finished. */
7307 case HLUA_E_OK:
7308 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7309
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007310 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007311 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007312 res->flags |= CF_READ_NULL;
7313 si_shutr(si);
7314 return;
7315
7316 /* yield. */
7317 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007318 if (hlua->wake_time != TICK_ETERNITY)
7319 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007320 return;
7321
7322 /* finished with error. */
7323 case HLUA_E_ERRMSG:
7324 /* Display log. */
7325 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007326 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007327 lua_pop(hlua->T, 1);
7328 goto error;
7329
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007330 case HLUA_E_ETMOUT:
7331 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007332 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007333 goto error;
7334
7335 case HLUA_E_NOMEM:
7336 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007337 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007338 goto error;
7339
7340 case HLUA_E_YIELD: /* unexpected */
7341 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007342 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007343 goto error;
7344
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007345 case HLUA_E_ERR:
7346 /* Display log. */
7347 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007348 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007349 goto error;
7350
7351 default:
7352 goto error;
7353 }
7354
7355error:
7356
7357 /* For all other cases, just close the stream. */
7358 si_shutw(si);
7359 si_shutr(si);
7360 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7361}
7362
7363static void hlua_applet_tcp_release(struct appctx *ctx)
7364{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007365 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007366 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007367 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007368 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007369}
7370
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007371/* The function returns 1 if the initialisation is complete, 0 if
7372 * an errors occurs and -1 if more data are required for initializing
7373 * the applet.
7374 */
7375static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7376{
7377 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007378 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007379 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007380 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007381 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007382 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007383
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007384 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007385 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007386 if (!hlua) {
7387 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007388 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007389 return 0;
7390 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007391 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007392 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007393 ctx->ctx.hlua_apphttp.left_bytes = -1;
7394 ctx->ctx.hlua_apphttp.flags = 0;
7395
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007396 if (txn->req.flags & HTTP_MSGF_VER_11)
7397 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7398
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007399 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007400 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007401 if (!task) {
7402 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007403 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007404 return 0;
7405 }
7406 task->nice = 0;
7407 task->context = ctx;
7408 task->process = hlua_applet_wakeup;
7409 ctx->ctx.hlua_apphttp.task = task;
7410
7411 /* In the execution wrappers linked with a stream, the
7412 * Lua context can be not initialized. This behavior
7413 * permits to save performances because a systematic
7414 * Lua initialization cause 5% performances loss.
7415 */
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01007416 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007417 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007418 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007419 return 0;
7420 }
7421
7422 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007423 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007424
7425 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007426 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007427 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7428 error = lua_tostring(hlua->T, -1);
7429 else
7430 error = "critical error";
7431 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007432 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007433 return 0;
7434 }
7435
7436 /* Check stack available size. */
7437 if (!lua_checkstack(hlua->T, 1)) {
7438 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007439 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007440 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007441 return 0;
7442 }
7443
7444 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007445 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007446
7447 /* Create and and push object stream in the stack. */
7448 if (!hlua_applet_http_new(hlua->T, ctx)) {
7449 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007450 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007451 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007452 return 0;
7453 }
7454 hlua->nargs = 1;
7455
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007456 /* push keywords in the stack. */
7457 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7458 if (!lua_checkstack(hlua->T, 1)) {
7459 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007460 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007461 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007462 return 0;
7463 }
7464 lua_pushstring(hlua->T, *arg);
7465 hlua->nargs++;
7466 }
7467
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007468 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007469
7470 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007471 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007472
7473 return 1;
7474}
7475
Willy Tarreau60409db2019-08-21 14:14:50 +02007476void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007477{
7478 struct stream_interface *si = ctx->owner;
7479 struct stream *strm = si_strm(si);
7480 struct channel *req = si_oc(si);
7481 struct channel *res = si_ic(si);
7482 struct act_rule *rule = ctx->rule;
7483 struct proxy *px = strm->be;
7484 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7485 struct htx *req_htx, *res_htx;
7486
7487 res_htx = htx_from_buf(&res->buf);
7488
7489 /* If the stream is disconnect or closed, ldo nothing. */
7490 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7491 goto out;
7492
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007493 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007494 if (!b_size(&res->buf)) {
7495 si_rx_room_blk(si);
7496 goto out;
7497 }
7498 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007499 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007500 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7501
7502 /* Set the currently running flag. */
7503 if (!HLUA_IS_RUNNING(hlua) &&
7504 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007505 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007506 si_cant_get(si);
7507 goto out;
7508 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007509 }
7510
7511 /* Executes The applet if it is not done. */
7512 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7513
7514 /* Execute the function. */
7515 switch (hlua_ctx_resume(hlua, 1)) {
7516 /* finished. */
7517 case HLUA_E_OK:
7518 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7519 break;
7520
7521 /* yield. */
7522 case HLUA_E_AGAIN:
7523 if (hlua->wake_time != TICK_ETERNITY)
7524 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007525 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007526
7527 /* finished with error. */
7528 case HLUA_E_ERRMSG:
7529 /* Display log. */
7530 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007531 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007532 lua_pop(hlua->T, 1);
7533 goto error;
7534
7535 case HLUA_E_ETMOUT:
7536 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007537 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007538 goto error;
7539
7540 case HLUA_E_NOMEM:
7541 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007542 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007543 goto error;
7544
7545 case HLUA_E_YIELD: /* unexpected */
7546 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007547 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007548 goto error;
7549
7550 case HLUA_E_ERR:
7551 /* Display log. */
7552 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007553 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007554 goto error;
7555
7556 default:
7557 goto error;
7558 }
7559 }
7560
7561 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007562 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7563 goto done;
7564
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007565 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7566 goto error;
7567
Christopher Faulet868b3b12022-04-07 10:07:18 +02007568 /* no more data are expected. If the response buffer is empty
7569 * for a chunked message, be sure to add something (EOT block in
7570 * this case) to have something to send. It is important to be
7571 * sure the EOM flags will be handled by the endpoint.
7572 */
7573 if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) {
7574 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
7575 si_rx_room_blk(si);
7576 goto out;
7577 }
7578 channel_add_input(res, 1);
7579 }
7580
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007581 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet79c0fc72022-03-07 15:50:54 +01007582 res->flags |= CF_EOI;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007583 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7584 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007585 }
7586
7587 done:
7588 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007589 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007590 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007591 si_shutr(si);
7592 }
7593
7594 /* eat the whole request */
7595 if (co_data(req)) {
7596 req_htx = htx_from_buf(&req->buf);
7597 co_htx_skip(req, req_htx, co_data(req));
7598 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007599 }
7600 }
7601
7602 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007603 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007604 return;
7605
7606 error:
7607
7608 /* If we are in HTTP mode, and we are not send any
7609 * data, return a 500 server error in best effort:
7610 * if there is no room available in the buffer,
7611 * just close the connection.
7612 */
7613 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007614 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007615
7616 channel_erase(res);
7617 res->buf.data = b_data(err);
7618 memcpy(res->buf.area, b_head(err), b_data(err));
7619 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007620 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007621 }
7622 if (!(strm->flags & SF_ERR_MASK))
7623 strm->flags |= SF_ERR_RESOURCE;
7624 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7625 goto done;
7626}
7627
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007628static void hlua_applet_http_release(struct appctx *ctx)
7629{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007630 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007631 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007632 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007633 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007634}
7635
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007636/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007637 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007638 *
7639 * This function can fail with an abort() due to an Lua critical error.
7640 * We are in the configuration parsing process of HAProxy, this abort() is
7641 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007642 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007643static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7644 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007645{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007646 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007647 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007648
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007649 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007650 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007651 if (!rule->arg.hlua_rule) {
7652 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007653 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007654 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007655
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007656 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007657 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7658 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007659 if (!rule->arg.hlua_rule->args) {
7660 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007661 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007662 }
7663
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007664 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007665 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007666
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007667 /* Expect some arguments */
7668 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007669 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007670 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007671 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007672 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007673 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007674 if (!rule->arg.hlua_rule->args[i]) {
7675 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007676 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007677 }
7678 (*cur_arg)++;
7679 }
7680 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007681
Thierry FOURNIER42148732015-09-02 17:17:33 +02007682 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007683 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007684 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007685
7686 error:
7687 if (rule->arg.hlua_rule) {
7688 if (rule->arg.hlua_rule->args) {
7689 for (i = 0; i < fcn->nargs; i++)
7690 ha_free(&rule->arg.hlua_rule->args[i]);
7691 ha_free(&rule->arg.hlua_rule->args);
7692 }
7693 ha_free(&rule->arg.hlua_rule);
7694 }
7695 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007696}
7697
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007698static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7699 struct act_rule *rule, char **err)
7700{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007701 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007702
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007703 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007704 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007705 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007706 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007707 * the call of this analyzer.
7708 */
7709 if (rule->from != ACT_F_HTTP_REQ) {
7710 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7711 return ACT_RET_PRS_ERR;
7712 }
7713
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007714 /* Memory for the rule. */
7715 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7716 if (!rule->arg.hlua_rule) {
7717 memprintf(err, "out of memory error");
7718 return ACT_RET_PRS_ERR;
7719 }
7720
7721 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007722 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007723
7724 /* TODO: later accept arguments. */
7725 rule->arg.hlua_rule->args = NULL;
7726
7727 /* Add applet pointer in the rule. */
7728 rule->applet.obj_type = OBJ_TYPE_APPLET;
7729 rule->applet.name = fcn->name;
7730 rule->applet.init = hlua_applet_http_init;
7731 rule->applet.fct = hlua_applet_http_fct;
7732 rule->applet.release = hlua_applet_http_release;
7733 rule->applet.timeout = hlua_timeout_applet;
7734
7735 return ACT_RET_PRS_OK;
7736}
7737
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007738/* This function is an LUA binding used for registering
7739 * "sample-conv" functions. It expects a converter name used
7740 * in the haproxy configuration file, and an LUA function.
7741 */
7742__LJMP static int hlua_register_action(lua_State *L)
7743{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007744 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007745 const char *name;
7746 int ref;
7747 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007748 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007749 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007750 struct buffer *trash;
7751 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007752
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007753 /* Initialise the number of expected arguments at 0. */
7754 nargs = 0;
7755
7756 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7757 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007758
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01007759 if (hlua_gethlua(L)) {
7760 /* runtime processing */
7761 WILL_LJMP(luaL_error(L, "register_action: not available outside of body context"));
7762 }
7763
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007764 /* First argument : converter name. */
7765 name = MAY_LJMP(luaL_checkstring(L, 1));
7766
7767 /* Second argument : environment. */
7768 if (lua_type(L, 2) != LUA_TTABLE)
7769 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7770
7771 /* Third argument : lua function. */
7772 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7773
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007774 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007775 if (lua_gettop(L) >= 4)
7776 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7777
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007778 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007779 lua_pushnil(L);
7780 while (lua_next(L, 2) != 0) {
7781 if (lua_type(L, -1) != LUA_TSTRING)
7782 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7783
Thierry Fournierf67442e2020-11-28 20:41:07 +01007784 /* Check if action exists */
7785 trash = get_trash_chunk();
7786 chunk_printf(trash, "lua.%s", name);
7787 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7788 akw = tcp_req_cont_action(trash->area);
7789 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7790 akw = tcp_res_cont_action(trash->area);
7791 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7792 akw = action_http_req_custom(trash->area);
7793 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7794 akw = action_http_res_custom(trash->area);
7795 } else {
7796 akw = NULL;
7797 }
7798 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007799 fcn = akw->private;
7800 if (fcn->function_ref[hlua_state_id] != -1) {
7801 ha_warning("Trying to register action 'lua.%s' more than once. "
7802 "This will become a hard error in version 2.5.\n", name);
7803 }
7804 fcn->function_ref[hlua_state_id] = ref;
7805
7806 /* pop the environment string. */
7807 lua_pop(L, 1);
7808 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007809 }
7810
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007811 /* Check required environment. Only accepted "http" or "tcp". */
7812 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007813 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007814 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007815 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007816 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007817 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007818 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007819
7820 /* Fill fcn. */
7821 fcn->name = strdup(name);
7822 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007823 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007824 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007825
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007826 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007827 fcn->nargs = nargs;
7828
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007829 /* List head */
7830 akl->list.n = akl->list.p = NULL;
7831
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007832 /* action keyword. */
7833 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007834 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007835 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007836 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007837
7838 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7839
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007840 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007841 akl->kw[0].private = fcn;
7842 akl->kw[0].parse = action_register_lua;
7843
7844 /* select the action registering point. */
7845 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7846 tcp_req_cont_keywords_register(akl);
7847 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7848 tcp_res_cont_keywords_register(akl);
7849 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7850 http_req_keywords_register(akl);
7851 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7852 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007853 else {
7854 release_hlua_function(fcn);
7855 if (akl)
7856 ha_free((char **)&(akl->kw[0].kw));
7857 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007858 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007859 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7860 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007861 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007862
7863 /* pop the environment string. */
7864 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007865
7866 /* reset for next loop */
7867 akl = NULL;
7868 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007869 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007870 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007871
7872 alloc_error:
7873 release_hlua_function(fcn);
7874 ha_free(&akl);
7875 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7876 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007877}
7878
7879static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7880 struct act_rule *rule, char **err)
7881{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007882 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007883
Christopher Faulet280f85b2019-07-15 15:02:04 +02007884 if (px->mode == PR_MODE_HTTP) {
7885 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007886 return ACT_RET_PRS_ERR;
7887 }
7888
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007889 /* Memory for the rule. */
7890 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7891 if (!rule->arg.hlua_rule) {
7892 memprintf(err, "out of memory error");
7893 return ACT_RET_PRS_ERR;
7894 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007895
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007896 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007897 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007898
7899 /* TODO: later accept arguments. */
7900 rule->arg.hlua_rule->args = NULL;
7901
7902 /* Add applet pointer in the rule. */
7903 rule->applet.obj_type = OBJ_TYPE_APPLET;
7904 rule->applet.name = fcn->name;
7905 rule->applet.init = hlua_applet_tcp_init;
7906 rule->applet.fct = hlua_applet_tcp_fct;
7907 rule->applet.release = hlua_applet_tcp_release;
7908 rule->applet.timeout = hlua_timeout_applet;
7909
7910 return 0;
7911}
7912
7913/* This function is an LUA binding used for registering
7914 * "sample-conv" functions. It expects a converter name used
7915 * in the haproxy configuration file, and an LUA function.
7916 */
7917__LJMP static int hlua_register_service(lua_State *L)
7918{
7919 struct action_kw_list *akl;
7920 const char *name;
7921 const char *env;
7922 int ref;
7923 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007924 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007925 struct buffer *trash;
7926 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007927
7928 MAY_LJMP(check_args(L, 3, "register_service"));
7929
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01007930 if (hlua_gethlua(L)) {
7931 /* runtime processing */
7932 WILL_LJMP(luaL_error(L, "register_service: not available outside of body context"));
7933 }
7934
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007935 /* First argument : converter name. */
7936 name = MAY_LJMP(luaL_checkstring(L, 1));
7937
7938 /* Second argument : environment. */
7939 env = MAY_LJMP(luaL_checkstring(L, 2));
7940
7941 /* Third argument : lua function. */
7942 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7943
Thierry Fournierf67442e2020-11-28 20:41:07 +01007944 /* Check for service already registered */
7945 trash = get_trash_chunk();
7946 chunk_printf(trash, "lua.%s", name);
7947 akw = service_find(trash->area);
7948 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007949 fcn = akw->private;
7950 if (fcn->function_ref[hlua_state_id] != -1) {
7951 ha_warning("Trying to register service 'lua.%s' more than once. "
7952 "This will become a hard error in version 2.5.\n", name);
7953 }
7954 fcn->function_ref[hlua_state_id] = ref;
7955 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007956 }
7957
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007958 /* Allocate and fill the sample fetch keyword struct. */
7959 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7960 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007961 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007962 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007963 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007964 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007965
7966 /* Fill fcn. */
7967 len = strlen("<lua.>") + strlen(name) + 1;
7968 fcn->name = calloc(1, len);
7969 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007970 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007971 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007972 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007973
7974 /* List head */
7975 akl->list.n = akl->list.p = NULL;
7976
7977 /* converter keyword. */
7978 len = strlen("lua.") + strlen(name) + 1;
7979 akl->kw[0].kw = calloc(1, len);
7980 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007981 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007982
7983 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7984
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007985 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007986 if (strcmp(env, "tcp") == 0)
7987 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007988 else if (strcmp(env, "http") == 0)
7989 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007990 else {
7991 release_hlua_function(fcn);
7992 if (akl)
7993 ha_free((char **)&(akl->kw[0].kw));
7994 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007995 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007996 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02007997 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007998
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007999 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008000 akl->kw[0].private = fcn;
8001
8002 /* End of array. */
8003 memset(&akl->kw[1], 0, sizeof(*akl->kw));
8004
8005 /* Register this new converter */
8006 service_keywords_register(akl);
8007
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008008 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02008009
8010 alloc_error:
8011 release_hlua_function(fcn);
8012 ha_free(&akl);
8013 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8014 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008015}
8016
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008017/* This function initialises Lua cli handler. It copies the
8018 * arguments in the Lua stack and create channel IO objects.
8019 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02008020static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008021{
8022 struct hlua *hlua;
8023 struct hlua_function *fcn;
8024 int i;
8025 const char *error;
8026
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008027 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008028 appctx->ctx.hlua_cli.fcn = private;
8029
Willy Tarreaubafbe012017-11-24 17:34:44 +01008030 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008031 if (!hlua) {
8032 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008033 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008034 }
8035 HLUA_INIT(hlua);
8036 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008037
8038 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05008039 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008040 * applet_http. It is absolutely compatible.
8041 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01008042 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008043 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01008044 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008045 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008046 }
8047 appctx->ctx.hlua_cli.task->nice = 0;
8048 appctx->ctx.hlua_cli.task->context = appctx;
8049 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
8050
8051 /* Initialises the Lua context */
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01008052 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), appctx->ctx.hlua_cli.task)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008053 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008054 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008055 }
8056
8057 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008058 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008059 if (lua_type(hlua->T, -1) == LUA_TSTRING)
8060 error = lua_tostring(hlua->T, -1);
8061 else
8062 error = "critical error";
8063 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
8064 goto error;
8065 }
8066
8067 /* Check stack available size. */
8068 if (!lua_checkstack(hlua->T, 2)) {
8069 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8070 goto error;
8071 }
8072
8073 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008074 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008075
8076 /* Once the arguments parsed, the CLI is like an AppletTCP,
8077 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008078 */
8079 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
8080 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8081 goto error;
8082 }
8083 hlua->nargs = 1;
8084
8085 /* push keywords in the stack. */
8086 for (i = 0; *args[i]; i++) {
8087 /* Check stack available size. */
8088 if (!lua_checkstack(hlua->T, 1)) {
8089 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8090 goto error;
8091 }
8092 lua_pushstring(hlua->T, args[i]);
8093 hlua->nargs++;
8094 }
8095
8096 /* We must initialize the execution timeouts. */
8097 hlua->max_time = hlua_timeout_session;
8098
8099 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008100 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008101
8102 /* It's ok */
8103 return 0;
8104
8105 /* It's not ok. */
8106error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008107 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008108 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008109 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008110 return 1;
8111}
8112
8113static int hlua_cli_io_handler_fct(struct appctx *appctx)
8114{
8115 struct hlua *hlua;
8116 struct stream_interface *si;
8117 struct hlua_function *fcn;
8118
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008119 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008120 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008121 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008122
8123 /* If the stream is disconnect or closed, ldo nothing. */
8124 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8125 return 1;
8126
8127 /* Execute the function. */
8128 switch (hlua_ctx_resume(hlua, 1)) {
8129
8130 /* finished. */
8131 case HLUA_E_OK:
8132 return 1;
8133
8134 /* yield. */
8135 case HLUA_E_AGAIN:
8136 /* We want write. */
8137 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008138 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008139 /* Set the timeout. */
8140 if (hlua->wake_time != TICK_ETERNITY)
8141 task_schedule(hlua->task, hlua->wake_time);
8142 return 0;
8143
8144 /* finished with error. */
8145 case HLUA_E_ERRMSG:
8146 /* Display log. */
8147 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8148 fcn->name, lua_tostring(hlua->T, -1));
8149 lua_pop(hlua->T, 1);
8150 return 1;
8151
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008152 case HLUA_E_ETMOUT:
8153 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8154 fcn->name);
8155 return 1;
8156
8157 case HLUA_E_NOMEM:
8158 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8159 fcn->name);
8160 return 1;
8161
8162 case HLUA_E_YIELD: /* unexpected */
8163 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8164 fcn->name);
8165 return 1;
8166
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008167 case HLUA_E_ERR:
8168 /* Display log. */
8169 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8170 fcn->name);
8171 return 1;
8172
8173 default:
8174 return 1;
8175 }
8176
8177 return 1;
8178}
8179
8180static void hlua_cli_io_release_fct(struct appctx *appctx)
8181{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008182 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008183 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008184}
8185
8186/* This function is an LUA binding used for registering
8187 * new keywords in the cli. It expects a list of keywords
8188 * which are the "path". It is limited to 5 keywords. A
8189 * description of the command, a function to be executed
8190 * for the parsing and a function for io handlers.
8191 */
8192__LJMP static int hlua_register_cli(lua_State *L)
8193{
8194 struct cli_kw_list *cli_kws;
8195 const char *message;
8196 int ref_io;
8197 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008198 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008199 int index;
8200 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008201 struct buffer *trash;
8202 const char *kw[5];
8203 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008204 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008205
8206 MAY_LJMP(check_args(L, 3, "register_cli"));
8207
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01008208 if (hlua_gethlua(L)) {
8209 /* runtime processing */
8210 WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context"));
8211 }
8212
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008213 /* First argument : an array of maximum 5 keywords. */
8214 if (!lua_istable(L, 1))
8215 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8216
8217 /* Second argument : string with contextual message. */
8218 message = MAY_LJMP(luaL_checkstring(L, 2));
8219
8220 /* Third and fourth argument : lua function. */
8221 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8222
Thierry Fournierf67442e2020-11-28 20:41:07 +01008223 /* Check for CLI service already registered */
8224 trash = get_trash_chunk();
8225 index = 0;
8226 lua_pushnil(L);
8227 memset(kw, 0, sizeof(kw));
8228 while (lua_next(L, 1) != 0) {
8229 if (index >= CLI_PREFIX_KW_NB)
8230 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8231 if (lua_type(L, -1) != LUA_TSTRING)
8232 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8233 kw[index] = lua_tostring(L, -1);
8234 if (index == 0)
8235 chunk_printf(trash, "%s", kw[index]);
8236 else
8237 chunk_appendf(trash, " %s", kw[index]);
8238 index++;
8239 lua_pop(L, 1);
8240 }
8241 cli_kw = cli_find_kw_exact((char **)kw);
8242 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008243 fcn = cli_kw->private;
8244 if (fcn->function_ref[hlua_state_id] != -1) {
8245 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8246 "This will become a hard error in version 2.5.\n", trash->area);
8247 }
8248 fcn->function_ref[hlua_state_id] = ref_io;
8249 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008250 }
8251
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008252 /* Allocate and fill the sample fetch keyword struct. */
8253 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008254 if (!cli_kws) {
8255 errmsg = "Lua out of memory error.";
8256 goto error;
8257 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008258 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008259 if (!fcn) {
8260 errmsg = "Lua out of memory error.";
8261 goto error;
8262 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008263
8264 /* Fill path. */
8265 index = 0;
8266 lua_pushnil(L);
8267 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008268 if (index >= 5) {
8269 errmsg = "1st argument must be a table with a maximum of 5 entries";
8270 goto error;
8271 }
8272 if (lua_type(L, -1) != LUA_TSTRING) {
8273 errmsg = "1st argument must be a table filled with strings";
8274 goto error;
8275 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008276 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008277 if (!cli_kws->kw[0].str_kw[index]) {
8278 errmsg = "Lua out of memory error.";
8279 goto error;
8280 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008281 index++;
8282 lua_pop(L, 1);
8283 }
8284
8285 /* Copy help message. */
8286 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008287 if (!cli_kws->kw[0].usage) {
8288 errmsg = "Lua out of memory error.";
8289 goto error;
8290 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008291
8292 /* Fill fcn io handler. */
8293 len = strlen("<lua.cli>") + 1;
8294 for (i = 0; i < index; i++)
8295 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8296 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008297 if (!fcn->name) {
8298 errmsg = "Lua out of memory error.";
8299 goto error;
8300 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008301 strncat((char *)fcn->name, "<lua.cli", len);
8302 for (i = 0; i < index; i++) {
8303 strncat((char *)fcn->name, ".", len);
8304 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8305 }
8306 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008307 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008308
8309 /* Fill last entries. */
8310 cli_kws->kw[0].private = fcn;
8311 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8312 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8313 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8314
8315 /* Register this new converter */
8316 cli_register_kw(cli_kws);
8317
8318 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008319
8320 error:
8321 release_hlua_function(fcn);
8322 if (cli_kws) {
8323 for (i = 0; i < index; i++)
8324 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8325 ha_free((char **)&(cli_kws->kw[0].usage));
8326 }
8327 ha_free(&cli_kws);
8328 WILL_LJMP(luaL_error(L, errmsg));
8329 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008330}
8331
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008332static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008333 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008334 char **err, unsigned int *timeout)
8335{
8336 const char *error;
8337
8338 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008339 if (error == PARSE_TIME_OVER) {
8340 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8341 args[1], args[0]);
8342 return -1;
8343 }
8344 else if (error == PARSE_TIME_UNDER) {
8345 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8346 args[1], args[0]);
8347 return -1;
8348 }
8349 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008350 memprintf(err, "%s: invalid timeout", args[0]);
8351 return -1;
8352 }
8353 return 0;
8354}
8355
8356static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008357 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008358 char **err)
8359{
8360 return hlua_read_timeout(args, section_type, curpx, defpx,
8361 file, line, err, &hlua_timeout_session);
8362}
8363
8364static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008365 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008366 char **err)
8367{
8368 return hlua_read_timeout(args, section_type, curpx, defpx,
8369 file, line, err, &hlua_timeout_task);
8370}
8371
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008372static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008373 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008374 char **err)
8375{
8376 return hlua_read_timeout(args, section_type, curpx, defpx,
8377 file, line, err, &hlua_timeout_applet);
8378}
8379
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008380static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008381 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008382 char **err)
8383{
8384 char *error;
8385
8386 hlua_nb_instruction = strtoll(args[1], &error, 10);
8387 if (*error != '\0') {
8388 memprintf(err, "%s: invalid number", args[0]);
8389 return -1;
8390 }
8391 return 0;
8392}
8393
Willy Tarreau32f61e22015-03-18 17:54:59 +01008394static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008395 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008396 char **err)
8397{
8398 char *error;
8399
8400 if (*(args[1]) == 0) {
8401 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8402 return -1;
8403 }
8404 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8405 if (*error != '\0') {
8406 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8407 return -1;
8408 }
8409 return 0;
8410}
8411
8412
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008413/* This function is called by the main configuration key "lua-load". It loads and
8414 * execute an lua file during the parsing of the HAProxy configuration file. It is
8415 * the main lua entry point.
8416 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008417 * This function runs with the HAProxy keywords API. It returns -1 if an error
8418 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008419 *
8420 * In some error case, LUA set an error message in top of the stack. This function
8421 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008422 *
8423 * This function can fail with an abort() due to an Lua critical error.
8424 * We are in the configuration parsing process of HAProxy, this abort() is
8425 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008426 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008427static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008428{
8429 int error;
8430
8431 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008432 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008433 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008434 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8435 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008436 return -1;
8437 }
8438
8439 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008440 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008441 switch (error) {
8442 case LUA_OK:
8443 break;
8444 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008445 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8446 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008447 return -1;
8448 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008449 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008450 return -1;
8451 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008452 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8453 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008454 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008455#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008456 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008457 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8458 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008459 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008460#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008461 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008462 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8463 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008464 return -1;
8465 }
8466
8467 return 0;
8468}
8469
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008470static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008471 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008472 char **err)
8473{
8474 if (*(args[1]) == 0) {
8475 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8476 return -1;
8477 }
8478
Thierry Fournier59f11be2020-11-29 00:37:41 +01008479 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008480 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008481 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008482 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008483}
8484
Thierry Fournier59f11be2020-11-29 00:37:41 +01008485static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008486 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008487 char **err)
8488{
8489 int len;
8490
8491 if (*(args[1]) == 0) {
8492 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8493 return -1;
8494 }
8495
8496 if (per_thread_load == NULL) {
8497 /* allocate the first entry large enough to store the final NULL */
8498 per_thread_load = calloc(1, sizeof(*per_thread_load));
8499 if (per_thread_load == NULL) {
8500 memprintf(err, "out of memory error");
8501 return -1;
8502 }
8503 }
8504
8505 /* count used entries */
8506 for (len = 0; per_thread_load[len] != NULL; len++)
8507 ;
8508
8509 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8510 if (per_thread_load == NULL) {
8511 memprintf(err, "out of memory error");
8512 return -1;
8513 }
8514
8515 per_thread_load[len] = strdup(args[1]);
8516 per_thread_load[len + 1] = NULL;
8517
8518 if (per_thread_load[len] == NULL) {
8519 memprintf(err, "out of memory error");
8520 return -1;
8521 }
8522
8523 /* loading for thread 1 only */
8524 hlua_state_id = 1;
8525 ha_set_tid(0);
8526 return hlua_load_state(args[1], hlua_states[1], err);
8527}
8528
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008529/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8530 * in the given <ctx>.
8531 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008532static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008533{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008534 lua_getglobal(L, "package"); /* push package variable */
8535 lua_pushstring(L, path); /* push given path */
8536 lua_pushstring(L, ";"); /* push semicolon */
8537 lua_getfield(L, -3, type); /* push old path */
8538 lua_concat(L, 3); /* concatenate to new path */
8539 lua_setfield(L, -2, type); /* store new path */
8540 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008541
8542 return 0;
8543}
8544
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008545static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008546 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008547 char **err)
8548{
8549 char *path;
8550 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008551 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008552
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008553 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008554 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008555 }
8556
8557 if (!(*args[1])) {
8558 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008559 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008560 }
8561 path = args[1];
8562
8563 if (*args[2]) {
8564 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8565 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008566 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008567 }
8568 type = args[2];
8569 }
8570
Thierry Fournier59f11be2020-11-29 00:37:41 +01008571 p = calloc(1, sizeof(*p));
8572 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008573 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008574 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008575 }
8576 p->path = strdup(path);
8577 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008578 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008579 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008580 }
8581 p->type = strdup(type);
8582 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008583 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008584 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008585 }
Willy Tarreau2b718102021-04-21 07:32:39 +02008586 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01008587
8588 hlua_prepend_path(hlua_states[0], type, path);
8589 hlua_prepend_path(hlua_states[1], type, path);
8590 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008591
8592err2:
8593 free(p->type);
8594 free(p->path);
8595err:
8596 free(p);
8597 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008598}
8599
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008600/* configuration keywords declaration */
8601static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008602 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008603 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008604 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008605 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8606 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008607 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008608 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008609 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008610 { 0, NULL, NULL },
8611}};
8612
Willy Tarreau0108d902018-11-25 19:14:37 +01008613INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8614
Christopher Fauletafd8f102018-11-08 11:34:21 +01008615
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008616/* This function can fail with an abort() due to an Lua critical error.
8617 * We are in the initialisation process of HAProxy, this abort() is
8618 * tolerated.
8619 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008620int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008621{
8622 struct hlua_init_function *init;
8623 const char *msg;
8624 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008625 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008626 const char *kind;
8627 const char *trace;
8628 int return_status = 1;
8629#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8630 int nres;
8631#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008632
Willy Tarreaucdb53462020-12-02 12:12:00 +01008633 /* disable memory limit checks if limit is not set */
8634 if (!hlua_global_allocator.limit)
8635 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8636
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008637 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008638 if (setjmp(safe_ljmp_env) != 0) {
8639 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008640 if (lua_type(L, -1) == LUA_TSTRING)
8641 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008642 else
8643 error = "critical error";
8644 fprintf(stderr, "Lua post-init: %s.\n", error);
8645 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008646 } else {
8647 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008648 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008649
Thierry Fournierb8cef172020-11-28 15:37:17 +01008650 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008651
Thierry Fournierc7492592020-11-28 23:57:24 +01008652 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008653 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Aurelien DARRAGON93591b42023-03-20 16:29:55 +01008654 /* function ref should be released right away since it was pushed
8655 * on the stack and will not be used anymore
8656 */
8657 hlua_unref(L, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008658
8659#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008660 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008661#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008662 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008663#endif
8664 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008665 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008666
8667 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008668 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008669 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008670
8671 case LUA_ERRERR:
8672 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008673 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008674 case LUA_ERRRUN:
8675 if (!kind)
8676 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008677 msg = lua_tostring(L, -1);
8678 lua_settop(L, 0); /* Empty the stack. */
8679 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008680 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008681 if (msg)
8682 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8683 else
8684 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8685 return_status = 0;
8686 break;
8687
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008688 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008689 /* Unknown error */
8690 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008691 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008692 case LUA_YIELD:
8693 /* yield is not configured at this step, this state doesn't happen */
8694 if (!kind)
8695 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008696 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008697 case LUA_ERRMEM:
8698 if (!kind)
8699 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008700 lua_settop(L, 0);
8701 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008702 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008703 ha_alert("Lua init: %s: %s\n", kind, trace);
8704 return_status = 0;
8705 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008706 }
Thierry Fournier670db242020-11-28 10:49:59 +01008707 if (!return_status)
8708 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008709 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008710
8711 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008712 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008713}
8714
Thierry Fournierb8cef172020-11-28 15:37:17 +01008715int hlua_post_init()
8716{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008717 int ret;
8718 int i;
8719 int errors;
8720 char *err = NULL;
8721 struct hlua_function *fcn;
8722
Willy Tarreau42afc592021-08-30 09:35:18 +02008723#if defined(USE_OPENSSL)
Thierry Fournierb8cef172020-11-28 15:37:17 +01008724 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008725 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008726 int saved_used_backed = global.ssl_used_backend;
8727 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008728 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008729 global.ssl_used_backend = saved_used_backed;
8730 }
8731#endif
8732
Thierry Fournierc7492592020-11-28 23:57:24 +01008733 /* Perform post init of common thread */
8734 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008735 ha_set_tid(0);
8736 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8737 if (ret == 0)
8738 return 0;
8739
8740 /* init remaining lua states and load files */
8741 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8742
8743 /* set thread context */
8744 ha_set_tid(hlua_state_id - 1);
8745
8746 /* Init lua state */
8747 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8748
8749 /* Load lua files */
8750 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8751 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8752 if (ret != 0) {
8753 ha_alert("Lua init: %s\n", err);
8754 return 0;
8755 }
8756 }
8757 }
8758
8759 /* Reset thread context */
8760 ha_set_tid(0);
8761
8762 /* Execute post init for all states */
8763 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8764
8765 /* set thread context */
8766 ha_set_tid(hlua_state_id - 1);
8767
8768 /* run post init */
8769 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8770 if (ret == 0)
8771 return 0;
8772 }
8773
8774 /* Reset thread context */
8775 ha_set_tid(0);
8776
8777 /* control functions registering. Each function must have:
8778 * - only the function_ref[0] set positive and all other to -1
8779 * - only the function_ref[0] set to -1 and all other positive
8780 * This ensure a same reference is not used both in shared
8781 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008782 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008783 * complicated to found for the end user.
8784 */
8785 errors = 0;
8786 list_for_each_entry(fcn, &referenced_functions, l) {
8787 ret = 0;
8788 for (i = 1; i < global.nbthread + 1; i++) {
8789 if (fcn->function_ref[i] == -1)
8790 ret--;
8791 else
8792 ret++;
8793 }
8794 if (abs(ret) != global.nbthread) {
8795 ha_alert("Lua function '%s' is not referenced in all thread. "
8796 "Expect function in all thread or in none thread.\n", fcn->name);
8797 errors++;
8798 continue;
8799 }
8800
8801 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008802 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8803 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008804 "exclusive.\n", fcn->name);
8805 errors++;
8806 }
8807 }
8808
8809 if (errors > 0)
8810 return 0;
8811
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008812 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008813 * -1 in order to have probably a segfault if someone use it
8814 */
8815 hlua_state_id = -1;
8816
8817 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008818}
8819
Willy Tarreau32f61e22015-03-18 17:54:59 +01008820/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8821 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8822 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008823 * allocation. <nsize> is the requested new size. A new allocation is
8824 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008825 * zero. This one verifies that the limits are respected but is optimized
8826 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreaue00ad052021-10-22 16:00:02 +02008827 *
8828 * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses
8829 * POSIX by making realloc(ptr,0) an effective free(), but others do not do
8830 * that and will simply allocate zero as if it were the result of malloc(0),
8831 * so mapping this onto realloc() will lead to memory leaks on non-glibc
8832 * systems.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008833 */
8834static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8835{
8836 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008837 size_t limit, old, new;
8838
Tim Duesterhus22586522021-01-08 10:35:33 +01008839 if (unlikely(!ptr && !nsize))
8840 return NULL;
8841
Willy Tarreaucdb53462020-12-02 12:12:00 +01008842 /* a limit of ~0 means unlimited and boot complete, so there's no need
8843 * for accounting anymore.
8844 */
Willy Tarreaue00ad052021-10-22 16:00:02 +02008845 if (likely(~zone->limit == 0)) {
8846 if (!nsize)
8847 ha_free(&ptr);
8848 else
8849 ptr = realloc(ptr, nsize);
8850 return ptr;
8851 }
Willy Tarreau32f61e22015-03-18 17:54:59 +01008852
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008853 if (!ptr)
8854 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008855
Willy Tarreaucdb53462020-12-02 12:12:00 +01008856 /* enforce strict limits across all threads */
8857 limit = zone->limit;
8858 old = _HA_ATOMIC_LOAD(&zone->allocated);
8859 do {
8860 new = old + nsize - osize;
8861 if (unlikely(nsize && limit && new > limit))
8862 return NULL;
8863 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008864
Willy Tarreaue00ad052021-10-22 16:00:02 +02008865 if (!nsize)
8866 ha_free(&ptr);
8867 else
8868 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008869
8870 if (unlikely(!ptr && nsize)) // failed
8871 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8872
8873 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008874 return ptr;
8875}
8876
Thierry Fournierecb83c22020-11-28 15:49:44 +01008877/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008878 * We are in the initialisation process of HAProxy, this abort() is
8879 * tolerated.
8880 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008881lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008882{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008883 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008884 int idx;
8885 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008886 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008887 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008888 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008889 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008890 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008891 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008892
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008893 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008894 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008895
firexinghebdd336c2023-07-13 11:03:34 +08008896 if (!L) {
8897 fprintf(stderr,
8898 "Lua init: critical error: lua_newstate() returned NULL."
8899 " This may possibly be caused by a memory allocation error.\n");
8900 exit(1);
8901 }
8902
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008903 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008904 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008905 *context = NULL;
8906
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008907 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008908 * the Lua function can fail with an abort. We are in the initialisation
8909 * process of HAProxy, this abort() is tolerated.
8910 */
8911
Thierry Fournier3c539322020-11-28 16:05:05 +01008912 /* Call post initialisation function in safe environment. */
8913 if (setjmp(safe_ljmp_env) != 0) {
8914 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008915 if (lua_type(L, -1) == LUA_TSTRING)
8916 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008917 else
8918 error_msg = "critical error";
8919 fprintf(stderr, "Lua init: %s.\n", error_msg);
8920 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008921 } else {
8922 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008923 }
8924
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008925 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008926 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008927#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8928#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8929#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008930 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008931#endif
8932#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008933 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008934#endif
8935#undef HLUA_PREPEND_PATH_TOSTRING
8936#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008937
Thierry Fournier59f11be2020-11-29 00:37:41 +01008938 /* Apply configured prepend path */
8939 list_for_each_entry(pp, &prepend_path_list, l)
8940 hlua_prepend_path(L, pp->type, pp->path);
8941
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008942 /*
8943 *
8944 * Create "core" object.
8945 *
8946 */
8947
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008948 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008949 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008950
Thierry Fournierecb83c22020-11-28 15:49:44 +01008951 /* set the thread id */
8952 hlua_class_const_int(L, "thread", thread_num);
8953
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008954 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008955 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008956 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008957
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008958 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008959 hlua_class_function(L, "register_init", hlua_register_init);
8960 hlua_class_function(L, "register_task", hlua_register_task);
8961 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8962 hlua_class_function(L, "register_converters", hlua_register_converters);
8963 hlua_class_function(L, "register_action", hlua_register_action);
8964 hlua_class_function(L, "register_service", hlua_register_service);
8965 hlua_class_function(L, "register_cli", hlua_register_cli);
8966 hlua_class_function(L, "yield", hlua_yield);
8967 hlua_class_function(L, "set_nice", hlua_set_nice);
8968 hlua_class_function(L, "sleep", hlua_sleep);
8969 hlua_class_function(L, "msleep", hlua_msleep);
8970 hlua_class_function(L, "add_acl", hlua_add_acl);
8971 hlua_class_function(L, "del_acl", hlua_del_acl);
8972 hlua_class_function(L, "set_map", hlua_set_map);
8973 hlua_class_function(L, "del_map", hlua_del_map);
8974 hlua_class_function(L, "tcp", hlua_socket_new);
8975 hlua_class_function(L, "log", hlua_log);
8976 hlua_class_function(L, "Debug", hlua_log_debug);
8977 hlua_class_function(L, "Info", hlua_log_info);
8978 hlua_class_function(L, "Warning", hlua_log_warning);
8979 hlua_class_function(L, "Alert", hlua_log_alert);
8980 hlua_class_function(L, "done", hlua_done);
8981 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008982
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008983 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008984
8985 /*
8986 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008987 * Create "act" object.
8988 *
8989 */
8990
8991 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008992 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008993
8994 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008995 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8996 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8997 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8998 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8999 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
9000 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
9001 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
9002 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009003
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009004 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01009005
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009006 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009007
9008 /*
9009 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009010 * Register class Map
9011 *
9012 */
9013
9014 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009015 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009016
9017 /* register pattern types. */
9018 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009019 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009020 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009021 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009022 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009023 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009024
9025 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009026 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009027
9028 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009029 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009030
Ilya Shipitsind4259502020-04-08 01:07:56 +05009031 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009032 lua_pushstring(L, "__index");
9033 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009034
9035 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009036 hlua_class_function(L, "lookup", hlua_map_lookup);
9037 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009038
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009039 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009040
Thierry Fournier45e78d72016-02-19 18:34:46 +01009041 /* Register previous table in the registry with reference and named entry.
9042 * The function hlua_register_metatable() pops the stack, so we
9043 * previously create a copy of the table.
9044 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009045 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
9046 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009047
9048 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009049 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009050
9051 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009052 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009053
9054 /*
9055 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009056 * Register class Channel
9057 *
9058 */
9059
9060 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009061 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009062
Ilya Shipitsind4259502020-04-08 01:07:56 +05009063 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009064 lua_pushstring(L, "__index");
9065 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009066
9067 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009068 hlua_class_function(L, "get", hlua_channel_get);
9069 hlua_class_function(L, "dup", hlua_channel_dup);
9070 hlua_class_function(L, "getline", hlua_channel_getline);
9071 hlua_class_function(L, "set", hlua_channel_set);
9072 hlua_class_function(L, "append", hlua_channel_append);
9073 hlua_class_function(L, "send", hlua_channel_send);
9074 hlua_class_function(L, "forward", hlua_channel_forward);
9075 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
9076 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
9077 hlua_class_function(L, "is_full", hlua_channel_is_full);
9078 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009079
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009080 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009081
9082 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009083 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009084
9085 /*
9086 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009087 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009088 *
9089 */
9090
9091 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009092 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009093
Ilya Shipitsind4259502020-04-08 01:07:56 +05009094 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009095 lua_pushstring(L, "__index");
9096 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009097
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009098 /* Browse existing fetches and create the associated
9099 * object method.
9100 */
9101 sf = NULL;
9102 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009103 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9104 * by an underscore.
9105 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009106 strlcpy2(trash.area, sf->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009107 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009108 if (*p == '.' || *p == '-' || *p == '+')
9109 *p = '_';
9110
9111 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009112 lua_pushstring(L, trash.area);
9113 lua_pushlightuserdata(L, sf);
9114 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
9115 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009116 }
9117
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009118 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009119
9120 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009121 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009122
9123 /*
9124 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009125 * Register class Converters
9126 *
9127 */
9128
9129 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009130 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009131
9132 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009133 lua_pushstring(L, "__index");
9134 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009135
9136 /* Browse existing converters and create the associated
9137 * object method.
9138 */
9139 sc = NULL;
9140 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009141 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9142 * by an underscore.
9143 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009144 strlcpy2(trash.area, sc->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009145 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009146 if (*p == '.' || *p == '-' || *p == '+')
9147 *p = '_';
9148
9149 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009150 lua_pushstring(L, trash.area);
9151 lua_pushlightuserdata(L, sc);
9152 lua_pushcclosure(L, hlua_run_sample_conv, 1);
9153 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009154 }
9155
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009156 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009157
9158 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009159 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009160
9161 /*
9162 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009163 * Register class HTTP
9164 *
9165 */
9166
9167 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009168 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009169
Ilya Shipitsind4259502020-04-08 01:07:56 +05009170 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009171 lua_pushstring(L, "__index");
9172 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009173
9174 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009175 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9176 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9177 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9178 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9179 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9180 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9181 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9182 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9183 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9184 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009185
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009186 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9187 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9188 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9189 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9190 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9191 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9192 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009193
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009194 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009195
9196 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009197 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009198
9199 /*
9200 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009201 * Register class AppletTCP
9202 *
9203 */
9204
9205 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009206 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009207
Ilya Shipitsind4259502020-04-08 01:07:56 +05009208 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009209 lua_pushstring(L, "__index");
9210 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009211
9212 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009213 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9214 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9215 hlua_class_function(L, "send", hlua_applet_tcp_send);
9216 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9217 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9218 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9219 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9220 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009221
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009222 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009223
9224 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009225 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009226
9227 /*
9228 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009229 * Register class AppletHTTP
9230 *
9231 */
9232
9233 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009234 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009235
Ilya Shipitsind4259502020-04-08 01:07:56 +05009236 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009237 lua_pushstring(L, "__index");
9238 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009239
9240 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009241 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9242 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9243 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9244 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9245 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9246 hlua_class_function(L, "getline", hlua_applet_http_getline);
9247 hlua_class_function(L, "receive", hlua_applet_http_recv);
9248 hlua_class_function(L, "send", hlua_applet_http_send);
9249 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9250 hlua_class_function(L, "set_status", hlua_applet_http_status);
9251 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009252
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009253 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009254
9255 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009256 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009257
9258 /*
9259 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009260 * Register class TXN
9261 *
9262 */
9263
9264 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009265 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009266
Ilya Shipitsind4259502020-04-08 01:07:56 +05009267 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009268 lua_pushstring(L, "__index");
9269 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009270
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009271 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009272 hlua_class_function(L, "set_priv", hlua_set_priv);
9273 hlua_class_function(L, "get_priv", hlua_get_priv);
9274 hlua_class_function(L, "set_var", hlua_set_var);
9275 hlua_class_function(L, "unset_var", hlua_unset_var);
9276 hlua_class_function(L, "get_var", hlua_get_var);
9277 hlua_class_function(L, "done", hlua_txn_done);
9278 hlua_class_function(L, "reply", hlua_txn_reply_new);
9279 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9280 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9281 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9282 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9283 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9284 hlua_class_function(L, "deflog", hlua_txn_deflog);
9285 hlua_class_function(L, "log", hlua_txn_log);
9286 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9287 hlua_class_function(L, "Info", hlua_txn_log_info);
9288 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9289 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009290
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009291 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009292
9293 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009294 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009295
9296 /*
9297 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009298 * Register class reply
9299 *
9300 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009301 lua_newtable(L);
9302 lua_pushstring(L, "__index");
9303 lua_newtable(L);
9304 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9305 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9306 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9307 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9308 lua_settable(L, -3); /* Sets the __index entry. */
9309 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009310
9311
9312 /*
9313 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009314 * Register class Socket
9315 *
9316 */
9317
9318 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009319 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009320
Ilya Shipitsind4259502020-04-08 01:07:56 +05009321 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009322 lua_pushstring(L, "__index");
9323 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009324
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009325#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009326 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009327#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009328 hlua_class_function(L, "connect", hlua_socket_connect);
9329 hlua_class_function(L, "send", hlua_socket_send);
9330 hlua_class_function(L, "receive", hlua_socket_receive);
9331 hlua_class_function(L, "close", hlua_socket_close);
9332 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9333 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9334 hlua_class_function(L, "setoption", hlua_socket_setoption);
9335 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009336
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009337 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009338
9339 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009340 lua_pushstring(L, "__gc");
9341 lua_pushcclosure(L, hlua_socket_gc, 0);
9342 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009343
9344 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009345 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009346
Thierry Fournieraafc7772020-12-04 11:47:47 +01009347 lua_atpanic(L, hlua_panic_safe);
9348
9349 return L;
9350}
9351
9352void hlua_init(void) {
9353 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009354 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009355#ifdef USE_OPENSSL
9356 struct srv_kw *kw;
9357 int tmp_error;
9358 char *error;
9359 char *args[] = { /* SSL client configuration. */
9360 "ssl",
9361 "verify",
9362 "none",
9363 NULL
9364 };
9365#endif
9366
9367 /* Init post init function list head */
9368 for (i = 0; i < MAX_THREADS + 1; i++)
9369 LIST_INIT(&hlua_init_functions[i]);
9370
9371 /* Init state for common/shared lua parts */
9372 hlua_state_id = 0;
9373 ha_set_tid(0);
9374 hlua_states[0] = hlua_init_state(0);
9375
9376 /* Init state 1 for thread 0. We have at least one thread. */
9377 hlua_state_id = 1;
9378 ha_set_tid(0);
9379 hlua_states[1] = hlua_init_state(1);
9380
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009381 /* Proxy and server configuration initialisation. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009382 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg);
9383 if (!socket_proxy) {
9384 fprintf(stderr, "Lua init: %s\n", errmsg);
9385 exit(1);
9386 }
9387 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009388
9389 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009390 socket_tcp = new_server(socket_proxy);
9391 if (!socket_tcp) {
9392 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9393 exit(1);
9394 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009395
9396#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009397 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009398 socket_ssl = new_server(socket_proxy);
9399 if (!socket_ssl) {
9400 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9401 exit(1);
9402 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009403
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009404 socket_ssl->use_ssl = 1;
9405 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009406
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009407 for (i = 0; args[i] != NULL; i++) {
9408 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009409 /*
9410 *
9411 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009412 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009413 * features like client certificates and ssl_verify.
9414 *
9415 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009416 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009417 if (tmp_error != 0) {
9418 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9419 abort(); /* This must be never arrives because the command line
9420 not editable by the user. */
9421 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009422 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009423 }
9424 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009425#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009426
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009427}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009428
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009429static void hlua_deinit()
9430{
Willy Tarreau186f3762020-12-04 11:48:12 +01009431 int thr;
9432
9433 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9434 if (hlua_states[thr])
9435 lua_close(hlua_states[thr]);
9436 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009437
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009438 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009439
Willy Tarreau0f143af2021-03-05 10:41:48 +01009440#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009441 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009442#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009443
9444 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009445}
9446
9447REGISTER_POST_DEINIT(hlua_deinit);
9448
Willy Tarreau80713382018-11-26 10:19:54 +01009449static void hlua_register_build_options(void)
9450{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009451 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009452
Willy Tarreaubb57d942016-12-21 19:04:56 +01009453 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9454 hap_register_build_opts(ptr, 1);
9455}
Willy Tarreau80713382018-11-26 10:19:54 +01009456
9457INITCALL0(STG_REGISTER, hlua_register_build_options);