blob: 075477d23b331e768c8ebc4a0572ca1e243da037 [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
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100146#define SET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200147 ({ \
148 int ret; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100149 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100150 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 if (setjmp(safe_ljmp_env) != 0) { \
152 lua_atpanic(__L, hlua_panic_safe); \
153 ret = 0; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100154 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100155 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200156 } else { \
157 lua_atpanic(__L, hlua_panic_ljmp); \
158 ret = 1; \
159 } \
160 ret; \
161 })
162
163/* If we are the last function catching Lua errors, we
164 * must reset the panic function.
165 */
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100166#define RESET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200167 do { \
168 lua_atpanic(__L, hlua_panic_safe); \
Thierry Fournier021d9862020-11-28 23:42:03 +0100169 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100170 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200171 } while(0)
172
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100173#define SET_SAFE_LJMP(__HLUA) \
174 SET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
175
176#define RESET_SAFE_LJMP(__HLUA) \
177 RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
178
179#define SET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100180 SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100181
182#define RESET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100183 RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100184
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200185/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200186#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100187/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200189/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100190#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100191#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200192
Thierry Fournierafc63e22020-11-28 17:06:51 +0100193/* The main Lua execution context. The 0 index is the
194 * common state shared by all threads.
195 */
Willy Tarreau186f3762020-12-04 11:48:12 +0100196static lua_State *hlua_states[MAX_THREADS + 1];
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100197
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100198/* This is the memory pool containing struct lua for applets
199 * (including cli).
200 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100201DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100202
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100203/* Used for Socket connection. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +0100204static struct proxy *socket_proxy;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100205static struct server *socket_tcp;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100206#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100207static struct server *socket_ssl;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100208#endif
209
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100210/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100211struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100212
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100213/* The following variables contains the reference of the different
214 * Lua classes. These references are useful for identify metadata
215 * associated with an object.
216 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100217static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100218static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100219static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100220static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100221static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100222static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200223static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200224static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200225static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100226static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100227
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100228/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200229 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100230 * short timeout. Lua linked with tasks doesn't have a timeout
231 * because a task may remain alive during all the haproxy execution.
232 */
233static unsigned int hlua_timeout_session = 4000; /* session timeout. */
234static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200235static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100236
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100237/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
238 * it is used for preventing infinite loops.
239 *
240 * I test the scheer with an infinite loop containing one incrementation
241 * and one test. I run this loop between 10 seconds, I raise a ceil of
242 * 710M loops from one interrupt each 9000 instructions, so I fix the value
243 * to one interrupt each 10 000 instructions.
244 *
245 * configured | Number of
246 * instructions | loops executed
247 * between two | in milions
248 * forced yields |
249 * ---------------+---------------
250 * 10 | 160
251 * 500 | 670
252 * 1000 | 680
253 * 5000 | 700
254 * 7000 | 700
255 * 8000 | 700
256 * 9000 | 710 <- ceil
257 * 10000 | 710
258 * 100000 | 710
259 * 1000000 | 710
260 *
261 */
262static unsigned int hlua_nb_instruction = 10000;
263
Willy Tarreaucdb53462020-12-02 12:12:00 +0100264/* Descriptor for the memory allocation state. The limit is pre-initialised to
265 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
266 * is replaced with ~0 during post_init after everything was loaded. This way
267 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
268 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100269 */
270struct hlua_mem_allocator {
271 size_t allocated;
272 size_t limit;
273};
274
Willy Tarreaucdb53462020-12-02 12:12:00 +0100275static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100276
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100277/* These functions converts types between HAProxy internal args or
278 * sample and LUA types. Another function permits to check if the
279 * LUA stack contains arguments according with an required ARG_T
280 * format.
281 */
282static int hlua_arg2lua(lua_State *L, const struct arg *arg);
283static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100284__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100285 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100286static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100287static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100288static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
289
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100290__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200291
Thierry Fournier59f11be2020-11-29 00:37:41 +0100292struct prepend_path {
293 struct list l;
294 char *type;
295 char *path;
296};
297
298static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
299
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200300#define SEND_ERR(__be, __fmt, __args...) \
301 do { \
302 send_log(__be, LOG_ERR, __fmt, ## __args); \
303 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100304 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200305 } while (0)
306
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100307static inline struct hlua_function *new_hlua_function()
308{
309 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100310 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100311
312 fcn = calloc(1, sizeof(*fcn));
313 if (!fcn)
314 return NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200315 LIST_APPEND(&referenced_functions, &fcn->l);
Thierry Fournierc7492592020-11-28 23:57:24 +0100316 for (i = 0; i < MAX_THREADS + 1; i++)
317 fcn->function_ref[i] = -1;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100318 return fcn;
319}
320
Christopher Fauletdda44442021-04-12 14:05:43 +0200321static inline void release_hlua_function(struct hlua_function *fcn)
322{
323 if (!fcn)
324 return;
325 if (fcn->name)
326 ha_free(&fcn->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200327 LIST_DELETE(&fcn->l);
Christopher Fauletdda44442021-04-12 14:05:43 +0200328 ha_free(&fcn);
329}
330
Thierry Fournierc7492592020-11-28 23:57:24 +0100331/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
332static inline int fcn_ref_to_stack_id(struct hlua_function *fcn)
333{
334 if (fcn->function_ref[0] == -1)
335 return tid + 1;
336 return 0;
337}
338
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100339/* Used to check an Lua function type in the stack. It creates and
340 * returns a reference of the function. This function throws an
341 * error if the rgument is not a "function".
342 */
343__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
344{
345 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100346 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100347 WILL_LJMP(luaL_argerror(L, argno, msg));
348 }
349 lua_pushvalue(L, argno);
350 return luaL_ref(L, LUA_REGISTRYINDEX);
351}
352
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200353/* Return the string that is of the top of the stack. */
354const char *hlua_get_top_error_string(lua_State *L)
355{
356 if (lua_gettop(L) < 1)
357 return "unknown error";
358 if (lua_type(L, -1) != LUA_TSTRING)
359 return "unknown error";
360 return lua_tostring(L, -1);
361}
362
Christopher Fauletd09cc512021-03-24 14:48:45 +0100363__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200364{
365 lua_Debug ar;
366 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200367 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200368
369 while (lua_getstack(L, level++, &ar)) {
370
371 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100372 if (b_data(msg))
373 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200374
375 /* Fill fields:
376 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
377 * 'l': fills in the field currentline;
378 * 'n': fills in the field name and namewhat;
379 * 't': fills in the field istailcall;
380 */
381 lua_getinfo(L, "Slnt", &ar);
382
383 /* Append code localisation */
384 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100385 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200386 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100387 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200388
389 /*
390 * Get function name
391 *
392 * if namewhat is no empty, name is defined.
393 * what contains "Lua" for Lua function, "C" for C function,
394 * or "main" for main code.
395 */
396 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100397 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200398
399 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100400 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200401
402 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100403 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200404
405 else /* nothing left... */
406 chunk_appendf(msg, "?");
407
408
409 /* Display tailed call */
410 if (ar.istailcall)
411 chunk_appendf(msg, " ...");
412 }
413
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200415}
416
417
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100418/* This function check the number of arguments available in the
419 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500420 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100421 */
422__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
423{
424 if (lua_gettop(L) == nb)
425 return;
426 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
427}
428
Mark Lakes22154b42018-01-29 14:38:40 -0800429/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100430 * and the line number where the error is encountered.
431 */
432static int hlua_pusherror(lua_State *L, const char *fmt, ...)
433{
434 va_list argp;
435 va_start(argp, fmt);
436 luaL_where(L, 1);
437 lua_pushvfstring(L, fmt, argp);
438 va_end(argp);
439 lua_concat(L, 2);
440 return 1;
441}
442
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443/* This functions is used with sample fetch and converters. It
444 * converts the HAProxy configuration argument in a lua stack
445 * values.
446 *
447 * It takes an array of "arg", and each entry of the array is
448 * converted and pushed in the LUA stack.
449 */
450static int hlua_arg2lua(lua_State *L, const struct arg *arg)
451{
452 switch (arg->type) {
453 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 case ARGT_TIME:
455 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100456 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200460 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 break;
462
463 case ARGT_IPV4:
464 case ARGT_IPV6:
465 case ARGT_MSK4:
466 case ARGT_MSK6:
467 case ARGT_FE:
468 case ARGT_BE:
469 case ARGT_TAB:
470 case ARGT_SRV:
471 case ARGT_USR:
472 case ARGT_MAP:
473 default:
474 lua_pushnil(L);
475 break;
476 }
477 return 1;
478}
479
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500480/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100481 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500482 * with sample fetch wrappers. The input arguments are given to the
483 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100484 */
485static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
486{
487 switch (lua_type(L, ud)) {
488
489 case LUA_TNUMBER:
490 case LUA_TBOOLEAN:
491 arg->type = ARGT_SINT;
492 arg->data.sint = lua_tointeger(L, ud);
493 break;
494
495 case LUA_TSTRING:
496 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200497 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200498 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200499 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200500 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100501 break;
502
503 case LUA_TUSERDATA:
504 case LUA_TNIL:
505 case LUA_TTABLE:
506 case LUA_TFUNCTION:
507 case LUA_TTHREAD:
508 case LUA_TLIGHTUSERDATA:
509 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200510 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100511 break;
512 }
513 return 1;
514}
515
516/* the following functions are used to convert a struct sample
517 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500518 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100519 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100520static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100521{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200522 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100523 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100524 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200525 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100526 break;
527
528 case SMP_T_BIN:
529 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100531 break;
532
533 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200534 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100535 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
536 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
537 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
538 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
539 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
540 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
541 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
542 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
543 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200544 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100545 break;
546 default:
547 lua_pushnil(L);
548 break;
549 }
550 break;
551
552 case SMP_T_IPV4:
553 case SMP_T_IPV6:
554 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 if (sample_casts[smp->data.type][SMP_T_STR] &&
556 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200557 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100558 else
559 lua_pushnil(L);
560 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 default:
562 lua_pushnil(L);
563 break;
564 }
565 return 1;
566}
567
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100568/* the following functions are used to convert a struct sample
569 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500570 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100571 */
572static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
573{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200574 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100575
576 case SMP_T_BIN:
577 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200578 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100579 break;
580
581 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200582 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100583 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
584 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
585 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
586 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
587 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
588 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
589 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
590 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
591 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200592 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100593 break;
594 default:
595 lua_pushstring(L, "");
596 break;
597 }
598 break;
599
600 case SMP_T_SINT:
601 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100602 case SMP_T_IPV4:
603 case SMP_T_IPV6:
604 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200605 if (sample_casts[smp->data.type][SMP_T_STR] &&
606 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200607 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100608 else
609 lua_pushstring(L, "");
610 break;
611 default:
612 lua_pushstring(L, "");
613 break;
614 }
615 return 1;
616}
617
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100618/* the following functions are used to convert an Lua type in a
619 * struct sample. This is useful to provide data from a converter
620 * to the LUA code.
621 */
622static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
623{
624 switch (lua_type(L, ud)) {
625
626 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200627 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200628 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100629 break;
630
631
632 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200633 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200634 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100635 break;
636
637 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200638 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100639 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200640 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200641 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200642 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200643 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100644 break;
645
646 case LUA_TUSERDATA:
647 case LUA_TNIL:
648 case LUA_TTABLE:
649 case LUA_TFUNCTION:
650 case LUA_TTHREAD:
651 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200652 case LUA_TNONE:
653 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200654 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200655 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100656 break;
657 }
658 return 1;
659}
660
Ilya Shipitsind4259502020-04-08 01:07:56 +0500661/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800662 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100663 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100664 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500665 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100666 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100667 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100668__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100669 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100670{
671 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200672 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100673 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200674 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200675 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200676 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200677 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100678
679 idx = 0;
680 min_arg = ARGM(mask);
681 mask >>= ARGM_BITS;
682
683 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200684 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100685
686 /* Check oversize. */
687 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200688 msg = "Malformed argument mask";
689 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100690 }
691
692 /* Check for mandatory arguments. */
693 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100694 if (idx < min_arg) {
695
696 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200697 if (idx > 0) {
698 msg = "Mandatory argument expected";
699 goto error;
700 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100701
702 /* If first argument have a certain type, some default values
703 * may be used. See the function smp_resolve_args().
704 */
705 switch (mask & ARGT_MASK) {
706
707 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200708 if (!(p->cap & PR_CAP_FE)) {
709 msg = "Mandatory argument expected";
710 goto error;
711 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100712 argp[idx].data.prx = p;
713 argp[idx].type = ARGT_FE;
714 argp[idx+1].type = ARGT_STOP;
715 break;
716
717 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200718 if (!(p->cap & PR_CAP_BE)) {
719 msg = "Mandatory argument expected";
720 goto error;
721 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100722 argp[idx].data.prx = p;
723 argp[idx].type = ARGT_BE;
724 argp[idx+1].type = ARGT_STOP;
725 break;
726
727 case ARGT_TAB:
728 argp[idx].data.prx = p;
729 argp[idx].type = ARGT_TAB;
730 argp[idx+1].type = ARGT_STOP;
731 break;
732
733 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200734 msg = "Mandatory argument expected";
735 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100736 break;
737 }
738 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200739 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100740 }
741
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500742 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100743 if ((mask & ARGT_MASK) == ARGT_STOP &&
744 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200745 msg = "Last argument expected";
746 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100747 }
748
749 if ((mask & ARGT_MASK) == ARGT_STOP &&
750 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200751 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100752 }
753
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200754 /* Convert some argument types. All string in argp[] are for not
755 * duplicated yet.
756 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100757 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100758 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200759 if (argp[idx].type != ARGT_SINT) {
760 msg = "integer expected";
761 goto error;
762 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 argp[idx].type = ARGT_SINT;
764 break;
765
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100766 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200767 if (argp[idx].type != ARGT_SINT) {
768 msg = "integer expected";
769 goto error;
770 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200771 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100772 break;
773
774 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200775 if (argp[idx].type != ARGT_SINT) {
776 msg = "integer expected";
777 goto error;
778 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200779 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100780 break;
781
782 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200783 if (argp[idx].type != ARGT_STR) {
784 msg = "string expected";
785 goto error;
786 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200787 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200788 if (!argp[idx].data.prx) {
789 msg = "frontend doesn't exist";
790 goto error;
791 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100792 argp[idx].type = ARGT_FE;
793 break;
794
795 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200796 if (argp[idx].type != ARGT_STR) {
797 msg = "string expected";
798 goto error;
799 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200800 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200801 if (!argp[idx].data.prx) {
802 msg = "backend doesn't exist";
803 goto error;
804 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100805 argp[idx].type = ARGT_BE;
806 break;
807
808 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200809 if (argp[idx].type != ARGT_STR) {
810 msg = "string expected";
811 goto error;
812 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200813 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200814 if (!argp[idx].data.t) {
815 msg = "table doesn't exist";
816 goto error;
817 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100818 argp[idx].type = ARGT_TAB;
819 break;
820
821 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200822 if (argp[idx].type != ARGT_STR) {
823 msg = "string expected";
824 goto error;
825 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200826 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100827 if (sname) {
828 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200829 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200830 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200831 if (!px) {
832 msg = "backend doesn't exist";
833 goto error;
834 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100835 }
836 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200837 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100838 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100839 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100840 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200841 if (!argp[idx].data.srv) {
842 msg = "server doesn't exist";
843 goto error;
844 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100845 argp[idx].type = ARGT_SRV;
846 break;
847
848 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200849 if (argp[idx].type != ARGT_STR) {
850 msg = "string expected";
851 goto error;
852 }
853 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
854 msg = "invalid IPv4 address";
855 goto error;
856 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100857 argp[idx].type = ARGT_IPV4;
858 break;
859
860 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200861 if (argp[idx].type == ARGT_SINT)
862 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
863 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200864 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
865 msg = "invalid IPv4 mask";
866 goto error;
867 }
868 }
869 else {
870 msg = "integer or string expected";
871 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200872 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100873 argp[idx].type = ARGT_MSK4;
874 break;
875
876 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200877 if (argp[idx].type != ARGT_STR) {
878 msg = "string expected";
879 goto error;
880 }
881 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
882 msg = "invalid IPv6 address";
883 goto error;
884 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100885 argp[idx].type = ARGT_IPV6;
886 break;
887
888 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200889 if (argp[idx].type == ARGT_SINT)
890 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
891 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200892 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
893 msg = "invalid IPv6 mask";
894 goto error;
895 }
896 }
897 else {
898 msg = "integer or string expected";
899 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200900 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100901 argp[idx].type = ARGT_MSK6;
902 break;
903
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200904 case ARGT_REG:
905 if (argp[idx].type != ARGT_STR) {
906 msg = "string expected";
907 goto error;
908 }
909 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
910 if (!reg) {
911 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
912 argp[idx].data.str.area, err);
913 free(err);
914 goto error;
915 }
916 argp[idx].type = ARGT_REG;
917 argp[idx].data.reg = reg;
918 break;
919
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100920 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200921 if (argp[idx].type != ARGT_STR) {
922 msg = "string expected";
923 goto error;
924 }
925 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100926 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200927 ul = p->uri_auth->userlist;
928 else
929 ul = auth_find_userlist(argp[idx].data.str.area);
930
931 if (!ul) {
932 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
933 goto error;
934 }
935 argp[idx].type = ARGT_USR;
936 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200937 break;
938
939 case ARGT_STR:
940 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
941 msg = "unable to duplicate string arg";
942 goto error;
943 }
944 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100945 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200946
Christopher Fauletd25d9262020-08-06 11:04:46 +0200947 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200948 msg = "type not yet supported";
949 goto error;
950 break;
951
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100952 }
953
954 /* Check for type of argument. */
955 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200956 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
957 arg_type_names[(mask & ARGT_MASK)],
958 arg_type_names[argp[idx].type & ARGT_MASK]);
959 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100960 }
961
962 /* Next argument. */
963 mask >>= ARGT_BITS;
964 idx++;
965 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200966 return 0;
967
968 error:
969 for (i = 0; i < idx; i++) {
970 if (argp[i].type == ARGT_STR)
971 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200972 else if (argp[i].type == ARGT_REG)
973 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200974 }
975 WILL_LJMP(luaL_argerror(L, first + idx, msg));
976 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100977}
978
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100979/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500980 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100981 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100982 *
983 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100984 * - hlua_sethlua : create the association between hlua context and lua_state.
985 */
986static inline struct hlua *hlua_gethlua(lua_State *L)
987{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100988 struct hlua **hlua = lua_getextraspace(L);
989 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100990}
991static inline void hlua_sethlua(struct hlua *hlua)
992{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100993 struct hlua **hlua_store = lua_getextraspace(hlua->T);
994 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100995}
996
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100997/* This function is used to send logs. It try to send on screen (stderr)
998 * and on the default syslog server.
999 */
1000static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
1001{
1002 struct tm tm;
1003 char *p;
1004
1005 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001006 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001007 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001008 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001009 /* Break the message if exceed the buffer size. */
1010 *(p-4) = ' ';
1011 *(p-3) = '.';
1012 *(p-2) = '.';
1013 *(p-1) = '.';
1014 break;
1015 }
Willy Tarreau90807112020-02-25 08:16:33 +01001016 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001017 *p = *msg;
1018 else
1019 *p = '.';
1020 }
1021 *p = '\0';
1022
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001023 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001024 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001025 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1026 return;
1027
Willy Tarreaua678b432015-08-28 10:14:59 +02001028 get_localtime(date.tv_sec, &tm);
1029 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001030 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001031 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001032 fflush(stderr);
1033 }
1034}
1035
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001036/* This function just ensure that the yield will be always
1037 * returned with a timeout and permit to set some flags
1038 */
1039__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001040 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001041{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001042 struct hlua *hlua;
1043
1044 /* Get hlua struct, or NULL if we execute from main lua state */
1045 hlua = hlua_gethlua(L);
1046 if (!hlua) {
1047 return;
1048 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001049
1050 /* Set the wake timeout. If timeout is required, we set
1051 * the expiration time.
1052 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001053 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001054
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001055 hlua->flags |= flags;
1056
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001057 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001058 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001059}
1060
Willy Tarreau87b09662015-04-03 00:22:06 +02001061/* This function initialises the Lua environment stored in the stream.
1062 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001063 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001064 *
1065 * This function is particular. it initialises a new Lua thread. If the
1066 * initialisation fails (example: out of memory error), the lua function
1067 * throws an error (longjmp).
1068 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001069 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001070 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001071 * threads appear, the safe environment set a lock to ensure only one
1072 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001073 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001074 *
1075 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001076 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001077 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001078 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001079 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001080 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001081 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001082 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001083int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001084{
1085 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001086 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001087 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001088 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001089 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001090 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001091 if (!already_safe) {
1092 if (!SET_SAFE_LJMP_PARENT(lua)) {
1093 lua->Tref = LUA_REFNIL;
1094 return 0;
1095 }
1096 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001097 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001098 if (!lua->T) {
1099 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001100 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001101 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001102 return 0;
1103 }
1104 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001105 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001106 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001107 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001108 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001109 return 1;
1110}
1111
Willy Tarreau87b09662015-04-03 00:22:06 +02001112/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001113 * is destroyed. The destroy also the memory context. The struct "lua"
1114 * is not freed.
1115 */
1116void hlua_ctx_destroy(struct hlua *lua)
1117{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001118 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001119 return;
1120
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001121 if (!lua->T)
1122 goto end;
1123
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001124 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001125 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001126
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001127 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001128 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001129 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001130 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001131
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001132 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001133 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001134 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001135 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001136 /* Forces a garbage collecting process. If the Lua program is finished
1137 * without error, we run the GC on the thread pointer. Its freed all
1138 * the unused memory.
1139 * If the thread is finnish with an error or is currently yielded,
1140 * it seems that the GC applied on the thread doesn't clean anything,
1141 * so e run the GC on the main thread.
1142 * NOTE: maybe this action locks all the Lua threads untiml the en of
1143 * the garbage collection.
1144 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001145 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001146 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001147 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001148 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001149 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001150 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001151
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001152 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001153
1154end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001155 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001156}
1157
1158/* This function is used to restore the Lua context when a coroutine
1159 * fails. This function copy the common memory between old coroutine
1160 * and the new coroutine. The old coroutine is destroyed, and its
1161 * replaced by the new coroutine.
1162 * If the flag "keep_msg" is set, the last entry of the old is assumed
1163 * as string error message and it is copied in the new stack.
1164 */
1165static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1166{
1167 lua_State *T;
1168 int new_ref;
1169
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001171 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172 if (!T)
1173 return 0;
1174
1175 /* Copy last error message. */
1176 if (keep_msg)
1177 lua_xmove(lua->T, T, 1);
1178
1179 /* Copy data between the coroutines. */
1180 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1181 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001182 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001183
1184 /* Destroy old data. */
1185 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1186
1187 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001188 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001189
1190 /* Fill the struct with the new coroutine values. */
1191 lua->Mref = new_ref;
1192 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001193 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001194
1195 /* Set context. */
1196 hlua_sethlua(lua);
1197
1198 return 1;
1199}
1200
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001201void hlua_hook(lua_State *L, lua_Debug *ar)
1202{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001203 struct hlua *hlua;
1204
1205 /* Get hlua struct, or NULL if we execute from main lua state */
1206 hlua = hlua_gethlua(L);
1207 if (!hlua)
1208 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001209
1210 /* Lua cannot yield when its returning from a function,
1211 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001212 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001213 */
1214 if (lua_gethookmask(L) & LUA_MASKRET) {
1215 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1216 return;
1217 }
1218
1219 /* restore the interrupt condition. */
1220 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1221
1222 /* If we interrupt the Lua processing in yieldable state, we yield.
1223 * If the state is not yieldable, trying yield causes an error.
1224 */
1225 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001226 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001227
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001228 /* If we cannot yield, update the clock and check the timeout. */
1229 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001230 hlua->run_time += now_ms - hlua->start_time;
1231 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001232 lua_pushfstring(L, "execution timeout");
1233 WILL_LJMP(lua_error(L));
1234 }
1235
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001236 /* Update the start time. */
1237 hlua->start_time = now_ms;
1238
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001239 /* Try to interrupt the process at the end of the current
1240 * unyieldable function.
1241 */
1242 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001243}
1244
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001245/* This function start or resumes the Lua stack execution. If the flag
1246 * "yield_allowed" if no set and the LUA stack execution returns a yield
1247 * The function return an error.
1248 *
1249 * The function can returns 4 values:
1250 * - HLUA_E_OK : The execution is terminated without any errors.
1251 * - HLUA_E_AGAIN : The execution must continue at the next associated
1252 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001253 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001254 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001255 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001256 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001257 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001258 * LUA code.
1259 */
1260static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1261{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001262#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1263 int nres;
1264#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001265 int ret;
1266 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001267 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001268
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001269 /* Initialise run time counter. */
1270 if (!HLUA_IS_RUNNING(lua))
1271 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001272
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001273 /* Lock the whole Lua execution. This lock must be before the
1274 * label "resume_execution".
1275 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001276 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001277 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001278
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001279resume_execution:
1280
1281 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1282 * instructions. it is used for preventing infinite loops.
1283 */
1284 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1285
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001286 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001287 HLUA_SET_RUN(lua);
1288 HLUA_CLR_CTRLYIELD(lua);
1289 HLUA_CLR_WAKERESWR(lua);
1290 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001291
Christopher Fauletbc275a92020-02-26 14:55:16 +01001292 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001293 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001294 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001295
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001296 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001297#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001298 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001299#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001300 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001301#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001302 switch (ret) {
1303
1304 case LUA_OK:
1305 ret = HLUA_E_OK;
1306 break;
1307
1308 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001309 /* Check if the execution timeout is expired. It it is the case, we
1310 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001311 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001312 tv_update_date(0, 1);
1313 lua->run_time += now_ms - lua->start_time;
1314 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001315 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001316 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001317 break;
1318 }
1319 /* Process the forced yield. if the general yield is not allowed or
1320 * if no task were associated this the current Lua execution
1321 * coroutine, we resume the execution. Else we want to return in the
1322 * scheduler and we want to be waked up again, to continue the
1323 * current Lua execution. So we schedule our own task.
1324 */
1325 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001326 if (!yield_allowed || !lua->task)
1327 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001328 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001329 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001330 if (!yield_allowed) {
1331 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001332 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001333 break;
1334 }
1335 ret = HLUA_E_AGAIN;
1336 break;
1337
1338 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001339
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001340 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001341 * because the errors ares the only one mean to return immediately
1342 * from and lua execution.
1343 */
1344 if (lua->flags & HLUA_EXIT) {
1345 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001346 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001347 break;
1348 }
1349
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001350 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001351 if (!lua_checkstack(lua->T, 1)) {
1352 ret = HLUA_E_ERR;
1353 break;
1354 }
1355 msg = lua_tostring(lua->T, -1);
1356 lua_settop(lua->T, 0); /* Empty the stack. */
1357 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001358 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001359 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001360 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001361 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001362 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001363 ret = HLUA_E_ERRMSG;
1364 break;
1365
1366 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001367 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001368 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001369 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001370 break;
1371
1372 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001373 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001374 if (!lua_checkstack(lua->T, 1)) {
1375 ret = HLUA_E_ERR;
1376 break;
1377 }
1378 msg = lua_tostring(lua->T, -1);
1379 lua_settop(lua->T, 0); /* Empty the stack. */
1380 lua_pop(lua->T, 1);
1381 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001382 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001383 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001384 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001385 ret = HLUA_E_ERRMSG;
1386 break;
1387
1388 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001389 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001390 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001391 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001392 break;
1393 }
1394
1395 switch (ret) {
1396 case HLUA_E_AGAIN:
1397 break;
1398
1399 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001400 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001401 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001402 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001403 break;
1404
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001405 case HLUA_E_ETMOUT:
1406 case HLUA_E_NOMEM:
1407 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001408 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001409 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001410 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001411 hlua_ctx_renew(lua, 0);
1412 break;
1413
1414 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001415 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001416 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001417 break;
1418 }
1419
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001420 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001421 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001422 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001423
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001424 return ret;
1425}
1426
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001427/* This function exit the current code. */
1428__LJMP static int hlua_done(lua_State *L)
1429{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001430 struct hlua *hlua;
1431
1432 /* Get hlua struct, or NULL if we execute from main lua state */
1433 hlua = hlua_gethlua(L);
1434 if (!hlua)
1435 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001436
1437 hlua->flags |= HLUA_EXIT;
1438 WILL_LJMP(lua_error(L));
1439
1440 return 0;
1441}
1442
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001443/* This function is an LUA binding. It provides a function
1444 * for deleting ACL from a referenced ACL file.
1445 */
1446__LJMP static int hlua_del_acl(lua_State *L)
1447{
1448 const char *name;
1449 const char *key;
1450 struct pat_ref *ref;
1451
1452 MAY_LJMP(check_args(L, 2, "del_acl"));
1453
1454 name = MAY_LJMP(luaL_checkstring(L, 1));
1455 key = MAY_LJMP(luaL_checkstring(L, 2));
1456
1457 ref = pat_ref_lookup(name);
1458 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001459 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001460
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001461 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001462 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001463 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001464 return 0;
1465}
1466
1467/* This function is an LUA binding. It provides a function
1468 * for deleting map entry from a referenced map file.
1469 */
1470static int hlua_del_map(lua_State *L)
1471{
1472 const char *name;
1473 const char *key;
1474 struct pat_ref *ref;
1475
1476 MAY_LJMP(check_args(L, 2, "del_map"));
1477
1478 name = MAY_LJMP(luaL_checkstring(L, 1));
1479 key = MAY_LJMP(luaL_checkstring(L, 2));
1480
1481 ref = pat_ref_lookup(name);
1482 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001483 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001484
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001485 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001486 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001487 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001488 return 0;
1489}
1490
1491/* This function is an LUA binding. It provides a function
1492 * for adding ACL pattern from a referenced ACL file.
1493 */
1494static int hlua_add_acl(lua_State *L)
1495{
1496 const char *name;
1497 const char *key;
1498 struct pat_ref *ref;
1499
1500 MAY_LJMP(check_args(L, 2, "add_acl"));
1501
1502 name = MAY_LJMP(luaL_checkstring(L, 1));
1503 key = MAY_LJMP(luaL_checkstring(L, 2));
1504
1505 ref = pat_ref_lookup(name);
1506 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001507 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001508
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001509 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001510 if (pat_ref_find_elt(ref, key) == NULL)
1511 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001512 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001513 return 0;
1514}
1515
1516/* This function is an LUA binding. It provides a function
1517 * for setting map pattern and sample from a referenced map
1518 * file.
1519 */
1520static int hlua_set_map(lua_State *L)
1521{
1522 const char *name;
1523 const char *key;
1524 const char *value;
1525 struct pat_ref *ref;
1526
1527 MAY_LJMP(check_args(L, 3, "set_map"));
1528
1529 name = MAY_LJMP(luaL_checkstring(L, 1));
1530 key = MAY_LJMP(luaL_checkstring(L, 2));
1531 value = MAY_LJMP(luaL_checkstring(L, 3));
1532
1533 ref = pat_ref_lookup(name);
1534 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001535 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001536
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001537 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001538 if (pat_ref_find_elt(ref, key) != NULL)
1539 pat_ref_set(ref, key, value, NULL);
1540 else
1541 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001542 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001543 return 0;
1544}
1545
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001546/* A class is a lot of memory that contain data. This data can be a table,
1547 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001548 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001549 * the name of the object (_G[<name>] = <metable> ).
1550 *
1551 * A metable is a table that modify the standard behavior of a standard
1552 * access to the associated data. The entries of this new metatable are
1553 * defined as is:
1554 *
1555 * http://lua-users.org/wiki/MetatableEvents
1556 *
1557 * __index
1558 *
1559 * we access an absent field in a table, the result is nil. This is
1560 * true, but it is not the whole truth. Actually, such access triggers
1561 * the interpreter to look for an __index metamethod: If there is no
1562 * such method, as usually happens, then the access results in nil;
1563 * otherwise, the metamethod will provide the result.
1564 *
1565 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1566 * the key does not appear in the table, but the metatable has an __index
1567 * property:
1568 *
1569 * - if the value is a function, the function is called, passing in the
1570 * table and the key; the return value of that function is returned as
1571 * the result.
1572 *
1573 * - if the value is another table, the value of the key in that table is
1574 * asked for and returned (and if it doesn't exist in that table, but that
1575 * table's metatable has an __index property, then it continues on up)
1576 *
1577 * - Use "rawget(myTable,key)" to skip this metamethod.
1578 *
1579 * http://www.lua.org/pil/13.4.1.html
1580 *
1581 * __newindex
1582 *
1583 * Like __index, but control property assignment.
1584 *
1585 * __mode - Control weak references. A string value with one or both
1586 * of the characters 'k' and 'v' which specifies that the the
1587 * keys and/or values in the table are weak references.
1588 *
1589 * __call - Treat a table like a function. When a table is followed by
1590 * parenthesis such as "myTable( 'foo' )" and the metatable has
1591 * a __call key pointing to a function, that function is invoked
1592 * (passing any specified arguments) and the return value is
1593 * returned.
1594 *
1595 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1596 * called, if the metatable for myTable has a __metatable
1597 * key, the value of that key is returned instead of the
1598 * actual metatable.
1599 *
1600 * __tostring - Control string representation. When the builtin
1601 * "tostring( myTable )" function is called, if the metatable
1602 * for myTable has a __tostring property set to a function,
1603 * that function is invoked (passing myTable to it) and the
1604 * return value is used as the string representation.
1605 *
1606 * __len - Control table length. When the table length is requested using
1607 * the length operator ( '#' ), if the metatable for myTable has
1608 * a __len key pointing to a function, that function is invoked
1609 * (passing myTable to it) and the return value used as the value
1610 * of "#myTable".
1611 *
1612 * __gc - Userdata finalizer code. When userdata is set to be garbage
1613 * collected, if the metatable has a __gc field pointing to a
1614 * function, that function is first invoked, passing the userdata
1615 * to it. The __gc metamethod is not called for tables.
1616 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1617 *
1618 * Special metamethods for redefining standard operators:
1619 * http://www.lua.org/pil/13.1.html
1620 *
1621 * __add "+"
1622 * __sub "-"
1623 * __mul "*"
1624 * __div "/"
1625 * __unm "!"
1626 * __pow "^"
1627 * __concat ".."
1628 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001629 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001630 * http://www.lua.org/pil/13.2.html
1631 *
1632 * __eq "=="
1633 * __lt "<"
1634 * __le "<="
1635 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001636
1637/*
1638 *
1639 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001640 * Class Map
1641 *
1642 *
1643 */
1644
1645/* Returns a struct hlua_map if the stack entry "ud" is
1646 * a class session, otherwise it throws an error.
1647 */
1648__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1649{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001650 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001651}
1652
1653/* This function is the map constructor. It don't need
1654 * the class Map object. It creates and return a new Map
1655 * object. It must be called only during "body" or "init"
1656 * context because it process some filesystem accesses.
1657 */
1658__LJMP static int hlua_map_new(struct lua_State *L)
1659{
1660 const char *fn;
1661 int match = PAT_MATCH_STR;
1662 struct sample_conv conv;
1663 const char *file = "";
1664 int line = 0;
1665 lua_Debug ar;
1666 char *err = NULL;
1667 struct arg args[2];
1668
1669 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1670 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1671
1672 fn = MAY_LJMP(luaL_checkstring(L, 1));
1673
1674 if (lua_gettop(L) >= 2) {
1675 match = MAY_LJMP(luaL_checkinteger(L, 2));
1676 if (match < 0 || match >= PAT_MATCH_NUM)
1677 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1678 }
1679
1680 /* Get Lua filename and line number. */
1681 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1682 lua_getinfo(L, "Sl", &ar); /* get info about it */
1683 if (ar.currentline > 0) { /* is there info? */
1684 file = ar.short_src;
1685 line = ar.currentline;
1686 }
1687 }
1688
1689 /* fill fake sample_conv struct. */
1690 conv.kw = ""; /* unused. */
1691 conv.process = NULL; /* unused. */
1692 conv.arg_mask = 0; /* unused. */
1693 conv.val_args = NULL; /* unused. */
1694 conv.out_type = SMP_T_STR;
1695 conv.private = (void *)(long)match;
1696 switch (match) {
1697 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1698 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1699 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1700 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1701 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1702 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1703 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001704 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001705 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1706 default:
1707 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1708 }
1709
1710 /* fill fake args. */
1711 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001712 args[0].data.str.area = strdup(fn);
1713 args[0].data.str.data = strlen(fn);
1714 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001715 args[1].type = ARGT_STOP;
1716
1717 /* load the map. */
1718 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001719 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001720 * free the err variable.
1721 */
1722 luaL_where(L, 1);
1723 lua_pushfstring(L, "'new': %s.", err);
1724 lua_concat(L, 2);
1725 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001726 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001727 WILL_LJMP(lua_error(L));
1728 }
1729
1730 /* create the lua object. */
1731 lua_newtable(L);
1732 lua_pushlightuserdata(L, args[0].data.map);
1733 lua_rawseti(L, -2, 0);
1734
1735 /* Pop a class Map metatable and affect it to the userdata. */
1736 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1737 lua_setmetatable(L, -2);
1738
1739
1740 return 1;
1741}
1742
1743__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1744{
1745 struct map_descriptor *desc;
1746 struct pattern *pat;
1747 struct sample smp;
1748
1749 MAY_LJMP(check_args(L, 2, "lookup"));
1750 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001751 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001752 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001753 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001754 }
1755 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001756 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001757 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001758 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 +01001759 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001760 }
1761
1762 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001763 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001764 if (str)
1765 lua_pushstring(L, "");
1766 else
1767 lua_pushnil(L);
1768 return 1;
1769 }
1770
1771 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001772 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001773 return 1;
1774}
1775
1776__LJMP static int hlua_map_lookup(struct lua_State *L)
1777{
1778 return _hlua_map_lookup(L, 0);
1779}
1780
1781__LJMP static int hlua_map_slookup(struct lua_State *L)
1782{
1783 return _hlua_map_lookup(L, 1);
1784}
1785
1786/*
1787 *
1788 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 * Class Socket
1790 *
1791 *
1792 */
1793
1794__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1795{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001796 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001797}
1798
1799/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001800 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001801 * received.
1802 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001803static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001804{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001805 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001806
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001807 if (appctx->ctx.hlua_cosocket.die) {
1808 si_shutw(si);
1809 si_shutr(si);
1810 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001811 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1812 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001813 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1814 }
1815
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001816 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001817 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001818 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001819
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001820 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001821 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001822 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001823
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001824 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001825 * to be notified whenever the connection completes.
1826 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001827 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001828 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001829 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001830 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001831 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001832 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833
1834 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001835 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001837 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001838 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001839 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840
1841 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001842 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001843 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001844
1845 /* Some data were injected in the buffer, notify the stream
1846 * interface.
1847 */
1848 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001849 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001850
1851 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001852 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001853 */
1854 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001855 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001856}
1857
Willy Tarreau87b09662015-04-03 00:22:06 +02001858/* This function is called when the "struct stream" is destroyed.
1859 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860 * Wake all the pending signals.
1861 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001862static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001863{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001864 struct xref *peer;
1865
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001867 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1868 if (peer)
1869 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001870
1871 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001872 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1873 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874}
1875
1876/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001877 * uses this object. If the stream does not exists, just quit.
1878 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001879 * pending signal can rest in the read and write lists. destroy
1880 * it.
1881 */
1882__LJMP static int hlua_socket_gc(lua_State *L)
1883{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001884 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001886 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001887
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001888 MAY_LJMP(check_args(L, 1, "__gc"));
1889
1890 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001891 peer = xref_get_peer_and_lock(&socket->xref);
1892 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001894 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001896 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001897 appctx->ctx.hlua_cosocket.die = 1;
1898 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001900 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001901 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 return 0;
1903}
1904
1905/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001906 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907 */
sada05ed3302018-05-11 11:48:18 -07001908__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001910 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001912 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001913 struct hlua *hlua;
1914
1915 /* Get hlua struct, or NULL if we execute from main lua state */
1916 hlua = hlua_gethlua(L);
1917 if (!hlua)
1918 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001919
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001920 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001921
1922 /* Check if we run on the same thread than the xreator thread.
1923 * We cannot access to the socket if the thread is different.
1924 */
1925 if (socket->tid != tid)
1926 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1927
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001928 peer = xref_get_peer_and_lock(&socket->xref);
1929 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001931
1932 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001933 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001935 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001936 appctx->ctx.hlua_cosocket.die = 1;
1937 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001938
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001939 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001940 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001941 return 0;
1942}
1943
sada05ed3302018-05-11 11:48:18 -07001944/* The close function calls close_helper.
1945 */
1946__LJMP static int hlua_socket_close(lua_State *L)
1947{
1948 MAY_LJMP(check_args(L, 1, "close"));
1949 return hlua_socket_close_helper(L);
1950}
1951
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001952/* This Lua function assumes that the stack contain three parameters.
1953 * 1 - USERDATA containing a struct socket
1954 * 2 - INTEGER with values of the macro defined below
1955 * If the integer is -1, we must read at most one line.
1956 * If the integer is -2, we ust read all the data until the
1957 * end of the stream.
1958 * If the integer is positive value, we must read a number of
1959 * bytes corresponding to this value.
1960 */
1961#define HLSR_READ_LINE (-1)
1962#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001963__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001964{
1965 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1966 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001967 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001968 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001969 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001970 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001971 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001972 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001973 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001974 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001975 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001976 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001977 struct stream_interface *si;
1978 struct stream *s;
1979 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001980 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001981
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001982 /* Get hlua struct, or NULL if we execute from main lua state */
1983 hlua = hlua_gethlua(L);
1984
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985 /* Check if this lua stack is schedulable. */
1986 if (!hlua || !hlua->task)
1987 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1988 "'frontend', 'backend' or 'task'"));
1989
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001990 /* Check if we run on the same thread than the xreator thread.
1991 * We cannot access to the socket if the thread is different.
1992 */
1993 if (socket->tid != tid)
1994 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1995
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001996 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001997 peer = xref_get_peer_and_lock(&socket->xref);
1998 if (!peer)
1999 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002000 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2001 si = appctx->owner;
2002 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002004 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002006 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002007 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002008 if (nblk < 0) /* Connection close. */
2009 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002010 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002012
2013 /* remove final \r\n. */
2014 if (nblk == 1) {
2015 if (blk1[len1-1] == '\n') {
2016 len1--;
2017 skip_at_end++;
2018 if (blk1[len1-1] == '\r') {
2019 len1--;
2020 skip_at_end++;
2021 }
2022 }
2023 }
2024 else {
2025 if (blk2[len2-1] == '\n') {
2026 len2--;
2027 skip_at_end++;
2028 if (blk2[len2-1] == '\r') {
2029 len2--;
2030 skip_at_end++;
2031 }
2032 }
2033 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034 }
2035
2036 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002038 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002039 if (nblk < 0) /* Connection close. */
2040 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002041 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042 goto connection_empty;
2043 }
2044
2045 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002046 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002047 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 if (nblk < 0) /* Connection close. */
2049 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002050 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051 goto connection_empty;
2052
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002053 missing_bytes = wanted - socket->b.n;
2054 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002055 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002056 len1 = missing_bytes;
2057 } if (nblk == 2 && len1 + len2 > missing_bytes)
2058 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002059 }
2060
2061 len = len1;
2062
2063 luaL_addlstring(&socket->b, blk1, len1);
2064 if (nblk == 2) {
2065 len += len2;
2066 luaL_addlstring(&socket->b, blk2, len2);
2067 }
2068
2069 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002070 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071
2072 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002073 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002074
2075 /* If the pattern reclaim to read all the data
2076 * in the connection, got out.
2077 */
2078 if (wanted == HLSR_READ_ALL)
2079 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002080 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081 goto connection_empty;
2082
2083 /* Return result. */
2084 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002085 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086 return 1;
2087
2088connection_closed:
2089
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002090 xref_unlock(&socket->xref, peer);
2091
2092no_peer:
2093
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002094 /* If the buffer containds data. */
2095 if (socket->b.n > 0) {
2096 luaL_pushresult(&socket->b);
2097 return 1;
2098 }
2099 lua_pushnil(L);
2100 lua_pushstring(L, "connection closed.");
2101 return 2;
2102
2103connection_empty:
2104
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002105 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2106 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002107 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002108 }
2109 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002110 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002111 return 0;
2112}
2113
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002114/* This Lua function gets two parameters. The first one can be string
2115 * or a number. If the string is "*l", the user requires one line. If
2116 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002117 * If the value is a number, the user require a number of bytes equal
2118 * to the value. The default value is "*l" (a line).
2119 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002120 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002121 * integer takes this values:
2122 * -1 : read a line
2123 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002124 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002125 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002126 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127 * concatenated with the read data.
2128 */
2129__LJMP static int hlua_socket_receive(struct lua_State *L)
2130{
2131 int wanted = HLSR_READ_LINE;
2132 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002133 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002134 char *error;
2135 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002136 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137
2138 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2139 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2140
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002141 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002142
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002143 /* Check if we run on the same thread than the xreator thread.
2144 * We cannot access to the socket if the thread is different.
2145 */
2146 if (socket->tid != tid)
2147 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2148
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002149 /* check for pattern. */
2150 if (lua_gettop(L) >= 2) {
2151 type = lua_type(L, 2);
2152 if (type == LUA_TSTRING) {
2153 pattern = lua_tostring(L, 2);
2154 if (strcmp(pattern, "*a") == 0)
2155 wanted = HLSR_READ_ALL;
2156 else if (strcmp(pattern, "*l") == 0)
2157 wanted = HLSR_READ_LINE;
2158 else {
2159 wanted = strtoll(pattern, &error, 10);
2160 if (*error != '\0')
2161 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2162 }
2163 }
2164 else if (type == LUA_TNUMBER) {
2165 wanted = lua_tointeger(L, 2);
2166 if (wanted < 0)
2167 WILL_LJMP(luaL_error(L, "Unsupported size."));
2168 }
2169 }
2170
2171 /* Set pattern. */
2172 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002173
2174 /* Check if we would replace the top by itself. */
2175 if (lua_gettop(L) != 2)
2176 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177
Christopher Fauletc31b2002021-05-03 10:11:13 +02002178 /* Save index of the top of the stack because since buffers are used, it
2179 * may change
2180 */
2181 lastarg = lua_gettop(L);
2182
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002183 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002184 luaL_buffinit(L, &socket->b);
2185
2186 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002187 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002188 if (lua_type(L, 3) != LUA_TSTRING)
2189 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2190 pattern = lua_tolstring(L, 3, &len);
2191 luaL_addlstring(&socket->b, pattern, len);
2192 }
2193
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002194 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002195}
2196
2197/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002198 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002199 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002200static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201{
2202 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002203 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204 struct appctx *appctx;
2205 size_t buf_len;
2206 const char *buf;
2207 int len;
2208 int send_len;
2209 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002210 struct xref *peer;
2211 struct stream_interface *si;
2212 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002213
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002214 /* Get hlua struct, or NULL if we execute from main lua state */
2215 hlua = hlua_gethlua(L);
2216
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002217 /* Check if this lua stack is schedulable. */
2218 if (!hlua || !hlua->task)
2219 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2220 "'frontend', 'backend' or 'task'"));
2221
2222 /* Get object */
2223 socket = MAY_LJMP(hlua_checksocket(L, 1));
2224 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002225 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002226
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002227 /* Check if we run on the same thread than the xreator thread.
2228 * We cannot access to the socket if the thread is different.
2229 */
2230 if (socket->tid != tid)
2231 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2232
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002233 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002234 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002235 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002236 lua_pushinteger(L, -1);
2237 return 1;
2238 }
2239 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2240 si = appctx->owner;
2241 s = si_strm(si);
2242
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002243 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002244 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002245 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002246 lua_pushinteger(L, -1);
2247 return 1;
2248 }
2249
2250 /* Update the input buffer data. */
2251 buf += sent;
2252 send_len = buf_len - sent;
2253
2254 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002255 if (sent >= buf_len) {
2256 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002257 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002258 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002260 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002261 * the request buffer if its not required.
2262 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002263 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002264 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002265 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002266 }
2267
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002268 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002269 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002270 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002272 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002273
2274 /* send data */
2275 if (len < send_len)
2276 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002277 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278
2279 /* "Not enough space" (-1), "Buffer too little to contain
2280 * the data" (-2) are not expected because the available length
2281 * is tested.
2282 * Other unknown error are also not expected.
2283 */
2284 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002285 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002286 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002287
sada05ed3302018-05-11 11:48:18 -07002288 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002290 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002291 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292 return 1;
2293 }
2294
2295 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002296 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002297
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002298 s->req.rex = TICK_ETERNITY;
2299 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300
2301 /* Update length sent. */
2302 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002303 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002304
2305 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 if (sent + len >= buf_len) {
2307 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002309 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310
2311hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002312 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2313 xref_unlock(&socket->xref, peer);
2314 WILL_LJMP(luaL_error(L, "out of memory"));
2315 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002316 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002317 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318 return 0;
2319}
2320
2321/* This function initiate the send of data. It just check the input
2322 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002323 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002324 * "hlua_socket_write_yield" that can yield.
2325 *
2326 * The Lua function gets between 3 and 4 parameters. The first one is
2327 * the associated object. The second is a string buffer. The third is
2328 * a facultative integer that represents where is the buffer position
2329 * of the start of the data that can send. The first byte is the
2330 * position "1". The default value is "1". The fourth argument is a
2331 * facultative integer that represents where is the buffer position
2332 * of the end of the data that can send. The default is the last byte.
2333 */
2334static int hlua_socket_send(struct lua_State *L)
2335{
2336 int i;
2337 int j;
2338 const char *buf;
2339 size_t buf_len;
2340
2341 /* Check number of arguments. */
2342 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2343 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2344
2345 /* Get the string. */
2346 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2347
2348 /* Get and check j. */
2349 if (lua_gettop(L) == 4) {
2350 j = MAY_LJMP(luaL_checkinteger(L, 4));
2351 if (j < 0)
2352 j = buf_len + j + 1;
2353 if (j > buf_len)
2354 j = buf_len + 1;
2355 lua_pop(L, 1);
2356 }
2357 else
2358 j = buf_len;
2359
2360 /* Get and check i. */
2361 if (lua_gettop(L) == 3) {
2362 i = MAY_LJMP(luaL_checkinteger(L, 3));
2363 if (i < 0)
2364 i = buf_len + i + 1;
2365 if (i > buf_len)
2366 i = buf_len + 1;
2367 lua_pop(L, 1);
2368 } else
2369 i = 1;
2370
2371 /* Check bth i and j. */
2372 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002373 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374 return 1;
2375 }
2376 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002377 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 return 1;
2379 }
2380 if (i == 0)
2381 i = 1;
2382 if (j == 0)
2383 j = 1;
2384
2385 /* Pop the string. */
2386 lua_pop(L, 1);
2387
2388 /* Update the buffer length. */
2389 buf += i - 1;
2390 buf_len = j - i + 1;
2391 lua_pushlstring(L, buf, buf_len);
2392
2393 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002394 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002395
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002396 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002397}
2398
Willy Tarreau22b0a682015-06-17 19:43:49 +02002399#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002400__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2401{
2402 static char buffer[SOCKET_INFO_MAX_LEN];
2403 int ret;
2404 int len;
2405 char *p;
2406
2407 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2408 if (ret <= 0) {
2409 lua_pushnil(L);
2410 return 1;
2411 }
2412
2413 if (ret == AF_UNIX) {
2414 lua_pushstring(L, buffer+1);
2415 return 1;
2416 }
2417 else if (ret == AF_INET6) {
2418 buffer[0] = '[';
2419 len = strlen(buffer);
2420 buffer[len] = ']';
2421 len++;
2422 buffer[len] = ':';
2423 len++;
2424 p = buffer;
2425 }
2426 else if (ret == AF_INET) {
2427 p = buffer + 1;
2428 len = strlen(p);
2429 p[len] = ':';
2430 len++;
2431 }
2432 else {
2433 lua_pushnil(L);
2434 return 1;
2435 }
2436
2437 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2438 lua_pushnil(L);
2439 return 1;
2440 }
2441
2442 lua_pushstring(L, p);
2443 return 1;
2444}
2445
2446/* Returns information about the peer of the connection. */
2447__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2448{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002449 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002450 struct xref *peer;
2451 struct appctx *appctx;
2452 struct stream_interface *si;
2453 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002454 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002455
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002456 MAY_LJMP(check_args(L, 1, "getpeername"));
2457
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002458 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002459
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002460 /* Check if we run on the same thread than the xreator thread.
2461 * We cannot access to the socket if the thread is different.
2462 */
2463 if (socket->tid != tid)
2464 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2465
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002466 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002467 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002468 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002469 lua_pushnil(L);
2470 return 1;
2471 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002472 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2473 si = appctx->owner;
2474 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002475
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002476 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002477 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002478 lua_pushnil(L);
2479 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002480 }
2481
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002482 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002483 xref_unlock(&socket->xref, peer);
2484 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002485}
2486
2487/* Returns information about my connection side. */
2488static int hlua_socket_getsockname(struct lua_State *L)
2489{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002490 struct hlua_socket *socket;
2491 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002492 struct appctx *appctx;
2493 struct xref *peer;
2494 struct stream_interface *si;
2495 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002496 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002497
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002498 MAY_LJMP(check_args(L, 1, "getsockname"));
2499
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002500 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002501
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002502 /* Check if we run on the same thread than the xreator thread.
2503 * We cannot access to the socket if the thread is different.
2504 */
2505 if (socket->tid != tid)
2506 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2507
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002508 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002509 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002510 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002511 lua_pushnil(L);
2512 return 1;
2513 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002514 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2515 si = appctx->owner;
2516 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002517
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002518 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002519 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002520 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002521 lua_pushnil(L);
2522 return 1;
2523 }
2524
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002525 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002526 xref_unlock(&socket->xref, peer);
2527 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002528}
2529
2530/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002531static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002532 .obj_type = OBJ_TYPE_APPLET,
2533 .name = "<LUA_TCP>",
2534 .fct = hlua_socket_handler,
2535 .release = hlua_socket_release,
2536};
2537
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002538__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002539{
2540 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002541 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002542 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002543 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002544 struct stream_interface *si;
2545 struct stream *s;
2546
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002547 /* Get hlua struct, or NULL if we execute from main lua state */
2548 hlua = hlua_gethlua(L);
2549 if (!hlua)
2550 return 0;
2551
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002552 /* Check if we run on the same thread than the xreator thread.
2553 * We cannot access to the socket if the thread is different.
2554 */
2555 if (socket->tid != tid)
2556 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2557
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002558 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002559 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002561 lua_pushnil(L);
2562 lua_pushstring(L, "Can't connect");
2563 return 2;
2564 }
2565 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2566 si = appctx->owner;
2567 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002568
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002569 /* Check if we run on the same thread than the xreator thread.
2570 * We cannot access to the socket if the thread is different.
2571 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002572 if (socket->tid != tid) {
2573 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002574 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002575 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002576
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002577 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002578 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002579 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002580 lua_pushnil(L);
2581 lua_pushstring(L, "Can't connect");
2582 return 2;
2583 }
2584
Willy Tarreaue09101e2018-10-16 17:37:12 +02002585 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002586
2587 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002588 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002589 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002590 lua_pushinteger(L, 1);
2591 return 1;
2592 }
2593
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002594 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2595 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002597 }
2598 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002599 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002600 return 0;
2601}
2602
2603/* This function fail or initite the connection. */
2604__LJMP static int hlua_socket_connect(struct lua_State *L)
2605{
2606 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002607 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002608 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002609 struct hlua *hlua;
2610 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002611 int low, high;
2612 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002613 struct xref *peer;
2614 struct stream_interface *si;
2615 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002617 if (lua_gettop(L) < 2)
2618 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002619
2620 /* Get args. */
2621 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002622
2623 /* Check if we run on the same thread than the xreator thread.
2624 * We cannot access to the socket if the thread is different.
2625 */
2626 if (socket->tid != tid)
2627 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2628
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002629 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002630 if (lua_gettop(L) >= 3) {
2631 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002632 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002633
Tim Duesterhus6edab862018-01-06 19:04:45 +01002634 /* Force the ip to end with a colon, to support IPv6 addresses
2635 * that are not enclosed within square brackets.
2636 */
2637 if (port > 0) {
2638 luaL_buffinit(L, &b);
2639 luaL_addstring(&b, ip);
2640 luaL_addchar(&b, ':');
2641 luaL_pushresult(&b);
2642 ip = lua_tolstring(L, lua_gettop(L), NULL);
2643 }
2644 }
2645
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002646 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002647 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002648 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002649 lua_pushnil(L);
2650 return 1;
2651 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652
2653 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002654 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 +02002655 if (!addr) {
2656 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002657 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002658 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002659
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002660 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002661 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002662 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002663 if (port == -1) {
2664 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002665 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002666 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002667 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2668 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002669 if (port == -1) {
2670 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002671 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002672 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002673 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002674 }
2675 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002676
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002677 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2678 si = appctx->owner;
2679 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002680
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002681 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002682 xref_unlock(&socket->xref, peer);
2683 WILL_LJMP(luaL_error(L, "connect: internal error"));
2684 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002685 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002686
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002687 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002688 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002689 if (!hlua)
2690 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002691
2692 /* inform the stream that we want to be notified whenever the
2693 * connection completes.
2694 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002695 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002696 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002697 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002698
Willy Tarreauf31af932020-01-14 09:59:38 +01002699 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002700
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002701 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2702 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002703 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002704 }
2705 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002706
2707 task_wakeup(s->task, TASK_WOKEN_INIT);
2708 /* Return yield waiting for connection. */
2709
Willy Tarreau9635e032018-10-16 17:52:55 +02002710 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002711
2712 return 0;
2713}
2714
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002715#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002716__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2717{
2718 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002719 struct xref *peer;
2720 struct appctx *appctx;
2721 struct stream_interface *si;
2722 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002723
2724 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2725 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002726
2727 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002728 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002729 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002730 lua_pushnil(L);
2731 return 1;
2732 }
2733 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2734 si = appctx->owner;
2735 s = si_strm(si);
2736
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002737 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002738 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002739 return MAY_LJMP(hlua_socket_connect(L));
2740}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002741#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002742
2743__LJMP static int hlua_socket_setoption(struct lua_State *L)
2744{
2745 return 0;
2746}
2747
2748__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2749{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002750 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002751 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002752 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002753 struct xref *peer;
2754 struct appctx *appctx;
2755 struct stream_interface *si;
2756 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002757
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002758 MAY_LJMP(check_args(L, 2, "settimeout"));
2759
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002760 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002761
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002762 /* convert the timeout to millis */
2763 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002764
Thierry Fournier17a921b2018-03-08 09:59:02 +01002765 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002766 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002767 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2768
Mark Lakes56cc1252018-03-27 09:48:06 +02002769 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002770 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002771
2772 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002773 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002774 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002775
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002776 /* Check if we run on the same thread than the xreator thread.
2777 * We cannot access to the socket if the thread is different.
2778 */
2779 if (socket->tid != tid)
2780 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2781
Mark Lakes56cc1252018-03-27 09:48:06 +02002782 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002783 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002784 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002785 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2786 WILL_LJMP(lua_error(L));
2787 return 0;
2788 }
2789 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2790 si = appctx->owner;
2791 s = si_strm(si);
2792
Cyril Bonté7bb63452018-08-17 23:51:02 +02002793 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002794 s->req.rto = tmout;
2795 s->req.wto = tmout;
2796 s->res.rto = tmout;
2797 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002798 s->req.rex = tick_add_ifset(now_ms, tmout);
2799 s->req.wex = tick_add_ifset(now_ms, tmout);
2800 s->res.rex = tick_add_ifset(now_ms, tmout);
2801 s->res.wex = tick_add_ifset(now_ms, tmout);
2802
2803 s->task->expire = tick_add_ifset(now_ms, tmout);
2804 task_queue(s->task);
2805
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002806 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002807
Thierry Fourniere9636f12018-03-08 09:54:32 +01002808 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002809 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002810}
2811
2812__LJMP static int hlua_socket_new(lua_State *L)
2813{
2814 struct hlua_socket *socket;
2815 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002816 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002817 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002818
2819 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002820 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002821 hlua_pusherror(L, "socket: full stack");
2822 goto out_fail_conf;
2823 }
2824
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002825 /* Create the object: obj[0] = userdata. */
2826 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002827 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002828 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002829 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002830 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002831
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002832 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002833 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002834 hlua_pusherror(L, "socket: uninitialized pools.");
2835 goto out_fail_conf;
2836 }
2837
Willy Tarreau87b09662015-04-03 00:22:06 +02002838 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002839 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2840 lua_setmetatable(L, -2);
2841
Willy Tarreaud420a972015-04-06 00:39:18 +02002842 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002843 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002844 if (!appctx) {
2845 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002846 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002847 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002848
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002849 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002850 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002851 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2852 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002853
Willy Tarreaud420a972015-04-06 00:39:18 +02002854 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002855 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002856 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002857 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002858 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002859 }
2860
Christopher Faulet26256f82020-09-14 11:40:13 +02002861 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002862 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002863 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002864 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002865 }
2866
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002867 /* Initialise cross reference between stream and Lua socket object. */
2868 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002869
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002870 /* Configure "right" stream interface. this "si" is used to connect
2871 * and retrieve data from the server. The connection is initialized
2872 * with the "struct server".
2873 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002874 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002875
2876 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002877 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002878 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002879
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002880 return 1;
2881
Willy Tarreaud420a972015-04-06 00:39:18 +02002882 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002883 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002884 out_fail_sess:
2885 appctx_free(appctx);
2886 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002887 WILL_LJMP(lua_error(L));
2888 return 0;
2889}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002890
2891/*
2892 *
2893 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 * Class Channel
2895 *
2896 *
2897 */
2898
2899/* Returns the struct hlua_channel join to the class channel in the
2900 * stack entry "ud" or throws an argument error.
2901 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002902__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002903{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002904 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002905}
2906
Willy Tarreau47860ed2015-03-10 14:07:50 +01002907/* Pushes the channel onto the top of the stack. If the stask does not have a
2908 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002909 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002910static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002913 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 return 0;
2915
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002916 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002917 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002918 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919
2920 /* Pop a class sesison metatable and affect it to the userdata. */
2921 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2922 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002923 return 1;
2924}
2925
2926/* Duplicate all the data present in the input channel and put it
2927 * in a string LUA variables. Returns -1 and push a nil value in
2928 * the stack if the channel is closed and all the data are consumed,
2929 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002930 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002932static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933{
2934 char *blk1;
2935 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002936 size_t len1;
2937 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002938 int ret;
2939 luaL_Buffer b;
2940
Willy Tarreau06d80a92017-10-19 14:32:15 +02002941 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002942 if (unlikely(ret == 0))
2943 return 0;
2944
2945 if (unlikely(ret < 0)) {
2946 lua_pushnil(L);
2947 return -1;
2948 }
2949
2950 luaL_buffinit(L, &b);
2951 luaL_addlstring(&b, blk1, len1);
2952 if (unlikely(ret == 2))
2953 luaL_addlstring(&b, blk2, len2);
2954 luaL_pushresult(&b);
2955
2956 if (unlikely(ret == 2))
2957 return len1 + len2;
2958 return len1;
2959}
2960
2961/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2962 * a yield. This function keep the data in the buffer.
2963 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002964__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002965{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002966 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002967
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002968 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2969
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01002970 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02002971 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002972 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002973 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002974
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002975 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002976 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977 return 1;
2978}
2979
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002980/* Check arguments for the function "hlua_channel_dup_yield". */
2981__LJMP static int hlua_channel_dup(lua_State *L)
2982{
2983 MAY_LJMP(check_args(L, 1, "dup"));
2984 MAY_LJMP(hlua_checkchannel(L, 1));
2985 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2986}
2987
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002988/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2989 * a yield. This function consumes the data in the buffer. It returns
2990 * a string containing the data or a nil pointer if no data are available
2991 * and the channel is closed.
2992 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002993__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002995 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002996 int ret;
2997
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002998 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002999
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003000 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003001 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003002 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003003 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003004
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003005 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003006 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02003007 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003008
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009 if (unlikely(ret == -1))
3010 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003012 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003013 return 1;
3014}
3015
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003016/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003017__LJMP static int hlua_channel_get(lua_State *L)
3018{
3019 MAY_LJMP(check_args(L, 1, "get"));
3020 MAY_LJMP(hlua_checkchannel(L, 1));
3021 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3022}
3023
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024/* This functions consumes and returns one line. If the channel is closed,
3025 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003026 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 * value.
3028 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003029__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 char *blk1;
3032 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003033 size_t len1;
3034 size_t len2;
3035 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003036 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037 int ret;
3038 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003039
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003040 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3041
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003042 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003043 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003044 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003045 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003046
Willy Tarreau06d80a92017-10-19 14:32:15 +02003047 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02003049 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003050
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051 if (ret == -1) {
3052 lua_pushnil(L);
3053 return 1;
3054 }
3055
3056 luaL_buffinit(L, &b);
3057 luaL_addlstring(&b, blk1, len1);
3058 len = len1;
3059 if (unlikely(ret == 2)) {
3060 luaL_addlstring(&b, blk2, len2);
3061 len += len2;
3062 }
3063 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003064 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065 return 1;
3066}
3067
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003068/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003069__LJMP static int hlua_channel_getline(lua_State *L)
3070{
3071 MAY_LJMP(check_args(L, 1, "getline"));
3072 MAY_LJMP(hlua_checkchannel(L, 1));
3073 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3074}
3075
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076/* This function takes a string as input, and append it at the
3077 * input side of channel. If the data is too big, but a space
3078 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003079 * yields. If the data is bigger than the buffer, or if the
3080 * channel is closed, it returns -1. Otherwise, it returns the
3081 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003082 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003083__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003084{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003085 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003086 size_t len;
3087 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3088 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3089 int ret;
3090 int max;
3091
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003092 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003093 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003094 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003095 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003096
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003097 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003098 * the request buffer if its not required.
3099 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003100 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003101 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003102 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003103 }
3104
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003105 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003106 if (max > len - l)
3107 max = len - l;
3108
Willy Tarreau06d80a92017-10-19 14:32:15 +02003109 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003110 if (ret == -2 || ret == -3) {
3111 lua_pushinteger(L, -1);
3112 return 1;
3113 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003114 if (ret == -1) {
3115 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003116 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003117 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003118 l += ret;
3119 lua_pop(L, 1);
3120 lua_pushinteger(L, l);
3121
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003122 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003123 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003124 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003125 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003126 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 */
3128 return 1;
3129 }
3130 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003131 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003132 return 1;
3133}
3134
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003135/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3136 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003137 * buffer size is too little for the data.
3138 */
3139__LJMP static int hlua_channel_append(lua_State *L)
3140{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003141 size_t len;
3142
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003143 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003144 MAY_LJMP(hlua_checkchannel(L, 1));
3145 MAY_LJMP(luaL_checklstring(L, 2, &len));
3146 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003147 lua_pushinteger(L, 0);
3148
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003149 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003150}
3151
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003152/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003153 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003154 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003155 * or -1 if the channel is closed or if the buffer size is too
3156 * little for the data.
3157 */
3158__LJMP static int hlua_channel_set(lua_State *L)
3159{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003160 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003161
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003163 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164 lua_pushinteger(L, 0);
3165
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003166 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003167 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003168 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003169 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003170
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003171 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003173 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003174}
3175
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003176/* Append data in the output side of the buffer. This data is immediately
3177 * sent. The function returns the amount of data written. If the buffer
3178 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179 * if the channel is closed.
3180 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003181__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003182{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003183 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003184 size_t len;
3185 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3186 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3187 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003188 struct hlua *hlua;
3189
3190 /* Get hlua struct, or NULL if we execute from main lua state */
3191 hlua = hlua_gethlua(L);
3192 if (!hlua) {
3193 lua_pushnil(L);
3194 return 1;
3195 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003196
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003197 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003198 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003199 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003200 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003201
Willy Tarreau47860ed2015-03-10 14:07:50 +01003202 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003203 lua_pushinteger(L, -1);
3204 return 1;
3205 }
3206
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003207 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003208 * the request buffer if its not required.
3209 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003210 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003211 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003212 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003213 }
3214
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003215 /* The written data will be immediately sent, so we can check
3216 * the available space without taking in account the reserve.
3217 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003218 * data, because the buffer will be flushed.
3219 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003220 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003221
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003222 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003223 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003224 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003225 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003226 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003227 return 1;
3228
3229 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003230 if (max > len - l)
3231 max = len - l;
3232
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003233 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003234 * detects a non contiguous buffer and realign it.
3235 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003236 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003237 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003238
3239 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003240 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003241
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003242 /* buffer replace considers that the input part is filled.
3243 * so, I must forward these new data in the output part.
3244 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003245 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003246
3247 l += max;
3248 lua_pop(L, 1);
3249 lua_pushinteger(L, l);
3250
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003251 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003252 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003253 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003254 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003255 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003256 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003257 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003258
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003259 if (l < len) {
3260 /* If we are waiting for space in the response buffer, we
3261 * must set the flag WAKERESWR. This flag required the task
3262 * wake up if any activity is detected on the response buffer.
3263 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003264 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003265 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003266 else
3267 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003268 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003269 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003270
3271 return 1;
3272}
3273
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003274/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003275 * yield the LUA process, and resume it without checking the
3276 * input arguments.
3277 */
3278__LJMP static int hlua_channel_send(lua_State *L)
3279{
3280 MAY_LJMP(check_args(L, 2, "send"));
3281 lua_pushinteger(L, 0);
3282
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003283 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003284}
3285
3286/* This function forward and amount of butes. The data pass from
3287 * the input side of the buffer to the output side, and can be
3288 * forwarded. This function never fails.
3289 *
3290 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003291 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003292 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003293__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003294{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003295 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003296 int len;
3297 int l;
3298 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003299 struct hlua *hlua;
3300
3301 /* Get hlua struct, or NULL if we execute from main lua state */
3302 hlua = hlua_gethlua(L);
3303 if (!hlua)
3304 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003305
3306 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003307
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003308 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003309 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003310 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003311 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003312
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003313 len = MAY_LJMP(luaL_checkinteger(L, 2));
3314 l = MAY_LJMP(luaL_checkinteger(L, -1));
3315
3316 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003317 if (max > ci_data(chn))
3318 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003319 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003320 l += max;
3321
3322 lua_pop(L, 1);
3323 lua_pushinteger(L, l);
3324
3325 /* Check if it miss bytes to forward. */
3326 if (l < len) {
3327 /* The the input channel or the output channel are closed, we
3328 * must return the amount of data forwarded.
3329 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003330 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003331 return 1;
3332
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003333 /* If we are waiting for space data in the response buffer, we
3334 * must set the flag WAKERESWR. This flag required the task
3335 * wake up if any activity is detected on the response buffer.
3336 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003337 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003338 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003339 else
3340 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003341
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003342 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003343 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003344 }
3345
3346 return 1;
3347}
3348
3349/* Just check the input and prepare the stack for the previous
3350 * function "hlua_channel_forward_yield"
3351 */
3352__LJMP static int hlua_channel_forward(lua_State *L)
3353{
3354 MAY_LJMP(check_args(L, 2, "forward"));
3355 MAY_LJMP(hlua_checkchannel(L, 1));
3356 MAY_LJMP(luaL_checkinteger(L, 2));
3357
3358 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003359 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003360}
3361
3362/* Just returns the number of bytes available in the input
3363 * side of the buffer. This function never fails.
3364 */
3365__LJMP static int hlua_channel_get_in_len(lua_State *L)
3366{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003367 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003368
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003369 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003370 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003371 if (IS_HTX_STRM(chn_strm(chn))) {
3372 struct htx *htx = htxbuf(&chn->buf);
3373 lua_pushinteger(L, htx->data - co_data(chn));
3374 }
3375 else
3376 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003377 return 1;
3378}
3379
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003380/* Returns true if the channel is full. */
3381__LJMP static int hlua_channel_is_full(lua_State *L)
3382{
3383 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003384
3385 MAY_LJMP(check_args(L, 1, "is_full"));
3386 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003387 /* ignore the reserve, we are not on a producer side (ie in an
3388 * applet).
3389 */
3390 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003391 return 1;
3392}
3393
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003394/* Returns true if the channel is the response channel. */
3395__LJMP static int hlua_channel_is_resp(lua_State *L)
3396{
3397 struct channel *chn;
3398
3399 MAY_LJMP(check_args(L, 1, "is_resp"));
3400 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3401
3402 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3403 return 1;
3404}
3405
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003406/* Just returns the number of bytes available in the output
3407 * side of the buffer. This function never fails.
3408 */
3409__LJMP static int hlua_channel_get_out_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_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003414 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003415 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003416 return 1;
3417}
3418
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003419/*
3420 *
3421 *
3422 * Class Fetches
3423 *
3424 *
3425 */
3426
3427/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003428 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003429 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003430__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003431{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003432 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003433}
3434
3435/* This function creates and push in the stack a fetch object according
3436 * with a current TXN.
3437 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003438static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003439{
Willy Tarreau7073c472015-04-06 11:15:40 +02003440 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003441
3442 /* Check stack size. */
3443 if (!lua_checkstack(L, 3))
3444 return 0;
3445
3446 /* Create the object: obj[0] = userdata.
3447 * Note that the base of the Fetches object is the
3448 * transaction object.
3449 */
3450 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003451 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003452 lua_rawseti(L, -2, 0);
3453
Willy Tarreau7073c472015-04-06 11:15:40 +02003454 hsmp->s = txn->s;
3455 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003456 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003457 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003458
3459 /* Pop a class sesison metatable and affect it to the userdata. */
3460 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3461 lua_setmetatable(L, -2);
3462
3463 return 1;
3464}
3465
3466/* This function is an LUA binding. It is called with each sample-fetch.
3467 * It uses closure argument to store the associated sample-fetch. It
3468 * returns only one argument or throws an error. An error is thrown
3469 * only if an error is encountered during the argument parsing. If
3470 * the "sample-fetch" function fails, nil is returned.
3471 */
3472__LJMP static int hlua_run_sample_fetch(lua_State *L)
3473{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003474 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003475 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003476 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003477 int i;
3478 struct sample smp;
3479
3480 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003481 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003482
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003483 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003484 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003485
Thierry FOURNIERca988662015-12-20 18:43:03 +01003486 /* Check execution authorization. */
3487 if (f->use & SMP_USE_HTTP_ANY &&
3488 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3489 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3490 "is not available in Lua services", f->kw);
3491 WILL_LJMP(lua_error(L));
3492 }
3493
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003494 /* Get extra arguments. */
3495 for (i = 0; i < lua_gettop(L) - 1; i++) {
3496 if (i >= ARGM_NBARGS)
3497 break;
3498 hlua_lua2arg(L, i + 2, &args[i]);
3499 }
3500 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003501 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003502
3503 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003504 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003505
3506 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003507 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003508 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003509 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003510 }
3511
3512 /* Initialise the sample. */
3513 memset(&smp, 0, sizeof(smp));
3514
3515 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003516 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003517 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003518 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003519 lua_pushstring(L, "");
3520 else
3521 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003522 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003523 }
3524
3525 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003526 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003527 hlua_smp2lua_str(L, &smp);
3528 else
3529 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003530
3531 end:
3532 for (i = 0; args[i].type != ARGT_STOP; i++) {
3533 if (args[i].type == ARGT_STR)
3534 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003535 else if (args[i].type == ARGT_REG)
3536 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003537 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003538 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003539
3540 error:
3541 for (i = 0; args[i].type != ARGT_STOP; i++) {
3542 if (args[i].type == ARGT_STR)
3543 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003544 else if (args[i].type == ARGT_REG)
3545 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003546 }
3547 WILL_LJMP(lua_error(L));
3548 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003549}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003550
3551/*
3552 *
3553 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003554 * Class Converters
3555 *
3556 *
3557 */
3558
3559/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003560 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003561 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003562__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003563{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003564 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003565}
3566
3567/* This function creates and push in the stack a Converters object
3568 * according with a current TXN.
3569 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003570static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003571{
Willy Tarreau7073c472015-04-06 11:15:40 +02003572 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003573
3574 /* Check stack size. */
3575 if (!lua_checkstack(L, 3))
3576 return 0;
3577
3578 /* Create the object: obj[0] = userdata.
3579 * Note that the base of the Converters object is the
3580 * same than the TXN object.
3581 */
3582 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003583 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003584 lua_rawseti(L, -2, 0);
3585
Willy Tarreau7073c472015-04-06 11:15:40 +02003586 hsmp->s = txn->s;
3587 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003588 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003589 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003590
Willy Tarreau87b09662015-04-03 00:22:06 +02003591 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003592 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3593 lua_setmetatable(L, -2);
3594
3595 return 1;
3596}
3597
3598/* This function is an LUA binding. It is called with each converter.
3599 * It uses closure argument to store the associated converter. It
3600 * returns only one argument or throws an error. An error is thrown
3601 * only if an error is encountered during the argument parsing. If
3602 * the converter function function fails, nil is returned.
3603 */
3604__LJMP static int hlua_run_sample_conv(lua_State *L)
3605{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003606 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003607 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003608 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003609 int i;
3610 struct sample smp;
3611
3612 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003613 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003614
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003615 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003616 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003617
3618 /* Get extra arguments. */
3619 for (i = 0; i < lua_gettop(L) - 2; i++) {
3620 if (i >= ARGM_NBARGS)
3621 break;
3622 hlua_lua2arg(L, i + 3, &args[i]);
3623 }
3624 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003625 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003626
3627 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003628 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003629
3630 /* Run the special args checker. */
3631 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3632 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003633 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003634 }
3635
3636 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003637 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003638 if (!hlua_lua2smp(L, 2, &smp)) {
3639 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003640 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003641 }
3642
Willy Tarreau1777ea62016-03-10 16:15:46 +01003643 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3644
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003645 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003646 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003647 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003648 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003649 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003650 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003651 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3652 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003653 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003654 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003655 }
3656
3657 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003658 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003659 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003660 lua_pushstring(L, "");
3661 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003662 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003663 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003664 }
3665
3666 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003667 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003668 hlua_smp2lua_str(L, &smp);
3669 else
3670 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003671 end:
3672 for (i = 0; args[i].type != ARGT_STOP; i++) {
3673 if (args[i].type == ARGT_STR)
3674 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003675 else if (args[i].type == ARGT_REG)
3676 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003677 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003678 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003679
3680 error:
3681 for (i = 0; args[i].type != ARGT_STOP; i++) {
3682 if (args[i].type == ARGT_STR)
3683 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003684 else if (args[i].type == ARGT_REG)
3685 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003686 }
3687 WILL_LJMP(lua_error(L));
3688 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003689}
3690
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003691/*
3692 *
3693 *
3694 * Class AppletTCP
3695 *
3696 *
3697 */
3698
3699/* Returns a struct hlua_txn if the stack entry "ud" is
3700 * a class stream, otherwise it throws an error.
3701 */
3702__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3703{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003704 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003705}
3706
3707/* This function creates and push in the stack an Applet object
3708 * according with a current TXN.
3709 */
3710static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3711{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003712 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003713 struct stream_interface *si = ctx->owner;
3714 struct stream *s = si_strm(si);
3715 struct proxy *p = s->be;
3716
3717 /* Check stack size. */
3718 if (!lua_checkstack(L, 3))
3719 return 0;
3720
3721 /* Create the object: obj[0] = userdata.
3722 * Note that the base of the Converters object is the
3723 * same than the TXN object.
3724 */
3725 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003726 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003727 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003728 luactx->appctx = ctx;
3729 luactx->htxn.s = s;
3730 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003731
3732 /* Create the "f" field that contains a list of fetches. */
3733 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003734 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003735 return 0;
3736 lua_settable(L, -3);
3737
3738 /* Create the "sf" field that contains a list of stringsafe fetches. */
3739 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003740 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003741 return 0;
3742 lua_settable(L, -3);
3743
3744 /* Create the "c" field that contains a list of converters. */
3745 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003746 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003747 return 0;
3748 lua_settable(L, -3);
3749
3750 /* Create the "sc" field that contains a list of stringsafe converters. */
3751 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003752 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003753 return 0;
3754 lua_settable(L, -3);
3755
3756 /* Pop a class stream metatable and affect it to the table. */
3757 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3758 lua_setmetatable(L, -2);
3759
3760 return 1;
3761}
3762
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003763__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3764{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003765 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003766 struct stream *s;
3767 const char *name;
3768 size_t len;
3769 struct sample smp;
3770
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003771 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3772 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003773
3774 /* It is useles to retrieve the stream, but this function
3775 * runs only in a stream context.
3776 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003777 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003778 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003779 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003780
3781 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003782 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003783 hlua_lua2smp(L, 3, &smp);
3784
3785 /* Store the sample in a variable. */
3786 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003787
3788 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3789 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3790 else
3791 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3792
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003793 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003794}
3795
3796__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3797{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003798 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003799 struct stream *s;
3800 const char *name;
3801 size_t len;
3802 struct sample smp;
3803
3804 MAY_LJMP(check_args(L, 2, "unset_var"));
3805
3806 /* It is useles to retrieve the stream, but this function
3807 * runs only in a stream context.
3808 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003809 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003810 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003811 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003812
3813 /* Unset the variable. */
3814 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003815 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3816 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003817}
3818
3819__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3820{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003821 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003822 struct stream *s;
3823 const char *name;
3824 size_t len;
3825 struct sample smp;
3826
3827 MAY_LJMP(check_args(L, 2, "get_var"));
3828
3829 /* It is useles to retrieve the stream, but this function
3830 * runs only in a stream context.
3831 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003832 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003833 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003834 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003835
3836 smp_set_owner(&smp, s->be, s->sess, s, 0);
3837 if (!vars_get_by_name(name, len, &smp)) {
3838 lua_pushnil(L);
3839 return 1;
3840 }
3841
3842 return hlua_smp2lua(L, &smp);
3843}
3844
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003845__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3846{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003847 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3848 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003849 struct hlua *hlua;
3850
3851 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003852 if (!s->hlua)
3853 return 0;
3854 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003855
3856 MAY_LJMP(check_args(L, 2, "set_priv"));
3857
3858 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003859 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003860
3861 /* Get and store new value. */
3862 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3863 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3864
3865 return 0;
3866}
3867
3868__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3869{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003870 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3871 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003872 struct hlua *hlua;
3873
3874 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003875 if (!s->hlua) {
3876 lua_pushnil(L);
3877 return 1;
3878 }
3879 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003880
3881 /* Push configuration index in the stack. */
3882 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3883
3884 return 1;
3885}
3886
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003887/* If expected data not yet available, it returns a yield. This function
3888 * consumes the data in the buffer. It returns a string containing the
3889 * data. This string can be empty.
3890 */
3891__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3892{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003893 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3894 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003895 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003896 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003897 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003898 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003899 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003900
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003901 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003902 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003903
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003904 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003905 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003906 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003907 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003908 }
3909
3910 /* End of data: commit the total strings and return. */
3911 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003912 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003913 return 1;
3914 }
3915
3916 /* Ensure that the block 2 length is usable. */
3917 if (ret == 1)
3918 len2 = 0;
3919
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003920 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003921 luaL_addlstring(&luactx->b, blk1, len1);
3922 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003923
3924 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003925 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003926 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003927 return 1;
3928}
3929
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003930/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003931__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3932{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003933 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003934
3935 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003936 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003937
3938 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3939}
3940
3941/* If expected data not yet available, it returns a yield. This function
3942 * consumes the data in the buffer. It returns a string containing the
3943 * data. This string can be empty.
3944 */
3945__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3946{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003947 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3948 struct stream_interface *si = luactx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003949 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003950 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003951 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003952 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003953 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003954 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003955
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003956 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003957 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003958
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003959 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003960 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003961 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003962 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003963 }
3964
3965 /* End of data: commit the total strings and return. */
3966 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003967 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003968 return 1;
3969 }
3970
3971 /* Ensure that the block 2 length is usable. */
3972 if (ret == 1)
3973 len2 = 0;
3974
3975 if (len == -1) {
3976
3977 /* If len == -1, catenate all the data avalaile and
3978 * yield because we want to get all the data until
3979 * the end of data stream.
3980 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003981 luaL_addlstring(&luactx->b, blk1, len1);
3982 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003983 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003984 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003985 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003986
3987 } else {
3988
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003989 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003990 if (len1 > len)
3991 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02003992 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003993 len -= len1;
3994
3995 /* Copy the second block. */
3996 if (len2 > len)
3997 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02003998 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003999 len -= len2;
4000
4001 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004002 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004004 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004005 if (len > 0) {
4006 lua_pushinteger(L, len);
4007 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004008 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004009 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004010 }
4011
4012 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004013 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004014 return 1;
4015 }
4016
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004017 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004018 hlua_pusherror(L, "Lua: internal error");
4019 WILL_LJMP(lua_error(L));
4020 return 0;
4021}
4022
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004023/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004024__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4025{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004026 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004027 int len = -1;
4028
4029 if (lua_gettop(L) > 2)
4030 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4031 if (lua_gettop(L) >= 2) {
4032 len = MAY_LJMP(luaL_checkinteger(L, 2));
4033 lua_pop(L, 1);
4034 }
4035
4036 /* Confirm or set the required length */
4037 lua_pushinteger(L, len);
4038
4039 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004040 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004041
4042 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4043}
4044
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004045/* Append data in the output side of the buffer. This data is immediately
4046 * sent. The function returns the amount of data written. If the buffer
4047 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004048 * if the channel is closed.
4049 */
4050__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4051{
4052 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004053 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004054 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4055 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004056 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004057 struct channel *chn = si_ic(si);
4058 int max;
4059
4060 /* Get the max amount of data which can write as input in the channel. */
4061 max = channel_recv_max(chn);
4062 if (max > (len - l))
4063 max = len - l;
4064
4065 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004066 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004067
4068 /* update counters. */
4069 l += max;
4070 lua_pop(L, 1);
4071 lua_pushinteger(L, l);
4072
4073 /* If some data is not send, declares the situation to the
4074 * applet, and returns a yield.
4075 */
4076 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004077 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004078 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004079 }
4080
4081 return 1;
4082}
4083
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004084/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004085 * yield the LUA process, and resume it without checking the
4086 * input arguments.
4087 */
4088__LJMP static int hlua_applet_tcp_send(lua_State *L)
4089{
4090 MAY_LJMP(check_args(L, 2, "send"));
4091 lua_pushinteger(L, 0);
4092
4093 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4094}
4095
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004096/*
4097 *
4098 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004099 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004100 *
4101 *
4102 */
4103
4104/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004105 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004106 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004107__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004108{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004109 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004110}
4111
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004112/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004113 * according with a current TXN.
4114 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004115static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004116{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004117 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004118 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004119 struct stream_interface *si = ctx->owner;
4120 struct stream *s = si_strm(si);
4121 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004122 struct htx *htx;
4123 struct htx_blk *blk;
4124 struct htx_sl *sl;
4125 struct ist path;
4126 unsigned long long len = 0;
4127 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004128
4129 /* Check stack size. */
4130 if (!lua_checkstack(L, 3))
4131 return 0;
4132
4133 /* Create the object: obj[0] = userdata.
4134 * Note that the base of the Converters object is the
4135 * same than the TXN object.
4136 */
4137 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004138 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004139 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004140 luactx->appctx = ctx;
4141 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4142 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4143 luactx->htxn.s = s;
4144 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004145
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004146 /* Create the "f" field that contains a list of fetches. */
4147 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004148 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004149 return 0;
4150 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004151
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004152 /* Create the "sf" field that contains a list of stringsafe fetches. */
4153 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004154 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004155 return 0;
4156 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004157
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004158 /* Create the "c" field that contains a list of converters. */
4159 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004160 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004161 return 0;
4162 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004163
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004164 /* Create the "sc" field that contains a list of stringsafe converters. */
4165 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004166 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004167 return 0;
4168 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004169
Christopher Fauleta2097962019-07-15 16:25:33 +02004170 htx = htxbuf(&s->req.buf);
4171 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004172 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004173 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004174
Christopher Fauleta2097962019-07-15 16:25:33 +02004175 /* Stores the request method. */
4176 lua_pushstring(L, "method");
4177 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4178 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004179
Christopher Fauleta2097962019-07-15 16:25:33 +02004180 /* Stores the http version. */
4181 lua_pushstring(L, "version");
4182 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4183 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004184
Christopher Fauleta2097962019-07-15 16:25:33 +02004185 /* creates an array of headers. hlua_http_get_headers() crates and push
4186 * the array on the top of the stack.
4187 */
4188 lua_pushstring(L, "headers");
4189 htxn.s = s;
4190 htxn.p = px;
4191 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004192 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004193 return 0;
4194 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004195
Christopher Fauleta2097962019-07-15 16:25:33 +02004196 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004197 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004198 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004199
Christopher Fauleta2097962019-07-15 16:25:33 +02004200 p = path.ptr;
4201 end = path.ptr + path.len;
4202 q = p;
4203 while (q < end && *q != '?')
4204 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004205
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004206 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004207 lua_pushstring(L, "path");
4208 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004209 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004210
Christopher Fauleta2097962019-07-15 16:25:33 +02004211 /* Stores the query string. */
4212 lua_pushstring(L, "qs");
4213 if (*q == '?')
4214 q++;
4215 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004216 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004217 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004218
Christopher Fauleta2097962019-07-15 16:25:33 +02004219 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4220 struct htx_blk *blk = htx_get_blk(htx, pos);
4221 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004222
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004223 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004224 break;
4225 if (type == HTX_BLK_DATA)
4226 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004227 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004228 if (htx->extra != ULLONG_MAX)
4229 len += htx->extra;
4230
4231 /* Stores the request path. */
4232 lua_pushstring(L, "length");
4233 lua_pushinteger(L, len);
4234 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004235
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004236 /* Create an empty array of HTTP request headers. */
4237 lua_pushstring(L, "response");
4238 lua_newtable(L);
4239 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004240
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004241 /* Pop a class stream metatable and affect it to the table. */
4242 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4243 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004244
4245 return 1;
4246}
4247
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004248__LJMP static int hlua_applet_http_set_var(lua_State *L)
4249{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004250 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004251 struct stream *s;
4252 const char *name;
4253 size_t len;
4254 struct sample smp;
4255
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004256 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4257 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004258
4259 /* It is useles to retrieve the stream, but this function
4260 * runs only in a stream context.
4261 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004262 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004263 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004264 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004265
4266 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004267 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004268 hlua_lua2smp(L, 3, &smp);
4269
4270 /* Store the sample in a variable. */
4271 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004272
4273 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4274 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4275 else
4276 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4277
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004278 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004279}
4280
4281__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4282{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004283 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004284 struct stream *s;
4285 const char *name;
4286 size_t len;
4287 struct sample smp;
4288
4289 MAY_LJMP(check_args(L, 2, "unset_var"));
4290
4291 /* It is useles to retrieve the stream, but this function
4292 * runs only in a stream context.
4293 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004294 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004295 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004296 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004297
4298 /* Unset the variable. */
4299 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004300 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4301 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004302}
4303
4304__LJMP static int hlua_applet_http_get_var(lua_State *L)
4305{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004306 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004307 struct stream *s;
4308 const char *name;
4309 size_t len;
4310 struct sample smp;
4311
4312 MAY_LJMP(check_args(L, 2, "get_var"));
4313
4314 /* It is useles to retrieve the stream, but this function
4315 * runs only in a stream context.
4316 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004317 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004318 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004319 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004320
4321 smp_set_owner(&smp, s->be, s->sess, s, 0);
4322 if (!vars_get_by_name(name, len, &smp)) {
4323 lua_pushnil(L);
4324 return 1;
4325 }
4326
4327 return hlua_smp2lua(L, &smp);
4328}
4329
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004330__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4331{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004332 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4333 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004334 struct hlua *hlua;
4335
4336 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004337 if (!s->hlua)
4338 return 0;
4339 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004340
4341 MAY_LJMP(check_args(L, 2, "set_priv"));
4342
4343 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004344 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004345
4346 /* Get and store new value. */
4347 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4348 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4349
4350 return 0;
4351}
4352
4353__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4354{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004355 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4356 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004357 struct hlua *hlua;
4358
4359 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004360 if (!s->hlua) {
4361 lua_pushnil(L);
4362 return 1;
4363 }
4364 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004365
4366 /* Push configuration index in the stack. */
4367 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4368
4369 return 1;
4370}
4371
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004372/* If expected data not yet available, it returns a yield. This function
4373 * consumes the data in the buffer. It returns a string containing the
4374 * data. This string can be empty.
4375 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004376__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004377{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004378 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4379 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004380 struct channel *req = si_oc(si);
4381 struct htx *htx;
4382 struct htx_blk *blk;
4383 size_t count;
4384 int stop = 0;
4385
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004386 htx = htx_from_buf(&req->buf);
4387 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004388 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004389
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004390 while (count && !stop && blk) {
4391 enum htx_blk_type type = htx_get_blk_type(blk);
4392 uint32_t sz = htx_get_blksz(blk);
4393 struct ist v;
4394 uint32_t vlen;
4395 char *nl;
4396
4397 vlen = sz;
4398 if (vlen > count) {
4399 if (type != HTX_BLK_DATA)
4400 break;
4401 vlen = count;
4402 }
4403
4404 switch (type) {
4405 case HTX_BLK_UNUSED:
4406 break;
4407
4408 case HTX_BLK_DATA:
4409 v = htx_get_blk_value(htx, blk);
4410 v.len = vlen;
4411 nl = istchr(v, '\n');
4412 if (nl != NULL) {
4413 stop = 1;
4414 vlen = nl - v.ptr + 1;
4415 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004416 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004417 break;
4418
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004419 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004420 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004421 stop = 1;
4422 break;
4423
4424 default:
4425 break;
4426 }
4427
4428 co_set_data(req, co_data(req) - vlen);
4429 count -= vlen;
4430 if (sz == vlen)
4431 blk = htx_remove_blk(htx, blk);
4432 else {
4433 htx_cut_data_blk(htx, blk, vlen);
4434 break;
4435 }
4436 }
4437
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004438 /* The message was fully consumed and no more data are expected
4439 * (EOM flag set).
4440 */
4441 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4442 stop = 1;
4443
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004444 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004445 if (!stop) {
4446 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004447 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004448 }
4449
4450 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004451 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004452 return 1;
4453}
4454
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004455
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004456/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004457__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004458{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004459 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004460
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004461 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004462 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004463
Christopher Fauleta2097962019-07-15 16:25:33 +02004464 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004465}
4466
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004467/* If expected data not yet available, it returns a yield. This function
4468 * consumes the data in the buffer. It returns a string containing the
4469 * data. This string can be empty.
4470 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004471__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004472{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004473 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4474 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004475 struct channel *req = si_oc(si);
4476 struct htx *htx;
4477 struct htx_blk *blk;
4478 size_t count;
4479 int len;
4480
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004481 htx = htx_from_buf(&req->buf);
4482 len = MAY_LJMP(luaL_checkinteger(L, 2));
4483 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004484 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004485 while (count && len && blk) {
4486 enum htx_blk_type type = htx_get_blk_type(blk);
4487 uint32_t sz = htx_get_blksz(blk);
4488 struct ist v;
4489 uint32_t vlen;
4490
4491 vlen = sz;
4492 if (len > 0 && vlen > len)
4493 vlen = len;
4494 if (vlen > count) {
4495 if (type != HTX_BLK_DATA)
4496 break;
4497 vlen = count;
4498 }
4499
4500 switch (type) {
4501 case HTX_BLK_UNUSED:
4502 break;
4503
4504 case HTX_BLK_DATA:
4505 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004506 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004507 break;
4508
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004509 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004510 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004511 len = 0;
4512 break;
4513
4514 default:
4515 break;
4516 }
4517
4518 co_set_data(req, co_data(req) - vlen);
4519 count -= vlen;
4520 if (len > 0)
4521 len -= vlen;
4522 if (sz == vlen)
4523 blk = htx_remove_blk(htx, blk);
4524 else {
4525 htx_cut_data_blk(htx, blk, vlen);
4526 break;
4527 }
4528 }
4529
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004530 /* The message was fully consumed and no more data are expected
4531 * (EOM flag set).
4532 */
4533 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4534 len = 0;
4535
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004536 htx_to_buf(htx, &req->buf);
4537
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004538 /* If we are no other data available, yield waiting for new data. */
4539 if (len) {
4540 if (len > 0) {
4541 lua_pushinteger(L, len);
4542 lua_replace(L, 2);
4543 }
4544 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004545 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004546 }
4547
4548 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004549 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004550 return 1;
4551}
4552
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004553/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004554__LJMP static int hlua_applet_http_recv(lua_State *L)
4555{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004556 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004557 int len = -1;
4558
4559 /* Check arguments. */
4560 if (lua_gettop(L) > 2)
4561 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4562 if (lua_gettop(L) >= 2) {
4563 len = MAY_LJMP(luaL_checkinteger(L, 2));
4564 lua_pop(L, 1);
4565 }
4566
Christopher Fauleta2097962019-07-15 16:25:33 +02004567 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004568
Christopher Fauleta2097962019-07-15 16:25:33 +02004569 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004570 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004571
Christopher Fauleta2097962019-07-15 16:25:33 +02004572 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004573}
4574
4575/* Append data in the output side of the buffer. This data is immediately
4576 * sent. The function returns the amount of data written. If the buffer
4577 * cannot contain the data, the function yields. The function returns -1
4578 * if the channel is closed.
4579 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004580__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004581{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004582 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4583 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004584 struct channel *res = si_ic(si);
4585 struct htx *htx = htx_from_buf(&res->buf);
4586 const char *data;
4587 size_t len;
4588 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4589 int max;
4590
Christopher Faulet9060fc02019-07-03 11:39:30 +02004591 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004592 if (!max)
4593 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004594
4595 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4596
4597 /* Get the max amount of data which can write as input in the channel. */
4598 if (max > (len - l))
4599 max = len - l;
4600
4601 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004602 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004603 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004604
4605 /* update counters. */
4606 l += max;
4607 lua_pop(L, 1);
4608 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004609
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004610 /* If some data is not send, declares the situation to the
4611 * applet, and returns a yield.
4612 */
4613 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004614 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004615 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004616 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004617 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004618 }
4619
Christopher Fauleta2097962019-07-15 16:25:33 +02004620 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004621 return 1;
4622}
4623
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004624/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004625 * yield the LUA process, and resume it without checking the
4626 * input arguments.
4627 */
4628__LJMP static int hlua_applet_http_send(lua_State *L)
4629{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004630 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004631
4632 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004633 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004634 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4635 WILL_LJMP(lua_error(L));
4636 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004637
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004638 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004639 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004640
Christopher Fauleta2097962019-07-15 16:25:33 +02004641 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004642}
4643
4644__LJMP static int hlua_applet_http_addheader(lua_State *L)
4645{
4646 const char *name;
4647 int ret;
4648
4649 MAY_LJMP(hlua_checkapplet_http(L, 1));
4650 name = MAY_LJMP(luaL_checkstring(L, 2));
4651 MAY_LJMP(luaL_checkstring(L, 3));
4652
4653 /* Push in the stack the "response" entry. */
4654 ret = lua_getfield(L, 1, "response");
4655 if (ret != LUA_TTABLE) {
4656 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4657 "is expected as an array. %s found", lua_typename(L, ret));
4658 WILL_LJMP(lua_error(L));
4659 }
4660
4661 /* check if the header is already registered if it is not
4662 * the case, register it.
4663 */
4664 ret = lua_getfield(L, -1, name);
4665 if (ret == LUA_TNIL) {
4666
4667 /* Entry not found. */
4668 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4669
4670 /* Insert the new header name in the array in the top of the stack.
4671 * It left the new array in the top of the stack.
4672 */
4673 lua_newtable(L);
4674 lua_pushvalue(L, 2);
4675 lua_pushvalue(L, -2);
4676 lua_settable(L, -4);
4677
4678 } else if (ret != LUA_TTABLE) {
4679
4680 /* corruption error. */
4681 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4682 "is expected as an array. %s found", name, lua_typename(L, ret));
4683 WILL_LJMP(lua_error(L));
4684 }
4685
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004686 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004687 * the header value as new entry.
4688 */
4689 lua_pushvalue(L, 3);
4690 ret = lua_rawlen(L, -2);
4691 lua_rawseti(L, -2, ret + 1);
4692 lua_pushboolean(L, 1);
4693 return 1;
4694}
4695
4696__LJMP static int hlua_applet_http_status(lua_State *L)
4697{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004698 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004699 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004700 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004701
4702 if (status < 100 || status > 599) {
4703 lua_pushboolean(L, 0);
4704 return 1;
4705 }
4706
Willy Tarreau7e702d12021-04-28 17:59:21 +02004707 luactx->appctx->ctx.hlua_apphttp.status = status;
4708 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004709 lua_pushboolean(L, 1);
4710 return 1;
4711}
4712
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004713
Christopher Fauleta2097962019-07-15 16:25:33 +02004714__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004715{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004716 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4717 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004718 struct channel *res = si_ic(si);
4719 struct htx *htx;
4720 struct htx_sl *sl;
4721 struct h1m h1m;
4722 const char *status, *reason;
4723 const char *name, *value;
4724 size_t nlen, vlen;
4725 unsigned int flags;
4726
4727 /* Send the message at once. */
4728 htx = htx_from_buf(&res->buf);
4729 h1m_init_res(&h1m);
4730
4731 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004732 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4733 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004734 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02004735 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
4736 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004737 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4738 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4739 }
4740 else {
4741 flags = HTX_SL_F_IS_RESP;
4742 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4743 }
4744 if (!sl) {
4745 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004746 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004747 WILL_LJMP(lua_error(L));
4748 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004749 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004750
4751 /* Get the array associated to the field "response" in the object AppletHTTP. */
4752 lua_pushvalue(L, 0);
4753 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4754 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004755 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004756 WILL_LJMP(lua_error(L));
4757 }
4758
4759 /* Browse the list of headers. */
4760 lua_pushnil(L);
4761 while(lua_next(L, -2) != 0) {
4762 /* We expect a string as -2. */
4763 if (lua_type(L, -2) != LUA_TSTRING) {
4764 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004765 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004766 lua_typename(L, lua_type(L, -2)));
4767 WILL_LJMP(lua_error(L));
4768 }
4769 name = lua_tolstring(L, -2, &nlen);
4770
4771 /* We expect an array as -1. */
4772 if (lua_type(L, -1) != LUA_TTABLE) {
4773 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 +02004774 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004775 name,
4776 lua_typename(L, lua_type(L, -1)));
4777 WILL_LJMP(lua_error(L));
4778 }
4779
4780 /* Browse the table who is on the top of the stack. */
4781 lua_pushnil(L);
4782 while(lua_next(L, -2) != 0) {
4783 int id;
4784
4785 /* We expect a number as -2. */
4786 if (lua_type(L, -2) != LUA_TNUMBER) {
4787 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 +02004788 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004789 name,
4790 lua_typename(L, lua_type(L, -2)));
4791 WILL_LJMP(lua_error(L));
4792 }
4793 id = lua_tointeger(L, -2);
4794
4795 /* We expect a string as -2. */
4796 if (lua_type(L, -1) != LUA_TSTRING) {
4797 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 +02004798 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004799 name, id,
4800 lua_typename(L, lua_type(L, -1)));
4801 WILL_LJMP(lua_error(L));
4802 }
4803 value = lua_tolstring(L, -1, &vlen);
4804
4805 /* Simple Protocol checks. */
4806 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004807 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004808 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4809 struct ist v = ist2(value, vlen);
4810 int ret;
4811
4812 ret = h1_parse_cont_len_header(&h1m, &v);
4813 if (ret < 0) {
4814 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004815 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004816 name);
4817 WILL_LJMP(lua_error(L));
4818 }
4819 else if (ret == 0)
4820 goto next; /* Skip it */
4821 }
4822
4823 /* Add a new header */
4824 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4825 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004826 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004827 name);
4828 WILL_LJMP(lua_error(L));
4829 }
4830 next:
4831 /* Remove the array from the stack, and get next element with a remaining string. */
4832 lua_pop(L, 1);
4833 }
4834
4835 /* Remove the array from the stack, and get next element with a remaining string. */
4836 lua_pop(L, 1);
4837 }
4838
4839 if (h1m.flags & H1_MF_CHNK)
4840 h1m.flags &= ~H1_MF_CLEN;
4841 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4842 h1m.flags |= H1_MF_XFER_LEN;
4843
4844 /* Uset HTX start-line flags */
4845 if (h1m.flags & H1_MF_XFER_ENC)
4846 flags |= HTX_SL_F_XFER_ENC;
4847 if (h1m.flags & H1_MF_XFER_LEN) {
4848 flags |= HTX_SL_F_XFER_LEN;
4849 if (h1m.flags & H1_MF_CHNK)
4850 flags |= HTX_SL_F_CHNK;
4851 else if (h1m.flags & H1_MF_CLEN)
4852 flags |= HTX_SL_F_CLEN;
4853 if (h1m.body_len == 0)
4854 flags |= HTX_SL_F_BODYLESS;
4855 }
4856 sl->flags |= flags;
4857
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004858 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004859 * and the status code implies the presence of a message body, we must
4860 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004861 * for the keepalive compliance. If the applet announces a transfer-encoding
4862 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004863 */
4864 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02004865 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
4866 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
4867 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004868 /* Add a new header */
4869 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4870 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4871 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004872 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004873 WILL_LJMP(lua_error(L));
4874 }
4875 }
4876
4877 /* Finalize headers. */
4878 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4879 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004880 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004881 WILL_LJMP(lua_error(L));
4882 }
4883
4884 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4885 b_reset(&res->buf);
4886 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4887 WILL_LJMP(lua_error(L));
4888 }
4889
4890 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004891 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004892
4893 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004894 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004895 return 0;
4896
4897}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004898/* We will build the status line and the headers of the HTTP response.
4899 * We will try send at once if its not possible, we give back the hand
4900 * waiting for more room.
4901 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004902__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004903{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004904 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4905 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004906 struct channel *res = si_ic(si);
4907
4908 if (co_data(res)) {
4909 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004910 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004911 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004912 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004913}
4914
4915
Christopher Fauleta2097962019-07-15 16:25:33 +02004916__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004917{
Christopher Fauleta2097962019-07-15 16:25:33 +02004918 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004919}
4920
Christopher Fauleta2097962019-07-15 16:25:33 +02004921/*
4922 *
4923 *
4924 * Class HTTP
4925 *
4926 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004927 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004928
4929/* Returns a struct hlua_txn if the stack entry "ud" is
4930 * a class stream, otherwise it throws an error.
4931 */
4932__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004933{
Christopher Fauleta2097962019-07-15 16:25:33 +02004934 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4935}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004936
Christopher Fauleta2097962019-07-15 16:25:33 +02004937/* This function creates and push in the stack a HTTP object
4938 * according with a current TXN.
4939 */
4940static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4941{
4942 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004943
Christopher Fauleta2097962019-07-15 16:25:33 +02004944 /* Check stack size. */
4945 if (!lua_checkstack(L, 3))
4946 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004947
Christopher Fauleta2097962019-07-15 16:25:33 +02004948 /* Create the object: obj[0] = userdata.
4949 * Note that the base of the Converters object is the
4950 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004951 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004952 lua_newtable(L);
4953 htxn = lua_newuserdata(L, sizeof(*htxn));
4954 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004955
4956 htxn->s = txn->s;
4957 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004958 htxn->dir = txn->dir;
4959 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004960
4961 /* Pop a class stream metatable and affect it to the table. */
4962 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4963 lua_setmetatable(L, -2);
4964
4965 return 1;
4966}
4967
4968/* This function creates ans returns an array of HTTP headers.
4969 * This function does not fails. It is used as wrapper with the
4970 * 2 following functions.
4971 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004972__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004973{
Christopher Fauleta2097962019-07-15 16:25:33 +02004974 struct htx *htx;
4975 int32_t pos;
4976
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004977 /* Create the table. */
4978 lua_newtable(L);
4979
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004980
Christopher Fauleta2097962019-07-15 16:25:33 +02004981 htx = htxbuf(&msg->chn->buf);
4982 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4983 struct htx_blk *blk = htx_get_blk(htx, pos);
4984 enum htx_blk_type type = htx_get_blk_type(blk);
4985 struct ist n, v;
4986 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004987
Christopher Fauleta2097962019-07-15 16:25:33 +02004988 if (type == HTX_BLK_HDR) {
4989 n = htx_get_blk_name(htx,blk);
4990 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004991 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004992 else if (type == HTX_BLK_EOH)
4993 break;
4994 else
4995 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004996
Christopher Fauleta2097962019-07-15 16:25:33 +02004997 /* Check for existing entry:
4998 * assume that the table is on the top of the stack, and
4999 * push the key in the stack, the function lua_gettable()
5000 * perform the lookup.
5001 */
5002 lua_pushlstring(L, n.ptr, n.len);
5003 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005004
Christopher Fauleta2097962019-07-15 16:25:33 +02005005 switch (lua_type(L, -1)) {
5006 case LUA_TNIL:
5007 /* Table not found, create it. */
5008 lua_pop(L, 1); /* remove the nil value. */
5009 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5010 lua_newtable(L); /* create and push empty table. */
5011 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5012 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5013 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005014 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005015
Christopher Fauleta2097962019-07-15 16:25:33 +02005016 case LUA_TTABLE:
5017 /* Entry found: push the value in the table. */
5018 len = lua_rawlen(L, -1);
5019 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5020 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5021 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5022 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005023
Christopher Fauleta2097962019-07-15 16:25:33 +02005024 default:
5025 /* Other cases are errors. */
5026 hlua_pusherror(L, "internal error during the parsing of headers.");
5027 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005028 }
5029 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005030 return 1;
5031}
5032
5033__LJMP static int hlua_http_req_get_headers(lua_State *L)
5034{
5035 struct hlua_txn *htxn;
5036
5037 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5038 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5039
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005040 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005041 WILL_LJMP(lua_error(L));
5042
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005043 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005044}
5045
5046__LJMP static int hlua_http_res_get_headers(lua_State *L)
5047{
5048 struct hlua_txn *htxn;
5049
5050 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5051 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5052
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005053 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005054 WILL_LJMP(lua_error(L));
5055
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005056 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005057}
5058
5059/* This function replace full header, or just a value in
5060 * the request or in the response. It is a wrapper fir the
5061 * 4 following functions.
5062 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005063__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005064{
5065 size_t name_len;
5066 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5067 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5068 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005069 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005070 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005071
Dragan Dosen26743032019-04-30 15:54:36 +02005072 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005073 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5074
Christopher Fauleta2097962019-07-15 16:25:33 +02005075 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005076 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005077 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005078 return 0;
5079}
5080
5081__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5082{
5083 struct hlua_txn *htxn;
5084
5085 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
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 Fauletd1914aa2020-02-24 16:52:46 +01005091 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005092}
5093
5094__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5095{
5096 struct hlua_txn *htxn;
5097
5098 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
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 Fauletd1914aa2020-02-24 16:52:46 +01005104 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005105}
5106
5107__LJMP static int hlua_http_req_rep_val(lua_State *L)
5108{
5109 struct hlua_txn *htxn;
5110
5111 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5112 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5113
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005114 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005115 WILL_LJMP(lua_error(L));
5116
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005117 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005118}
5119
5120__LJMP static int hlua_http_res_rep_val(lua_State *L)
5121{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005122 struct hlua_txn *htxn;
5123
5124 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5125 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5126
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005127 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005128 WILL_LJMP(lua_error(L));
5129
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005130 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005131}
5132
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005133/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005134 * It is a wrapper for the 2 following functions.
5135 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005136__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005137{
5138 size_t len;
5139 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005140 struct htx *htx = htxbuf(&msg->chn->buf);
5141 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005142
Christopher Fauleta2097962019-07-15 16:25:33 +02005143 ctx.blk = NULL;
5144 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5145 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005146 return 0;
5147}
5148
5149__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5150{
5151 struct hlua_txn *htxn;
5152
5153 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5154 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5155
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005156 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005157 WILL_LJMP(lua_error(L));
5158
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005159 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005160}
5161
5162__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5163{
5164 struct hlua_txn *htxn;
5165
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005166 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005167 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5168
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005169 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005170 WILL_LJMP(lua_error(L));
5171
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005172 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005173}
5174
5175/* This function adds an header. It is a wrapper used by
5176 * the 2 following functions.
5177 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005178__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005179{
5180 size_t name_len;
5181 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5182 size_t value_len;
5183 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005184 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005185
Christopher Fauleta2097962019-07-15 16:25:33 +02005186 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5187 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005188 return 0;
5189}
5190
5191__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5192{
5193 struct hlua_txn *htxn;
5194
5195 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5196 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5197
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005198 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005199 WILL_LJMP(lua_error(L));
5200
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005201 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005202}
5203
5204__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5205{
5206 struct hlua_txn *htxn;
5207
5208 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5209 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5210
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005211 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005212 WILL_LJMP(lua_error(L));
5213
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005214 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005215}
5216
5217static int hlua_http_req_set_hdr(lua_State *L)
5218{
5219 struct hlua_txn *htxn;
5220
5221 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5222 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5223
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005224 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005225 WILL_LJMP(lua_error(L));
5226
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005227 hlua_http_del_hdr(L, &htxn->s->txn->req);
5228 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005229}
5230
5231static int hlua_http_res_set_hdr(lua_State *L)
5232{
5233 struct hlua_txn *htxn;
5234
5235 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5236 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5237
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005238 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005239 WILL_LJMP(lua_error(L));
5240
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005241 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5242 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005243}
5244
5245/* This function set the method. */
5246static int hlua_http_req_set_meth(lua_State *L)
5247{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005248 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005249 size_t name_len;
5250 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005251
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005252 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005253 WILL_LJMP(lua_error(L));
5254
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005255 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005256 return 1;
5257}
5258
5259/* This function set the method. */
5260static int hlua_http_req_set_path(lua_State *L)
5261{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005262 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005263 size_t name_len;
5264 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005265
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005266 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005267 WILL_LJMP(lua_error(L));
5268
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005269 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005270 return 1;
5271}
5272
5273/* This function set the query-string. */
5274static int hlua_http_req_set_query(lua_State *L)
5275{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005276 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005277 size_t name_len;
5278 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005279
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005280 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005281 WILL_LJMP(lua_error(L));
5282
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005283 /* Check length. */
5284 if (name_len > trash.size - 1) {
5285 lua_pushboolean(L, 0);
5286 return 1;
5287 }
5288
5289 /* Add the mark question as prefix. */
5290 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005291 trash.area[trash.data++] = '?';
5292 memcpy(trash.area + trash.data, name, name_len);
5293 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005294
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005295 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005296 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005297 return 1;
5298}
5299
5300/* This function set the uri. */
5301static int hlua_http_req_set_uri(lua_State *L)
5302{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005303 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005304 size_t name_len;
5305 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005306
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005307 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005308 WILL_LJMP(lua_error(L));
5309
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005310 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005311 return 1;
5312}
5313
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005314/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005315static int hlua_http_res_set_status(lua_State *L)
5316{
5317 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5318 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005319 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5320 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005321
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005322 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005323 WILL_LJMP(lua_error(L));
5324
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005325 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005326 return 0;
5327}
5328
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005329/*
5330 *
5331 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005332 * Class TXN
5333 *
5334 *
5335 */
5336
5337/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005338 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005339 */
5340__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5341{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005342 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005343}
5344
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005345__LJMP static int hlua_set_var(lua_State *L)
5346{
5347 struct hlua_txn *htxn;
5348 const char *name;
5349 size_t len;
5350 struct sample smp;
5351
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005352 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5353 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005354
5355 /* It is useles to retrieve the stream, but this function
5356 * runs only in a stream context.
5357 */
5358 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5359 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5360
5361 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005362 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005363 hlua_lua2smp(L, 3, &smp);
5364
5365 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005366 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005367
5368 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5369 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5370 else
5371 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5372
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005373 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005374}
5375
Christopher Faulet85d79c92016-11-09 16:54:56 +01005376__LJMP static int hlua_unset_var(lua_State *L)
5377{
5378 struct hlua_txn *htxn;
5379 const char *name;
5380 size_t len;
5381 struct sample smp;
5382
5383 MAY_LJMP(check_args(L, 2, "unset_var"));
5384
5385 /* It is useles to retrieve the stream, but this function
5386 * runs only in a stream context.
5387 */
5388 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5389 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5390
5391 /* Unset the variable. */
5392 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005393 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5394 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005395}
5396
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005397__LJMP static int hlua_get_var(lua_State *L)
5398{
5399 struct hlua_txn *htxn;
5400 const char *name;
5401 size_t len;
5402 struct sample smp;
5403
5404 MAY_LJMP(check_args(L, 2, "get_var"));
5405
5406 /* It is useles to retrieve the stream, but this function
5407 * runs only in a stream context.
5408 */
5409 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5410 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5411
Willy Tarreau7560dd42016-03-10 16:28:58 +01005412 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005413 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005414 lua_pushnil(L);
5415 return 1;
5416 }
5417
5418 return hlua_smp2lua(L, &smp);
5419}
5420
Willy Tarreau59551662015-03-10 14:23:13 +01005421__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005422{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005423 struct hlua *hlua;
5424
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005425 MAY_LJMP(check_args(L, 2, "set_priv"));
5426
Willy Tarreau87b09662015-04-03 00:22:06 +02005427 /* It is useles to retrieve the stream, but this function
5428 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005429 */
5430 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005431
5432 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005433 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005434 if (!hlua)
5435 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005436
5437 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005438 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005439
5440 /* Get and store new value. */
5441 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5442 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5443
5444 return 0;
5445}
5446
Willy Tarreau59551662015-03-10 14:23:13 +01005447__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005448{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005449 struct hlua *hlua;
5450
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005451 MAY_LJMP(check_args(L, 1, "get_priv"));
5452
Willy Tarreau87b09662015-04-03 00:22:06 +02005453 /* It is useles to retrieve the stream, but this function
5454 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005455 */
5456 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005457
5458 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005459 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005460 if (!hlua) {
5461 lua_pushnil(L);
5462 return 1;
5463 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005464
5465 /* Push configuration index in the stack. */
5466 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5467
5468 return 1;
5469}
5470
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005471/* Create stack entry containing a class TXN. This function
5472 * return 0 if the stack does not contains free slots,
5473 * otherwise it returns 1.
5474 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005475static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005476{
Willy Tarreaude491382015-04-06 11:04:28 +02005477 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005478
5479 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005480 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005481 return 0;
5482
5483 /* NOTE: The allocation never fails. The failure
5484 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005485 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005486 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005487 /* Create the object: obj[0] = userdata. */
5488 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005489 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005490 lua_rawseti(L, -2, 0);
5491
Willy Tarreaude491382015-04-06 11:04:28 +02005492 htxn->s = s;
5493 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005494 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005495 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005496
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005497 /* Create the "f" field that contains a list of fetches. */
5498 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005499 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005500 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005501 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005502
5503 /* Create the "sf" field that contains a list of stringsafe fetches. */
5504 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005505 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005506 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005507 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005508
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005509 /* Create the "c" field that contains a list of converters. */
5510 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005511 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005512 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005513 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005514
5515 /* Create the "sc" field that contains a list of stringsafe converters. */
5516 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005517 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005518 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005519 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005520
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005521 /* Create the "req" field that contains the request channel object. */
5522 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005523 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005524 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005525 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005526
5527 /* Create the "res" field that contains the response channel object. */
5528 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005529 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005530 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005531 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005532
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005533 /* Creates the HTTP object is the current proxy allows http. */
5534 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005535 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005536 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005537 return 0;
5538 }
5539 else
5540 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005541 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005542
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005543 /* Pop a class sesison metatable and affect it to the userdata. */
5544 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5545 lua_setmetatable(L, -2);
5546
5547 return 1;
5548}
5549
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005550__LJMP static int hlua_txn_deflog(lua_State *L)
5551{
5552 const char *msg;
5553 struct hlua_txn *htxn;
5554
5555 MAY_LJMP(check_args(L, 2, "deflog"));
5556 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5557 msg = MAY_LJMP(luaL_checkstring(L, 2));
5558
5559 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5560 return 0;
5561}
5562
5563__LJMP static int hlua_txn_log(lua_State *L)
5564{
5565 int level;
5566 const char *msg;
5567 struct hlua_txn *htxn;
5568
5569 MAY_LJMP(check_args(L, 3, "log"));
5570 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5571 level = MAY_LJMP(luaL_checkinteger(L, 2));
5572 msg = MAY_LJMP(luaL_checkstring(L, 3));
5573
5574 if (level < 0 || level >= NB_LOG_LEVELS)
5575 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5576
5577 hlua_sendlog(htxn->s->be, level, msg);
5578 return 0;
5579}
5580
5581__LJMP static int hlua_txn_log_debug(lua_State *L)
5582{
5583 const char *msg;
5584 struct hlua_txn *htxn;
5585
5586 MAY_LJMP(check_args(L, 2, "Debug"));
5587 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5588 msg = MAY_LJMP(luaL_checkstring(L, 2));
5589 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5590 return 0;
5591}
5592
5593__LJMP static int hlua_txn_log_info(lua_State *L)
5594{
5595 const char *msg;
5596 struct hlua_txn *htxn;
5597
5598 MAY_LJMP(check_args(L, 2, "Info"));
5599 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5600 msg = MAY_LJMP(luaL_checkstring(L, 2));
5601 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5602 return 0;
5603}
5604
5605__LJMP static int hlua_txn_log_warning(lua_State *L)
5606{
5607 const char *msg;
5608 struct hlua_txn *htxn;
5609
5610 MAY_LJMP(check_args(L, 2, "Warning"));
5611 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5612 msg = MAY_LJMP(luaL_checkstring(L, 2));
5613 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5614 return 0;
5615}
5616
5617__LJMP static int hlua_txn_log_alert(lua_State *L)
5618{
5619 const char *msg;
5620 struct hlua_txn *htxn;
5621
5622 MAY_LJMP(check_args(L, 2, "Alert"));
5623 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5624 msg = MAY_LJMP(luaL_checkstring(L, 2));
5625 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5626 return 0;
5627}
5628
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005629__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5630{
5631 struct hlua_txn *htxn;
5632 int ll;
5633
5634 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5635 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5636 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5637
5638 if (ll < 0 || ll > 7)
5639 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5640
5641 htxn->s->logs.level = ll;
5642 return 0;
5643}
5644
5645__LJMP static int hlua_txn_set_tos(lua_State *L)
5646{
5647 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005648 int tos;
5649
5650 MAY_LJMP(check_args(L, 2, "set_tos"));
5651 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5652 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5653
Willy Tarreau1a18b542018-12-11 16:37:42 +01005654 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005655 return 0;
5656}
5657
5658__LJMP static int hlua_txn_set_mark(lua_State *L)
5659{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005660 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005661 int mark;
5662
5663 MAY_LJMP(check_args(L, 2, "set_mark"));
5664 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5665 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5666
Lukas Tribus579e3e32019-08-11 18:03:45 +02005667 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005668 return 0;
5669}
5670
Patrick Hemmer268a7072018-05-11 12:52:31 -04005671__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5672{
5673 struct hlua_txn *htxn;
5674
5675 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5676 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5677 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5678 return 0;
5679}
5680
5681__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5682{
5683 struct hlua_txn *htxn;
5684
5685 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5686 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5687 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5688 return 0;
5689}
5690
Christopher Faulet700d9e82020-01-31 12:21:52 +01005691/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005692 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005693 * message and terminate the transaction. It returns 1 on success and 0 on
5694 * error. The Reply must be on top of the stack.
5695 */
5696__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5697{
5698 struct htx *htx;
5699 struct htx_sl *sl;
5700 struct h1m h1m;
5701 const char *status, *reason, *body;
5702 size_t status_len, reason_len, body_len;
5703 int ret, code, flags;
5704
5705 code = 200;
5706 status = "200";
5707 status_len = 3;
5708 ret = lua_getfield(L, -1, "status");
5709 if (ret == LUA_TNUMBER) {
5710 code = lua_tointeger(L, -1);
5711 status = lua_tolstring(L, -1, &status_len);
5712 }
5713 lua_pop(L, 1);
5714
5715 reason = http_get_reason(code);
5716 reason_len = strlen(reason);
5717 ret = lua_getfield(L, -1, "reason");
5718 if (ret == LUA_TSTRING)
5719 reason = lua_tolstring(L, -1, &reason_len);
5720 lua_pop(L, 1);
5721
5722 body = NULL;
5723 body_len = 0;
5724 ret = lua_getfield(L, -1, "body");
5725 if (ret == LUA_TSTRING)
5726 body = lua_tolstring(L, -1, &body_len);
5727 lua_pop(L, 1);
5728
5729 /* Prepare the response before inserting the headers */
5730 h1m_init_res(&h1m);
5731 htx = htx_from_buf(&s->res.buf);
5732 channel_htx_truncate(&s->res, htx);
5733 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5734 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5735 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5736 ist2(status, status_len), ist2(reason, reason_len));
5737 }
5738 else {
5739 flags = HTX_SL_F_IS_RESP;
5740 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5741 ist2(status, status_len), ist2(reason, reason_len));
5742 }
5743 if (!sl)
5744 goto fail;
5745 sl->info.res.status = code;
5746
5747 /* Push in the stack the "headers" entry. */
5748 ret = lua_getfield(L, -1, "headers");
5749 if (ret != LUA_TTABLE)
5750 goto skip_headers;
5751
5752 lua_pushnil(L);
5753 while (lua_next(L, -2) != 0) {
5754 struct ist name, value;
5755 const char *n, *v;
5756 size_t nlen, vlen;
5757
5758 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5759 /* Skip element if the key is not a string or if the value is not a table */
5760 goto next_hdr;
5761 }
5762
5763 n = lua_tolstring(L, -2, &nlen);
5764 name = ist2(n, nlen);
5765 if (isteqi(name, ist("content-length"))) {
5766 /* Always skip content-length header. It will be added
5767 * later with the correct len
5768 */
5769 goto next_hdr;
5770 }
5771
5772 /* Loop on header's values */
5773 lua_pushnil(L);
5774 while (lua_next(L, -2)) {
5775 if (!lua_isstring(L, -1)) {
5776 /* Skip the value if it is not a string */
5777 goto next_value;
5778 }
5779
5780 v = lua_tolstring(L, -1, &vlen);
5781 value = ist2(v, vlen);
5782
5783 if (isteqi(name, ist("transfer-encoding")))
5784 h1_parse_xfer_enc_header(&h1m, value);
5785 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5786 goto fail;
5787
5788 next_value:
5789 lua_pop(L, 1);
5790 }
5791
5792 next_hdr:
5793 lua_pop(L, 1);
5794 }
5795 skip_headers:
5796 lua_pop(L, 1);
5797
5798 /* Update h1m flags: CLEN is set if CHNK is not present */
5799 if (!(h1m.flags & H1_MF_CHNK)) {
5800 const char *clen = ultoa(body_len);
5801
5802 h1m.flags |= H1_MF_CLEN;
5803 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5804 goto fail;
5805 }
5806 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5807 h1m.flags |= H1_MF_XFER_LEN;
5808
5809 /* Update HTX start-line flags */
5810 if (h1m.flags & H1_MF_XFER_ENC)
5811 flags |= HTX_SL_F_XFER_ENC;
5812 if (h1m.flags & H1_MF_XFER_LEN) {
5813 flags |= HTX_SL_F_XFER_LEN;
5814 if (h1m.flags & H1_MF_CHNK)
5815 flags |= HTX_SL_F_CHNK;
5816 else if (h1m.flags & H1_MF_CLEN)
5817 flags |= HTX_SL_F_CLEN;
5818 if (h1m.body_len == 0)
5819 flags |= HTX_SL_F_BODYLESS;
5820 }
5821 sl->flags |= flags;
5822
5823
5824 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005825 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005826 goto fail;
5827
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005828 htx->flags |= HTX_FL_EOM;
5829
Christopher Faulet700d9e82020-01-31 12:21:52 +01005830 /* Now, forward the response and terminate the transaction */
5831 s->txn->status = code;
5832 htx_to_buf(htx, &s->res.buf);
5833 if (!http_forward_proxy_resp(s, 1))
5834 goto fail;
5835
5836 return 1;
5837
5838 fail:
5839 channel_htx_truncate(&s->res, htx);
5840 return 0;
5841}
5842
5843/* Terminate a transaction if called from a lua action. For TCP streams,
5844 * processing is just aborted. Nothing is returned to the client and all
5845 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5846 * is forwarded to the client before terminating the transaction. On success,
5847 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5848 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5849 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005850 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005851__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005852{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005853 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005854 struct stream *s;
5855 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005856
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005857 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005858
Christopher Faulet700d9e82020-01-31 12:21:52 +01005859 /* If the flags NOTERM is set, we cannot terminate the session, so we
5860 * just end the execution of the current lua code. */
5861 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005862 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005863
Christopher Faulet700d9e82020-01-31 12:21:52 +01005864 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005865 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005866 struct channel *req = &s->req;
5867 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005868
Christopher Faulet700d9e82020-01-31 12:21:52 +01005869 channel_auto_read(req);
5870 channel_abort(req);
5871 channel_auto_close(req);
5872 channel_erase(req);
5873
5874 res->wex = tick_add_ifset(now_ms, res->wto);
5875 channel_auto_read(res);
5876 channel_auto_close(res);
5877 channel_shutr_now(res);
5878
5879 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5880 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005881 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005882
Christopher Faulet700d9e82020-01-31 12:21:52 +01005883 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5884 /* No reply or invalid reply */
5885 s->txn->status = 0;
5886 http_reply_and_close(s, 0, NULL);
5887 }
5888 else {
5889 /* Remove extra args to have the reply on top of the stack */
5890 if (lua_gettop(L) > 2)
5891 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005892
Christopher Faulet700d9e82020-01-31 12:21:52 +01005893 if (!hlua_txn_forward_reply(L, s)) {
5894 if (!(s->flags & SF_ERR_MASK))
5895 s->flags |= SF_ERR_PRXCOND;
5896 lua_pushinteger(L, ACT_RET_ERR);
5897 WILL_LJMP(hlua_done(L));
5898 return 0; /* Never reached */
5899 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005900 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005901
Christopher Faulet700d9e82020-01-31 12:21:52 +01005902 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5903 if (htxn->dir == SMP_OPT_DIR_REQ) {
5904 /* let's log the request time */
5905 s->logs.tv_request = now;
5906 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02005907 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01005908 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005909
Christopher Faulet700d9e82020-01-31 12:21:52 +01005910 done:
5911 if (!(s->flags & SF_ERR_MASK))
5912 s->flags |= SF_ERR_LOCAL;
5913 if (!(s->flags & SF_FINST_MASK))
5914 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005915
Christopher Faulet4ad73102020-03-05 11:07:31 +01005916 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005917 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005918 return 0;
5919}
5920
Christopher Faulet700d9e82020-01-31 12:21:52 +01005921/*
5922 *
5923 *
5924 * Class REPLY
5925 *
5926 *
5927 */
5928
5929/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5930 * free slots, the function fails and returns 0;
5931 */
5932static int hlua_txn_reply_new(lua_State *L)
5933{
5934 struct hlua_txn *htxn;
5935 const char *reason, *body = NULL;
5936 int ret, status;
5937
5938 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005939 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005940 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5941 WILL_LJMP(lua_error(L));
5942 }
5943
5944 /* Default value */
5945 status = 200;
5946 reason = http_get_reason(status);
5947
5948 if (lua_istable(L, 2)) {
5949 /* load status and reason from the table argument at index 2 */
5950 ret = lua_getfield(L, 2, "status");
5951 if (ret == LUA_TNIL)
5952 goto reason;
5953 else if (ret != LUA_TNUMBER) {
5954 /* invalid status: ignore the reason */
5955 goto body;
5956 }
5957 status = lua_tointeger(L, -1);
5958
5959 reason:
5960 lua_pop(L, 1); /* restore the stack: remove status */
5961 ret = lua_getfield(L, 2, "reason");
5962 if (ret == LUA_TSTRING)
5963 reason = lua_tostring(L, -1);
5964
5965 body:
5966 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5967 ret = lua_getfield(L, 2, "body");
5968 if (ret == LUA_TSTRING)
5969 body = lua_tostring(L, -1);
5970 lua_pop(L, 1); /* restore the stack: remove body */
5971 }
5972
5973 /* Create the Reply table */
5974 lua_newtable(L);
5975
5976 /* Add status element */
5977 lua_pushstring(L, "status");
5978 lua_pushinteger(L, status);
5979 lua_settable(L, -3);
5980
5981 /* Add reason element */
5982 reason = http_get_reason(status);
5983 lua_pushstring(L, "reason");
5984 lua_pushstring(L, reason);
5985 lua_settable(L, -3);
5986
5987 /* Add body element, nil if undefined */
5988 lua_pushstring(L, "body");
5989 if (body)
5990 lua_pushstring(L, body);
5991 else
5992 lua_pushnil(L);
5993 lua_settable(L, -3);
5994
5995 /* Add headers element */
5996 lua_pushstring(L, "headers");
5997 lua_newtable(L);
5998
5999 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6000 if (lua_istable(L, 2)) {
6001 /* load headers from the table argument at index 2. If it is a table, copy it. */
6002 ret = lua_getfield(L, 2, "headers");
6003 if (ret == LUA_TTABLE) {
6004 /* stack: [ ... <headers:table>, <table> ] */
6005 lua_pushnil(L);
6006 while (lua_next(L, -2) != 0) {
6007 /* stack: [ ... <headers:table>, <table>, k, v] */
6008 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
6009 /* invalid value type, skip it */
6010 lua_pop(L, 1);
6011 continue;
6012 }
6013
6014
6015 /* Duplicate the key and swap it with the value. */
6016 lua_pushvalue(L, -2);
6017 lua_insert(L, -2);
6018 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6019
6020 lua_newtable(L);
6021 lua_insert(L, -2);
6022 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6023
6024 if (lua_isstring(L, -1)) {
6025 /* push the value in the inner table */
6026 lua_rawseti(L, -2, 1);
6027 }
6028 else { /* table */
6029 lua_pushnil(L);
6030 while (lua_next(L, -2) != 0) {
6031 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6032 if (!lua_isstring(L, -1)) {
6033 /* invalid value type, skip it*/
6034 lua_pop(L, 1);
6035 continue;
6036 }
6037 /* push the value in the inner table */
6038 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6039 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6040 }
6041 lua_pop(L, 1);
6042 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6043 }
6044
6045 /* push (k,v) on the stack in the headers table:
6046 * stack: [ ... <headers:table>, <table>, k, k, v ]
6047 */
6048 lua_settable(L, -5);
6049 /* stack: [ ... <headers:table>, <table>, k ] */
6050 }
6051 }
6052 lua_pop(L, 1);
6053 }
6054 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6055 lua_settable(L, -3);
6056 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6057
6058 /* Pop a class sesison metatable and affect it to the userdata. */
6059 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6060 lua_setmetatable(L, -2);
6061 return 1;
6062}
6063
6064/* Set the reply status code, and optionally the reason. If no reason is
6065 * provided, the default one corresponding to the status code is used.
6066 */
6067__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6068{
6069 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6070 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6071
6072 /* First argument (self) must be a table */
6073 luaL_checktype(L, 1, LUA_TTABLE);
6074
6075 if (status < 100 || status > 599) {
6076 lua_pushboolean(L, 0);
6077 return 1;
6078 }
6079 if (!reason)
6080 reason = http_get_reason(status);
6081
6082 lua_pushinteger(L, status);
6083 lua_setfield(L, 1, "status");
6084
6085 lua_pushstring(L, reason);
6086 lua_setfield(L, 1, "reason");
6087
6088 lua_pushboolean(L, 1);
6089 return 1;
6090}
6091
6092/* Add a header into the reply object. Each header name is associated to an
6093 * array of values in the "headers" table. If the header name is not found, a
6094 * new entry is created.
6095 */
6096__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6097{
6098 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6099 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6100 int ret;
6101
6102 /* First argument (self) must be a table */
6103 luaL_checktype(L, 1, LUA_TTABLE);
6104
6105 /* Push in the stack the "headers" entry. */
6106 ret = lua_getfield(L, 1, "headers");
6107 if (ret != LUA_TTABLE) {
6108 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6109 WILL_LJMP(lua_error(L));
6110 }
6111
6112 /* check if the header is already registered. If not, register it. */
6113 ret = lua_getfield(L, -1, name);
6114 if (ret == LUA_TNIL) {
6115 /* Entry not found. */
6116 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6117
6118 /* Insert the new header name in the array in the top of the stack.
6119 * It left the new array in the top of the stack.
6120 */
6121 lua_newtable(L);
6122 lua_pushstring(L, name);
6123 lua_pushvalue(L, -2);
6124 lua_settable(L, -4);
6125 }
6126 else if (ret != LUA_TTABLE) {
6127 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6128 WILL_LJMP(lua_error(L));
6129 }
6130
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006131 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006132 * the header value as new entry.
6133 */
6134 lua_pushstring(L, value);
6135 ret = lua_rawlen(L, -2);
6136 lua_rawseti(L, -2, ret + 1);
6137
6138 lua_pushboolean(L, 1);
6139 return 1;
6140}
6141
6142/* Remove all occurrences of a given header name. */
6143__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6144{
6145 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6146 int ret;
6147
6148 /* First argument (self) must be a table */
6149 luaL_checktype(L, 1, LUA_TTABLE);
6150
6151 /* Push in the stack the "headers" entry. */
6152 ret = lua_getfield(L, 1, "headers");
6153 if (ret != LUA_TTABLE) {
6154 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6155 WILL_LJMP(lua_error(L));
6156 }
6157
6158 lua_pushstring(L, name);
6159 lua_pushnil(L);
6160 lua_settable(L, -3);
6161
6162 lua_pushboolean(L, 1);
6163 return 1;
6164}
6165
6166/* Set the reply's body. Overwrite any existing entry. */
6167__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6168{
6169 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6170
6171 /* First argument (self) must be a table */
6172 luaL_checktype(L, 1, LUA_TTABLE);
6173
6174 lua_pushstring(L, payload);
6175 lua_setfield(L, 1, "body");
6176
6177 lua_pushboolean(L, 1);
6178 return 1;
6179}
6180
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006181__LJMP static int hlua_log(lua_State *L)
6182{
6183 int level;
6184 const char *msg;
6185
6186 MAY_LJMP(check_args(L, 2, "log"));
6187 level = MAY_LJMP(luaL_checkinteger(L, 1));
6188 msg = MAY_LJMP(luaL_checkstring(L, 2));
6189
6190 if (level < 0 || level >= NB_LOG_LEVELS)
6191 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6192
6193 hlua_sendlog(NULL, level, msg);
6194 return 0;
6195}
6196
6197__LJMP static int hlua_log_debug(lua_State *L)
6198{
6199 const char *msg;
6200
6201 MAY_LJMP(check_args(L, 1, "debug"));
6202 msg = MAY_LJMP(luaL_checkstring(L, 1));
6203 hlua_sendlog(NULL, LOG_DEBUG, msg);
6204 return 0;
6205}
6206
6207__LJMP static int hlua_log_info(lua_State *L)
6208{
6209 const char *msg;
6210
6211 MAY_LJMP(check_args(L, 1, "info"));
6212 msg = MAY_LJMP(luaL_checkstring(L, 1));
6213 hlua_sendlog(NULL, LOG_INFO, msg);
6214 return 0;
6215}
6216
6217__LJMP static int hlua_log_warning(lua_State *L)
6218{
6219 const char *msg;
6220
6221 MAY_LJMP(check_args(L, 1, "warning"));
6222 msg = MAY_LJMP(luaL_checkstring(L, 1));
6223 hlua_sendlog(NULL, LOG_WARNING, msg);
6224 return 0;
6225}
6226
6227__LJMP static int hlua_log_alert(lua_State *L)
6228{
6229 const char *msg;
6230
6231 MAY_LJMP(check_args(L, 1, "alert"));
6232 msg = MAY_LJMP(luaL_checkstring(L, 1));
6233 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006234 return 0;
6235}
6236
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006237__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006238{
6239 int wakeup_ms = lua_tointeger(L, -1);
6240 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006241 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006242 return 0;
6243}
6244
6245__LJMP static int hlua_sleep(lua_State *L)
6246{
6247 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006248 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006249
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006250 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006251
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006252 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006253 wakeup_ms = tick_add(now_ms, delay);
6254 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006255
Willy Tarreau9635e032018-10-16 17:52:55 +02006256 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006257 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006258}
6259
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006260__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006261{
6262 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006263 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006264
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006265 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006266
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006267 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006268 wakeup_ms = tick_add(now_ms, delay);
6269 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006270
Willy Tarreau9635e032018-10-16 17:52:55 +02006271 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006272 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006273}
6274
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006275/* This functionis an LUA binding. it permits to give back
6276 * the hand at the HAProxy scheduler. It is used when the
6277 * LUA processing consumes a lot of time.
6278 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006279__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006280{
6281 return 0;
6282}
6283
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006284__LJMP static int hlua_yield(lua_State *L)
6285{
Willy Tarreau9635e032018-10-16 17:52:55 +02006286 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006287 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006288}
6289
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006290/* This function change the nice of the currently executed
6291 * task. It is used set low or high priority at the current
6292 * task.
6293 */
Willy Tarreau59551662015-03-10 14:23:13 +01006294__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006295{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006296 struct hlua *hlua;
6297 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006298
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006299 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006300 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006301
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006302 /* Get hlua struct, or NULL if we execute from main lua state */
6303 hlua = hlua_gethlua(L);
6304
6305 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006306 if (!hlua || !hlua->task)
6307 return 0;
6308
6309 if (nice < -1024)
6310 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006311 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006312 nice = 1024;
6313
6314 hlua->task->nice = nice;
6315 return 0;
6316}
6317
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006318/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006319 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6320 * return an E_AGAIN signal, the emmiter of this signal must set a
6321 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006322 *
6323 * Task wrapper are longjmp safe because the only one Lua code
6324 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006325 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006326struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006327{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006328 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006329 enum hlua_exec status;
6330
Christopher Faulet5bc99722018-04-25 10:34:45 +02006331 if (task->thread_mask == MAX_THREADS_MASK)
6332 task_set_affinity(task, tid_bit);
6333
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006334 /* If it is the first call to the task, we must initialize the
6335 * execution timeouts.
6336 */
6337 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006338 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006339
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006340 /* Execute the Lua code. */
6341 status = hlua_ctx_resume(hlua, 1);
6342
6343 switch (status) {
6344 /* finished or yield */
6345 case HLUA_E_OK:
6346 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006347 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006348 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006349 break;
6350
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006351 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006352 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006353 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006354 break;
6355
6356 /* finished with error. */
6357 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006358 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006359 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006360 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006361 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006362 break;
6363
6364 case HLUA_E_ERR:
6365 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006366 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006367 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006368 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006369 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006370 break;
6371 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006372 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006373}
6374
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006375/* This function is an LUA binding that register LUA function to be
6376 * executed after the HAProxy configuration parsing and before the
6377 * HAProxy scheduler starts. This function expect only one LUA
6378 * argument that is a function. This function returns nothing, but
6379 * throws if an error is encountered.
6380 */
6381__LJMP static int hlua_register_init(lua_State *L)
6382{
6383 struct hlua_init_function *init;
6384 int ref;
6385
6386 MAY_LJMP(check_args(L, 1, "register_init"));
6387
6388 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6389
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006390 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006391 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006392 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006393
6394 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006395 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006396 return 0;
6397}
6398
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006399/* This functio is an LUA binding. It permits to register a task
6400 * executed in parallel of the main HAroxy activity. The task is
6401 * created and it is set in the HAProxy scheduler. It can be called
6402 * from the "init" section, "post init" or during the runtime.
6403 *
6404 * Lua prototype:
6405 *
6406 * <none> core.register_task(<function>)
6407 */
6408static int hlua_register_task(lua_State *L)
6409{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006410 struct hlua *hlua = NULL;
6411 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006412 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006413 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006414
6415 MAY_LJMP(check_args(L, 1, "register_task"));
6416
6417 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6418
Thierry Fournier75fc0292020-11-28 13:18:56 +01006419 /* Get the reference state. If the reference is NULL, L is the master
6420 * state, otherwise hlua->T is.
6421 */
6422 hlua = hlua_gethlua(L);
6423 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006424 /* we are in runtime processing */
6425 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006426 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006427 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006428 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006429
Willy Tarreaubafbe012017-11-24 17:34:44 +01006430 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006431 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006432 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006433 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006434
Thierry Fournier59f11be2020-11-29 00:37:41 +01006435 /* We are in the common lua state, execute the task anywhere,
6436 * otherwise, inherit the current thread identifier
6437 */
6438 if (state_id == 0)
6439 task = task_new(MAX_THREADS_MASK);
6440 else
6441 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006442 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006443 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006444
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006445 task->context = hlua;
6446 task->process = hlua_process_task;
6447
Thierry Fournier021d9862020-11-28 23:42:03 +01006448 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006449 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006450
6451 /* Restore the function in the stack. */
6452 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6453 hlua->nargs = 0;
6454
6455 /* Schedule task. */
6456 task_schedule(task, now_ms);
6457
6458 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006459
6460 alloc_error:
6461 task_destroy(task);
6462 hlua_ctx_destroy(hlua);
6463 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6464 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006465}
6466
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006467/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6468 * doesn't allow "yield" functions because the HAProxy engine cannot
6469 * resume converters.
6470 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006471static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006472{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006473 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006474 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006475 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006476
Willy Tarreaube508f12016-03-10 11:47:01 +01006477 if (!stream)
6478 return 0;
6479
Willy Tarreau87b09662015-04-03 00:22:06 +02006480 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006481 * Lua context can be not initialized. This behavior
6482 * permits to save performances because a systematic
6483 * Lua initialization cause 5% performances loss.
6484 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006485 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006486 struct hlua *hlua;
6487
6488 hlua = pool_alloc(pool_head_hlua);
6489 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006490 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6491 return 0;
6492 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006493 HLUA_INIT(hlua);
6494 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006495 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006496 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6497 return 0;
6498 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006499 }
6500
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006501 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006502 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006503
6504 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006505 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006506 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6507 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006508 else
6509 error = "critical error";
6510 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006511 return 0;
6512 }
6513
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006514 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006515 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006516 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006517 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006518 return 0;
6519 }
6520
6521 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006522 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006523
6524 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006525 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006526 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006527 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006528 return 0;
6529 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006530 hlua_smp2lua(stream->hlua->T, smp);
6531 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006532
6533 /* push keywords in the stack. */
6534 if (arg_p) {
6535 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006536 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006537 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006538 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006539 return 0;
6540 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006541 hlua_arg2lua(stream->hlua->T, arg_p);
6542 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006543 }
6544 }
6545
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006546 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006547 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006548
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006549 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006550 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006551 }
6552
6553 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006554 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006555 /* finished. */
6556 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006557 /* If the stack is empty, the function fails. */
6558 if (lua_gettop(stream->hlua->T) <= 0)
6559 return 0;
6560
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006561 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006562 hlua_lua2smp(stream->hlua->T, -1, smp);
6563 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006564 return 1;
6565
6566 /* yield. */
6567 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006568 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006569 return 0;
6570
6571 /* finished with error. */
6572 case HLUA_E_ERRMSG:
6573 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006574 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 fcn->name, lua_tostring(stream->hlua->T, -1));
6576 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006577 return 0;
6578
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006579 case HLUA_E_ETMOUT:
6580 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6581 return 0;
6582
6583 case HLUA_E_NOMEM:
6584 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6585 return 0;
6586
6587 case HLUA_E_YIELD:
6588 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6589 return 0;
6590
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006591 case HLUA_E_ERR:
6592 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006593 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006594 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006595
6596 default:
6597 return 0;
6598 }
6599}
6600
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006601/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6602 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006603 * resume sample-fetches. This function will be called by the sample
6604 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006605 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006606static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6607 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006608{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006609 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006610 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006611 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006612 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006613
Willy Tarreaube508f12016-03-10 11:47:01 +01006614 if (!stream)
6615 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006616
Willy Tarreau87b09662015-04-03 00:22:06 +02006617 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006618 * Lua context can be not initialized. This behavior
6619 * permits to save performances because a systematic
6620 * Lua initialization cause 5% performances loss.
6621 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006622 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006623 struct hlua *hlua;
6624
6625 hlua = pool_alloc(pool_head_hlua);
6626 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006627 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6628 return 0;
6629 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006630 hlua->T = NULL;
6631 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006632 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006633 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6634 return 0;
6635 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006636 }
6637
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006638 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006639 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006640
6641 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006642 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006643 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6644 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006645 else
6646 error = "critical error";
6647 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006648 return 0;
6649 }
6650
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006651 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006652 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006653 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006654 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006655 return 0;
6656 }
6657
6658 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006659 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006660
6661 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006662 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006663 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006664 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006665 return 0;
6666 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006667 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006668
6669 /* push keywords in the stack. */
6670 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6671 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006672 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006673 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006674 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006675 return 0;
6676 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006677 hlua_arg2lua(stream->hlua->T, arg_p);
6678 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006679 }
6680
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006681 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006682 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006683
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006684 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006685 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006686 }
6687
6688 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006689 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006690 /* finished. */
6691 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006692 /* If the stack is empty, the function fails. */
6693 if (lua_gettop(stream->hlua->T) <= 0)
6694 return 0;
6695
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006696 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006697 hlua_lua2smp(stream->hlua->T, -1, smp);
6698 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006699
6700 /* Set the end of execution flag. */
6701 smp->flags &= ~SMP_F_MAY_CHANGE;
6702 return 1;
6703
6704 /* yield. */
6705 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006706 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006707 return 0;
6708
6709 /* finished with error. */
6710 case HLUA_E_ERRMSG:
6711 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006712 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006713 fcn->name, lua_tostring(stream->hlua->T, -1));
6714 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006715 return 0;
6716
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006717 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006718 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6719 return 0;
6720
6721 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006722 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6723 return 0;
6724
6725 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006726 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6727 return 0;
6728
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006729 case HLUA_E_ERR:
6730 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006731 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006732 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006733
6734 default:
6735 return 0;
6736 }
6737}
6738
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006739/* This function is an LUA binding used for registering
6740 * "sample-conv" functions. It expects a converter name used
6741 * in the haproxy configuration file, and an LUA function.
6742 */
6743__LJMP static int hlua_register_converters(lua_State *L)
6744{
6745 struct sample_conv_kw_list *sck;
6746 const char *name;
6747 int ref;
6748 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02006749 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006750 struct sample_conv *sc;
6751 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006752
6753 MAY_LJMP(check_args(L, 2, "register_converters"));
6754
6755 /* First argument : converter name. */
6756 name = MAY_LJMP(luaL_checkstring(L, 1));
6757
6758 /* Second argument : lua function. */
6759 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6760
Thierry Fournierf67442e2020-11-28 20:41:07 +01006761 /* Check if the converter is already registered */
6762 trash = get_trash_chunk();
6763 chunk_printf(trash, "lua.%s", name);
6764 sc = find_sample_conv(trash->area, trash->data);
6765 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006766 fcn = sc->private;
6767 if (fcn->function_ref[hlua_state_id] != -1) {
6768 ha_warning("Trying to register converter 'lua.%s' more than once. "
6769 "This will become a hard error in version 2.5.\n", name);
6770 }
6771 fcn->function_ref[hlua_state_id] = ref;
6772 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006773 }
6774
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006775 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006776 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006777 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02006778 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006779 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006780 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02006781 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006782
6783 /* Fill fcn. */
6784 fcn->name = strdup(name);
6785 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02006786 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006787 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006788
6789 /* List head */
6790 sck->list.n = sck->list.p = NULL;
6791
6792 /* converter keyword. */
6793 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006794 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006795 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02006796 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006797
6798 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6799 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006800 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 +01006801 sck->kw[0].val_args = NULL;
6802 sck->kw[0].in_type = SMP_T_STR;
6803 sck->kw[0].out_type = SMP_T_STR;
6804 sck->kw[0].private = fcn;
6805
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006806 /* Register this new converter */
6807 sample_register_convs(sck);
6808
6809 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02006810
6811 alloc_error:
6812 release_hlua_function(fcn);
6813 ha_free(&sck);
6814 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6815 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006816}
6817
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006818/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006819 * "sample-fetch" functions. It expects a converter name used
6820 * in the haproxy configuration file, and an LUA function.
6821 */
6822__LJMP static int hlua_register_fetches(lua_State *L)
6823{
6824 const char *name;
6825 int ref;
6826 int len;
6827 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02006828 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006829 struct sample_fetch *sf;
6830 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006831
6832 MAY_LJMP(check_args(L, 2, "register_fetches"));
6833
6834 /* First argument : sample-fetch name. */
6835 name = MAY_LJMP(luaL_checkstring(L, 1));
6836
6837 /* Second argument : lua function. */
6838 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6839
Thierry Fournierf67442e2020-11-28 20:41:07 +01006840 /* Check if the sample-fetch is already registered */
6841 trash = get_trash_chunk();
6842 chunk_printf(trash, "lua.%s", name);
6843 sf = find_sample_fetch(trash->area, trash->data);
6844 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006845 fcn = sf->private;
6846 if (fcn->function_ref[hlua_state_id] != -1) {
6847 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6848 "This will become a hard error in version 2.5.\n", name);
6849 }
6850 fcn->function_ref[hlua_state_id] = ref;
6851 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006852 }
6853
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006854 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006855 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006856 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02006857 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006858 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006859 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02006860 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006861
6862 /* Fill fcn. */
6863 fcn->name = strdup(name);
6864 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02006865 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006866 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006867
6868 /* List head */
6869 sfk->list.n = sfk->list.p = NULL;
6870
6871 /* sample-fetch keyword. */
6872 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006873 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006874 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02006875 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006876
6877 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6878 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006879 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 +01006880 sfk->kw[0].val_args = NULL;
6881 sfk->kw[0].out_type = SMP_T_STR;
6882 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6883 sfk->kw[0].val = 0;
6884 sfk->kw[0].private = fcn;
6885
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006886 /* Register this new fetch. */
6887 sample_register_fetches(sfk);
6888
6889 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02006890
6891 alloc_error:
6892 release_hlua_function(fcn);
6893 ha_free(&sfk);
6894 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6895 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006896}
6897
Christopher Faulet501465d2020-02-26 14:54:16 +01006898/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006899 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006900__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006901{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006902 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006903 unsigned int delay;
6904 unsigned int wakeup_ms;
6905
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006906 /* Get hlua struct, or NULL if we execute from main lua state */
6907 hlua = hlua_gethlua(L);
6908 if (!hlua) {
6909 return 0;
6910 }
6911
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006912 MAY_LJMP(check_args(L, 1, "wake_time"));
6913
6914 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6915 wakeup_ms = tick_add(now_ms, delay);
6916 hlua->wake_time = wakeup_ms;
6917 return 0;
6918}
6919
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006920/* This function is a wrapper to execute each LUA function declared as an action
6921 * wrapper during the initialisation period. This function may return any
6922 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6923 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6924 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006925 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006926static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006927 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006928{
6929 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006930 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006931 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006932 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006933
6934 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006935 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6936 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6937 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6938 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006939 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006940 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006941 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006942 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006943
Willy Tarreau87b09662015-04-03 00:22:06 +02006944 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006945 * Lua context can be not initialized. This behavior
6946 * permits to save performances because a systematic
6947 * Lua initialization cause 5% performances loss.
6948 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006949 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006950 struct hlua *hlua;
6951
6952 hlua = pool_alloc(pool_head_hlua);
6953 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006954 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006955 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006956 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006957 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006958 HLUA_INIT(hlua);
6959 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006960 if (!hlua_ctx_init(s->hlua, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn), s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006961 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006962 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006963 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006964 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006965 }
6966
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006967 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006968 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006969
6970 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006971 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006972 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6973 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006974 else
6975 error = "critical error";
6976 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006977 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006978 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006979 }
6980
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006981 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006982 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006983 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006984 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006985 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006986 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006987 }
6988
6989 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006990 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 +01006991
Willy Tarreau87b09662015-04-03 00:22:06 +02006992 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006993 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006994 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006995 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006996 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006997 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006998 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006999 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007000
7001 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007002 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007003 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007004 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007005 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007006 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007007 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007008 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007009 lua_pushstring(s->hlua->T, *arg);
7010 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007011 }
7012
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007013 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007014 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007015
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007016 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007017 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007018 }
7019
7020 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01007021 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007022 /* finished. */
7023 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007024 /* Catch the return value */
7025 if (lua_gettop(s->hlua->T) > 0)
7026 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007027
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007028 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007029 if (act_ret == ACT_RET_YIELD) {
7030 if (flags & ACT_OPT_FINAL)
7031 goto err_yield;
7032
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007033 if (dir == SMP_OPT_DIR_REQ)
7034 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7035 s->hlua->wake_time);
7036 else
7037 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7038 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007039 }
7040 goto end;
7041
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007042 /* yield. */
7043 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007044 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007045 if (dir == SMP_OPT_DIR_REQ)
7046 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7047 s->hlua->wake_time);
7048 else
7049 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7050 s->hlua->wake_time);
7051
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007052 /* Some actions can be wake up when a "write" event
7053 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007054 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007055 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007056 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007057 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007058 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007059 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007060 act_ret = ACT_RET_YIELD;
7061 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007062
7063 /* finished with error. */
7064 case HLUA_E_ERRMSG:
7065 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007066 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007067 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007068 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007069 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007070
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007071 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007072 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007073 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007074
7075 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007076 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007077 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007078
7079 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007080 err_yield:
7081 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007082 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007083 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007084 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007085
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007086 case HLUA_E_ERR:
7087 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007088 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007089 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007090
7091 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007092 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007093 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007094
7095 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007096 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007097 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007098 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007099}
7100
Willy Tarreau144f84a2021-03-02 16:09:26 +01007101struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007102{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007103 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007104
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007105 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007106 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007107 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007108}
7109
7110static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7111{
7112 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007113 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007114 struct task *task;
7115 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007116 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007117
Willy Tarreaubafbe012017-11-24 17:34:44 +01007118 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007119 if (!hlua) {
7120 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007121 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007122 return 0;
7123 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007124 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007125 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007126 ctx->ctx.hlua_apptcp.flags = 0;
7127
7128 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007129 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007130 if (!task) {
7131 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007132 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007133 return 0;
7134 }
7135 task->nice = 0;
7136 task->context = ctx;
7137 task->process = hlua_applet_wakeup;
7138 ctx->ctx.hlua_apptcp.task = task;
7139
7140 /* In the execution wrappers linked with a stream, the
7141 * Lua context can be not initialized. This behavior
7142 * permits to save performances because a systematic
7143 * Lua initialization cause 5% performances loss.
7144 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007145 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007146 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007147 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007148 return 0;
7149 }
7150
7151 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007152 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007153
7154 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007155 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007156 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7157 error = lua_tostring(hlua->T, -1);
7158 else
7159 error = "critical error";
7160 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007161 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007162 return 0;
7163 }
7164
7165 /* Check stack available size. */
7166 if (!lua_checkstack(hlua->T, 1)) {
7167 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007168 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007169 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007170 return 0;
7171 }
7172
7173 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007174 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007175
7176 /* Create and and push object stream in the stack. */
7177 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7178 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007179 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007180 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007181 return 0;
7182 }
7183 hlua->nargs = 1;
7184
7185 /* push keywords in the stack. */
7186 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7187 if (!lua_checkstack(hlua->T, 1)) {
7188 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007189 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007190 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007191 return 0;
7192 }
7193 lua_pushstring(hlua->T, *arg);
7194 hlua->nargs++;
7195 }
7196
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007197 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007198
7199 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007200 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007201 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007202
7203 return 1;
7204}
7205
Willy Tarreau60409db2019-08-21 14:14:50 +02007206void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007207{
7208 struct stream_interface *si = ctx->owner;
7209 struct stream *strm = si_strm(si);
7210 struct channel *res = si_ic(si);
7211 struct act_rule *rule = ctx->rule;
7212 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007213 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007214
7215 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007216 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7217 /* eat the whole request */
7218 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007219 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007220 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007221
7222 /* If the stream is disconnect or closed, ldo nothing. */
7223 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7224 return;
7225
7226 /* Execute the function. */
7227 switch (hlua_ctx_resume(hlua, 1)) {
7228 /* finished. */
7229 case HLUA_E_OK:
7230 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7231
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007232 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007233 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007234 res->flags |= CF_READ_NULL;
7235 si_shutr(si);
7236 return;
7237
7238 /* yield. */
7239 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007240 if (hlua->wake_time != TICK_ETERNITY)
7241 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007242 return;
7243
7244 /* finished with error. */
7245 case HLUA_E_ERRMSG:
7246 /* Display log. */
7247 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007248 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007249 lua_pop(hlua->T, 1);
7250 goto error;
7251
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007252 case HLUA_E_ETMOUT:
7253 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007254 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007255 goto error;
7256
7257 case HLUA_E_NOMEM:
7258 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007259 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007260 goto error;
7261
7262 case HLUA_E_YIELD: /* unexpected */
7263 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007264 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007265 goto error;
7266
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007267 case HLUA_E_ERR:
7268 /* Display log. */
7269 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007270 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007271 goto error;
7272
7273 default:
7274 goto error;
7275 }
7276
7277error:
7278
7279 /* For all other cases, just close the stream. */
7280 si_shutw(si);
7281 si_shutr(si);
7282 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7283}
7284
7285static void hlua_applet_tcp_release(struct appctx *ctx)
7286{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007287 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007288 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007289 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007290 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007291}
7292
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007293/* The function returns 1 if the initialisation is complete, 0 if
7294 * an errors occurs and -1 if more data are required for initializing
7295 * the applet.
7296 */
7297static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7298{
7299 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007300 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007301 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007302 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007303 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007304 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007305
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007306 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007307 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007308 if (!hlua) {
7309 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007310 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007311 return 0;
7312 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007313 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007314 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007315 ctx->ctx.hlua_apphttp.left_bytes = -1;
7316 ctx->ctx.hlua_apphttp.flags = 0;
7317
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007318 if (txn->req.flags & HTTP_MSGF_VER_11)
7319 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7320
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007321 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007322 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007323 if (!task) {
7324 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007325 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007326 return 0;
7327 }
7328 task->nice = 0;
7329 task->context = ctx;
7330 task->process = hlua_applet_wakeup;
7331 ctx->ctx.hlua_apphttp.task = task;
7332
7333 /* In the execution wrappers linked with a stream, the
7334 * Lua context can be not initialized. This behavior
7335 * permits to save performances because a systematic
7336 * Lua initialization cause 5% performances loss.
7337 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007338 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007339 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007340 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007341 return 0;
7342 }
7343
7344 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007345 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007346
7347 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007348 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007349 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7350 error = lua_tostring(hlua->T, -1);
7351 else
7352 error = "critical error";
7353 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007354 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007355 return 0;
7356 }
7357
7358 /* Check stack available size. */
7359 if (!lua_checkstack(hlua->T, 1)) {
7360 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007361 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007362 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007363 return 0;
7364 }
7365
7366 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007367 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007368
7369 /* Create and and push object stream in the stack. */
7370 if (!hlua_applet_http_new(hlua->T, ctx)) {
7371 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007372 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007373 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007374 return 0;
7375 }
7376 hlua->nargs = 1;
7377
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007378 /* push keywords in the stack. */
7379 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7380 if (!lua_checkstack(hlua->T, 1)) {
7381 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007382 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007383 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007384 return 0;
7385 }
7386 lua_pushstring(hlua->T, *arg);
7387 hlua->nargs++;
7388 }
7389
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007390 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007391
7392 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007393 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007394
7395 return 1;
7396}
7397
Willy Tarreau60409db2019-08-21 14:14:50 +02007398void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007399{
7400 struct stream_interface *si = ctx->owner;
7401 struct stream *strm = si_strm(si);
7402 struct channel *req = si_oc(si);
7403 struct channel *res = si_ic(si);
7404 struct act_rule *rule = ctx->rule;
7405 struct proxy *px = strm->be;
7406 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7407 struct htx *req_htx, *res_htx;
7408
7409 res_htx = htx_from_buf(&res->buf);
7410
7411 /* If the stream is disconnect or closed, ldo nothing. */
7412 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7413 goto out;
7414
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007415 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007416 if (!b_size(&res->buf)) {
7417 si_rx_room_blk(si);
7418 goto out;
7419 }
7420 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007421 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007422 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7423
7424 /* Set the currently running flag. */
7425 if (!HLUA_IS_RUNNING(hlua) &&
7426 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007427 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007428 si_cant_get(si);
7429 goto out;
7430 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007431 }
7432
7433 /* Executes The applet if it is not done. */
7434 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7435
7436 /* Execute the function. */
7437 switch (hlua_ctx_resume(hlua, 1)) {
7438 /* finished. */
7439 case HLUA_E_OK:
7440 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7441 break;
7442
7443 /* yield. */
7444 case HLUA_E_AGAIN:
7445 if (hlua->wake_time != TICK_ETERNITY)
7446 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007447 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007448
7449 /* finished with error. */
7450 case HLUA_E_ERRMSG:
7451 /* Display log. */
7452 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007453 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007454 lua_pop(hlua->T, 1);
7455 goto error;
7456
7457 case HLUA_E_ETMOUT:
7458 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007459 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007460 goto error;
7461
7462 case HLUA_E_NOMEM:
7463 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007464 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007465 goto error;
7466
7467 case HLUA_E_YIELD: /* unexpected */
7468 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007469 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007470 goto error;
7471
7472 case HLUA_E_ERR:
7473 /* Display log. */
7474 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007475 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007476 goto error;
7477
7478 default:
7479 goto error;
7480 }
7481 }
7482
7483 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007484 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7485 goto done;
7486
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007487 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7488 goto error;
7489
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007490 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
7491 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007492 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7493 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007494 }
7495
7496 done:
7497 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007498 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007499 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007500 si_shutr(si);
7501 }
7502
7503 /* eat the whole request */
7504 if (co_data(req)) {
7505 req_htx = htx_from_buf(&req->buf);
7506 co_htx_skip(req, req_htx, co_data(req));
7507 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007508 }
7509 }
7510
7511 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007512 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007513 return;
7514
7515 error:
7516
7517 /* If we are in HTTP mode, and we are not send any
7518 * data, return a 500 server error in best effort:
7519 * if there is no room available in the buffer,
7520 * just close the connection.
7521 */
7522 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007523 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007524
7525 channel_erase(res);
7526 res->buf.data = b_data(err);
7527 memcpy(res->buf.area, b_head(err), b_data(err));
7528 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007529 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007530 }
7531 if (!(strm->flags & SF_ERR_MASK))
7532 strm->flags |= SF_ERR_RESOURCE;
7533 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7534 goto done;
7535}
7536
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007537static void hlua_applet_http_release(struct appctx *ctx)
7538{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007539 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007540 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007541 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007542 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007543}
7544
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007545/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007546 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007547 *
7548 * This function can fail with an abort() due to an Lua critical error.
7549 * We are in the configuration parsing process of HAProxy, this abort() is
7550 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007551 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007552static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7553 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007554{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007555 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007556 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007557
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007558 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007559 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007560 if (!rule->arg.hlua_rule) {
7561 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007562 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007563 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007564
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007565 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007566 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7567 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007568 if (!rule->arg.hlua_rule->args) {
7569 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007570 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007571 }
7572
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007573 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007574 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007575
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007576 /* Expect some arguments */
7577 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007578 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007579 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007580 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007581 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007582 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007583 if (!rule->arg.hlua_rule->args[i]) {
7584 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007585 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007586 }
7587 (*cur_arg)++;
7588 }
7589 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007590
Thierry FOURNIER42148732015-09-02 17:17:33 +02007591 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007592 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007593 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007594
7595 error:
7596 if (rule->arg.hlua_rule) {
7597 if (rule->arg.hlua_rule->args) {
7598 for (i = 0; i < fcn->nargs; i++)
7599 ha_free(&rule->arg.hlua_rule->args[i]);
7600 ha_free(&rule->arg.hlua_rule->args);
7601 }
7602 ha_free(&rule->arg.hlua_rule);
7603 }
7604 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007605}
7606
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007607static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7608 struct act_rule *rule, char **err)
7609{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007610 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007611
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007612 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007613 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007614 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007615 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007616 * the call of this analyzer.
7617 */
7618 if (rule->from != ACT_F_HTTP_REQ) {
7619 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7620 return ACT_RET_PRS_ERR;
7621 }
7622
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007623 /* Memory for the rule. */
7624 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7625 if (!rule->arg.hlua_rule) {
7626 memprintf(err, "out of memory error");
7627 return ACT_RET_PRS_ERR;
7628 }
7629
7630 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007631 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007632
7633 /* TODO: later accept arguments. */
7634 rule->arg.hlua_rule->args = NULL;
7635
7636 /* Add applet pointer in the rule. */
7637 rule->applet.obj_type = OBJ_TYPE_APPLET;
7638 rule->applet.name = fcn->name;
7639 rule->applet.init = hlua_applet_http_init;
7640 rule->applet.fct = hlua_applet_http_fct;
7641 rule->applet.release = hlua_applet_http_release;
7642 rule->applet.timeout = hlua_timeout_applet;
7643
7644 return ACT_RET_PRS_OK;
7645}
7646
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007647/* This function is an LUA binding used for registering
7648 * "sample-conv" functions. It expects a converter name used
7649 * in the haproxy configuration file, and an LUA function.
7650 */
7651__LJMP static int hlua_register_action(lua_State *L)
7652{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007653 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007654 const char *name;
7655 int ref;
7656 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007657 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007658 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007659 struct buffer *trash;
7660 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007661
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007662 /* Initialise the number of expected arguments at 0. */
7663 nargs = 0;
7664
7665 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7666 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007667
7668 /* First argument : converter name. */
7669 name = MAY_LJMP(luaL_checkstring(L, 1));
7670
7671 /* Second argument : environment. */
7672 if (lua_type(L, 2) != LUA_TTABLE)
7673 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7674
7675 /* Third argument : lua function. */
7676 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7677
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007678 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007679 if (lua_gettop(L) >= 4)
7680 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7681
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007682 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007683 lua_pushnil(L);
7684 while (lua_next(L, 2) != 0) {
7685 if (lua_type(L, -1) != LUA_TSTRING)
7686 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7687
Thierry Fournierf67442e2020-11-28 20:41:07 +01007688 /* Check if action exists */
7689 trash = get_trash_chunk();
7690 chunk_printf(trash, "lua.%s", name);
7691 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7692 akw = tcp_req_cont_action(trash->area);
7693 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7694 akw = tcp_res_cont_action(trash->area);
7695 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7696 akw = action_http_req_custom(trash->area);
7697 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7698 akw = action_http_res_custom(trash->area);
7699 } else {
7700 akw = NULL;
7701 }
7702 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007703 fcn = akw->private;
7704 if (fcn->function_ref[hlua_state_id] != -1) {
7705 ha_warning("Trying to register action 'lua.%s' more than once. "
7706 "This will become a hard error in version 2.5.\n", name);
7707 }
7708 fcn->function_ref[hlua_state_id] = ref;
7709
7710 /* pop the environment string. */
7711 lua_pop(L, 1);
7712 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007713 }
7714
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007715 /* Check required environment. Only accepted "http" or "tcp". */
7716 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007717 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007718 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007719 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007720 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007721 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007722 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007723
7724 /* Fill fcn. */
7725 fcn->name = strdup(name);
7726 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007727 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007728 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007729
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007730 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007731 fcn->nargs = nargs;
7732
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007733 /* List head */
7734 akl->list.n = akl->list.p = NULL;
7735
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007736 /* action keyword. */
7737 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007738 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007739 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007740 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007741
7742 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7743
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007744 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007745 akl->kw[0].private = fcn;
7746 akl->kw[0].parse = action_register_lua;
7747
7748 /* select the action registering point. */
7749 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7750 tcp_req_cont_keywords_register(akl);
7751 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7752 tcp_res_cont_keywords_register(akl);
7753 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7754 http_req_keywords_register(akl);
7755 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7756 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007757 else {
7758 release_hlua_function(fcn);
7759 if (akl)
7760 ha_free((char **)&(akl->kw[0].kw));
7761 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007762 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007763 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7764 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007765 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007766
7767 /* pop the environment string. */
7768 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007769
7770 /* reset for next loop */
7771 akl = NULL;
7772 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007773 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007774 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007775
7776 alloc_error:
7777 release_hlua_function(fcn);
7778 ha_free(&akl);
7779 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7780 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007781}
7782
7783static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7784 struct act_rule *rule, char **err)
7785{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007786 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007787
Christopher Faulet280f85b2019-07-15 15:02:04 +02007788 if (px->mode == PR_MODE_HTTP) {
7789 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007790 return ACT_RET_PRS_ERR;
7791 }
7792
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007793 /* Memory for the rule. */
7794 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7795 if (!rule->arg.hlua_rule) {
7796 memprintf(err, "out of memory error");
7797 return ACT_RET_PRS_ERR;
7798 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007799
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007800 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007801 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007802
7803 /* TODO: later accept arguments. */
7804 rule->arg.hlua_rule->args = NULL;
7805
7806 /* Add applet pointer in the rule. */
7807 rule->applet.obj_type = OBJ_TYPE_APPLET;
7808 rule->applet.name = fcn->name;
7809 rule->applet.init = hlua_applet_tcp_init;
7810 rule->applet.fct = hlua_applet_tcp_fct;
7811 rule->applet.release = hlua_applet_tcp_release;
7812 rule->applet.timeout = hlua_timeout_applet;
7813
7814 return 0;
7815}
7816
7817/* This function is an LUA binding used for registering
7818 * "sample-conv" functions. It expects a converter name used
7819 * in the haproxy configuration file, and an LUA function.
7820 */
7821__LJMP static int hlua_register_service(lua_State *L)
7822{
7823 struct action_kw_list *akl;
7824 const char *name;
7825 const char *env;
7826 int ref;
7827 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007828 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007829 struct buffer *trash;
7830 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007831
7832 MAY_LJMP(check_args(L, 3, "register_service"));
7833
7834 /* First argument : converter name. */
7835 name = MAY_LJMP(luaL_checkstring(L, 1));
7836
7837 /* Second argument : environment. */
7838 env = MAY_LJMP(luaL_checkstring(L, 2));
7839
7840 /* Third argument : lua function. */
7841 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7842
Thierry Fournierf67442e2020-11-28 20:41:07 +01007843 /* Check for service already registered */
7844 trash = get_trash_chunk();
7845 chunk_printf(trash, "lua.%s", name);
7846 akw = service_find(trash->area);
7847 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007848 fcn = akw->private;
7849 if (fcn->function_ref[hlua_state_id] != -1) {
7850 ha_warning("Trying to register service 'lua.%s' more than once. "
7851 "This will become a hard error in version 2.5.\n", name);
7852 }
7853 fcn->function_ref[hlua_state_id] = ref;
7854 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007855 }
7856
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007857 /* Allocate and fill the sample fetch keyword struct. */
7858 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7859 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007860 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007861 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007862 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007863 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007864
7865 /* Fill fcn. */
7866 len = strlen("<lua.>") + strlen(name) + 1;
7867 fcn->name = calloc(1, len);
7868 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007869 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007870 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007871 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007872
7873 /* List head */
7874 akl->list.n = akl->list.p = NULL;
7875
7876 /* converter keyword. */
7877 len = strlen("lua.") + strlen(name) + 1;
7878 akl->kw[0].kw = calloc(1, len);
7879 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007880 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007881
7882 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7883
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007884 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007885 if (strcmp(env, "tcp") == 0)
7886 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007887 else if (strcmp(env, "http") == 0)
7888 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007889 else {
7890 release_hlua_function(fcn);
7891 if (akl)
7892 ha_free((char **)&(akl->kw[0].kw));
7893 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007894 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007895 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02007896 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007897
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007898 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007899 akl->kw[0].private = fcn;
7900
7901 /* End of array. */
7902 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7903
7904 /* Register this new converter */
7905 service_keywords_register(akl);
7906
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007907 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007908
7909 alloc_error:
7910 release_hlua_function(fcn);
7911 ha_free(&akl);
7912 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7913 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007914}
7915
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007916/* This function initialises Lua cli handler. It copies the
7917 * arguments in the Lua stack and create channel IO objects.
7918 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007919static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007920{
7921 struct hlua *hlua;
7922 struct hlua_function *fcn;
7923 int i;
7924 const char *error;
7925
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007926 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007927 appctx->ctx.hlua_cli.fcn = private;
7928
Willy Tarreaubafbe012017-11-24 17:34:44 +01007929 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007930 if (!hlua) {
7931 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007932 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007933 }
7934 HLUA_INIT(hlua);
7935 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007936
7937 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007938 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007939 * applet_http. It is absolutely compatible.
7940 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007941 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007942 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007943 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007944 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007945 }
7946 appctx->ctx.hlua_cli.task->nice = 0;
7947 appctx->ctx.hlua_cli.task->context = appctx;
7948 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7949
7950 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01007951 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007952 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007953 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007954 }
7955
7956 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007957 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007958 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7959 error = lua_tostring(hlua->T, -1);
7960 else
7961 error = "critical error";
7962 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7963 goto error;
7964 }
7965
7966 /* Check stack available size. */
7967 if (!lua_checkstack(hlua->T, 2)) {
7968 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7969 goto error;
7970 }
7971
7972 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007973 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007974
7975 /* Once the arguments parsed, the CLI is like an AppletTCP,
7976 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007977 */
7978 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7979 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7980 goto error;
7981 }
7982 hlua->nargs = 1;
7983
7984 /* push keywords in the stack. */
7985 for (i = 0; *args[i]; i++) {
7986 /* Check stack available size. */
7987 if (!lua_checkstack(hlua->T, 1)) {
7988 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7989 goto error;
7990 }
7991 lua_pushstring(hlua->T, args[i]);
7992 hlua->nargs++;
7993 }
7994
7995 /* We must initialize the execution timeouts. */
7996 hlua->max_time = hlua_timeout_session;
7997
7998 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007999 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008000
8001 /* It's ok */
8002 return 0;
8003
8004 /* It's not ok. */
8005error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008006 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008007 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008008 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008009 return 1;
8010}
8011
8012static int hlua_cli_io_handler_fct(struct appctx *appctx)
8013{
8014 struct hlua *hlua;
8015 struct stream_interface *si;
8016 struct hlua_function *fcn;
8017
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008018 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008019 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008020 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008021
8022 /* If the stream is disconnect or closed, ldo nothing. */
8023 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8024 return 1;
8025
8026 /* Execute the function. */
8027 switch (hlua_ctx_resume(hlua, 1)) {
8028
8029 /* finished. */
8030 case HLUA_E_OK:
8031 return 1;
8032
8033 /* yield. */
8034 case HLUA_E_AGAIN:
8035 /* We want write. */
8036 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008037 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008038 /* Set the timeout. */
8039 if (hlua->wake_time != TICK_ETERNITY)
8040 task_schedule(hlua->task, hlua->wake_time);
8041 return 0;
8042
8043 /* finished with error. */
8044 case HLUA_E_ERRMSG:
8045 /* Display log. */
8046 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8047 fcn->name, lua_tostring(hlua->T, -1));
8048 lua_pop(hlua->T, 1);
8049 return 1;
8050
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008051 case HLUA_E_ETMOUT:
8052 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8053 fcn->name);
8054 return 1;
8055
8056 case HLUA_E_NOMEM:
8057 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8058 fcn->name);
8059 return 1;
8060
8061 case HLUA_E_YIELD: /* unexpected */
8062 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8063 fcn->name);
8064 return 1;
8065
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008066 case HLUA_E_ERR:
8067 /* Display log. */
8068 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8069 fcn->name);
8070 return 1;
8071
8072 default:
8073 return 1;
8074 }
8075
8076 return 1;
8077}
8078
8079static void hlua_cli_io_release_fct(struct appctx *appctx)
8080{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008081 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008082 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008083}
8084
8085/* This function is an LUA binding used for registering
8086 * new keywords in the cli. It expects a list of keywords
8087 * which are the "path". It is limited to 5 keywords. A
8088 * description of the command, a function to be executed
8089 * for the parsing and a function for io handlers.
8090 */
8091__LJMP static int hlua_register_cli(lua_State *L)
8092{
8093 struct cli_kw_list *cli_kws;
8094 const char *message;
8095 int ref_io;
8096 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008097 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008098 int index;
8099 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008100 struct buffer *trash;
8101 const char *kw[5];
8102 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008103 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008104
8105 MAY_LJMP(check_args(L, 3, "register_cli"));
8106
8107 /* First argument : an array of maximum 5 keywords. */
8108 if (!lua_istable(L, 1))
8109 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8110
8111 /* Second argument : string with contextual message. */
8112 message = MAY_LJMP(luaL_checkstring(L, 2));
8113
8114 /* Third and fourth argument : lua function. */
8115 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8116
Thierry Fournierf67442e2020-11-28 20:41:07 +01008117 /* Check for CLI service already registered */
8118 trash = get_trash_chunk();
8119 index = 0;
8120 lua_pushnil(L);
8121 memset(kw, 0, sizeof(kw));
8122 while (lua_next(L, 1) != 0) {
8123 if (index >= CLI_PREFIX_KW_NB)
8124 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8125 if (lua_type(L, -1) != LUA_TSTRING)
8126 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8127 kw[index] = lua_tostring(L, -1);
8128 if (index == 0)
8129 chunk_printf(trash, "%s", kw[index]);
8130 else
8131 chunk_appendf(trash, " %s", kw[index]);
8132 index++;
8133 lua_pop(L, 1);
8134 }
8135 cli_kw = cli_find_kw_exact((char **)kw);
8136 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008137 fcn = cli_kw->private;
8138 if (fcn->function_ref[hlua_state_id] != -1) {
8139 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8140 "This will become a hard error in version 2.5.\n", trash->area);
8141 }
8142 fcn->function_ref[hlua_state_id] = ref_io;
8143 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008144 }
8145
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008146 /* Allocate and fill the sample fetch keyword struct. */
8147 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008148 if (!cli_kws) {
8149 errmsg = "Lua out of memory error.";
8150 goto error;
8151 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008152 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008153 if (!fcn) {
8154 errmsg = "Lua out of memory error.";
8155 goto error;
8156 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008157
8158 /* Fill path. */
8159 index = 0;
8160 lua_pushnil(L);
8161 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008162 if (index >= 5) {
8163 errmsg = "1st argument must be a table with a maximum of 5 entries";
8164 goto error;
8165 }
8166 if (lua_type(L, -1) != LUA_TSTRING) {
8167 errmsg = "1st argument must be a table filled with strings";
8168 goto error;
8169 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008170 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008171 if (!cli_kws->kw[0].str_kw[index]) {
8172 errmsg = "Lua out of memory error.";
8173 goto error;
8174 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008175 index++;
8176 lua_pop(L, 1);
8177 }
8178
8179 /* Copy help message. */
8180 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008181 if (!cli_kws->kw[0].usage) {
8182 errmsg = "Lua out of memory error.";
8183 goto error;
8184 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008185
8186 /* Fill fcn io handler. */
8187 len = strlen("<lua.cli>") + 1;
8188 for (i = 0; i < index; i++)
8189 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8190 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008191 if (!fcn->name) {
8192 errmsg = "Lua out of memory error.";
8193 goto error;
8194 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008195 strncat((char *)fcn->name, "<lua.cli", len);
8196 for (i = 0; i < index; i++) {
8197 strncat((char *)fcn->name, ".", len);
8198 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8199 }
8200 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008201 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008202
8203 /* Fill last entries. */
8204 cli_kws->kw[0].private = fcn;
8205 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8206 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8207 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8208
8209 /* Register this new converter */
8210 cli_register_kw(cli_kws);
8211
8212 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008213
8214 error:
8215 release_hlua_function(fcn);
8216 if (cli_kws) {
8217 for (i = 0; i < index; i++)
8218 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8219 ha_free((char **)&(cli_kws->kw[0].usage));
8220 }
8221 ha_free(&cli_kws);
8222 WILL_LJMP(luaL_error(L, errmsg));
8223 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008224}
8225
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008226static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008227 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008228 char **err, unsigned int *timeout)
8229{
8230 const char *error;
8231
8232 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008233 if (error == PARSE_TIME_OVER) {
8234 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8235 args[1], args[0]);
8236 return -1;
8237 }
8238 else if (error == PARSE_TIME_UNDER) {
8239 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8240 args[1], args[0]);
8241 return -1;
8242 }
8243 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008244 memprintf(err, "%s: invalid timeout", args[0]);
8245 return -1;
8246 }
8247 return 0;
8248}
8249
8250static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008251 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008252 char **err)
8253{
8254 return hlua_read_timeout(args, section_type, curpx, defpx,
8255 file, line, err, &hlua_timeout_session);
8256}
8257
8258static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008259 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008260 char **err)
8261{
8262 return hlua_read_timeout(args, section_type, curpx, defpx,
8263 file, line, err, &hlua_timeout_task);
8264}
8265
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008266static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008267 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008268 char **err)
8269{
8270 return hlua_read_timeout(args, section_type, curpx, defpx,
8271 file, line, err, &hlua_timeout_applet);
8272}
8273
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008274static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008275 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008276 char **err)
8277{
8278 char *error;
8279
8280 hlua_nb_instruction = strtoll(args[1], &error, 10);
8281 if (*error != '\0') {
8282 memprintf(err, "%s: invalid number", args[0]);
8283 return -1;
8284 }
8285 return 0;
8286}
8287
Willy Tarreau32f61e22015-03-18 17:54:59 +01008288static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008289 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008290 char **err)
8291{
8292 char *error;
8293
8294 if (*(args[1]) == 0) {
8295 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8296 return -1;
8297 }
8298 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8299 if (*error != '\0') {
8300 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8301 return -1;
8302 }
8303 return 0;
8304}
8305
8306
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008307/* This function is called by the main configuration key "lua-load". It loads and
8308 * execute an lua file during the parsing of the HAProxy configuration file. It is
8309 * the main lua entry point.
8310 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008311 * This function runs with the HAProxy keywords API. It returns -1 if an error
8312 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008313 *
8314 * In some error case, LUA set an error message in top of the stack. This function
8315 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008316 *
8317 * This function can fail with an abort() due to an Lua critical error.
8318 * We are in the configuration parsing process of HAProxy, this abort() is
8319 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008320 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008321static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008322{
8323 int error;
8324
8325 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008326 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008327 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008328 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8329 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008330 return -1;
8331 }
8332
8333 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008334 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008335 switch (error) {
8336 case LUA_OK:
8337 break;
8338 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008339 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8340 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008341 return -1;
8342 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008343 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008344 return -1;
8345 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008346 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8347 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008348 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008349#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008350 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008351 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8352 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008353 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008354#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008355 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008356 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8357 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008358 return -1;
8359 }
8360
8361 return 0;
8362}
8363
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008364static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008365 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008366 char **err)
8367{
8368 if (*(args[1]) == 0) {
8369 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8370 return -1;
8371 }
8372
Thierry Fournier59f11be2020-11-29 00:37:41 +01008373 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008374 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008375 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008376 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008377}
8378
Thierry Fournier59f11be2020-11-29 00:37:41 +01008379static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008380 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008381 char **err)
8382{
8383 int len;
8384
8385 if (*(args[1]) == 0) {
8386 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8387 return -1;
8388 }
8389
8390 if (per_thread_load == NULL) {
8391 /* allocate the first entry large enough to store the final NULL */
8392 per_thread_load = calloc(1, sizeof(*per_thread_load));
8393 if (per_thread_load == NULL) {
8394 memprintf(err, "out of memory error");
8395 return -1;
8396 }
8397 }
8398
8399 /* count used entries */
8400 for (len = 0; per_thread_load[len] != NULL; len++)
8401 ;
8402
8403 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8404 if (per_thread_load == NULL) {
8405 memprintf(err, "out of memory error");
8406 return -1;
8407 }
8408
8409 per_thread_load[len] = strdup(args[1]);
8410 per_thread_load[len + 1] = NULL;
8411
8412 if (per_thread_load[len] == NULL) {
8413 memprintf(err, "out of memory error");
8414 return -1;
8415 }
8416
8417 /* loading for thread 1 only */
8418 hlua_state_id = 1;
8419 ha_set_tid(0);
8420 return hlua_load_state(args[1], hlua_states[1], err);
8421}
8422
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008423/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8424 * in the given <ctx>.
8425 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008426static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008427{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008428 lua_getglobal(L, "package"); /* push package variable */
8429 lua_pushstring(L, path); /* push given path */
8430 lua_pushstring(L, ";"); /* push semicolon */
8431 lua_getfield(L, -3, type); /* push old path */
8432 lua_concat(L, 3); /* concatenate to new path */
8433 lua_setfield(L, -2, type); /* store new path */
8434 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008435
8436 return 0;
8437}
8438
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008439static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008440 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008441 char **err)
8442{
8443 char *path;
8444 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008445 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008446
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008447 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008448 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008449 }
8450
8451 if (!(*args[1])) {
8452 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008453 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008454 }
8455 path = args[1];
8456
8457 if (*args[2]) {
8458 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8459 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008460 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008461 }
8462 type = args[2];
8463 }
8464
Thierry Fournier59f11be2020-11-29 00:37:41 +01008465 p = calloc(1, sizeof(*p));
8466 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008467 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008468 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008469 }
8470 p->path = strdup(path);
8471 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008472 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008473 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008474 }
8475 p->type = strdup(type);
8476 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008477 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008478 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008479 }
Willy Tarreau2b718102021-04-21 07:32:39 +02008480 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01008481
8482 hlua_prepend_path(hlua_states[0], type, path);
8483 hlua_prepend_path(hlua_states[1], type, path);
8484 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008485
8486err2:
8487 free(p->type);
8488 free(p->path);
8489err:
8490 free(p);
8491 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008492}
8493
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008494/* configuration keywords declaration */
8495static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008496 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008497 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008498 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008499 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8500 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008501 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008502 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008503 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008504 { 0, NULL, NULL },
8505}};
8506
Willy Tarreau0108d902018-11-25 19:14:37 +01008507INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8508
Christopher Fauletafd8f102018-11-08 11:34:21 +01008509
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008510/* This function can fail with an abort() due to an Lua critical error.
8511 * We are in the initialisation process of HAProxy, this abort() is
8512 * tolerated.
8513 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008514int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008515{
8516 struct hlua_init_function *init;
8517 const char *msg;
8518 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008519 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008520 const char *kind;
8521 const char *trace;
8522 int return_status = 1;
8523#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8524 int nres;
8525#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008526
Willy Tarreaucdb53462020-12-02 12:12:00 +01008527 /* disable memory limit checks if limit is not set */
8528 if (!hlua_global_allocator.limit)
8529 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8530
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008531 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008532 if (setjmp(safe_ljmp_env) != 0) {
8533 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008534 if (lua_type(L, -1) == LUA_TSTRING)
8535 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008536 else
8537 error = "critical error";
8538 fprintf(stderr, "Lua post-init: %s.\n", error);
8539 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008540 } else {
8541 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008542 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008543
Thierry Fournierb8cef172020-11-28 15:37:17 +01008544 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008545
Thierry Fournierc7492592020-11-28 23:57:24 +01008546 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008547 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008548
8549#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008550 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008551#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008552 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008553#endif
8554 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008555 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008556
8557 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008558 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008559 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008560
8561 case LUA_ERRERR:
8562 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008563 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008564 case LUA_ERRRUN:
8565 if (!kind)
8566 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008567 msg = lua_tostring(L, -1);
8568 lua_settop(L, 0); /* Empty the stack. */
8569 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008570 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008571 if (msg)
8572 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8573 else
8574 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8575 return_status = 0;
8576 break;
8577
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008578 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008579 /* Unknown error */
8580 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008581 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008582 case LUA_YIELD:
8583 /* yield is not configured at this step, this state doesn't happen */
8584 if (!kind)
8585 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008586 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008587 case LUA_ERRMEM:
8588 if (!kind)
8589 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008590 lua_settop(L, 0);
8591 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008592 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008593 ha_alert("Lua init: %s: %s\n", kind, trace);
8594 return_status = 0;
8595 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008596 }
Thierry Fournier670db242020-11-28 10:49:59 +01008597 if (!return_status)
8598 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008599 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008600
8601 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008602 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008603}
8604
Thierry Fournierb8cef172020-11-28 15:37:17 +01008605int hlua_post_init()
8606{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008607 int ret;
8608 int i;
8609 int errors;
8610 char *err = NULL;
8611 struct hlua_function *fcn;
8612
Thierry Fournierb8cef172020-11-28 15:37:17 +01008613#if USE_OPENSSL
8614 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008615 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008616 int saved_used_backed = global.ssl_used_backend;
8617 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008618 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008619 global.ssl_used_backend = saved_used_backed;
8620 }
8621#endif
8622
Thierry Fournierc7492592020-11-28 23:57:24 +01008623 /* Perform post init of common thread */
8624 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008625 ha_set_tid(0);
8626 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8627 if (ret == 0)
8628 return 0;
8629
8630 /* init remaining lua states and load files */
8631 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8632
8633 /* set thread context */
8634 ha_set_tid(hlua_state_id - 1);
8635
8636 /* Init lua state */
8637 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8638
8639 /* Load lua files */
8640 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8641 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8642 if (ret != 0) {
8643 ha_alert("Lua init: %s\n", err);
8644 return 0;
8645 }
8646 }
8647 }
8648
8649 /* Reset thread context */
8650 ha_set_tid(0);
8651
8652 /* Execute post init for all states */
8653 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8654
8655 /* set thread context */
8656 ha_set_tid(hlua_state_id - 1);
8657
8658 /* run post init */
8659 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8660 if (ret == 0)
8661 return 0;
8662 }
8663
8664 /* Reset thread context */
8665 ha_set_tid(0);
8666
8667 /* control functions registering. Each function must have:
8668 * - only the function_ref[0] set positive and all other to -1
8669 * - only the function_ref[0] set to -1 and all other positive
8670 * This ensure a same reference is not used both in shared
8671 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008672 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008673 * complicated to found for the end user.
8674 */
8675 errors = 0;
8676 list_for_each_entry(fcn, &referenced_functions, l) {
8677 ret = 0;
8678 for (i = 1; i < global.nbthread + 1; i++) {
8679 if (fcn->function_ref[i] == -1)
8680 ret--;
8681 else
8682 ret++;
8683 }
8684 if (abs(ret) != global.nbthread) {
8685 ha_alert("Lua function '%s' is not referenced in all thread. "
8686 "Expect function in all thread or in none thread.\n", fcn->name);
8687 errors++;
8688 continue;
8689 }
8690
8691 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008692 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8693 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008694 "exclusive.\n", fcn->name);
8695 errors++;
8696 }
8697 }
8698
8699 if (errors > 0)
8700 return 0;
8701
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008702 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008703 * -1 in order to have probably a segfault if someone use it
8704 */
8705 hlua_state_id = -1;
8706
8707 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008708}
8709
Willy Tarreau32f61e22015-03-18 17:54:59 +01008710/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8711 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8712 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008713 * allocation. <nsize> is the requested new size. A new allocation is
8714 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008715 * zero. This one verifies that the limits are respected but is optimized
8716 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008717 */
8718static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8719{
8720 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008721 size_t limit, old, new;
8722
Tim Duesterhus22586522021-01-08 10:35:33 +01008723 if (unlikely(!ptr && !nsize))
8724 return NULL;
8725
Willy Tarreaucdb53462020-12-02 12:12:00 +01008726 /* a limit of ~0 means unlimited and boot complete, so there's no need
8727 * for accounting anymore.
8728 */
Christopher Fauletcc2c4f82021-03-24 14:52:24 +01008729 if (likely(~zone->limit == 0))
8730 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008731
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008732 if (!ptr)
8733 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008734
Willy Tarreaucdb53462020-12-02 12:12:00 +01008735 /* enforce strict limits across all threads */
8736 limit = zone->limit;
8737 old = _HA_ATOMIC_LOAD(&zone->allocated);
8738 do {
8739 new = old + nsize - osize;
8740 if (unlikely(nsize && limit && new > limit))
8741 return NULL;
8742 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008743
8744 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008745
8746 if (unlikely(!ptr && nsize)) // failed
8747 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8748
8749 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008750 return ptr;
8751}
8752
Thierry Fournierecb83c22020-11-28 15:49:44 +01008753/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008754 * We are in the initialisation process of HAProxy, this abort() is
8755 * tolerated.
8756 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008757lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008758{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008759 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008760 int idx;
8761 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008762 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008763 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008764 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008765 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008766 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008767 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008768
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008769 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008770 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008771
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008772 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008773 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008774 *context = NULL;
8775
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008776 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008777 * the Lua function can fail with an abort. We are in the initialisation
8778 * process of HAProxy, this abort() is tolerated.
8779 */
8780
Thierry Fournier3c539322020-11-28 16:05:05 +01008781 /* Call post initialisation function in safe environment. */
8782 if (setjmp(safe_ljmp_env) != 0) {
8783 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008784 if (lua_type(L, -1) == LUA_TSTRING)
8785 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008786 else
8787 error_msg = "critical error";
8788 fprintf(stderr, "Lua init: %s.\n", error_msg);
8789 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008790 } else {
8791 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008792 }
8793
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008794 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008795 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008796#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8797#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8798#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008799 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008800#endif
8801#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008802 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008803#endif
8804#undef HLUA_PREPEND_PATH_TOSTRING
8805#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008806
Thierry Fournier59f11be2020-11-29 00:37:41 +01008807 /* Apply configured prepend path */
8808 list_for_each_entry(pp, &prepend_path_list, l)
8809 hlua_prepend_path(L, pp->type, pp->path);
8810
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008811 /*
8812 *
8813 * Create "core" object.
8814 *
8815 */
8816
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008817 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008818 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008819
Thierry Fournierecb83c22020-11-28 15:49:44 +01008820 /* set the thread id */
8821 hlua_class_const_int(L, "thread", thread_num);
8822
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008823 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008824 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008825 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008826
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008827 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008828 hlua_class_function(L, "register_init", hlua_register_init);
8829 hlua_class_function(L, "register_task", hlua_register_task);
8830 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8831 hlua_class_function(L, "register_converters", hlua_register_converters);
8832 hlua_class_function(L, "register_action", hlua_register_action);
8833 hlua_class_function(L, "register_service", hlua_register_service);
8834 hlua_class_function(L, "register_cli", hlua_register_cli);
8835 hlua_class_function(L, "yield", hlua_yield);
8836 hlua_class_function(L, "set_nice", hlua_set_nice);
8837 hlua_class_function(L, "sleep", hlua_sleep);
8838 hlua_class_function(L, "msleep", hlua_msleep);
8839 hlua_class_function(L, "add_acl", hlua_add_acl);
8840 hlua_class_function(L, "del_acl", hlua_del_acl);
8841 hlua_class_function(L, "set_map", hlua_set_map);
8842 hlua_class_function(L, "del_map", hlua_del_map);
8843 hlua_class_function(L, "tcp", hlua_socket_new);
8844 hlua_class_function(L, "log", hlua_log);
8845 hlua_class_function(L, "Debug", hlua_log_debug);
8846 hlua_class_function(L, "Info", hlua_log_info);
8847 hlua_class_function(L, "Warning", hlua_log_warning);
8848 hlua_class_function(L, "Alert", hlua_log_alert);
8849 hlua_class_function(L, "done", hlua_done);
8850 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008851
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008852 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008853
8854 /*
8855 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008856 * Create "act" object.
8857 *
8858 */
8859
8860 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008861 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008862
8863 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008864 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8865 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8866 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8867 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8868 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8869 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8870 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8871 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008872
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008873 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008874
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008875 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008876
8877 /*
8878 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008879 * Register class Map
8880 *
8881 */
8882
8883 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008884 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008885
8886 /* register pattern types. */
8887 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008888 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008889 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008890 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008891 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008892 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008893
8894 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008895 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008896
8897 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008898 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008899
Ilya Shipitsind4259502020-04-08 01:07:56 +05008900 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008901 lua_pushstring(L, "__index");
8902 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008903
8904 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008905 hlua_class_function(L, "lookup", hlua_map_lookup);
8906 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008907
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008908 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008909
Thierry Fournier45e78d72016-02-19 18:34:46 +01008910 /* Register previous table in the registry with reference and named entry.
8911 * The function hlua_register_metatable() pops the stack, so we
8912 * previously create a copy of the table.
8913 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008914 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8915 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008916
8917 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008918 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008919
8920 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008921 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008922
8923 /*
8924 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008925 * Register class Channel
8926 *
8927 */
8928
8929 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008930 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008931
Ilya Shipitsind4259502020-04-08 01:07:56 +05008932 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008933 lua_pushstring(L, "__index");
8934 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008935
8936 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008937 hlua_class_function(L, "get", hlua_channel_get);
8938 hlua_class_function(L, "dup", hlua_channel_dup);
8939 hlua_class_function(L, "getline", hlua_channel_getline);
8940 hlua_class_function(L, "set", hlua_channel_set);
8941 hlua_class_function(L, "append", hlua_channel_append);
8942 hlua_class_function(L, "send", hlua_channel_send);
8943 hlua_class_function(L, "forward", hlua_channel_forward);
8944 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
8945 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
8946 hlua_class_function(L, "is_full", hlua_channel_is_full);
8947 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008948
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008949 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008950
8951 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008952 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008953
8954 /*
8955 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008956 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008957 *
8958 */
8959
8960 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008961 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008962
Ilya Shipitsind4259502020-04-08 01:07:56 +05008963 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008964 lua_pushstring(L, "__index");
8965 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008966
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008967 /* Browse existing fetches and create the associated
8968 * object method.
8969 */
8970 sf = NULL;
8971 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008972 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8973 * by an underscore.
8974 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008975 strncpy(trash.area, sf->kw, trash.size);
8976 trash.area[trash.size - 1] = '\0';
8977 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008978 if (*p == '.' || *p == '-' || *p == '+')
8979 *p = '_';
8980
8981 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008982 lua_pushstring(L, trash.area);
8983 lua_pushlightuserdata(L, sf);
8984 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
8985 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008986 }
8987
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008988 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008989
8990 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008991 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008992
8993 /*
8994 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008995 * Register class Converters
8996 *
8997 */
8998
8999 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009000 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009001
9002 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009003 lua_pushstring(L, "__index");
9004 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009005
9006 /* Browse existing converters and create the associated
9007 * object method.
9008 */
9009 sc = NULL;
9010 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009011 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9012 * by an underscore.
9013 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009014 strncpy(trash.area, sc->kw, trash.size);
9015 trash.area[trash.size - 1] = '\0';
9016 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009017 if (*p == '.' || *p == '-' || *p == '+')
9018 *p = '_';
9019
9020 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009021 lua_pushstring(L, trash.area);
9022 lua_pushlightuserdata(L, sc);
9023 lua_pushcclosure(L, hlua_run_sample_conv, 1);
9024 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009025 }
9026
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009027 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009028
9029 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009030 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009031
9032 /*
9033 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009034 * Register class HTTP
9035 *
9036 */
9037
9038 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009039 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009040
Ilya Shipitsind4259502020-04-08 01:07:56 +05009041 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009042 lua_pushstring(L, "__index");
9043 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009044
9045 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009046 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9047 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9048 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9049 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9050 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9051 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9052 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9053 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9054 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9055 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009056
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009057 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9058 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9059 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9060 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9061 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9062 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9063 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009064
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009065 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009066
9067 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009068 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009069
9070 /*
9071 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009072 * Register class AppletTCP
9073 *
9074 */
9075
9076 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009077 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009078
Ilya Shipitsind4259502020-04-08 01:07:56 +05009079 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009080 lua_pushstring(L, "__index");
9081 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009082
9083 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009084 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9085 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9086 hlua_class_function(L, "send", hlua_applet_tcp_send);
9087 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9088 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9089 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9090 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9091 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009092
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009093 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009094
9095 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009096 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009097
9098 /*
9099 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009100 * Register class AppletHTTP
9101 *
9102 */
9103
9104 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009105 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009106
Ilya Shipitsind4259502020-04-08 01:07:56 +05009107 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009108 lua_pushstring(L, "__index");
9109 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009110
9111 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009112 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9113 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9114 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9115 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9116 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9117 hlua_class_function(L, "getline", hlua_applet_http_getline);
9118 hlua_class_function(L, "receive", hlua_applet_http_recv);
9119 hlua_class_function(L, "send", hlua_applet_http_send);
9120 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9121 hlua_class_function(L, "set_status", hlua_applet_http_status);
9122 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009123
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009124 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009125
9126 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009127 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009128
9129 /*
9130 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009131 * Register class TXN
9132 *
9133 */
9134
9135 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009136 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009137
Ilya Shipitsind4259502020-04-08 01:07:56 +05009138 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009139 lua_pushstring(L, "__index");
9140 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009141
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009142 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009143 hlua_class_function(L, "set_priv", hlua_set_priv);
9144 hlua_class_function(L, "get_priv", hlua_get_priv);
9145 hlua_class_function(L, "set_var", hlua_set_var);
9146 hlua_class_function(L, "unset_var", hlua_unset_var);
9147 hlua_class_function(L, "get_var", hlua_get_var);
9148 hlua_class_function(L, "done", hlua_txn_done);
9149 hlua_class_function(L, "reply", hlua_txn_reply_new);
9150 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9151 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9152 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9153 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9154 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9155 hlua_class_function(L, "deflog", hlua_txn_deflog);
9156 hlua_class_function(L, "log", hlua_txn_log);
9157 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9158 hlua_class_function(L, "Info", hlua_txn_log_info);
9159 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9160 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009161
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009162 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009163
9164 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009165 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009166
9167 /*
9168 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009169 * Register class reply
9170 *
9171 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009172 lua_newtable(L);
9173 lua_pushstring(L, "__index");
9174 lua_newtable(L);
9175 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9176 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9177 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9178 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9179 lua_settable(L, -3); /* Sets the __index entry. */
9180 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009181
9182
9183 /*
9184 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009185 * Register class Socket
9186 *
9187 */
9188
9189 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009190 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009191
Ilya Shipitsind4259502020-04-08 01:07:56 +05009192 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009193 lua_pushstring(L, "__index");
9194 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009195
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009196#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009197 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009198#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009199 hlua_class_function(L, "connect", hlua_socket_connect);
9200 hlua_class_function(L, "send", hlua_socket_send);
9201 hlua_class_function(L, "receive", hlua_socket_receive);
9202 hlua_class_function(L, "close", hlua_socket_close);
9203 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9204 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9205 hlua_class_function(L, "setoption", hlua_socket_setoption);
9206 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009207
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009208 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009209
9210 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009211 lua_pushstring(L, "__gc");
9212 lua_pushcclosure(L, hlua_socket_gc, 0);
9213 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009214
9215 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009216 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009217
Thierry Fournieraafc7772020-12-04 11:47:47 +01009218 lua_atpanic(L, hlua_panic_safe);
9219
9220 return L;
9221}
9222
9223void hlua_init(void) {
9224 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009225 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009226#ifdef USE_OPENSSL
9227 struct srv_kw *kw;
9228 int tmp_error;
9229 char *error;
9230 char *args[] = { /* SSL client configuration. */
9231 "ssl",
9232 "verify",
9233 "none",
9234 NULL
9235 };
9236#endif
9237
9238 /* Init post init function list head */
9239 for (i = 0; i < MAX_THREADS + 1; i++)
9240 LIST_INIT(&hlua_init_functions[i]);
9241
9242 /* Init state for common/shared lua parts */
9243 hlua_state_id = 0;
9244 ha_set_tid(0);
9245 hlua_states[0] = hlua_init_state(0);
9246
9247 /* Init state 1 for thread 0. We have at least one thread. */
9248 hlua_state_id = 1;
9249 ha_set_tid(0);
9250 hlua_states[1] = hlua_init_state(1);
9251
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009252 /* Proxy and server configuration initialisation. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009253 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg);
9254 if (!socket_proxy) {
9255 fprintf(stderr, "Lua init: %s\n", errmsg);
9256 exit(1);
9257 }
9258 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009259
9260 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009261 socket_tcp = new_server(socket_proxy);
9262 if (!socket_tcp) {
9263 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9264 exit(1);
9265 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009266
9267#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009268 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009269 socket_ssl = new_server(socket_proxy);
9270 if (!socket_ssl) {
9271 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9272 exit(1);
9273 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009274
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009275 socket_ssl->use_ssl = 1;
9276 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009277
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009278 for (i = 0; args[i] != NULL; i++) {
9279 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009280 /*
9281 *
9282 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009283 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009284 * features like client certificates and ssl_verify.
9285 *
9286 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009287 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009288 if (tmp_error != 0) {
9289 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9290 abort(); /* This must be never arrives because the command line
9291 not editable by the user. */
9292 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009293 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009294 }
9295 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009296#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009297
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009298}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009299
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009300static void hlua_deinit()
9301{
Willy Tarreau186f3762020-12-04 11:48:12 +01009302 int thr;
9303
9304 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9305 if (hlua_states[thr])
9306 lua_close(hlua_states[thr]);
9307 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009308
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009309 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009310
Willy Tarreau0f143af2021-03-05 10:41:48 +01009311#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009312 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009313#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009314
9315 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009316}
9317
9318REGISTER_POST_DEINIT(hlua_deinit);
9319
Willy Tarreau80713382018-11-26 10:19:54 +01009320static void hlua_register_build_options(void)
9321{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009322 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009323
Willy Tarreaubb57d942016-12-21 19:04:56 +01009324 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9325 hap_register_build_opts(ptr, 1);
9326}
Willy Tarreau80713382018-11-26 10:19:54 +01009327
9328INITCALL0(STG_REGISTER, hlua_register_build_options);