blob: ce76a321b353811b3711e01c315b5f8783e31f5d [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. */
Christopher Fauletd09cc512021-03-24 14:48:45 +01001437 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001438 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001439 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001440 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001441 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001442 ret = HLUA_E_ERRMSG;
1443 break;
1444
1445 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001446 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001447 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001448 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001449 break;
1450
1451 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001452 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001453 if (!lua_checkstack(lua->T, 1)) {
1454 ret = HLUA_E_ERR;
1455 break;
1456 }
1457 msg = lua_tostring(lua->T, -1);
1458 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001459 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001460 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001461 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001462 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001463 ret = HLUA_E_ERRMSG;
1464 break;
1465
1466 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001467 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001468 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001469 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001470 break;
1471 }
1472
1473 switch (ret) {
1474 case HLUA_E_AGAIN:
1475 break;
1476
1477 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001478 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001479 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001480 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001481 break;
1482
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001483 case HLUA_E_ETMOUT:
1484 case HLUA_E_NOMEM:
1485 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001486 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001487 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001488 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001489 hlua_ctx_renew(lua, 0);
1490 break;
1491
1492 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001493 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001494 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001495 break;
1496 }
1497
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001498 /* This is the main exit point, remove the Lua lock. */
Aurelien DARRAGONe00bf5d2023-03-21 13:22:33 +01001499 hlua_unlock(lua);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001500
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001501 return ret;
1502}
1503
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001504/* This function exit the current code. */
1505__LJMP static int hlua_done(lua_State *L)
1506{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001507 struct hlua *hlua;
1508
1509 /* Get hlua struct, or NULL if we execute from main lua state */
1510 hlua = hlua_gethlua(L);
1511 if (!hlua)
1512 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001513
1514 hlua->flags |= HLUA_EXIT;
1515 WILL_LJMP(lua_error(L));
1516
1517 return 0;
1518}
1519
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001520/* This function is an LUA binding. It provides a function
1521 * for deleting ACL from a referenced ACL file.
1522 */
1523__LJMP static int hlua_del_acl(lua_State *L)
1524{
1525 const char *name;
1526 const char *key;
1527 struct pat_ref *ref;
1528
1529 MAY_LJMP(check_args(L, 2, "del_acl"));
1530
1531 name = MAY_LJMP(luaL_checkstring(L, 1));
1532 key = MAY_LJMP(luaL_checkstring(L, 2));
1533
1534 ref = pat_ref_lookup(name);
1535 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001536 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001537
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001538 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001539 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001540 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001541 return 0;
1542}
1543
1544/* This function is an LUA binding. It provides a function
1545 * for deleting map entry from a referenced map file.
1546 */
1547static int hlua_del_map(lua_State *L)
1548{
1549 const char *name;
1550 const char *key;
1551 struct pat_ref *ref;
1552
1553 MAY_LJMP(check_args(L, 2, "del_map"));
1554
1555 name = MAY_LJMP(luaL_checkstring(L, 1));
1556 key = MAY_LJMP(luaL_checkstring(L, 2));
1557
1558 ref = pat_ref_lookup(name);
1559 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001560 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001561
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001562 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001563 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001564 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001565 return 0;
1566}
1567
1568/* This function is an LUA binding. It provides a function
1569 * for adding ACL pattern from a referenced ACL file.
1570 */
1571static int hlua_add_acl(lua_State *L)
1572{
1573 const char *name;
1574 const char *key;
1575 struct pat_ref *ref;
1576
1577 MAY_LJMP(check_args(L, 2, "add_acl"));
1578
1579 name = MAY_LJMP(luaL_checkstring(L, 1));
1580 key = MAY_LJMP(luaL_checkstring(L, 2));
1581
1582 ref = pat_ref_lookup(name);
1583 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001584 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001585
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001586 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001587 if (pat_ref_find_elt(ref, key) == NULL)
1588 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001589 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001590 return 0;
1591}
1592
1593/* This function is an LUA binding. It provides a function
1594 * for setting map pattern and sample from a referenced map
1595 * file.
1596 */
1597static int hlua_set_map(lua_State *L)
1598{
1599 const char *name;
1600 const char *key;
1601 const char *value;
1602 struct pat_ref *ref;
1603
1604 MAY_LJMP(check_args(L, 3, "set_map"));
1605
1606 name = MAY_LJMP(luaL_checkstring(L, 1));
1607 key = MAY_LJMP(luaL_checkstring(L, 2));
1608 value = MAY_LJMP(luaL_checkstring(L, 3));
1609
1610 ref = pat_ref_lookup(name);
1611 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001612 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001613
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001614 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001615 if (pat_ref_find_elt(ref, key) != NULL)
1616 pat_ref_set(ref, key, value, NULL);
1617 else
1618 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001619 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001620 return 0;
1621}
1622
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001623/* A class is a lot of memory that contain data. This data can be a table,
1624 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001625 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001626 * the name of the object (_G[<name>] = <metable> ).
1627 *
1628 * A metable is a table that modify the standard behavior of a standard
1629 * access to the associated data. The entries of this new metatable are
1630 * defined as is:
1631 *
1632 * http://lua-users.org/wiki/MetatableEvents
1633 *
1634 * __index
1635 *
1636 * we access an absent field in a table, the result is nil. This is
1637 * true, but it is not the whole truth. Actually, such access triggers
1638 * the interpreter to look for an __index metamethod: If there is no
1639 * such method, as usually happens, then the access results in nil;
1640 * otherwise, the metamethod will provide the result.
1641 *
1642 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1643 * the key does not appear in the table, but the metatable has an __index
1644 * property:
1645 *
1646 * - if the value is a function, the function is called, passing in the
1647 * table and the key; the return value of that function is returned as
1648 * the result.
1649 *
1650 * - if the value is another table, the value of the key in that table is
1651 * asked for and returned (and if it doesn't exist in that table, but that
1652 * table's metatable has an __index property, then it continues on up)
1653 *
1654 * - Use "rawget(myTable,key)" to skip this metamethod.
1655 *
1656 * http://www.lua.org/pil/13.4.1.html
1657 *
1658 * __newindex
1659 *
1660 * Like __index, but control property assignment.
1661 *
1662 * __mode - Control weak references. A string value with one or both
1663 * of the characters 'k' and 'v' which specifies that the the
1664 * keys and/or values in the table are weak references.
1665 *
1666 * __call - Treat a table like a function. When a table is followed by
1667 * parenthesis such as "myTable( 'foo' )" and the metatable has
1668 * a __call key pointing to a function, that function is invoked
1669 * (passing any specified arguments) and the return value is
1670 * returned.
1671 *
1672 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1673 * called, if the metatable for myTable has a __metatable
1674 * key, the value of that key is returned instead of the
1675 * actual metatable.
1676 *
1677 * __tostring - Control string representation. When the builtin
1678 * "tostring( myTable )" function is called, if the metatable
1679 * for myTable has a __tostring property set to a function,
1680 * that function is invoked (passing myTable to it) and the
1681 * return value is used as the string representation.
1682 *
1683 * __len - Control table length. When the table length is requested using
1684 * the length operator ( '#' ), if the metatable for myTable has
1685 * a __len key pointing to a function, that function is invoked
1686 * (passing myTable to it) and the return value used as the value
1687 * of "#myTable".
1688 *
1689 * __gc - Userdata finalizer code. When userdata is set to be garbage
1690 * collected, if the metatable has a __gc field pointing to a
1691 * function, that function is first invoked, passing the userdata
1692 * to it. The __gc metamethod is not called for tables.
1693 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1694 *
1695 * Special metamethods for redefining standard operators:
1696 * http://www.lua.org/pil/13.1.html
1697 *
1698 * __add "+"
1699 * __sub "-"
1700 * __mul "*"
1701 * __div "/"
1702 * __unm "!"
1703 * __pow "^"
1704 * __concat ".."
1705 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001706 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001707 * http://www.lua.org/pil/13.2.html
1708 *
1709 * __eq "=="
1710 * __lt "<"
1711 * __le "<="
1712 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713
1714/*
1715 *
1716 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001717 * Class Map
1718 *
1719 *
1720 */
1721
1722/* Returns a struct hlua_map if the stack entry "ud" is
1723 * a class session, otherwise it throws an error.
1724 */
1725__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1726{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001727 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001728}
1729
1730/* This function is the map constructor. It don't need
1731 * the class Map object. It creates and return a new Map
1732 * object. It must be called only during "body" or "init"
1733 * context because it process some filesystem accesses.
1734 */
1735__LJMP static int hlua_map_new(struct lua_State *L)
1736{
1737 const char *fn;
1738 int match = PAT_MATCH_STR;
1739 struct sample_conv conv;
1740 const char *file = "";
1741 int line = 0;
1742 lua_Debug ar;
1743 char *err = NULL;
1744 struct arg args[2];
1745
1746 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1747 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1748
1749 fn = MAY_LJMP(luaL_checkstring(L, 1));
1750
1751 if (lua_gettop(L) >= 2) {
1752 match = MAY_LJMP(luaL_checkinteger(L, 2));
1753 if (match < 0 || match >= PAT_MATCH_NUM)
1754 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1755 }
1756
1757 /* Get Lua filename and line number. */
1758 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1759 lua_getinfo(L, "Sl", &ar); /* get info about it */
1760 if (ar.currentline > 0) { /* is there info? */
1761 file = ar.short_src;
1762 line = ar.currentline;
1763 }
1764 }
1765
1766 /* fill fake sample_conv struct. */
1767 conv.kw = ""; /* unused. */
1768 conv.process = NULL; /* unused. */
1769 conv.arg_mask = 0; /* unused. */
1770 conv.val_args = NULL; /* unused. */
1771 conv.out_type = SMP_T_STR;
1772 conv.private = (void *)(long)match;
1773 switch (match) {
1774 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1775 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1776 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1777 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1778 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1779 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1780 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001781 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001782 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1783 default:
1784 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1785 }
1786
1787 /* fill fake args. */
1788 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001789 args[0].data.str.area = strdup(fn);
1790 args[0].data.str.data = strlen(fn);
1791 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001792 args[1].type = ARGT_STOP;
1793
1794 /* load the map. */
1795 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001796 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001797 * free the err variable.
1798 */
1799 luaL_where(L, 1);
1800 lua_pushfstring(L, "'new': %s.", err);
1801 lua_concat(L, 2);
1802 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001803 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001804 WILL_LJMP(lua_error(L));
1805 }
1806
1807 /* create the lua object. */
1808 lua_newtable(L);
1809 lua_pushlightuserdata(L, args[0].data.map);
1810 lua_rawseti(L, -2, 0);
1811
1812 /* Pop a class Map metatable and affect it to the userdata. */
1813 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1814 lua_setmetatable(L, -2);
1815
1816
1817 return 1;
1818}
1819
1820__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1821{
1822 struct map_descriptor *desc;
1823 struct pattern *pat;
1824 struct sample smp;
1825
1826 MAY_LJMP(check_args(L, 2, "lookup"));
1827 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001828 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001829 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001830 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001831 }
1832 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001833 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001834 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001835 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 +01001836 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001837 }
1838
1839 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001840 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001841 if (str)
1842 lua_pushstring(L, "");
1843 else
1844 lua_pushnil(L);
1845 return 1;
1846 }
1847
1848 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001849 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001850 return 1;
1851}
1852
1853__LJMP static int hlua_map_lookup(struct lua_State *L)
1854{
1855 return _hlua_map_lookup(L, 0);
1856}
1857
1858__LJMP static int hlua_map_slookup(struct lua_State *L)
1859{
1860 return _hlua_map_lookup(L, 1);
1861}
1862
1863/*
1864 *
1865 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 * Class Socket
1867 *
1868 *
1869 */
1870
1871__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1872{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001873 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874}
1875
1876/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001877 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001878 * received.
1879 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001880static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001881{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001882 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001884 if (appctx->ctx.hlua_cosocket.die) {
1885 si_shutw(si);
1886 si_shutr(si);
1887 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001888 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1889 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001890 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1891 }
1892
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001893 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001894 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001895 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001896
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001897 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001898 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001899 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001900
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001901 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001902 * to be notified whenever the connection completes.
1903 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001904 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001905 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001906 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001907 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001908 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001909 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001910
1911 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001912 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001913
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001914 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001915 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001916 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001917
1918 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001919 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001920 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001921
1922 /* Some data were injected in the buffer, notify the stream
1923 * interface.
1924 */
1925 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001926 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001927
1928 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001929 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001930 */
1931 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001932 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933}
1934
Willy Tarreau87b09662015-04-03 00:22:06 +02001935/* This function is called when the "struct stream" is destroyed.
1936 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001937 * Wake all the pending signals.
1938 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001939static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001940{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001941 struct xref *peer;
1942
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001943 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001944 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1945 if (peer)
1946 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947
1948 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001949 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1950 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001951}
1952
1953/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001954 * uses this object. If the stream does not exists, just quit.
1955 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956 * pending signal can rest in the read and write lists. destroy
1957 * it.
1958 */
1959__LJMP static int hlua_socket_gc(lua_State *L)
1960{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001961 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001963 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001964
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001965 MAY_LJMP(check_args(L, 1, "__gc"));
1966
1967 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001968 peer = xref_get_peer_and_lock(&socket->xref);
1969 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001970 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001971 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001972
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001973 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001974 appctx->ctx.hlua_cosocket.die = 1;
1975 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001977 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001978 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001979 return 0;
1980}
1981
1982/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001983 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001984 */
sada05ed3302018-05-11 11:48:18 -07001985__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001987 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001989 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001990 struct hlua *hlua;
1991
1992 /* Get hlua struct, or NULL if we execute from main lua state */
1993 hlua = hlua_gethlua(L);
1994 if (!hlua)
1995 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001996
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001997 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001998
1999 /* Check if we run on the same thread than the xreator thread.
2000 * We cannot access to the socket if the thread is different.
2001 */
2002 if (socket->tid != tid)
2003 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2004
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002005 peer = xref_get_peer_and_lock(&socket->xref);
2006 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002007 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01002008
2009 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002010 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002012 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002013 appctx->ctx.hlua_cosocket.die = 1;
2014 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002015
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002016 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002017 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002018 return 0;
2019}
2020
sada05ed3302018-05-11 11:48:18 -07002021/* The close function calls close_helper.
2022 */
2023__LJMP static int hlua_socket_close(lua_State *L)
2024{
2025 MAY_LJMP(check_args(L, 1, "close"));
2026 return hlua_socket_close_helper(L);
2027}
2028
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029/* This Lua function assumes that the stack contain three parameters.
2030 * 1 - USERDATA containing a struct socket
2031 * 2 - INTEGER with values of the macro defined below
2032 * If the integer is -1, we must read at most one line.
2033 * If the integer is -2, we ust read all the data until the
2034 * end of the stream.
2035 * If the integer is positive value, we must read a number of
2036 * bytes corresponding to this value.
2037 */
2038#define HLSR_READ_LINE (-1)
2039#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002040__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002041{
2042 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2043 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002044 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002046 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002047 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02002048 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002049 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02002050 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002051 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002052 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01002053 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002054 struct stream_interface *si;
2055 struct stream *s;
2056 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002057 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002058
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002059 /* Get hlua struct, or NULL if we execute from main lua state */
2060 hlua = hlua_gethlua(L);
2061
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002062 /* Check if this lua stack is schedulable. */
2063 if (!hlua || !hlua->task)
2064 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
2065 "'frontend', 'backend' or 'task'"));
2066
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002067 /* Check if we run on the same thread than the xreator thread.
2068 * We cannot access to the socket if the thread is different.
2069 */
2070 if (socket->tid != tid)
2071 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2072
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002073 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002074 peer = xref_get_peer_and_lock(&socket->xref);
2075 if (!peer)
2076 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002077 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2078 si = appctx->owner;
2079 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002080
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002081 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002084 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002085 if (nblk < 0) /* Connection close. */
2086 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002087 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002089
2090 /* remove final \r\n. */
2091 if (nblk == 1) {
2092 if (blk1[len1-1] == '\n') {
2093 len1--;
2094 skip_at_end++;
2095 if (blk1[len1-1] == '\r') {
2096 len1--;
2097 skip_at_end++;
2098 }
2099 }
2100 }
2101 else {
2102 if (blk2[len2-1] == '\n') {
2103 len2--;
2104 skip_at_end++;
2105 if (blk2[len2-1] == '\r') {
2106 len2--;
2107 skip_at_end++;
2108 }
2109 }
2110 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002111 }
2112
2113 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002114 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002115 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116 if (nblk < 0) /* Connection close. */
2117 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002118 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119 goto connection_empty;
2120 }
2121
2122 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002123 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002124 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002125 if (nblk < 0) /* Connection close. */
2126 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002127 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002128 goto connection_empty;
2129
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002130 missing_bytes = wanted - socket->b.n;
2131 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002132 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002133 len1 = missing_bytes;
2134 } if (nblk == 2 && len1 + len2 > missing_bytes)
2135 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002136 }
2137
2138 len = len1;
2139
2140 luaL_addlstring(&socket->b, blk1, len1);
2141 if (nblk == 2) {
2142 len += len2;
2143 luaL_addlstring(&socket->b, blk2, len2);
2144 }
2145
2146 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002147 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148
2149 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002150 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151
2152 /* If the pattern reclaim to read all the data
2153 * in the connection, got out.
2154 */
2155 if (wanted == HLSR_READ_ALL)
2156 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002157 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002158 goto connection_empty;
2159
2160 /* Return result. */
2161 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002162 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002163 return 1;
2164
2165connection_closed:
2166
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002167 xref_unlock(&socket->xref, peer);
2168
2169no_peer:
2170
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171 /* If the buffer containds data. */
2172 if (socket->b.n > 0) {
2173 luaL_pushresult(&socket->b);
2174 return 1;
2175 }
2176 lua_pushnil(L);
2177 lua_pushstring(L, "connection closed.");
2178 return 2;
2179
2180connection_empty:
2181
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002182 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2183 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002184 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002185 }
2186 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002187 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002188 return 0;
2189}
2190
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002191/* This Lua function gets two parameters. The first one can be string
2192 * or a number. If the string is "*l", the user requires one line. If
2193 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002194 * If the value is a number, the user require a number of bytes equal
2195 * to the value. The default value is "*l" (a line).
2196 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002197 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198 * integer takes this values:
2199 * -1 : read a line
2200 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002201 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002203 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204 * concatenated with the read data.
2205 */
2206__LJMP static int hlua_socket_receive(struct lua_State *L)
2207{
2208 int wanted = HLSR_READ_LINE;
2209 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002210 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002211 char *error;
2212 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002213 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002214
2215 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2216 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2217
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002218 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002219
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002220 /* Check if we run on the same thread than the xreator thread.
2221 * We cannot access to the socket if the thread is different.
2222 */
2223 if (socket->tid != tid)
2224 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2225
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002226 /* check for pattern. */
2227 if (lua_gettop(L) >= 2) {
2228 type = lua_type(L, 2);
2229 if (type == LUA_TSTRING) {
2230 pattern = lua_tostring(L, 2);
2231 if (strcmp(pattern, "*a") == 0)
2232 wanted = HLSR_READ_ALL;
2233 else if (strcmp(pattern, "*l") == 0)
2234 wanted = HLSR_READ_LINE;
2235 else {
2236 wanted = strtoll(pattern, &error, 10);
2237 if (*error != '\0')
2238 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2239 }
2240 }
2241 else if (type == LUA_TNUMBER) {
2242 wanted = lua_tointeger(L, 2);
2243 if (wanted < 0)
2244 WILL_LJMP(luaL_error(L, "Unsupported size."));
2245 }
2246 }
2247
2248 /* Set pattern. */
2249 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002250
2251 /* Check if we would replace the top by itself. */
2252 if (lua_gettop(L) != 2)
2253 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002254
Christopher Fauletc31b2002021-05-03 10:11:13 +02002255 /* Save index of the top of the stack because since buffers are used, it
2256 * may change
2257 */
2258 lastarg = lua_gettop(L);
2259
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002260 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002261 luaL_buffinit(L, &socket->b);
2262
2263 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002264 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265 if (lua_type(L, 3) != LUA_TSTRING)
2266 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2267 pattern = lua_tolstring(L, 3, &len);
2268 luaL_addlstring(&socket->b, pattern, len);
2269 }
2270
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002271 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002272}
2273
2274/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002275 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002276 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002277static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278{
2279 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002280 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281 struct appctx *appctx;
2282 size_t buf_len;
2283 const char *buf;
2284 int len;
2285 int send_len;
2286 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002287 struct xref *peer;
2288 struct stream_interface *si;
2289 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002291 /* Get hlua struct, or NULL if we execute from main lua state */
2292 hlua = hlua_gethlua(L);
2293
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294 /* Check if this lua stack is schedulable. */
2295 if (!hlua || !hlua->task)
2296 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2297 "'frontend', 'backend' or 'task'"));
2298
2299 /* Get object */
2300 socket = MAY_LJMP(hlua_checksocket(L, 1));
2301 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002302 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002304 /* Check if we run on the same thread than the xreator thread.
2305 * We cannot access to the socket if the thread is different.
2306 */
2307 if (socket->tid != tid)
2308 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2309
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002310 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002311 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002312 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002313 lua_pushinteger(L, -1);
2314 return 1;
2315 }
2316 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2317 si = appctx->owner;
2318 s = si_strm(si);
2319
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002320 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002321 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002322 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002323 lua_pushinteger(L, -1);
2324 return 1;
2325 }
2326
2327 /* Update the input buffer data. */
2328 buf += sent;
2329 send_len = buf_len - sent;
2330
2331 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002332 if (sent >= buf_len) {
2333 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002335 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002336
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002337 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002338 * the request buffer if its not required.
2339 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002340 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002341 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002342 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002343 }
2344
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002345 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002346 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002347 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002348 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002349 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
2351 /* send data */
2352 if (len < send_len)
2353 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002354 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002355
2356 /* "Not enough space" (-1), "Buffer too little to contain
2357 * the data" (-2) are not expected because the available length
2358 * is tested.
2359 * Other unknown error are also not expected.
2360 */
2361 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002362 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002363 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002364
sada05ed3302018-05-11 11:48:18 -07002365 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002367 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002368 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002369 return 1;
2370 }
2371
2372 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002373 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002374
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002375 s->req.rex = TICK_ETERNITY;
2376 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002377
2378 /* Update length sent. */
2379 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002380 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002381
2382 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002383 if (sent + len >= buf_len) {
2384 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002386 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387
2388hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002389 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2390 xref_unlock(&socket->xref, peer);
2391 WILL_LJMP(luaL_error(L, "out of memory"));
2392 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002393 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002394 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002395 return 0;
2396}
2397
2398/* This function initiate the send of data. It just check the input
2399 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002400 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002401 * "hlua_socket_write_yield" that can yield.
2402 *
2403 * The Lua function gets between 3 and 4 parameters. The first one is
2404 * the associated object. The second is a string buffer. The third is
2405 * a facultative integer that represents where is the buffer position
2406 * of the start of the data that can send. The first byte is the
2407 * position "1". The default value is "1". The fourth argument is a
2408 * facultative integer that represents where is the buffer position
2409 * of the end of the data that can send. The default is the last byte.
2410 */
2411static int hlua_socket_send(struct lua_State *L)
2412{
2413 int i;
2414 int j;
2415 const char *buf;
2416 size_t buf_len;
2417
2418 /* Check number of arguments. */
2419 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2420 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2421
2422 /* Get the string. */
2423 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2424
2425 /* Get and check j. */
2426 if (lua_gettop(L) == 4) {
2427 j = MAY_LJMP(luaL_checkinteger(L, 4));
2428 if (j < 0)
2429 j = buf_len + j + 1;
2430 if (j > buf_len)
2431 j = buf_len + 1;
2432 lua_pop(L, 1);
2433 }
2434 else
2435 j = buf_len;
2436
2437 /* Get and check i. */
2438 if (lua_gettop(L) == 3) {
2439 i = MAY_LJMP(luaL_checkinteger(L, 3));
2440 if (i < 0)
2441 i = buf_len + i + 1;
2442 if (i > buf_len)
2443 i = buf_len + 1;
2444 lua_pop(L, 1);
2445 } else
2446 i = 1;
2447
2448 /* Check bth i and j. */
2449 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002450 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002451 return 1;
2452 }
2453 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002454 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002455 return 1;
2456 }
2457 if (i == 0)
2458 i = 1;
2459 if (j == 0)
2460 j = 1;
2461
2462 /* Pop the string. */
2463 lua_pop(L, 1);
2464
2465 /* Update the buffer length. */
2466 buf += i - 1;
2467 buf_len = j - i + 1;
2468 lua_pushlstring(L, buf, buf_len);
2469
2470 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002471 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002473 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002474}
2475
Willy Tarreau22b0a682015-06-17 19:43:49 +02002476#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002477__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2478{
2479 static char buffer[SOCKET_INFO_MAX_LEN];
2480 int ret;
2481 int len;
2482 char *p;
2483
2484 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2485 if (ret <= 0) {
2486 lua_pushnil(L);
2487 return 1;
2488 }
2489
2490 if (ret == AF_UNIX) {
2491 lua_pushstring(L, buffer+1);
2492 return 1;
2493 }
2494 else if (ret == AF_INET6) {
2495 buffer[0] = '[';
2496 len = strlen(buffer);
2497 buffer[len] = ']';
2498 len++;
2499 buffer[len] = ':';
2500 len++;
2501 p = buffer;
2502 }
2503 else if (ret == AF_INET) {
2504 p = buffer + 1;
2505 len = strlen(p);
2506 p[len] = ':';
2507 len++;
2508 }
2509 else {
2510 lua_pushnil(L);
2511 return 1;
2512 }
2513
2514 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2515 lua_pushnil(L);
2516 return 1;
2517 }
2518
2519 lua_pushstring(L, p);
2520 return 1;
2521}
2522
2523/* Returns information about the peer of the connection. */
2524__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2525{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002526 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002527 struct xref *peer;
2528 struct appctx *appctx;
2529 struct stream_interface *si;
2530 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002531 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002532
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533 MAY_LJMP(check_args(L, 1, "getpeername"));
2534
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002535 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002536
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002537 /* Check if we run on the same thread than the xreator thread.
2538 * We cannot access to the socket if the thread is different.
2539 */
2540 if (socket->tid != tid)
2541 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2542
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002543 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002544 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002545 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002546 lua_pushnil(L);
2547 return 1;
2548 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002549 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2550 si = appctx->owner;
2551 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002552
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002553 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002554 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002555 lua_pushnil(L);
2556 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002557 }
2558
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002559 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002560 xref_unlock(&socket->xref, peer);
2561 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002562}
2563
2564/* Returns information about my connection side. */
2565static int hlua_socket_getsockname(struct lua_State *L)
2566{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002567 struct hlua_socket *socket;
2568 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002569 struct appctx *appctx;
2570 struct xref *peer;
2571 struct stream_interface *si;
2572 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002573 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002574
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002575 MAY_LJMP(check_args(L, 1, "getsockname"));
2576
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002577 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002578
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002579 /* Check if we run on the same thread than the xreator thread.
2580 * We cannot access to the socket if the thread is different.
2581 */
2582 if (socket->tid != tid)
2583 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2584
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002585 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002586 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002587 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002588 lua_pushnil(L);
2589 return 1;
2590 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002591 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2592 si = appctx->owner;
2593 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002594
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002595 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002596 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002597 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598 lua_pushnil(L);
2599 return 1;
2600 }
2601
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002602 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002603 xref_unlock(&socket->xref, peer);
2604 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605}
2606
2607/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002608static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609 .obj_type = OBJ_TYPE_APPLET,
2610 .name = "<LUA_TCP>",
2611 .fct = hlua_socket_handler,
2612 .release = hlua_socket_release,
2613};
2614
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002615__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616{
2617 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002618 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002619 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002621 struct stream_interface *si;
2622 struct stream *s;
2623
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002624 /* Get hlua struct, or NULL if we execute from main lua state */
2625 hlua = hlua_gethlua(L);
2626 if (!hlua)
2627 return 0;
2628
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002629 /* Check if we run on the same thread than the xreator thread.
2630 * We cannot access to the socket if the thread is different.
2631 */
2632 if (socket->tid != tid)
2633 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2634
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002635 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002636 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002637 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002638 lua_pushnil(L);
2639 lua_pushstring(L, "Can't connect");
2640 return 2;
2641 }
2642 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2643 si = appctx->owner;
2644 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002646 /* Check if we run on the same thread than the xreator thread.
2647 * We cannot access to the socket if the thread is different.
2648 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002649 if (socket->tid != tid) {
2650 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002651 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002652 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002653
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002655 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002656 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002657 lua_pushnil(L);
2658 lua_pushstring(L, "Can't connect");
2659 return 2;
2660 }
2661
Willy Tarreaue09101e2018-10-16 17:37:12 +02002662 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002663
2664 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002665 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002666 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002667 lua_pushinteger(L, 1);
2668 return 1;
2669 }
2670
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002671 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2672 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002673 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002674 }
2675 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002676 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002677 return 0;
2678}
2679
2680/* This function fail or initite the connection. */
2681__LJMP static int hlua_socket_connect(struct lua_State *L)
2682{
2683 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002684 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002685 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002686 struct hlua *hlua;
2687 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002688 int low, high;
2689 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002690 struct xref *peer;
2691 struct stream_interface *si;
2692 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002693
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002694 if (lua_gettop(L) < 2)
2695 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002696
2697 /* Get args. */
2698 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002699
2700 /* Check if we run on the same thread than the xreator thread.
2701 * We cannot access to the socket if the thread is different.
2702 */
2703 if (socket->tid != tid)
2704 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2705
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002706 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002707 if (lua_gettop(L) >= 3) {
2708 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002709 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710
Tim Duesterhus6edab862018-01-06 19:04:45 +01002711 /* Force the ip to end with a colon, to support IPv6 addresses
2712 * that are not enclosed within square brackets.
2713 */
2714 if (port > 0) {
2715 luaL_buffinit(L, &b);
2716 luaL_addstring(&b, ip);
2717 luaL_addchar(&b, ':');
2718 luaL_pushresult(&b);
2719 ip = lua_tolstring(L, lua_gettop(L), NULL);
2720 }
2721 }
2722
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002723 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002724 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002725 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002726 lua_pushnil(L);
2727 return 1;
2728 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002729
2730 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002731 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 +02002732 if (!addr) {
2733 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002734 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002735 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002736
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002737 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002738 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002739 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002740 if (port == -1) {
2741 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002742 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002743 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002744 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2745 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002746 if (port == -1) {
2747 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002748 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002749 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002750 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002751 }
2752 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002753
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002754 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2755 si = appctx->owner;
2756 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002757
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002758 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002759 xref_unlock(&socket->xref, peer);
2760 WILL_LJMP(luaL_error(L, "connect: internal error"));
2761 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002762 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002763
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002764 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002765 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002766 if (!hlua)
2767 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002768
2769 /* inform the stream that we want to be notified whenever the
2770 * connection completes.
2771 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002772 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002773 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002774 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002775
Willy Tarreauf31af932020-01-14 09:59:38 +01002776 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002777
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002778 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2779 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002780 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002781 }
2782 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002783
2784 task_wakeup(s->task, TASK_WOKEN_INIT);
2785 /* Return yield waiting for connection. */
2786
Willy Tarreau9635e032018-10-16 17:52:55 +02002787 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002788
2789 return 0;
2790}
2791
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002792#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002793__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2794{
2795 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002796 struct xref *peer;
2797 struct appctx *appctx;
2798 struct stream_interface *si;
2799 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002800
2801 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2802 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002803
2804 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002805 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002806 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002807 lua_pushnil(L);
2808 return 1;
2809 }
2810 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2811 si = appctx->owner;
2812 s = si_strm(si);
2813
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002814 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002815 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002816 return MAY_LJMP(hlua_socket_connect(L));
2817}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002818#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002819
2820__LJMP static int hlua_socket_setoption(struct lua_State *L)
2821{
2822 return 0;
2823}
2824
2825__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2826{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002827 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002828 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002829 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002830 struct xref *peer;
2831 struct appctx *appctx;
2832 struct stream_interface *si;
2833 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002834
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002835 MAY_LJMP(check_args(L, 2, "settimeout"));
2836
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002837 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002838
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002839 /* convert the timeout to millis */
2840 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002841
Thierry Fournier17a921b2018-03-08 09:59:02 +01002842 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002843 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002844 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2845
Mark Lakes56cc1252018-03-27 09:48:06 +02002846 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002847 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002848
2849 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002850 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002851 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002852
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002853 /* Check if we run on the same thread than the xreator thread.
2854 * We cannot access to the socket if the thread is different.
2855 */
2856 if (socket->tid != tid)
2857 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2858
Mark Lakes56cc1252018-03-27 09:48:06 +02002859 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002860 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002861 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002862 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2863 WILL_LJMP(lua_error(L));
2864 return 0;
2865 }
2866 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2867 si = appctx->owner;
2868 s = si_strm(si);
2869
Cyril Bonté7bb63452018-08-17 23:51:02 +02002870 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002871 s->req.rto = tmout;
2872 s->req.wto = tmout;
2873 s->res.rto = tmout;
2874 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002875 s->req.rex = tick_add_ifset(now_ms, tmout);
2876 s->req.wex = tick_add_ifset(now_ms, tmout);
2877 s->res.rex = tick_add_ifset(now_ms, tmout);
2878 s->res.wex = tick_add_ifset(now_ms, tmout);
2879
2880 s->task->expire = tick_add_ifset(now_ms, tmout);
2881 task_queue(s->task);
2882
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002883 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002884
Thierry Fourniere9636f12018-03-08 09:54:32 +01002885 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002886 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002887}
2888
2889__LJMP static int hlua_socket_new(lua_State *L)
2890{
2891 struct hlua_socket *socket;
2892 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002893 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002894 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002895
2896 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002897 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002898 hlua_pusherror(L, "socket: full stack");
2899 goto out_fail_conf;
2900 }
2901
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002902 /* Create the object: obj[0] = userdata. */
2903 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002904 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002905 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002906 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002907 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002908
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002909 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002910 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002911 hlua_pusherror(L, "socket: uninitialized pools.");
2912 goto out_fail_conf;
2913 }
2914
Willy Tarreau87b09662015-04-03 00:22:06 +02002915 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002916 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2917 lua_setmetatable(L, -2);
2918
Willy Tarreaud420a972015-04-06 00:39:18 +02002919 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002920 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002921 if (!appctx) {
2922 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002923 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002924 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002925
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002926 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002927 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002928 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2929 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002930
Willy Tarreaud420a972015-04-06 00:39:18 +02002931 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002932 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002933 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002934 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002935 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002936 }
2937
Christopher Faulet26256f82020-09-14 11:40:13 +02002938 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002939 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002940 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002941 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002942 }
2943
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002944 /* Initialise cross reference between stream and Lua socket object. */
2945 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002946
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002947 /* Configure "right" stream interface. this "si" is used to connect
2948 * and retrieve data from the server. The connection is initialized
2949 * with the "struct server".
2950 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002951 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002952
2953 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002954 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002955 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002956
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002957 return 1;
2958
Willy Tarreaud420a972015-04-06 00:39:18 +02002959 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002960 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002961 out_fail_sess:
2962 appctx_free(appctx);
2963 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002964 WILL_LJMP(lua_error(L));
2965 return 0;
2966}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002967
2968/*
2969 *
2970 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 * Class Channel
2972 *
2973 *
2974 */
2975
2976/* Returns the struct hlua_channel join to the class channel in the
2977 * stack entry "ud" or throws an argument error.
2978 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002979__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002980{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002981 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002982}
2983
Willy Tarreau47860ed2015-03-10 14:07:50 +01002984/* Pushes the channel onto the top of the stack. If the stask does not have a
2985 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002987static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002988{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002989 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002990 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002991 return 0;
2992
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002993 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002994 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002995 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996
2997 /* Pop a class sesison metatable and affect it to the userdata. */
2998 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2999 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000 return 1;
3001}
3002
3003/* Duplicate all the data present in the input channel and put it
3004 * in a string LUA variables. Returns -1 and push a nil value in
3005 * the stack if the channel is closed and all the data are consumed,
3006 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05003007 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003008 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003009static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010{
3011 char *blk1;
3012 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003013 size_t len1;
3014 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015 int ret;
3016 luaL_Buffer b;
3017
Willy Tarreau06d80a92017-10-19 14:32:15 +02003018 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02003019 if (unlikely(ret <= 0)) {
3020 if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) {
3021 lua_pushnil(L);
3022 return -1;
3023 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 return 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003025 }
3026
3027 luaL_buffinit(L, &b);
3028 luaL_addlstring(&b, blk1, len1);
3029 if (unlikely(ret == 2))
3030 luaL_addlstring(&b, blk2, len2);
3031 luaL_pushresult(&b);
3032
3033 if (unlikely(ret == 2))
3034 return len1 + len2;
3035 return len1;
3036}
3037
3038/* "_hlua_channel_dup" wrapper. If no data are available, it returns
3039 * a yield. This function keep the data in the buffer.
3040 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003041__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003042{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003043 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003044
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003045 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3046
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003047 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003048 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003049 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003050 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003051
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02003053 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054 return 1;
3055}
3056
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003057/* Check arguments for the function "hlua_channel_dup_yield". */
3058__LJMP static int hlua_channel_dup(lua_State *L)
3059{
3060 MAY_LJMP(check_args(L, 1, "dup"));
3061 MAY_LJMP(hlua_checkchannel(L, 1));
3062 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
3063}
3064
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065/* "_hlua_channel_dup" wrapper. If no data are available, it returns
3066 * a yield. This function consumes the data in the buffer. It returns
3067 * a string containing the data or a nil pointer if no data are available
3068 * and the channel is closed.
3069 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003070__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003072 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003073 int ret;
3074
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003075 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003076
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003077 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003078 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003079 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003080 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003081
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003082 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003083 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02003084 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003085
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003086 if (unlikely(ret == -1))
3087 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003088
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003089 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003090 return 1;
3091}
3092
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003093/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003094__LJMP static int hlua_channel_get(lua_State *L)
3095{
3096 MAY_LJMP(check_args(L, 1, "get"));
3097 MAY_LJMP(hlua_checkchannel(L, 1));
3098 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3099}
3100
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003101/* This functions consumes and returns one line. If the channel is closed,
3102 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003103 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003104 * value.
3105 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003106__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003108 char *blk1;
3109 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003110 size_t len1;
3111 size_t len2;
3112 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003113 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003114 int ret;
3115 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003116
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003117 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3118
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003119 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003120 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003121 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003122 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003123
Willy Tarreau06d80a92017-10-19 14:32:15 +02003124 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02003125 if (ret == 0) {
3126 if (HLUA_CANT_YIELD(hlua_gethlua(L))) {
3127 _hlua_channel_dup(chn, L);
3128 return 1;
3129 }
Willy Tarreau9635e032018-10-16 17:52:55 +02003130 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet055310f2021-08-05 11:58:37 +02003131 }
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003132
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003133 if (ret == -1) {
3134 lua_pushnil(L);
3135 return 1;
3136 }
3137
3138 luaL_buffinit(L, &b);
3139 luaL_addlstring(&b, blk1, len1);
3140 len = len1;
3141 if (unlikely(ret == 2)) {
3142 luaL_addlstring(&b, blk2, len2);
3143 len += len2;
3144 }
3145 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003146 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003147 return 1;
3148}
3149
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003150/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003151__LJMP static int hlua_channel_getline(lua_State *L)
3152{
3153 MAY_LJMP(check_args(L, 1, "getline"));
3154 MAY_LJMP(hlua_checkchannel(L, 1));
3155 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3156}
3157
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003158/* This function takes a string as argument, and append it at the input side of
3159 * channel. If data cannot be copied, because there is not enough space to do so
3160 * or because the channel is closed, it returns -1. Otherwise, it returns the
3161 * amount of data written (the string length). This function does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162 */
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003163__LJMP static int hlua_channel_append(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164{
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003165 struct channel *chn;
3166 const char *str;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003167 size_t len;
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003168
3169 MAY_LJMP(check_args(L, 2, "append"));
3170 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3171 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003173 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003174 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003175 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003176 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003177
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003178 if (len > c_room(chn) || ci_putblk(chn, str, len) < 0)
3179 lua_pushinteger(L, -1);
3180 else
3181 lua_pushinteger(L, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003182 return 1;
3183}
3184
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003185/* The function replaces input data from the channel by the string passed as
3186 * argument. Before clearing the buffer, we take care the copy will be
3187 * possible. It returns the amount of data written (the string length) on
3188 * success or -1 if the copy is not performed. This function does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003189 */
3190__LJMP static int hlua_channel_set(lua_State *L)
3191{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003192 struct channel *chn;
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003193 const char *str;
3194 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003195
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003196 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003197 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003198 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003199
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003200 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003201 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003202 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003203 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003204
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003205 /* Be sure we can copied the string once input data will be removed. */
3206 if (len > ci_data(chn) || channel_input_closed(chn))
3207 lua_pushinteger(L, -1);
3208 else {
3209 b_set_data(&chn->buf, co_data(chn));
3210 if (ci_putblk(chn, str, len) < 0)
3211 lua_pushinteger(L, -1);
3212 else
3213 lua_pushinteger(L, len);
3214 }
3215 return -1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003216}
3217
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003218/* Append data in the output side of the buffer. This data is immediately
3219 * sent. The function returns the amount of data written. If the buffer
3220 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003221 * if the channel is closed.
3222 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003223__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003224{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003225 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003226 size_t len;
3227 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3228 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3229 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003230 struct hlua *hlua;
3231
3232 /* Get hlua struct, or NULL if we execute from main lua state */
3233 hlua = hlua_gethlua(L);
3234 if (!hlua) {
3235 lua_pushnil(L);
3236 return 1;
3237 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003238
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003239 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003240 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003241 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003242 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003243
Willy Tarreau47860ed2015-03-10 14:07:50 +01003244 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003245 lua_pushinteger(L, -1);
3246 return 1;
3247 }
3248
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003249 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003250 * the request buffer if its not required.
3251 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003252 if (chn->buf.size == 0) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003253 if (HLUA_CANT_YIELD(hlua_gethlua(L)))
3254 return 1;
Willy Tarreau4b962a42018-11-15 11:03:21 +01003255 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003256 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003257 }
3258
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003259 /* The written data will be immediately sent, so we can check
3260 * the available space without taking in account the reserve.
3261 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003262 * data, because the buffer will be flushed.
3263 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003264 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003265
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003266 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003267 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003268 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003269 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003270 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003271 return 1;
3272
3273 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003274 if (max > len - l)
3275 max = len - l;
3276
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003277 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003278 * detects a non contiguous buffer and realign it.
3279 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003280 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003281 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003282
3283 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003284 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003285
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003286 /* buffer replace considers that the input part is filled.
3287 * so, I must forward these new data in the output part.
3288 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003289 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003290
3291 l += max;
3292 lua_pop(L, 1);
3293 lua_pushinteger(L, l);
3294
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003295 if (l < len) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003296 /* If there is no space available, and the output buffer is empty.
3297 * in this case, we cannot add more data, so we cannot yield,
3298 * we return the amount of copied data.
3299 */
3300 max = b_room(&chn->buf);
3301 if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L)))
3302 return 1;
3303
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003304 /* If we are waiting for space in the response buffer, we
3305 * must set the flag WAKERESWR. This flag required the task
3306 * wake up if any activity is detected on the response buffer.
3307 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003308 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003309 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003310 else
3311 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003312 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003313 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003314
3315 return 1;
3316}
3317
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003318/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003319 * yield the LUA process, and resume it without checking the
3320 * input arguments.
3321 */
3322__LJMP static int hlua_channel_send(lua_State *L)
3323{
3324 MAY_LJMP(check_args(L, 2, "send"));
3325 lua_pushinteger(L, 0);
3326
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003327 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003328}
3329
3330/* This function forward and amount of butes. The data pass from
3331 * the input side of the buffer to the output side, and can be
3332 * forwarded. This function never fails.
3333 *
3334 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003335 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003336 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003337__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003338{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003339 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003340 int len;
3341 int l;
3342 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003343 struct hlua *hlua;
3344
3345 /* Get hlua struct, or NULL if we execute from main lua state */
3346 hlua = hlua_gethlua(L);
3347 if (!hlua)
3348 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003349
3350 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003351
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003352 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003353 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003354 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003355 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003356
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003357 len = MAY_LJMP(luaL_checkinteger(L, 2));
3358 l = MAY_LJMP(luaL_checkinteger(L, -1));
3359
3360 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003361 if (max > ci_data(chn))
3362 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003363 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003364 l += max;
3365
3366 lua_pop(L, 1);
3367 lua_pushinteger(L, l);
3368
3369 /* Check if it miss bytes to forward. */
3370 if (l < len) {
3371 /* The the input channel or the output channel are closed, we
3372 * must return the amount of data forwarded.
3373 */
Christopher Faulet055310f2021-08-05 11:58:37 +02003374 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003375 return 1;
3376
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003377 /* If we are waiting for space data in the response buffer, we
3378 * must set the flag WAKERESWR. This flag required the task
3379 * wake up if any activity is detected on the response buffer.
3380 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003381 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003382 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003383 else
3384 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003385
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003386 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003387 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003388 }
3389
3390 return 1;
3391}
3392
3393/* Just check the input and prepare the stack for the previous
3394 * function "hlua_channel_forward_yield"
3395 */
3396__LJMP static int hlua_channel_forward(lua_State *L)
3397{
3398 MAY_LJMP(check_args(L, 2, "forward"));
3399 MAY_LJMP(hlua_checkchannel(L, 1));
3400 MAY_LJMP(luaL_checkinteger(L, 2));
3401
3402 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003403 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003404}
3405
3406/* Just returns the number of bytes available in the input
3407 * side of the buffer. This function never fails.
3408 */
3409__LJMP static int hlua_channel_get_in_len(lua_State *L)
3410{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003411 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003412
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003413 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003414 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003415 if (IS_HTX_STRM(chn_strm(chn))) {
3416 struct htx *htx = htxbuf(&chn->buf);
3417 lua_pushinteger(L, htx->data - co_data(chn));
3418 }
3419 else
3420 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003421 return 1;
3422}
3423
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003424/* Returns true if the channel is full. */
3425__LJMP static int hlua_channel_is_full(lua_State *L)
3426{
3427 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003428
3429 MAY_LJMP(check_args(L, 1, "is_full"));
3430 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003431 /* ignore the reserve, we are not on a producer side (ie in an
3432 * applet).
3433 */
3434 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003435 return 1;
3436}
3437
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003438/* Returns true if the channel is the response channel. */
3439__LJMP static int hlua_channel_is_resp(lua_State *L)
3440{
3441 struct channel *chn;
3442
3443 MAY_LJMP(check_args(L, 1, "is_resp"));
3444 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3445
3446 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3447 return 1;
3448}
3449
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003450/* Just returns the number of bytes available in the output
3451 * side of the buffer. This function never fails.
3452 */
3453__LJMP static int hlua_channel_get_out_len(lua_State *L)
3454{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003455 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003456
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003457 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003458 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003459 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003460 return 1;
3461}
3462
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003463/*
3464 *
3465 *
3466 * Class Fetches
3467 *
3468 *
3469 */
3470
3471/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003472 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003473 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003474__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003475{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003476 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003477}
3478
3479/* This function creates and push in the stack a fetch object according
3480 * with a current TXN.
3481 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003482static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003483{
Willy Tarreau7073c472015-04-06 11:15:40 +02003484 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003485
3486 /* Check stack size. */
3487 if (!lua_checkstack(L, 3))
3488 return 0;
3489
3490 /* Create the object: obj[0] = userdata.
3491 * Note that the base of the Fetches object is the
3492 * transaction object.
3493 */
3494 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003495 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003496 lua_rawseti(L, -2, 0);
3497
Willy Tarreau7073c472015-04-06 11:15:40 +02003498 hsmp->s = txn->s;
3499 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003500 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003501 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003502
3503 /* Pop a class sesison metatable and affect it to the userdata. */
3504 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3505 lua_setmetatable(L, -2);
3506
3507 return 1;
3508}
3509
3510/* This function is an LUA binding. It is called with each sample-fetch.
3511 * It uses closure argument to store the associated sample-fetch. It
3512 * returns only one argument or throws an error. An error is thrown
3513 * only if an error is encountered during the argument parsing. If
3514 * the "sample-fetch" function fails, nil is returned.
3515 */
3516__LJMP static int hlua_run_sample_fetch(lua_State *L)
3517{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003518 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003519 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003520 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003521 int i;
3522 struct sample smp;
3523
3524 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003525 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003526
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003527 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003528 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003529
Thierry FOURNIERca988662015-12-20 18:43:03 +01003530 /* Check execution authorization. */
3531 if (f->use & SMP_USE_HTTP_ANY &&
3532 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3533 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3534 "is not available in Lua services", f->kw);
3535 WILL_LJMP(lua_error(L));
3536 }
3537
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003538 /* Get extra arguments. */
3539 for (i = 0; i < lua_gettop(L) - 1; i++) {
3540 if (i >= ARGM_NBARGS)
3541 break;
3542 hlua_lua2arg(L, i + 2, &args[i]);
3543 }
3544 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003545 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003546
3547 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003548 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003549
3550 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003551 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003552 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003553 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003554 }
3555
3556 /* Initialise the sample. */
3557 memset(&smp, 0, sizeof(smp));
3558
3559 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003560 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003561 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003562 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003563 lua_pushstring(L, "");
3564 else
3565 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003566 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003567 }
3568
3569 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003570 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003571 hlua_smp2lua_str(L, &smp);
3572 else
3573 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003574
3575 end:
3576 for (i = 0; args[i].type != ARGT_STOP; i++) {
3577 if (args[i].type == ARGT_STR)
3578 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003579 else if (args[i].type == ARGT_REG)
3580 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003581 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003582 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003583
3584 error:
3585 for (i = 0; args[i].type != ARGT_STOP; i++) {
3586 if (args[i].type == ARGT_STR)
3587 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003588 else if (args[i].type == ARGT_REG)
3589 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003590 }
3591 WILL_LJMP(lua_error(L));
3592 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003593}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003594
3595/*
3596 *
3597 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003598 * Class Converters
3599 *
3600 *
3601 */
3602
3603/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003604 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003605 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003606__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003607{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003608 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003609}
3610
3611/* This function creates and push in the stack a Converters object
3612 * according with a current TXN.
3613 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003614static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003615{
Willy Tarreau7073c472015-04-06 11:15:40 +02003616 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003617
3618 /* Check stack size. */
3619 if (!lua_checkstack(L, 3))
3620 return 0;
3621
3622 /* Create the object: obj[0] = userdata.
3623 * Note that the base of the Converters object is the
3624 * same than the TXN object.
3625 */
3626 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003627 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003628 lua_rawseti(L, -2, 0);
3629
Willy Tarreau7073c472015-04-06 11:15:40 +02003630 hsmp->s = txn->s;
3631 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003632 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003633 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003634
Willy Tarreau87b09662015-04-03 00:22:06 +02003635 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003636 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3637 lua_setmetatable(L, -2);
3638
3639 return 1;
3640}
3641
3642/* This function is an LUA binding. It is called with each converter.
3643 * It uses closure argument to store the associated converter. It
3644 * returns only one argument or throws an error. An error is thrown
3645 * only if an error is encountered during the argument parsing. If
3646 * the converter function function fails, nil is returned.
3647 */
3648__LJMP static int hlua_run_sample_conv(lua_State *L)
3649{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003650 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003651 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003652 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003653 int i;
3654 struct sample smp;
3655
3656 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003657 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003658
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003659 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003660 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003661
3662 /* Get extra arguments. */
3663 for (i = 0; i < lua_gettop(L) - 2; i++) {
3664 if (i >= ARGM_NBARGS)
3665 break;
3666 hlua_lua2arg(L, i + 3, &args[i]);
3667 }
3668 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003669 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003670
3671 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003672 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003673
3674 /* Run the special args checker. */
3675 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3676 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003677 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003678 }
3679
3680 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003681 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003682 if (!hlua_lua2smp(L, 2, &smp)) {
3683 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003684 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003685 }
3686
Willy Tarreau1777ea62016-03-10 16:15:46 +01003687 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3688
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003689 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003690 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003691 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003692 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003693 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003694 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003695 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3696 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003697 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003698 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003699 }
3700
3701 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003702 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003703 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003704 lua_pushstring(L, "");
3705 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003706 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003707 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003708 }
3709
3710 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003711 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003712 hlua_smp2lua_str(L, &smp);
3713 else
3714 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003715 end:
3716 for (i = 0; args[i].type != ARGT_STOP; i++) {
3717 if (args[i].type == ARGT_STR)
3718 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003719 else if (args[i].type == ARGT_REG)
3720 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003721 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003722 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003723
3724 error:
3725 for (i = 0; args[i].type != ARGT_STOP; i++) {
3726 if (args[i].type == ARGT_STR)
3727 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003728 else if (args[i].type == ARGT_REG)
3729 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003730 }
3731 WILL_LJMP(lua_error(L));
3732 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003733}
3734
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003735/*
3736 *
3737 *
3738 * Class AppletTCP
3739 *
3740 *
3741 */
3742
3743/* Returns a struct hlua_txn if the stack entry "ud" is
3744 * a class stream, otherwise it throws an error.
3745 */
3746__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3747{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003748 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003749}
3750
3751/* This function creates and push in the stack an Applet object
3752 * according with a current TXN.
3753 */
3754static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3755{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003756 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003757 struct stream_interface *si = ctx->owner;
3758 struct stream *s = si_strm(si);
3759 struct proxy *p = s->be;
3760
3761 /* Check stack size. */
3762 if (!lua_checkstack(L, 3))
3763 return 0;
3764
3765 /* Create the object: obj[0] = userdata.
3766 * Note that the base of the Converters object is the
3767 * same than the TXN object.
3768 */
3769 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003770 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003772 luactx->appctx = ctx;
3773 luactx->htxn.s = s;
3774 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003775
3776 /* Create the "f" field that contains a list of fetches. */
3777 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003778 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003779 return 0;
3780 lua_settable(L, -3);
3781
3782 /* Create the "sf" field that contains a list of stringsafe fetches. */
3783 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003784 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003785 return 0;
3786 lua_settable(L, -3);
3787
3788 /* Create the "c" field that contains a list of converters. */
3789 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003790 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003791 return 0;
3792 lua_settable(L, -3);
3793
3794 /* Create the "sc" field that contains a list of stringsafe converters. */
3795 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003796 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003797 return 0;
3798 lua_settable(L, -3);
3799
3800 /* Pop a class stream metatable and affect it to the table. */
3801 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3802 lua_setmetatable(L, -2);
3803
3804 return 1;
3805}
3806
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003807__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3808{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003809 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003810 struct stream *s;
3811 const char *name;
3812 size_t len;
3813 struct sample smp;
3814
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003815 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3816 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003817
3818 /* It is useles to retrieve the stream, but this function
3819 * runs only in a stream context.
3820 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003821 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003822 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003823 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003824
3825 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003826 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003827 hlua_lua2smp(L, 3, &smp);
3828
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02003829 /* Store the sample in a variable. We don't need to dup the smp, vars API
3830 * already takes care of duplicating dynamic var data.
3831 */
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003832 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003833
3834 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3835 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3836 else
3837 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3838
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003839 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003840}
3841
3842__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3843{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003844 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003845 struct stream *s;
3846 const char *name;
3847 size_t len;
3848 struct sample smp;
3849
3850 MAY_LJMP(check_args(L, 2, "unset_var"));
3851
3852 /* It is useles to retrieve the stream, but this function
3853 * runs only in a stream context.
3854 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003855 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003856 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003857 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003858
3859 /* Unset the variable. */
3860 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003861 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3862 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003863}
3864
3865__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3866{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003867 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003868 struct stream *s;
3869 const char *name;
3870 size_t len;
3871 struct sample smp;
3872
3873 MAY_LJMP(check_args(L, 2, "get_var"));
3874
3875 /* It is useles to retrieve the stream, but this function
3876 * runs only in a stream context.
3877 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003878 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003879 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003880 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003881
3882 smp_set_owner(&smp, s->be, s->sess, s, 0);
3883 if (!vars_get_by_name(name, len, &smp)) {
3884 lua_pushnil(L);
3885 return 1;
3886 }
3887
3888 return hlua_smp2lua(L, &smp);
3889}
3890
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003891__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3892{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003893 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3894 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003895 struct hlua *hlua;
3896
3897 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003898 if (!s->hlua)
3899 return 0;
3900 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003901
3902 MAY_LJMP(check_args(L, 2, "set_priv"));
3903
3904 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003905 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003906
3907 /* Get and store new value. */
3908 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3909 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3910
3911 return 0;
3912}
3913
3914__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3915{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003916 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3917 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003918 struct hlua *hlua;
3919
3920 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003921 if (!s->hlua) {
3922 lua_pushnil(L);
3923 return 1;
3924 }
3925 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003926
3927 /* Push configuration index in the stack. */
3928 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3929
3930 return 1;
3931}
3932
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003933/* If expected data not yet available, it returns a yield. This function
3934 * consumes the data in the buffer. It returns a string containing the
3935 * data. This string can be empty.
3936 */
3937__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3938{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003939 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3940 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003941 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003942 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003943 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003944 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003945 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003946
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003947 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003948 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003949
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003950 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003951 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003952 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003953 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003954 }
3955
3956 /* End of data: commit the total strings and return. */
3957 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003958 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003959 return 1;
3960 }
3961
3962 /* Ensure that the block 2 length is usable. */
3963 if (ret == 1)
3964 len2 = 0;
3965
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003966 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003967 luaL_addlstring(&luactx->b, blk1, len1);
3968 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003969
3970 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003971 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003972 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003973 return 1;
3974}
3975
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003976/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003977__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3978{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003979 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003980
3981 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003982 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003983
3984 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3985}
3986
3987/* If expected data not yet available, it returns a yield. This function
3988 * consumes the data in the buffer. It returns a string containing the
3989 * data. This string can be empty.
3990 */
3991__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3992{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003993 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3994 struct stream_interface *si = luactx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003995 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003996 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003997 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003998 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003999 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004000 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004001
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004002 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004003 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004004
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004005 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004006 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004007 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004008 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004009 }
4010
4011 /* End of data: commit the total strings and return. */
4012 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004013 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004014 return 1;
4015 }
4016
4017 /* Ensure that the block 2 length is usable. */
4018 if (ret == 1)
4019 len2 = 0;
4020
4021 if (len == -1) {
4022
4023 /* If len == -1, catenate all the data avalaile and
4024 * yield because we want to get all the data until
4025 * the end of data stream.
4026 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004027 luaL_addlstring(&luactx->b, blk1, len1);
4028 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004029 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004030 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004031 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004032
4033 } else {
4034
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004035 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004036 if (len1 > len)
4037 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004038 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004039 len -= len1;
4040
4041 /* Copy the second block. */
4042 if (len2 > len)
4043 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004044 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004045 len -= len2;
4046
4047 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004048 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004049
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004050 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004051 if (len > 0) {
4052 lua_pushinteger(L, len);
4053 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004054 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004055 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004056 }
4057
4058 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004059 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004060 return 1;
4061 }
4062
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004063 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004064 hlua_pusherror(L, "Lua: internal error");
4065 WILL_LJMP(lua_error(L));
4066 return 0;
4067}
4068
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004069/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004070__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4071{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004072 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004073 int len = -1;
4074
4075 if (lua_gettop(L) > 2)
4076 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4077 if (lua_gettop(L) >= 2) {
4078 len = MAY_LJMP(luaL_checkinteger(L, 2));
4079 lua_pop(L, 1);
4080 }
4081
4082 /* Confirm or set the required length */
4083 lua_pushinteger(L, len);
4084
4085 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004086 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004087
4088 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4089}
4090
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004091/* Append data in the output side of the buffer. This data is immediately
4092 * sent. The function returns the amount of data written. If the buffer
4093 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004094 * if the channel is closed.
4095 */
4096__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4097{
4098 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004099 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004100 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4101 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004102 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004103 struct channel *chn = si_ic(si);
4104 int max;
4105
4106 /* Get the max amount of data which can write as input in the channel. */
4107 max = channel_recv_max(chn);
4108 if (max > (len - l))
4109 max = len - l;
4110
4111 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004112 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004113
4114 /* update counters. */
4115 l += max;
4116 lua_pop(L, 1);
4117 lua_pushinteger(L, l);
4118
4119 /* If some data is not send, declares the situation to the
4120 * applet, and returns a yield.
4121 */
4122 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004123 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004124 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004125 }
4126
4127 return 1;
4128}
4129
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004130/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004131 * yield the LUA process, and resume it without checking the
4132 * input arguments.
4133 */
4134__LJMP static int hlua_applet_tcp_send(lua_State *L)
4135{
4136 MAY_LJMP(check_args(L, 2, "send"));
4137 lua_pushinteger(L, 0);
4138
4139 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4140}
4141
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004142/*
4143 *
4144 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004145 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004146 *
4147 *
4148 */
4149
4150/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004151 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004152 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004153__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004154{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004155 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004156}
4157
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004158/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004159 * according with a current TXN.
4160 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004161static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004162{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004163 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004164 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004165 struct stream_interface *si = ctx->owner;
4166 struct stream *s = si_strm(si);
4167 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004168 struct htx *htx;
4169 struct htx_blk *blk;
4170 struct htx_sl *sl;
4171 struct ist path;
4172 unsigned long long len = 0;
4173 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004174
4175 /* Check stack size. */
4176 if (!lua_checkstack(L, 3))
4177 return 0;
4178
4179 /* Create the object: obj[0] = userdata.
4180 * Note that the base of the Converters object is the
4181 * same than the TXN object.
4182 */
4183 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004184 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004185 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004186 luactx->appctx = ctx;
4187 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4188 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4189 luactx->htxn.s = s;
4190 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004191
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004192 /* Create the "f" field that contains a list of fetches. */
4193 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004194 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004195 return 0;
4196 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004197
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004198 /* Create the "sf" field that contains a list of stringsafe fetches. */
4199 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004200 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004201 return 0;
4202 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004203
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004204 /* Create the "c" field that contains a list of converters. */
4205 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004206 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004207 return 0;
4208 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004209
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004210 /* Create the "sc" field that contains a list of stringsafe converters. */
4211 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004212 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004213 return 0;
4214 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004215
Christopher Fauleta2097962019-07-15 16:25:33 +02004216 htx = htxbuf(&s->req.buf);
4217 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004218 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004219 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004220
Christopher Fauleta2097962019-07-15 16:25:33 +02004221 /* Stores the request method. */
4222 lua_pushstring(L, "method");
4223 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4224 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004225
Christopher Fauleta2097962019-07-15 16:25:33 +02004226 /* Stores the http version. */
4227 lua_pushstring(L, "version");
4228 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4229 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004230
Christopher Fauleta2097962019-07-15 16:25:33 +02004231 /* creates an array of headers. hlua_http_get_headers() crates and push
4232 * the array on the top of the stack.
4233 */
4234 lua_pushstring(L, "headers");
4235 htxn.s = s;
4236 htxn.p = px;
4237 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004238 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004239 return 0;
4240 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004241
Christopher Fauleta2097962019-07-15 16:25:33 +02004242 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004243 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004244 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004245
Christopher Fauleta2097962019-07-15 16:25:33 +02004246 p = path.ptr;
4247 end = path.ptr + path.len;
4248 q = p;
4249 while (q < end && *q != '?')
4250 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004251
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004252 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004253 lua_pushstring(L, "path");
4254 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004255 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004256
Christopher Fauleta2097962019-07-15 16:25:33 +02004257 /* Stores the query string. */
4258 lua_pushstring(L, "qs");
4259 if (*q == '?')
4260 q++;
4261 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004262 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004263 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004264
Christopher Fauleta2097962019-07-15 16:25:33 +02004265 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4266 struct htx_blk *blk = htx_get_blk(htx, pos);
4267 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004268
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004269 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004270 break;
4271 if (type == HTX_BLK_DATA)
4272 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004273 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004274 if (htx->extra != ULLONG_MAX)
4275 len += htx->extra;
4276
4277 /* Stores the request path. */
4278 lua_pushstring(L, "length");
4279 lua_pushinteger(L, len);
4280 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004281
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004282 /* Create an empty array of HTTP request headers. */
4283 lua_pushstring(L, "response");
4284 lua_newtable(L);
4285 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004286
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004287 /* Pop a class stream metatable and affect it to the table. */
4288 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4289 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004290
4291 return 1;
4292}
4293
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004294__LJMP static int hlua_applet_http_set_var(lua_State *L)
4295{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004296 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004297 struct stream *s;
4298 const char *name;
4299 size_t len;
4300 struct sample smp;
4301
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004302 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4303 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004304
4305 /* It is useles to retrieve the stream, but this function
4306 * runs only in a stream context.
4307 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004308 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004309 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004310 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004311
4312 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004313 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004314 hlua_lua2smp(L, 3, &smp);
4315
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02004316 /* Store the sample in a variable. We don't need to dup the smp, vars API
4317 * already takes care of duplicating dynamic var data.
4318 */
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004319 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004320
4321 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4322 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4323 else
4324 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4325
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004326 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004327}
4328
4329__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4330{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004331 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004332 struct stream *s;
4333 const char *name;
4334 size_t len;
4335 struct sample smp;
4336
4337 MAY_LJMP(check_args(L, 2, "unset_var"));
4338
4339 /* It is useles to retrieve the stream, but this function
4340 * runs only in a stream context.
4341 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004342 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004343 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004344 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004345
4346 /* Unset the variable. */
4347 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004348 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4349 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004350}
4351
4352__LJMP static int hlua_applet_http_get_var(lua_State *L)
4353{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004354 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004355 struct stream *s;
4356 const char *name;
4357 size_t len;
4358 struct sample smp;
4359
4360 MAY_LJMP(check_args(L, 2, "get_var"));
4361
4362 /* It is useles to retrieve the stream, but this function
4363 * runs only in a stream context.
4364 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004365 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004366 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004367 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004368
4369 smp_set_owner(&smp, s->be, s->sess, s, 0);
4370 if (!vars_get_by_name(name, len, &smp)) {
4371 lua_pushnil(L);
4372 return 1;
4373 }
4374
4375 return hlua_smp2lua(L, &smp);
4376}
4377
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004378__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4379{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004380 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4381 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004382 struct hlua *hlua;
4383
4384 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004385 if (!s->hlua)
4386 return 0;
4387 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004388
4389 MAY_LJMP(check_args(L, 2, "set_priv"));
4390
4391 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004392 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004393
4394 /* Get and store new value. */
4395 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4396 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4397
4398 return 0;
4399}
4400
4401__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4402{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004403 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4404 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004405 struct hlua *hlua;
4406
4407 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004408 if (!s->hlua) {
4409 lua_pushnil(L);
4410 return 1;
4411 }
4412 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004413
4414 /* Push configuration index in the stack. */
4415 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4416
4417 return 1;
4418}
4419
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004420/* If expected data not yet available, it returns a yield. This function
4421 * consumes the data in the buffer. It returns a string containing the
4422 * data. This string can be empty.
4423 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004424__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004425{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004426 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4427 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004428 struct channel *req = si_oc(si);
4429 struct htx *htx;
4430 struct htx_blk *blk;
4431 size_t count;
4432 int stop = 0;
4433
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004434 htx = htx_from_buf(&req->buf);
4435 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004436 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004437
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004438 while (count && !stop && blk) {
4439 enum htx_blk_type type = htx_get_blk_type(blk);
4440 uint32_t sz = htx_get_blksz(blk);
4441 struct ist v;
4442 uint32_t vlen;
4443 char *nl;
4444
4445 vlen = sz;
4446 if (vlen > count) {
4447 if (type != HTX_BLK_DATA)
4448 break;
4449 vlen = count;
4450 }
4451
4452 switch (type) {
4453 case HTX_BLK_UNUSED:
4454 break;
4455
4456 case HTX_BLK_DATA:
4457 v = htx_get_blk_value(htx, blk);
4458 v.len = vlen;
4459 nl = istchr(v, '\n');
4460 if (nl != NULL) {
4461 stop = 1;
4462 vlen = nl - v.ptr + 1;
4463 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004464 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004465 break;
4466
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004467 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004468 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004469 stop = 1;
4470 break;
4471
4472 default:
4473 break;
4474 }
4475
4476 co_set_data(req, co_data(req) - vlen);
4477 count -= vlen;
4478 if (sz == vlen)
4479 blk = htx_remove_blk(htx, blk);
4480 else {
4481 htx_cut_data_blk(htx, blk, vlen);
4482 break;
4483 }
4484 }
4485
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004486 /* The message was fully consumed and no more data are expected
4487 * (EOM flag set).
4488 */
Christopher Fauleta5a25b12022-08-29 15:37:16 +02004489 if (htx_is_empty(htx) && (req->flags & CF_EOI))
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004490 stop = 1;
4491
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004492 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004493 if (!stop) {
4494 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004495 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004496 }
4497
4498 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004499 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004500 return 1;
4501}
4502
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004503
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004504/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004505__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004506{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004507 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004508
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004509 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004510 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004511
Christopher Fauleta2097962019-07-15 16:25:33 +02004512 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004513}
4514
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004515/* If expected data not yet available, it returns a yield. This function
4516 * consumes the data in the buffer. It returns a string containing the
4517 * data. This string can be empty.
4518 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004519__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004520{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004521 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4522 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004523 struct channel *req = si_oc(si);
4524 struct htx *htx;
4525 struct htx_blk *blk;
4526 size_t count;
4527 int len;
4528
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004529 htx = htx_from_buf(&req->buf);
4530 len = MAY_LJMP(luaL_checkinteger(L, 2));
4531 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004532 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004533 while (count && len && blk) {
4534 enum htx_blk_type type = htx_get_blk_type(blk);
4535 uint32_t sz = htx_get_blksz(blk);
4536 struct ist v;
4537 uint32_t vlen;
4538
4539 vlen = sz;
4540 if (len > 0 && vlen > len)
4541 vlen = len;
4542 if (vlen > count) {
4543 if (type != HTX_BLK_DATA)
4544 break;
4545 vlen = count;
4546 }
4547
4548 switch (type) {
4549 case HTX_BLK_UNUSED:
4550 break;
4551
4552 case HTX_BLK_DATA:
4553 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004554 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004555 break;
4556
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004557 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004558 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004559 len = 0;
4560 break;
4561
4562 default:
4563 break;
4564 }
4565
4566 co_set_data(req, co_data(req) - vlen);
4567 count -= vlen;
4568 if (len > 0)
4569 len -= vlen;
4570 if (sz == vlen)
4571 blk = htx_remove_blk(htx, blk);
4572 else {
4573 htx_cut_data_blk(htx, blk, vlen);
4574 break;
4575 }
4576 }
4577
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004578 /* The message was fully consumed and no more data are expected
4579 * (EOM flag set).
4580 */
Christopher Fauleta5a25b12022-08-29 15:37:16 +02004581 if (htx_is_empty(htx) && (req->flags & CF_EOI))
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004582 len = 0;
4583
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004584 htx_to_buf(htx, &req->buf);
4585
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004586 /* If we are no other data available, yield waiting for new data. */
4587 if (len) {
4588 if (len > 0) {
4589 lua_pushinteger(L, len);
4590 lua_replace(L, 2);
4591 }
4592 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004593 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004594 }
4595
4596 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004597 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004598 return 1;
4599}
4600
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004601/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004602__LJMP static int hlua_applet_http_recv(lua_State *L)
4603{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004604 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004605 int len = -1;
4606
4607 /* Check arguments. */
4608 if (lua_gettop(L) > 2)
4609 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4610 if (lua_gettop(L) >= 2) {
4611 len = MAY_LJMP(luaL_checkinteger(L, 2));
4612 lua_pop(L, 1);
4613 }
4614
Christopher Fauleta2097962019-07-15 16:25:33 +02004615 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004616
Christopher Fauleta2097962019-07-15 16:25:33 +02004617 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004618 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004619
Christopher Fauleta2097962019-07-15 16:25:33 +02004620 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004621}
4622
4623/* Append data in the output side of the buffer. This data is immediately
4624 * sent. The function returns the amount of data written. If the buffer
4625 * cannot contain the data, the function yields. The function returns -1
4626 * if the channel is closed.
4627 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004628__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004629{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004630 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4631 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004632 struct channel *res = si_ic(si);
4633 struct htx *htx = htx_from_buf(&res->buf);
4634 const char *data;
4635 size_t len;
4636 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4637 int max;
4638
Christopher Faulet9060fc02019-07-03 11:39:30 +02004639 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004640 if (!max)
4641 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004642
4643 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4644
4645 /* Get the max amount of data which can write as input in the channel. */
4646 if (max > (len - l))
4647 max = len - l;
4648
4649 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004650 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004651 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004652
4653 /* update counters. */
4654 l += max;
4655 lua_pop(L, 1);
4656 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004657
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004658 /* If some data is not send, declares the situation to the
4659 * applet, and returns a yield.
4660 */
4661 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004662 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004663 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004664 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004665 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004666 }
4667
Christopher Fauleta2097962019-07-15 16:25:33 +02004668 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004669 return 1;
4670}
4671
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004672/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004673 * yield the LUA process, and resume it without checking the
4674 * input arguments.
4675 */
4676__LJMP static int hlua_applet_http_send(lua_State *L)
4677{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004678 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004679
4680 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004681 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004682 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4683 WILL_LJMP(lua_error(L));
4684 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004685
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004686 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004687 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004688
Christopher Fauleta2097962019-07-15 16:25:33 +02004689 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004690}
4691
4692__LJMP static int hlua_applet_http_addheader(lua_State *L)
4693{
4694 const char *name;
4695 int ret;
4696
4697 MAY_LJMP(hlua_checkapplet_http(L, 1));
4698 name = MAY_LJMP(luaL_checkstring(L, 2));
4699 MAY_LJMP(luaL_checkstring(L, 3));
4700
4701 /* Push in the stack the "response" entry. */
4702 ret = lua_getfield(L, 1, "response");
4703 if (ret != LUA_TTABLE) {
4704 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4705 "is expected as an array. %s found", lua_typename(L, ret));
4706 WILL_LJMP(lua_error(L));
4707 }
4708
4709 /* check if the header is already registered if it is not
4710 * the case, register it.
4711 */
4712 ret = lua_getfield(L, -1, name);
4713 if (ret == LUA_TNIL) {
4714
4715 /* Entry not found. */
4716 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4717
4718 /* Insert the new header name in the array in the top of the stack.
4719 * It left the new array in the top of the stack.
4720 */
4721 lua_newtable(L);
4722 lua_pushvalue(L, 2);
4723 lua_pushvalue(L, -2);
4724 lua_settable(L, -4);
4725
4726 } else if (ret != LUA_TTABLE) {
4727
4728 /* corruption error. */
4729 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4730 "is expected as an array. %s found", name, lua_typename(L, ret));
4731 WILL_LJMP(lua_error(L));
4732 }
4733
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004734 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004735 * the header value as new entry.
4736 */
4737 lua_pushvalue(L, 3);
4738 ret = lua_rawlen(L, -2);
4739 lua_rawseti(L, -2, ret + 1);
4740 lua_pushboolean(L, 1);
4741 return 1;
4742}
4743
4744__LJMP static int hlua_applet_http_status(lua_State *L)
4745{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004746 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004747 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004748 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004749
4750 if (status < 100 || status > 599) {
4751 lua_pushboolean(L, 0);
4752 return 1;
4753 }
4754
Willy Tarreau7e702d12021-04-28 17:59:21 +02004755 luactx->appctx->ctx.hlua_apphttp.status = status;
4756 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004757 lua_pushboolean(L, 1);
4758 return 1;
4759}
4760
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004761
Christopher Fauleta2097962019-07-15 16:25:33 +02004762__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004763{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004764 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4765 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004766 struct channel *res = si_ic(si);
4767 struct htx *htx;
4768 struct htx_sl *sl;
4769 struct h1m h1m;
4770 const char *status, *reason;
4771 const char *name, *value;
4772 size_t nlen, vlen;
4773 unsigned int flags;
4774
4775 /* Send the message at once. */
4776 htx = htx_from_buf(&res->buf);
4777 h1m_init_res(&h1m);
4778
4779 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004780 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4781 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004782 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02004783 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
4784 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004785 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4786 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4787 }
4788 else {
4789 flags = HTX_SL_F_IS_RESP;
4790 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4791 }
4792 if (!sl) {
4793 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004794 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004795 WILL_LJMP(lua_error(L));
4796 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004797 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004798
4799 /* Get the array associated to the field "response" in the object AppletHTTP. */
4800 lua_pushvalue(L, 0);
4801 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4802 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004803 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004804 WILL_LJMP(lua_error(L));
4805 }
4806
4807 /* Browse the list of headers. */
4808 lua_pushnil(L);
4809 while(lua_next(L, -2) != 0) {
4810 /* We expect a string as -2. */
4811 if (lua_type(L, -2) != LUA_TSTRING) {
4812 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004813 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004814 lua_typename(L, lua_type(L, -2)));
4815 WILL_LJMP(lua_error(L));
4816 }
4817 name = lua_tolstring(L, -2, &nlen);
4818
4819 /* We expect an array as -1. */
4820 if (lua_type(L, -1) != LUA_TTABLE) {
4821 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 +02004822 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004823 name,
4824 lua_typename(L, lua_type(L, -1)));
4825 WILL_LJMP(lua_error(L));
4826 }
4827
4828 /* Browse the table who is on the top of the stack. */
4829 lua_pushnil(L);
4830 while(lua_next(L, -2) != 0) {
4831 int id;
4832
4833 /* We expect a number as -2. */
4834 if (lua_type(L, -2) != LUA_TNUMBER) {
4835 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 +02004836 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004837 name,
4838 lua_typename(L, lua_type(L, -2)));
4839 WILL_LJMP(lua_error(L));
4840 }
4841 id = lua_tointeger(L, -2);
4842
4843 /* We expect a string as -2. */
4844 if (lua_type(L, -1) != LUA_TSTRING) {
4845 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 +02004846 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004847 name, id,
4848 lua_typename(L, lua_type(L, -1)));
4849 WILL_LJMP(lua_error(L));
4850 }
4851 value = lua_tolstring(L, -1, &vlen);
4852
4853 /* Simple Protocol checks. */
4854 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004855 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004856 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4857 struct ist v = ist2(value, vlen);
4858 int ret;
4859
4860 ret = h1_parse_cont_len_header(&h1m, &v);
4861 if (ret < 0) {
4862 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004863 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004864 name);
4865 WILL_LJMP(lua_error(L));
4866 }
4867 else if (ret == 0)
4868 goto next; /* Skip it */
4869 }
4870
4871 /* Add a new header */
4872 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4873 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004874 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004875 name);
4876 WILL_LJMP(lua_error(L));
4877 }
4878 next:
4879 /* Remove the array from the stack, and get next element with a remaining string. */
4880 lua_pop(L, 1);
4881 }
4882
4883 /* Remove the array from the stack, and get next element with a remaining string. */
4884 lua_pop(L, 1);
4885 }
4886
4887 if (h1m.flags & H1_MF_CHNK)
4888 h1m.flags &= ~H1_MF_CLEN;
4889 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4890 h1m.flags |= H1_MF_XFER_LEN;
4891
4892 /* Uset HTX start-line flags */
4893 if (h1m.flags & H1_MF_XFER_ENC)
4894 flags |= HTX_SL_F_XFER_ENC;
4895 if (h1m.flags & H1_MF_XFER_LEN) {
4896 flags |= HTX_SL_F_XFER_LEN;
4897 if (h1m.flags & H1_MF_CHNK)
4898 flags |= HTX_SL_F_CHNK;
4899 else if (h1m.flags & H1_MF_CLEN)
4900 flags |= HTX_SL_F_CLEN;
4901 if (h1m.body_len == 0)
4902 flags |= HTX_SL_F_BODYLESS;
4903 }
4904 sl->flags |= flags;
4905
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004906 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004907 * and the status code implies the presence of a message body, we must
4908 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004909 * for the keepalive compliance. If the applet announces a transfer-encoding
4910 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004911 */
4912 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02004913 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
4914 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
4915 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004916 /* Add a new header */
4917 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4918 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4919 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004920 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004921 WILL_LJMP(lua_error(L));
4922 }
4923 }
4924
4925 /* Finalize headers. */
4926 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4927 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004928 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004929 WILL_LJMP(lua_error(L));
4930 }
4931
4932 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4933 b_reset(&res->buf);
4934 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4935 WILL_LJMP(lua_error(L));
4936 }
4937
4938 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004939 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004940
4941 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004942 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004943 return 0;
4944
4945}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004946/* We will build the status line and the headers of the HTTP response.
4947 * We will try send at once if its not possible, we give back the hand
4948 * waiting for more room.
4949 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004950__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004951{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004952 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4953 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004954 struct channel *res = si_ic(si);
4955
4956 if (co_data(res)) {
4957 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004958 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004959 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004960 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004961}
4962
4963
Christopher Fauleta2097962019-07-15 16:25:33 +02004964__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004965{
Christopher Fauleta2097962019-07-15 16:25:33 +02004966 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004967}
4968
Christopher Fauleta2097962019-07-15 16:25:33 +02004969/*
4970 *
4971 *
4972 * Class HTTP
4973 *
4974 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004975 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004976
4977/* Returns a struct hlua_txn if the stack entry "ud" is
4978 * a class stream, otherwise it throws an error.
4979 */
4980__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004981{
Christopher Fauleta2097962019-07-15 16:25:33 +02004982 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4983}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004984
Christopher Fauleta2097962019-07-15 16:25:33 +02004985/* This function creates and push in the stack a HTTP object
4986 * according with a current TXN.
4987 */
4988static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4989{
4990 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004991
Christopher Fauleta2097962019-07-15 16:25:33 +02004992 /* Check stack size. */
4993 if (!lua_checkstack(L, 3))
4994 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004995
Christopher Fauleta2097962019-07-15 16:25:33 +02004996 /* Create the object: obj[0] = userdata.
4997 * Note that the base of the Converters object is the
4998 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004999 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005000 lua_newtable(L);
5001 htxn = lua_newuserdata(L, sizeof(*htxn));
5002 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005003
5004 htxn->s = txn->s;
5005 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005006 htxn->dir = txn->dir;
5007 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005008
5009 /* Pop a class stream metatable and affect it to the table. */
5010 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5011 lua_setmetatable(L, -2);
5012
5013 return 1;
5014}
5015
5016/* This function creates ans returns an array of HTTP headers.
5017 * This function does not fails. It is used as wrapper with the
5018 * 2 following functions.
5019 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005020__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005021{
Christopher Fauleta2097962019-07-15 16:25:33 +02005022 struct htx *htx;
5023 int32_t pos;
5024
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005025 /* Create the table. */
5026 lua_newtable(L);
5027
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005028
Christopher Fauleta2097962019-07-15 16:25:33 +02005029 htx = htxbuf(&msg->chn->buf);
5030 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5031 struct htx_blk *blk = htx_get_blk(htx, pos);
5032 enum htx_blk_type type = htx_get_blk_type(blk);
5033 struct ist n, v;
5034 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005035
Christopher Fauleta2097962019-07-15 16:25:33 +02005036 if (type == HTX_BLK_HDR) {
5037 n = htx_get_blk_name(htx,blk);
5038 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005039 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005040 else if (type == HTX_BLK_EOH)
5041 break;
5042 else
5043 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005044
Christopher Fauleta2097962019-07-15 16:25:33 +02005045 /* Check for existing entry:
5046 * assume that the table is on the top of the stack, and
5047 * push the key in the stack, the function lua_gettable()
5048 * perform the lookup.
5049 */
5050 lua_pushlstring(L, n.ptr, n.len);
5051 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005052
Christopher Fauleta2097962019-07-15 16:25:33 +02005053 switch (lua_type(L, -1)) {
5054 case LUA_TNIL:
5055 /* Table not found, create it. */
5056 lua_pop(L, 1); /* remove the nil value. */
5057 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5058 lua_newtable(L); /* create and push empty table. */
5059 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5060 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5061 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005062 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005063
Christopher Fauleta2097962019-07-15 16:25:33 +02005064 case LUA_TTABLE:
5065 /* Entry found: push the value in the table. */
5066 len = lua_rawlen(L, -1);
5067 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5068 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5069 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5070 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005071
Christopher Fauleta2097962019-07-15 16:25:33 +02005072 default:
5073 /* Other cases are errors. */
5074 hlua_pusherror(L, "internal error during the parsing of headers.");
5075 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005076 }
5077 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005078 return 1;
5079}
5080
5081__LJMP static int hlua_http_req_get_headers(lua_State *L)
5082{
5083 struct hlua_txn *htxn;
5084
5085 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5086 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5087
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005088 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005089 WILL_LJMP(lua_error(L));
5090
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005091 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005092}
5093
5094__LJMP static int hlua_http_res_get_headers(lua_State *L)
5095{
5096 struct hlua_txn *htxn;
5097
5098 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5099 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5100
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005101 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005102 WILL_LJMP(lua_error(L));
5103
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005104 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005105}
5106
5107/* This function replace full header, or just a value in
5108 * the request or in the response. It is a wrapper fir the
5109 * 4 following functions.
5110 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005111__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005112{
5113 size_t name_len;
5114 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5115 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5116 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005117 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005118 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005119
Dragan Dosen26743032019-04-30 15:54:36 +02005120 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005121 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5122
Christopher Fauleta2097962019-07-15 16:25:33 +02005123 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005124 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005125 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005126 return 0;
5127}
5128
5129__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5130{
5131 struct hlua_txn *htxn;
5132
5133 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5134 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5135
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005136 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005137 WILL_LJMP(lua_error(L));
5138
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005139 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005140}
5141
5142__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5143{
5144 struct hlua_txn *htxn;
5145
5146 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5147 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5148
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005149 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005150 WILL_LJMP(lua_error(L));
5151
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005152 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005153}
5154
5155__LJMP static int hlua_http_req_rep_val(lua_State *L)
5156{
5157 struct hlua_txn *htxn;
5158
5159 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5160 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5161
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005162 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005163 WILL_LJMP(lua_error(L));
5164
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005165 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005166}
5167
5168__LJMP static int hlua_http_res_rep_val(lua_State *L)
5169{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005170 struct hlua_txn *htxn;
5171
5172 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5173 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5174
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005175 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005176 WILL_LJMP(lua_error(L));
5177
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005178 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005179}
5180
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005181/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005182 * It is a wrapper for the 2 following functions.
5183 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005184__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005185{
5186 size_t len;
5187 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005188 struct htx *htx = htxbuf(&msg->chn->buf);
5189 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005190
Christopher Fauleta2097962019-07-15 16:25:33 +02005191 ctx.blk = NULL;
5192 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5193 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005194 return 0;
5195}
5196
5197__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5198{
5199 struct hlua_txn *htxn;
5200
5201 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5202 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5203
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005204 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005205 WILL_LJMP(lua_error(L));
5206
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005207 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005208}
5209
5210__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5211{
5212 struct hlua_txn *htxn;
5213
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005214 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005215 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5216
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005217 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005218 WILL_LJMP(lua_error(L));
5219
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005220 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005221}
5222
5223/* This function adds an header. It is a wrapper used by
5224 * the 2 following functions.
5225 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005226__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005227{
5228 size_t name_len;
5229 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5230 size_t value_len;
5231 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005232 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005233
Christopher Fauleta2097962019-07-15 16:25:33 +02005234 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5235 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005236 return 0;
5237}
5238
5239__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5240{
5241 struct hlua_txn *htxn;
5242
5243 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5244 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5245
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005246 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005247 WILL_LJMP(lua_error(L));
5248
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005249 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005250}
5251
5252__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5253{
5254 struct hlua_txn *htxn;
5255
5256 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5257 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5258
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005259 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005260 WILL_LJMP(lua_error(L));
5261
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005262 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005263}
5264
5265static int hlua_http_req_set_hdr(lua_State *L)
5266{
5267 struct hlua_txn *htxn;
5268
5269 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5270 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5271
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005272 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005273 WILL_LJMP(lua_error(L));
5274
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005275 hlua_http_del_hdr(L, &htxn->s->txn->req);
5276 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005277}
5278
5279static int hlua_http_res_set_hdr(lua_State *L)
5280{
5281 struct hlua_txn *htxn;
5282
5283 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5284 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5285
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005286 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005287 WILL_LJMP(lua_error(L));
5288
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005289 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5290 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005291}
5292
5293/* This function set the method. */
5294static int hlua_http_req_set_meth(lua_State *L)
5295{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005296 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005297 size_t name_len;
5298 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005299
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005300 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005301 WILL_LJMP(lua_error(L));
5302
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005303 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005304 return 1;
5305}
5306
5307/* This function set the method. */
5308static int hlua_http_req_set_path(lua_State *L)
5309{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005310 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005311 size_t name_len;
5312 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005313
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005314 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005315 WILL_LJMP(lua_error(L));
5316
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005317 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005318 return 1;
5319}
5320
5321/* This function set the query-string. */
5322static int hlua_http_req_set_query(lua_State *L)
5323{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005324 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005325 size_t name_len;
5326 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005327
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005328 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005329 WILL_LJMP(lua_error(L));
5330
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005331 /* Check length. */
5332 if (name_len > trash.size - 1) {
5333 lua_pushboolean(L, 0);
5334 return 1;
5335 }
5336
5337 /* Add the mark question as prefix. */
5338 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005339 trash.area[trash.data++] = '?';
5340 memcpy(trash.area + trash.data, name, name_len);
5341 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005342
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005343 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005344 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005345 return 1;
5346}
5347
5348/* This function set the uri. */
5349static int hlua_http_req_set_uri(lua_State *L)
5350{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005351 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005352 size_t name_len;
5353 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005354
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005355 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005356 WILL_LJMP(lua_error(L));
5357
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005358 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005359 return 1;
5360}
5361
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005362/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005363static int hlua_http_res_set_status(lua_State *L)
5364{
5365 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5366 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005367 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5368 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005369
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005370 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005371 WILL_LJMP(lua_error(L));
5372
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005373 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005374 return 0;
5375}
5376
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005377/*
5378 *
5379 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005380 * Class TXN
5381 *
5382 *
5383 */
5384
5385/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005386 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005387 */
5388__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5389{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005390 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005391}
5392
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005393__LJMP static int hlua_set_var(lua_State *L)
5394{
5395 struct hlua_txn *htxn;
5396 const char *name;
5397 size_t len;
5398 struct sample smp;
5399
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005400 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5401 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005402
5403 /* It is useles to retrieve the stream, but this function
5404 * runs only in a stream context.
5405 */
5406 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5407 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5408
5409 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005410 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005411 hlua_lua2smp(L, 3, &smp);
5412
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02005413 /* Store the sample in a variable. We don't need to dup the smp, vars API
5414 * already takes care of duplicating dynamic var data.
5415 */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005416 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005417
5418 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5419 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5420 else
5421 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5422
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005423 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005424}
5425
Christopher Faulet85d79c92016-11-09 16:54:56 +01005426__LJMP static int hlua_unset_var(lua_State *L)
5427{
5428 struct hlua_txn *htxn;
5429 const char *name;
5430 size_t len;
5431 struct sample smp;
5432
5433 MAY_LJMP(check_args(L, 2, "unset_var"));
5434
5435 /* It is useles to retrieve the stream, but this function
5436 * runs only in a stream context.
5437 */
5438 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5439 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5440
5441 /* Unset the variable. */
5442 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005443 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5444 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005445}
5446
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005447__LJMP static int hlua_get_var(lua_State *L)
5448{
5449 struct hlua_txn *htxn;
5450 const char *name;
5451 size_t len;
5452 struct sample smp;
5453
5454 MAY_LJMP(check_args(L, 2, "get_var"));
5455
5456 /* It is useles to retrieve the stream, but this function
5457 * runs only in a stream context.
5458 */
5459 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5460 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5461
Willy Tarreau7560dd42016-03-10 16:28:58 +01005462 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005463 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005464 lua_pushnil(L);
5465 return 1;
5466 }
5467
5468 return hlua_smp2lua(L, &smp);
5469}
5470
Willy Tarreau59551662015-03-10 14:23:13 +01005471__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005472{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005473 struct hlua *hlua;
5474
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005475 MAY_LJMP(check_args(L, 2, "set_priv"));
5476
Willy Tarreau87b09662015-04-03 00:22:06 +02005477 /* It is useles to retrieve the stream, but this function
5478 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005479 */
5480 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005481
5482 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005483 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005484 if (!hlua)
5485 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005486
5487 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005488 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005489
5490 /* Get and store new value. */
5491 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5492 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5493
5494 return 0;
5495}
5496
Willy Tarreau59551662015-03-10 14:23:13 +01005497__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005498{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005499 struct hlua *hlua;
5500
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005501 MAY_LJMP(check_args(L, 1, "get_priv"));
5502
Willy Tarreau87b09662015-04-03 00:22:06 +02005503 /* It is useles to retrieve the stream, but this function
5504 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005505 */
5506 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005507
5508 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005509 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005510 if (!hlua) {
5511 lua_pushnil(L);
5512 return 1;
5513 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005514
5515 /* Push configuration index in the stack. */
5516 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5517
5518 return 1;
5519}
5520
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005521/* Create stack entry containing a class TXN. This function
5522 * return 0 if the stack does not contains free slots,
5523 * otherwise it returns 1.
5524 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005525static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005526{
Willy Tarreaude491382015-04-06 11:04:28 +02005527 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005528
5529 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005530 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005531 return 0;
5532
5533 /* NOTE: The allocation never fails. The failure
5534 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005535 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005536 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005537 /* Create the object: obj[0] = userdata. */
5538 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005539 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005540 lua_rawseti(L, -2, 0);
5541
Willy Tarreaude491382015-04-06 11:04:28 +02005542 htxn->s = s;
5543 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005544 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005545 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005546
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005547 /* Create the "f" field that contains a list of fetches. */
5548 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005549 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005550 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005551 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005552
5553 /* Create the "sf" field that contains a list of stringsafe fetches. */
5554 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005555 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005556 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005557 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005558
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005559 /* Create the "c" field that contains a list of converters. */
5560 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005561 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005562 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005563 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005564
5565 /* Create the "sc" field that contains a list of stringsafe converters. */
5566 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005567 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005568 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005569 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005570
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005571 /* Create the "req" field that contains the request channel object. */
5572 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005573 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005574 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005575 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005576
5577 /* Create the "res" field that contains the response channel object. */
5578 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005579 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005580 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005581 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005582
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005583 /* Creates the HTTP object is the current proxy allows http. */
5584 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005585 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005586 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005587 return 0;
5588 }
5589 else
5590 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005591 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005592
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005593 /* Pop a class sesison metatable and affect it to the userdata. */
5594 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5595 lua_setmetatable(L, -2);
5596
5597 return 1;
5598}
5599
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005600__LJMP static int hlua_txn_deflog(lua_State *L)
5601{
5602 const char *msg;
5603 struct hlua_txn *htxn;
5604
5605 MAY_LJMP(check_args(L, 2, "deflog"));
5606 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5607 msg = MAY_LJMP(luaL_checkstring(L, 2));
5608
5609 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5610 return 0;
5611}
5612
5613__LJMP static int hlua_txn_log(lua_State *L)
5614{
5615 int level;
5616 const char *msg;
5617 struct hlua_txn *htxn;
5618
5619 MAY_LJMP(check_args(L, 3, "log"));
5620 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5621 level = MAY_LJMP(luaL_checkinteger(L, 2));
5622 msg = MAY_LJMP(luaL_checkstring(L, 3));
5623
5624 if (level < 0 || level >= NB_LOG_LEVELS)
5625 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5626
5627 hlua_sendlog(htxn->s->be, level, msg);
5628 return 0;
5629}
5630
5631__LJMP static int hlua_txn_log_debug(lua_State *L)
5632{
5633 const char *msg;
5634 struct hlua_txn *htxn;
5635
5636 MAY_LJMP(check_args(L, 2, "Debug"));
5637 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5638 msg = MAY_LJMP(luaL_checkstring(L, 2));
5639 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5640 return 0;
5641}
5642
5643__LJMP static int hlua_txn_log_info(lua_State *L)
5644{
5645 const char *msg;
5646 struct hlua_txn *htxn;
5647
5648 MAY_LJMP(check_args(L, 2, "Info"));
5649 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5650 msg = MAY_LJMP(luaL_checkstring(L, 2));
5651 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5652 return 0;
5653}
5654
5655__LJMP static int hlua_txn_log_warning(lua_State *L)
5656{
5657 const char *msg;
5658 struct hlua_txn *htxn;
5659
5660 MAY_LJMP(check_args(L, 2, "Warning"));
5661 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5662 msg = MAY_LJMP(luaL_checkstring(L, 2));
5663 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5664 return 0;
5665}
5666
5667__LJMP static int hlua_txn_log_alert(lua_State *L)
5668{
5669 const char *msg;
5670 struct hlua_txn *htxn;
5671
5672 MAY_LJMP(check_args(L, 2, "Alert"));
5673 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5674 msg = MAY_LJMP(luaL_checkstring(L, 2));
5675 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5676 return 0;
5677}
5678
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005679__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5680{
5681 struct hlua_txn *htxn;
5682 int ll;
5683
5684 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5685 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5686 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5687
5688 if (ll < 0 || ll > 7)
5689 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5690
5691 htxn->s->logs.level = ll;
5692 return 0;
5693}
5694
5695__LJMP static int hlua_txn_set_tos(lua_State *L)
5696{
5697 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005698 int tos;
5699
5700 MAY_LJMP(check_args(L, 2, "set_tos"));
5701 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5702 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5703
Willy Tarreau1a18b542018-12-11 16:37:42 +01005704 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005705 return 0;
5706}
5707
5708__LJMP static int hlua_txn_set_mark(lua_State *L)
5709{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005710 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005711 int mark;
5712
5713 MAY_LJMP(check_args(L, 2, "set_mark"));
5714 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5715 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5716
Lukas Tribus579e3e32019-08-11 18:03:45 +02005717 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005718 return 0;
5719}
5720
Patrick Hemmer268a7072018-05-11 12:52:31 -04005721__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5722{
5723 struct hlua_txn *htxn;
5724
5725 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5726 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5727 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5728 return 0;
5729}
5730
5731__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5732{
5733 struct hlua_txn *htxn;
5734
5735 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5736 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5737 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5738 return 0;
5739}
5740
Christopher Faulet700d9e82020-01-31 12:21:52 +01005741/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005742 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005743 * message and terminate the transaction. It returns 1 on success and 0 on
5744 * error. The Reply must be on top of the stack.
5745 */
5746__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5747{
5748 struct htx *htx;
5749 struct htx_sl *sl;
5750 struct h1m h1m;
5751 const char *status, *reason, *body;
5752 size_t status_len, reason_len, body_len;
5753 int ret, code, flags;
5754
5755 code = 200;
5756 status = "200";
5757 status_len = 3;
5758 ret = lua_getfield(L, -1, "status");
5759 if (ret == LUA_TNUMBER) {
5760 code = lua_tointeger(L, -1);
5761 status = lua_tolstring(L, -1, &status_len);
5762 }
5763 lua_pop(L, 1);
5764
5765 reason = http_get_reason(code);
5766 reason_len = strlen(reason);
5767 ret = lua_getfield(L, -1, "reason");
5768 if (ret == LUA_TSTRING)
5769 reason = lua_tolstring(L, -1, &reason_len);
5770 lua_pop(L, 1);
5771
5772 body = NULL;
5773 body_len = 0;
5774 ret = lua_getfield(L, -1, "body");
5775 if (ret == LUA_TSTRING)
5776 body = lua_tolstring(L, -1, &body_len);
5777 lua_pop(L, 1);
5778
5779 /* Prepare the response before inserting the headers */
5780 h1m_init_res(&h1m);
5781 htx = htx_from_buf(&s->res.buf);
5782 channel_htx_truncate(&s->res, htx);
5783 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5784 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5785 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5786 ist2(status, status_len), ist2(reason, reason_len));
5787 }
5788 else {
5789 flags = HTX_SL_F_IS_RESP;
5790 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5791 ist2(status, status_len), ist2(reason, reason_len));
5792 }
5793 if (!sl)
5794 goto fail;
5795 sl->info.res.status = code;
5796
5797 /* Push in the stack the "headers" entry. */
5798 ret = lua_getfield(L, -1, "headers");
5799 if (ret != LUA_TTABLE)
5800 goto skip_headers;
5801
5802 lua_pushnil(L);
5803 while (lua_next(L, -2) != 0) {
5804 struct ist name, value;
5805 const char *n, *v;
5806 size_t nlen, vlen;
5807
5808 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5809 /* Skip element if the key is not a string or if the value is not a table */
5810 goto next_hdr;
5811 }
5812
5813 n = lua_tolstring(L, -2, &nlen);
5814 name = ist2(n, nlen);
5815 if (isteqi(name, ist("content-length"))) {
5816 /* Always skip content-length header. It will be added
5817 * later with the correct len
5818 */
5819 goto next_hdr;
5820 }
5821
5822 /* Loop on header's values */
5823 lua_pushnil(L);
5824 while (lua_next(L, -2)) {
5825 if (!lua_isstring(L, -1)) {
5826 /* Skip the value if it is not a string */
5827 goto next_value;
5828 }
5829
5830 v = lua_tolstring(L, -1, &vlen);
5831 value = ist2(v, vlen);
5832
5833 if (isteqi(name, ist("transfer-encoding")))
5834 h1_parse_xfer_enc_header(&h1m, value);
5835 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5836 goto fail;
5837
5838 next_value:
5839 lua_pop(L, 1);
5840 }
5841
5842 next_hdr:
5843 lua_pop(L, 1);
5844 }
5845 skip_headers:
5846 lua_pop(L, 1);
5847
5848 /* Update h1m flags: CLEN is set if CHNK is not present */
5849 if (!(h1m.flags & H1_MF_CHNK)) {
5850 const char *clen = ultoa(body_len);
5851
5852 h1m.flags |= H1_MF_CLEN;
5853 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5854 goto fail;
5855 }
5856 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5857 h1m.flags |= H1_MF_XFER_LEN;
5858
5859 /* Update HTX start-line flags */
5860 if (h1m.flags & H1_MF_XFER_ENC)
5861 flags |= HTX_SL_F_XFER_ENC;
5862 if (h1m.flags & H1_MF_XFER_LEN) {
5863 flags |= HTX_SL_F_XFER_LEN;
5864 if (h1m.flags & H1_MF_CHNK)
5865 flags |= HTX_SL_F_CHNK;
5866 else if (h1m.flags & H1_MF_CLEN)
5867 flags |= HTX_SL_F_CLEN;
5868 if (h1m.body_len == 0)
5869 flags |= HTX_SL_F_BODYLESS;
5870 }
5871 sl->flags |= flags;
5872
5873
5874 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005875 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005876 goto fail;
5877
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005878 htx->flags |= HTX_FL_EOM;
5879
Christopher Faulet700d9e82020-01-31 12:21:52 +01005880 /* Now, forward the response and terminate the transaction */
5881 s->txn->status = code;
5882 htx_to_buf(htx, &s->res.buf);
5883 if (!http_forward_proxy_resp(s, 1))
5884 goto fail;
5885
5886 return 1;
5887
5888 fail:
5889 channel_htx_truncate(&s->res, htx);
5890 return 0;
5891}
5892
5893/* Terminate a transaction if called from a lua action. For TCP streams,
5894 * processing is just aborted. Nothing is returned to the client and all
5895 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5896 * is forwarded to the client before terminating the transaction. On success,
5897 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5898 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5899 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005900 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005901__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005902{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005903 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005904 struct stream *s;
5905 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005906
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005907 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005908
Christopher Faulet700d9e82020-01-31 12:21:52 +01005909 /* If the flags NOTERM is set, we cannot terminate the session, so we
5910 * just end the execution of the current lua code. */
5911 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005912 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005913
Christopher Faulet700d9e82020-01-31 12:21:52 +01005914 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005915 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005916 struct channel *req = &s->req;
5917 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005918
Christopher Faulet700d9e82020-01-31 12:21:52 +01005919 channel_auto_read(req);
5920 channel_abort(req);
5921 channel_auto_close(req);
5922 channel_erase(req);
5923
5924 res->wex = tick_add_ifset(now_ms, res->wto);
5925 channel_auto_read(res);
5926 channel_auto_close(res);
5927 channel_shutr_now(res);
5928
5929 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5930 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005931 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005932
Christopher Faulet700d9e82020-01-31 12:21:52 +01005933 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5934 /* No reply or invalid reply */
5935 s->txn->status = 0;
5936 http_reply_and_close(s, 0, NULL);
5937 }
5938 else {
5939 /* Remove extra args to have the reply on top of the stack */
5940 if (lua_gettop(L) > 2)
5941 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005942
Christopher Faulet700d9e82020-01-31 12:21:52 +01005943 if (!hlua_txn_forward_reply(L, s)) {
5944 if (!(s->flags & SF_ERR_MASK))
5945 s->flags |= SF_ERR_PRXCOND;
5946 lua_pushinteger(L, ACT_RET_ERR);
5947 WILL_LJMP(hlua_done(L));
5948 return 0; /* Never reached */
5949 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005950 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005951
Christopher Faulet700d9e82020-01-31 12:21:52 +01005952 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5953 if (htxn->dir == SMP_OPT_DIR_REQ) {
5954 /* let's log the request time */
5955 s->logs.tv_request = now;
5956 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02005957 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01005958 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005959
Christopher Faulet700d9e82020-01-31 12:21:52 +01005960 done:
5961 if (!(s->flags & SF_ERR_MASK))
5962 s->flags |= SF_ERR_LOCAL;
5963 if (!(s->flags & SF_FINST_MASK))
5964 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005965
Christopher Faulet4ad73102020-03-05 11:07:31 +01005966 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005967 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005968 return 0;
5969}
5970
Christopher Faulet700d9e82020-01-31 12:21:52 +01005971/*
5972 *
5973 *
5974 * Class REPLY
5975 *
5976 *
5977 */
5978
5979/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5980 * free slots, the function fails and returns 0;
5981 */
5982static int hlua_txn_reply_new(lua_State *L)
5983{
5984 struct hlua_txn *htxn;
5985 const char *reason, *body = NULL;
5986 int ret, status;
5987
5988 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005989 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005990 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5991 WILL_LJMP(lua_error(L));
5992 }
5993
5994 /* Default value */
5995 status = 200;
5996 reason = http_get_reason(status);
5997
5998 if (lua_istable(L, 2)) {
5999 /* load status and reason from the table argument at index 2 */
6000 ret = lua_getfield(L, 2, "status");
6001 if (ret == LUA_TNIL)
6002 goto reason;
6003 else if (ret != LUA_TNUMBER) {
6004 /* invalid status: ignore the reason */
6005 goto body;
6006 }
6007 status = lua_tointeger(L, -1);
6008
6009 reason:
6010 lua_pop(L, 1); /* restore the stack: remove status */
6011 ret = lua_getfield(L, 2, "reason");
6012 if (ret == LUA_TSTRING)
6013 reason = lua_tostring(L, -1);
6014
6015 body:
6016 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
6017 ret = lua_getfield(L, 2, "body");
6018 if (ret == LUA_TSTRING)
6019 body = lua_tostring(L, -1);
6020 lua_pop(L, 1); /* restore the stack: remove body */
6021 }
6022
6023 /* Create the Reply table */
6024 lua_newtable(L);
6025
6026 /* Add status element */
6027 lua_pushstring(L, "status");
6028 lua_pushinteger(L, status);
6029 lua_settable(L, -3);
6030
6031 /* Add reason element */
6032 reason = http_get_reason(status);
6033 lua_pushstring(L, "reason");
6034 lua_pushstring(L, reason);
6035 lua_settable(L, -3);
6036
6037 /* Add body element, nil if undefined */
6038 lua_pushstring(L, "body");
6039 if (body)
6040 lua_pushstring(L, body);
6041 else
6042 lua_pushnil(L);
6043 lua_settable(L, -3);
6044
6045 /* Add headers element */
6046 lua_pushstring(L, "headers");
6047 lua_newtable(L);
6048
6049 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6050 if (lua_istable(L, 2)) {
6051 /* load headers from the table argument at index 2. If it is a table, copy it. */
6052 ret = lua_getfield(L, 2, "headers");
6053 if (ret == LUA_TTABLE) {
6054 /* stack: [ ... <headers:table>, <table> ] */
6055 lua_pushnil(L);
6056 while (lua_next(L, -2) != 0) {
6057 /* stack: [ ... <headers:table>, <table>, k, v] */
6058 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
6059 /* invalid value type, skip it */
6060 lua_pop(L, 1);
6061 continue;
6062 }
6063
6064
6065 /* Duplicate the key and swap it with the value. */
6066 lua_pushvalue(L, -2);
6067 lua_insert(L, -2);
6068 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6069
6070 lua_newtable(L);
6071 lua_insert(L, -2);
6072 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6073
6074 if (lua_isstring(L, -1)) {
6075 /* push the value in the inner table */
6076 lua_rawseti(L, -2, 1);
6077 }
6078 else { /* table */
6079 lua_pushnil(L);
6080 while (lua_next(L, -2) != 0) {
6081 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6082 if (!lua_isstring(L, -1)) {
6083 /* invalid value type, skip it*/
6084 lua_pop(L, 1);
6085 continue;
6086 }
6087 /* push the value in the inner table */
6088 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6089 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6090 }
6091 lua_pop(L, 1);
6092 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6093 }
6094
6095 /* push (k,v) on the stack in the headers table:
6096 * stack: [ ... <headers:table>, <table>, k, k, v ]
6097 */
6098 lua_settable(L, -5);
6099 /* stack: [ ... <headers:table>, <table>, k ] */
6100 }
6101 }
6102 lua_pop(L, 1);
6103 }
6104 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6105 lua_settable(L, -3);
6106 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6107
6108 /* Pop a class sesison metatable and affect it to the userdata. */
6109 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6110 lua_setmetatable(L, -2);
6111 return 1;
6112}
6113
6114/* Set the reply status code, and optionally the reason. If no reason is
6115 * provided, the default one corresponding to the status code is used.
6116 */
6117__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6118{
6119 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6120 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6121
6122 /* First argument (self) must be a table */
6123 luaL_checktype(L, 1, LUA_TTABLE);
6124
6125 if (status < 100 || status > 599) {
6126 lua_pushboolean(L, 0);
6127 return 1;
6128 }
6129 if (!reason)
6130 reason = http_get_reason(status);
6131
6132 lua_pushinteger(L, status);
6133 lua_setfield(L, 1, "status");
6134
6135 lua_pushstring(L, reason);
6136 lua_setfield(L, 1, "reason");
6137
6138 lua_pushboolean(L, 1);
6139 return 1;
6140}
6141
6142/* Add a header into the reply object. Each header name is associated to an
6143 * array of values in the "headers" table. If the header name is not found, a
6144 * new entry is created.
6145 */
6146__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6147{
6148 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6149 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6150 int ret;
6151
6152 /* First argument (self) must be a table */
6153 luaL_checktype(L, 1, LUA_TTABLE);
6154
6155 /* Push in the stack the "headers" entry. */
6156 ret = lua_getfield(L, 1, "headers");
6157 if (ret != LUA_TTABLE) {
6158 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6159 WILL_LJMP(lua_error(L));
6160 }
6161
6162 /* check if the header is already registered. If not, register it. */
6163 ret = lua_getfield(L, -1, name);
6164 if (ret == LUA_TNIL) {
6165 /* Entry not found. */
6166 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6167
6168 /* Insert the new header name in the array in the top of the stack.
6169 * It left the new array in the top of the stack.
6170 */
6171 lua_newtable(L);
6172 lua_pushstring(L, name);
6173 lua_pushvalue(L, -2);
6174 lua_settable(L, -4);
6175 }
6176 else if (ret != LUA_TTABLE) {
6177 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6178 WILL_LJMP(lua_error(L));
6179 }
6180
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006181 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006182 * the header value as new entry.
6183 */
6184 lua_pushstring(L, value);
6185 ret = lua_rawlen(L, -2);
6186 lua_rawseti(L, -2, ret + 1);
6187
6188 lua_pushboolean(L, 1);
6189 return 1;
6190}
6191
6192/* Remove all occurrences of a given header name. */
6193__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6194{
6195 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6196 int ret;
6197
6198 /* First argument (self) must be a table */
6199 luaL_checktype(L, 1, LUA_TTABLE);
6200
6201 /* Push in the stack the "headers" entry. */
6202 ret = lua_getfield(L, 1, "headers");
6203 if (ret != LUA_TTABLE) {
6204 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6205 WILL_LJMP(lua_error(L));
6206 }
6207
6208 lua_pushstring(L, name);
6209 lua_pushnil(L);
6210 lua_settable(L, -3);
6211
6212 lua_pushboolean(L, 1);
6213 return 1;
6214}
6215
6216/* Set the reply's body. Overwrite any existing entry. */
6217__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6218{
6219 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6220
6221 /* First argument (self) must be a table */
6222 luaL_checktype(L, 1, LUA_TTABLE);
6223
6224 lua_pushstring(L, payload);
6225 lua_setfield(L, 1, "body");
6226
6227 lua_pushboolean(L, 1);
6228 return 1;
6229}
6230
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006231__LJMP static int hlua_log(lua_State *L)
6232{
6233 int level;
6234 const char *msg;
6235
6236 MAY_LJMP(check_args(L, 2, "log"));
6237 level = MAY_LJMP(luaL_checkinteger(L, 1));
6238 msg = MAY_LJMP(luaL_checkstring(L, 2));
6239
6240 if (level < 0 || level >= NB_LOG_LEVELS)
6241 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6242
6243 hlua_sendlog(NULL, level, msg);
6244 return 0;
6245}
6246
6247__LJMP static int hlua_log_debug(lua_State *L)
6248{
6249 const char *msg;
6250
6251 MAY_LJMP(check_args(L, 1, "debug"));
6252 msg = MAY_LJMP(luaL_checkstring(L, 1));
6253 hlua_sendlog(NULL, LOG_DEBUG, msg);
6254 return 0;
6255}
6256
6257__LJMP static int hlua_log_info(lua_State *L)
6258{
6259 const char *msg;
6260
6261 MAY_LJMP(check_args(L, 1, "info"));
6262 msg = MAY_LJMP(luaL_checkstring(L, 1));
6263 hlua_sendlog(NULL, LOG_INFO, msg);
6264 return 0;
6265}
6266
6267__LJMP static int hlua_log_warning(lua_State *L)
6268{
6269 const char *msg;
6270
6271 MAY_LJMP(check_args(L, 1, "warning"));
6272 msg = MAY_LJMP(luaL_checkstring(L, 1));
6273 hlua_sendlog(NULL, LOG_WARNING, msg);
6274 return 0;
6275}
6276
6277__LJMP static int hlua_log_alert(lua_State *L)
6278{
6279 const char *msg;
6280
6281 MAY_LJMP(check_args(L, 1, "alert"));
6282 msg = MAY_LJMP(luaL_checkstring(L, 1));
6283 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006284 return 0;
6285}
6286
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006287__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006288{
6289 int wakeup_ms = lua_tointeger(L, -1);
Willy Tarreau9cc2e4c2021-09-30 16:12:31 +02006290 if (!tick_is_expired(wakeup_ms, now_ms))
Willy Tarreau9635e032018-10-16 17:52:55 +02006291 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006292 return 0;
6293}
6294
6295__LJMP static int hlua_sleep(lua_State *L)
6296{
6297 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006298 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006299
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006300 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006301
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006302 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006303 wakeup_ms = tick_add(now_ms, delay);
6304 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006305
Willy Tarreau9635e032018-10-16 17:52:55 +02006306 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006307 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006308}
6309
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006310__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006311{
6312 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006313 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006314
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006315 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006316
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006317 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006318 wakeup_ms = tick_add(now_ms, delay);
6319 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006320
Willy Tarreau9635e032018-10-16 17:52:55 +02006321 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006322 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006323}
6324
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006325/* This functionis an LUA binding. it permits to give back
6326 * the hand at the HAProxy scheduler. It is used when the
6327 * LUA processing consumes a lot of time.
6328 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006329__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006330{
6331 return 0;
6332}
6333
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006334__LJMP static int hlua_yield(lua_State *L)
6335{
Willy Tarreau9635e032018-10-16 17:52:55 +02006336 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006337 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006338}
6339
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006340/* This function change the nice of the currently executed
6341 * task. It is used set low or high priority at the current
6342 * task.
6343 */
Willy Tarreau59551662015-03-10 14:23:13 +01006344__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006345{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006346 struct hlua *hlua;
6347 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006348
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006349 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006350 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006351
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006352 /* Get hlua struct, or NULL if we execute from main lua state */
6353 hlua = hlua_gethlua(L);
6354
6355 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006356 if (!hlua || !hlua->task)
6357 return 0;
6358
6359 if (nice < -1024)
6360 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006361 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006362 nice = 1024;
6363
6364 hlua->task->nice = nice;
6365 return 0;
6366}
6367
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006368/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006369 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6370 * return an E_AGAIN signal, the emmiter of this signal must set a
6371 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006372 *
6373 * Task wrapper are longjmp safe because the only one Lua code
6374 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006375 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006376struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006377{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006378 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006379 enum hlua_exec status;
6380
Christopher Faulet5bc99722018-04-25 10:34:45 +02006381 if (task->thread_mask == MAX_THREADS_MASK)
6382 task_set_affinity(task, tid_bit);
6383
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006384 /* If it is the first call to the task, we must initialize the
6385 * execution timeouts.
6386 */
6387 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006388 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006389
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006390 /* Execute the Lua code. */
6391 status = hlua_ctx_resume(hlua, 1);
6392
6393 switch (status) {
6394 /* finished or yield */
6395 case HLUA_E_OK:
6396 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006397 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006398 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006399 break;
6400
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006401 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006402 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006403 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006404 break;
6405
6406 /* finished with error. */
6407 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006408 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006409 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006410 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006411 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006412 break;
6413
6414 case HLUA_E_ERR:
6415 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006416 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006417 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006418 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006419 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006420 break;
6421 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006422 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006423}
6424
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006425/* This function is an LUA binding that register LUA function to be
6426 * executed after the HAProxy configuration parsing and before the
6427 * HAProxy scheduler starts. This function expect only one LUA
6428 * argument that is a function. This function returns nothing, but
6429 * throws if an error is encountered.
6430 */
6431__LJMP static int hlua_register_init(lua_State *L)
6432{
6433 struct hlua_init_function *init;
6434 int ref;
6435
6436 MAY_LJMP(check_args(L, 1, "register_init"));
6437
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006438 if (hlua_gethlua(L)) {
6439 /* runtime processing */
6440 WILL_LJMP(luaL_error(L, "register_init: not available outside of body context"));
6441 }
6442
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006443 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6444
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006445 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006446 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006447 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006448
6449 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006450 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006451 return 0;
6452}
6453
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006454/* This functio is an LUA binding. It permits to register a task
6455 * executed in parallel of the main HAroxy activity. The task is
6456 * created and it is set in the HAProxy scheduler. It can be called
6457 * from the "init" section, "post init" or during the runtime.
6458 *
6459 * Lua prototype:
6460 *
6461 * <none> core.register_task(<function>)
6462 */
6463static int hlua_register_task(lua_State *L)
6464{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006465 struct hlua *hlua = NULL;
6466 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006467 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006468 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006469
6470 MAY_LJMP(check_args(L, 1, "register_task"));
6471
6472 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6473
Thierry Fournier75fc0292020-11-28 13:18:56 +01006474 /* Get the reference state. If the reference is NULL, L is the master
6475 * state, otherwise hlua->T is.
6476 */
6477 hlua = hlua_gethlua(L);
6478 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006479 /* we are in runtime processing */
6480 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006481 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006482 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006483 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006484
Willy Tarreaubafbe012017-11-24 17:34:44 +01006485 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006486 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006487 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006488 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006489
Thierry Fournier59f11be2020-11-29 00:37:41 +01006490 /* We are in the common lua state, execute the task anywhere,
6491 * otherwise, inherit the current thread identifier
6492 */
6493 if (state_id == 0)
6494 task = task_new(MAX_THREADS_MASK);
6495 else
6496 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006497 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006498 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006499
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006500 task->context = hlua;
6501 task->process = hlua_process_task;
6502
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01006503 if (!hlua_ctx_init(hlua, state_id, task))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006504 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006505
6506 /* Restore the function in the stack. */
6507 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
Aurelien DARRAGONb6aade32023-03-13 14:09:21 +01006508 /* function ref not needed anymore since it was pushed to the substack */
6509 hlua_unref(L, ref);
6510
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006511 hlua->nargs = 0;
6512
6513 /* Schedule task. */
Willy Tarreau790810f2021-09-30 16:17:37 +02006514 task_wakeup(task, TASK_WOKEN_INIT);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006515
6516 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006517
6518 alloc_error:
6519 task_destroy(task);
6520 hlua_ctx_destroy(hlua);
6521 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6522 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006523}
6524
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006525/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6526 * doesn't allow "yield" functions because the HAProxy engine cannot
6527 * resume converters.
6528 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006529static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006530{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006531 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006532 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006533 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006534
Willy Tarreaube508f12016-03-10 11:47:01 +01006535 if (!stream)
6536 return 0;
6537
Willy Tarreau87b09662015-04-03 00:22:06 +02006538 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006539 * Lua context can be not initialized. This behavior
6540 * permits to save performances because a systematic
6541 * Lua initialization cause 5% performances loss.
6542 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006543 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006544 struct hlua *hlua;
6545
6546 hlua = pool_alloc(pool_head_hlua);
6547 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006548 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6549 return 0;
6550 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006551 HLUA_INIT(hlua);
6552 stream->hlua = hlua;
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01006553 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006554 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6555 return 0;
6556 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006557 }
6558
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006559 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006560 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006561
6562 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006563 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006564 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6565 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006566 else
6567 error = "critical error";
6568 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006569 return 0;
6570 }
6571
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006572 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006574 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006575 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006576 return 0;
6577 }
6578
6579 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006580 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006581
6582 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006583 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006584 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006585 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006586 return 0;
6587 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006588 hlua_smp2lua(stream->hlua->T, smp);
6589 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006590
6591 /* push keywords in the stack. */
6592 if (arg_p) {
6593 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006594 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006595 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006596 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006597 return 0;
6598 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006599 hlua_arg2lua(stream->hlua->T, arg_p);
6600 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006601 }
6602 }
6603
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006604 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006605 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006606
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006607 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006608 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006609 }
6610
6611 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006612 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006613 /* finished. */
6614 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006615 /* If the stack is empty, the function fails. */
6616 if (lua_gettop(stream->hlua->T) <= 0)
6617 return 0;
6618
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006619 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006620 hlua_lua2smp(stream->hlua->T, -1, smp);
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02006621 /* dup the smp before popping the related lua value and
6622 * returning it to haproxy
6623 */
6624 smp_dup(smp);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006625 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006626 return 1;
6627
6628 /* yield. */
6629 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006630 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006631 return 0;
6632
6633 /* finished with error. */
6634 case HLUA_E_ERRMSG:
6635 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006636 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006637 fcn->name, lua_tostring(stream->hlua->T, -1));
6638 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006639 return 0;
6640
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006641 case HLUA_E_ETMOUT:
6642 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6643 return 0;
6644
6645 case HLUA_E_NOMEM:
6646 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6647 return 0;
6648
6649 case HLUA_E_YIELD:
6650 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6651 return 0;
6652
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006653 case HLUA_E_ERR:
6654 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006655 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006656 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006657
6658 default:
6659 return 0;
6660 }
6661}
6662
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006663/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6664 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006665 * resume sample-fetches. This function will be called by the sample
6666 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006667 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006668static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6669 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006670{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006671 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006672 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006673 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006674 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006675
Willy Tarreaube508f12016-03-10 11:47:01 +01006676 if (!stream)
6677 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006678
Willy Tarreau87b09662015-04-03 00:22:06 +02006679 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006680 * Lua context can be not initialized. This behavior
6681 * permits to save performances because a systematic
6682 * Lua initialization cause 5% performances loss.
6683 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006684 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006685 struct hlua *hlua;
6686
6687 hlua = pool_alloc(pool_head_hlua);
6688 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006689 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6690 return 0;
6691 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006692 hlua->T = NULL;
6693 stream->hlua = hlua;
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01006694 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006695 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6696 return 0;
6697 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006698 }
6699
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006700 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006701 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006702
6703 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006704 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006705 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6706 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006707 else
6708 error = "critical error";
6709 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006710 return 0;
6711 }
6712
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006713 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006714 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006715 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006716 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006717 return 0;
6718 }
6719
6720 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006721 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006722
6723 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006724 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006725 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006726 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006727 return 0;
6728 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006729 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006730
6731 /* push keywords in the stack. */
6732 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6733 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006734 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006735 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006736 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006737 return 0;
6738 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006739 hlua_arg2lua(stream->hlua->T, arg_p);
6740 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006741 }
6742
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006743 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006744 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006745
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006746 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006747 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006748 }
6749
6750 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006751 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006752 /* finished. */
6753 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006754 /* If the stack is empty, the function fails. */
6755 if (lua_gettop(stream->hlua->T) <= 0)
6756 return 0;
6757
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006758 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006759 hlua_lua2smp(stream->hlua->T, -1, smp);
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02006760 /* dup the smp before popping the related lua value and
6761 * returning it to haproxy
6762 */
6763 smp_dup(smp);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006764 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006765
6766 /* Set the end of execution flag. */
6767 smp->flags &= ~SMP_F_MAY_CHANGE;
6768 return 1;
6769
6770 /* yield. */
6771 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006772 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006773 return 0;
6774
6775 /* finished with error. */
6776 case HLUA_E_ERRMSG:
6777 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006778 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006779 fcn->name, lua_tostring(stream->hlua->T, -1));
6780 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006781 return 0;
6782
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006783 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006784 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6785 return 0;
6786
6787 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006788 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6789 return 0;
6790
6791 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006792 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6793 return 0;
6794
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006795 case HLUA_E_ERR:
6796 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006797 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006798 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006799
6800 default:
6801 return 0;
6802 }
6803}
6804
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006805/* This function is an LUA binding used for registering
6806 * "sample-conv" functions. It expects a converter name used
6807 * in the haproxy configuration file, and an LUA function.
6808 */
6809__LJMP static int hlua_register_converters(lua_State *L)
6810{
6811 struct sample_conv_kw_list *sck;
6812 const char *name;
6813 int ref;
6814 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02006815 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006816 struct sample_conv *sc;
6817 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006818
6819 MAY_LJMP(check_args(L, 2, "register_converters"));
6820
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006821 if (hlua_gethlua(L)) {
6822 /* runtime processing */
6823 WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context"));
6824 }
6825
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006826 /* First argument : converter name. */
6827 name = MAY_LJMP(luaL_checkstring(L, 1));
6828
6829 /* Second argument : lua function. */
6830 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6831
Thierry Fournierf67442e2020-11-28 20:41:07 +01006832 /* Check if the converter is already registered */
6833 trash = get_trash_chunk();
6834 chunk_printf(trash, "lua.%s", name);
6835 sc = find_sample_conv(trash->area, trash->data);
6836 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006837 fcn = sc->private;
6838 if (fcn->function_ref[hlua_state_id] != -1) {
6839 ha_warning("Trying to register converter 'lua.%s' more than once. "
6840 "This will become a hard error in version 2.5.\n", name);
6841 }
6842 fcn->function_ref[hlua_state_id] = ref;
6843 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006844 }
6845
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006846 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006847 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006848 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02006849 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006850 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006851 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02006852 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006853
6854 /* Fill fcn. */
6855 fcn->name = strdup(name);
6856 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02006857 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006858 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006859
6860 /* List head */
6861 sck->list.n = sck->list.p = NULL;
6862
6863 /* converter keyword. */
6864 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006865 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006866 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02006867 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006868
6869 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6870 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006871 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 +01006872 sck->kw[0].val_args = NULL;
6873 sck->kw[0].in_type = SMP_T_STR;
6874 sck->kw[0].out_type = SMP_T_STR;
6875 sck->kw[0].private = fcn;
6876
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006877 /* Register this new converter */
6878 sample_register_convs(sck);
6879
6880 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02006881
6882 alloc_error:
6883 release_hlua_function(fcn);
6884 ha_free(&sck);
6885 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6886 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006887}
6888
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006889/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006890 * "sample-fetch" functions. It expects a converter name used
6891 * in the haproxy configuration file, and an LUA function.
6892 */
6893__LJMP static int hlua_register_fetches(lua_State *L)
6894{
6895 const char *name;
6896 int ref;
6897 int len;
6898 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02006899 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006900 struct sample_fetch *sf;
6901 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006902
6903 MAY_LJMP(check_args(L, 2, "register_fetches"));
6904
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006905 if (hlua_gethlua(L)) {
6906 /* runtime processing */
6907 WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context"));
6908 }
6909
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006910 /* First argument : sample-fetch name. */
6911 name = MAY_LJMP(luaL_checkstring(L, 1));
6912
6913 /* Second argument : lua function. */
6914 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6915
Thierry Fournierf67442e2020-11-28 20:41:07 +01006916 /* Check if the sample-fetch is already registered */
6917 trash = get_trash_chunk();
6918 chunk_printf(trash, "lua.%s", name);
6919 sf = find_sample_fetch(trash->area, trash->data);
6920 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006921 fcn = sf->private;
6922 if (fcn->function_ref[hlua_state_id] != -1) {
6923 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6924 "This will become a hard error in version 2.5.\n", name);
6925 }
6926 fcn->function_ref[hlua_state_id] = ref;
6927 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006928 }
6929
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006930 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006931 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006932 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02006933 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006934 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006935 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02006936 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006937
6938 /* Fill fcn. */
6939 fcn->name = strdup(name);
6940 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02006941 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006942 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006943
6944 /* List head */
6945 sfk->list.n = sfk->list.p = NULL;
6946
6947 /* sample-fetch keyword. */
6948 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006949 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006950 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02006951 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006952
6953 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6954 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006955 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 +01006956 sfk->kw[0].val_args = NULL;
6957 sfk->kw[0].out_type = SMP_T_STR;
6958 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6959 sfk->kw[0].val = 0;
6960 sfk->kw[0].private = fcn;
6961
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006962 /* Register this new fetch. */
6963 sample_register_fetches(sfk);
6964
6965 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02006966
6967 alloc_error:
6968 release_hlua_function(fcn);
6969 ha_free(&sfk);
6970 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6971 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006972}
6973
Christopher Faulet501465d2020-02-26 14:54:16 +01006974/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006975 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006976__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006977{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006978 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006979 unsigned int delay;
6980 unsigned int wakeup_ms;
6981
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006982 /* Get hlua struct, or NULL if we execute from main lua state */
6983 hlua = hlua_gethlua(L);
6984 if (!hlua) {
6985 return 0;
6986 }
6987
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006988 MAY_LJMP(check_args(L, 1, "wake_time"));
6989
6990 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6991 wakeup_ms = tick_add(now_ms, delay);
6992 hlua->wake_time = wakeup_ms;
6993 return 0;
6994}
6995
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006996/* This function is a wrapper to execute each LUA function declared as an action
6997 * wrapper during the initialisation period. This function may return any
6998 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6999 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
7000 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007001 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007002static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02007003 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007004{
7005 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007006 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007007 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007008 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007009
7010 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01007011 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
7012 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
7013 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
7014 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007015 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007016 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007017 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007018 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007019
Willy Tarreau87b09662015-04-03 00:22:06 +02007020 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007021 * Lua context can be not initialized. This behavior
7022 * permits to save performances because a systematic
7023 * Lua initialization cause 5% performances loss.
7024 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007025 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007026 struct hlua *hlua;
7027
7028 hlua = pool_alloc(pool_head_hlua);
7029 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007030 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007031 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007032 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007033 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007034 HLUA_INIT(hlua);
7035 s->hlua = hlua;
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01007036 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 +01007037 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007038 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007039 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007040 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007041 }
7042
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007043 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007044 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007045
7046 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007047 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007048 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
7049 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01007050 else
7051 error = "critical error";
7052 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007053 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007054 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007055 }
7056
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007057 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007058 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007059 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007060 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007061 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007062 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007063 }
7064
7065 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007066 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 +01007067
Willy Tarreau87b09662015-04-03 00:22:06 +02007068 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007069 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007070 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007071 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007072 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007073 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007074 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007075 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007076
7077 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007078 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007079 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007080 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007081 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007082 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007083 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007084 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007085 lua_pushstring(s->hlua->T, *arg);
7086 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007087 }
7088
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007089 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007090 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007091
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007092 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007093 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007094 }
7095
7096 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01007097 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007098 /* finished. */
7099 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007100 /* Catch the return value */
7101 if (lua_gettop(s->hlua->T) > 0)
7102 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007103
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007104 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007105 if (act_ret == ACT_RET_YIELD) {
7106 if (flags & ACT_OPT_FINAL)
7107 goto err_yield;
7108
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007109 if (dir == SMP_OPT_DIR_REQ)
7110 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7111 s->hlua->wake_time);
7112 else
7113 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7114 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007115 }
7116 goto end;
7117
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007118 /* yield. */
7119 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007120 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007121 if (dir == SMP_OPT_DIR_REQ)
7122 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7123 s->hlua->wake_time);
7124 else
7125 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7126 s->hlua->wake_time);
7127
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007128 /* Some actions can be wake up when a "write" event
7129 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007130 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007131 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007132 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007133 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007134 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007135 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007136 act_ret = ACT_RET_YIELD;
7137 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007138
7139 /* finished with error. */
7140 case HLUA_E_ERRMSG:
7141 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007142 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007143 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007144 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007145 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007146
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007147 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007148 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007149 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007150
7151 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007152 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007153 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007154
7155 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007156 err_yield:
7157 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007158 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007159 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007160 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007161
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007162 case HLUA_E_ERR:
7163 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007164 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007165 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007166
7167 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007168 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007169 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007170
7171 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007172 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007173 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007174 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007175}
7176
Willy Tarreau144f84a2021-03-02 16:09:26 +01007177struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007178{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007179 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007180
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007181 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007182 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007183 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007184}
7185
7186static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7187{
7188 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007189 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007190 struct task *task;
7191 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007192 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007193
Willy Tarreaubafbe012017-11-24 17:34:44 +01007194 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007195 if (!hlua) {
7196 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007197 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007198 return 0;
7199 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007200 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007201 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007202 ctx->ctx.hlua_apptcp.flags = 0;
7203
7204 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007205 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007206 if (!task) {
7207 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007208 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007209 return 0;
7210 }
7211 task->nice = 0;
7212 task->context = ctx;
7213 task->process = hlua_applet_wakeup;
7214 ctx->ctx.hlua_apptcp.task = task;
7215
7216 /* In the execution wrappers linked with a stream, the
7217 * Lua context can be not initialized. This behavior
7218 * permits to save performances because a systematic
7219 * Lua initialization cause 5% performances loss.
7220 */
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01007221 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007222 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007223 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007224 return 0;
7225 }
7226
7227 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007228 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007229
7230 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007231 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007232 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7233 error = lua_tostring(hlua->T, -1);
7234 else
7235 error = "critical error";
7236 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007237 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007238 return 0;
7239 }
7240
7241 /* Check stack available size. */
7242 if (!lua_checkstack(hlua->T, 1)) {
7243 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007244 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007245 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007246 return 0;
7247 }
7248
7249 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007250 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007251
7252 /* Create and and push object stream in the stack. */
7253 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7254 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007255 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007256 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007257 return 0;
7258 }
7259 hlua->nargs = 1;
7260
7261 /* push keywords in the stack. */
7262 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7263 if (!lua_checkstack(hlua->T, 1)) {
7264 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007265 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007266 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007267 return 0;
7268 }
7269 lua_pushstring(hlua->T, *arg);
7270 hlua->nargs++;
7271 }
7272
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007273 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007274
7275 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007276 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007277 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007278
7279 return 1;
7280}
7281
Willy Tarreau60409db2019-08-21 14:14:50 +02007282void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007283{
7284 struct stream_interface *si = ctx->owner;
7285 struct stream *strm = si_strm(si);
7286 struct channel *res = si_ic(si);
7287 struct act_rule *rule = ctx->rule;
7288 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007289 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007290
7291 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007292 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7293 /* eat the whole request */
7294 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007295 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007296 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007297
7298 /* If the stream is disconnect or closed, ldo nothing. */
7299 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7300 return;
7301
7302 /* Execute the function. */
7303 switch (hlua_ctx_resume(hlua, 1)) {
7304 /* finished. */
7305 case HLUA_E_OK:
7306 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7307
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007308 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007309 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007310 res->flags |= CF_READ_NULL;
7311 si_shutr(si);
7312 return;
7313
7314 /* yield. */
7315 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007316 if (hlua->wake_time != TICK_ETERNITY)
7317 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007318 return;
7319
7320 /* finished with error. */
7321 case HLUA_E_ERRMSG:
7322 /* Display log. */
7323 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007324 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007325 lua_pop(hlua->T, 1);
7326 goto error;
7327
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007328 case HLUA_E_ETMOUT:
7329 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007330 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007331 goto error;
7332
7333 case HLUA_E_NOMEM:
7334 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007335 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007336 goto error;
7337
7338 case HLUA_E_YIELD: /* unexpected */
7339 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007340 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007341 goto error;
7342
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007343 case HLUA_E_ERR:
7344 /* Display log. */
7345 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007346 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007347 goto error;
7348
7349 default:
7350 goto error;
7351 }
7352
7353error:
7354
7355 /* For all other cases, just close the stream. */
7356 si_shutw(si);
7357 si_shutr(si);
7358 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7359}
7360
7361static void hlua_applet_tcp_release(struct appctx *ctx)
7362{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007363 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007364 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007365 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007366 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007367}
7368
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007369/* The function returns 1 if the initialisation is complete, 0 if
7370 * an errors occurs and -1 if more data are required for initializing
7371 * the applet.
7372 */
7373static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7374{
7375 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007376 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007377 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007378 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007379 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007380 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007381
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007382 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007383 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007384 if (!hlua) {
7385 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007386 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007387 return 0;
7388 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007389 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007390 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007391 ctx->ctx.hlua_apphttp.left_bytes = -1;
7392 ctx->ctx.hlua_apphttp.flags = 0;
7393
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007394 if (txn->req.flags & HTTP_MSGF_VER_11)
7395 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7396
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007397 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007398 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007399 if (!task) {
7400 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007401 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007402 return 0;
7403 }
7404 task->nice = 0;
7405 task->context = ctx;
7406 task->process = hlua_applet_wakeup;
7407 ctx->ctx.hlua_apphttp.task = task;
7408
7409 /* In the execution wrappers linked with a stream, the
7410 * Lua context can be not initialized. This behavior
7411 * permits to save performances because a systematic
7412 * Lua initialization cause 5% performances loss.
7413 */
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01007414 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007415 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007416 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007417 return 0;
7418 }
7419
7420 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007421 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007422
7423 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007424 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007425 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7426 error = lua_tostring(hlua->T, -1);
7427 else
7428 error = "critical error";
7429 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007430 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007431 return 0;
7432 }
7433
7434 /* Check stack available size. */
7435 if (!lua_checkstack(hlua->T, 1)) {
7436 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007437 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007438 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007439 return 0;
7440 }
7441
7442 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007443 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007444
7445 /* Create and and push object stream in the stack. */
7446 if (!hlua_applet_http_new(hlua->T, ctx)) {
7447 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007448 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007449 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007450 return 0;
7451 }
7452 hlua->nargs = 1;
7453
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007454 /* push keywords in the stack. */
7455 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7456 if (!lua_checkstack(hlua->T, 1)) {
7457 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007458 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007459 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007460 return 0;
7461 }
7462 lua_pushstring(hlua->T, *arg);
7463 hlua->nargs++;
7464 }
7465
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007466 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007467
7468 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007469 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007470
7471 return 1;
7472}
7473
Willy Tarreau60409db2019-08-21 14:14:50 +02007474void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007475{
7476 struct stream_interface *si = ctx->owner;
7477 struct stream *strm = si_strm(si);
7478 struct channel *req = si_oc(si);
7479 struct channel *res = si_ic(si);
7480 struct act_rule *rule = ctx->rule;
7481 struct proxy *px = strm->be;
7482 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7483 struct htx *req_htx, *res_htx;
7484
7485 res_htx = htx_from_buf(&res->buf);
7486
7487 /* If the stream is disconnect or closed, ldo nothing. */
7488 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7489 goto out;
7490
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007491 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007492 if (!b_size(&res->buf)) {
7493 si_rx_room_blk(si);
7494 goto out;
7495 }
7496 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007497 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007498 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7499
7500 /* Set the currently running flag. */
7501 if (!HLUA_IS_RUNNING(hlua) &&
7502 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007503 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007504 si_cant_get(si);
7505 goto out;
7506 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007507 }
7508
7509 /* Executes The applet if it is not done. */
7510 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7511
7512 /* Execute the function. */
7513 switch (hlua_ctx_resume(hlua, 1)) {
7514 /* finished. */
7515 case HLUA_E_OK:
7516 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7517 break;
7518
7519 /* yield. */
7520 case HLUA_E_AGAIN:
7521 if (hlua->wake_time != TICK_ETERNITY)
7522 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007523 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007524
7525 /* finished with error. */
7526 case HLUA_E_ERRMSG:
7527 /* Display log. */
7528 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007529 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007530 lua_pop(hlua->T, 1);
7531 goto error;
7532
7533 case HLUA_E_ETMOUT:
7534 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007535 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007536 goto error;
7537
7538 case HLUA_E_NOMEM:
7539 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007540 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007541 goto error;
7542
7543 case HLUA_E_YIELD: /* unexpected */
7544 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007545 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007546 goto error;
7547
7548 case HLUA_E_ERR:
7549 /* Display log. */
7550 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007551 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007552 goto error;
7553
7554 default:
7555 goto error;
7556 }
7557 }
7558
7559 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007560 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7561 goto done;
7562
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007563 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7564 goto error;
7565
Christopher Faulet868b3b12022-04-07 10:07:18 +02007566 /* no more data are expected. If the response buffer is empty
7567 * for a chunked message, be sure to add something (EOT block in
7568 * this case) to have something to send. It is important to be
7569 * sure the EOM flags will be handled by the endpoint.
7570 */
7571 if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) {
7572 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
7573 si_rx_room_blk(si);
7574 goto out;
7575 }
7576 channel_add_input(res, 1);
7577 }
7578
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007579 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet79c0fc72022-03-07 15:50:54 +01007580 res->flags |= CF_EOI;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007581 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7582 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007583 }
7584
7585 done:
7586 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007587 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007588 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007589 si_shutr(si);
7590 }
7591
7592 /* eat the whole request */
7593 if (co_data(req)) {
7594 req_htx = htx_from_buf(&req->buf);
7595 co_htx_skip(req, req_htx, co_data(req));
7596 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007597 }
7598 }
7599
7600 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007601 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007602 return;
7603
7604 error:
7605
7606 /* If we are in HTTP mode, and we are not send any
7607 * data, return a 500 server error in best effort:
7608 * if there is no room available in the buffer,
7609 * just close the connection.
7610 */
7611 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007612 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007613
7614 channel_erase(res);
7615 res->buf.data = b_data(err);
7616 memcpy(res->buf.area, b_head(err), b_data(err));
7617 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007618 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007619 }
7620 if (!(strm->flags & SF_ERR_MASK))
7621 strm->flags |= SF_ERR_RESOURCE;
7622 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7623 goto done;
7624}
7625
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007626static void hlua_applet_http_release(struct appctx *ctx)
7627{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007628 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007629 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007630 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007631 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007632}
7633
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007634/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007635 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007636 *
7637 * This function can fail with an abort() due to an Lua critical error.
7638 * We are in the configuration parsing process of HAProxy, this abort() is
7639 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007640 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007641static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7642 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007643{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007644 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007645 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007646
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007647 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007648 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007649 if (!rule->arg.hlua_rule) {
7650 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007651 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007652 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007653
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007654 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007655 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7656 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007657 if (!rule->arg.hlua_rule->args) {
7658 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007659 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007660 }
7661
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007662 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007663 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007664
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007665 /* Expect some arguments */
7666 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007667 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007668 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007669 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007670 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007671 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007672 if (!rule->arg.hlua_rule->args[i]) {
7673 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007674 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007675 }
7676 (*cur_arg)++;
7677 }
7678 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007679
Thierry FOURNIER42148732015-09-02 17:17:33 +02007680 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007681 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007682 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007683
7684 error:
7685 if (rule->arg.hlua_rule) {
7686 if (rule->arg.hlua_rule->args) {
7687 for (i = 0; i < fcn->nargs; i++)
7688 ha_free(&rule->arg.hlua_rule->args[i]);
7689 ha_free(&rule->arg.hlua_rule->args);
7690 }
7691 ha_free(&rule->arg.hlua_rule);
7692 }
7693 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007694}
7695
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007696static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7697 struct act_rule *rule, char **err)
7698{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007699 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007700
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007701 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007702 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007703 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007704 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007705 * the call of this analyzer.
7706 */
7707 if (rule->from != ACT_F_HTTP_REQ) {
7708 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7709 return ACT_RET_PRS_ERR;
7710 }
7711
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007712 /* Memory for the rule. */
7713 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7714 if (!rule->arg.hlua_rule) {
7715 memprintf(err, "out of memory error");
7716 return ACT_RET_PRS_ERR;
7717 }
7718
7719 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007720 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007721
7722 /* TODO: later accept arguments. */
7723 rule->arg.hlua_rule->args = NULL;
7724
7725 /* Add applet pointer in the rule. */
7726 rule->applet.obj_type = OBJ_TYPE_APPLET;
7727 rule->applet.name = fcn->name;
7728 rule->applet.init = hlua_applet_http_init;
7729 rule->applet.fct = hlua_applet_http_fct;
7730 rule->applet.release = hlua_applet_http_release;
7731 rule->applet.timeout = hlua_timeout_applet;
7732
7733 return ACT_RET_PRS_OK;
7734}
7735
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007736/* This function is an LUA binding used for registering
7737 * "sample-conv" functions. It expects a converter name used
7738 * in the haproxy configuration file, and an LUA function.
7739 */
7740__LJMP static int hlua_register_action(lua_State *L)
7741{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007742 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007743 const char *name;
7744 int ref;
7745 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007746 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007747 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007748 struct buffer *trash;
7749 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007750
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007751 /* Initialise the number of expected arguments at 0. */
7752 nargs = 0;
7753
7754 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7755 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007756
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01007757 if (hlua_gethlua(L)) {
7758 /* runtime processing */
7759 WILL_LJMP(luaL_error(L, "register_action: not available outside of body context"));
7760 }
7761
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007762 /* First argument : converter name. */
7763 name = MAY_LJMP(luaL_checkstring(L, 1));
7764
7765 /* Second argument : environment. */
7766 if (lua_type(L, 2) != LUA_TTABLE)
7767 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7768
7769 /* Third argument : lua function. */
7770 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7771
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007772 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007773 if (lua_gettop(L) >= 4)
7774 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7775
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007776 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007777 lua_pushnil(L);
7778 while (lua_next(L, 2) != 0) {
7779 if (lua_type(L, -1) != LUA_TSTRING)
7780 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7781
Thierry Fournierf67442e2020-11-28 20:41:07 +01007782 /* Check if action exists */
7783 trash = get_trash_chunk();
7784 chunk_printf(trash, "lua.%s", name);
7785 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7786 akw = tcp_req_cont_action(trash->area);
7787 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7788 akw = tcp_res_cont_action(trash->area);
7789 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7790 akw = action_http_req_custom(trash->area);
7791 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7792 akw = action_http_res_custom(trash->area);
7793 } else {
7794 akw = NULL;
7795 }
7796 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007797 fcn = akw->private;
7798 if (fcn->function_ref[hlua_state_id] != -1) {
7799 ha_warning("Trying to register action 'lua.%s' more than once. "
7800 "This will become a hard error in version 2.5.\n", name);
7801 }
7802 fcn->function_ref[hlua_state_id] = ref;
7803
7804 /* pop the environment string. */
7805 lua_pop(L, 1);
7806 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007807 }
7808
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007809 /* Check required environment. Only accepted "http" or "tcp". */
7810 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007811 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007812 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007813 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007814 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007815 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007816 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007817
7818 /* Fill fcn. */
7819 fcn->name = strdup(name);
7820 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007821 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007822 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007823
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007824 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007825 fcn->nargs = nargs;
7826
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007827 /* List head */
7828 akl->list.n = akl->list.p = NULL;
7829
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007830 /* action keyword. */
7831 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007832 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007833 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007834 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007835
7836 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7837
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007838 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007839 akl->kw[0].private = fcn;
7840 akl->kw[0].parse = action_register_lua;
7841
7842 /* select the action registering point. */
7843 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7844 tcp_req_cont_keywords_register(akl);
7845 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7846 tcp_res_cont_keywords_register(akl);
7847 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7848 http_req_keywords_register(akl);
7849 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7850 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007851 else {
7852 release_hlua_function(fcn);
7853 if (akl)
7854 ha_free((char **)&(akl->kw[0].kw));
7855 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007856 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007857 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7858 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007859 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007860
7861 /* pop the environment string. */
7862 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007863
7864 /* reset for next loop */
7865 akl = NULL;
7866 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007867 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007868 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007869
7870 alloc_error:
7871 release_hlua_function(fcn);
7872 ha_free(&akl);
7873 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7874 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007875}
7876
7877static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7878 struct act_rule *rule, char **err)
7879{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007880 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007881
Christopher Faulet280f85b2019-07-15 15:02:04 +02007882 if (px->mode == PR_MODE_HTTP) {
7883 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007884 return ACT_RET_PRS_ERR;
7885 }
7886
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007887 /* Memory for the rule. */
7888 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7889 if (!rule->arg.hlua_rule) {
7890 memprintf(err, "out of memory error");
7891 return ACT_RET_PRS_ERR;
7892 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007893
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007894 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007895 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007896
7897 /* TODO: later accept arguments. */
7898 rule->arg.hlua_rule->args = NULL;
7899
7900 /* Add applet pointer in the rule. */
7901 rule->applet.obj_type = OBJ_TYPE_APPLET;
7902 rule->applet.name = fcn->name;
7903 rule->applet.init = hlua_applet_tcp_init;
7904 rule->applet.fct = hlua_applet_tcp_fct;
7905 rule->applet.release = hlua_applet_tcp_release;
7906 rule->applet.timeout = hlua_timeout_applet;
7907
7908 return 0;
7909}
7910
7911/* This function is an LUA binding used for registering
7912 * "sample-conv" functions. It expects a converter name used
7913 * in the haproxy configuration file, and an LUA function.
7914 */
7915__LJMP static int hlua_register_service(lua_State *L)
7916{
7917 struct action_kw_list *akl;
7918 const char *name;
7919 const char *env;
7920 int ref;
7921 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007922 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007923 struct buffer *trash;
7924 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007925
7926 MAY_LJMP(check_args(L, 3, "register_service"));
7927
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01007928 if (hlua_gethlua(L)) {
7929 /* runtime processing */
7930 WILL_LJMP(luaL_error(L, "register_service: not available outside of body context"));
7931 }
7932
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007933 /* First argument : converter name. */
7934 name = MAY_LJMP(luaL_checkstring(L, 1));
7935
7936 /* Second argument : environment. */
7937 env = MAY_LJMP(luaL_checkstring(L, 2));
7938
7939 /* Third argument : lua function. */
7940 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7941
Thierry Fournierf67442e2020-11-28 20:41:07 +01007942 /* Check for service already registered */
7943 trash = get_trash_chunk();
7944 chunk_printf(trash, "lua.%s", name);
7945 akw = service_find(trash->area);
7946 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007947 fcn = akw->private;
7948 if (fcn->function_ref[hlua_state_id] != -1) {
7949 ha_warning("Trying to register service 'lua.%s' more than once. "
7950 "This will become a hard error in version 2.5.\n", name);
7951 }
7952 fcn->function_ref[hlua_state_id] = ref;
7953 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007954 }
7955
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007956 /* Allocate and fill the sample fetch keyword struct. */
7957 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7958 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007959 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007960 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007961 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007962 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007963
7964 /* Fill fcn. */
7965 len = strlen("<lua.>") + strlen(name) + 1;
7966 fcn->name = calloc(1, len);
7967 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007968 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007969 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007970 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007971
7972 /* List head */
7973 akl->list.n = akl->list.p = NULL;
7974
7975 /* converter keyword. */
7976 len = strlen("lua.") + strlen(name) + 1;
7977 akl->kw[0].kw = calloc(1, len);
7978 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007979 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007980
7981 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7982
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007983 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007984 if (strcmp(env, "tcp") == 0)
7985 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007986 else if (strcmp(env, "http") == 0)
7987 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007988 else {
7989 release_hlua_function(fcn);
7990 if (akl)
7991 ha_free((char **)&(akl->kw[0].kw));
7992 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007993 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007994 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02007995 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007996
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007997 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007998 akl->kw[0].private = fcn;
7999
8000 /* End of array. */
8001 memset(&akl->kw[1], 0, sizeof(*akl->kw));
8002
8003 /* Register this new converter */
8004 service_keywords_register(akl);
8005
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008006 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02008007
8008 alloc_error:
8009 release_hlua_function(fcn);
8010 ha_free(&akl);
8011 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8012 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008013}
8014
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008015/* This function initialises Lua cli handler. It copies the
8016 * arguments in the Lua stack and create channel IO objects.
8017 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02008018static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008019{
8020 struct hlua *hlua;
8021 struct hlua_function *fcn;
8022 int i;
8023 const char *error;
8024
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008025 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008026 appctx->ctx.hlua_cli.fcn = private;
8027
Willy Tarreaubafbe012017-11-24 17:34:44 +01008028 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008029 if (!hlua) {
8030 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008031 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008032 }
8033 HLUA_INIT(hlua);
8034 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008035
8036 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05008037 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008038 * applet_http. It is absolutely compatible.
8039 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01008040 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008041 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01008042 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008043 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008044 }
8045 appctx->ctx.hlua_cli.task->nice = 0;
8046 appctx->ctx.hlua_cli.task->context = appctx;
8047 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
8048
8049 /* Initialises the Lua context */
Aurelien DARRAGON5483bde2023-03-21 14:02:16 +01008050 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 +01008051 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008052 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008053 }
8054
8055 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008056 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008057 if (lua_type(hlua->T, -1) == LUA_TSTRING)
8058 error = lua_tostring(hlua->T, -1);
8059 else
8060 error = "critical error";
8061 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
8062 goto error;
8063 }
8064
8065 /* Check stack available size. */
8066 if (!lua_checkstack(hlua->T, 2)) {
8067 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8068 goto error;
8069 }
8070
8071 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008072 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008073
8074 /* Once the arguments parsed, the CLI is like an AppletTCP,
8075 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008076 */
8077 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
8078 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8079 goto error;
8080 }
8081 hlua->nargs = 1;
8082
8083 /* push keywords in the stack. */
8084 for (i = 0; *args[i]; i++) {
8085 /* Check stack available size. */
8086 if (!lua_checkstack(hlua->T, 1)) {
8087 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8088 goto error;
8089 }
8090 lua_pushstring(hlua->T, args[i]);
8091 hlua->nargs++;
8092 }
8093
8094 /* We must initialize the execution timeouts. */
8095 hlua->max_time = hlua_timeout_session;
8096
8097 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008098 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008099
8100 /* It's ok */
8101 return 0;
8102
8103 /* It's not ok. */
8104error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008105 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008106 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008107 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008108 return 1;
8109}
8110
8111static int hlua_cli_io_handler_fct(struct appctx *appctx)
8112{
8113 struct hlua *hlua;
8114 struct stream_interface *si;
8115 struct hlua_function *fcn;
8116
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008117 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008118 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008119 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008120
8121 /* If the stream is disconnect or closed, ldo nothing. */
8122 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8123 return 1;
8124
8125 /* Execute the function. */
8126 switch (hlua_ctx_resume(hlua, 1)) {
8127
8128 /* finished. */
8129 case HLUA_E_OK:
8130 return 1;
8131
8132 /* yield. */
8133 case HLUA_E_AGAIN:
8134 /* We want write. */
8135 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008136 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008137 /* Set the timeout. */
8138 if (hlua->wake_time != TICK_ETERNITY)
8139 task_schedule(hlua->task, hlua->wake_time);
8140 return 0;
8141
8142 /* finished with error. */
8143 case HLUA_E_ERRMSG:
8144 /* Display log. */
8145 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8146 fcn->name, lua_tostring(hlua->T, -1));
8147 lua_pop(hlua->T, 1);
8148 return 1;
8149
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008150 case HLUA_E_ETMOUT:
8151 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8152 fcn->name);
8153 return 1;
8154
8155 case HLUA_E_NOMEM:
8156 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8157 fcn->name);
8158 return 1;
8159
8160 case HLUA_E_YIELD: /* unexpected */
8161 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8162 fcn->name);
8163 return 1;
8164
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008165 case HLUA_E_ERR:
8166 /* Display log. */
8167 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8168 fcn->name);
8169 return 1;
8170
8171 default:
8172 return 1;
8173 }
8174
8175 return 1;
8176}
8177
8178static void hlua_cli_io_release_fct(struct appctx *appctx)
8179{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008180 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008181 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008182}
8183
8184/* This function is an LUA binding used for registering
8185 * new keywords in the cli. It expects a list of keywords
8186 * which are the "path". It is limited to 5 keywords. A
8187 * description of the command, a function to be executed
8188 * for the parsing and a function for io handlers.
8189 */
8190__LJMP static int hlua_register_cli(lua_State *L)
8191{
8192 struct cli_kw_list *cli_kws;
8193 const char *message;
8194 int ref_io;
8195 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008196 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008197 int index;
8198 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008199 struct buffer *trash;
8200 const char *kw[5];
8201 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008202 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008203
8204 MAY_LJMP(check_args(L, 3, "register_cli"));
8205
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01008206 if (hlua_gethlua(L)) {
8207 /* runtime processing */
8208 WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context"));
8209 }
8210
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008211 /* First argument : an array of maximum 5 keywords. */
8212 if (!lua_istable(L, 1))
8213 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8214
8215 /* Second argument : string with contextual message. */
8216 message = MAY_LJMP(luaL_checkstring(L, 2));
8217
8218 /* Third and fourth argument : lua function. */
8219 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8220
Thierry Fournierf67442e2020-11-28 20:41:07 +01008221 /* Check for CLI service already registered */
8222 trash = get_trash_chunk();
8223 index = 0;
8224 lua_pushnil(L);
8225 memset(kw, 0, sizeof(kw));
8226 while (lua_next(L, 1) != 0) {
8227 if (index >= CLI_PREFIX_KW_NB)
8228 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8229 if (lua_type(L, -1) != LUA_TSTRING)
8230 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8231 kw[index] = lua_tostring(L, -1);
8232 if (index == 0)
8233 chunk_printf(trash, "%s", kw[index]);
8234 else
8235 chunk_appendf(trash, " %s", kw[index]);
8236 index++;
8237 lua_pop(L, 1);
8238 }
8239 cli_kw = cli_find_kw_exact((char **)kw);
8240 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008241 fcn = cli_kw->private;
8242 if (fcn->function_ref[hlua_state_id] != -1) {
8243 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8244 "This will become a hard error in version 2.5.\n", trash->area);
8245 }
8246 fcn->function_ref[hlua_state_id] = ref_io;
8247 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008248 }
8249
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008250 /* Allocate and fill the sample fetch keyword struct. */
8251 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008252 if (!cli_kws) {
8253 errmsg = "Lua out of memory error.";
8254 goto error;
8255 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008256 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008257 if (!fcn) {
8258 errmsg = "Lua out of memory error.";
8259 goto error;
8260 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008261
8262 /* Fill path. */
8263 index = 0;
8264 lua_pushnil(L);
8265 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008266 if (index >= 5) {
8267 errmsg = "1st argument must be a table with a maximum of 5 entries";
8268 goto error;
8269 }
8270 if (lua_type(L, -1) != LUA_TSTRING) {
8271 errmsg = "1st argument must be a table filled with strings";
8272 goto error;
8273 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008274 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008275 if (!cli_kws->kw[0].str_kw[index]) {
8276 errmsg = "Lua out of memory error.";
8277 goto error;
8278 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008279 index++;
8280 lua_pop(L, 1);
8281 }
8282
8283 /* Copy help message. */
8284 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008285 if (!cli_kws->kw[0].usage) {
8286 errmsg = "Lua out of memory error.";
8287 goto error;
8288 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008289
8290 /* Fill fcn io handler. */
8291 len = strlen("<lua.cli>") + 1;
8292 for (i = 0; i < index; i++)
8293 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8294 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008295 if (!fcn->name) {
8296 errmsg = "Lua out of memory error.";
8297 goto error;
8298 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008299 strncat((char *)fcn->name, "<lua.cli", len);
8300 for (i = 0; i < index; i++) {
8301 strncat((char *)fcn->name, ".", len);
8302 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8303 }
8304 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008305 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008306
8307 /* Fill last entries. */
8308 cli_kws->kw[0].private = fcn;
8309 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8310 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8311 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8312
8313 /* Register this new converter */
8314 cli_register_kw(cli_kws);
8315
8316 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008317
8318 error:
8319 release_hlua_function(fcn);
8320 if (cli_kws) {
8321 for (i = 0; i < index; i++)
8322 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8323 ha_free((char **)&(cli_kws->kw[0].usage));
8324 }
8325 ha_free(&cli_kws);
8326 WILL_LJMP(luaL_error(L, errmsg));
8327 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008328}
8329
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008330static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008331 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008332 char **err, unsigned int *timeout)
8333{
8334 const char *error;
8335
8336 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008337 if (error == PARSE_TIME_OVER) {
8338 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8339 args[1], args[0]);
8340 return -1;
8341 }
8342 else if (error == PARSE_TIME_UNDER) {
8343 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8344 args[1], args[0]);
8345 return -1;
8346 }
8347 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008348 memprintf(err, "%s: invalid timeout", args[0]);
8349 return -1;
8350 }
8351 return 0;
8352}
8353
8354static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008355 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008356 char **err)
8357{
8358 return hlua_read_timeout(args, section_type, curpx, defpx,
8359 file, line, err, &hlua_timeout_session);
8360}
8361
8362static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008363 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008364 char **err)
8365{
8366 return hlua_read_timeout(args, section_type, curpx, defpx,
8367 file, line, err, &hlua_timeout_task);
8368}
8369
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008370static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008371 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008372 char **err)
8373{
8374 return hlua_read_timeout(args, section_type, curpx, defpx,
8375 file, line, err, &hlua_timeout_applet);
8376}
8377
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008378static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008379 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008380 char **err)
8381{
8382 char *error;
8383
8384 hlua_nb_instruction = strtoll(args[1], &error, 10);
8385 if (*error != '\0') {
8386 memprintf(err, "%s: invalid number", args[0]);
8387 return -1;
8388 }
8389 return 0;
8390}
8391
Willy Tarreau32f61e22015-03-18 17:54:59 +01008392static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008393 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008394 char **err)
8395{
8396 char *error;
8397
8398 if (*(args[1]) == 0) {
8399 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8400 return -1;
8401 }
8402 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8403 if (*error != '\0') {
8404 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8405 return -1;
8406 }
8407 return 0;
8408}
8409
8410
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008411/* This function is called by the main configuration key "lua-load". It loads and
8412 * execute an lua file during the parsing of the HAProxy configuration file. It is
8413 * the main lua entry point.
8414 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008415 * This function runs with the HAProxy keywords API. It returns -1 if an error
8416 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008417 *
8418 * In some error case, LUA set an error message in top of the stack. This function
8419 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008420 *
8421 * This function can fail with an abort() due to an Lua critical error.
8422 * We are in the configuration parsing process of HAProxy, this abort() is
8423 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008424 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008425static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008426{
8427 int error;
8428
8429 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008430 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008431 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008432 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8433 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008434 return -1;
8435 }
8436
8437 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008438 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008439 switch (error) {
8440 case LUA_OK:
8441 break;
8442 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008443 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8444 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008445 return -1;
8446 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008447 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008448 return -1;
8449 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008450 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8451 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008452 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008453#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008454 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008455 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8456 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008457 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008458#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008459 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008460 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8461 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008462 return -1;
8463 }
8464
8465 return 0;
8466}
8467
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008468static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008469 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008470 char **err)
8471{
8472 if (*(args[1]) == 0) {
8473 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8474 return -1;
8475 }
8476
Thierry Fournier59f11be2020-11-29 00:37:41 +01008477 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008478 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008479 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008480 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008481}
8482
Thierry Fournier59f11be2020-11-29 00:37:41 +01008483static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008484 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008485 char **err)
8486{
8487 int len;
8488
8489 if (*(args[1]) == 0) {
8490 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8491 return -1;
8492 }
8493
8494 if (per_thread_load == NULL) {
8495 /* allocate the first entry large enough to store the final NULL */
8496 per_thread_load = calloc(1, sizeof(*per_thread_load));
8497 if (per_thread_load == NULL) {
8498 memprintf(err, "out of memory error");
8499 return -1;
8500 }
8501 }
8502
8503 /* count used entries */
8504 for (len = 0; per_thread_load[len] != NULL; len++)
8505 ;
8506
8507 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8508 if (per_thread_load == NULL) {
8509 memprintf(err, "out of memory error");
8510 return -1;
8511 }
8512
8513 per_thread_load[len] = strdup(args[1]);
8514 per_thread_load[len + 1] = NULL;
8515
8516 if (per_thread_load[len] == NULL) {
8517 memprintf(err, "out of memory error");
8518 return -1;
8519 }
8520
8521 /* loading for thread 1 only */
8522 hlua_state_id = 1;
8523 ha_set_tid(0);
8524 return hlua_load_state(args[1], hlua_states[1], err);
8525}
8526
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008527/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8528 * in the given <ctx>.
8529 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008530static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008531{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008532 lua_getglobal(L, "package"); /* push package variable */
8533 lua_pushstring(L, path); /* push given path */
8534 lua_pushstring(L, ";"); /* push semicolon */
8535 lua_getfield(L, -3, type); /* push old path */
8536 lua_concat(L, 3); /* concatenate to new path */
8537 lua_setfield(L, -2, type); /* store new path */
8538 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008539
8540 return 0;
8541}
8542
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008543static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008544 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008545 char **err)
8546{
8547 char *path;
8548 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008549 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008550
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008551 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008552 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008553 }
8554
8555 if (!(*args[1])) {
8556 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008557 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008558 }
8559 path = args[1];
8560
8561 if (*args[2]) {
8562 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8563 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008564 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008565 }
8566 type = args[2];
8567 }
8568
Thierry Fournier59f11be2020-11-29 00:37:41 +01008569 p = calloc(1, sizeof(*p));
8570 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008571 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008572 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008573 }
8574 p->path = strdup(path);
8575 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008576 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008577 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008578 }
8579 p->type = strdup(type);
8580 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008581 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008582 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008583 }
Willy Tarreau2b718102021-04-21 07:32:39 +02008584 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01008585
8586 hlua_prepend_path(hlua_states[0], type, path);
8587 hlua_prepend_path(hlua_states[1], type, path);
8588 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008589
8590err2:
8591 free(p->type);
8592 free(p->path);
8593err:
8594 free(p);
8595 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008596}
8597
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008598/* configuration keywords declaration */
8599static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008600 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008601 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008602 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008603 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8604 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008605 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008606 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008607 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008608 { 0, NULL, NULL },
8609}};
8610
Willy Tarreau0108d902018-11-25 19:14:37 +01008611INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8612
Christopher Fauletafd8f102018-11-08 11:34:21 +01008613
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008614/* This function can fail with an abort() due to an Lua critical error.
8615 * We are in the initialisation process of HAProxy, this abort() is
8616 * tolerated.
8617 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008618int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008619{
8620 struct hlua_init_function *init;
8621 const char *msg;
8622 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008623 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008624 const char *kind;
8625 const char *trace;
8626 int return_status = 1;
8627#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8628 int nres;
8629#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008630
Willy Tarreaucdb53462020-12-02 12:12:00 +01008631 /* disable memory limit checks if limit is not set */
8632 if (!hlua_global_allocator.limit)
8633 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8634
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008635 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008636 if (setjmp(safe_ljmp_env) != 0) {
8637 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008638 if (lua_type(L, -1) == LUA_TSTRING)
8639 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008640 else
8641 error = "critical error";
8642 fprintf(stderr, "Lua post-init: %s.\n", error);
8643 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008644 } else {
8645 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008646 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008647
Thierry Fournierb8cef172020-11-28 15:37:17 +01008648 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008649
Thierry Fournierc7492592020-11-28 23:57:24 +01008650 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008651 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Aurelien DARRAGON93591b42023-03-20 16:29:55 +01008652 /* function ref should be released right away since it was pushed
8653 * on the stack and will not be used anymore
8654 */
8655 hlua_unref(L, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008656
8657#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008658 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008659#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008660 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008661#endif
8662 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008663 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008664
8665 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008666 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008667 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008668
8669 case LUA_ERRERR:
8670 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008671 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008672 case LUA_ERRRUN:
8673 if (!kind)
8674 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008675 msg = lua_tostring(L, -1);
8676 lua_settop(L, 0); /* Empty the stack. */
Christopher Fauletd09cc512021-03-24 14:48:45 +01008677 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008678 if (msg)
8679 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8680 else
8681 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8682 return_status = 0;
8683 break;
8684
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008685 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008686 /* Unknown error */
8687 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008688 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008689 case LUA_YIELD:
8690 /* yield is not configured at this step, this state doesn't happen */
8691 if (!kind)
8692 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008693 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008694 case LUA_ERRMEM:
8695 if (!kind)
8696 kind = "out of memory error";
Aurelien DARRAGON93f5d8d2023-08-09 10:11:49 +02008697 lua_settop(L, 0); /* Empty the stack. */
Christopher Fauletd09cc512021-03-24 14:48:45 +01008698 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008699 ha_alert("Lua init: %s: %s\n", kind, trace);
8700 return_status = 0;
8701 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008702 }
Thierry Fournier670db242020-11-28 10:49:59 +01008703 if (!return_status)
8704 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008705 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008706
8707 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008708 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008709}
8710
Thierry Fournierb8cef172020-11-28 15:37:17 +01008711int hlua_post_init()
8712{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008713 int ret;
8714 int i;
8715 int errors;
8716 char *err = NULL;
8717 struct hlua_function *fcn;
8718
Willy Tarreau42afc592021-08-30 09:35:18 +02008719#if defined(USE_OPENSSL)
Thierry Fournierb8cef172020-11-28 15:37:17 +01008720 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008721 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008722 int saved_used_backed = global.ssl_used_backend;
8723 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008724 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008725 global.ssl_used_backend = saved_used_backed;
8726 }
8727#endif
8728
Thierry Fournierc7492592020-11-28 23:57:24 +01008729 /* Perform post init of common thread */
8730 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008731 ha_set_tid(0);
8732 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8733 if (ret == 0)
8734 return 0;
8735
8736 /* init remaining lua states and load files */
8737 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8738
8739 /* set thread context */
8740 ha_set_tid(hlua_state_id - 1);
8741
8742 /* Init lua state */
8743 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8744
8745 /* Load lua files */
8746 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8747 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8748 if (ret != 0) {
8749 ha_alert("Lua init: %s\n", err);
8750 return 0;
8751 }
8752 }
8753 }
8754
8755 /* Reset thread context */
8756 ha_set_tid(0);
8757
8758 /* Execute post init for all states */
8759 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8760
8761 /* set thread context */
8762 ha_set_tid(hlua_state_id - 1);
8763
8764 /* run post init */
8765 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8766 if (ret == 0)
8767 return 0;
8768 }
8769
8770 /* Reset thread context */
8771 ha_set_tid(0);
8772
8773 /* control functions registering. Each function must have:
8774 * - only the function_ref[0] set positive and all other to -1
8775 * - only the function_ref[0] set to -1 and all other positive
8776 * This ensure a same reference is not used both in shared
8777 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008778 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008779 * complicated to found for the end user.
8780 */
8781 errors = 0;
8782 list_for_each_entry(fcn, &referenced_functions, l) {
8783 ret = 0;
8784 for (i = 1; i < global.nbthread + 1; i++) {
8785 if (fcn->function_ref[i] == -1)
8786 ret--;
8787 else
8788 ret++;
8789 }
8790 if (abs(ret) != global.nbthread) {
8791 ha_alert("Lua function '%s' is not referenced in all thread. "
8792 "Expect function in all thread or in none thread.\n", fcn->name);
8793 errors++;
8794 continue;
8795 }
8796
8797 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008798 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8799 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008800 "exclusive.\n", fcn->name);
8801 errors++;
8802 }
8803 }
8804
8805 if (errors > 0)
8806 return 0;
8807
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008808 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008809 * -1 in order to have probably a segfault if someone use it
8810 */
8811 hlua_state_id = -1;
8812
8813 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008814}
8815
Willy Tarreau32f61e22015-03-18 17:54:59 +01008816/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8817 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8818 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008819 * allocation. <nsize> is the requested new size. A new allocation is
8820 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008821 * zero. This one verifies that the limits are respected but is optimized
8822 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreaue00ad052021-10-22 16:00:02 +02008823 *
8824 * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses
8825 * POSIX by making realloc(ptr,0) an effective free(), but others do not do
8826 * that and will simply allocate zero as if it were the result of malloc(0),
8827 * so mapping this onto realloc() will lead to memory leaks on non-glibc
8828 * systems.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008829 */
8830static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8831{
8832 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008833 size_t limit, old, new;
8834
Tim Duesterhus22586522021-01-08 10:35:33 +01008835 if (unlikely(!ptr && !nsize))
8836 return NULL;
8837
Willy Tarreaucdb53462020-12-02 12:12:00 +01008838 /* a limit of ~0 means unlimited and boot complete, so there's no need
8839 * for accounting anymore.
8840 */
Willy Tarreaue00ad052021-10-22 16:00:02 +02008841 if (likely(~zone->limit == 0)) {
8842 if (!nsize)
8843 ha_free(&ptr);
8844 else
8845 ptr = realloc(ptr, nsize);
8846 return ptr;
8847 }
Willy Tarreau32f61e22015-03-18 17:54:59 +01008848
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008849 if (!ptr)
8850 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008851
Willy Tarreaucdb53462020-12-02 12:12:00 +01008852 /* enforce strict limits across all threads */
8853 limit = zone->limit;
8854 old = _HA_ATOMIC_LOAD(&zone->allocated);
8855 do {
8856 new = old + nsize - osize;
8857 if (unlikely(nsize && limit && new > limit))
8858 return NULL;
8859 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008860
Willy Tarreaue00ad052021-10-22 16:00:02 +02008861 if (!nsize)
8862 ha_free(&ptr);
8863 else
8864 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008865
8866 if (unlikely(!ptr && nsize)) // failed
8867 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8868
8869 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008870 return ptr;
8871}
8872
Thierry Fournierecb83c22020-11-28 15:49:44 +01008873/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008874 * We are in the initialisation process of HAProxy, this abort() is
8875 * tolerated.
8876 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008877lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008878{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008879 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008880 int idx;
8881 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008882 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008883 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008884 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008885 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008886 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008887 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008888
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008889 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008890 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008891
firexinghebdd336c2023-07-13 11:03:34 +08008892 if (!L) {
8893 fprintf(stderr,
8894 "Lua init: critical error: lua_newstate() returned NULL."
8895 " This may possibly be caused by a memory allocation error.\n");
8896 exit(1);
8897 }
8898
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008899 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008900 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008901 *context = NULL;
8902
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008903 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008904 * the Lua function can fail with an abort. We are in the initialisation
8905 * process of HAProxy, this abort() is tolerated.
8906 */
8907
Thierry Fournier3c539322020-11-28 16:05:05 +01008908 /* Call post initialisation function in safe environment. */
8909 if (setjmp(safe_ljmp_env) != 0) {
8910 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008911 if (lua_type(L, -1) == LUA_TSTRING)
8912 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008913 else
8914 error_msg = "critical error";
8915 fprintf(stderr, "Lua init: %s.\n", error_msg);
8916 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008917 } else {
8918 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008919 }
8920
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008921 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008922 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008923#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8924#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8925#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008926 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008927#endif
8928#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008929 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008930#endif
8931#undef HLUA_PREPEND_PATH_TOSTRING
8932#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008933
Thierry Fournier59f11be2020-11-29 00:37:41 +01008934 /* Apply configured prepend path */
8935 list_for_each_entry(pp, &prepend_path_list, l)
8936 hlua_prepend_path(L, pp->type, pp->path);
8937
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008938 /*
8939 *
8940 * Create "core" object.
8941 *
8942 */
8943
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008944 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008945 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008946
Thierry Fournierecb83c22020-11-28 15:49:44 +01008947 /* set the thread id */
8948 hlua_class_const_int(L, "thread", thread_num);
8949
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008950 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008951 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008952 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008953
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008954 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008955 hlua_class_function(L, "register_init", hlua_register_init);
8956 hlua_class_function(L, "register_task", hlua_register_task);
8957 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8958 hlua_class_function(L, "register_converters", hlua_register_converters);
8959 hlua_class_function(L, "register_action", hlua_register_action);
8960 hlua_class_function(L, "register_service", hlua_register_service);
8961 hlua_class_function(L, "register_cli", hlua_register_cli);
8962 hlua_class_function(L, "yield", hlua_yield);
8963 hlua_class_function(L, "set_nice", hlua_set_nice);
8964 hlua_class_function(L, "sleep", hlua_sleep);
8965 hlua_class_function(L, "msleep", hlua_msleep);
8966 hlua_class_function(L, "add_acl", hlua_add_acl);
8967 hlua_class_function(L, "del_acl", hlua_del_acl);
8968 hlua_class_function(L, "set_map", hlua_set_map);
8969 hlua_class_function(L, "del_map", hlua_del_map);
8970 hlua_class_function(L, "tcp", hlua_socket_new);
8971 hlua_class_function(L, "log", hlua_log);
8972 hlua_class_function(L, "Debug", hlua_log_debug);
8973 hlua_class_function(L, "Info", hlua_log_info);
8974 hlua_class_function(L, "Warning", hlua_log_warning);
8975 hlua_class_function(L, "Alert", hlua_log_alert);
8976 hlua_class_function(L, "done", hlua_done);
8977 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008978
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008979 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008980
8981 /*
8982 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008983 * Create "act" object.
8984 *
8985 */
8986
8987 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008988 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008989
8990 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008991 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8992 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8993 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8994 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8995 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8996 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8997 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8998 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008999
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009000 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01009001
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009002 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009003
9004 /*
9005 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009006 * Register class Map
9007 *
9008 */
9009
9010 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009011 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009012
9013 /* register pattern types. */
9014 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009015 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009016 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009017 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009018 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009019 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009020
9021 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009022 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009023
9024 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009025 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009026
Ilya Shipitsind4259502020-04-08 01:07:56 +05009027 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009028 lua_pushstring(L, "__index");
9029 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009030
9031 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009032 hlua_class_function(L, "lookup", hlua_map_lookup);
9033 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009034
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009035 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009036
Thierry Fournier45e78d72016-02-19 18:34:46 +01009037 /* Register previous table in the registry with reference and named entry.
9038 * The function hlua_register_metatable() pops the stack, so we
9039 * previously create a copy of the table.
9040 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009041 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
9042 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009043
9044 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009045 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009046
9047 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009048 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009049
9050 /*
9051 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009052 * Register class Channel
9053 *
9054 */
9055
9056 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009057 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009058
Ilya Shipitsind4259502020-04-08 01:07:56 +05009059 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009060 lua_pushstring(L, "__index");
9061 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009062
9063 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009064 hlua_class_function(L, "get", hlua_channel_get);
9065 hlua_class_function(L, "dup", hlua_channel_dup);
9066 hlua_class_function(L, "getline", hlua_channel_getline);
9067 hlua_class_function(L, "set", hlua_channel_set);
9068 hlua_class_function(L, "append", hlua_channel_append);
9069 hlua_class_function(L, "send", hlua_channel_send);
9070 hlua_class_function(L, "forward", hlua_channel_forward);
9071 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
9072 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
9073 hlua_class_function(L, "is_full", hlua_channel_is_full);
9074 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009075
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009076 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009077
9078 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009079 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009080
9081 /*
9082 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009083 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009084 *
9085 */
9086
9087 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009088 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009089
Ilya Shipitsind4259502020-04-08 01:07:56 +05009090 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009091 lua_pushstring(L, "__index");
9092 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009093
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009094 /* Browse existing fetches and create the associated
9095 * object method.
9096 */
9097 sf = NULL;
9098 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009099 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9100 * by an underscore.
9101 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009102 strlcpy2(trash.area, sf->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009103 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009104 if (*p == '.' || *p == '-' || *p == '+')
9105 *p = '_';
9106
9107 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009108 lua_pushstring(L, trash.area);
9109 lua_pushlightuserdata(L, sf);
9110 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
9111 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009112 }
9113
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009114 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009115
9116 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009117 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009118
9119 /*
9120 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009121 * Register class Converters
9122 *
9123 */
9124
9125 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009126 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009127
9128 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009129 lua_pushstring(L, "__index");
9130 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009131
9132 /* Browse existing converters and create the associated
9133 * object method.
9134 */
9135 sc = NULL;
9136 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009137 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9138 * by an underscore.
9139 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009140 strlcpy2(trash.area, sc->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009141 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009142 if (*p == '.' || *p == '-' || *p == '+')
9143 *p = '_';
9144
9145 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009146 lua_pushstring(L, trash.area);
9147 lua_pushlightuserdata(L, sc);
9148 lua_pushcclosure(L, hlua_run_sample_conv, 1);
9149 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009150 }
9151
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009152 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009153
9154 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009155 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009156
9157 /*
9158 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009159 * Register class HTTP
9160 *
9161 */
9162
9163 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009164 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009165
Ilya Shipitsind4259502020-04-08 01:07:56 +05009166 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009167 lua_pushstring(L, "__index");
9168 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009169
9170 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009171 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9172 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9173 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9174 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9175 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9176 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9177 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9178 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9179 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9180 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009181
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009182 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9183 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9184 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9185 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9186 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9187 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9188 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009189
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009190 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009191
9192 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009193 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009194
9195 /*
9196 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009197 * Register class AppletTCP
9198 *
9199 */
9200
9201 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009202 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009203
Ilya Shipitsind4259502020-04-08 01:07:56 +05009204 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009205 lua_pushstring(L, "__index");
9206 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009207
9208 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009209 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9210 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9211 hlua_class_function(L, "send", hlua_applet_tcp_send);
9212 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9213 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9214 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9215 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9216 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009217
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009218 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009219
9220 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009221 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009222
9223 /*
9224 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009225 * Register class AppletHTTP
9226 *
9227 */
9228
9229 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009230 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009231
Ilya Shipitsind4259502020-04-08 01:07:56 +05009232 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009233 lua_pushstring(L, "__index");
9234 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009235
9236 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009237 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9238 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9239 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9240 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9241 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9242 hlua_class_function(L, "getline", hlua_applet_http_getline);
9243 hlua_class_function(L, "receive", hlua_applet_http_recv);
9244 hlua_class_function(L, "send", hlua_applet_http_send);
9245 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9246 hlua_class_function(L, "set_status", hlua_applet_http_status);
9247 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009248
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009249 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009250
9251 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009252 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009253
9254 /*
9255 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009256 * Register class TXN
9257 *
9258 */
9259
9260 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009261 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009262
Ilya Shipitsind4259502020-04-08 01:07:56 +05009263 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009264 lua_pushstring(L, "__index");
9265 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009266
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009267 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009268 hlua_class_function(L, "set_priv", hlua_set_priv);
9269 hlua_class_function(L, "get_priv", hlua_get_priv);
9270 hlua_class_function(L, "set_var", hlua_set_var);
9271 hlua_class_function(L, "unset_var", hlua_unset_var);
9272 hlua_class_function(L, "get_var", hlua_get_var);
9273 hlua_class_function(L, "done", hlua_txn_done);
9274 hlua_class_function(L, "reply", hlua_txn_reply_new);
9275 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9276 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9277 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9278 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9279 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9280 hlua_class_function(L, "deflog", hlua_txn_deflog);
9281 hlua_class_function(L, "log", hlua_txn_log);
9282 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9283 hlua_class_function(L, "Info", hlua_txn_log_info);
9284 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9285 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009286
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009287 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009288
9289 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009290 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009291
9292 /*
9293 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009294 * Register class reply
9295 *
9296 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009297 lua_newtable(L);
9298 lua_pushstring(L, "__index");
9299 lua_newtable(L);
9300 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9301 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9302 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9303 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9304 lua_settable(L, -3); /* Sets the __index entry. */
9305 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009306
9307
9308 /*
9309 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009310 * Register class Socket
9311 *
9312 */
9313
9314 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009315 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009316
Ilya Shipitsind4259502020-04-08 01:07:56 +05009317 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009318 lua_pushstring(L, "__index");
9319 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009320
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009321#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009322 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009323#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009324 hlua_class_function(L, "connect", hlua_socket_connect);
9325 hlua_class_function(L, "send", hlua_socket_send);
9326 hlua_class_function(L, "receive", hlua_socket_receive);
9327 hlua_class_function(L, "close", hlua_socket_close);
9328 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9329 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9330 hlua_class_function(L, "setoption", hlua_socket_setoption);
9331 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009332
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009333 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009334
9335 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009336 lua_pushstring(L, "__gc");
9337 lua_pushcclosure(L, hlua_socket_gc, 0);
9338 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009339
9340 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009341 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009342
Thierry Fournieraafc7772020-12-04 11:47:47 +01009343 lua_atpanic(L, hlua_panic_safe);
9344
9345 return L;
9346}
9347
9348void hlua_init(void) {
9349 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009350 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009351#ifdef USE_OPENSSL
9352 struct srv_kw *kw;
9353 int tmp_error;
9354 char *error;
9355 char *args[] = { /* SSL client configuration. */
9356 "ssl",
9357 "verify",
9358 "none",
9359 NULL
9360 };
9361#endif
9362
9363 /* Init post init function list head */
9364 for (i = 0; i < MAX_THREADS + 1; i++)
9365 LIST_INIT(&hlua_init_functions[i]);
9366
9367 /* Init state for common/shared lua parts */
9368 hlua_state_id = 0;
9369 ha_set_tid(0);
9370 hlua_states[0] = hlua_init_state(0);
9371
9372 /* Init state 1 for thread 0. We have at least one thread. */
9373 hlua_state_id = 1;
9374 ha_set_tid(0);
9375 hlua_states[1] = hlua_init_state(1);
9376
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009377 /* Proxy and server configuration initialisation. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009378 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg);
9379 if (!socket_proxy) {
9380 fprintf(stderr, "Lua init: %s\n", errmsg);
9381 exit(1);
9382 }
9383 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009384
9385 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009386 socket_tcp = new_server(socket_proxy);
9387 if (!socket_tcp) {
9388 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9389 exit(1);
9390 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009391
9392#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009393 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009394 socket_ssl = new_server(socket_proxy);
9395 if (!socket_ssl) {
9396 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9397 exit(1);
9398 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009399
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009400 socket_ssl->use_ssl = 1;
9401 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009402
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009403 for (i = 0; args[i] != NULL; i++) {
9404 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009405 /*
9406 *
9407 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009408 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009409 * features like client certificates and ssl_verify.
9410 *
9411 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009412 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009413 if (tmp_error != 0) {
9414 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9415 abort(); /* This must be never arrives because the command line
9416 not editable by the user. */
9417 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009418 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009419 }
9420 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009421#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009422
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009423}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009424
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009425static void hlua_deinit()
9426{
Willy Tarreau186f3762020-12-04 11:48:12 +01009427 int thr;
9428
9429 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9430 if (hlua_states[thr])
9431 lua_close(hlua_states[thr]);
9432 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009433
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009434 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009435
Willy Tarreau0f143af2021-03-05 10:41:48 +01009436#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009437 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009438#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009439
9440 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009441}
9442
9443REGISTER_POST_DEINIT(hlua_deinit);
9444
Willy Tarreau80713382018-11-26 10:19:54 +01009445static void hlua_register_build_options(void)
9446{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009447 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009448
Willy Tarreaubb57d942016-12-21 19:04:56 +01009449 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9450 hap_register_build_opts(ptr, 1);
9451}
Willy Tarreau80713382018-11-26 10:19:54 +01009452
9453INITCALL0(STG_REGISTER, hlua_register_build_options);