blob: 3ca698e6646829125cfe0c3e4aba1dbd2d367ec0 [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 Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020036#include <haproxy/connection.h>
Christopher Faulet69c581a2021-05-31 08:54:04 +020037#include <haproxy/filters.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020038#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020039#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020040#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020041#include <haproxy/http_ana.h>
William Lallemand3956c4e2021-09-21 16:25:15 +020042#include <haproxy/http_client.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020043#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020044#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020045#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020046#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020047#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020048#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020049#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020050#include <haproxy/payload.h>
Willy Tarreau3d6ee402021-05-08 20:28:07 +020051#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020053#include <haproxy/sample.h>
Willy Tarreau198e92a2021-03-05 10:23:32 +010054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020056#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020057#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020058#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020059#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020060#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020061#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020062#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020063#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020064#include <haproxy/xref.h>
65
Thierry FOURNIER380d0932015-01-23 14:27:52 +010066
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010067/* Lua uses longjmp to perform yield or throwing errors. This
68 * macro is used only for identifying the function that can
69 * not return because a longjmp is executed.
70 * __LJMP marks a prototype of hlua file that can use longjmp.
71 * WILL_LJMP() marks an lua function that will use longjmp.
72 * MAY_LJMP() marks an lua function that may use longjmp.
73 */
74#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020075#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010076#define MAY_LJMP(func) func
77
Thierry FOURNIERbabae282015-09-17 11:36:37 +020078/* This couple of function executes securely some Lua calls outside of
79 * the lua runtime environment. Each Lua call can return a longjmp
80 * if it encounter a memory error.
81 *
82 * Lua documentation extract:
83 *
84 * If an error happens outside any protected environment, Lua calls
85 * a panic function (see lua_atpanic) and then calls abort, thus
86 * exiting the host application. Your panic function can avoid this
87 * exit by never returning (e.g., doing a long jump to your own
88 * recovery point outside Lua).
89 *
90 * The panic function runs as if it were a message handler (see
91 * §2.3); in particular, the error message is at the top of the
92 * stack. However, there is no guarantee about stack space. To push
93 * anything on the stack, the panic function must first check the
94 * available space (see §4.2).
95 *
96 * We must check all the Lua entry point. This includes:
97 * - The include/proto/hlua.h exported functions
98 * - the task wrapper function
99 * - The action wrapper function
100 * - The converters wrapper function
101 * - The sample-fetch wrapper functions
102 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500103 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800104 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200105 *
106 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
107 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
108 * because they must be exists in the program stack when the longjmp
109 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200110 *
111 * Note that the Lua processing is not really thread safe. It provides
112 * heavy system which consists to add our own lock function in the Lua
113 * code and recompile the library. This system will probably not accepted
114 * by maintainers of various distribs.
115 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500116 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200117 * quick looking on the Lua sources displays a lua_lock() a the start
118 * of function and a lua_unlock() at the end of the function. So I
119 * conclude that the Lua thread safe mode just perform a mutex around
120 * all execution. So I prefer to do this in the HAProxy code, it will be
121 * easier for distro maintainers.
122 *
123 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
124 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
125 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100127__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200128THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200129static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau6a510902021-07-14 19:41:25 +0200130static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200131
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100132/* This is the chained list of struct hlua_function referenced
133 * for haproxy action, sample-fetches, converters, cli and
134 * applet bindings. It is used for a post-initialisation control.
135 */
136static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions);
137
Thierry Fournierc7492592020-11-28 23:57:24 +0100138/* This variable is used only during initialization to identify the Lua state
139 * currently being initialized. 0 is the common lua state, 1 to n are the Lua
140 * states dedicated to each thread (in this case hlua_state_id==tid+1).
141 */
142static int hlua_state_id;
143
Thierry Fournier59f11be2020-11-29 00:37:41 +0100144/* This is a NULL-terminated list of lua file which are referenced to load per thread */
145static char **per_thread_load = NULL;
146
147lua_State *hlua_init_state(int thread_id);
148
Willy Tarreau1e7bef12021-08-20 15:47:25 +0200149/* This function takes the Lua global lock. Keep this function's visibility
150 * global so that it can appear in stack dumps and performance profiles!
151 */
152void lua_take_global_lock()
153{
154 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
155}
156
157static inline void lua_drop_global_lock()
158{
159 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
160}
161
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100162#define SET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200163 ({ \
164 int ret; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100165 if ((__HLUA)->state_id == 0) \
Willy Tarreau1e7bef12021-08-20 15:47:25 +0200166 lua_take_global_lock(); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200167 if (setjmp(safe_ljmp_env) != 0) { \
168 lua_atpanic(__L, hlua_panic_safe); \
169 ret = 0; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100170 if ((__HLUA)->state_id == 0) \
Willy Tarreau1e7bef12021-08-20 15:47:25 +0200171 lua_drop_global_lock(); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200172 } else { \
173 lua_atpanic(__L, hlua_panic_ljmp); \
174 ret = 1; \
175 } \
176 ret; \
177 })
178
179/* If we are the last function catching Lua errors, we
180 * must reset the panic function.
181 */
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100182#define RESET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200183 do { \
184 lua_atpanic(__L, hlua_panic_safe); \
Thierry Fournier021d9862020-11-28 23:42:03 +0100185 if ((__HLUA)->state_id == 0) \
Willy Tarreau1e7bef12021-08-20 15:47:25 +0200186 lua_drop_global_lock(); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200187 } while(0)
188
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100189#define SET_SAFE_LJMP(__HLUA) \
190 SET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
191
192#define RESET_SAFE_LJMP(__HLUA) \
193 RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
194
195#define SET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100196 SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100197
198#define RESET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100199 RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100200
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200201/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200202#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100203/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200204#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200205/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100206#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100207#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200208
Thierry Fournierafc63e22020-11-28 17:06:51 +0100209/* The main Lua execution context. The 0 index is the
210 * common state shared by all threads.
211 */
Willy Tarreau186f3762020-12-04 11:48:12 +0100212static lua_State *hlua_states[MAX_THREADS + 1];
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100213
Christopher Fauletc404f112020-02-26 15:03:09 +0100214#define HLUA_FLT_CB_FINAL 0x00000001
215#define HLUA_FLT_CB_RETVAL 0x00000002
216#define HLUA_FLT_CB_ARG_CHN 0x00000004
217#define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008
218
Christopher Faulet9f55a502020-02-25 15:21:02 +0100219#define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001
220
Christopher Faulet69c581a2021-05-31 08:54:04 +0200221struct hlua_reg_filter {
222 char *name;
223 int flt_ref[MAX_THREADS + 1];
224 int fun_ref[MAX_THREADS + 1];
225 struct list l;
226};
227
228struct hlua_flt_config {
229 struct hlua_reg_filter *reg;
230 int ref[MAX_THREADS + 1];
231 char **args;
232};
233
234struct hlua_flt_ctx {
235 int ref; /* ref to the filter lua object */
236 struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */
237 unsigned int cur_off[2]; /* current offset (0: request, 1: response) */
238 unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */
239 unsigned int flags; /* HLUA_FLT_CTX_FL_* */
240};
241
242DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx));
243
Christopher Faulet9f55a502020-02-25 15:21:02 +0100244static int hlua_filter_from_payload(struct filter *filter);
245
Christopher Faulet69c581a2021-05-31 08:54:04 +0200246/* This is the chained list of struct hlua_flt referenced
247 * for haproxy filters. It is used for a post-initialisation control.
248 */
249static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters);
250
251
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100252/* This is the memory pool containing struct lua for applets
253 * (including cli).
254 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100255DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100256
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100257/* Used for Socket connection. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +0100258static struct proxy *socket_proxy;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100259static struct server *socket_tcp;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100260#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100261static struct server *socket_ssl;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100262#endif
263
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100264/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100265struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100266
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100267/* The following variables contains the reference of the different
268 * Lua classes. These references are useful for identify metadata
269 * associated with an object.
270 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100271static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100272static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100273static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100274static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100275static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100276static int class_http_ref;
Christopher Fauletdf97ac42020-02-26 16:57:19 +0100277static int class_http_msg_ref;
William Lallemand3956c4e2021-09-21 16:25:15 +0200278static int class_httpclient_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200279static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200280static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200281static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100282static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100283
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100284/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200285 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100286 * short timeout. Lua linked with tasks doesn't have a timeout
287 * because a task may remain alive during all the haproxy execution.
288 */
289static unsigned int hlua_timeout_session = 4000; /* session timeout. */
290static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200291static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100292
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100293/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
294 * it is used for preventing infinite loops.
295 *
296 * I test the scheer with an infinite loop containing one incrementation
297 * and one test. I run this loop between 10 seconds, I raise a ceil of
298 * 710M loops from one interrupt each 9000 instructions, so I fix the value
299 * to one interrupt each 10 000 instructions.
300 *
301 * configured | Number of
302 * instructions | loops executed
303 * between two | in milions
304 * forced yields |
305 * ---------------+---------------
306 * 10 | 160
307 * 500 | 670
308 * 1000 | 680
309 * 5000 | 700
310 * 7000 | 700
311 * 8000 | 700
312 * 9000 | 710 <- ceil
313 * 10000 | 710
314 * 100000 | 710
315 * 1000000 | 710
316 *
317 */
318static unsigned int hlua_nb_instruction = 10000;
319
Willy Tarreaucdb53462020-12-02 12:12:00 +0100320/* Descriptor for the memory allocation state. The limit is pre-initialised to
321 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
322 * is replaced with ~0 during post_init after everything was loaded. This way
323 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
324 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100325 */
326struct hlua_mem_allocator {
327 size_t allocated;
328 size_t limit;
329};
330
Willy Tarreaucdb53462020-12-02 12:12:00 +0100331static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100332
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100333/* These functions converts types between HAProxy internal args or
334 * sample and LUA types. Another function permits to check if the
335 * LUA stack contains arguments according with an required ARG_T
336 * format.
337 */
338static int hlua_arg2lua(lua_State *L, const struct arg *arg);
339static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100340__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100341 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100342static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100343static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100344static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
345
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100346__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200347
Thierry Fournier59f11be2020-11-29 00:37:41 +0100348struct prepend_path {
349 struct list l;
350 char *type;
351 char *path;
352};
353
354static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
355
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200356#define SEND_ERR(__be, __fmt, __args...) \
357 do { \
358 send_log(__be, LOG_ERR, __fmt, ## __args); \
359 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100360 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200361 } while (0)
362
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100363static inline struct hlua_function *new_hlua_function()
364{
365 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100366 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100367
368 fcn = calloc(1, sizeof(*fcn));
369 if (!fcn)
370 return NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200371 LIST_APPEND(&referenced_functions, &fcn->l);
Thierry Fournierc7492592020-11-28 23:57:24 +0100372 for (i = 0; i < MAX_THREADS + 1; i++)
373 fcn->function_ref[i] = -1;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100374 return fcn;
375}
376
Christopher Fauletdda44442021-04-12 14:05:43 +0200377static inline void release_hlua_function(struct hlua_function *fcn)
378{
379 if (!fcn)
380 return;
381 if (fcn->name)
382 ha_free(&fcn->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200383 LIST_DELETE(&fcn->l);
Christopher Fauletdda44442021-04-12 14:05:43 +0200384 ha_free(&fcn);
385}
386
Thierry Fournierc7492592020-11-28 23:57:24 +0100387/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
388static inline int fcn_ref_to_stack_id(struct hlua_function *fcn)
389{
390 if (fcn->function_ref[0] == -1)
391 return tid + 1;
392 return 0;
393}
394
Christopher Faulet69c581a2021-05-31 08:54:04 +0200395/* Create a new registered filter. Only its name is filled */
396static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name)
397{
398 struct hlua_reg_filter *reg_flt;
399 int i;
400
401 reg_flt = calloc(1, sizeof(*reg_flt));
402 if (!reg_flt)
403 return NULL;
404 reg_flt->name = strdup(name);
405 if (!reg_flt->name) {
406 free(reg_flt);
407 return NULL;
408 }
409 LIST_APPEND(&referenced_filters, &reg_flt->l);
410 for (i = 0; i < MAX_THREADS + 1; i++) {
411 reg_flt->flt_ref[i] = -1;
412 reg_flt->fun_ref[i] = -1;
413 }
414 return reg_flt;
415}
416
417/* Release a registered filter */
418static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt)
419{
420 if (!reg_flt)
421 return;
422 if (reg_flt->name)
423 ha_free(&reg_flt->name);
424 LIST_DELETE(&reg_flt->l);
425 ha_free(&reg_flt);
426}
427
428/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
429static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt)
430{
431 if (reg_flt->fun_ref[0] == -1)
432 return tid + 1;
433 return 0;
434}
435
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100436/* Used to check an Lua function type in the stack. It creates and
437 * returns a reference of the function. This function throws an
438 * error if the rgument is not a "function".
439 */
440__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
441{
442 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100443 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100444 WILL_LJMP(luaL_argerror(L, argno, msg));
445 }
446 lua_pushvalue(L, argno);
447 return luaL_ref(L, LUA_REGISTRYINDEX);
448}
449
Christopher Fauletba9e21d2020-02-25 10:20:04 +0100450/* Used to check an Lua table type in the stack. It creates and
451 * returns a reference of the table. This function throws an
452 * error if the rgument is not a "table".
453 */
454__LJMP unsigned int hlua_checktable(lua_State *L, int argno)
455{
456 if (!lua_istable(L, argno)) {
457 const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno));
458 WILL_LJMP(luaL_argerror(L, argno, msg));
459 }
460 lua_pushvalue(L, argno);
461 return luaL_ref(L, LUA_REGISTRYINDEX);
462}
463
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200464/* Return the string that is of the top of the stack. */
465const char *hlua_get_top_error_string(lua_State *L)
466{
467 if (lua_gettop(L) < 1)
468 return "unknown error";
469 if (lua_type(L, -1) != LUA_TSTRING)
470 return "unknown error";
471 return lua_tostring(L, -1);
472}
473
Christopher Fauletd09cc512021-03-24 14:48:45 +0100474__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200475{
476 lua_Debug ar;
477 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200478 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200479
480 while (lua_getstack(L, level++, &ar)) {
481
482 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100483 if (b_data(msg))
484 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200485
486 /* Fill fields:
487 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
488 * 'l': fills in the field currentline;
489 * 'n': fills in the field name and namewhat;
490 * 't': fills in the field istailcall;
491 */
492 lua_getinfo(L, "Slnt", &ar);
493
494 /* Append code localisation */
495 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100496 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200497 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100498 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200499
500 /*
501 * Get function name
502 *
503 * if namewhat is no empty, name is defined.
504 * what contains "Lua" for Lua function, "C" for C function,
505 * or "main" for main code.
506 */
507 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100508 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200509
510 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100511 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200512
513 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100514 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200515
516 else /* nothing left... */
517 chunk_appendf(msg, "?");
518
519
520 /* Display tailed call */
521 if (ar.istailcall)
522 chunk_appendf(msg, " ...");
523 }
524
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200525 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200526}
527
528
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100529/* This function check the number of arguments available in the
530 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500531 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100532 */
533__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
534{
535 if (lua_gettop(L) == nb)
536 return;
537 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
538}
539
Mark Lakes22154b42018-01-29 14:38:40 -0800540/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100541 * and the line number where the error is encountered.
542 */
543static int hlua_pusherror(lua_State *L, const char *fmt, ...)
544{
545 va_list argp;
546 va_start(argp, fmt);
547 luaL_where(L, 1);
548 lua_pushvfstring(L, fmt, argp);
549 va_end(argp);
550 lua_concat(L, 2);
551 return 1;
552}
553
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100554/* This functions is used with sample fetch and converters. It
555 * converts the HAProxy configuration argument in a lua stack
556 * values.
557 *
558 * It takes an array of "arg", and each entry of the array is
559 * converted and pushed in the LUA stack.
560 */
561static int hlua_arg2lua(lua_State *L, const struct arg *arg)
562{
563 switch (arg->type) {
564 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100565 case ARGT_TIME:
566 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100567 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100568 break;
569
570 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200571 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100572 break;
573
574 case ARGT_IPV4:
575 case ARGT_IPV6:
576 case ARGT_MSK4:
577 case ARGT_MSK6:
578 case ARGT_FE:
579 case ARGT_BE:
580 case ARGT_TAB:
581 case ARGT_SRV:
582 case ARGT_USR:
583 case ARGT_MAP:
584 default:
585 lua_pushnil(L);
586 break;
587 }
588 return 1;
589}
590
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500591/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500593 * with sample fetch wrappers. The input arguments are given to the
594 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595 */
596static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
597{
598 switch (lua_type(L, ud)) {
599
600 case LUA_TNUMBER:
601 case LUA_TBOOLEAN:
602 arg->type = ARGT_SINT;
603 arg->data.sint = lua_tointeger(L, ud);
604 break;
605
606 case LUA_TSTRING:
607 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200608 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200609 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200610 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200611 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100612 break;
613
614 case LUA_TUSERDATA:
615 case LUA_TNIL:
616 case LUA_TTABLE:
617 case LUA_TFUNCTION:
618 case LUA_TTHREAD:
619 case LUA_TLIGHTUSERDATA:
620 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200621 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100622 break;
623 }
624 return 1;
625}
626
627/* the following functions are used to convert a struct sample
628 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500629 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100630 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100631static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100632{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200633 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100634 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100635 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200636 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100637 break;
638
639 case SMP_T_BIN:
640 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200641 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100642 break;
643
644 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200645 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100646 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
647 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
648 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
649 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
650 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
651 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
652 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
653 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
654 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200655 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100656 break;
657 default:
658 lua_pushnil(L);
659 break;
660 }
661 break;
662
663 case SMP_T_IPV4:
664 case SMP_T_IPV6:
665 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200666 if (sample_casts[smp->data.type][SMP_T_STR] &&
667 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200668 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100669 else
670 lua_pushnil(L);
671 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100672 default:
673 lua_pushnil(L);
674 break;
675 }
676 return 1;
677}
678
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100679/* the following functions are used to convert a struct sample
680 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500681 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100682 */
683static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
684{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200685 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100686
687 case SMP_T_BIN:
688 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100690 break;
691
692 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200693 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100694 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
695 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
696 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
697 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
698 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
699 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
700 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
701 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
702 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100704 break;
705 default:
706 lua_pushstring(L, "");
707 break;
708 }
709 break;
710
711 case SMP_T_SINT:
712 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100713 case SMP_T_IPV4:
714 case SMP_T_IPV6:
715 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200716 if (sample_casts[smp->data.type][SMP_T_STR] &&
717 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200718 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100719 else
720 lua_pushstring(L, "");
721 break;
722 default:
723 lua_pushstring(L, "");
724 break;
725 }
726 return 1;
727}
728
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100729/* the following functions are used to convert an Lua type in a
730 * struct sample. This is useful to provide data from a converter
731 * to the LUA code.
732 */
733static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
734{
735 switch (lua_type(L, ud)) {
736
737 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200738 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200739 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100740 break;
741
742
743 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200744 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200745 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100746 break;
747
748 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200749 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100750 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200751 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200752 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200753 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200754 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100755 break;
756
757 case LUA_TUSERDATA:
758 case LUA_TNIL:
759 case LUA_TTABLE:
760 case LUA_TFUNCTION:
761 case LUA_TTHREAD:
762 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200763 case LUA_TNONE:
764 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200765 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200766 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100767 break;
768 }
769 return 1;
770}
771
Ilya Shipitsind4259502020-04-08 01:07:56 +0500772/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800773 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100774 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100775 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500776 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Willy Tarreauee0d7272021-07-16 10:26:56 +0200777 * entries and that there is at least one stop at the last position.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100778 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100779__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100780 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100781{
782 int min_arg;
Willy Tarreauee0d7272021-07-16 10:26:56 +0200783 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100784 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200785 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200786 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200787 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200788 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100789
790 idx = 0;
791 min_arg = ARGM(mask);
792 mask >>= ARGM_BITS;
793
794 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200795 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100796
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100797 /* Check for mandatory arguments. */
798 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100799 if (idx < min_arg) {
800
801 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200802 if (idx > 0) {
803 msg = "Mandatory argument expected";
804 goto error;
805 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100806
807 /* If first argument have a certain type, some default values
808 * may be used. See the function smp_resolve_args().
809 */
810 switch (mask & ARGT_MASK) {
811
812 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200813 if (!(p->cap & PR_CAP_FE)) {
814 msg = "Mandatory argument expected";
815 goto error;
816 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100817 argp[idx].data.prx = p;
818 argp[idx].type = ARGT_FE;
819 argp[idx+1].type = ARGT_STOP;
820 break;
821
822 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200823 if (!(p->cap & PR_CAP_BE)) {
824 msg = "Mandatory argument expected";
825 goto error;
826 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100827 argp[idx].data.prx = p;
828 argp[idx].type = ARGT_BE;
829 argp[idx+1].type = ARGT_STOP;
830 break;
831
832 case ARGT_TAB:
833 argp[idx].data.prx = p;
834 argp[idx].type = ARGT_TAB;
835 argp[idx+1].type = ARGT_STOP;
836 break;
837
838 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200839 msg = "Mandatory argument expected";
840 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100841 break;
842 }
843 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200844 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100845 }
846
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500847 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100848 if ((mask & ARGT_MASK) == ARGT_STOP &&
849 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200850 msg = "Last argument expected";
851 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100852 }
853
854 if ((mask & ARGT_MASK) == ARGT_STOP &&
855 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200856 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100857 }
858
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200859 /* Convert some argument types. All string in argp[] are for not
860 * duplicated yet.
861 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100862 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100863 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200864 if (argp[idx].type != ARGT_SINT) {
865 msg = "integer expected";
866 goto error;
867 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100868 argp[idx].type = ARGT_SINT;
869 break;
870
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100871 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200872 if (argp[idx].type != ARGT_SINT) {
873 msg = "integer expected";
874 goto error;
875 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200876 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100877 break;
878
879 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200880 if (argp[idx].type != ARGT_SINT) {
881 msg = "integer expected";
882 goto error;
883 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200884 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100885 break;
886
887 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200888 if (argp[idx].type != ARGT_STR) {
889 msg = "string expected";
890 goto error;
891 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200892 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200893 if (!argp[idx].data.prx) {
894 msg = "frontend doesn't exist";
895 goto error;
896 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100897 argp[idx].type = ARGT_FE;
898 break;
899
900 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200901 if (argp[idx].type != ARGT_STR) {
902 msg = "string expected";
903 goto error;
904 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200905 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200906 if (!argp[idx].data.prx) {
907 msg = "backend doesn't exist";
908 goto error;
909 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100910 argp[idx].type = ARGT_BE;
911 break;
912
913 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200914 if (argp[idx].type != ARGT_STR) {
915 msg = "string expected";
916 goto error;
917 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200918 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200919 if (!argp[idx].data.t) {
920 msg = "table doesn't exist";
921 goto error;
922 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100923 argp[idx].type = ARGT_TAB;
924 break;
925
926 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200927 if (argp[idx].type != ARGT_STR) {
928 msg = "string expected";
929 goto error;
930 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200931 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100932 if (sname) {
933 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200934 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200935 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200936 if (!px) {
937 msg = "backend doesn't exist";
938 goto error;
939 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100940 }
941 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200942 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100943 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100944 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100945 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200946 if (!argp[idx].data.srv) {
947 msg = "server doesn't exist";
948 goto error;
949 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100950 argp[idx].type = ARGT_SRV;
951 break;
952
953 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200954 if (argp[idx].type != ARGT_STR) {
955 msg = "string expected";
956 goto error;
957 }
958 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
959 msg = "invalid IPv4 address";
960 goto error;
961 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100962 argp[idx].type = ARGT_IPV4;
963 break;
964
965 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200966 if (argp[idx].type == ARGT_SINT)
967 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
968 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200969 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
970 msg = "invalid IPv4 mask";
971 goto error;
972 }
973 }
974 else {
975 msg = "integer or string expected";
976 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200977 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100978 argp[idx].type = ARGT_MSK4;
979 break;
980
981 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200982 if (argp[idx].type != ARGT_STR) {
983 msg = "string expected";
984 goto error;
985 }
986 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
987 msg = "invalid IPv6 address";
988 goto error;
989 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100990 argp[idx].type = ARGT_IPV6;
991 break;
992
993 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200994 if (argp[idx].type == ARGT_SINT)
995 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
996 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200997 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
998 msg = "invalid IPv6 mask";
999 goto error;
1000 }
1001 }
1002 else {
1003 msg = "integer or string expected";
1004 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +02001005 }
Tim Duesterhusb814da62018-01-25 16:24:50 +01001006 argp[idx].type = ARGT_MSK6;
1007 break;
1008
Christopher Fauletfd2e9062020-08-06 11:10:57 +02001009 case ARGT_REG:
1010 if (argp[idx].type != ARGT_STR) {
1011 msg = "string expected";
1012 goto error;
1013 }
1014 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
1015 if (!reg) {
1016 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
1017 argp[idx].data.str.area, err);
1018 free(err);
1019 goto error;
1020 }
1021 argp[idx].type = ARGT_REG;
1022 argp[idx].data.reg = reg;
1023 break;
1024
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +01001025 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +02001026 if (argp[idx].type != ARGT_STR) {
1027 msg = "string expected";
1028 goto error;
1029 }
1030 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001031 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +02001032 ul = p->uri_auth->userlist;
1033 else
1034 ul = auth_find_userlist(argp[idx].data.str.area);
1035
1036 if (!ul) {
1037 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
1038 goto error;
1039 }
1040 argp[idx].type = ARGT_USR;
1041 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001042 break;
1043
1044 case ARGT_STR:
1045 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
1046 msg = "unable to duplicate string arg";
1047 goto error;
1048 }
1049 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001050 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001051
Christopher Fauletd25d9262020-08-06 11:04:46 +02001052 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +02001053 msg = "type not yet supported";
1054 goto error;
1055 break;
1056
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001057 }
1058
1059 /* Check for type of argument. */
1060 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001061 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
1062 arg_type_names[(mask & ARGT_MASK)],
1063 arg_type_names[argp[idx].type & ARGT_MASK]);
1064 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001065 }
1066
1067 /* Next argument. */
1068 mask >>= ARGT_BITS;
1069 idx++;
1070 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001071 return 0;
1072
1073 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02001074 free_args(argp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001075 WILL_LJMP(luaL_argerror(L, first + idx, msg));
1076 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001077}
1078
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001079/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001080 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001081 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001082 *
1083 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001084 * - hlua_sethlua : create the association between hlua context and lua_state.
1085 */
1086static inline struct hlua *hlua_gethlua(lua_State *L)
1087{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001088 struct hlua **hlua = lua_getextraspace(L);
1089 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090}
1091static inline void hlua_sethlua(struct hlua *hlua)
1092{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001093 struct hlua **hlua_store = lua_getextraspace(hlua->T);
1094 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001095}
1096
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001097/* This function is used to send logs. It try to send on screen (stderr)
1098 * and on the default syslog server.
1099 */
1100static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
1101{
1102 struct tm tm;
1103 char *p;
1104
1105 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001106 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001107 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001108 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001109 /* Break the message if exceed the buffer size. */
1110 *(p-4) = ' ';
1111 *(p-3) = '.';
1112 *(p-2) = '.';
1113 *(p-1) = '.';
1114 break;
1115 }
Willy Tarreau90807112020-02-25 08:16:33 +01001116 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001117 *p = *msg;
1118 else
1119 *p = '.';
1120 }
1121 *p = '\0';
1122
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001123 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001124 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001125 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1126 return;
1127
Willy Tarreaua678b432015-08-28 10:14:59 +02001128 get_localtime(date.tv_sec, &tm);
1129 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001130 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001131 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001132 fflush(stderr);
1133 }
1134}
1135
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001136/* This function just ensure that the yield will be always
1137 * returned with a timeout and permit to set some flags
1138 */
1139__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001140 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001141{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001142 struct hlua *hlua;
1143
1144 /* Get hlua struct, or NULL if we execute from main lua state */
1145 hlua = hlua_gethlua(L);
1146 if (!hlua) {
1147 return;
1148 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001149
1150 /* Set the wake timeout. If timeout is required, we set
1151 * the expiration time.
1152 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001153 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001154
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001155 hlua->flags |= flags;
1156
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001157 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001158 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001159}
1160
Willy Tarreau87b09662015-04-03 00:22:06 +02001161/* This function initialises the Lua environment stored in the stream.
1162 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001163 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001164 *
1165 * This function is particular. it initialises a new Lua thread. If the
1166 * initialisation fails (example: out of memory error), the lua function
1167 * throws an error (longjmp).
1168 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001169 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001170 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001171 * threads appear, the safe environment set a lock to ensure only one
1172 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001173 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001174 *
1175 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001176 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001177 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001178 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001179 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001180 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001181 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001183int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001184{
1185 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001186 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001187 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001188 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001189 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001190 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001191 if (!already_safe) {
1192 if (!SET_SAFE_LJMP_PARENT(lua)) {
1193 lua->Tref = LUA_REFNIL;
1194 return 0;
1195 }
1196 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001197 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001198 if (!lua->T) {
1199 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001200 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001201 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001202 return 0;
1203 }
1204 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001205 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001207 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001208 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001209 return 1;
1210}
1211
Willy Tarreau87b09662015-04-03 00:22:06 +02001212/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001213 * is destroyed. The destroy also the memory context. The struct "lua"
1214 * is not freed.
1215 */
1216void hlua_ctx_destroy(struct hlua *lua)
1217{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001218 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001219 return;
1220
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001221 if (!lua->T)
1222 goto end;
1223
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001224 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001225 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001226
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001227 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001228 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001229 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001230 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001231
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001232 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001233 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001234 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001235 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001236 /* Forces a garbage collecting process. If the Lua program is finished
1237 * without error, we run the GC on the thread pointer. Its freed all
1238 * the unused memory.
1239 * If the thread is finnish with an error or is currently yielded,
1240 * it seems that the GC applied on the thread doesn't clean anything,
1241 * so e run the GC on the main thread.
1242 * NOTE: maybe this action locks all the Lua threads untiml the en of
1243 * the garbage collection.
1244 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001245 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001246 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001247 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001248 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001249 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001250 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001251
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001252 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001253
1254end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001255 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001256}
1257
1258/* This function is used to restore the Lua context when a coroutine
1259 * fails. This function copy the common memory between old coroutine
1260 * and the new coroutine. The old coroutine is destroyed, and its
1261 * replaced by the new coroutine.
1262 * If the flag "keep_msg" is set, the last entry of the old is assumed
1263 * as string error message and it is copied in the new stack.
1264 */
1265static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1266{
1267 lua_State *T;
1268 int new_ref;
1269
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001270 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001271 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001272 if (!T)
1273 return 0;
1274
1275 /* Copy last error message. */
1276 if (keep_msg)
1277 lua_xmove(lua->T, T, 1);
1278
1279 /* Copy data between the coroutines. */
1280 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1281 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001282 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001283
1284 /* Destroy old data. */
1285 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1286
1287 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001288 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001289
1290 /* Fill the struct with the new coroutine values. */
1291 lua->Mref = new_ref;
1292 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001293 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001294
1295 /* Set context. */
1296 hlua_sethlua(lua);
1297
1298 return 1;
1299}
1300
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001301void hlua_hook(lua_State *L, lua_Debug *ar)
1302{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001303 struct hlua *hlua;
1304
1305 /* Get hlua struct, or NULL if we execute from main lua state */
1306 hlua = hlua_gethlua(L);
1307 if (!hlua)
1308 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001309
1310 /* Lua cannot yield when its returning from a function,
1311 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001312 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001313 */
1314 if (lua_gethookmask(L) & LUA_MASKRET) {
1315 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1316 return;
1317 }
1318
1319 /* restore the interrupt condition. */
1320 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1321
1322 /* If we interrupt the Lua processing in yieldable state, we yield.
1323 * If the state is not yieldable, trying yield causes an error.
1324 */
1325 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001326 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001327
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001328 /* If we cannot yield, update the clock and check the timeout. */
Willy Tarreau55542642021-10-08 09:33:24 +02001329 clock_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001330 hlua->run_time += now_ms - hlua->start_time;
1331 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001332 lua_pushfstring(L, "execution timeout");
1333 WILL_LJMP(lua_error(L));
1334 }
1335
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001336 /* Update the start time. */
1337 hlua->start_time = now_ms;
1338
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001339 /* Try to interrupt the process at the end of the current
1340 * unyieldable function.
1341 */
1342 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001343}
1344
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001345/* This function start or resumes the Lua stack execution. If the flag
1346 * "yield_allowed" if no set and the LUA stack execution returns a yield
1347 * The function return an error.
1348 *
1349 * The function can returns 4 values:
1350 * - HLUA_E_OK : The execution is terminated without any errors.
1351 * - HLUA_E_AGAIN : The execution must continue at the next associated
1352 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001353 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001354 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001355 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001356 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001357 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001358 * LUA code.
1359 */
1360static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1361{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001362#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1363 int nres;
1364#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001365 int ret;
1366 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001367 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001368
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001369 /* Initialise run time counter. */
1370 if (!HLUA_IS_RUNNING(lua))
1371 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001372
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001373 /* Lock the whole Lua execution. This lock must be before the
1374 * label "resume_execution".
1375 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001376 if (lua->state_id == 0)
Willy Tarreau1e7bef12021-08-20 15:47:25 +02001377 lua_take_global_lock();
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001378
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001379resume_execution:
1380
1381 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1382 * instructions. it is used for preventing infinite loops.
1383 */
1384 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1385
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001386 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001387 HLUA_SET_RUN(lua);
1388 HLUA_CLR_CTRLYIELD(lua);
1389 HLUA_CLR_WAKERESWR(lua);
1390 HLUA_CLR_WAKEREQWR(lua);
Christopher Faulet1f43a342021-08-04 17:58:21 +02001391 HLUA_CLR_NOYIELD(lua);
1392 if (!yield_allowed)
1393 HLUA_SET_NOYIELD(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001394
Christopher Fauletbc275a92020-02-26 14:55:16 +01001395 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001396 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001397 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001398
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001399 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001400#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001401 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001402#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001403 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001404#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001405 switch (ret) {
1406
1407 case LUA_OK:
1408 ret = HLUA_E_OK;
1409 break;
1410
1411 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001412 /* Check if the execution timeout is expired. It it is the case, we
1413 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001414 */
Willy Tarreau55542642021-10-08 09:33:24 +02001415 clock_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001416 lua->run_time += now_ms - lua->start_time;
1417 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001418 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001419 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001420 break;
1421 }
1422 /* Process the forced yield. if the general yield is not allowed or
1423 * if no task were associated this the current Lua execution
1424 * coroutine, we resume the execution. Else we want to return in the
1425 * scheduler and we want to be waked up again, to continue the
1426 * current Lua execution. So we schedule our own task.
1427 */
1428 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001429 if (!yield_allowed || !lua->task)
1430 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001431 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001432 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001433 if (!yield_allowed) {
1434 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001435 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001436 break;
1437 }
1438 ret = HLUA_E_AGAIN;
1439 break;
1440
1441 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001442
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001443 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001444 * because the errors ares the only one mean to return immediately
1445 * from and lua execution.
1446 */
1447 if (lua->flags & HLUA_EXIT) {
1448 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001449 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001450 break;
1451 }
1452
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001453 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001454 if (!lua_checkstack(lua->T, 1)) {
1455 ret = HLUA_E_ERR;
1456 break;
1457 }
1458 msg = lua_tostring(lua->T, -1);
1459 lua_settop(lua->T, 0); /* Empty the stack. */
1460 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001461 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001462 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001463 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001464 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001465 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001466 ret = HLUA_E_ERRMSG;
1467 break;
1468
1469 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001470 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001471 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001472 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001473 break;
1474
1475 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001476 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001477 if (!lua_checkstack(lua->T, 1)) {
1478 ret = HLUA_E_ERR;
1479 break;
1480 }
1481 msg = lua_tostring(lua->T, -1);
1482 lua_settop(lua->T, 0); /* Empty the stack. */
1483 lua_pop(lua->T, 1);
1484 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001485 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001486 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001487 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001488 ret = HLUA_E_ERRMSG;
1489 break;
1490
1491 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001492 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001493 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001494 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001495 break;
1496 }
1497
1498 switch (ret) {
1499 case HLUA_E_AGAIN:
1500 break;
1501
1502 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001503 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001504 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001505 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001506 break;
1507
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001508 case HLUA_E_ETMOUT:
1509 case HLUA_E_NOMEM:
1510 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001511 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001512 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001513 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001514 hlua_ctx_renew(lua, 0);
1515 break;
1516
1517 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001518 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001519 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001520 break;
1521 }
1522
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001523 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001524 if (lua->state_id == 0)
Willy Tarreau1e7bef12021-08-20 15:47:25 +02001525 lua_drop_global_lock();
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001526
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001527 return ret;
1528}
1529
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001530/* This function exit the current code. */
1531__LJMP static int hlua_done(lua_State *L)
1532{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001533 struct hlua *hlua;
1534
1535 /* Get hlua struct, or NULL if we execute from main lua state */
1536 hlua = hlua_gethlua(L);
1537 if (!hlua)
1538 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001539
1540 hlua->flags |= HLUA_EXIT;
1541 WILL_LJMP(lua_error(L));
1542
1543 return 0;
1544}
1545
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001546/* This function is an LUA binding. It provides a function
1547 * for deleting ACL from a referenced ACL file.
1548 */
1549__LJMP static int hlua_del_acl(lua_State *L)
1550{
1551 const char *name;
1552 const char *key;
1553 struct pat_ref *ref;
1554
1555 MAY_LJMP(check_args(L, 2, "del_acl"));
1556
1557 name = MAY_LJMP(luaL_checkstring(L, 1));
1558 key = MAY_LJMP(luaL_checkstring(L, 2));
1559
1560 ref = pat_ref_lookup(name);
1561 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001562 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001563
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001564 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001565 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001566 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001567 return 0;
1568}
1569
1570/* This function is an LUA binding. It provides a function
1571 * for deleting map entry from a referenced map file.
1572 */
1573static int hlua_del_map(lua_State *L)
1574{
1575 const char *name;
1576 const char *key;
1577 struct pat_ref *ref;
1578
1579 MAY_LJMP(check_args(L, 2, "del_map"));
1580
1581 name = MAY_LJMP(luaL_checkstring(L, 1));
1582 key = MAY_LJMP(luaL_checkstring(L, 2));
1583
1584 ref = pat_ref_lookup(name);
1585 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001586 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001587
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001588 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001589 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001590 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001591 return 0;
1592}
1593
1594/* This function is an LUA binding. It provides a function
1595 * for adding ACL pattern from a referenced ACL file.
1596 */
1597static int hlua_add_acl(lua_State *L)
1598{
1599 const char *name;
1600 const char *key;
1601 struct pat_ref *ref;
1602
1603 MAY_LJMP(check_args(L, 2, "add_acl"));
1604
1605 name = MAY_LJMP(luaL_checkstring(L, 1));
1606 key = MAY_LJMP(luaL_checkstring(L, 2));
1607
1608 ref = pat_ref_lookup(name);
1609 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001610 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001611
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001612 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001613 if (pat_ref_find_elt(ref, key) == NULL)
1614 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001615 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001616 return 0;
1617}
1618
1619/* This function is an LUA binding. It provides a function
1620 * for setting map pattern and sample from a referenced map
1621 * file.
1622 */
1623static int hlua_set_map(lua_State *L)
1624{
1625 const char *name;
1626 const char *key;
1627 const char *value;
1628 struct pat_ref *ref;
1629
1630 MAY_LJMP(check_args(L, 3, "set_map"));
1631
1632 name = MAY_LJMP(luaL_checkstring(L, 1));
1633 key = MAY_LJMP(luaL_checkstring(L, 2));
1634 value = MAY_LJMP(luaL_checkstring(L, 3));
1635
1636 ref = pat_ref_lookup(name);
1637 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001638 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001639
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001640 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001641 if (pat_ref_find_elt(ref, key) != NULL)
1642 pat_ref_set(ref, key, value, NULL);
1643 else
1644 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001645 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001646 return 0;
1647}
1648
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001649/* A class is a lot of memory that contain data. This data can be a table,
1650 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001651 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001652 * the name of the object (_G[<name>] = <metable> ).
1653 *
1654 * A metable is a table that modify the standard behavior of a standard
1655 * access to the associated data. The entries of this new metatable are
1656 * defined as is:
1657 *
1658 * http://lua-users.org/wiki/MetatableEvents
1659 *
1660 * __index
1661 *
1662 * we access an absent field in a table, the result is nil. This is
1663 * true, but it is not the whole truth. Actually, such access triggers
1664 * the interpreter to look for an __index metamethod: If there is no
1665 * such method, as usually happens, then the access results in nil;
1666 * otherwise, the metamethod will provide the result.
1667 *
1668 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1669 * the key does not appear in the table, but the metatable has an __index
1670 * property:
1671 *
1672 * - if the value is a function, the function is called, passing in the
1673 * table and the key; the return value of that function is returned as
1674 * the result.
1675 *
1676 * - if the value is another table, the value of the key in that table is
1677 * asked for and returned (and if it doesn't exist in that table, but that
1678 * table's metatable has an __index property, then it continues on up)
1679 *
1680 * - Use "rawget(myTable,key)" to skip this metamethod.
1681 *
1682 * http://www.lua.org/pil/13.4.1.html
1683 *
1684 * __newindex
1685 *
1686 * Like __index, but control property assignment.
1687 *
1688 * __mode - Control weak references. A string value with one or both
1689 * of the characters 'k' and 'v' which specifies that the the
1690 * keys and/or values in the table are weak references.
1691 *
1692 * __call - Treat a table like a function. When a table is followed by
1693 * parenthesis such as "myTable( 'foo' )" and the metatable has
1694 * a __call key pointing to a function, that function is invoked
1695 * (passing any specified arguments) and the return value is
1696 * returned.
1697 *
1698 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1699 * called, if the metatable for myTable has a __metatable
1700 * key, the value of that key is returned instead of the
1701 * actual metatable.
1702 *
1703 * __tostring - Control string representation. When the builtin
1704 * "tostring( myTable )" function is called, if the metatable
1705 * for myTable has a __tostring property set to a function,
1706 * that function is invoked (passing myTable to it) and the
1707 * return value is used as the string representation.
1708 *
1709 * __len - Control table length. When the table length is requested using
1710 * the length operator ( '#' ), if the metatable for myTable has
1711 * a __len key pointing to a function, that function is invoked
1712 * (passing myTable to it) and the return value used as the value
1713 * of "#myTable".
1714 *
1715 * __gc - Userdata finalizer code. When userdata is set to be garbage
1716 * collected, if the metatable has a __gc field pointing to a
1717 * function, that function is first invoked, passing the userdata
1718 * to it. The __gc metamethod is not called for tables.
1719 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1720 *
1721 * Special metamethods for redefining standard operators:
1722 * http://www.lua.org/pil/13.1.html
1723 *
1724 * __add "+"
1725 * __sub "-"
1726 * __mul "*"
1727 * __div "/"
1728 * __unm "!"
1729 * __pow "^"
1730 * __concat ".."
1731 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001732 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001733 * http://www.lua.org/pil/13.2.html
1734 *
1735 * __eq "=="
1736 * __lt "<"
1737 * __le "<="
1738 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001739
1740/*
1741 *
1742 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001743 * Class Map
1744 *
1745 *
1746 */
1747
1748/* Returns a struct hlua_map if the stack entry "ud" is
1749 * a class session, otherwise it throws an error.
1750 */
1751__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1752{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001753 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001754}
1755
1756/* This function is the map constructor. It don't need
1757 * the class Map object. It creates and return a new Map
1758 * object. It must be called only during "body" or "init"
1759 * context because it process some filesystem accesses.
1760 */
1761__LJMP static int hlua_map_new(struct lua_State *L)
1762{
1763 const char *fn;
1764 int match = PAT_MATCH_STR;
1765 struct sample_conv conv;
1766 const char *file = "";
1767 int line = 0;
1768 lua_Debug ar;
1769 char *err = NULL;
1770 struct arg args[2];
1771
1772 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1773 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1774
1775 fn = MAY_LJMP(luaL_checkstring(L, 1));
1776
1777 if (lua_gettop(L) >= 2) {
1778 match = MAY_LJMP(luaL_checkinteger(L, 2));
1779 if (match < 0 || match >= PAT_MATCH_NUM)
1780 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1781 }
1782
1783 /* Get Lua filename and line number. */
1784 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1785 lua_getinfo(L, "Sl", &ar); /* get info about it */
1786 if (ar.currentline > 0) { /* is there info? */
1787 file = ar.short_src;
1788 line = ar.currentline;
1789 }
1790 }
1791
1792 /* fill fake sample_conv struct. */
1793 conv.kw = ""; /* unused. */
1794 conv.process = NULL; /* unused. */
1795 conv.arg_mask = 0; /* unused. */
1796 conv.val_args = NULL; /* unused. */
1797 conv.out_type = SMP_T_STR;
1798 conv.private = (void *)(long)match;
1799 switch (match) {
1800 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1801 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1802 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1803 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1804 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1805 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1806 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001807 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001808 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1809 default:
1810 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1811 }
1812
1813 /* fill fake args. */
1814 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001815 args[0].data.str.area = strdup(fn);
1816 args[0].data.str.data = strlen(fn);
1817 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001818 args[1].type = ARGT_STOP;
1819
1820 /* load the map. */
1821 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001822 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001823 * free the err variable.
1824 */
1825 luaL_where(L, 1);
1826 lua_pushfstring(L, "'new': %s.", err);
1827 lua_concat(L, 2);
1828 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001829 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001830 WILL_LJMP(lua_error(L));
1831 }
1832
1833 /* create the lua object. */
1834 lua_newtable(L);
1835 lua_pushlightuserdata(L, args[0].data.map);
1836 lua_rawseti(L, -2, 0);
1837
1838 /* Pop a class Map metatable and affect it to the userdata. */
1839 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1840 lua_setmetatable(L, -2);
1841
1842
1843 return 1;
1844}
1845
1846__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1847{
1848 struct map_descriptor *desc;
1849 struct pattern *pat;
1850 struct sample smp;
1851
1852 MAY_LJMP(check_args(L, 2, "lookup"));
1853 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001854 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001855 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001856 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001857 }
1858 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001859 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001860 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001861 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 +01001862 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001863 }
1864
1865 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001866 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001867 if (str)
1868 lua_pushstring(L, "");
1869 else
1870 lua_pushnil(L);
1871 return 1;
1872 }
1873
1874 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001875 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001876 return 1;
1877}
1878
1879__LJMP static int hlua_map_lookup(struct lua_State *L)
1880{
1881 return _hlua_map_lookup(L, 0);
1882}
1883
1884__LJMP static int hlua_map_slookup(struct lua_State *L)
1885{
1886 return _hlua_map_lookup(L, 1);
1887}
1888
1889/*
1890 *
1891 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 * Class Socket
1893 *
1894 *
1895 */
1896
1897__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1898{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001899 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900}
1901
1902/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001903 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 * received.
1905 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001906static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907{
Christopher Faulet86e1c332021-12-20 17:09:39 +01001908 struct stream_interface *si = cs_si(appctx->owner);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001910 if (appctx->ctx.hlua_cosocket.die) {
1911 si_shutw(si);
1912 si_shutr(si);
1913 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001914 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1915 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001916 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1917 }
1918
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001919 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001920 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001921 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001922
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001923 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001924 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001925 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001926
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001927 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001928 * to be notified whenever the connection completes.
1929 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001930 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001931 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001932 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001933 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001934 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001935 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936
1937 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001938 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001939
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001940 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001941 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001942 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001943
1944 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001945 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001946 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001947
1948 /* Some data were injected in the buffer, notify the stream
1949 * interface.
1950 */
1951 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001952 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001953
1954 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001955 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001956 */
1957 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001958 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959}
1960
Willy Tarreau87b09662015-04-03 00:22:06 +02001961/* This function is called when the "struct stream" is destroyed.
1962 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963 * Wake all the pending signals.
1964 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001965static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001966{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001967 struct xref *peer;
1968
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001970 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1971 if (peer)
1972 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973
1974 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001975 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1976 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977}
1978
1979/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001980 * uses this object. If the stream does not exists, just quit.
1981 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982 * pending signal can rest in the read and write lists. destroy
1983 * it.
1984 */
1985__LJMP static int hlua_socket_gc(lua_State *L)
1986{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001987 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001989 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001990
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001991 MAY_LJMP(check_args(L, 1, "__gc"));
1992
1993 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001994 peer = xref_get_peer_and_lock(&socket->xref);
1995 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001996 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001997 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001998
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001999 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002000 appctx->ctx.hlua_cosocket.die = 1;
2001 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002002
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002003 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002004 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005 return 0;
2006}
2007
2008/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02002009 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002010 */
sada05ed3302018-05-11 11:48:18 -07002011__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002012{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002013 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002015 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002016 struct hlua *hlua;
2017
2018 /* Get hlua struct, or NULL if we execute from main lua state */
2019 hlua = hlua_gethlua(L);
2020 if (!hlua)
2021 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002022
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002023 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002024
2025 /* Check if we run on the same thread than the xreator thread.
2026 * We cannot access to the socket if the thread is different.
2027 */
2028 if (socket->tid != tid)
2029 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2030
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002031 peer = xref_get_peer_and_lock(&socket->xref);
2032 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002033 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01002034
2035 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002036 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002038 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002039 appctx->ctx.hlua_cosocket.die = 1;
2040 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002041
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002042 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002043 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044 return 0;
2045}
2046
sada05ed3302018-05-11 11:48:18 -07002047/* The close function calls close_helper.
2048 */
2049__LJMP static int hlua_socket_close(lua_State *L)
2050{
2051 MAY_LJMP(check_args(L, 1, "close"));
2052 return hlua_socket_close_helper(L);
2053}
2054
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002055/* This Lua function assumes that the stack contain three parameters.
2056 * 1 - USERDATA containing a struct socket
2057 * 2 - INTEGER with values of the macro defined below
2058 * If the integer is -1, we must read at most one line.
2059 * If the integer is -2, we ust read all the data until the
2060 * end of the stream.
2061 * If the integer is positive value, we must read a number of
2062 * bytes corresponding to this value.
2063 */
2064#define HLSR_READ_LINE (-1)
2065#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002066__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067{
2068 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2069 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002070 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002072 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02002074 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002075 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02002076 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002077 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002078 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01002079 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002080 struct stream_interface *si;
2081 struct stream *s;
2082 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002083 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002085 /* Get hlua struct, or NULL if we execute from main lua state */
2086 hlua = hlua_gethlua(L);
2087
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088 /* Check if this lua stack is schedulable. */
2089 if (!hlua || !hlua->task)
2090 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
2091 "'frontend', 'backend' or 'task'"));
2092
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002093 /* Check if we run on the same thread than the xreator thread.
2094 * We cannot access to the socket if the thread is different.
2095 */
2096 if (socket->tid != tid)
2097 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2098
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002099 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002100 peer = xref_get_peer_and_lock(&socket->xref);
2101 if (!peer)
2102 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002103 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002104 si = cs_si(appctx->owner);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002105 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002106
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002107 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002108 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002109 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002110 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002111 if (nblk < 0) /* Connection close. */
2112 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002113 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002114 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002115
2116 /* remove final \r\n. */
2117 if (nblk == 1) {
2118 if (blk1[len1-1] == '\n') {
2119 len1--;
2120 skip_at_end++;
2121 if (blk1[len1-1] == '\r') {
2122 len1--;
2123 skip_at_end++;
2124 }
2125 }
2126 }
2127 else {
2128 if (blk2[len2-1] == '\n') {
2129 len2--;
2130 skip_at_end++;
2131 if (blk2[len2-1] == '\r') {
2132 len2--;
2133 skip_at_end++;
2134 }
2135 }
2136 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137 }
2138
2139 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002140 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002141 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002142 if (nblk < 0) /* Connection close. */
2143 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002144 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002145 goto connection_empty;
2146 }
2147
2148 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002149 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002150 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151 if (nblk < 0) /* Connection close. */
2152 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002153 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154 goto connection_empty;
2155
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002156 missing_bytes = wanted - socket->b.n;
2157 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002158 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002159 len1 = missing_bytes;
2160 } if (nblk == 2 && len1 + len2 > missing_bytes)
2161 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002162 }
2163
2164 len = len1;
2165
2166 luaL_addlstring(&socket->b, blk1, len1);
2167 if (nblk == 2) {
2168 len += len2;
2169 luaL_addlstring(&socket->b, blk2, len2);
2170 }
2171
2172 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002173 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002174
2175 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002176 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177
2178 /* If the pattern reclaim to read all the data
2179 * in the connection, got out.
2180 */
2181 if (wanted == HLSR_READ_ALL)
2182 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002183 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002184 goto connection_empty;
2185
2186 /* Return result. */
2187 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002188 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 return 1;
2190
2191connection_closed:
2192
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002193 xref_unlock(&socket->xref, peer);
2194
2195no_peer:
2196
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002197 /* If the buffer containds data. */
2198 if (socket->b.n > 0) {
2199 luaL_pushresult(&socket->b);
2200 return 1;
2201 }
2202 lua_pushnil(L);
2203 lua_pushstring(L, "connection closed.");
2204 return 2;
2205
2206connection_empty:
2207
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002208 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2209 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002210 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002211 }
2212 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002213 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002214 return 0;
2215}
2216
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002217/* This Lua function gets two parameters. The first one can be string
2218 * or a number. If the string is "*l", the user requires one line. If
2219 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002220 * If the value is a number, the user require a number of bytes equal
2221 * to the value. The default value is "*l" (a line).
2222 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002223 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002224 * integer takes this values:
2225 * -1 : read a line
2226 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002227 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002229 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230 * concatenated with the read data.
2231 */
2232__LJMP static int hlua_socket_receive(struct lua_State *L)
2233{
2234 int wanted = HLSR_READ_LINE;
2235 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002236 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237 char *error;
2238 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002239 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002240
2241 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2242 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2243
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002244 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002245
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002246 /* Check if we run on the same thread than the xreator thread.
2247 * We cannot access to the socket if the thread is different.
2248 */
2249 if (socket->tid != tid)
2250 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2251
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002252 /* check for pattern. */
2253 if (lua_gettop(L) >= 2) {
2254 type = lua_type(L, 2);
2255 if (type == LUA_TSTRING) {
2256 pattern = lua_tostring(L, 2);
2257 if (strcmp(pattern, "*a") == 0)
2258 wanted = HLSR_READ_ALL;
2259 else if (strcmp(pattern, "*l") == 0)
2260 wanted = HLSR_READ_LINE;
2261 else {
2262 wanted = strtoll(pattern, &error, 10);
2263 if (*error != '\0')
2264 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2265 }
2266 }
2267 else if (type == LUA_TNUMBER) {
2268 wanted = lua_tointeger(L, 2);
2269 if (wanted < 0)
2270 WILL_LJMP(luaL_error(L, "Unsupported size."));
2271 }
2272 }
2273
2274 /* Set pattern. */
2275 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002276
2277 /* Check if we would replace the top by itself. */
2278 if (lua_gettop(L) != 2)
2279 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280
Christopher Fauletc31b2002021-05-03 10:11:13 +02002281 /* Save index of the top of the stack because since buffers are used, it
2282 * may change
2283 */
2284 lastarg = lua_gettop(L);
2285
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002286 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287 luaL_buffinit(L, &socket->b);
2288
2289 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002290 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291 if (lua_type(L, 3) != LUA_TSTRING)
2292 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2293 pattern = lua_tolstring(L, 3, &len);
2294 luaL_addlstring(&socket->b, pattern, len);
2295 }
2296
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002297 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298}
2299
2300/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002301 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002302 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002303static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002304{
2305 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002306 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 struct appctx *appctx;
2308 size_t buf_len;
2309 const char *buf;
2310 int len;
2311 int send_len;
2312 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002313 struct xref *peer;
2314 struct stream_interface *si;
2315 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002317 /* Get hlua struct, or NULL if we execute from main lua state */
2318 hlua = hlua_gethlua(L);
2319
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002320 /* Check if this lua stack is schedulable. */
2321 if (!hlua || !hlua->task)
2322 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2323 "'frontend', 'backend' or 'task'"));
2324
2325 /* Get object */
2326 socket = MAY_LJMP(hlua_checksocket(L, 1));
2327 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002328 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002329
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002330 /* Check if we run on the same thread than the xreator thread.
2331 * We cannot access to the socket if the thread is different.
2332 */
2333 if (socket->tid != tid)
2334 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2335
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002336 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002337 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002338 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002339 lua_pushinteger(L, -1);
2340 return 1;
2341 }
2342 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002343 si = cs_si(appctx->owner);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002344 s = si_strm(si);
2345
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002346 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002347 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002349 lua_pushinteger(L, -1);
2350 return 1;
2351 }
2352
2353 /* Update the input buffer data. */
2354 buf += sent;
2355 send_len = buf_len - sent;
2356
2357 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002358 if (sent >= buf_len) {
2359 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002361 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002362
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002363 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002364 * the request buffer if its not required.
2365 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002366 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002367 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002368 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002369 }
2370
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002371 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002372 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002373 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002375 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002376
2377 /* send data */
2378 if (len < send_len)
2379 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002380 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002381
2382 /* "Not enough space" (-1), "Buffer too little to contain
2383 * the data" (-2) are not expected because the available length
2384 * is tested.
2385 * Other unknown error are also not expected.
2386 */
2387 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002388 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002389 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002390
sada05ed3302018-05-11 11:48:18 -07002391 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002392 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002393 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002394 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002395 return 1;
2396 }
2397
2398 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002399 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002400
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002401 s->req.rex = TICK_ETERNITY;
2402 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403
2404 /* Update length sent. */
2405 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002406 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002407
2408 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002409 if (sent + len >= buf_len) {
2410 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002411 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002412 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002413
2414hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002415 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2416 xref_unlock(&socket->xref, peer);
2417 WILL_LJMP(luaL_error(L, "out of memory"));
2418 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002419 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002420 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002421 return 0;
2422}
2423
2424/* This function initiate the send of data. It just check the input
2425 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002426 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002427 * "hlua_socket_write_yield" that can yield.
2428 *
2429 * The Lua function gets between 3 and 4 parameters. The first one is
2430 * the associated object. The second is a string buffer. The third is
2431 * a facultative integer that represents where is the buffer position
2432 * of the start of the data that can send. The first byte is the
2433 * position "1". The default value is "1". The fourth argument is a
2434 * facultative integer that represents where is the buffer position
2435 * of the end of the data that can send. The default is the last byte.
2436 */
2437static int hlua_socket_send(struct lua_State *L)
2438{
2439 int i;
2440 int j;
2441 const char *buf;
2442 size_t buf_len;
2443
2444 /* Check number of arguments. */
2445 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2446 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2447
2448 /* Get the string. */
2449 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2450
2451 /* Get and check j. */
2452 if (lua_gettop(L) == 4) {
2453 j = MAY_LJMP(luaL_checkinteger(L, 4));
2454 if (j < 0)
2455 j = buf_len + j + 1;
2456 if (j > buf_len)
2457 j = buf_len + 1;
2458 lua_pop(L, 1);
2459 }
2460 else
2461 j = buf_len;
2462
2463 /* Get and check i. */
2464 if (lua_gettop(L) == 3) {
2465 i = MAY_LJMP(luaL_checkinteger(L, 3));
2466 if (i < 0)
2467 i = buf_len + i + 1;
2468 if (i > buf_len)
2469 i = buf_len + 1;
2470 lua_pop(L, 1);
2471 } else
2472 i = 1;
2473
2474 /* Check bth i and j. */
2475 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002476 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002477 return 1;
2478 }
2479 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002480 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002481 return 1;
2482 }
2483 if (i == 0)
2484 i = 1;
2485 if (j == 0)
2486 j = 1;
2487
2488 /* Pop the string. */
2489 lua_pop(L, 1);
2490
2491 /* Update the buffer length. */
2492 buf += i - 1;
2493 buf_len = j - i + 1;
2494 lua_pushlstring(L, buf, buf_len);
2495
2496 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002497 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002498
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002499 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500}
2501
Willy Tarreau22b0a682015-06-17 19:43:49 +02002502#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Christopher Faulete6465b32021-10-22 15:36:08 +02002503__LJMP static inline int hlua_socket_info(struct lua_State *L, const struct sockaddr_storage *addr)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002504{
2505 static char buffer[SOCKET_INFO_MAX_LEN];
2506 int ret;
2507 int len;
2508 char *p;
2509
2510 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2511 if (ret <= 0) {
2512 lua_pushnil(L);
2513 return 1;
2514 }
2515
2516 if (ret == AF_UNIX) {
2517 lua_pushstring(L, buffer+1);
2518 return 1;
2519 }
2520 else if (ret == AF_INET6) {
2521 buffer[0] = '[';
2522 len = strlen(buffer);
2523 buffer[len] = ']';
2524 len++;
2525 buffer[len] = ':';
2526 len++;
2527 p = buffer;
2528 }
2529 else if (ret == AF_INET) {
2530 p = buffer + 1;
2531 len = strlen(p);
2532 p[len] = ':';
2533 len++;
2534 }
2535 else {
2536 lua_pushnil(L);
2537 return 1;
2538 }
2539
2540 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2541 lua_pushnil(L);
2542 return 1;
2543 }
2544
2545 lua_pushstring(L, p);
2546 return 1;
2547}
2548
2549/* Returns information about the peer of the connection. */
2550__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2551{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002552 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002553 struct xref *peer;
2554 struct appctx *appctx;
2555 struct stream_interface *si;
Christopher Faulet16f16af2021-10-27 09:34:56 +02002556 const struct sockaddr_storage *dst;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002557 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002558
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002559 MAY_LJMP(check_args(L, 1, "getpeername"));
2560
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002561 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002562
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002563 /* Check if we run on the same thread than the xreator thread.
2564 * We cannot access to the socket if the thread is different.
2565 */
2566 if (socket->tid != tid)
2567 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2568
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002569 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002570 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002571 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002572 lua_pushnil(L);
2573 return 1;
2574 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002575 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002576 si = cs_si(appctx->owner);
Christopher Faulet16f16af2021-10-27 09:34:56 +02002577 dst = si_dst(si_opposite(si));
2578 if (!dst) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002579 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002580 lua_pushnil(L);
2581 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002582 }
2583
Christopher Faulet16f16af2021-10-27 09:34:56 +02002584 ret = MAY_LJMP(hlua_socket_info(L, dst));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002585 xref_unlock(&socket->xref, peer);
2586 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587}
2588
2589/* Returns information about my connection side. */
2590static int hlua_socket_getsockname(struct lua_State *L)
2591{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002592 struct hlua_socket *socket;
2593 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002594 struct appctx *appctx;
2595 struct xref *peer;
2596 struct stream_interface *si;
2597 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002598 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002599
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002600 MAY_LJMP(check_args(L, 1, "getsockname"));
2601
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002602 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002603
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002604 /* Check if we run on the same thread than the xreator thread.
2605 * We cannot access to the socket if the thread is different.
2606 */
2607 if (socket->tid != tid)
2608 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2609
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002610 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002611 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002612 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002613 lua_pushnil(L);
2614 return 1;
2615 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002616 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002617 si = cs_si(appctx->owner);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002618 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002619
Christopher Faulet95a61e82021-12-22 14:22:03 +01002620 conn = cs_conn(s->csb);
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002621 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002622 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002623 lua_pushnil(L);
2624 return 1;
2625 }
2626
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002627 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002628 xref_unlock(&socket->xref, peer);
2629 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002630}
2631
2632/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002633static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002634 .obj_type = OBJ_TYPE_APPLET,
2635 .name = "<LUA_TCP>",
2636 .fct = hlua_socket_handler,
2637 .release = hlua_socket_release,
2638};
2639
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002640__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641{
2642 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002643 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002644 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002646 struct stream_interface *si;
2647 struct stream *s;
2648
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002649 /* Get hlua struct, or NULL if we execute from main lua state */
2650 hlua = hlua_gethlua(L);
2651 if (!hlua)
2652 return 0;
2653
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002654 /* Check if we run on the same thread than the xreator thread.
2655 * We cannot access to the socket if the thread is different.
2656 */
2657 if (socket->tid != tid)
2658 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2659
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002660 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002661 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002662 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002663 lua_pushnil(L);
2664 lua_pushstring(L, "Can't connect");
2665 return 2;
2666 }
2667 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002668 si = cs_si(appctx->owner);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002669 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002670
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002671 /* Check if we run on the same thread than the xreator thread.
2672 * We cannot access to the socket if the thread is different.
2673 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002674 if (socket->tid != tid) {
2675 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002676 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002677 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002678
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002679 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002680 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002681 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002682 lua_pushnil(L);
2683 lua_pushstring(L, "Can't connect");
2684 return 2;
2685 }
2686
Christopher Faulet693b23b2022-02-28 09:09:05 +01002687 appctx = __cs_appctx(s->csf);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002688
2689 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002690 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002691 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002692 lua_pushinteger(L, 1);
2693 return 1;
2694 }
2695
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002696 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2697 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002698 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002699 }
2700 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002701 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002702 return 0;
2703}
2704
2705/* This function fail or initite the connection. */
2706__LJMP static int hlua_socket_connect(struct lua_State *L)
2707{
2708 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002709 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002711 struct hlua *hlua;
2712 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002713 int low, high;
2714 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002715 struct xref *peer;
2716 struct stream_interface *si;
2717 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002718
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002719 if (lua_gettop(L) < 2)
2720 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002721
2722 /* Get args. */
2723 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002724
2725 /* Check if we run on the same thread than the xreator thread.
2726 * We cannot access to the socket if the thread is different.
2727 */
2728 if (socket->tid != tid)
2729 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2730
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002731 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002732 if (lua_gettop(L) >= 3) {
2733 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002734 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002735
Tim Duesterhus6edab862018-01-06 19:04:45 +01002736 /* Force the ip to end with a colon, to support IPv6 addresses
2737 * that are not enclosed within square brackets.
2738 */
2739 if (port > 0) {
2740 luaL_buffinit(L, &b);
2741 luaL_addstring(&b, ip);
2742 luaL_addchar(&b, ':');
2743 luaL_pushresult(&b);
2744 ip = lua_tolstring(L, lua_gettop(L), NULL);
2745 }
2746 }
2747
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002748 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002749 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002750 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002751 lua_pushnil(L);
2752 return 1;
2753 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002754
2755 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002756 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 +02002757 if (!addr) {
2758 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002759 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002760 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002761
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002762 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002763 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002764 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002765 if (port == -1) {
2766 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002767 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002768 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002769 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2770 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002771 if (port == -1) {
2772 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002773 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002774 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002775 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002776 }
2777 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002778
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002779 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002780 si = cs_si(appctx->owner);
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002781 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002782
Christopher Faulet16f16af2021-10-27 09:34:56 +02002783 if (!sockaddr_alloc(&si_opposite(si)->dst, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002784 xref_unlock(&socket->xref, peer);
2785 WILL_LJMP(luaL_error(L, "connect: internal error"));
2786 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002787 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002788
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002789 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002790 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002791 if (!hlua)
2792 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002793
2794 /* inform the stream that we want to be notified whenever the
2795 * connection completes.
2796 */
Christopher Faulet436811f2021-12-23 13:39:38 +01002797 si_cant_get(cs_si(s->csf));
2798 si_rx_endp_more(cs_si(s->csf));
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002799 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002800
Willy Tarreauf31af932020-01-14 09:59:38 +01002801 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002802
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002803 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2804 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002805 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002806 }
2807 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002808
2809 task_wakeup(s->task, TASK_WOKEN_INIT);
2810 /* Return yield waiting for connection. */
2811
Willy Tarreau9635e032018-10-16 17:52:55 +02002812 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002813
2814 return 0;
2815}
2816
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002817#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002818__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2819{
2820 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002821 struct xref *peer;
2822 struct appctx *appctx;
2823 struct stream_interface *si;
2824 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002825
2826 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2827 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002828
2829 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002830 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002831 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002832 lua_pushnil(L);
2833 return 1;
2834 }
2835 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002836 si = cs_si(appctx->owner);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002837 s = si_strm(si);
2838
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002839 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002840 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002841 return MAY_LJMP(hlua_socket_connect(L));
2842}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002843#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002844
2845__LJMP static int hlua_socket_setoption(struct lua_State *L)
2846{
2847 return 0;
2848}
2849
2850__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2851{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002852 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002853 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002854 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002855 struct xref *peer;
2856 struct appctx *appctx;
2857 struct stream_interface *si;
2858 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002859
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002860 MAY_LJMP(check_args(L, 2, "settimeout"));
2861
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002862 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002863
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002864 /* convert the timeout to millis */
2865 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002866
Thierry Fournier17a921b2018-03-08 09:59:02 +01002867 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002868 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002869 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2870
Mark Lakes56cc1252018-03-27 09:48:06 +02002871 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002872 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002873
2874 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002875 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002876 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002877
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002878 /* Check if we run on the same thread than the xreator thread.
2879 * We cannot access to the socket if the thread is different.
2880 */
2881 if (socket->tid != tid)
2882 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2883
Mark Lakes56cc1252018-03-27 09:48:06 +02002884 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002885 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002886 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002887 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2888 WILL_LJMP(lua_error(L));
2889 return 0;
2890 }
2891 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002892 si = cs_si(appctx->owner);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002893 s = si_strm(si);
2894
Cyril Bonté7bb63452018-08-17 23:51:02 +02002895 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002896 s->req.rto = tmout;
2897 s->req.wto = tmout;
2898 s->res.rto = tmout;
2899 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002900 s->req.rex = tick_add_ifset(now_ms, tmout);
2901 s->req.wex = tick_add_ifset(now_ms, tmout);
2902 s->res.rex = tick_add_ifset(now_ms, tmout);
2903 s->res.wex = tick_add_ifset(now_ms, tmout);
2904
2905 s->task->expire = tick_add_ifset(now_ms, tmout);
2906 task_queue(s->task);
2907
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002908 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002909
Thierry Fourniere9636f12018-03-08 09:54:32 +01002910 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002911 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002912}
2913
2914__LJMP static int hlua_socket_new(lua_State *L)
2915{
2916 struct hlua_socket *socket;
2917 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002918 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +01002919 struct conn_stream *cs;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002920 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002921
2922 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002923 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002924 hlua_pusherror(L, "socket: full stack");
2925 goto out_fail_conf;
2926 }
2927
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002928 /* Create the object: obj[0] = userdata. */
2929 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002930 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002931 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002932 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002933 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002934
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002935 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002936 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002937 hlua_pusherror(L, "socket: uninitialized pools.");
2938 goto out_fail_conf;
2939 }
2940
Willy Tarreau87b09662015-04-03 00:22:06 +02002941 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002942 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2943 lua_setmetatable(L, -2);
2944
Willy Tarreaud420a972015-04-06 00:39:18 +02002945 /* Create the applet context */
Willy Tarreaue6124462021-09-13 10:07:38 +02002946 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002947 if (!appctx) {
2948 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002949 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002950 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002951
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002952 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002953 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002954 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2955 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002956
Willy Tarreaud420a972015-04-06 00:39:18 +02002957 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002958 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002959 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002960 hlua_pusherror(L, "socket: out of memory");
Christopher Faulet13a35e52021-12-20 15:34:16 +01002961 goto out_fail_appctx;
2962 }
2963
Christopher Fauletcda94ac2021-12-23 17:28:17 +01002964 cs = cs_new();
Christopher Faulet13a35e52021-12-20 15:34:16 +01002965 if (!cs) {
2966 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002967 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002968 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +01002969 cs_attach_endp(cs, &appctx->obj_type, appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002970
Christopher Faulet13a35e52021-12-20 15:34:16 +01002971 strm = stream_new(sess, cs, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002972 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002973 hlua_pusherror(L, "socket: out of memory");
Christopher Faulet13a35e52021-12-20 15:34:16 +01002974 goto out_fail_cs;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002975 }
2976
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002977 /* Initialise cross reference between stream and Lua socket object. */
2978 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002979
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002980 /* Configure "right" stream interface. this "si" is used to connect
2981 * and retrieve data from the server. The connection is initialized
2982 * with the "struct server".
2983 */
Christopher Fauletcda94ac2021-12-23 17:28:17 +01002984 si_set_state(strm->csb->si, SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002985
2986 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002987 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002988 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002989
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002990 return 1;
2991
Christopher Faulet13a35e52021-12-20 15:34:16 +01002992 out_fail_cs:
2993 cs_free(cs);
Willy Tarreaud420a972015-04-06 00:39:18 +02002994 out_fail_sess:
Christopher Faulet13a35e52021-12-20 15:34:16 +01002995 session_free(sess);
2996 out_fail_appctx:
Willy Tarreaud420a972015-04-06 00:39:18 +02002997 appctx_free(appctx);
2998 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002999 WILL_LJMP(lua_error(L));
3000 return 0;
3001}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003002
3003/*
3004 *
3005 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003006 * Class Channel
3007 *
3008 *
3009 */
3010
3011/* Returns the struct hlua_channel join to the class channel in the
3012 * stack entry "ud" or throws an argument error.
3013 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003014__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003016 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003017}
3018
Willy Tarreau47860ed2015-03-10 14:07:50 +01003019/* Pushes the channel onto the top of the stack. If the stask does not have a
3020 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01003022static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003023{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003025 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 return 0;
3027
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003028 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003029 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003030 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031
3032 /* Pop a class sesison metatable and affect it to the userdata. */
3033 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
3034 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003035 return 1;
3036}
3037
Christopher Faulet9f55a502020-02-25 15:21:02 +01003038/* Helper function returning a filter attached to a channel at the position <ud>
3039 * in the stack, filling the current offset and length of the filter. If no
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003040 * filter is attached, NULL is returned and <offset> and <len> are not
Christopher Faulet9f55a502020-02-25 15:21:02 +01003041 * initialized.
3042 */
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003043static struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len)
Christopher Faulet9f55a502020-02-25 15:21:02 +01003044{
3045 struct filter *filter = NULL;
3046
3047 if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) {
3048 struct hlua_flt_ctx *flt_ctx;
3049
3050 filter = lua_touserdata (L, -1);
3051 flt_ctx = filter->ctx;
3052 if (hlua_filter_from_payload(filter)) {
3053 *offset = flt_ctx->cur_off[CHN_IDX(chn)];
3054 *len = flt_ctx->cur_len[CHN_IDX(chn)];
3055 }
3056 }
3057
3058 lua_pop(L, 1);
3059 return filter;
3060}
3061
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003062/* Copies <len> bytes of data present in the channel's buffer, starting at the
3063* offset <offset>, and put it in a LUA string variable. It is the caller
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003064* responsibility to ensure <len> and <offset> are valid. It always return the
3065* length of the built string. <len> may be 0, in this case, an empty string is
3066* created and 0 is returned.
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003067*/
3068static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003070 size_t block1, block2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071 luaL_Buffer b;
3072
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003073 block1 = len;
3074 if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)))
3075 block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset));
3076 block2 = len - block1;
3077
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003078 luaL_buffinit(L, &b);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003079 luaL_addlstring(&b, b_peek(&chn->buf, offset), block1);
3080 if (block2)
3081 luaL_addlstring(&b, b_orig(&chn->buf), block2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003082 luaL_pushresult(&b);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003083 return len;
3084}
3085
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003086/* Inserts the string <str> to the channel's buffer at the offset <offset>. This
3087 * function returns -1 if data cannot be copied. Otherwise, it returns the
3088 * number of bytes copied.
3089 */
3090static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset)
3091{
3092 int ret = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003094 /* Nothing to do, just return */
3095 if (unlikely(istlen(str) == 0))
3096 goto end;
3097
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003098 if (istlen(str) > c_room(chn)) {
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003099 ret = -1;
3100 goto end;
3101 }
3102 ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str));
3103
3104 end:
3105 return ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003106}
3107
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003108/* Removes <len> bytes of data at the absolute position <offset>.
3109 */
3110static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len)
3111{
3112 size_t end = offset + len;
3113
3114 if (b_peek(&chn->buf, end) != b_tail(&chn->buf))
3115 b_move(&chn->buf, b_peek_ofs(&chn->buf, end),
3116 b_data(&chn->buf) - end, -len);
3117 b_sub(&chn->buf, len);
3118}
3119
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003120/* Copies input data in the channel's buffer. It is possible to set a specific
3121 * offset (0 by default) and a length (all remaining input data starting for the
3122 * offset by default). If there is not enough input data and more data can be
3123 * received, this function yields.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003124 *
3125 * From an action, All input data are considered. For a filter, the offset and
3126 * the length of input data to consider are retrieved from the filter context.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003128__LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003129{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003130 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003131 struct filter *filter;
3132 size_t input, output;
3133 int offset, len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003134
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003135 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003136
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003137 output = co_data(chn);
3138 input = ci_data(chn);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003139
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003140 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3141 if (filter && !hlua_filter_from_payload(filter))
3142 WILL_LJMP(lua_error(L));
3143
3144 offset = output;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003145 if (lua_gettop(L) > 1) {
3146 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3147 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003148 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003149 offset += output;
3150 if (offset < output || offset > input + output) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003151 lua_pushfstring(L, "offset out of range.");
3152 WILL_LJMP(lua_error(L));
3153 }
3154 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003155 len = output + input - offset;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003156 if (lua_gettop(L) == 3) {
3157 len = MAY_LJMP(luaL_checkinteger(L, 3));
3158 if (!len)
3159 goto dup;
3160 if (len == -1)
3161 len = global.tune.bufsize;
3162 if (len < 0) {
3163 lua_pushfstring(L, "length out of range.");
3164 WILL_LJMP(lua_error(L));
3165 }
3166 }
3167
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003168 if (offset + len > output + input) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003169 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3170 /* Yield waiting for more data, as requested */
3171 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0));
3172 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003173 len = output + input - offset;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003174 }
3175
3176 dup:
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003177 _hlua_channel_dup(chn, L, offset, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003178 return 1;
3179}
3180
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003181/* Copies the first line (including the trailing LF) of input data in the
3182 * channel's buffer. It is possible to set a specific offset (0 by default) and
3183 * a length (all remaining input data starting for the offset by default). If
3184 * there is not enough input data and more data can be received, the function
3185 * yields. If a length is explicitly specified, no more data are
3186 * copied. Otherwise, if no LF is found and more data can be received, this
3187 * function yields.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003188 *
3189 * From an action, All input data are considered. For a filter, the offset and
3190 * the length of input data to consider are retrieved from the filter context.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003191 */
3192__LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003193{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003194 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003195 struct filter *filter;
3196 size_t l, input, output;
3197 int offset, len;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003198
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003199 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003200 output = co_data(chn);
3201 input = ci_data(chn);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003202
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003203 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3204 if (filter && !hlua_filter_from_payload(filter))
3205 WILL_LJMP(lua_error(L));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003206
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003207 offset = output;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003208 if (lua_gettop(L) > 1) {
3209 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3210 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003211 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003212 offset += output;
3213 if (offset < output || offset > input + output) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003214 lua_pushfstring(L, "offset out of range.");
3215 WILL_LJMP(lua_error(L));
3216 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003217 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003218
3219 len = output + input - offset;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003220 if (lua_gettop(L) == 3) {
3221 len = MAY_LJMP(luaL_checkinteger(L, 3));
3222 if (!len)
3223 goto dup;
3224 if (len == -1)
3225 len = global.tune.bufsize;
3226 if (len < 0) {
3227 lua_pushfstring(L, "length out of range.");
3228 WILL_LJMP(lua_error(L));
3229 }
3230 }
3231
3232 for (l = 0; l < len; l++) {
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003233 if (l + offset >= output + input)
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003234 break;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003235 if (*(b_peek(&chn->buf, offset + l)) == '\n') {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003236 len = l+1;
3237 goto dup;
3238 }
3239 }
3240
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003241 if (offset + len > output + input) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003242 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3243 /* Yield waiting for more data */
3244 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0));
3245 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003246 len = output + input - offset;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003247 }
3248
3249 dup:
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003250 _hlua_channel_dup(chn, L, offset, len);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003251 return 1;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003252}
3253
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003254/* [ DEPRECATED ]
3255 *
3256 * Duplicate all input data foud in the channel's buffer. The data are not
3257 * removed from the buffer. This function relies on _hlua_channel_dup().
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003258 *
3259 * From an action, All input data are considered. For a filter, the offset and
3260 * the length of input data to consider are retrieved from the filter context.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003261 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003262__LJMP static int hlua_channel_dup(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003263{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003264 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003265 struct filter *filter;
3266 size_t offset, len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003267
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003268 MAY_LJMP(check_args(L, 1, "dup"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003269 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003270 if (IS_HTX_STRM(chn_strm(chn))) {
3271 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3272 WILL_LJMP(lua_error(L));
3273 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003274
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003275 offset = co_data(chn);
3276 len = ci_data(chn);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003277
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003278 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3279 if (filter && !hlua_filter_from_payload(filter))
3280 WILL_LJMP(lua_error(L));
3281
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003282 if (!ci_data(chn) && channel_input_closed(chn)) {
3283 lua_pushnil(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003284 return 1;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003285 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003286
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003287 _hlua_channel_dup(chn, L, offset, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003288 return 1;
3289}
3290
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003291/* [ DEPRECATED ]
3292 *
3293 * Get all input data foud in the channel's buffer. The data are removed from
3294 * the buffer after the copy. This function relies on _hlua_channel_dup() and
3295 * _hlua_channel_delete().
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003296 *
3297 * From an action, All input data are considered. For a filter, the offset and
3298 * the length of input data to consider are retrieved from the filter context.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003299 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003300__LJMP static int hlua_channel_get(lua_State *L)
3301{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003302 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003303 struct filter *filter;
3304 size_t offset, len;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003305 int ret;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003306
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003307 MAY_LJMP(check_args(L, 1, "get"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003308 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3309 if (IS_HTX_STRM(chn_strm(chn))) {
3310 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3311 WILL_LJMP(lua_error(L));
3312 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003313
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003314 offset = co_data(chn);
3315 len = ci_data(chn);
3316
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003317 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3318 if (filter && !hlua_filter_from_payload(filter))
3319 WILL_LJMP(lua_error(L));
3320
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003321 if (!ci_data(chn) && channel_input_closed(chn)) {
3322 lua_pushnil(L);
3323 return 1;
3324 }
3325
3326 ret = _hlua_channel_dup(chn, L, offset, len);
3327 _hlua_channel_delete(chn, offset, ret);
3328 return 1;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003329}
3330
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003331/* This functions consumes and returns one line. If the channel is closed,
3332 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003333 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003334 * value.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003335 *
3336 * From an action, All input data are considered. For a filter, the offset and
3337 * the length of input data to consider are retrieved from the filter context.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003338 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003339__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003340{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003341 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003342 struct filter *filter;
3343 size_t l, offset, len;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003344 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003345
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003346 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003347
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003348 offset = co_data(chn);
3349 len = ci_data(chn);
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003350
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003351 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3352 if (filter && !hlua_filter_from_payload(filter))
3353 WILL_LJMP(lua_error(L));
3354
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003355 if (!ci_data(chn) && channel_input_closed(chn)) {
3356 lua_pushnil(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003357 return 1;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003358 }
3359
3360 for (l = 0; l < len; l++) {
3361 if (*(b_peek(&chn->buf, offset+l)) == '\n') {
3362 len = l+1;
3363 goto dup;
3364 }
3365 }
3366
3367 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3368 /* Yield waiting for more data */
3369 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
3370 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003371
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003372 dup:
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003373 ret = _hlua_channel_dup(chn, L, offset, len);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003374 _hlua_channel_delete(chn, offset, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003375 return 1;
3376}
3377
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003378/* [ DEPRECATED ]
3379 *
3380 * Check arguments for the function "hlua_channel_getline_yield".
3381 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003382__LJMP static int hlua_channel_getline(lua_State *L)
3383{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003384 struct channel *chn;
3385
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003386 MAY_LJMP(check_args(L, 1, "getline"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003387 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3388 if (IS_HTX_STRM(chn_strm(chn))) {
3389 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3390 WILL_LJMP(lua_error(L));
3391 }
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003392 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3393}
3394
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003395/* Retrieves a given amount of input data at the given offset. By default all
3396 * available input data are returned. The offset may be negactive to start from
3397 * the end of input data. The length may be -1 to set it to the maximum buffer
3398 * size.
3399 */
3400__LJMP static int hlua_channel_get_data(lua_State *L)
3401{
3402 struct channel *chn;
3403
3404 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3405 WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments"));
3406 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3407 if (IS_HTX_STRM(chn_strm(chn))) {
3408 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3409 WILL_LJMP(lua_error(L));
3410 }
3411 return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0));
3412}
3413
3414/* Retrieves a given amount of input data at the given offset. By default all
3415 * available input data are returned. The offset may be negactive to start from
3416 * the end of input data. The length may be -1 to set it to the maximum buffer
3417 * size.
3418 */
3419__LJMP static int hlua_channel_get_line(lua_State *L)
3420{
3421 struct channel *chn;
3422
3423 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3424 WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments"));
3425 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3426 if (IS_HTX_STRM(chn_strm(chn))) {
3427 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3428 WILL_LJMP(lua_error(L));
3429 }
3430 return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0));
3431}
3432
3433/* Appends a string into the input side of channel. It returns the length of the
3434 * written string, or -1 if the channel is closed or if the buffer size is too
3435 * little for the data. 0 may be returned if nothing is copied. This function
3436 * does not yield.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003437 *
3438 * For a filter, the context is updated on success.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003439 */
Christopher Faulet23976d92021-08-06 09:59:49 +02003440__LJMP static int hlua_channel_append(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003441{
Christopher Faulet23976d92021-08-06 09:59:49 +02003442 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003443 struct filter *filter;
Christopher Faulet23976d92021-08-06 09:59:49 +02003444 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003445 size_t sz, offset, len;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003446 int ret;
Christopher Faulet23976d92021-08-06 09:59:49 +02003447
3448 MAY_LJMP(check_args(L, 2, "append"));
3449 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003450 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003451 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003452 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003453 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003454 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003455
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003456 offset = co_data(chn);
3457 len = ci_data(chn);
3458
3459 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3460 if (filter && !hlua_filter_from_payload(filter))
3461 WILL_LJMP(lua_error(L));
3462
3463 ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset);
3464 if (ret > 0 && filter) {
3465 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3466
3467 flt_update_offsets(filter, chn, ret);
3468 flt_ctx->cur_len[CHN_IDX(chn)] += ret;
3469 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003470 lua_pushinteger(L, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003471 return 1;
3472}
3473
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003474/* Prepends a string into the input side of channel. It returns the length of the
3475 * written string, or -1 if the channel is closed or if the buffer size is too
3476 * little for the data. 0 may be returned if nothing is copied. This function
3477 * does not yield.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003478 *
3479 * For a filter, the context is updated on success.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003480 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003481__LJMP static int hlua_channel_prepend(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003482{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003483 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003484 struct filter *filter;
Christopher Faulet23976d92021-08-06 09:59:49 +02003485 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003486 size_t sz, offset, len;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003487 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003488
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003489 MAY_LJMP(check_args(L, 2, "prepend"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003490 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003491 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3492 if (IS_HTX_STRM(chn_strm(chn))) {
3493 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3494 WILL_LJMP(lua_error(L));
3495 }
3496
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003497 offset = co_data(chn);
3498 len = ci_data(chn);
3499
3500 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3501 if (filter && !hlua_filter_from_payload(filter))
3502 WILL_LJMP(lua_error(L));
3503
3504 ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset);
3505 if (ret > 0 && filter) {
3506 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3507
3508 flt_update_offsets(filter, chn, ret);
3509 flt_ctx->cur_len[CHN_IDX(chn)] += ret;
3510 }
3511
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003512 lua_pushinteger(L, ret);
3513 return 1;
3514}
3515
3516/* Inserts a given amount of input data at the given offset by a string
3517 * content. By default the string is appended at the end of input data. It
3518 * returns the length of the written string, or -1 if the channel is closed or
3519 * if the buffer size is too little for the data.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003520 *
3521 * For a filter, the context is updated on success.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003522 */
3523__LJMP static int hlua_channel_insert_data(lua_State *L)
3524{
3525 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003526 struct filter *filter;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003527 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003528 size_t sz, input, output;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003529 int ret, offset;
3530
3531 if (lua_gettop(L) < 2 || lua_gettop(L) > 3)
3532 WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments"));
3533 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3534 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003535
3536 output = co_data(chn);
3537 input = ci_data(chn);
3538
3539 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3540 if (filter && !hlua_filter_from_payload(filter))
3541 WILL_LJMP(lua_error(L));
3542
3543 offset = input + output;
3544 if (lua_gettop(L) > 2) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003545 offset = MAY_LJMP(luaL_checkinteger(L, 3));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003546 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003547 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003548 offset += output;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003549 if (offset < output || offset > output + input) {
3550 lua_pushfstring(L, "offset out of range.");
3551 WILL_LJMP(lua_error(L));
3552 }
3553 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003554 if (IS_HTX_STRM(chn_strm(chn))) {
3555 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3556 WILL_LJMP(lua_error(L));
3557 }
3558
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003559 ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset);
3560 if (ret > 0 && filter) {
3561 struct hlua_flt_ctx *flt_ctx = filter->ctx;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003562
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003563 flt_update_offsets(filter, chn, ret);
3564 flt_ctx->cur_len[CHN_IDX(chn)] += ret;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003565 }
3566
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003567 lua_pushinteger(L, ret);
3568 return 1;
3569}
3570/* Replaces a given amount of input data at the given offset by a string
3571 * content. By default all remaining data are removed (offset = 0 and len =
3572 * -1). It returns the length of the written string, or -1 if the channel is
3573 * closed or if the buffer size is too little for the data.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003574 *
3575 * For a filter, the context is updated on success.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003576 */
3577__LJMP static int hlua_channel_set_data(lua_State *L)
3578{
3579 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003580 struct filter *filter;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003581 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003582 size_t sz, input, output;
3583 int ret, offset, len;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003584
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003585 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
3586 WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments"));
3587 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3588 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003589
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003590 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003591 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003592 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003593 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003594
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003595 output = co_data(chn);
3596 input = ci_data(chn);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003597
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003598 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3599 if (filter && !hlua_filter_from_payload(filter))
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003600 WILL_LJMP(lua_error(L));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003601
3602 offset = output;
3603 if (lua_gettop(L) > 2) {
3604 offset = MAY_LJMP(luaL_checkinteger(L, 3));
3605 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003606 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003607 offset += output;
3608 if (offset < output || offset > input + output) {
3609 lua_pushfstring(L, "offset out of range.");
3610 WILL_LJMP(lua_error(L));
3611 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003612 }
3613
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003614 len = output + input - offset;
3615 if (lua_gettop(L) == 4) {
3616 len = MAY_LJMP(luaL_checkinteger(L, 4));
3617 if (!len)
3618 goto set;
3619 if (len == -1)
3620 len = output + input - offset;
3621 if (len < 0 || offset + len > output + input) {
3622 lua_pushfstring(L, "length out of range.");
3623 WILL_LJMP(lua_error(L));
3624 }
3625 }
3626
3627 set:
Christopher Faulet23976d92021-08-06 09:59:49 +02003628 /* Be sure we can copied the string once input data will be removed. */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003629 if (sz > c_room(chn) + len)
Christopher Faulet23976d92021-08-06 09:59:49 +02003630 lua_pushinteger(L, -1);
3631 else {
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003632 _hlua_channel_delete(chn, offset, len);
3633 ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset);
3634 if (filter) {
3635 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3636
3637 len -= (ret > 0 ? ret : 0);
3638 flt_update_offsets(filter, chn, -len);
3639 flt_ctx->cur_len[CHN_IDX(chn)] -= len;
3640 }
3641
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003642 lua_pushinteger(L, ret);
Christopher Faulet23976d92021-08-06 09:59:49 +02003643 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003644 return 1;
3645}
3646
3647/* Removes a given amount of input data at the given offset. By default all
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003648 * input data are removed (offset = 0 and len = -1). It returns the amount of
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003649 * the removed data.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003650 *
3651 * For a filter, the context is updated on success.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003652 */
3653__LJMP static int hlua_channel_del_data(lua_State *L)
3654{
3655 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003656 struct filter *filter;
3657 size_t input, output;
3658 int offset, len;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003659
3660 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3661 WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments"));
3662 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003663
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003664 if (IS_HTX_STRM(chn_strm(chn))) {
3665 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3666 WILL_LJMP(lua_error(L));
3667 }
3668
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003669 output = co_data(chn);
3670 input = ci_data(chn);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003671
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003672 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3673 if (filter && !hlua_filter_from_payload(filter))
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003674 WILL_LJMP(lua_error(L));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003675
3676 offset = output;
3677 if (lua_gettop(L) > 2) {
3678 offset = MAY_LJMP(luaL_checkinteger(L, 3));
3679 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003680 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003681 offset += output;
3682 if (offset < output || offset > input + output) {
3683 lua_pushfstring(L, "offset out of range.");
3684 WILL_LJMP(lua_error(L));
3685 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003686 }
3687
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003688 len = output + input - offset;
3689 if (lua_gettop(L) == 4) {
3690 len = MAY_LJMP(luaL_checkinteger(L, 4));
3691 if (!len)
3692 goto end;
3693 if (len == -1)
3694 len = output + input - offset;
3695 if (len < 0 || offset + len > output + input) {
3696 lua_pushfstring(L, "length out of range.");
3697 WILL_LJMP(lua_error(L));
3698 }
3699 }
3700
3701 _hlua_channel_delete(chn, offset, len);
3702 if (filter) {
3703 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3704
3705 flt_update_offsets(filter, chn, -len);
3706 flt_ctx->cur_len[CHN_IDX(chn)] -= len;
3707 }
3708
3709 end:
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003710 lua_pushinteger(L, len);
Christopher Faulet23976d92021-08-06 09:59:49 +02003711 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003712}
3713
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003714/* Append data in the output side of the buffer. This data is immediately
3715 * sent. The function returns the amount of data written. If the buffer
3716 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003717 * if the channel is closed.
3718 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003719__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003720{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003721 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003722 struct filter *filter;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003723 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003724 size_t offset, len, sz;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003725 int l, ret;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003726 struct hlua *hlua;
3727
3728 /* Get hlua struct, or NULL if we execute from main lua state */
3729 hlua = hlua_gethlua(L);
3730 if (!hlua) {
3731 lua_pushnil(L);
3732 return 1;
3733 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003734
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003735 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3736 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3737 l = MAY_LJMP(luaL_checkinteger(L, 3));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003738
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003739 offset = co_data(chn);
3740 len = ci_data(chn);
3741
3742 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3743 if (filter && !hlua_filter_from_payload(filter))
3744 WILL_LJMP(lua_error(L));
3745
3746
Willy Tarreau47860ed2015-03-10 14:07:50 +01003747 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003748 lua_pushinteger(L, -1);
3749 return 1;
3750 }
3751
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003752 len = c_room(chn);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003753 if (len > sz -l) {
3754 if (filter) {
3755 lua_pushinteger(L, -1);
3756 return 1;
3757 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003758 len = sz - l;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003759 }
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003760
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003761 ret = _hlua_channel_insert(chn, L, ist2(str, len), offset);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003762 if (ret == -1) {
3763 lua_pop(L, 1);
3764 lua_pushinteger(L, -1);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003765 return 1;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003766 }
3767 if (ret) {
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003768 if (filter) {
3769 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3770
3771
3772 flt_update_offsets(filter, chn, ret);
3773 FLT_OFF(filter, chn) += ret;
3774 flt_ctx->cur_off[CHN_IDX(chn)] += ret;
3775 }
3776 else
3777 c_adv(chn, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003778
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003779 l += ret;
3780 lua_pop(L, 1);
3781 lua_pushinteger(L, l);
3782 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003783
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003784 if (l < sz) {
3785 /* Yield only if the channel's output is not empty.
3786 * Otherwise it means we cannot add more data. */
3787 if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L)))
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003788 return 1;
3789
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003790 /* If we are waiting for space in the response buffer, we
3791 * must set the flag WAKERESWR. This flag required the task
3792 * wake up if any activity is detected on the response buffer.
3793 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003794 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003795 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003796 else
3797 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003798 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003799 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003800
3801 return 1;
3802}
3803
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003804/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003805 * yield the LUA process, and resume it without checking the
3806 * input arguments.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003807 *
3808 * This function cannot be called from a filter.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003809 */
3810__LJMP static int hlua_channel_send(lua_State *L)
3811{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003812 struct channel *chn;
3813
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003814 MAY_LJMP(check_args(L, 2, "send"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003815 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3816 if (IS_HTX_STRM(chn_strm(chn))) {
3817 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3818 WILL_LJMP(lua_error(L));
3819 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003820 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003821 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003822}
3823
3824/* This function forward and amount of butes. The data pass from
3825 * the input side of the buffer to the output side, and can be
3826 * forwarded. This function never fails.
3827 *
3828 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003829 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003830 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003831__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003832{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003833 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003834 struct filter *filter;
3835 size_t offset, len, fwd;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003836 int l, max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003837 struct hlua *hlua;
3838
3839 /* Get hlua struct, or NULL if we execute from main lua state */
3840 hlua = hlua_gethlua(L);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003841 if (!hlua) {
3842 lua_pushnil(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003843 return 1;
Thierry Fournier77016da2020-08-15 14:35:51 +02003844 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003845
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003846 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003847 fwd = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003848 l = MAY_LJMP(luaL_checkinteger(L, -1));
3849
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003850 offset = co_data(chn);
3851 len = ci_data(chn);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003852
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003853 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3854 if (filter && !hlua_filter_from_payload(filter))
3855 WILL_LJMP(lua_error(L));
3856
3857 max = fwd - l;
3858 if (max > len)
3859 max = len;
3860
3861 if (filter) {
3862 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3863
3864 FLT_OFF(filter, chn) += max;
3865 flt_ctx->cur_off[CHN_IDX(chn)] += max;
3866 flt_ctx->cur_len[CHN_IDX(chn)] -= max;
3867 }
3868 else
3869 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003870
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003871 l += max;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003872 lua_pop(L, 1);
3873 lua_pushinteger(L, l);
3874
3875 /* Check if it miss bytes to forward. */
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003876 if (l < fwd) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003877 /* The the input channel or the output channel are closed, we
3878 * must return the amount of data forwarded.
3879 */
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003880 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003881 return 1;
3882
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003883 /* If we are waiting for space data in the response buffer, we
3884 * must set the flag WAKERESWR. This flag required the task
3885 * wake up if any activity is detected on the response buffer.
3886 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003887 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003888 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003889 else
3890 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003891
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003892 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003893 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003894 }
3895
3896 return 1;
3897}
3898
3899/* Just check the input and prepare the stack for the previous
3900 * function "hlua_channel_forward_yield"
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003901 *
3902 * This function cannot be called from a filter.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003903 */
3904__LJMP static int hlua_channel_forward(lua_State *L)
3905{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003906 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003907
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003908 MAY_LJMP(check_args(L, 2, "forward"));
3909 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3910 if (IS_HTX_STRM(chn_strm(chn))) {
3911 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3912 WILL_LJMP(lua_error(L));
3913 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003914 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003915 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003916}
3917
3918/* Just returns the number of bytes available in the input
3919 * side of the buffer. This function never fails.
3920 */
3921__LJMP static int hlua_channel_get_in_len(lua_State *L)
3922{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003923 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003924 struct filter *filter;
3925 size_t output, input;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003926
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003927 MAY_LJMP(check_args(L, 1, "input"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003928 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003929
3930 output = co_data(chn);
3931 input = ci_data(chn);
3932 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3933 if (filter || !IS_HTX_STRM(chn_strm(chn)))
3934 lua_pushinteger(L, input);
3935 else {
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003936 struct htx *htx = htxbuf(&chn->buf);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003937
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003938 lua_pushinteger(L, htx->data - co_data(chn));
3939 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003940 return 1;
3941}
3942
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003943/* Returns true if the channel is full. */
3944__LJMP static int hlua_channel_is_full(lua_State *L)
3945{
3946 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003947
3948 MAY_LJMP(check_args(L, 1, "is_full"));
3949 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003950 /* ignore the reserve, we are not on a producer side (ie in an
3951 * applet).
3952 */
3953 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003954 return 1;
3955}
3956
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003957/* Returns true if the channel may still receive data. */
3958__LJMP static int hlua_channel_may_recv(lua_State *L)
3959{
3960 struct channel *chn;
3961
3962 MAY_LJMP(check_args(L, 1, "may_recv"));
3963 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3964 lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn)));
3965 return 1;
3966}
3967
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003968/* Returns true if the channel is the response channel. */
3969__LJMP static int hlua_channel_is_resp(lua_State *L)
3970{
3971 struct channel *chn;
3972
3973 MAY_LJMP(check_args(L, 1, "is_resp"));
3974 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3975
3976 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3977 return 1;
3978}
3979
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003980/* Just returns the number of bytes available in the output
3981 * side of the buffer. This function never fails.
3982 */
3983__LJMP static int hlua_channel_get_out_len(lua_State *L)
3984{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003985 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003986 size_t output, input;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003987
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003988 MAY_LJMP(check_args(L, 1, "output"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003989 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003990
3991 output = co_data(chn);
3992 input = ci_data(chn);
3993 hlua_channel_filter(L, 1, chn, &output, &input);
3994
3995 lua_pushinteger(L, output);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003996 return 1;
3997}
3998
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003999/*
4000 *
4001 *
4002 * Class Fetches
4003 *
4004 *
4005 */
4006
4007/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004008 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004009 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004010__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004011{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004012 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004013}
4014
4015/* This function creates and push in the stack a fetch object according
4016 * with a current TXN.
4017 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004018static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004019{
Willy Tarreau7073c472015-04-06 11:15:40 +02004020 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004021
4022 /* Check stack size. */
4023 if (!lua_checkstack(L, 3))
4024 return 0;
4025
4026 /* Create the object: obj[0] = userdata.
4027 * Note that the base of the Fetches object is the
4028 * transaction object.
4029 */
4030 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02004031 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004032 lua_rawseti(L, -2, 0);
4033
Willy Tarreau7073c472015-04-06 11:15:40 +02004034 hsmp->s = txn->s;
4035 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01004036 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004037 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004038
4039 /* Pop a class sesison metatable and affect it to the userdata. */
4040 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
4041 lua_setmetatable(L, -2);
4042
4043 return 1;
4044}
4045
4046/* This function is an LUA binding. It is called with each sample-fetch.
4047 * It uses closure argument to store the associated sample-fetch. It
4048 * returns only one argument or throws an error. An error is thrown
4049 * only if an error is encountered during the argument parsing. If
4050 * the "sample-fetch" function fails, nil is returned.
4051 */
4052__LJMP static int hlua_run_sample_fetch(lua_State *L)
4053{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02004054 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01004055 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02004056 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004057 int i;
4058 struct sample smp;
4059
4060 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004061 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004062
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004063 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02004064 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004065
Thierry FOURNIERca988662015-12-20 18:43:03 +01004066 /* Check execution authorization. */
4067 if (f->use & SMP_USE_HTTP_ANY &&
4068 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
4069 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
4070 "is not available in Lua services", f->kw);
4071 WILL_LJMP(lua_error(L));
4072 }
4073
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004074 /* Get extra arguments. */
4075 for (i = 0; i < lua_gettop(L) - 1; i++) {
4076 if (i >= ARGM_NBARGS)
4077 break;
4078 hlua_lua2arg(L, i + 2, &args[i]);
4079 }
4080 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004081 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004082
4083 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02004084 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004085
4086 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01004087 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004088 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004089 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004090 }
4091
4092 /* Initialise the sample. */
4093 memset(&smp, 0, sizeof(smp));
4094
4095 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01004096 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004097 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004098 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004099 lua_pushstring(L, "");
4100 else
4101 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004102 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004103 }
4104
4105 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004106 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004107 hlua_smp2lua_str(L, &smp);
4108 else
4109 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004110
4111 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004112 free_args(args);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004113 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004114
4115 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004116 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004117 WILL_LJMP(lua_error(L));
4118 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004119}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01004120
4121/*
4122 *
4123 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004124 * Class Converters
4125 *
4126 *
4127 */
4128
4129/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004130 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004131 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004132__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004133{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004134 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004135}
4136
4137/* This function creates and push in the stack a Converters object
4138 * according with a current TXN.
4139 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004140static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004141{
Willy Tarreau7073c472015-04-06 11:15:40 +02004142 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004143
4144 /* Check stack size. */
4145 if (!lua_checkstack(L, 3))
4146 return 0;
4147
4148 /* Create the object: obj[0] = userdata.
4149 * Note that the base of the Converters object is the
4150 * same than the TXN object.
4151 */
4152 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02004153 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004154 lua_rawseti(L, -2, 0);
4155
Willy Tarreau7073c472015-04-06 11:15:40 +02004156 hsmp->s = txn->s;
4157 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01004158 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004159 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004160
Willy Tarreau87b09662015-04-03 00:22:06 +02004161 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004162 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
4163 lua_setmetatable(L, -2);
4164
4165 return 1;
4166}
4167
4168/* This function is an LUA binding. It is called with each converter.
4169 * It uses closure argument to store the associated converter. It
4170 * returns only one argument or throws an error. An error is thrown
4171 * only if an error is encountered during the argument parsing. If
4172 * the converter function function fails, nil is returned.
4173 */
4174__LJMP static int hlua_run_sample_conv(lua_State *L)
4175{
Willy Tarreauda5f1082015-04-06 11:17:13 +02004176 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004177 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02004178 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004179 int i;
4180 struct sample smp;
4181
4182 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004183 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004184
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004185 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02004186 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004187
4188 /* Get extra arguments. */
4189 for (i = 0; i < lua_gettop(L) - 2; i++) {
4190 if (i >= ARGM_NBARGS)
4191 break;
4192 hlua_lua2arg(L, i + 3, &args[i]);
4193 }
4194 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004195 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004196
4197 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02004198 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004199
4200 /* Run the special args checker. */
4201 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
4202 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004203 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004204 }
4205
4206 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004207 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004208 if (!hlua_lua2smp(L, 2, &smp)) {
4209 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004210 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004211 }
4212
Willy Tarreau1777ea62016-03-10 16:15:46 +01004213 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
4214
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004215 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004216 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004217 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004218 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004219 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004220 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004221 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
4222 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004223 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004224 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004225 }
4226
4227 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004228 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004229 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004230 lua_pushstring(L, "");
4231 else
Willy Tarreaua678b432015-08-28 10:14:59 +02004232 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004233 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004234 }
4235
4236 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004237 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004238 hlua_smp2lua_str(L, &smp);
4239 else
4240 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004241 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004242 free_args(args);
Willy Tarreaua678b432015-08-28 10:14:59 +02004243 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004244
4245 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004246 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004247 WILL_LJMP(lua_error(L));
4248 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004249}
4250
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004251/*
4252 *
4253 *
4254 * Class AppletTCP
4255 *
4256 *
4257 */
4258
4259/* Returns a struct hlua_txn if the stack entry "ud" is
4260 * a class stream, otherwise it throws an error.
4261 */
4262__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
4263{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004264 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004265}
4266
4267/* This function creates and push in the stack an Applet object
4268 * according with a current TXN.
4269 */
4270static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
4271{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004272 struct hlua_appctx *luactx;
Christopher Faulet86e1c332021-12-20 17:09:39 +01004273 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004274 struct stream *s = si_strm(si);
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004275 struct proxy *p;
4276
4277 ALREADY_CHECKED(s);
4278 p = s->be;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004279
4280 /* Check stack size. */
4281 if (!lua_checkstack(L, 3))
4282 return 0;
4283
4284 /* Create the object: obj[0] = userdata.
4285 * Note that the base of the Converters object is the
4286 * same than the TXN object.
4287 */
4288 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004289 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004290 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004291 luactx->appctx = ctx;
4292 luactx->htxn.s = s;
4293 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004294
4295 /* Create the "f" field that contains a list of fetches. */
4296 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004297 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004298 return 0;
4299 lua_settable(L, -3);
4300
4301 /* Create the "sf" field that contains a list of stringsafe fetches. */
4302 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004303 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004304 return 0;
4305 lua_settable(L, -3);
4306
4307 /* Create the "c" field that contains a list of converters. */
4308 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004309 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004310 return 0;
4311 lua_settable(L, -3);
4312
4313 /* Create the "sc" field that contains a list of stringsafe converters. */
4314 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004315 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004316 return 0;
4317 lua_settable(L, -3);
4318
4319 /* Pop a class stream metatable and affect it to the table. */
4320 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
4321 lua_setmetatable(L, -2);
4322
4323 return 1;
4324}
4325
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004326__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
4327{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004328 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004329 struct stream *s;
4330 const char *name;
4331 size_t len;
4332 struct sample smp;
4333
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004334 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4335 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004336
4337 /* It is useles to retrieve the stream, but this function
4338 * runs only in a stream context.
4339 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004340 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004341 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004342 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004343
4344 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004345 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004346 hlua_lua2smp(L, 3, &smp);
4347
4348 /* Store the sample in a variable. */
4349 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004350
4351 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4352 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4353 else
4354 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4355
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004356 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004357}
4358
4359__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
4360{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004361 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004362 struct stream *s;
4363 const char *name;
4364 size_t len;
4365 struct sample smp;
4366
4367 MAY_LJMP(check_args(L, 2, "unset_var"));
4368
4369 /* It is useles to retrieve the stream, but this function
4370 * runs only in a stream context.
4371 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004372 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004373 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004374 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004375
4376 /* Unset the variable. */
4377 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004378 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4379 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004380}
4381
4382__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
4383{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004384 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004385 struct stream *s;
4386 const char *name;
4387 size_t len;
4388 struct sample smp;
4389
4390 MAY_LJMP(check_args(L, 2, "get_var"));
4391
4392 /* It is useles to retrieve the stream, but this function
4393 * runs only in a stream context.
4394 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004395 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004396 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004397 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004398
4399 smp_set_owner(&smp, s->be, s->sess, s, 0);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02004400 if (!vars_get_by_name(name, len, &smp, NULL)) {
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004401 lua_pushnil(L);
4402 return 1;
4403 }
4404
4405 return hlua_smp2lua(L, &smp);
4406}
4407
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004408__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
4409{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004410 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4411 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004412 struct hlua *hlua;
4413
4414 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004415 if (!s->hlua)
4416 return 0;
4417 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004418
4419 MAY_LJMP(check_args(L, 2, "set_priv"));
4420
4421 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004422 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004423
4424 /* Get and store new value. */
4425 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4426 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4427
4428 return 0;
4429}
4430
4431__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
4432{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004433 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4434 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004435 struct hlua *hlua;
4436
4437 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004438 if (!s->hlua) {
4439 lua_pushnil(L);
4440 return 1;
4441 }
4442 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004443
4444 /* Push configuration index in the stack. */
4445 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4446
4447 return 1;
4448}
4449
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004450/* If expected data not yet available, it returns a yield. This function
4451 * consumes the data in the buffer. It returns a string containing the
4452 * data. This string can be empty.
4453 */
4454__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
4455{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004456 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01004457 struct stream_interface *si = cs_si(luactx->appctx->owner);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004458 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004459 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004460 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004461 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004462 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004463
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004464 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004465 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004466
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004467 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004468 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004469 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004470 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004471 }
4472
4473 /* End of data: commit the total strings and return. */
4474 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004475 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004476 return 1;
4477 }
4478
4479 /* Ensure that the block 2 length is usable. */
4480 if (ret == 1)
4481 len2 = 0;
4482
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004483 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004484 luaL_addlstring(&luactx->b, blk1, len1);
4485 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004486
4487 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004488 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004489 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004490 return 1;
4491}
4492
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004493/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004494__LJMP static int hlua_applet_tcp_getline(lua_State *L)
4495{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004496 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004497
4498 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004499 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004500
4501 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
4502}
4503
4504/* If expected data not yet available, it returns a yield. This function
4505 * consumes the data in the buffer. It returns a string containing the
4506 * data. This string can be empty.
4507 */
4508__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
4509{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004510 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01004511 struct stream_interface *si = cs_si(luactx->appctx->owner);
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004512 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004513 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004514 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004515 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004516 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004517 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004518
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004519 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004520 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004521
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004522 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004523 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004524 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004525 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004526 }
4527
4528 /* End of data: commit the total strings and return. */
4529 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004530 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004531 return 1;
4532 }
4533
4534 /* Ensure that the block 2 length is usable. */
4535 if (ret == 1)
4536 len2 = 0;
4537
4538 if (len == -1) {
4539
4540 /* If len == -1, catenate all the data avalaile and
4541 * yield because we want to get all the data until
4542 * the end of data stream.
4543 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004544 luaL_addlstring(&luactx->b, blk1, len1);
4545 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004546 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004547 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004548 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004549
4550 } else {
4551
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004552 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004553 if (len1 > len)
4554 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004555 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004556 len -= len1;
4557
4558 /* Copy the second block. */
4559 if (len2 > len)
4560 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004561 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004562 len -= len2;
4563
4564 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004565 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004566
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004567 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004568 if (len > 0) {
4569 lua_pushinteger(L, len);
4570 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004571 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004572 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004573 }
4574
4575 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004576 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004577 return 1;
4578 }
4579
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004580 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004581 hlua_pusherror(L, "Lua: internal error");
4582 WILL_LJMP(lua_error(L));
4583 return 0;
4584}
4585
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004586/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004587__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4588{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004589 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004590 int len = -1;
4591
4592 if (lua_gettop(L) > 2)
4593 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4594 if (lua_gettop(L) >= 2) {
4595 len = MAY_LJMP(luaL_checkinteger(L, 2));
4596 lua_pop(L, 1);
4597 }
4598
4599 /* Confirm or set the required length */
4600 lua_pushinteger(L, len);
4601
4602 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004603 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004604
4605 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4606}
4607
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004608/* Append data in the output side of the buffer. This data is immediately
4609 * sent. The function returns the amount of data written. If the buffer
4610 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004611 * if the channel is closed.
4612 */
4613__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4614{
4615 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004616 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004617 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4618 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Christopher Faulet86e1c332021-12-20 17:09:39 +01004619 struct stream_interface *si = cs_si(luactx->appctx->owner);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004620 struct channel *chn = si_ic(si);
4621 int max;
4622
4623 /* Get the max amount of data which can write as input in the channel. */
4624 max = channel_recv_max(chn);
4625 if (max > (len - l))
4626 max = len - l;
4627
4628 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004629 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004630
4631 /* update counters. */
4632 l += max;
4633 lua_pop(L, 1);
4634 lua_pushinteger(L, l);
4635
4636 /* If some data is not send, declares the situation to the
4637 * applet, and returns a yield.
4638 */
4639 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004640 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004641 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004642 }
4643
4644 return 1;
4645}
4646
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004647/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004648 * yield the LUA process, and resume it without checking the
4649 * input arguments.
4650 */
4651__LJMP static int hlua_applet_tcp_send(lua_State *L)
4652{
4653 MAY_LJMP(check_args(L, 2, "send"));
4654 lua_pushinteger(L, 0);
4655
4656 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4657}
4658
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004659/*
4660 *
4661 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004662 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004663 *
4664 *
4665 */
4666
4667/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004668 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004669 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004670__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004671{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004672 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004673}
4674
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004675/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004676 * according with a current TXN.
4677 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004678static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004679{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004680 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004681 struct hlua_txn htxn;
Christopher Faulet86e1c332021-12-20 17:09:39 +01004682 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004683 struct stream *s = si_strm(si);
4684 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004685 struct htx *htx;
4686 struct htx_blk *blk;
4687 struct htx_sl *sl;
4688 struct ist path;
4689 unsigned long long len = 0;
4690 int32_t pos;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004691 struct http_uri_parser parser;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004692
4693 /* Check stack size. */
4694 if (!lua_checkstack(L, 3))
4695 return 0;
4696
4697 /* Create the object: obj[0] = userdata.
4698 * Note that the base of the Converters object is the
4699 * same than the TXN object.
4700 */
4701 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004702 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004703 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004704 luactx->appctx = ctx;
4705 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4706 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4707 luactx->htxn.s = s;
4708 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004709
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004710 /* Create the "f" field that contains a list of fetches. */
4711 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004712 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004713 return 0;
4714 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004715
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004716 /* Create the "sf" field that contains a list of stringsafe fetches. */
4717 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004718 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004719 return 0;
4720 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004721
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004722 /* Create the "c" field that contains a list of converters. */
4723 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004724 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004725 return 0;
4726 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004727
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004728 /* Create the "sc" field that contains a list of stringsafe converters. */
4729 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004730 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004731 return 0;
4732 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004733
Christopher Fauleta2097962019-07-15 16:25:33 +02004734 htx = htxbuf(&s->req.buf);
4735 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004736 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004737 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004738
Christopher Fauleta2097962019-07-15 16:25:33 +02004739 /* Stores the request method. */
4740 lua_pushstring(L, "method");
4741 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4742 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004743
Christopher Fauleta2097962019-07-15 16:25:33 +02004744 /* Stores the http version. */
4745 lua_pushstring(L, "version");
4746 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4747 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004748
Christopher Fauleta2097962019-07-15 16:25:33 +02004749 /* creates an array of headers. hlua_http_get_headers() crates and push
4750 * the array on the top of the stack.
4751 */
4752 lua_pushstring(L, "headers");
4753 htxn.s = s;
4754 htxn.p = px;
4755 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004756 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004757 return 0;
4758 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004759
Amaury Denoyellec453f952021-07-06 11:40:12 +02004760 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4761 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004762 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004763 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004764
Christopher Fauleta2097962019-07-15 16:25:33 +02004765 p = path.ptr;
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +01004766 end = istend(path);
Christopher Fauleta2097962019-07-15 16:25:33 +02004767 q = p;
4768 while (q < end && *q != '?')
4769 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004770
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004771 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004772 lua_pushstring(L, "path");
4773 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004774 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004775
Christopher Fauleta2097962019-07-15 16:25:33 +02004776 /* Stores the query string. */
4777 lua_pushstring(L, "qs");
4778 if (*q == '?')
4779 q++;
4780 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004781 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004782 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004783
Christopher Fauleta2097962019-07-15 16:25:33 +02004784 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4785 struct htx_blk *blk = htx_get_blk(htx, pos);
4786 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004787
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004788 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004789 break;
4790 if (type == HTX_BLK_DATA)
4791 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004792 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004793 if (htx->extra != ULLONG_MAX)
4794 len += htx->extra;
4795
4796 /* Stores the request path. */
4797 lua_pushstring(L, "length");
4798 lua_pushinteger(L, len);
4799 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004800
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004801 /* Create an empty array of HTTP request headers. */
4802 lua_pushstring(L, "response");
4803 lua_newtable(L);
4804 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004805
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004806 /* Pop a class stream metatable and affect it to the table. */
4807 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4808 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004809
4810 return 1;
4811}
4812
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004813__LJMP static int hlua_applet_http_set_var(lua_State *L)
4814{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004815 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004816 struct stream *s;
4817 const char *name;
4818 size_t len;
4819 struct sample smp;
4820
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004821 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4822 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004823
4824 /* It is useles to retrieve the stream, but this function
4825 * runs only in a stream context.
4826 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004827 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004828 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004829 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004830
4831 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004832 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004833 hlua_lua2smp(L, 3, &smp);
4834
4835 /* Store the sample in a variable. */
4836 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004837
4838 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4839 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4840 else
4841 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4842
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004843 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004844}
4845
4846__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4847{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004848 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004849 struct stream *s;
4850 const char *name;
4851 size_t len;
4852 struct sample smp;
4853
4854 MAY_LJMP(check_args(L, 2, "unset_var"));
4855
4856 /* It is useles to retrieve the stream, but this function
4857 * runs only in a stream context.
4858 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004859 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004860 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004861 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004862
4863 /* Unset the variable. */
4864 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004865 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4866 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004867}
4868
4869__LJMP static int hlua_applet_http_get_var(lua_State *L)
4870{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004871 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004872 struct stream *s;
4873 const char *name;
4874 size_t len;
4875 struct sample smp;
4876
4877 MAY_LJMP(check_args(L, 2, "get_var"));
4878
4879 /* It is useles to retrieve the stream, but this function
4880 * runs only in a stream context.
4881 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004882 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004883 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004884 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004885
4886 smp_set_owner(&smp, s->be, s->sess, s, 0);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02004887 if (!vars_get_by_name(name, len, &smp, NULL)) {
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004888 lua_pushnil(L);
4889 return 1;
4890 }
4891
4892 return hlua_smp2lua(L, &smp);
4893}
4894
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004895__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4896{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004897 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4898 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004899 struct hlua *hlua;
4900
4901 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004902 if (!s->hlua)
4903 return 0;
4904 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004905
4906 MAY_LJMP(check_args(L, 2, "set_priv"));
4907
4908 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004909 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004910
4911 /* Get and store new value. */
4912 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4913 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4914
4915 return 0;
4916}
4917
4918__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4919{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004920 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4921 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004922 struct hlua *hlua;
4923
4924 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004925 if (!s->hlua) {
4926 lua_pushnil(L);
4927 return 1;
4928 }
4929 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004930
4931 /* Push configuration index in the stack. */
4932 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4933
4934 return 1;
4935}
4936
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004937/* If expected data not yet available, it returns a yield. This function
4938 * consumes the data in the buffer. It returns a string containing the
4939 * data. This string can be empty.
4940 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004941__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004942{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004943 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01004944 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004945 struct channel *req = si_oc(si);
4946 struct htx *htx;
4947 struct htx_blk *blk;
4948 size_t count;
4949 int stop = 0;
4950
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004951 htx = htx_from_buf(&req->buf);
4952 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004953 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004954
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004955 while (count && !stop && blk) {
4956 enum htx_blk_type type = htx_get_blk_type(blk);
4957 uint32_t sz = htx_get_blksz(blk);
4958 struct ist v;
4959 uint32_t vlen;
4960 char *nl;
4961
4962 vlen = sz;
4963 if (vlen > count) {
4964 if (type != HTX_BLK_DATA)
4965 break;
4966 vlen = count;
4967 }
4968
4969 switch (type) {
4970 case HTX_BLK_UNUSED:
4971 break;
4972
4973 case HTX_BLK_DATA:
4974 v = htx_get_blk_value(htx, blk);
4975 v.len = vlen;
4976 nl = istchr(v, '\n');
4977 if (nl != NULL) {
4978 stop = 1;
4979 vlen = nl - v.ptr + 1;
4980 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004981 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004982 break;
4983
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004984 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004985 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004986 stop = 1;
4987 break;
4988
4989 default:
4990 break;
4991 }
4992
Willy Tarreau84240042022-02-28 16:51:23 +01004993 c_rew(req, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004994 count -= vlen;
4995 if (sz == vlen)
4996 blk = htx_remove_blk(htx, blk);
4997 else {
4998 htx_cut_data_blk(htx, blk, vlen);
4999 break;
5000 }
5001 }
5002
Christopher Fauleteccb31c2021-04-02 14:24:56 +02005003 /* The message was fully consumed and no more data are expected
5004 * (EOM flag set).
5005 */
5006 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
5007 stop = 1;
5008
Christopher Faulet0ae79d02019-02-27 21:36:59 +01005009 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005010 if (!stop) {
5011 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02005012 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005013 }
5014
5015 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005016 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005017 return 1;
5018}
5019
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005020
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005021/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005022__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005023{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005024 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005025
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005026 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005027 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005028
Christopher Fauleta2097962019-07-15 16:25:33 +02005029 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005030}
5031
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005032/* If expected data not yet available, it returns a yield. This function
5033 * consumes the data in the buffer. It returns a string containing the
5034 * data. This string can be empty.
5035 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005036__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005037{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005038 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01005039 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005040 struct channel *req = si_oc(si);
5041 struct htx *htx;
5042 struct htx_blk *blk;
5043 size_t count;
5044 int len;
5045
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005046 htx = htx_from_buf(&req->buf);
5047 len = MAY_LJMP(luaL_checkinteger(L, 2));
5048 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02005049 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005050 while (count && len && blk) {
5051 enum htx_blk_type type = htx_get_blk_type(blk);
5052 uint32_t sz = htx_get_blksz(blk);
5053 struct ist v;
5054 uint32_t vlen;
5055
5056 vlen = sz;
5057 if (len > 0 && vlen > len)
5058 vlen = len;
5059 if (vlen > count) {
5060 if (type != HTX_BLK_DATA)
5061 break;
5062 vlen = count;
5063 }
5064
5065 switch (type) {
5066 case HTX_BLK_UNUSED:
5067 break;
5068
5069 case HTX_BLK_DATA:
5070 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02005071 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005072 break;
5073
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005074 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005075 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005076 len = 0;
5077 break;
5078
5079 default:
5080 break;
5081 }
5082
Willy Tarreau84240042022-02-28 16:51:23 +01005083 c_rew(req, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005084 count -= vlen;
5085 if (len > 0)
5086 len -= vlen;
5087 if (sz == vlen)
5088 blk = htx_remove_blk(htx, blk);
5089 else {
5090 htx_cut_data_blk(htx, blk, vlen);
5091 break;
5092 }
5093 }
5094
Christopher Fauleteccb31c2021-04-02 14:24:56 +02005095 /* The message was fully consumed and no more data are expected
5096 * (EOM flag set).
5097 */
5098 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
5099 len = 0;
5100
Christopher Faulet0ae79d02019-02-27 21:36:59 +01005101 htx_to_buf(htx, &req->buf);
5102
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005103 /* If we are no other data available, yield waiting for new data. */
5104 if (len) {
5105 if (len > 0) {
5106 lua_pushinteger(L, len);
5107 lua_replace(L, 2);
5108 }
5109 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005110 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005111 }
5112
5113 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005114 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005115 return 1;
5116}
5117
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005118/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005119__LJMP static int hlua_applet_http_recv(lua_State *L)
5120{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005121 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005122 int len = -1;
5123
5124 /* Check arguments. */
5125 if (lua_gettop(L) > 2)
5126 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
5127 if (lua_gettop(L) >= 2) {
5128 len = MAY_LJMP(luaL_checkinteger(L, 2));
5129 lua_pop(L, 1);
5130 }
5131
Christopher Fauleta2097962019-07-15 16:25:33 +02005132 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005133
Christopher Fauleta2097962019-07-15 16:25:33 +02005134 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005135 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005136
Christopher Fauleta2097962019-07-15 16:25:33 +02005137 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005138}
5139
5140/* Append data in the output side of the buffer. This data is immediately
5141 * sent. The function returns the amount of data written. If the buffer
5142 * cannot contain the data, the function yields. The function returns -1
5143 * if the channel is closed.
5144 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005145__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005146{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005147 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01005148 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005149 struct channel *res = si_ic(si);
5150 struct htx *htx = htx_from_buf(&res->buf);
5151 const char *data;
5152 size_t len;
5153 int l = MAY_LJMP(luaL_checkinteger(L, 3));
5154 int max;
5155
Christopher Faulet9060fc02019-07-03 11:39:30 +02005156 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01005157 if (!max)
5158 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005159
5160 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
5161
5162 /* Get the max amount of data which can write as input in the channel. */
5163 if (max > (len - l))
5164 max = len - l;
5165
5166 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005167 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01005168 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005169
5170 /* update counters. */
5171 l += max;
5172 lua_pop(L, 1);
5173 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005174
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005175 /* If some data is not send, declares the situation to the
5176 * applet, and returns a yield.
5177 */
5178 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01005179 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01005180 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005181 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005182 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005183 }
5184
Christopher Fauleta2097962019-07-15 16:25:33 +02005185 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005186 return 1;
5187}
5188
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005189/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005190 * yield the LUA process, and resume it without checking the
5191 * input arguments.
5192 */
5193__LJMP static int hlua_applet_http_send(lua_State *L)
5194{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005195 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005196
5197 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005198 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005199 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
5200 WILL_LJMP(lua_error(L));
5201 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005202
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005203 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02005204 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005205
Christopher Fauleta2097962019-07-15 16:25:33 +02005206 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005207}
5208
5209__LJMP static int hlua_applet_http_addheader(lua_State *L)
5210{
5211 const char *name;
5212 int ret;
5213
5214 MAY_LJMP(hlua_checkapplet_http(L, 1));
5215 name = MAY_LJMP(luaL_checkstring(L, 2));
5216 MAY_LJMP(luaL_checkstring(L, 3));
5217
5218 /* Push in the stack the "response" entry. */
5219 ret = lua_getfield(L, 1, "response");
5220 if (ret != LUA_TTABLE) {
5221 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
5222 "is expected as an array. %s found", lua_typename(L, ret));
5223 WILL_LJMP(lua_error(L));
5224 }
5225
5226 /* check if the header is already registered if it is not
5227 * the case, register it.
5228 */
5229 ret = lua_getfield(L, -1, name);
5230 if (ret == LUA_TNIL) {
5231
5232 /* Entry not found. */
5233 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
5234
5235 /* Insert the new header name in the array in the top of the stack.
5236 * It left the new array in the top of the stack.
5237 */
5238 lua_newtable(L);
5239 lua_pushvalue(L, 2);
5240 lua_pushvalue(L, -2);
5241 lua_settable(L, -4);
5242
5243 } else if (ret != LUA_TTABLE) {
5244
5245 /* corruption error. */
5246 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
5247 "is expected as an array. %s found", name, lua_typename(L, ret));
5248 WILL_LJMP(lua_error(L));
5249 }
5250
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07005251 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005252 * the header value as new entry.
5253 */
5254 lua_pushvalue(L, 3);
5255 ret = lua_rawlen(L, -2);
5256 lua_rawseti(L, -2, ret + 1);
5257 lua_pushboolean(L, 1);
5258 return 1;
5259}
5260
5261__LJMP static int hlua_applet_http_status(lua_State *L)
5262{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005263 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005264 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005265 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005266
5267 if (status < 100 || status > 599) {
5268 lua_pushboolean(L, 0);
5269 return 1;
5270 }
5271
Willy Tarreau7e702d12021-04-28 17:59:21 +02005272 luactx->appctx->ctx.hlua_apphttp.status = status;
5273 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005274 lua_pushboolean(L, 1);
5275 return 1;
5276}
5277
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005278
Christopher Fauleta2097962019-07-15 16:25:33 +02005279__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005280{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005281 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01005282 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005283 struct channel *res = si_ic(si);
5284 struct htx *htx;
5285 struct htx_sl *sl;
5286 struct h1m h1m;
5287 const char *status, *reason;
5288 const char *name, *value;
5289 size_t nlen, vlen;
5290 unsigned int flags;
5291
5292 /* Send the message at once. */
5293 htx = htx_from_buf(&res->buf);
5294 h1m_init_res(&h1m);
5295
5296 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005297 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
5298 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005299 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02005300 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
5301 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005302 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5303 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
5304 }
5305 else {
5306 flags = HTX_SL_F_IS_RESP;
5307 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
5308 }
5309 if (!sl) {
5310 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005311 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005312 WILL_LJMP(lua_error(L));
5313 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02005314 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005315
5316 /* Get the array associated to the field "response" in the object AppletHTTP. */
5317 lua_pushvalue(L, 0);
5318 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5319 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005320 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005321 WILL_LJMP(lua_error(L));
5322 }
5323
5324 /* Browse the list of headers. */
5325 lua_pushnil(L);
5326 while(lua_next(L, -2) != 0) {
5327 /* We expect a string as -2. */
5328 if (lua_type(L, -2) != LUA_TSTRING) {
5329 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005330 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005331 lua_typename(L, lua_type(L, -2)));
5332 WILL_LJMP(lua_error(L));
5333 }
5334 name = lua_tolstring(L, -2, &nlen);
5335
5336 /* We expect an array as -1. */
5337 if (lua_type(L, -1) != LUA_TTABLE) {
5338 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 +02005339 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005340 name,
5341 lua_typename(L, lua_type(L, -1)));
5342 WILL_LJMP(lua_error(L));
5343 }
5344
5345 /* Browse the table who is on the top of the stack. */
5346 lua_pushnil(L);
5347 while(lua_next(L, -2) != 0) {
5348 int id;
5349
5350 /* We expect a number as -2. */
5351 if (lua_type(L, -2) != LUA_TNUMBER) {
5352 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 +02005353 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005354 name,
5355 lua_typename(L, lua_type(L, -2)));
5356 WILL_LJMP(lua_error(L));
5357 }
5358 id = lua_tointeger(L, -2);
5359
5360 /* We expect a string as -2. */
5361 if (lua_type(L, -1) != LUA_TSTRING) {
5362 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 +02005363 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005364 name, id,
5365 lua_typename(L, lua_type(L, -1)));
5366 WILL_LJMP(lua_error(L));
5367 }
5368 value = lua_tolstring(L, -1, &vlen);
5369
5370 /* Simple Protocol checks. */
Christopher Faulet545fbba2021-09-28 09:36:25 +02005371 if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) {
5372 int ret;
5373
5374 ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
5375 if (ret < 0) {
5376 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
5377 luactx->appctx->rule->arg.hlua_rule->fcn->name,
5378 name);
5379 WILL_LJMP(lua_error(L));
5380 }
5381 else if (ret == 0)
5382 goto next; /* Skip it */
5383 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005384 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
5385 struct ist v = ist2(value, vlen);
5386 int ret;
5387
5388 ret = h1_parse_cont_len_header(&h1m, &v);
5389 if (ret < 0) {
5390 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005391 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005392 name);
5393 WILL_LJMP(lua_error(L));
5394 }
5395 else if (ret == 0)
5396 goto next; /* Skip it */
5397 }
5398
5399 /* Add a new header */
5400 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
5401 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005402 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005403 name);
5404 WILL_LJMP(lua_error(L));
5405 }
5406 next:
5407 /* Remove the array from the stack, and get next element with a remaining string. */
5408 lua_pop(L, 1);
5409 }
5410
5411 /* Remove the array from the stack, and get next element with a remaining string. */
5412 lua_pop(L, 1);
5413 }
5414
5415 if (h1m.flags & H1_MF_CHNK)
5416 h1m.flags &= ~H1_MF_CLEN;
5417 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5418 h1m.flags |= H1_MF_XFER_LEN;
5419
5420 /* Uset HTX start-line flags */
5421 if (h1m.flags & H1_MF_XFER_ENC)
5422 flags |= HTX_SL_F_XFER_ENC;
5423 if (h1m.flags & H1_MF_XFER_LEN) {
5424 flags |= HTX_SL_F_XFER_LEN;
5425 if (h1m.flags & H1_MF_CHNK)
5426 flags |= HTX_SL_F_CHNK;
5427 else if (h1m.flags & H1_MF_CLEN)
5428 flags |= HTX_SL_F_CLEN;
5429 if (h1m.body_len == 0)
5430 flags |= HTX_SL_F_BODYLESS;
5431 }
5432 sl->flags |= flags;
5433
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07005434 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005435 * and the status code implies the presence of a message body, we must
5436 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005437 * for the keepalive compliance. If the applet announces a transfer-encoding
5438 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005439 */
5440 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02005441 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
5442 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
5443 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005444 /* Add a new header */
5445 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
5446 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
5447 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005448 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005449 WILL_LJMP(lua_error(L));
5450 }
5451 }
5452
5453 /* Finalize headers. */
5454 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
5455 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005456 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005457 WILL_LJMP(lua_error(L));
5458 }
5459
5460 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
5461 b_reset(&res->buf);
5462 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5463 WILL_LJMP(lua_error(L));
5464 }
5465
5466 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01005467 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005468
5469 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005470 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005471 return 0;
5472
5473}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005474/* We will build the status line and the headers of the HTTP response.
5475 * We will try send at once if its not possible, we give back the hand
5476 * waiting for more room.
5477 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005478__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005479{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005480 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01005481 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005482 struct channel *res = si_ic(si);
5483
5484 if (co_data(res)) {
5485 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02005486 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005487 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005488 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005489}
5490
5491
Christopher Fauleta2097962019-07-15 16:25:33 +02005492__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005493{
Christopher Fauleta2097962019-07-15 16:25:33 +02005494 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005495}
5496
Christopher Fauleta2097962019-07-15 16:25:33 +02005497/*
5498 *
5499 *
5500 * Class HTTP
5501 *
5502 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005503 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005504
5505/* Returns a struct hlua_txn if the stack entry "ud" is
5506 * a class stream, otherwise it throws an error.
5507 */
5508__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005509{
Christopher Fauleta2097962019-07-15 16:25:33 +02005510 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
5511}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005512
Christopher Fauleta2097962019-07-15 16:25:33 +02005513/* This function creates and push in the stack a HTTP object
5514 * according with a current TXN.
5515 */
5516static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5517{
5518 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005519
Christopher Fauleta2097962019-07-15 16:25:33 +02005520 /* Check stack size. */
5521 if (!lua_checkstack(L, 3))
5522 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005523
Christopher Fauleta2097962019-07-15 16:25:33 +02005524 /* Create the object: obj[0] = userdata.
5525 * Note that the base of the Converters object is the
5526 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005527 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005528 lua_newtable(L);
5529 htxn = lua_newuserdata(L, sizeof(*htxn));
5530 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005531
5532 htxn->s = txn->s;
5533 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005534 htxn->dir = txn->dir;
5535 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005536
5537 /* Pop a class stream metatable and affect it to the table. */
5538 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5539 lua_setmetatable(L, -2);
5540
5541 return 1;
5542}
5543
Christopher Fauletdf97ac42020-02-26 16:57:19 +01005544/* This function creates and returns an array containing the status-line
5545 * elements. This function does not fails.
5546 */
5547__LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl)
5548{
5549 /* Create the table. */
5550 lua_newtable(L);
5551
5552 if (sl->flags & HTX_SL_F_IS_RESP) {
5553 lua_pushstring(L, "version");
5554 lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
5555 lua_settable(L, -3);
5556 lua_pushstring(L, "code");
5557 lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
5558 lua_settable(L, -3);
5559 lua_pushstring(L, "reason");
5560 lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
5561 lua_settable(L, -3);
5562 }
5563 else {
5564 lua_pushstring(L, "method");
5565 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
5566 lua_settable(L, -3);
5567 lua_pushstring(L, "uri");
5568 lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
5569 lua_settable(L, -3);
5570 lua_pushstring(L, "version");
5571 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
5572 lua_settable(L, -3);
5573 }
5574 return 1;
5575}
5576
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005577/* This function creates ans returns an array of HTTP headers.
5578 * This function does not fails. It is used as wrapper with the
5579 * 2 following functions.
5580 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005581__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005582{
Christopher Fauleta2097962019-07-15 16:25:33 +02005583 struct htx *htx;
5584 int32_t pos;
5585
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005586 /* Create the table. */
5587 lua_newtable(L);
5588
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005589
Christopher Fauleta2097962019-07-15 16:25:33 +02005590 htx = htxbuf(&msg->chn->buf);
5591 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5592 struct htx_blk *blk = htx_get_blk(htx, pos);
5593 enum htx_blk_type type = htx_get_blk_type(blk);
5594 struct ist n, v;
5595 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005596
Christopher Fauleta2097962019-07-15 16:25:33 +02005597 if (type == HTX_BLK_HDR) {
5598 n = htx_get_blk_name(htx,blk);
5599 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005600 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005601 else if (type == HTX_BLK_EOH)
5602 break;
5603 else
5604 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005605
Christopher Fauleta2097962019-07-15 16:25:33 +02005606 /* Check for existing entry:
5607 * assume that the table is on the top of the stack, and
5608 * push the key in the stack, the function lua_gettable()
5609 * perform the lookup.
5610 */
5611 lua_pushlstring(L, n.ptr, n.len);
5612 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005613
Christopher Fauleta2097962019-07-15 16:25:33 +02005614 switch (lua_type(L, -1)) {
5615 case LUA_TNIL:
5616 /* Table not found, create it. */
5617 lua_pop(L, 1); /* remove the nil value. */
5618 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5619 lua_newtable(L); /* create and push empty table. */
5620 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5621 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5622 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005623 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005624
Christopher Fauleta2097962019-07-15 16:25:33 +02005625 case LUA_TTABLE:
5626 /* Entry found: push the value in the table. */
5627 len = lua_rawlen(L, -1);
5628 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5629 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5630 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5631 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005632
Christopher Fauleta2097962019-07-15 16:25:33 +02005633 default:
5634 /* Other cases are errors. */
5635 hlua_pusherror(L, "internal error during the parsing of headers.");
5636 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005637 }
5638 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005639 return 1;
5640}
5641
5642__LJMP static int hlua_http_req_get_headers(lua_State *L)
5643{
5644 struct hlua_txn *htxn;
5645
5646 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5647 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5648
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005649 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005650 WILL_LJMP(lua_error(L));
5651
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005652 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005653}
5654
5655__LJMP static int hlua_http_res_get_headers(lua_State *L)
5656{
5657 struct hlua_txn *htxn;
5658
5659 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5660 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5661
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005662 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005663 WILL_LJMP(lua_error(L));
5664
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005665 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005666}
5667
5668/* This function replace full header, or just a value in
5669 * the request or in the response. It is a wrapper fir the
5670 * 4 following functions.
5671 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005672__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005673{
5674 size_t name_len;
5675 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5676 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5677 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005678 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005679 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005680
Dragan Dosen26743032019-04-30 15:54:36 +02005681 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005682 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5683
Christopher Fauleta2097962019-07-15 16:25:33 +02005684 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005685 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005686 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005687 return 0;
5688}
5689
5690__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5691{
5692 struct hlua_txn *htxn;
5693
5694 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5695 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5696
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005697 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005698 WILL_LJMP(lua_error(L));
5699
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005700 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005701}
5702
5703__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5704{
5705 struct hlua_txn *htxn;
5706
5707 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5708 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5709
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005710 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005711 WILL_LJMP(lua_error(L));
5712
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005713 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005714}
5715
5716__LJMP static int hlua_http_req_rep_val(lua_State *L)
5717{
5718 struct hlua_txn *htxn;
5719
5720 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5721 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5722
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005723 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005724 WILL_LJMP(lua_error(L));
5725
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005726 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005727}
5728
5729__LJMP static int hlua_http_res_rep_val(lua_State *L)
5730{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005731 struct hlua_txn *htxn;
5732
5733 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5734 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5735
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005736 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005737 WILL_LJMP(lua_error(L));
5738
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005739 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005740}
5741
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005742/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005743 * It is a wrapper for the 2 following functions.
5744 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005745__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005746{
5747 size_t len;
5748 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005749 struct htx *htx = htxbuf(&msg->chn->buf);
5750 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005751
Christopher Fauleta2097962019-07-15 16:25:33 +02005752 ctx.blk = NULL;
5753 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5754 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005755 return 0;
5756}
5757
5758__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5759{
5760 struct hlua_txn *htxn;
5761
5762 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5763 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5764
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005765 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005766 WILL_LJMP(lua_error(L));
5767
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005768 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005769}
5770
5771__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5772{
5773 struct hlua_txn *htxn;
5774
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005775 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005776 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5777
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005778 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005779 WILL_LJMP(lua_error(L));
5780
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005781 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005782}
5783
5784/* This function adds an header. It is a wrapper used by
5785 * the 2 following functions.
5786 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005787__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005788{
5789 size_t name_len;
5790 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5791 size_t value_len;
5792 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005793 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005794
Christopher Fauleta2097962019-07-15 16:25:33 +02005795 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5796 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005797 return 0;
5798}
5799
Christopher Fauletdf97ac42020-02-26 16:57:19 +01005800__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5801{
5802 struct hlua_txn *htxn;
5803
5804 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5805 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5806
5807 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5808 WILL_LJMP(lua_error(L));
5809
5810 return hlua_http_add_hdr(L, &htxn->s->txn->req);
5811}
5812
5813__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5814{
5815 struct hlua_txn *htxn;
5816
5817 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5818 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5819
5820 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
5821 WILL_LJMP(lua_error(L));
5822
5823 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
5824}
5825
5826static int hlua_http_req_set_hdr(lua_State *L)
5827{
5828 struct hlua_txn *htxn;
5829
5830 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5831 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5832
5833 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5834 WILL_LJMP(lua_error(L));
5835
5836 hlua_http_del_hdr(L, &htxn->s->txn->req);
5837 return hlua_http_add_hdr(L, &htxn->s->txn->req);
5838}
5839
5840static int hlua_http_res_set_hdr(lua_State *L)
5841{
5842 struct hlua_txn *htxn;
5843
5844 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5845 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5846
5847 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
5848 WILL_LJMP(lua_error(L));
5849
5850 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5851 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
5852}
5853
5854/* This function set the method. */
5855static int hlua_http_req_set_meth(lua_State *L)
5856{
5857 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5858 size_t name_len;
5859 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5860
5861 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5862 WILL_LJMP(lua_error(L));
5863
5864 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
5865 return 1;
5866}
5867
5868/* This function set the method. */
5869static int hlua_http_req_set_path(lua_State *L)
5870{
5871 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5872 size_t name_len;
5873 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5874
5875 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5876 WILL_LJMP(lua_error(L));
5877
5878 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
5879 return 1;
5880}
5881
5882/* This function set the query-string. */
5883static int hlua_http_req_set_query(lua_State *L)
5884{
5885 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5886 size_t name_len;
5887 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5888
5889 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5890 WILL_LJMP(lua_error(L));
5891
5892 /* Check length. */
5893 if (name_len > trash.size - 1) {
5894 lua_pushboolean(L, 0);
5895 return 1;
5896 }
5897
5898 /* Add the mark question as prefix. */
5899 chunk_reset(&trash);
5900 trash.area[trash.data++] = '?';
5901 memcpy(trash.area + trash.data, name, name_len);
5902 trash.data += name_len;
5903
5904 lua_pushboolean(L,
5905 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
5906 return 1;
5907}
5908
5909/* This function set the uri. */
5910static int hlua_http_req_set_uri(lua_State *L)
5911{
5912 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5913 size_t name_len;
5914 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5915
5916 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5917 WILL_LJMP(lua_error(L));
5918
5919 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
5920 return 1;
5921}
5922
5923/* This function set the response code & optionally reason. */
5924static int hlua_http_res_set_status(lua_State *L)
5925{
5926 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5927 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
5928 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5929 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
5930
5931 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
5932 WILL_LJMP(lua_error(L));
5933
5934 http_res_set_status(code, reason, htxn->s);
5935 return 0;
5936}
5937
5938/*
5939 *
5940 *
5941 * Class HTTPMessage
5942 *
5943 *
5944 */
5945
5946/* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage,
5947 * otherwise it throws an error.
5948 */
5949__LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud)
5950{
5951 return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref));
5952}
5953
5954/* Creates and pushes on the stack a HTTP object according with a current TXN.
5955 */
Christopher Faulet78c35472020-02-26 17:14:08 +01005956static int hlua_http_msg_new(lua_State *L, struct http_msg *msg)
Christopher Fauletdf97ac42020-02-26 16:57:19 +01005957{
5958 /* Check stack size. */
5959 if (!lua_checkstack(L, 3))
5960 return 0;
5961
5962 lua_newtable(L);
5963 lua_pushlightuserdata(L, msg);
5964 lua_rawseti(L, -2, 0);
5965
5966 /* Create the "channel" field that contains the request channel object. */
5967 lua_pushstring(L, "channel");
5968 if (!hlua_channel_new(L, msg->chn))
5969 return 0;
5970 lua_rawset(L, -3);
5971
5972 /* Pop a class stream metatable and affect it to the table. */
5973 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref);
5974 lua_setmetatable(L, -2);
5975
5976 return 1;
5977}
5978
5979/* Helper function returning a filter attached to the HTTP message at the
5980 * position <ud> in the stack, filling the current offset and length of the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05005981 * filter. If no filter is attached, NULL is returned and <offset> and <len> are
Christopher Fauletdf97ac42020-02-26 16:57:19 +01005982 * filled with output and input length respectively.
5983 */
5984static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len)
5985{
5986 struct channel *chn = msg->chn;
5987 struct htx *htx = htxbuf(&chn->buf);
5988 struct filter *filter = NULL;
5989
5990 *offset = co_data(msg->chn);
5991 *len = htx->data - co_data(msg->chn);
5992
5993 if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) {
5994 filter = lua_touserdata (L, -1);
5995 if (msg->msg_state >= HTTP_MSG_DATA) {
5996 struct hlua_flt_ctx *flt_ctx = filter->ctx;
5997
5998 *offset = flt_ctx->cur_off[CHN_IDX(chn)];
5999 *len = flt_ctx->cur_len[CHN_IDX(chn)];
6000 }
6001 }
6002
6003 lua_pop(L, 1);
6004 return filter;
6005}
6006
6007/* Returns true if the channel attached to the HTTP message is the response
6008 * channel.
6009 */
6010__LJMP static int hlua_http_msg_is_resp(lua_State *L)
6011{
6012 struct http_msg *msg;
6013
6014 MAY_LJMP(check_args(L, 1, "is_resp"));
6015 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6016
6017 lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP));
6018 return 1;
6019}
6020
6021/* Returns an array containing the elements status-line of the HTTP message. It relies
6022 * on hlua_http_get_stline().
6023 */
6024__LJMP static int hlua_http_msg_get_stline(lua_State *L)
6025{
6026 struct http_msg *msg;
6027 struct htx *htx;
6028 struct htx_sl *sl;
6029
6030 MAY_LJMP(check_args(L, 1, "get_stline"));
6031 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6032
6033 if (msg->msg_state > HTTP_MSG_BODY)
6034 WILL_LJMP(lua_error(L));
6035
6036 htx = htxbuf(&msg->chn->buf);
6037 sl = http_get_stline(htx);
6038 if (!sl)
6039 return 0;
6040 return hlua_http_get_stline(L, sl);
6041}
6042
6043/* Returns an array containing all headers of the HTTP message. it relies on
6044 * hlua_http_get_headers().
6045 */
6046__LJMP static int hlua_http_msg_get_headers(lua_State *L)
6047{
6048 struct http_msg *msg;
6049
6050 MAY_LJMP(check_args(L, 1, "get_headers"));
6051 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6052
6053 if (msg->msg_state > HTTP_MSG_BODY)
6054 WILL_LJMP(lua_error(L));
6055
6056 return hlua_http_get_headers(L, msg);
6057}
6058
6059/* Deletes all occurrences of an header in the HTTP message matching on its
6060 * name. It relies on hlua_http_del_hdr().
6061 */
6062__LJMP static int hlua_http_msg_del_hdr(lua_State *L)
6063{
6064 struct http_msg *msg;
6065
6066 MAY_LJMP(check_args(L, 2, "del_header"));
6067 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6068
6069 if (msg->msg_state > HTTP_MSG_BODY)
6070 WILL_LJMP(lua_error(L));
6071
6072 return hlua_http_del_hdr(L, msg);
6073}
6074
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006075/* Matches the full value line of all occurrences of an header in the HTTP
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006076 * message given its name against a regex and replaces it if it matches. It
6077 * relies on hlua_http_rep_hdr().
6078 */
6079__LJMP static int hlua_http_msg_rep_hdr(lua_State *L)
6080{
6081 struct http_msg *msg;
6082
6083 MAY_LJMP(check_args(L, 4, "rep_header"));
6084 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6085
6086 if (msg->msg_state > HTTP_MSG_BODY)
6087 WILL_LJMP(lua_error(L));
6088
6089 return hlua_http_rep_hdr(L, msg, 1);
6090}
6091
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05006092/* Matches all comma-separated values of all occurrences of an header in the HTTP
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006093 * message given its name against a regex and replaces it if it matches. It
6094 * relies on hlua_http_rep_hdr().
6095 */
6096__LJMP static int hlua_http_msg_rep_val(lua_State *L)
6097{
6098 struct http_msg *msg;
6099
6100 MAY_LJMP(check_args(L, 4, "rep_value"));
6101 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6102
6103 if (msg->msg_state > HTTP_MSG_BODY)
6104 WILL_LJMP(lua_error(L));
6105
6106 return hlua_http_rep_hdr(L, msg, 0);
6107}
6108
6109/* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */
6110__LJMP static int hlua_http_msg_add_hdr(lua_State *L)
6111{
6112 struct http_msg *msg;
6113
6114 MAY_LJMP(check_args(L, 3, "add_header"));
6115 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6116
6117 if (msg->msg_state > HTTP_MSG_BODY)
6118 WILL_LJMP(lua_error(L));
6119
6120 return hlua_http_add_hdr(L, msg);
6121}
6122
6123/* Add an header in the HTTP message removing existing headers with the same
6124 * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr().
6125 */
6126__LJMP static int hlua_http_msg_set_hdr(lua_State *L)
6127{
6128 struct http_msg *msg;
6129
6130 MAY_LJMP(check_args(L, 3, "set_header"));
6131 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6132
6133 if (msg->msg_state > HTTP_MSG_BODY)
6134 WILL_LJMP(lua_error(L));
6135
6136 hlua_http_del_hdr(L, msg);
6137 return hlua_http_add_hdr(L, msg);
6138}
6139
6140/* Rewrites the request method. It relies on http_req_replace_stline(). */
6141__LJMP static int hlua_http_msg_set_meth(lua_State *L)
6142{
6143 struct stream *s;
6144 struct http_msg *msg;
6145 const char *name;
6146 size_t name_len;
6147
6148 MAY_LJMP(check_args(L, 2, "set_method"));
6149 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6150 name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
6151
6152 if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6153 WILL_LJMP(lua_error(L));
6154
6155 s = chn_strm(msg->chn);
6156 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1);
6157 return 1;
6158}
6159
6160/* Rewrites the request path. It relies on http_req_replace_stline(). */
6161__LJMP static int hlua_http_msg_set_path(lua_State *L)
6162{
6163 struct stream *s;
6164 struct http_msg *msg;
6165 const char *name;
6166 size_t name_len;
6167
6168 MAY_LJMP(check_args(L, 2, "set_path"));
6169 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6170 name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
6171
6172 if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6173 WILL_LJMP(lua_error(L));
6174
6175 s = chn_strm(msg->chn);
6176 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1);
6177 return 1;
6178}
6179
6180/* Rewrites the request query-string. It relies on http_req_replace_stline(). */
6181__LJMP static int hlua_http_msg_set_query(lua_State *L)
6182{
6183 struct stream *s;
6184 struct http_msg *msg;
6185 const char *name;
6186 size_t name_len;
6187
6188 MAY_LJMP(check_args(L, 2, "set_query"));
6189 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6190 name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
6191
6192 if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6193 WILL_LJMP(lua_error(L));
6194
6195 /* Check length. */
6196 if (name_len > trash.size - 1) {
6197 lua_pushboolean(L, 0);
6198 return 1;
6199 }
6200
6201 /* Add the mark question as prefix. */
6202 chunk_reset(&trash);
6203 trash.area[trash.data++] = '?';
6204 memcpy(trash.area + trash.data, name, name_len);
6205 trash.data += name_len;
6206
6207 s = chn_strm(msg->chn);
6208 lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1);
6209 return 1;
6210}
6211
6212/* Rewrites the request URI. It relies on http_req_replace_stline(). */
6213__LJMP static int hlua_http_msg_set_uri(lua_State *L)
6214{
6215 struct stream *s;
6216 struct http_msg *msg;
6217 const char *name;
6218 size_t name_len;
6219
6220 MAY_LJMP(check_args(L, 2, "set_uri"));
6221 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6222 name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
6223
6224 if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6225 WILL_LJMP(lua_error(L));
6226
6227 s = chn_strm(msg->chn);
6228 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1);
6229 return 1;
6230}
6231
6232/* Rewrites the response status code. It relies on http_res_set_status(). */
6233__LJMP static int hlua_http_msg_set_status(lua_State *L)
6234{
6235 struct http_msg *msg;
6236 unsigned int code;
6237 const char *reason;
6238 size_t reason_len;
6239
6240 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6241 code = MAY_LJMP(luaL_checkinteger(L, 2));
6242 reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len));
6243
6244 if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6245 WILL_LJMP(lua_error(L));
6246
6247 lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1);
6248 return 1;
6249}
6250
6251/* Returns true if the HTTP message is full. */
6252__LJMP static int hlua_http_msg_is_full(lua_State *L)
6253{
6254 struct http_msg *msg;
6255
6256 MAY_LJMP(check_args(L, 1, "is_full"));
6257 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6258 lua_pushboolean(L, channel_full(msg->chn, 0));
6259 return 1;
6260}
6261
6262/* Returns true if the HTTP message may still receive data. */
6263__LJMP static int hlua_http_msg_may_recv(lua_State *L)
6264{
6265 struct http_msg *msg;
6266 struct htx *htx;
6267
6268 MAY_LJMP(check_args(L, 1, "may_recv"));
6269 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6270 htx = htxbuf(&msg->chn->buf);
6271 lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn)));
6272 return 1;
6273}
6274
6275/* Returns true if the HTTP message EOM was received */
6276__LJMP static int hlua_http_msg_is_eom(lua_State *L)
6277{
6278 struct http_msg *msg;
6279 struct htx *htx;
6280
6281 MAY_LJMP(check_args(L, 1, "may_recv"));
6282 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6283 htx = htxbuf(&msg->chn->buf);
6284 lua_pushboolean(L, !htx_expect_more(htx));
6285 return 1;
6286}
6287
6288/* Returns the number of bytes available in the input side of the HTTP
6289 * message. This function never fails.
6290 */
6291__LJMP static int hlua_http_msg_get_in_len(lua_State *L)
6292{
6293 struct http_msg *msg;
Christopher Fauleteae8afa2020-02-26 17:15:48 +01006294 size_t output, input;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006295
6296 MAY_LJMP(check_args(L, 1, "input"));
6297 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Christopher Fauleteae8afa2020-02-26 17:15:48 +01006298 hlua_http_msg_filter(L, 1, msg, &output, &input);
6299 lua_pushinteger(L, input);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006300 return 1;
6301}
6302
6303/* Returns the number of bytes available in the output side of the HTTP
6304 * message. This function never fails.
6305 */
6306__LJMP static int hlua_http_msg_get_out_len(lua_State *L)
6307{
6308 struct http_msg *msg;
Christopher Fauleteae8afa2020-02-26 17:15:48 +01006309 size_t output, input;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006310
6311 MAY_LJMP(check_args(L, 1, "output"));
6312 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Christopher Fauleteae8afa2020-02-26 17:15:48 +01006313 hlua_http_msg_filter(L, 1, msg, &output, &input);
6314 lua_pushinteger(L, output);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006315 return 1;
6316}
6317
6318/* Copies at most <len> bytes of DATA blocks from the HTTP message <msg>
6319 * starting at the offset <offset> and put it in a string LUA variables. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006320 * returns the built string length. It stops on the first non-DATA HTX
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006321 * block. This function is called during the payload filtering, so the headers
6322 * are already scheduled for output (from the filter point of view).
6323 */
6324static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len)
6325{
6326 struct htx *htx = htxbuf(&msg->chn->buf);
6327 struct htx_blk *blk;
6328 struct htx_ret htxret;
6329 luaL_Buffer b;
6330 int ret = 0;
6331
6332 luaL_buffinit(L, &b);
6333 htxret = htx_find_offset(htx, offset);
6334 for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) {
6335 enum htx_blk_type type = htx_get_blk_type(blk);
6336 struct ist v;
6337
6338 switch (type) {
6339 case HTX_BLK_UNUSED:
6340 break;
6341
6342 case HTX_BLK_DATA:
6343 v = htx_get_blk_value(htx, blk);
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02006344 v = istadv(v, offset);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01006345 v = isttrim(v, len);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006346
6347 luaL_addlstring(&b, v.ptr, v.len);
6348 ret += v.len;
6349 break;
6350
6351 default:
vishnu0af4bd72021-10-24 06:46:24 +05306352 if (!ret)
6353 goto no_data;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006354 goto end;
6355 }
6356 offset = 0;
6357 }
6358
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006359end:
vishnu0af4bd72021-10-24 06:46:24 +05306360 if (!ret && (htx->flags & HTX_FL_EOM))
6361 goto no_data;
6362 luaL_pushresult(&b);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006363 return ret;
vishnu0af4bd72021-10-24 06:46:24 +05306364
6365 no_data:
6366 /* Remove the empty string and push nil on the stack */
6367 lua_pop(L, 1);
6368 lua_pushnil(L);
6369 return 0;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006370}
6371
6372/* Copies the string <str> to the HTTP message <msg> at the offset
6373 * <offset>. This function returns -1 if data cannot be copied. Otherwise, it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006374 * returns the amount of data written. This function is responsible to update
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006375 * the filter context.
6376 */
6377static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset)
6378{
6379 struct htx *htx = htx_from_buf(&msg->chn->buf);
6380 struct htx_ret htxret;
6381 int /*max, */ret = 0;
6382
6383 /* Nothing to do, just return */
6384 if (unlikely(istlen(str) == 0))
6385 goto end;
6386
6387 if (istlen(str) > htx_free_data_space(htx)) {
6388 ret = -1;
6389 goto end;
6390 }
6391
6392 htxret = htx_find_offset(htx, offset);
6393 if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) {
6394 if (!htx_add_last_data(htx, str))
6395 goto end;
6396 }
6397 else {
6398 struct ist v = htx_get_blk_value(htx, htxret.blk);
6399 v.ptr += htxret.ret;
6400 v.len = 0;
6401 if (!htx_replace_blk_value(htx, htxret.blk, v, str))
6402 goto end;
6403 }
6404 ret = str.len;
6405 if (ret) {
6406 struct hlua_flt_ctx *flt_ctx = filter->ctx;
6407 flt_update_offsets(filter, msg->chn, ret);
6408 flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret;
6409 }
6410
6411 end:
6412 htx_to_buf(htx, &msg->chn->buf);
6413 return ret;
6414}
6415
6416/* Helper function removing at most <len> bytes of DATA blocks at the absolute
6417 * position <offset>. It stops on the first non-DATA HTX block. This function is
6418 * called during the payload filtering, so the headers are already scheduled for
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006419 * output (from the filter point of view). This function is responsible to
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006420 * update the filter context.
6421 */
6422static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len)
6423{
6424 struct hlua_flt_ctx *flt_ctx = filter->ctx;
6425 struct htx *htx = htx_from_buf(&msg->chn->buf);
6426 struct htx_blk *blk;
6427 struct htx_ret htxret;
6428 size_t ret = 0;
6429
6430 /* Be sure <len> is always the amount of DATA to remove */
6431 if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) {
6432 htx_truncate(htx, offset);
6433 ret = len;
6434 goto end;
6435 }
6436
6437 htxret = htx_find_offset(htx, offset);
6438 blk = htxret.blk;
6439 if (htxret.ret) {
6440 struct ist v;
6441
6442 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
6443 goto end;
6444 v = htx_get_blk_value(htx, blk);
6445 v.ptr += htxret.ret;
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01006446 v = isttrim(v, len);
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02006447 blk = htx_replace_blk_value(htx, blk, v, IST_NULL);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006448 len -= v.len;
6449 ret += v.len;
6450 }
6451
6452
6453 while (blk && len) {
6454 enum htx_blk_type type = htx_get_blk_type(blk);
6455 uint32_t sz = htx_get_blksz(blk);
6456
6457 switch (type) {
6458 case HTX_BLK_UNUSED:
6459 break;
6460
6461 case HTX_BLK_DATA:
6462 if (len < sz) {
6463 htx_cut_data_blk(htx, blk, len);
6464 ret += len;
6465 goto end;
6466 }
6467 break;
6468
6469 default:
6470 goto end;
6471 }
6472
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006473 /* Remove all the data block */
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006474 len -= sz;
6475 ret += sz;
6476 blk = htx_remove_blk(htx, blk);
6477 }
6478
6479end:
6480 flt_update_offsets(filter, msg->chn, -ret);
6481 flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret;
6482 /* WARNING: we don't call htx_to_buf() on purpose, because we don't want
6483 * to loose the EOM flag if the message is empty.
6484 */
6485}
6486
6487/* Copies input data found in an HTTP message. Unlike the channel function used
6488 * to duplicate raw data, this one can only be called inside a filter, from
6489 * http_payload callback. So it cannot yield. An exception is returned if it is
6490 * called from another callback. If nothing was copied, a nil value is pushed on
6491 * the stack.
6492 */
6493__LJMP static int hlua_http_msg_get_body(lua_State *L)
6494{
6495 struct http_msg *msg;
6496 struct filter *filter;
6497 size_t output, input;
6498 int offset, len;
6499
6500 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
6501 WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments"));
6502 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6503
6504 if (msg->msg_state < HTTP_MSG_DATA)
6505 WILL_LJMP(lua_error(L));
6506
6507 filter = hlua_http_msg_filter(L, 1, msg, &output, &input);
6508 if (!filter || !hlua_filter_from_payload(filter))
6509 WILL_LJMP(lua_error(L));
6510
6511 if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) {
6512 lua_pushnil(L);
6513 return 1;
6514 }
6515
6516 offset = output;
6517 if (lua_gettop(L) > 1) {
6518 offset = MAY_LJMP(luaL_checkinteger(L, 2));
6519 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02006520 offset = MAX(0, (int)input + offset);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006521 offset += output;
6522 if (offset < output || offset > input + output) {
6523 lua_pushfstring(L, "offset out of range.");
6524 WILL_LJMP(lua_error(L));
6525 }
6526 }
6527 len = output + input - offset;
6528 if (lua_gettop(L) == 3) {
6529 len = MAY_LJMP(luaL_checkinteger(L, 3));
6530 if (!len)
6531 goto dup;
6532 if (len == -1)
6533 len = global.tune.bufsize;
6534 if (len < 0) {
6535 lua_pushfstring(L, "length out of range.");
6536 WILL_LJMP(lua_error(L));
6537 }
6538 }
6539
6540 dup:
6541 _hlua_http_msg_dup(msg, L, offset, len);
6542 return 1;
6543}
6544
6545/* Appends a string to the HTTP message, after all existing DATA blocks but
6546 * before the trailers, if any. It returns the amount of data written or -1 if
6547 * nothing was copied. Unlike the channel function used to append data, this one
6548 * can only be called inside a filter, from http_payload callback. So it cannot
6549 * yield. An exception is returned if it is called from another callback.
6550 */
6551__LJMP static int hlua_http_msg_append(lua_State *L)
6552{
6553 struct http_msg *msg;
6554 struct filter *filter;
6555 const char *str;
6556 size_t offset, len, sz;
6557 int ret;
6558
6559 MAY_LJMP(check_args(L, 2, "append"));
6560 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6561
6562 if (msg->msg_state < HTTP_MSG_DATA)
6563 WILL_LJMP(lua_error(L));
6564
6565 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6566 filter = hlua_http_msg_filter(L, 1, msg, &offset, &len);
6567 if (!filter || !hlua_filter_from_payload(filter))
6568 WILL_LJMP(lua_error(L));
6569
6570 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len);
6571 lua_pushinteger(L, ret);
6572 return 1;
6573}
6574
6575/* Prepends a string to the HTTP message, before all existing DATA blocks. It
6576 * returns the amount of data written or -1 if nothing was copied. Unlike the
6577 * channel function used to prepend data, this one can only be called inside a
6578 * filter, from http_payload callback. So it cannot yield. An exception is
6579 * returned if it is called from another callback.
6580 */
6581__LJMP static int hlua_http_msg_prepend(lua_State *L)
6582{
6583 struct http_msg *msg;
6584 struct filter *filter;
6585 const char *str;
6586 size_t offset, len, sz;
6587 int ret;
6588
6589 MAY_LJMP(check_args(L, 2, "prepend"));
6590 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006591
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006592 if (msg->msg_state < HTTP_MSG_DATA)
6593 WILL_LJMP(lua_error(L));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006594
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006595 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6596 filter = hlua_http_msg_filter(L, 1, msg, &offset, &len);
6597 if (!filter || !hlua_filter_from_payload(filter))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006598 WILL_LJMP(lua_error(L));
6599
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006600 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset);
6601 lua_pushinteger(L, ret);
6602 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006603}
6604
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006605/* Inserts a string to the HTTP message at a given offset. By default the string
6606 * is appended at the end of DATA blocks. It returns the amount of data written
6607 * or -1 if nothing was copied. Unlike the channel function used to insert data,
6608 * this one can only be called inside a filter, from http_payload callback. So
6609 * it cannot yield. An exception is returned if it is called from another
6610 * callback.
6611 */
6612__LJMP static int hlua_http_msg_insert_data(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006613{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006614 struct http_msg *msg;
6615 struct filter *filter;
6616 const char *str;
6617 size_t input, output, sz;
6618 int offset;
6619 int ret;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006620
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006621 if (lua_gettop(L) < 2 || lua_gettop(L) > 3)
6622 WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments"));
6623 MAY_LJMP(check_args(L, 2, "insert"));
6624 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006625
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006626 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006627 WILL_LJMP(lua_error(L));
6628
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006629 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6630 filter = hlua_http_msg_filter(L, 1, msg, &input, &output);
6631 if (!filter || !hlua_filter_from_payload(filter))
6632 WILL_LJMP(lua_error(L));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006633
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006634 offset = input + output;
6635 if (lua_gettop(L) > 2) {
6636 offset = MAY_LJMP(luaL_checkinteger(L, 3));
6637 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02006638 offset = MAX(0, (int)input + offset);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006639 offset += output;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006640 if (offset < output || offset > output + input) {
6641 lua_pushfstring(L, "offset out of range.");
6642 WILL_LJMP(lua_error(L));
6643 }
6644 }
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006645
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006646 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset);
6647 lua_pushinteger(L, ret);
6648 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006649}
6650
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006651/* Removes a given amount of data from the HTTP message at a given offset. By
6652 * default all DATA blocks are removed. It returns the amount of data
6653 * removed. Unlike the channel function used to remove data, this one can only
6654 * be called inside a filter, from http_payload callback. So it cannot yield. An
6655 * exception is returned if it is called from another callback.
6656 */
6657__LJMP static int hlua_http_msg_del_data(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006658{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006659 struct http_msg *msg;
6660 struct filter *filter;
6661 size_t input, output;
6662 int offset, len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006663
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006664 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
6665 WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments"));
6666 MAY_LJMP(check_args(L, 2, "insert"));
6667 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006668
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006669 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006670 WILL_LJMP(lua_error(L));
6671
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006672 filter = hlua_http_msg_filter(L, 1, msg, &input, &output);
6673 if (!filter || !hlua_filter_from_payload(filter))
6674 WILL_LJMP(lua_error(L));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006675
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006676 offset = input + output;
6677 if (lua_gettop(L) > 2) {
6678 offset = MAY_LJMP(luaL_checkinteger(L, 3));
6679 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02006680 offset = MAX(0, (int)input + offset);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006681 offset += output;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006682 if (offset < output || offset > output + input) {
6683 lua_pushfstring(L, "offset out of range.");
6684 WILL_LJMP(lua_error(L));
6685 }
6686 }
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006687
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006688 len = output + input - offset;
6689 if (lua_gettop(L) == 4) {
6690 len = MAY_LJMP(luaL_checkinteger(L, 4));
6691 if (!len)
6692 goto end;
6693 if (len == -1)
6694 len = output + input - offset;
6695 if (len < 0 || offset + len > output + input) {
6696 lua_pushfstring(L, "length out of range.");
6697 WILL_LJMP(lua_error(L));
6698 }
6699 }
6700
6701 _hlua_http_msg_delete(msg, filter, offset, len);
6702
6703 end:
6704 lua_pushinteger(L, len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006705 return 1;
6706}
6707
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006708/* Replaces a given amount of data at the given offset by a string. By default,
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006709 * all remaining data are removed, accordingly to the filter context. It returns
6710 * the amount of data written or -1 if nothing was copied. Unlike the channel
6711 * function used to replace data, this one can only be called inside a filter,
6712 * from http_payload callback. So it cannot yield. An exception is returned if
6713 * it is called from another callback.
6714 */
6715__LJMP static int hlua_http_msg_set_data(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006716{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006717 struct http_msg *msg;
6718 struct filter *filter;
6719 struct htx *htx;
6720 const char *str;
6721 size_t input, output, sz;
6722 int offset, len;
6723 int ret;
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02006724
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006725 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
6726 WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments"));
6727 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6728
6729 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006730 WILL_LJMP(lua_error(L));
6731
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006732 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6733 filter = hlua_http_msg_filter(L, 1, msg, &output, &input);
6734 if (!filter || !hlua_filter_from_payload(filter))
6735 WILL_LJMP(lua_error(L));
6736
6737 offset = output;
6738 if (lua_gettop(L) > 2) {
6739 offset = MAY_LJMP(luaL_checkinteger(L, 3));
6740 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02006741 offset = MAX(0, (int)input + offset);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006742 offset += output;
6743 if (offset < output || offset > input + output) {
6744 lua_pushfstring(L, "offset out of range.");
6745 WILL_LJMP(lua_error(L));
6746 }
6747 }
6748
6749 len = output + input - offset;
6750 if (lua_gettop(L) == 4) {
6751 len = MAY_LJMP(luaL_checkinteger(L, 4));
6752 if (!len)
6753 goto set;
6754 if (len == -1)
6755 len = output + input - offset;
6756 if (len < 0 || offset + len > output + input) {
6757 lua_pushfstring(L, "length out of range.");
6758 WILL_LJMP(lua_error(L));
6759 }
6760 }
6761
6762 set:
6763 /* Be sure we can copied the string once input data will be removed. */
6764 htx = htx_from_buf(&msg->chn->buf);
6765 if (sz > htx_free_data_space(htx) + len)
6766 lua_pushinteger(L, -1);
6767 else {
6768 _hlua_http_msg_delete(msg, filter, offset, len);
6769 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset);
6770 lua_pushinteger(L, ret);
6771 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006772 return 1;
6773}
6774
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006775/* Prepends data into an HTTP message and forward it, from the filter point of
6776 * view. It returns the amount of data written or -1 if nothing was sent. Unlike
6777 * the channel function used to send data, this one can only be called inside a
6778 * filter, from http_payload callback. So it cannot yield. An exception is
6779 * returned if it is called from another callback.
6780 */
6781__LJMP static int hlua_http_msg_send(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006782{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006783 struct http_msg *msg;
6784 struct filter *filter;
6785 struct htx *htx;
6786 const char *str;
6787 size_t offset, len, sz;
6788 int ret;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006789
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006790 MAY_LJMP(check_args(L, 2, "send"));
6791 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6792
6793 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006794 WILL_LJMP(lua_error(L));
6795
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006796 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6797 filter = hlua_http_msg_filter(L, 1, msg, &offset, &len);
6798 if (!filter || !hlua_filter_from_payload(filter))
6799 WILL_LJMP(lua_error(L));
6800
6801 /* Return an error if the channel's output is closed */
6802 if (unlikely(channel_output_closed(msg->chn))) {
6803 lua_pushinteger(L, -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006804 return 1;
6805 }
6806
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006807 htx = htx_from_buf(&msg->chn->buf);
6808 if (sz > htx_free_data_space(htx)) {
6809 lua_pushinteger(L, -1);
6810 return 1;
6811 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006812
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006813 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset);
6814 if (ret > 0) {
6815 struct hlua_flt_ctx *flt_ctx = filter->ctx;
6816
6817 FLT_OFF(filter, msg->chn) += ret;
6818 flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret;
6819 flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret;
6820 }
6821
6822 lua_pushinteger(L, ret);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006823 return 1;
6824}
6825
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006826/* Forwards a given amount of bytes. It return -1 if the channel's output is
6827 * closed. Otherwise, it returns the number of bytes forwarded. Unlike the
6828 * channel function used to forward data, this one can only be called inside a
6829 * filter, from http_payload callback. So it cannot yield. An exception is
6830 * returned if it is called from another callback. All other functions deal with
6831 * DATA block, this one not.
6832*/
6833__LJMP static int hlua_http_msg_forward(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006834{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006835 struct http_msg *msg;
6836 struct filter *filter;
6837 size_t offset, len;
6838 int fwd, ret = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006839
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006840 MAY_LJMP(check_args(L, 2, "forward"));
6841 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6842
6843 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006844 WILL_LJMP(lua_error(L));
6845
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006846 fwd = MAY_LJMP(luaL_checkinteger(L, 2));
6847 filter = hlua_http_msg_filter(L, 1, msg, &offset, &len);
6848 if (!filter || !hlua_filter_from_payload(filter))
6849 WILL_LJMP(lua_error(L));
6850
6851 /* Nothing to do, just return */
6852 if (!fwd)
6853 goto end;
6854
6855 /* Return an error if the channel's output is closed */
6856 if (unlikely(channel_output_closed(msg->chn))) {
6857 ret = -1;
6858 goto end;
6859 }
6860
6861 ret = fwd;
6862 if (ret > len)
6863 ret = len;
6864
6865 if (ret) {
6866 struct hlua_flt_ctx *flt_ctx = filter->ctx;
6867
6868 FLT_OFF(filter, msg->chn) += ret;
6869 flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret;
6870 flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret;
6871 }
6872
6873 end:
6874 lua_pushinteger(L, ret);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006875 return 1;
6876}
6877
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006878/* Set EOM flag on the HTX message.
6879 *
6880 * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't
6881 * really know how to do without this feature.
6882 */
6883__LJMP static int hlua_http_msg_set_eom(lua_State *L)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02006884{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006885 struct http_msg *msg;
6886 struct htx *htx;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02006887
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006888 MAY_LJMP(check_args(L, 1, "set_eom"));
6889 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6890 htx = htxbuf(&msg->chn->buf);
6891 htx->flags |= HTX_FL_EOM;
6892 return 0;
6893}
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006894
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006895/* Unset EOM flag on the HTX message.
6896 *
6897 * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't
6898 * really know how to do without this feature.
6899 */
6900__LJMP static int hlua_http_msg_unset_eom(lua_State *L)
6901{
6902 struct http_msg *msg;
6903 struct htx *htx;
6904
6905 MAY_LJMP(check_args(L, 1, "set_eom"));
6906 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6907 htx = htxbuf(&msg->chn->buf);
6908 htx->flags &= ~HTX_FL_EOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02006909 return 0;
6910}
6911
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006912/*
6913 *
6914 *
William Lallemand3956c4e2021-09-21 16:25:15 +02006915 * Class HTTPClient
6916 *
6917 *
6918 */
6919__LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud)
6920{
6921 return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref));
6922}
6923
William Lallemandf77f1de2021-09-28 19:10:38 +02006924
6925/* stops the httpclient and ask it to kill itself */
6926__LJMP static int hlua_httpclient_gc(lua_State *L)
6927{
6928 struct hlua_httpclient *hlua_hc;
6929
6930 MAY_LJMP(check_args(L, 1, "__gc"));
6931
6932 hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1));
6933
6934 httpclient_stop_and_destroy(hlua_hc->hc);
6935
6936 hlua_hc->hc = NULL;
6937
6938
6939 return 0;
6940}
6941
6942
William Lallemand3956c4e2021-09-21 16:25:15 +02006943__LJMP static int hlua_httpclient_new(lua_State *L)
6944{
6945 struct hlua_httpclient *hlua_hc;
6946 struct hlua *hlua;
6947
6948 /* Get hlua struct, or NULL if we execute from main lua state */
6949 hlua = hlua_gethlua(L);
6950 if (!hlua)
6951 return 0;
6952
6953 /* Check stack size. */
6954 if (!lua_checkstack(L, 3)) {
6955 hlua_pusherror(L, "httpclient: full stack");
6956 goto err;
6957 }
6958 /* Create the object: obj[0] = userdata. */
6959 lua_newtable(L);
6960 hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc)));
6961 lua_rawseti(L, -2, 0);
6962 memset(hlua_hc, 0, sizeof(*hlua_hc));
6963
6964 hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL);
6965 if (!hlua_hc->hc)
6966 goto err;
6967
6968 /* Pop a class stream metatable and affect it to the userdata. */
6969 lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref);
6970 lua_setmetatable(L, -2);
6971
6972 return 1;
6973
6974 err:
6975 WILL_LJMP(lua_error(L));
6976 return 0;
6977}
6978
6979
6980/*
6981 * Callback of the httpclient, this callback wakes the lua task up, once the
6982 * httpclient receives some data
6983 *
6984 */
6985
William Lallemandbd5739e2021-10-28 15:41:38 +02006986static void hlua_httpclient_cb(struct httpclient *hc)
William Lallemand3956c4e2021-09-21 16:25:15 +02006987{
6988 struct hlua *hlua = hc->caller;
6989
6990 if (!hlua || !hlua->task)
6991 return;
6992
6993 task_wakeup(hlua->task, TASK_WOKEN_MSG);
6994}
6995
6996/*
William Lallemandd7df73a2021-09-23 17:54:00 +02006997 * Fill the lua stack with headers from the httpclient response
6998 * This works the same way as the hlua_http_get_headers() function
6999 */
7000__LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc)
7001{
7002 struct http_hdr *hdr;
7003
7004 lua_newtable(L);
7005
William Lallemandef574b22021-10-05 16:19:31 +02007006 for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) {
William Lallemandd7df73a2021-09-23 17:54:00 +02007007 struct ist n, v;
7008 int len;
7009
7010 n = hdr->n;
7011 v = hdr->v;
7012
7013 /* Check for existing entry:
7014 * assume that the table is on the top of the stack, and
7015 * push the key in the stack, the function lua_gettable()
7016 * perform the lookup.
7017 */
7018
7019 lua_pushlstring(L, n.ptr, n.len);
7020 lua_gettable(L, -2);
7021
7022 switch (lua_type(L, -1)) {
7023 case LUA_TNIL:
7024 /* Table not found, create it. */
7025 lua_pop(L, 1); /* remove the nil value. */
7026 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
7027 lua_newtable(L); /* create and push empty table. */
7028 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
7029 lua_rawseti(L, -2, 0); /* index header value (pop it). */
7030 lua_rawset(L, -3); /* index new table with header name (pop the values). */
7031 break;
7032
7033 case LUA_TTABLE:
7034 /* Entry found: push the value in the table. */
7035 len = lua_rawlen(L, -1);
7036 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
7037 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
7038 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
7039 break;
7040
7041 default:
7042 /* Other cases are errors. */
7043 hlua_pusherror(L, "internal error during the parsing of headers.");
7044 WILL_LJMP(lua_error(L));
7045 }
7046 }
7047 return 1;
7048}
7049
7050/*
William Lallemand746e6f32021-10-06 10:57:44 +02007051 * Allocate and return an array of http_hdr ist extracted from the <headers> lua table
7052 *
7053 * Caller must free the result
7054 */
7055struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L)
7056{
7057 struct http_hdr hdrs[global.tune.max_http_hdr];
7058 struct http_hdr *result = NULL;
7059 uint32_t hdr_num = 0;
7060
7061 lua_pushnil(L);
7062 while (lua_next(L, -2) != 0) {
7063 struct ist name, value;
7064 const char *n, *v;
7065 size_t nlen, vlen;
7066
7067 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
7068 /* Skip element if the key is not a string or if the value is not a table */
7069 goto next_hdr;
7070 }
7071
7072 n = lua_tolstring(L, -2, &nlen);
7073 name = ist2(n, nlen);
7074
7075 /* Loop on header's values */
7076 lua_pushnil(L);
7077 while (lua_next(L, -2)) {
7078 if (!lua_isstring(L, -1)) {
7079 /* Skip the value if it is not a string */
7080 goto next_value;
7081 }
7082
7083 v = lua_tolstring(L, -1, &vlen);
7084 value = ist2(v, vlen);
7085 name = ist2(n, nlen);
7086
7087 hdrs[hdr_num].n = istdup(name);
7088 hdrs[hdr_num].v = istdup(value);
7089
7090 hdr_num++;
7091
7092 next_value:
7093 lua_pop(L, 1);
7094 }
7095
7096 next_hdr:
7097 lua_pop(L, 1);
7098
7099 }
7100
7101 if (hdr_num) {
7102 /* alloc and copy the headers in the httpclient struct */
Tim Duesterhus16cc16d2021-11-06 15:14:45 +01007103 result = calloc((hdr_num + 1), sizeof(*result));
William Lallemand746e6f32021-10-06 10:57:44 +02007104 if (!result)
7105 goto skip_headers;
7106 memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1));
7107
7108 result[hdr_num].n = IST_NULL;
7109 result[hdr_num].v = IST_NULL;
7110 }
7111
7112skip_headers:
William Lallemand746e6f32021-10-06 10:57:44 +02007113
7114 return result;
7115}
7116
7117
William Lallemandbd5739e2021-10-28 15:41:38 +02007118/*
7119 * For each yield, checks if there is some data in the httpclient and push them
7120 * in the lua buffer, once the httpclient finished its job, push the result on
7121 * the stack
7122 */
7123__LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx)
7124{
7125 struct buffer *tr;
7126 int res;
7127 struct hlua *hlua = hlua_gethlua(L);
7128 struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1);
7129
7130
7131 tr = get_trash_chunk();
7132
7133 res = httpclient_res_xfer(hlua_hc->hc, tr);
7134 luaL_addlstring(&hlua_hc->b, b_orig(tr), res);
7135
7136 if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) {
7137
7138 luaL_pushresult(&hlua_hc->b);
7139 lua_settable(L, -3);
7140
7141 lua_pushstring(L, "status");
7142 lua_pushinteger(L, hlua_hc->hc->res.status);
7143 lua_settable(L, -3);
7144
7145
7146 lua_pushstring(L, "reason");
7147 lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len);
7148 lua_settable(L, -3);
7149
7150 lua_pushstring(L, "headers");
7151 hlua_httpclient_get_headers(L, hlua_hc);
7152 lua_settable(L, -3);
7153
7154 return 1;
7155 }
7156
7157 if (httpclient_data(hlua_hc->hc))
7158 task_wakeup(hlua->task, TASK_WOKEN_MSG);
7159
7160 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0));
7161
7162 return 0;
7163}
7164
7165/*
7166 * Call this when trying to stream a body during a request
7167 */
7168__LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx)
7169{
7170 struct hlua *hlua;
7171 struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1);
7172 const char *body_str = NULL;
7173 int ret;
7174 int end = 0;
7175 size_t buf_len;
7176 size_t to_send = 0;
7177
7178 hlua = hlua_gethlua(L);
7179
7180 if (!hlua || !hlua->task)
7181 WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in "
7182 "'frontend', 'backend' or 'task'"));
7183
7184 ret = lua_getfield(L, -1, "body");
7185 if (ret != LUA_TSTRING)
7186 goto rcv;
7187
7188 body_str = lua_tolstring(L, -1, &buf_len);
7189 lua_pop(L, 1);
7190
Christopher Fauletfc591292022-01-12 14:46:03 +01007191 to_send = buf_len - hlua_hc->sent;
William Lallemandbd5739e2021-10-28 15:41:38 +02007192
7193 if ((hlua_hc->sent + to_send) >= buf_len)
7194 end = 1;
7195
7196 /* the end flag is always set since we are using the whole remaining size */
7197 hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end);
7198
7199 if (buf_len > hlua_hc->sent) {
7200 /* still need to process the buffer */
7201 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0));
7202 } else {
7203 goto rcv;
7204 /* we sent the whole request buffer we can recv */
7205 }
7206 return 0;
7207
7208rcv:
7209
7210 /* we return a "res" object */
7211 lua_newtable(L);
7212
William Lallemandbd5739e2021-10-28 15:41:38 +02007213 lua_pushstring(L, "body");
William Lallemandd1187eb2021-11-02 10:40:06 +01007214 luaL_buffinit(L, &hlua_hc->b);
William Lallemandbd5739e2021-10-28 15:41:38 +02007215
William Lallemand933fe392021-11-04 09:45:58 +01007216 task_wakeup(hlua->task, TASK_WOKEN_MSG);
William Lallemandbd5739e2021-10-28 15:41:38 +02007217 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0));
7218
7219 return 1;
7220}
William Lallemand746e6f32021-10-06 10:57:44 +02007221
William Lallemand3956c4e2021-09-21 16:25:15 +02007222/*
William Lallemanddc2cc902021-10-26 11:43:26 +02007223 * Send an HTTP request and wait for a response
William Lallemand3956c4e2021-09-21 16:25:15 +02007224 */
7225
William Lallemanddc2cc902021-10-26 11:43:26 +02007226__LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
William Lallemand3956c4e2021-09-21 16:25:15 +02007227{
7228 struct hlua_httpclient *hlua_hc;
William Lallemand746e6f32021-10-06 10:57:44 +02007229 struct http_hdr *hdrs = NULL;
7230 struct http_hdr *hdrs_i = NULL;
William Lallemand3956c4e2021-09-21 16:25:15 +02007231 struct hlua *hlua;
William Lallemand746e6f32021-10-06 10:57:44 +02007232 const char *url_str = NULL;
William Lallemanddec25c32021-10-25 19:48:37 +02007233 const char *body_str = NULL;
William Lallemandb4a4ef62022-02-23 14:18:16 +01007234 int timeout;
William Lallemandbd5739e2021-10-28 15:41:38 +02007235 size_t buf_len;
William Lallemand746e6f32021-10-06 10:57:44 +02007236 int ret;
William Lallemand3956c4e2021-09-21 16:25:15 +02007237
7238 hlua = hlua_gethlua(L);
7239
7240 if (!hlua || !hlua->task)
7241 WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in "
7242 "'frontend', 'backend' or 'task'"));
7243
William Lallemand746e6f32021-10-06 10:57:44 +02007244 if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE)
7245 WILL_LJMP(luaL_error(L, "'get' needs a table as argument"));
7246
William Lallemand4f4f2b72022-02-17 20:00:23 +01007247 hlua_hc = hlua_checkhttpclient(L, 1);
7248
7249 ret = lua_getfield(L, -1, "dst");
7250 if (ret == LUA_TSTRING) {
7251 if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0)
7252 WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument"));
7253 }
7254 lua_pop(L, 1);
7255
William Lallemand746e6f32021-10-06 10:57:44 +02007256 ret = lua_getfield(L, -1, "url");
7257 if (ret == LUA_TSTRING) {
7258 url_str = lua_tostring(L, -1);
7259 }
7260 lua_pop(L, 1);
William Lallemand3956c4e2021-09-21 16:25:15 +02007261
William Lallemandb4a4ef62022-02-23 14:18:16 +01007262 ret = lua_getfield(L, -1, "timeout");
7263 if (ret == LUA_TNUMBER) {
7264 timeout = lua_tointeger(L, -1);
7265 httpclient_set_timeout(hlua_hc->hc, timeout);
7266 }
William Lallemandab90ee82022-02-23 15:16:08 +01007267 lua_pop(L, 1);
William Lallemandb4a4ef62022-02-23 14:18:16 +01007268
William Lallemand746e6f32021-10-06 10:57:44 +02007269 ret = lua_getfield(L, -1, "headers");
7270 if (ret == LUA_TTABLE) {
7271 hdrs = hlua_httpclient_table_to_hdrs(L);
7272 }
7273 lua_pop(L, 1);
William Lallemand79416cb2021-09-24 14:51:44 +02007274
William Lallemanddec25c32021-10-25 19:48:37 +02007275 ret = lua_getfield(L, -1, "body");
7276 if (ret == LUA_TSTRING) {
William Lallemandbd5739e2021-10-28 15:41:38 +02007277 body_str = lua_tolstring(L, -1, &buf_len);
William Lallemanddec25c32021-10-25 19:48:37 +02007278 }
William Lallemandbd5739e2021-10-28 15:41:38 +02007279
William Lallemanddec25c32021-10-25 19:48:37 +02007280 lua_pop(L, 1);
7281
William Lallemand746e6f32021-10-06 10:57:44 +02007282 if (!url_str) {
7283 WILL_LJMP(luaL_error(L, "'get' need a 'url' argument"));
7284 return 0;
7285 }
William Lallemand3956c4e2021-09-21 16:25:15 +02007286
William Lallemand3956c4e2021-09-21 16:25:15 +02007287
7288 hlua_hc->hc->req.url = istdup(ist(url_str));
William Lallemanddc2cc902021-10-26 11:43:26 +02007289 hlua_hc->hc->req.meth = meth;
William Lallemand3956c4e2021-09-21 16:25:15 +02007290
7291 /* update the httpclient callbacks */
William Lallemandbd5739e2021-10-28 15:41:38 +02007292 hlua_hc->hc->ops.res_stline = hlua_httpclient_cb;
7293 hlua_hc->hc->ops.res_headers = hlua_httpclient_cb;
7294 hlua_hc->hc->ops.res_payload = hlua_httpclient_cb;
William Lallemand3956c4e2021-09-21 16:25:15 +02007295
William Lallemandbd5739e2021-10-28 15:41:38 +02007296 /* a body is available, it will use the request callback */
7297 if (body_str) {
7298 hlua_hc->hc->ops.req_payload = hlua_httpclient_cb;
7299 }
William Lallemand3956c4e2021-09-21 16:25:15 +02007300
William Lallemandbd5739e2021-10-28 15:41:38 +02007301 ret = httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, IST_NULL);
William Lallemand3956c4e2021-09-21 16:25:15 +02007302
William Lallemand746e6f32021-10-06 10:57:44 +02007303 /* free the temporary headers array */
7304 hdrs_i = hdrs;
7305 while (hdrs_i && isttest(hdrs_i->n)) {
7306 istfree(&hdrs_i->n);
7307 istfree(&hdrs_i->v);
7308 hdrs_i++;
7309 }
7310 ha_free(&hdrs);
7311
7312
William Lallemand6137a9e2021-10-26 15:01:53 +02007313 if (ret != ERR_NONE) {
7314 WILL_LJMP(luaL_error(L, "Can't generate the HTTP request"));
7315 return 0;
7316 }
7317
William Lallemand6137a9e2021-10-26 15:01:53 +02007318 httpclient_start(hlua_hc->hc);
7319
William Lallemandbd5739e2021-10-28 15:41:38 +02007320 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0));
William Lallemand3956c4e2021-09-21 16:25:15 +02007321
William Lallemand3956c4e2021-09-21 16:25:15 +02007322 return 0;
7323}
7324
7325/*
William Lallemanddc2cc902021-10-26 11:43:26 +02007326 * Sends an HTTP HEAD request and wait for a response
7327 *
7328 * httpclient:head(url, headers, payload)
7329 */
7330__LJMP static int hlua_httpclient_head(lua_State *L)
7331{
7332 return hlua_httpclient_send(L, HTTP_METH_HEAD);
7333}
7334
7335/*
7336 * Send an HTTP GET request and wait for a response
7337 *
7338 * httpclient:get(url, headers, payload)
7339 */
7340__LJMP static int hlua_httpclient_get(lua_State *L)
7341{
7342 return hlua_httpclient_send(L, HTTP_METH_GET);
7343
7344}
7345
7346/*
7347 * Sends an HTTP PUT request and wait for a response
7348 *
7349 * httpclient:put(url, headers, payload)
7350 */
7351__LJMP static int hlua_httpclient_put(lua_State *L)
7352{
7353 return hlua_httpclient_send(L, HTTP_METH_PUT);
7354}
7355
7356/*
7357 * Send an HTTP POST request and wait for a response
7358 *
7359 * httpclient:post(url, headers, payload)
7360 */
7361__LJMP static int hlua_httpclient_post(lua_State *L)
7362{
7363 return hlua_httpclient_send(L, HTTP_METH_POST);
7364}
7365
7366
7367/*
7368 * Sends an HTTP DELETE request and wait for a response
7369 *
7370 * httpclient:delete(url, headers, payload)
7371 */
7372__LJMP static int hlua_httpclient_delete(lua_State *L)
7373{
7374 return hlua_httpclient_send(L, HTTP_METH_DELETE);
7375}
7376
7377/*
William Lallemand3956c4e2021-09-21 16:25:15 +02007378 *
7379 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007380 * Class TXN
7381 *
7382 *
7383 */
7384
7385/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02007386 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007387 */
7388__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
7389{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007390 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007391}
7392
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007393__LJMP static int hlua_set_var(lua_State *L)
7394{
7395 struct hlua_txn *htxn;
7396 const char *name;
7397 size_t len;
7398 struct sample smp;
7399
Tim Duesterhus4e172c92020-05-19 13:49:42 +02007400 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7401 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007402
7403 /* It is useles to retrieve the stream, but this function
7404 * runs only in a stream context.
7405 */
7406 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7407 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
7408
7409 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01007410 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007411 hlua_lua2smp(L, 3, &smp);
7412
7413 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01007414 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02007415
7416 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
7417 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
7418 else
7419 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
7420
Tim Duesterhus84ebc132020-05-19 13:49:41 +02007421 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007422}
7423
Christopher Faulet85d79c92016-11-09 16:54:56 +01007424__LJMP static int hlua_unset_var(lua_State *L)
7425{
7426 struct hlua_txn *htxn;
7427 const char *name;
7428 size_t len;
7429 struct sample smp;
7430
7431 MAY_LJMP(check_args(L, 2, "unset_var"));
7432
7433 /* It is useles to retrieve the stream, but this function
7434 * runs only in a stream context.
7435 */
7436 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7437 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
7438
7439 /* Unset the variable. */
7440 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02007441 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
7442 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01007443}
7444
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007445__LJMP static int hlua_get_var(lua_State *L)
7446{
7447 struct hlua_txn *htxn;
7448 const char *name;
7449 size_t len;
7450 struct sample smp;
7451
7452 MAY_LJMP(check_args(L, 2, "get_var"));
7453
7454 /* It is useles to retrieve the stream, but this function
7455 * runs only in a stream context.
7456 */
7457 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7458 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
7459
Willy Tarreau7560dd42016-03-10 16:28:58 +01007460 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02007461 if (!vars_get_by_name(name, len, &smp, NULL)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007462 lua_pushnil(L);
7463 return 1;
7464 }
7465
7466 return hlua_smp2lua(L, &smp);
7467}
7468
Willy Tarreau59551662015-03-10 14:23:13 +01007469__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007470{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007471 struct hlua *hlua;
7472
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007473 MAY_LJMP(check_args(L, 2, "set_priv"));
7474
Willy Tarreau87b09662015-04-03 00:22:06 +02007475 /* It is useles to retrieve the stream, but this function
7476 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007477 */
7478 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007479
7480 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007481 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007482 if (!hlua)
7483 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007484
7485 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02007486 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007487
7488 /* Get and store new value. */
7489 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
7490 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
7491
7492 return 0;
7493}
7494
Willy Tarreau59551662015-03-10 14:23:13 +01007495__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007496{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007497 struct hlua *hlua;
7498
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007499 MAY_LJMP(check_args(L, 1, "get_priv"));
7500
Willy Tarreau87b09662015-04-03 00:22:06 +02007501 /* It is useles to retrieve the stream, but this function
7502 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007503 */
7504 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007505
7506 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007507 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007508 if (!hlua) {
7509 lua_pushnil(L);
7510 return 1;
7511 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007512
7513 /* Push configuration index in the stack. */
7514 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
7515
7516 return 1;
7517}
7518
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007519/* Create stack entry containing a class TXN. This function
7520 * return 0 if the stack does not contains free slots,
7521 * otherwise it returns 1.
7522 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02007523static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007524{
Willy Tarreaude491382015-04-06 11:04:28 +02007525 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007526
7527 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01007528 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007529 return 0;
7530
7531 /* NOTE: The allocation never fails. The failure
7532 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007533 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007534 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01007535 /* Create the object: obj[0] = userdata. */
7536 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02007537 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01007538 lua_rawseti(L, -2, 0);
7539
Willy Tarreaude491382015-04-06 11:04:28 +02007540 htxn->s = s;
7541 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01007542 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02007543 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007544
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007545 /* Create the "f" field that contains a list of fetches. */
7546 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01007547 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01007548 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007549 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01007550
7551 /* Create the "sf" field that contains a list of stringsafe fetches. */
7552 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01007553 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007554 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007555 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007556
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007557 /* Create the "c" field that contains a list of converters. */
7558 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02007559 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01007560 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007561 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01007562
7563 /* Create the "sc" field that contains a list of stringsafe converters. */
7564 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01007565 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007566 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007567 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007568
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007569 /* Create the "req" field that contains the request channel object. */
7570 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01007571 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007572 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007573 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007574
7575 /* Create the "res" field that contains the response channel object. */
7576 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01007577 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007578 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007579 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007580
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007581 /* Creates the HTTP object is the current proxy allows http. */
7582 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01007583 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02007584 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007585 return 0;
7586 }
7587 else
7588 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007589 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007590
Christopher Faulet78c35472020-02-26 17:14:08 +01007591 if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) {
7592 /* HTTPMessage object are created when a lua TXN is created from
7593 * a filter context only
7594 */
7595
7596 /* Creates the HTTP-Request object is the current proxy allows http. */
7597 lua_pushstring(L, "http_req");
7598 if (p->mode == PR_MODE_HTTP) {
7599 if (!hlua_http_msg_new(L, &s->txn->req))
7600 return 0;
7601 }
7602 else
7603 lua_pushnil(L);
7604 lua_rawset(L, -3);
7605
7606 /* Creates the HTTP-Response object is the current proxy allows http. */
7607 lua_pushstring(L, "http_res");
7608 if (p->mode == PR_MODE_HTTP) {
7609 if (!hlua_http_msg_new(L, &s->txn->rsp))
7610 return 0;
7611 }
7612 else
7613 lua_pushnil(L);
7614 lua_rawset(L, -3);
7615 }
7616
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007617 /* Pop a class sesison metatable and affect it to the userdata. */
7618 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
7619 lua_setmetatable(L, -2);
7620
7621 return 1;
7622}
7623
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007624__LJMP static int hlua_txn_deflog(lua_State *L)
7625{
7626 const char *msg;
7627 struct hlua_txn *htxn;
7628
7629 MAY_LJMP(check_args(L, 2, "deflog"));
7630 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7631 msg = MAY_LJMP(luaL_checkstring(L, 2));
7632
7633 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
7634 return 0;
7635}
7636
7637__LJMP static int hlua_txn_log(lua_State *L)
7638{
7639 int level;
7640 const char *msg;
7641 struct hlua_txn *htxn;
7642
7643 MAY_LJMP(check_args(L, 3, "log"));
7644 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7645 level = MAY_LJMP(luaL_checkinteger(L, 2));
7646 msg = MAY_LJMP(luaL_checkstring(L, 3));
7647
7648 if (level < 0 || level >= NB_LOG_LEVELS)
7649 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
7650
7651 hlua_sendlog(htxn->s->be, level, msg);
7652 return 0;
7653}
7654
7655__LJMP static int hlua_txn_log_debug(lua_State *L)
7656{
7657 const char *msg;
7658 struct hlua_txn *htxn;
7659
7660 MAY_LJMP(check_args(L, 2, "Debug"));
7661 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7662 msg = MAY_LJMP(luaL_checkstring(L, 2));
7663 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
7664 return 0;
7665}
7666
7667__LJMP static int hlua_txn_log_info(lua_State *L)
7668{
7669 const char *msg;
7670 struct hlua_txn *htxn;
7671
7672 MAY_LJMP(check_args(L, 2, "Info"));
7673 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7674 msg = MAY_LJMP(luaL_checkstring(L, 2));
7675 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
7676 return 0;
7677}
7678
7679__LJMP static int hlua_txn_log_warning(lua_State *L)
7680{
7681 const char *msg;
7682 struct hlua_txn *htxn;
7683
7684 MAY_LJMP(check_args(L, 2, "Warning"));
7685 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7686 msg = MAY_LJMP(luaL_checkstring(L, 2));
7687 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
7688 return 0;
7689}
7690
7691__LJMP static int hlua_txn_log_alert(lua_State *L)
7692{
7693 const char *msg;
7694 struct hlua_txn *htxn;
7695
7696 MAY_LJMP(check_args(L, 2, "Alert"));
7697 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7698 msg = MAY_LJMP(luaL_checkstring(L, 2));
7699 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
7700 return 0;
7701}
7702
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007703__LJMP static int hlua_txn_set_loglevel(lua_State *L)
7704{
7705 struct hlua_txn *htxn;
7706 int ll;
7707
7708 MAY_LJMP(check_args(L, 2, "set_loglevel"));
7709 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7710 ll = MAY_LJMP(luaL_checkinteger(L, 2));
7711
7712 if (ll < 0 || ll > 7)
7713 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
7714
7715 htxn->s->logs.level = ll;
7716 return 0;
7717}
7718
7719__LJMP static int hlua_txn_set_tos(lua_State *L)
7720{
7721 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007722 int tos;
7723
7724 MAY_LJMP(check_args(L, 2, "set_tos"));
7725 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7726 tos = MAY_LJMP(luaL_checkinteger(L, 2));
7727
Willy Tarreau1a18b542018-12-11 16:37:42 +01007728 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007729 return 0;
7730}
7731
7732__LJMP static int hlua_txn_set_mark(lua_State *L)
7733{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007734 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007735 int mark;
7736
7737 MAY_LJMP(check_args(L, 2, "set_mark"));
7738 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7739 mark = MAY_LJMP(luaL_checkinteger(L, 2));
7740
Lukas Tribus579e3e32019-08-11 18:03:45 +02007741 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007742 return 0;
7743}
7744
Patrick Hemmer268a7072018-05-11 12:52:31 -04007745__LJMP static int hlua_txn_set_priority_class(lua_State *L)
7746{
7747 struct hlua_txn *htxn;
7748
7749 MAY_LJMP(check_args(L, 2, "set_priority_class"));
7750 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7751 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
7752 return 0;
7753}
7754
7755__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
7756{
7757 struct hlua_txn *htxn;
7758
7759 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
7760 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7761 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
7762 return 0;
7763}
7764
Christopher Faulet700d9e82020-01-31 12:21:52 +01007765/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007766 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01007767 * message and terminate the transaction. It returns 1 on success and 0 on
7768 * error. The Reply must be on top of the stack.
7769 */
7770__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
7771{
7772 struct htx *htx;
7773 struct htx_sl *sl;
7774 struct h1m h1m;
7775 const char *status, *reason, *body;
7776 size_t status_len, reason_len, body_len;
7777 int ret, code, flags;
7778
7779 code = 200;
7780 status = "200";
7781 status_len = 3;
7782 ret = lua_getfield(L, -1, "status");
7783 if (ret == LUA_TNUMBER) {
7784 code = lua_tointeger(L, -1);
7785 status = lua_tolstring(L, -1, &status_len);
7786 }
7787 lua_pop(L, 1);
7788
7789 reason = http_get_reason(code);
7790 reason_len = strlen(reason);
7791 ret = lua_getfield(L, -1, "reason");
7792 if (ret == LUA_TSTRING)
7793 reason = lua_tolstring(L, -1, &reason_len);
7794 lua_pop(L, 1);
7795
7796 body = NULL;
7797 body_len = 0;
7798 ret = lua_getfield(L, -1, "body");
7799 if (ret == LUA_TSTRING)
7800 body = lua_tolstring(L, -1, &body_len);
7801 lua_pop(L, 1);
7802
7803 /* Prepare the response before inserting the headers */
7804 h1m_init_res(&h1m);
7805 htx = htx_from_buf(&s->res.buf);
7806 channel_htx_truncate(&s->res, htx);
7807 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
7808 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
7809 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
7810 ist2(status, status_len), ist2(reason, reason_len));
7811 }
7812 else {
7813 flags = HTX_SL_F_IS_RESP;
7814 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
7815 ist2(status, status_len), ist2(reason, reason_len));
7816 }
7817 if (!sl)
7818 goto fail;
7819 sl->info.res.status = code;
7820
7821 /* Push in the stack the "headers" entry. */
7822 ret = lua_getfield(L, -1, "headers");
7823 if (ret != LUA_TTABLE)
7824 goto skip_headers;
7825
7826 lua_pushnil(L);
7827 while (lua_next(L, -2) != 0) {
7828 struct ist name, value;
7829 const char *n, *v;
7830 size_t nlen, vlen;
7831
7832 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
7833 /* Skip element if the key is not a string or if the value is not a table */
7834 goto next_hdr;
7835 }
7836
7837 n = lua_tolstring(L, -2, &nlen);
7838 name = ist2(n, nlen);
7839 if (isteqi(name, ist("content-length"))) {
7840 /* Always skip content-length header. It will be added
7841 * later with the correct len
7842 */
7843 goto next_hdr;
7844 }
7845
7846 /* Loop on header's values */
7847 lua_pushnil(L);
7848 while (lua_next(L, -2)) {
7849 if (!lua_isstring(L, -1)) {
7850 /* Skip the value if it is not a string */
7851 goto next_value;
7852 }
7853
7854 v = lua_tolstring(L, -1, &vlen);
7855 value = ist2(v, vlen);
7856
7857 if (isteqi(name, ist("transfer-encoding")))
7858 h1_parse_xfer_enc_header(&h1m, value);
7859 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
7860 goto fail;
7861
7862 next_value:
7863 lua_pop(L, 1);
7864 }
7865
7866 next_hdr:
7867 lua_pop(L, 1);
7868 }
7869 skip_headers:
7870 lua_pop(L, 1);
7871
7872 /* Update h1m flags: CLEN is set if CHNK is not present */
7873 if (!(h1m.flags & H1_MF_CHNK)) {
7874 const char *clen = ultoa(body_len);
7875
7876 h1m.flags |= H1_MF_CLEN;
7877 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
7878 goto fail;
7879 }
7880 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
7881 h1m.flags |= H1_MF_XFER_LEN;
7882
7883 /* Update HTX start-line flags */
7884 if (h1m.flags & H1_MF_XFER_ENC)
7885 flags |= HTX_SL_F_XFER_ENC;
7886 if (h1m.flags & H1_MF_XFER_LEN) {
7887 flags |= HTX_SL_F_XFER_LEN;
7888 if (h1m.flags & H1_MF_CHNK)
7889 flags |= HTX_SL_F_CHNK;
7890 else if (h1m.flags & H1_MF_CLEN)
7891 flags |= HTX_SL_F_CLEN;
7892 if (h1m.body_len == 0)
7893 flags |= HTX_SL_F_BODYLESS;
7894 }
7895 sl->flags |= flags;
7896
7897
7898 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007899 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01007900 goto fail;
7901
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007902 htx->flags |= HTX_FL_EOM;
7903
Christopher Faulet700d9e82020-01-31 12:21:52 +01007904 /* Now, forward the response and terminate the transaction */
7905 s->txn->status = code;
7906 htx_to_buf(htx, &s->res.buf);
7907 if (!http_forward_proxy_resp(s, 1))
7908 goto fail;
7909
7910 return 1;
7911
7912 fail:
7913 channel_htx_truncate(&s->res, htx);
7914 return 0;
7915}
7916
7917/* Terminate a transaction if called from a lua action. For TCP streams,
7918 * processing is just aborted. Nothing is returned to the client and all
7919 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
7920 * is forwarded to the client before terminating the transaction. On success,
7921 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
7922 * with ACT_RET_ERR code. If this function is not called from a lua action, it
7923 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007924 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007925__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007926{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02007927 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01007928 struct stream *s;
7929 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007930
Willy Tarreaub2ccb562015-04-06 11:11:15 +02007931 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007932
Christopher Faulet700d9e82020-01-31 12:21:52 +01007933 /* If the flags NOTERM is set, we cannot terminate the session, so we
7934 * just end the execution of the current lua code. */
7935 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02007936 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02007937
Christopher Faulet700d9e82020-01-31 12:21:52 +01007938 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01007939 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01007940 struct channel *req = &s->req;
7941 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01007942
Christopher Faulet700d9e82020-01-31 12:21:52 +01007943 channel_auto_read(req);
7944 channel_abort(req);
7945 channel_auto_close(req);
7946 channel_erase(req);
7947
7948 res->wex = tick_add_ifset(now_ms, res->wto);
7949 channel_auto_read(res);
7950 channel_auto_close(res);
7951 channel_shutr_now(res);
7952
7953 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
7954 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02007955 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02007956
Christopher Faulet700d9e82020-01-31 12:21:52 +01007957 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
7958 /* No reply or invalid reply */
7959 s->txn->status = 0;
7960 http_reply_and_close(s, 0, NULL);
7961 }
7962 else {
7963 /* Remove extra args to have the reply on top of the stack */
7964 if (lua_gettop(L) > 2)
7965 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007966
Christopher Faulet700d9e82020-01-31 12:21:52 +01007967 if (!hlua_txn_forward_reply(L, s)) {
7968 if (!(s->flags & SF_ERR_MASK))
7969 s->flags |= SF_ERR_PRXCOND;
7970 lua_pushinteger(L, ACT_RET_ERR);
7971 WILL_LJMP(hlua_done(L));
7972 return 0; /* Never reached */
7973 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02007974 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007975
Christopher Faulet700d9e82020-01-31 12:21:52 +01007976 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
7977 if (htxn->dir == SMP_OPT_DIR_REQ) {
7978 /* let's log the request time */
7979 s->logs.tv_request = now;
7980 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02007981 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01007982 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02007983
Christopher Faulet700d9e82020-01-31 12:21:52 +01007984 done:
7985 if (!(s->flags & SF_ERR_MASK))
7986 s->flags |= SF_ERR_LOCAL;
7987 if (!(s->flags & SF_FINST_MASK))
7988 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02007989
Christopher Faulete48d1dc2021-08-13 14:11:17 +02007990 if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX)
7991 lua_pushinteger(L, -1);
7992 else
7993 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007994 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007995 return 0;
7996}
7997
Christopher Faulet700d9e82020-01-31 12:21:52 +01007998/*
7999 *
8000 *
8001 * Class REPLY
8002 *
8003 *
8004 */
8005
8006/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
8007 * free slots, the function fails and returns 0;
8008 */
8009static int hlua_txn_reply_new(lua_State *L)
8010{
8011 struct hlua_txn *htxn;
8012 const char *reason, *body = NULL;
8013 int ret, status;
8014
8015 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01008016 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01008017 hlua_pusherror(L, "txn object is not an HTTP transaction.");
8018 WILL_LJMP(lua_error(L));
8019 }
8020
8021 /* Default value */
8022 status = 200;
8023 reason = http_get_reason(status);
8024
8025 if (lua_istable(L, 2)) {
8026 /* load status and reason from the table argument at index 2 */
8027 ret = lua_getfield(L, 2, "status");
8028 if (ret == LUA_TNIL)
8029 goto reason;
8030 else if (ret != LUA_TNUMBER) {
8031 /* invalid status: ignore the reason */
8032 goto body;
8033 }
8034 status = lua_tointeger(L, -1);
8035
8036 reason:
8037 lua_pop(L, 1); /* restore the stack: remove status */
8038 ret = lua_getfield(L, 2, "reason");
8039 if (ret == LUA_TSTRING)
8040 reason = lua_tostring(L, -1);
8041
8042 body:
8043 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
8044 ret = lua_getfield(L, 2, "body");
8045 if (ret == LUA_TSTRING)
8046 body = lua_tostring(L, -1);
8047 lua_pop(L, 1); /* restore the stack: remove body */
8048 }
8049
8050 /* Create the Reply table */
8051 lua_newtable(L);
8052
8053 /* Add status element */
8054 lua_pushstring(L, "status");
8055 lua_pushinteger(L, status);
8056 lua_settable(L, -3);
8057
8058 /* Add reason element */
8059 reason = http_get_reason(status);
8060 lua_pushstring(L, "reason");
8061 lua_pushstring(L, reason);
8062 lua_settable(L, -3);
8063
8064 /* Add body element, nil if undefined */
8065 lua_pushstring(L, "body");
8066 if (body)
8067 lua_pushstring(L, body);
8068 else
8069 lua_pushnil(L);
8070 lua_settable(L, -3);
8071
8072 /* Add headers element */
8073 lua_pushstring(L, "headers");
8074 lua_newtable(L);
8075
8076 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
8077 if (lua_istable(L, 2)) {
8078 /* load headers from the table argument at index 2. If it is a table, copy it. */
8079 ret = lua_getfield(L, 2, "headers");
8080 if (ret == LUA_TTABLE) {
8081 /* stack: [ ... <headers:table>, <table> ] */
8082 lua_pushnil(L);
8083 while (lua_next(L, -2) != 0) {
8084 /* stack: [ ... <headers:table>, <table>, k, v] */
8085 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
8086 /* invalid value type, skip it */
8087 lua_pop(L, 1);
8088 continue;
8089 }
8090
8091
8092 /* Duplicate the key and swap it with the value. */
8093 lua_pushvalue(L, -2);
8094 lua_insert(L, -2);
8095 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
8096
8097 lua_newtable(L);
8098 lua_insert(L, -2);
8099 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
8100
8101 if (lua_isstring(L, -1)) {
8102 /* push the value in the inner table */
8103 lua_rawseti(L, -2, 1);
8104 }
8105 else { /* table */
8106 lua_pushnil(L);
8107 while (lua_next(L, -2) != 0) {
8108 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
8109 if (!lua_isstring(L, -1)) {
8110 /* invalid value type, skip it*/
8111 lua_pop(L, 1);
8112 continue;
8113 }
8114 /* push the value in the inner table */
8115 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
8116 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
8117 }
8118 lua_pop(L, 1);
8119 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
8120 }
8121
8122 /* push (k,v) on the stack in the headers table:
8123 * stack: [ ... <headers:table>, <table>, k, k, v ]
8124 */
8125 lua_settable(L, -5);
8126 /* stack: [ ... <headers:table>, <table>, k ] */
8127 }
8128 }
8129 lua_pop(L, 1);
8130 }
8131 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
8132 lua_settable(L, -3);
8133 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
8134
8135 /* Pop a class sesison metatable and affect it to the userdata. */
8136 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
8137 lua_setmetatable(L, -2);
8138 return 1;
8139}
8140
8141/* Set the reply status code, and optionally the reason. If no reason is
8142 * provided, the default one corresponding to the status code is used.
8143 */
8144__LJMP static int hlua_txn_reply_set_status(lua_State *L)
8145{
8146 int status = MAY_LJMP(luaL_checkinteger(L, 2));
8147 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
8148
8149 /* First argument (self) must be a table */
8150 luaL_checktype(L, 1, LUA_TTABLE);
8151
8152 if (status < 100 || status > 599) {
8153 lua_pushboolean(L, 0);
8154 return 1;
8155 }
8156 if (!reason)
8157 reason = http_get_reason(status);
8158
8159 lua_pushinteger(L, status);
8160 lua_setfield(L, 1, "status");
8161
8162 lua_pushstring(L, reason);
8163 lua_setfield(L, 1, "reason");
8164
8165 lua_pushboolean(L, 1);
8166 return 1;
8167}
8168
8169/* Add a header into the reply object. Each header name is associated to an
8170 * array of values in the "headers" table. If the header name is not found, a
8171 * new entry is created.
8172 */
8173__LJMP static int hlua_txn_reply_add_header(lua_State *L)
8174{
8175 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
8176 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
8177 int ret;
8178
8179 /* First argument (self) must be a table */
8180 luaL_checktype(L, 1, LUA_TTABLE);
8181
8182 /* Push in the stack the "headers" entry. */
8183 ret = lua_getfield(L, 1, "headers");
8184 if (ret != LUA_TTABLE) {
8185 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
8186 WILL_LJMP(lua_error(L));
8187 }
8188
8189 /* check if the header is already registered. If not, register it. */
8190 ret = lua_getfield(L, -1, name);
8191 if (ret == LUA_TNIL) {
8192 /* Entry not found. */
8193 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
8194
8195 /* Insert the new header name in the array in the top of the stack.
8196 * It left the new array in the top of the stack.
8197 */
8198 lua_newtable(L);
8199 lua_pushstring(L, name);
8200 lua_pushvalue(L, -2);
8201 lua_settable(L, -4);
8202 }
8203 else if (ret != LUA_TTABLE) {
8204 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
8205 WILL_LJMP(lua_error(L));
8206 }
8207
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008208 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01008209 * the header value as new entry.
8210 */
8211 lua_pushstring(L, value);
8212 ret = lua_rawlen(L, -2);
8213 lua_rawseti(L, -2, ret + 1);
8214
8215 lua_pushboolean(L, 1);
8216 return 1;
8217}
8218
8219/* Remove all occurrences of a given header name. */
8220__LJMP static int hlua_txn_reply_del_header(lua_State *L)
8221{
8222 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
8223 int ret;
8224
8225 /* First argument (self) must be a table */
8226 luaL_checktype(L, 1, LUA_TTABLE);
8227
8228 /* Push in the stack the "headers" entry. */
8229 ret = lua_getfield(L, 1, "headers");
8230 if (ret != LUA_TTABLE) {
8231 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
8232 WILL_LJMP(lua_error(L));
8233 }
8234
8235 lua_pushstring(L, name);
8236 lua_pushnil(L);
8237 lua_settable(L, -3);
8238
8239 lua_pushboolean(L, 1);
8240 return 1;
8241}
8242
8243/* Set the reply's body. Overwrite any existing entry. */
8244__LJMP static int hlua_txn_reply_set_body(lua_State *L)
8245{
8246 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
8247
8248 /* First argument (self) must be a table */
8249 luaL_checktype(L, 1, LUA_TTABLE);
8250
8251 lua_pushstring(L, payload);
8252 lua_setfield(L, 1, "body");
8253
8254 lua_pushboolean(L, 1);
8255 return 1;
8256}
8257
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008258__LJMP static int hlua_log(lua_State *L)
8259{
8260 int level;
8261 const char *msg;
8262
8263 MAY_LJMP(check_args(L, 2, "log"));
8264 level = MAY_LJMP(luaL_checkinteger(L, 1));
8265 msg = MAY_LJMP(luaL_checkstring(L, 2));
8266
8267 if (level < 0 || level >= NB_LOG_LEVELS)
8268 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
8269
8270 hlua_sendlog(NULL, level, msg);
8271 return 0;
8272}
8273
8274__LJMP static int hlua_log_debug(lua_State *L)
8275{
8276 const char *msg;
8277
8278 MAY_LJMP(check_args(L, 1, "debug"));
8279 msg = MAY_LJMP(luaL_checkstring(L, 1));
8280 hlua_sendlog(NULL, LOG_DEBUG, msg);
8281 return 0;
8282}
8283
8284__LJMP static int hlua_log_info(lua_State *L)
8285{
8286 const char *msg;
8287
8288 MAY_LJMP(check_args(L, 1, "info"));
8289 msg = MAY_LJMP(luaL_checkstring(L, 1));
8290 hlua_sendlog(NULL, LOG_INFO, msg);
8291 return 0;
8292}
8293
8294__LJMP static int hlua_log_warning(lua_State *L)
8295{
8296 const char *msg;
8297
8298 MAY_LJMP(check_args(L, 1, "warning"));
8299 msg = MAY_LJMP(luaL_checkstring(L, 1));
8300 hlua_sendlog(NULL, LOG_WARNING, msg);
8301 return 0;
8302}
8303
8304__LJMP static int hlua_log_alert(lua_State *L)
8305{
8306 const char *msg;
8307
8308 MAY_LJMP(check_args(L, 1, "alert"));
8309 msg = MAY_LJMP(luaL_checkstring(L, 1));
8310 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01008311 return 0;
8312}
8313
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01008314__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008315{
8316 int wakeup_ms = lua_tointeger(L, -1);
Willy Tarreau12c02702021-09-30 16:12:31 +02008317 if (!tick_is_expired(wakeup_ms, now_ms))
Willy Tarreau9635e032018-10-16 17:52:55 +02008318 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008319 return 0;
8320}
8321
8322__LJMP static int hlua_sleep(lua_State *L)
8323{
8324 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008325 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008326
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008327 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008328
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01008329 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008330 wakeup_ms = tick_add(now_ms, delay);
8331 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008332
Willy Tarreau9635e032018-10-16 17:52:55 +02008333 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008334 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008335}
8336
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008337__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008338{
8339 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008340 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008341
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008342 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008343
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01008344 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008345 wakeup_ms = tick_add(now_ms, delay);
8346 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008347
Willy Tarreau9635e032018-10-16 17:52:55 +02008348 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008349 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008350}
8351
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008352/* This functionis an LUA binding. it permits to give back
8353 * the hand at the HAProxy scheduler. It is used when the
8354 * LUA processing consumes a lot of time.
8355 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01008356__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008357{
8358 return 0;
8359}
8360
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008361__LJMP static int hlua_yield(lua_State *L)
8362{
Willy Tarreau9635e032018-10-16 17:52:55 +02008363 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008364 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008365}
8366
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008367/* This function change the nice of the currently executed
8368 * task. It is used set low or high priority at the current
8369 * task.
8370 */
Willy Tarreau59551662015-03-10 14:23:13 +01008371__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008372{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008373 struct hlua *hlua;
8374 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008375
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008376 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008377 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008378
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008379 /* Get hlua struct, or NULL if we execute from main lua state */
8380 hlua = hlua_gethlua(L);
8381
8382 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008383 if (!hlua || !hlua->task)
8384 return 0;
8385
8386 if (nice < -1024)
8387 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008388 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008389 nice = 1024;
8390
8391 hlua->task->nice = nice;
8392 return 0;
8393}
8394
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008395/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008396 * HAProxy task subsystem when the task is awaked. The LUA runtime can
8397 * return an E_AGAIN signal, the emmiter of this signal must set a
8398 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008399 *
8400 * Task wrapper are longjmp safe because the only one Lua code
8401 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008402 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01008403struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008404{
Olivier Houchard9f6af332018-05-25 14:04:04 +02008405 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008406 enum hlua_exec status;
8407
Christopher Faulet5bc99722018-04-25 10:34:45 +02008408 if (task->thread_mask == MAX_THREADS_MASK)
8409 task_set_affinity(task, tid_bit);
8410
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008411 /* If it is the first call to the task, we must initialize the
8412 * execution timeouts.
8413 */
8414 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02008415 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008416
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008417 /* Execute the Lua code. */
8418 status = hlua_ctx_resume(hlua, 1);
8419
8420 switch (status) {
8421 /* finished or yield */
8422 case HLUA_E_OK:
8423 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02008424 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02008425 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008426 break;
8427
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01008428 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01008429 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02008430 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008431 break;
8432
8433 /* finished with error. */
8434 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008435 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008436 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02008437 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02008438 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008439 break;
8440
8441 case HLUA_E_ERR:
8442 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008443 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008444 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02008445 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02008446 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008447 break;
8448 }
Emeric Brun253e53e2017-10-17 18:58:40 +02008449 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008450}
8451
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008452/* This function is an LUA binding that register LUA function to be
8453 * executed after the HAProxy configuration parsing and before the
8454 * HAProxy scheduler starts. This function expect only one LUA
8455 * argument that is a function. This function returns nothing, but
8456 * throws if an error is encountered.
8457 */
8458__LJMP static int hlua_register_init(lua_State *L)
8459{
8460 struct hlua_init_function *init;
8461 int ref;
8462
8463 MAY_LJMP(check_args(L, 1, "register_init"));
8464
8465 ref = MAY_LJMP(hlua_checkfunction(L, 1));
8466
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008467 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008468 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008469 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008470
8471 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02008472 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008473 return 0;
8474}
8475
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008476/* This functio is an LUA binding. It permits to register a task
8477 * executed in parallel of the main HAroxy activity. The task is
8478 * created and it is set in the HAProxy scheduler. It can be called
8479 * from the "init" section, "post init" or during the runtime.
8480 *
8481 * Lua prototype:
8482 *
8483 * <none> core.register_task(<function>)
8484 */
8485static int hlua_register_task(lua_State *L)
8486{
Christopher Faulet5294ec02021-04-12 12:24:47 +02008487 struct hlua *hlua = NULL;
8488 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008489 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01008490 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008491
8492 MAY_LJMP(check_args(L, 1, "register_task"));
8493
8494 ref = MAY_LJMP(hlua_checkfunction(L, 1));
8495
Thierry Fournier75fc0292020-11-28 13:18:56 +01008496 /* Get the reference state. If the reference is NULL, L is the master
8497 * state, otherwise hlua->T is.
8498 */
8499 hlua = hlua_gethlua(L);
8500 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01008501 /* we are in runtime processing */
8502 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01008503 else
Thierry Fournier021d9862020-11-28 23:42:03 +01008504 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01008505 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01008506
Willy Tarreaubafbe012017-11-24 17:34:44 +01008507 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008508 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02008509 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008510 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008511
Thierry Fournier59f11be2020-11-29 00:37:41 +01008512 /* We are in the common lua state, execute the task anywhere,
8513 * otherwise, inherit the current thread identifier
8514 */
8515 if (state_id == 0)
Willy Tarreaubeeabf52021-10-01 18:23:30 +02008516 task = task_new_anywhere();
Thierry Fournier59f11be2020-11-29 00:37:41 +01008517 else
Willy Tarreaubeeabf52021-10-01 18:23:30 +02008518 task = task_new_here();
Willy Tarreaue09101e2018-10-16 17:37:12 +02008519 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02008520 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02008521
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008522 task->context = hlua;
8523 task->process = hlua_process_task;
8524
Thierry Fournier021d9862020-11-28 23:42:03 +01008525 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02008526 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008527
8528 /* Restore the function in the stack. */
8529 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
8530 hlua->nargs = 0;
8531
8532 /* Schedule task. */
Willy Tarreaue3957f82021-09-30 16:17:37 +02008533 task_wakeup(task, TASK_WOKEN_INIT);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008534
8535 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02008536
8537 alloc_error:
8538 task_destroy(task);
8539 hlua_ctx_destroy(hlua);
8540 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8541 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008542}
8543
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008544/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
8545 * doesn't allow "yield" functions because the HAProxy engine cannot
8546 * resume converters.
8547 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008548static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008549{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02008550 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008551 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008552 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008553
Willy Tarreaube508f12016-03-10 11:47:01 +01008554 if (!stream)
8555 return 0;
8556
Willy Tarreau87b09662015-04-03 00:22:06 +02008557 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01008558 * Lua context can be not initialized. This behavior
8559 * permits to save performances because a systematic
8560 * Lua initialization cause 5% performances loss.
8561 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008562 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008563 struct hlua *hlua;
8564
8565 hlua = pool_alloc(pool_head_hlua);
8566 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008567 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
8568 return 0;
8569 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008570 HLUA_INIT(hlua);
8571 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01008572 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008573 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
8574 return 0;
8575 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01008576 }
8577
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008578 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008579 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008580
8581 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008582 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008583 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
8584 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01008585 else
8586 error = "critical error";
8587 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008588 return 0;
8589 }
8590
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008591 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008592 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008593 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008594 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008595 return 0;
8596 }
8597
8598 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008599 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008600
8601 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008602 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008603 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008604 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008605 return 0;
8606 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008607 hlua_smp2lua(stream->hlua->T, smp);
8608 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008609
8610 /* push keywords in the stack. */
8611 if (arg_p) {
8612 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008613 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008614 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008615 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008616 return 0;
8617 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008618 hlua_arg2lua(stream->hlua->T, arg_p);
8619 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008620 }
8621 }
8622
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008623 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008624 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008625
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008626 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008627 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008628 }
8629
8630 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008631 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008632 /* finished. */
8633 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02008634 /* If the stack is empty, the function fails. */
8635 if (lua_gettop(stream->hlua->T) <= 0)
8636 return 0;
8637
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008638 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008639 hlua_lua2smp(stream->hlua->T, -1, smp);
8640 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008641 return 1;
8642
8643 /* yield. */
8644 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008645 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008646 return 0;
8647
8648 /* finished with error. */
8649 case HLUA_E_ERRMSG:
8650 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008651 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008652 fcn->name, lua_tostring(stream->hlua->T, -1));
8653 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008654 return 0;
8655
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008656 case HLUA_E_ETMOUT:
8657 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
8658 return 0;
8659
8660 case HLUA_E_NOMEM:
8661 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
8662 return 0;
8663
8664 case HLUA_E_YIELD:
8665 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
8666 return 0;
8667
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008668 case HLUA_E_ERR:
8669 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008670 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02008671 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008672
8673 default:
8674 return 0;
8675 }
8676}
8677
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008678/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
8679 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01008680 * resume sample-fetches. This function will be called by the sample
8681 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008682 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008683static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
8684 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008685{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02008686 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008687 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008688 const char *error;
Christopher Faulet8c9e6bb2021-08-06 16:29:41 +02008689 unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008690
Willy Tarreaube508f12016-03-10 11:47:01 +01008691 if (!stream)
8692 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01008693
Willy Tarreau87b09662015-04-03 00:22:06 +02008694 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01008695 * Lua context can be not initialized. This behavior
8696 * permits to save performances because a systematic
8697 * Lua initialization cause 5% performances loss.
8698 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008699 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008700 struct hlua *hlua;
8701
8702 hlua = pool_alloc(pool_head_hlua);
8703 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008704 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
8705 return 0;
8706 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008707 hlua->T = NULL;
8708 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01008709 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008710 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
8711 return 0;
8712 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01008713 }
8714
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008715 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008716 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008717
8718 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008719 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008720 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
8721 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01008722 else
8723 error = "critical error";
8724 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008725 return 0;
8726 }
8727
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008728 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008729 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008730 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008731 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008732 return 0;
8733 }
8734
8735 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008736 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008737
8738 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02008739 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008740 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008741 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008742 return 0;
8743 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008744 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008745
8746 /* push keywords in the stack. */
8747 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
8748 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008749 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008750 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008751 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008752 return 0;
8753 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008754 hlua_arg2lua(stream->hlua->T, arg_p);
8755 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008756 }
8757
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008758 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008759 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008760
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008761 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008762 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008763 }
8764
8765 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008766 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008767 /* finished. */
8768 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02008769 /* If the stack is empty, the function fails. */
8770 if (lua_gettop(stream->hlua->T) <= 0)
8771 return 0;
8772
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008773 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008774 hlua_lua2smp(stream->hlua->T, -1, smp);
8775 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008776
8777 /* Set the end of execution flag. */
8778 smp->flags &= ~SMP_F_MAY_CHANGE;
8779 return 1;
8780
8781 /* yield. */
8782 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008783 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008784 return 0;
8785
8786 /* finished with error. */
8787 case HLUA_E_ERRMSG:
8788 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008789 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008790 fcn->name, lua_tostring(stream->hlua->T, -1));
8791 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008792 return 0;
8793
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008794 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008795 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
8796 return 0;
8797
8798 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008799 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
8800 return 0;
8801
8802 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008803 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
8804 return 0;
8805
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008806 case HLUA_E_ERR:
8807 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008808 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02008809 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008810
8811 default:
8812 return 0;
8813 }
8814}
8815
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008816/* This function is an LUA binding used for registering
8817 * "sample-conv" functions. It expects a converter name used
8818 * in the haproxy configuration file, and an LUA function.
8819 */
8820__LJMP static int hlua_register_converters(lua_State *L)
8821{
8822 struct sample_conv_kw_list *sck;
8823 const char *name;
8824 int ref;
8825 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02008826 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008827 struct sample_conv *sc;
8828 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008829
8830 MAY_LJMP(check_args(L, 2, "register_converters"));
8831
8832 /* First argument : converter name. */
8833 name = MAY_LJMP(luaL_checkstring(L, 1));
8834
8835 /* Second argument : lua function. */
8836 ref = MAY_LJMP(hlua_checkfunction(L, 2));
8837
Thierry Fournierf67442e2020-11-28 20:41:07 +01008838 /* Check if the converter is already registered */
8839 trash = get_trash_chunk();
8840 chunk_printf(trash, "lua.%s", name);
8841 sc = find_sample_conv(trash->area, trash->data);
8842 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008843 fcn = sc->private;
8844 if (fcn->function_ref[hlua_state_id] != -1) {
8845 ha_warning("Trying to register converter 'lua.%s' more than once. "
8846 "This will become a hard error in version 2.5.\n", name);
8847 }
8848 fcn->function_ref[hlua_state_id] = ref;
8849 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008850 }
8851
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008852 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008853 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008854 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02008855 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008856 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008857 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02008858 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008859
8860 /* Fill fcn. */
8861 fcn->name = strdup(name);
8862 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02008863 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01008864 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008865
8866 /* List head */
8867 sck->list.n = sck->list.p = NULL;
8868
8869 /* converter keyword. */
8870 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008871 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008872 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02008873 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008874
8875 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
8876 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01008877 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 +01008878 sck->kw[0].val_args = NULL;
8879 sck->kw[0].in_type = SMP_T_STR;
8880 sck->kw[0].out_type = SMP_T_STR;
8881 sck->kw[0].private = fcn;
8882
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008883 /* Register this new converter */
8884 sample_register_convs(sck);
8885
8886 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02008887
8888 alloc_error:
8889 release_hlua_function(fcn);
8890 ha_free(&sck);
8891 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8892 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008893}
8894
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008895/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008896 * "sample-fetch" functions. It expects a converter name used
8897 * in the haproxy configuration file, and an LUA function.
8898 */
8899__LJMP static int hlua_register_fetches(lua_State *L)
8900{
8901 const char *name;
8902 int ref;
8903 int len;
8904 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02008905 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008906 struct sample_fetch *sf;
8907 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008908
8909 MAY_LJMP(check_args(L, 2, "register_fetches"));
8910
8911 /* First argument : sample-fetch name. */
8912 name = MAY_LJMP(luaL_checkstring(L, 1));
8913
8914 /* Second argument : lua function. */
8915 ref = MAY_LJMP(hlua_checkfunction(L, 2));
8916
Thierry Fournierf67442e2020-11-28 20:41:07 +01008917 /* Check if the sample-fetch is already registered */
8918 trash = get_trash_chunk();
8919 chunk_printf(trash, "lua.%s", name);
8920 sf = find_sample_fetch(trash->area, trash->data);
8921 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008922 fcn = sf->private;
8923 if (fcn->function_ref[hlua_state_id] != -1) {
8924 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
8925 "This will become a hard error in version 2.5.\n", name);
8926 }
8927 fcn->function_ref[hlua_state_id] = ref;
8928 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008929 }
8930
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008931 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008932 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008933 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02008934 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008935 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008936 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02008937 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008938
8939 /* Fill fcn. */
8940 fcn->name = strdup(name);
8941 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02008942 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01008943 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008944
8945 /* List head */
8946 sfk->list.n = sfk->list.p = NULL;
8947
8948 /* sample-fetch keyword. */
8949 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008950 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008951 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02008952 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008953
8954 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
8955 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01008956 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 +01008957 sfk->kw[0].val_args = NULL;
8958 sfk->kw[0].out_type = SMP_T_STR;
8959 sfk->kw[0].use = SMP_USE_HTTP_ANY;
8960 sfk->kw[0].val = 0;
8961 sfk->kw[0].private = fcn;
8962
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008963 /* Register this new fetch. */
8964 sample_register_fetches(sfk);
8965
8966 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02008967
8968 alloc_error:
8969 release_hlua_function(fcn);
8970 ha_free(&sfk);
8971 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8972 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008973}
8974
Christopher Faulet501465d2020-02-26 14:54:16 +01008975/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008976 */
Christopher Faulet501465d2020-02-26 14:54:16 +01008977__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008978{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008979 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008980 unsigned int delay;
8981 unsigned int wakeup_ms;
8982
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008983 /* Get hlua struct, or NULL if we execute from main lua state */
8984 hlua = hlua_gethlua(L);
8985 if (!hlua) {
8986 return 0;
8987 }
8988
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008989 MAY_LJMP(check_args(L, 1, "wake_time"));
8990
8991 delay = MAY_LJMP(luaL_checkinteger(L, 1));
8992 wakeup_ms = tick_add(now_ms, delay);
8993 hlua->wake_time = wakeup_ms;
8994 return 0;
8995}
8996
Christopher Faulet7716cdf2020-01-29 11:53:30 +01008997/* This function is a wrapper to execute each LUA function declared as an action
8998 * wrapper during the initialisation period. This function may return any
8999 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
9000 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
9001 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009002 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009003static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02009004 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009005{
9006 char **arg;
Christopher Faulet8c9e6bb2021-08-06 16:29:41 +02009007 unsigned int hflags = HLUA_TXN_ACT_CTX;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009008 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01009009 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009010
9011 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01009012 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
9013 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
9014 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
9015 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009016 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009017 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009018 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009019 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009020
Willy Tarreau87b09662015-04-03 00:22:06 +02009021 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01009022 * Lua context can be not initialized. This behavior
9023 * permits to save performances because a systematic
9024 * Lua initialization cause 5% performances loss.
9025 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009026 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01009027 struct hlua *hlua;
9028
9029 hlua = pool_alloc(pool_head_hlua);
9030 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009031 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009032 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009033 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009034 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01009035 HLUA_INIT(hlua);
9036 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01009037 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 +01009038 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009039 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009040 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009041 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01009042 }
9043
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009044 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009045 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009046
9047 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009048 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009049 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
9050 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01009051 else
9052 error = "critical error";
9053 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009054 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009055 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009056 }
9057
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009058 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009059 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009060 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009061 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009062 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009063 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009064 }
9065
9066 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01009067 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 +01009068
Willy Tarreau87b09662015-04-03 00:22:06 +02009069 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02009070 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009071 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009072 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009073 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009074 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009075 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009076 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009077
9078 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009079 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009080 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009081 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009082 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009083 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009084 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009085 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009086 lua_pushstring(s->hlua->T, *arg);
9087 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009088 }
9089
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009090 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009091 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009092
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009093 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009094 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009095 }
9096
9097 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01009098 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009099 /* finished. */
9100 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009101 /* Catch the return value */
9102 if (lua_gettop(s->hlua->T) > 0)
9103 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009104
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01009105 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02009106 if (act_ret == ACT_RET_YIELD) {
9107 if (flags & ACT_OPT_FINAL)
9108 goto err_yield;
9109
Christopher Faulet8f587ea2020-07-28 12:01:55 +02009110 if (dir == SMP_OPT_DIR_REQ)
9111 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
9112 s->hlua->wake_time);
9113 else
9114 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
9115 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01009116 }
9117 goto end;
9118
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009119 /* yield. */
9120 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01009121 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02009122 if (dir == SMP_OPT_DIR_REQ)
9123 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
9124 s->hlua->wake_time);
9125 else
9126 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
9127 s->hlua->wake_time);
9128
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009129 /* Some actions can be wake up when a "write" event
9130 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009131 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009132 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02009133 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009134 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009135 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009136 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009137 act_ret = ACT_RET_YIELD;
9138 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009139
9140 /* finished with error. */
9141 case HLUA_E_ERRMSG:
9142 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009143 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009144 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009145 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009146 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009147
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009148 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01009149 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009150 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009151
9152 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01009153 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009154 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009155
9156 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02009157 err_yield:
9158 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009159 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009160 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009161 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009162
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009163 case HLUA_E_ERR:
9164 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009165 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009166 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009167
9168 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009169 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009170 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009171
9172 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02009173 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02009174 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009175 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009176}
9177
Willy Tarreau144f84a2021-03-02 16:09:26 +01009178struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009179{
Olivier Houchard9f6af332018-05-25 14:04:04 +02009180 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009181
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009182 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02009183 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02009184 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009185}
9186
9187static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
9188{
Christopher Faulet86e1c332021-12-20 17:09:39 +01009189 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009190 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009191 struct task *task;
9192 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01009193 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009194
Willy Tarreaubafbe012017-11-24 17:34:44 +01009195 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009196 if (!hlua) {
9197 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009198 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009199 return 0;
9200 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009201 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009202 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009203 ctx->ctx.hlua_apptcp.flags = 0;
9204
9205 /* Create task used by signal to wakeup applets. */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02009206 task = task_new_here();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009207 if (!task) {
9208 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009209 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009210 return 0;
9211 }
9212 task->nice = 0;
9213 task->context = ctx;
9214 task->process = hlua_applet_wakeup;
9215 ctx->ctx.hlua_apptcp.task = task;
9216
9217 /* In the execution wrappers linked with a stream, the
9218 * Lua context can be not initialized. This behavior
9219 * permits to save performances because a systematic
9220 * Lua initialization cause 5% performances loss.
9221 */
Thierry Fournierc7492592020-11-28 23:57:24 +01009222 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 +02009223 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009224 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009225 return 0;
9226 }
9227
9228 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02009229 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009230
9231 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009232 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01009233 if (lua_type(hlua->T, -1) == LUA_TSTRING)
9234 error = lua_tostring(hlua->T, -1);
9235 else
9236 error = "critical error";
9237 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009238 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009239 return 0;
9240 }
9241
9242 /* Check stack available size. */
9243 if (!lua_checkstack(hlua->T, 1)) {
9244 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009245 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009246 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009247 return 0;
9248 }
9249
9250 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01009251 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009252
9253 /* Create and and push object stream in the stack. */
9254 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
9255 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009256 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009257 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009258 return 0;
9259 }
9260 hlua->nargs = 1;
9261
9262 /* push keywords in the stack. */
9263 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
9264 if (!lua_checkstack(hlua->T, 1)) {
9265 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009266 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009267 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009268 return 0;
9269 }
9270 lua_pushstring(hlua->T, *arg);
9271 hlua->nargs++;
9272 }
9273
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009274 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009275
9276 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01009277 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01009278 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009279
9280 return 1;
9281}
9282
Willy Tarreau60409db2019-08-21 14:14:50 +02009283void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009284{
Christopher Faulet86e1c332021-12-20 17:09:39 +01009285 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009286 struct stream *strm = si_strm(si);
9287 struct channel *res = si_ic(si);
9288 struct act_rule *rule = ctx->rule;
9289 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009290 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009291
9292 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02009293 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
9294 /* eat the whole request */
9295 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009296 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02009297 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009298
9299 /* If the stream is disconnect or closed, ldo nothing. */
9300 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
9301 return;
9302
9303 /* Execute the function. */
9304 switch (hlua_ctx_resume(hlua, 1)) {
9305 /* finished. */
9306 case HLUA_E_OK:
9307 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
9308
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009309 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02009310 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009311 res->flags |= CF_READ_NULL;
9312 si_shutr(si);
9313 return;
9314
9315 /* yield. */
9316 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01009317 if (hlua->wake_time != TICK_ETERNITY)
9318 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009319 return;
9320
9321 /* finished with error. */
9322 case HLUA_E_ERRMSG:
9323 /* Display log. */
9324 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009325 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009326 lua_pop(hlua->T, 1);
9327 goto error;
9328
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009329 case HLUA_E_ETMOUT:
9330 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009331 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009332 goto error;
9333
9334 case HLUA_E_NOMEM:
9335 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009336 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009337 goto error;
9338
9339 case HLUA_E_YIELD: /* unexpected */
9340 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009341 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009342 goto error;
9343
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009344 case HLUA_E_ERR:
9345 /* Display log. */
9346 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009347 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009348 goto error;
9349
9350 default:
9351 goto error;
9352 }
9353
9354error:
9355
9356 /* For all other cases, just close the stream. */
9357 si_shutw(si);
9358 si_shutr(si);
9359 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
9360}
9361
9362static void hlua_applet_tcp_release(struct appctx *ctx)
9363{
Olivier Houchard3f795f72019-04-17 22:51:06 +02009364 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009365 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009366 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009367 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009368}
9369
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009370/* The function returns 1 if the initialisation is complete, 0 if
9371 * an errors occurs and -1 if more data are required for initializing
9372 * the applet.
9373 */
9374static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
9375{
Christopher Faulet86e1c332021-12-20 17:09:39 +01009376 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009377 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009378 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009379 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009380 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01009381 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009382
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009383 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01009384 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009385 if (!hlua) {
9386 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009387 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009388 return 0;
9389 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009390 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009391 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009392 ctx->ctx.hlua_apphttp.left_bytes = -1;
9393 ctx->ctx.hlua_apphttp.flags = 0;
9394
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01009395 if (txn->req.flags & HTTP_MSGF_VER_11)
9396 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
9397
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009398 /* Create task used by signal to wakeup applets. */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02009399 task = task_new_here();
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009400 if (!task) {
9401 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009402 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009403 return 0;
9404 }
9405 task->nice = 0;
9406 task->context = ctx;
9407 task->process = hlua_applet_wakeup;
9408 ctx->ctx.hlua_apphttp.task = task;
9409
9410 /* In the execution wrappers linked with a stream, the
9411 * Lua context can be not initialized. This behavior
9412 * permits to save performances because a systematic
9413 * Lua initialization cause 5% performances loss.
9414 */
Thierry Fournierc7492592020-11-28 23:57:24 +01009415 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 +02009416 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009417 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009418 return 0;
9419 }
9420
9421 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02009422 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009423
9424 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009425 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01009426 if (lua_type(hlua->T, -1) == LUA_TSTRING)
9427 error = lua_tostring(hlua->T, -1);
9428 else
9429 error = "critical error";
9430 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009431 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009432 return 0;
9433 }
9434
9435 /* Check stack available size. */
9436 if (!lua_checkstack(hlua->T, 1)) {
9437 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009438 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009439 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009440 return 0;
9441 }
9442
9443 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01009444 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009445
9446 /* Create and and push object stream in the stack. */
9447 if (!hlua_applet_http_new(hlua->T, ctx)) {
9448 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009449 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009450 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009451 return 0;
9452 }
9453 hlua->nargs = 1;
9454
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009455 /* push keywords in the stack. */
9456 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
9457 if (!lua_checkstack(hlua->T, 1)) {
9458 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009459 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009460 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009461 return 0;
9462 }
9463 lua_pushstring(hlua->T, *arg);
9464 hlua->nargs++;
9465 }
9466
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009467 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009468
9469 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01009470 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009471
9472 return 1;
9473}
9474
Willy Tarreau60409db2019-08-21 14:14:50 +02009475void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009476{
Christopher Faulet86e1c332021-12-20 17:09:39 +01009477 struct stream_interface *si = cs_si(ctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009478 struct stream *strm = si_strm(si);
9479 struct channel *req = si_oc(si);
9480 struct channel *res = si_ic(si);
9481 struct act_rule *rule = ctx->rule;
9482 struct proxy *px = strm->be;
9483 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
9484 struct htx *req_htx, *res_htx;
9485
9486 res_htx = htx_from_buf(&res->buf);
9487
9488 /* If the stream is disconnect or closed, ldo nothing. */
9489 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
9490 goto out;
9491
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009492 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009493 if (!b_size(&res->buf)) {
9494 si_rx_room_blk(si);
9495 goto out;
9496 }
9497 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009498 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009499 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
9500
9501 /* Set the currently running flag. */
9502 if (!HLUA_IS_RUNNING(hlua) &&
9503 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02009504 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009505 si_cant_get(si);
9506 goto out;
9507 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009508 }
9509
9510 /* Executes The applet if it is not done. */
9511 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
9512
9513 /* Execute the function. */
9514 switch (hlua_ctx_resume(hlua, 1)) {
9515 /* finished. */
9516 case HLUA_E_OK:
9517 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
9518 break;
9519
9520 /* yield. */
9521 case HLUA_E_AGAIN:
9522 if (hlua->wake_time != TICK_ETERNITY)
9523 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009524 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009525
9526 /* finished with error. */
9527 case HLUA_E_ERRMSG:
9528 /* Display log. */
9529 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009530 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009531 lua_pop(hlua->T, 1);
9532 goto error;
9533
9534 case HLUA_E_ETMOUT:
9535 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009536 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009537 goto error;
9538
9539 case HLUA_E_NOMEM:
9540 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009541 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009542 goto error;
9543
9544 case HLUA_E_YIELD: /* unexpected */
9545 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009546 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009547 goto error;
9548
9549 case HLUA_E_ERR:
9550 /* Display log. */
9551 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009552 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009553 goto error;
9554
9555 default:
9556 goto error;
9557 }
9558 }
9559
9560 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009561 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
9562 goto done;
9563
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009564 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
9565 goto error;
9566
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01009567 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
9568 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009569 strm->txn->status = ctx->ctx.hlua_apphttp.status;
9570 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009571 }
9572
9573 done:
9574 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009575 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009576 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009577 si_shutr(si);
9578 }
9579
9580 /* eat the whole request */
9581 if (co_data(req)) {
9582 req_htx = htx_from_buf(&req->buf);
9583 co_htx_skip(req, req_htx, co_data(req));
9584 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009585 }
9586 }
9587
9588 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009589 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009590 return;
9591
9592 error:
9593
9594 /* If we are in HTTP mode, and we are not send any
9595 * data, return a 500 server error in best effort:
9596 * if there is no room available in the buffer,
9597 * just close the connection.
9598 */
9599 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02009600 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009601
9602 channel_erase(res);
9603 res->buf.data = b_data(err);
9604 memcpy(res->buf.area, b_head(err), b_data(err));
9605 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01009606 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009607 }
9608 if (!(strm->flags & SF_ERR_MASK))
9609 strm->flags |= SF_ERR_RESOURCE;
9610 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
9611 goto done;
9612}
9613
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009614static void hlua_applet_http_release(struct appctx *ctx)
9615{
Olivier Houchard3f795f72019-04-17 22:51:06 +02009616 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009617 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009618 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009619 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009620}
9621
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009622/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009623 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009624 *
9625 * This function can fail with an abort() due to an Lua critical error.
9626 * We are in the configuration parsing process of HAProxy, this abort() is
9627 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009628 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009629static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
9630 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009631{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009632 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009633 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009634
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009635 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02009636 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009637 if (!rule->arg.hlua_rule) {
9638 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02009639 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009640 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009641
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009642 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02009643 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
9644 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009645 if (!rule->arg.hlua_rule->args) {
9646 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02009647 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009648 }
9649
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009650 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01009651 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009652
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009653 /* Expect some arguments */
9654 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01009655 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009656 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02009657 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009658 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01009659 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009660 if (!rule->arg.hlua_rule->args[i]) {
9661 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02009662 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009663 }
9664 (*cur_arg)++;
9665 }
9666 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009667
Thierry FOURNIER42148732015-09-02 17:17:33 +02009668 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009669 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009670 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02009671
9672 error:
9673 if (rule->arg.hlua_rule) {
9674 if (rule->arg.hlua_rule->args) {
9675 for (i = 0; i < fcn->nargs; i++)
9676 ha_free(&rule->arg.hlua_rule->args[i]);
9677 ha_free(&rule->arg.hlua_rule->args);
9678 }
9679 ha_free(&rule->arg.hlua_rule);
9680 }
9681 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009682}
9683
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009684static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
9685 struct act_rule *rule, char **err)
9686{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009687 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009688
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01009689 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009690 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01009691 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009692 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01009693 * the call of this analyzer.
9694 */
9695 if (rule->from != ACT_F_HTTP_REQ) {
9696 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
9697 return ACT_RET_PRS_ERR;
9698 }
9699
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009700 /* Memory for the rule. */
9701 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
9702 if (!rule->arg.hlua_rule) {
9703 memprintf(err, "out of memory error");
9704 return ACT_RET_PRS_ERR;
9705 }
9706
9707 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01009708 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009709
9710 /* TODO: later accept arguments. */
9711 rule->arg.hlua_rule->args = NULL;
9712
9713 /* Add applet pointer in the rule. */
9714 rule->applet.obj_type = OBJ_TYPE_APPLET;
9715 rule->applet.name = fcn->name;
9716 rule->applet.init = hlua_applet_http_init;
9717 rule->applet.fct = hlua_applet_http_fct;
9718 rule->applet.release = hlua_applet_http_release;
9719 rule->applet.timeout = hlua_timeout_applet;
9720
9721 return ACT_RET_PRS_OK;
9722}
9723
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009724/* This function is an LUA binding used for registering
9725 * "sample-conv" functions. It expects a converter name used
9726 * in the haproxy configuration file, and an LUA function.
9727 */
9728__LJMP static int hlua_register_action(lua_State *L)
9729{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009730 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009731 const char *name;
9732 int ref;
9733 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009734 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009735 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01009736 struct buffer *trash;
9737 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009738
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009739 /* Initialise the number of expected arguments at 0. */
9740 nargs = 0;
9741
9742 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
9743 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009744
9745 /* First argument : converter name. */
9746 name = MAY_LJMP(luaL_checkstring(L, 1));
9747
9748 /* Second argument : environment. */
9749 if (lua_type(L, 2) != LUA_TTABLE)
9750 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
9751
9752 /* Third argument : lua function. */
9753 ref = MAY_LJMP(hlua_checkfunction(L, 3));
9754
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009755 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009756 if (lua_gettop(L) >= 4)
9757 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
9758
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009759 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009760 lua_pushnil(L);
9761 while (lua_next(L, 2) != 0) {
9762 if (lua_type(L, -1) != LUA_TSTRING)
9763 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
9764
Thierry Fournierf67442e2020-11-28 20:41:07 +01009765 /* Check if action exists */
9766 trash = get_trash_chunk();
9767 chunk_printf(trash, "lua.%s", name);
9768 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
9769 akw = tcp_req_cont_action(trash->area);
9770 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
9771 akw = tcp_res_cont_action(trash->area);
9772 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
9773 akw = action_http_req_custom(trash->area);
9774 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
9775 akw = action_http_res_custom(trash->area);
9776 } else {
9777 akw = NULL;
9778 }
9779 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01009780 fcn = akw->private;
9781 if (fcn->function_ref[hlua_state_id] != -1) {
9782 ha_warning("Trying to register action 'lua.%s' more than once. "
9783 "This will become a hard error in version 2.5.\n", name);
9784 }
9785 fcn->function_ref[hlua_state_id] = ref;
9786
9787 /* pop the environment string. */
9788 lua_pop(L, 1);
9789 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01009790 }
9791
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009792 /* Check required environment. Only accepted "http" or "tcp". */
9793 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02009794 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009795 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009796 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01009797 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009798 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009799 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009800
9801 /* Fill fcn. */
9802 fcn->name = strdup(name);
9803 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009804 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01009805 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009806
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07009807 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009808 fcn->nargs = nargs;
9809
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009810 /* List head */
9811 akl->list.n = akl->list.p = NULL;
9812
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009813 /* action keyword. */
9814 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02009815 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009816 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009817 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009818
9819 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
9820
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02009821 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009822 akl->kw[0].private = fcn;
9823 akl->kw[0].parse = action_register_lua;
9824
9825 /* select the action registering point. */
9826 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
9827 tcp_req_cont_keywords_register(akl);
9828 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
9829 tcp_res_cont_keywords_register(akl);
9830 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
9831 http_req_keywords_register(akl);
9832 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
9833 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009834 else {
9835 release_hlua_function(fcn);
9836 if (akl)
9837 ha_free((char **)&(akl->kw[0].kw));
9838 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01009839 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009840 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
9841 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009842 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009843
9844 /* pop the environment string. */
9845 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009846
9847 /* reset for next loop */
9848 akl = NULL;
9849 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009850 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009851 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009852
9853 alloc_error:
9854 release_hlua_function(fcn);
9855 ha_free(&akl);
9856 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
9857 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009858}
9859
9860static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
9861 struct act_rule *rule, char **err)
9862{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009863 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009864
Christopher Faulet280f85b2019-07-15 15:02:04 +02009865 if (px->mode == PR_MODE_HTTP) {
9866 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01009867 return ACT_RET_PRS_ERR;
9868 }
9869
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009870 /* Memory for the rule. */
9871 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
9872 if (!rule->arg.hlua_rule) {
9873 memprintf(err, "out of memory error");
9874 return ACT_RET_PRS_ERR;
9875 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009876
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009877 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01009878 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009879
9880 /* TODO: later accept arguments. */
9881 rule->arg.hlua_rule->args = NULL;
9882
9883 /* Add applet pointer in the rule. */
9884 rule->applet.obj_type = OBJ_TYPE_APPLET;
9885 rule->applet.name = fcn->name;
9886 rule->applet.init = hlua_applet_tcp_init;
9887 rule->applet.fct = hlua_applet_tcp_fct;
9888 rule->applet.release = hlua_applet_tcp_release;
9889 rule->applet.timeout = hlua_timeout_applet;
9890
9891 return 0;
9892}
9893
9894/* This function is an LUA binding used for registering
9895 * "sample-conv" functions. It expects a converter name used
9896 * in the haproxy configuration file, and an LUA function.
9897 */
9898__LJMP static int hlua_register_service(lua_State *L)
9899{
9900 struct action_kw_list *akl;
9901 const char *name;
9902 const char *env;
9903 int ref;
9904 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02009905 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01009906 struct buffer *trash;
9907 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009908
9909 MAY_LJMP(check_args(L, 3, "register_service"));
9910
9911 /* First argument : converter name. */
9912 name = MAY_LJMP(luaL_checkstring(L, 1));
9913
9914 /* Second argument : environment. */
9915 env = MAY_LJMP(luaL_checkstring(L, 2));
9916
9917 /* Third argument : lua function. */
9918 ref = MAY_LJMP(hlua_checkfunction(L, 3));
9919
Thierry Fournierf67442e2020-11-28 20:41:07 +01009920 /* Check for service already registered */
9921 trash = get_trash_chunk();
9922 chunk_printf(trash, "lua.%s", name);
9923 akw = service_find(trash->area);
9924 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01009925 fcn = akw->private;
9926 if (fcn->function_ref[hlua_state_id] != -1) {
9927 ha_warning("Trying to register service 'lua.%s' more than once. "
9928 "This will become a hard error in version 2.5.\n", name);
9929 }
9930 fcn->function_ref[hlua_state_id] = ref;
9931 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01009932 }
9933
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009934 /* Allocate and fill the sample fetch keyword struct. */
9935 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
9936 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02009937 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01009938 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009939 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02009940 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009941
9942 /* Fill fcn. */
9943 len = strlen("<lua.>") + strlen(name) + 1;
9944 fcn->name = calloc(1, len);
9945 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02009946 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009947 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01009948 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009949
9950 /* List head */
9951 akl->list.n = akl->list.p = NULL;
9952
9953 /* converter keyword. */
9954 len = strlen("lua.") + strlen(name) + 1;
9955 akl->kw[0].kw = calloc(1, len);
9956 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02009957 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009958
9959 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
9960
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01009961 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009962 if (strcmp(env, "tcp") == 0)
9963 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009964 else if (strcmp(env, "http") == 0)
9965 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02009966 else {
9967 release_hlua_function(fcn);
9968 if (akl)
9969 ha_free((char **)&(akl->kw[0].kw));
9970 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01009971 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01009972 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02009973 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009974
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02009975 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009976 akl->kw[0].private = fcn;
9977
9978 /* End of array. */
9979 memset(&akl->kw[1], 0, sizeof(*akl->kw));
9980
9981 /* Register this new converter */
9982 service_keywords_register(akl);
9983
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009984 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02009985
9986 alloc_error:
9987 release_hlua_function(fcn);
9988 ha_free(&akl);
9989 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
9990 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009991}
9992
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01009993/* This function initialises Lua cli handler. It copies the
9994 * arguments in the Lua stack and create channel IO objects.
9995 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009996static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01009997{
9998 struct hlua *hlua;
9999 struct hlua_function *fcn;
10000 int i;
10001 const char *error;
10002
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010003 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010004 appctx->ctx.hlua_cli.fcn = private;
10005
Willy Tarreaubafbe012017-11-24 17:34:44 +010010006 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010007 if (!hlua) {
10008 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +010010009 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010010 }
10011 HLUA_INIT(hlua);
10012 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010013
10014 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +050010015 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010016 * applet_http. It is absolutely compatible.
10017 */
Willy Tarreaubeeabf52021-10-01 18:23:30 +020010018 appctx->ctx.hlua_cli.task = task_new_here();
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010019 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +010010020 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +010010021 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010022 }
10023 appctx->ctx.hlua_cli.task->nice = 0;
10024 appctx->ctx.hlua_cli.task->context = appctx;
10025 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
10026
10027 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +010010028 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 +010010029 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +010010030 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010031 }
10032
10033 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +010010034 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010035 if (lua_type(hlua->T, -1) == LUA_TSTRING)
10036 error = lua_tostring(hlua->T, -1);
10037 else
10038 error = "critical error";
10039 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
10040 goto error;
10041 }
10042
10043 /* Check stack available size. */
10044 if (!lua_checkstack(hlua->T, 2)) {
10045 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
10046 goto error;
10047 }
10048
10049 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +010010050 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010051
10052 /* Once the arguments parsed, the CLI is like an AppletTCP,
10053 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010054 */
10055 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
10056 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
10057 goto error;
10058 }
10059 hlua->nargs = 1;
10060
10061 /* push keywords in the stack. */
10062 for (i = 0; *args[i]; i++) {
10063 /* Check stack available size. */
10064 if (!lua_checkstack(hlua->T, 1)) {
10065 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
10066 goto error;
10067 }
10068 lua_pushstring(hlua->T, args[i]);
10069 hlua->nargs++;
10070 }
10071
10072 /* We must initialize the execution timeouts. */
10073 hlua->max_time = hlua_timeout_session;
10074
10075 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +010010076 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010077
10078 /* It's ok */
10079 return 0;
10080
10081 /* It's not ok. */
10082error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +010010083 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010084 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +010010085 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010086 return 1;
10087}
10088
10089static int hlua_cli_io_handler_fct(struct appctx *appctx)
10090{
10091 struct hlua *hlua;
10092 struct stream_interface *si;
10093 struct hlua_function *fcn;
10094
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010095 hlua = appctx->ctx.hlua_cli.hlua;
Christopher Faulet86e1c332021-12-20 17:09:39 +010010096 si = cs_si(appctx->owner);
Willy Tarreau8ae4f752016-12-14 15:41:45 +010010097 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010098
10099 /* If the stream is disconnect or closed, ldo nothing. */
10100 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
10101 return 1;
10102
10103 /* Execute the function. */
10104 switch (hlua_ctx_resume(hlua, 1)) {
10105
10106 /* finished. */
10107 case HLUA_E_OK:
10108 return 1;
10109
10110 /* yield. */
10111 case HLUA_E_AGAIN:
10112 /* We want write. */
10113 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +010010114 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010115 /* Set the timeout. */
10116 if (hlua->wake_time != TICK_ETERNITY)
10117 task_schedule(hlua->task, hlua->wake_time);
10118 return 0;
10119
10120 /* finished with error. */
10121 case HLUA_E_ERRMSG:
10122 /* Display log. */
10123 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
10124 fcn->name, lua_tostring(hlua->T, -1));
10125 lua_pop(hlua->T, 1);
10126 return 1;
10127
Thierry Fournierd5b073c2018-05-21 19:42:47 +020010128 case HLUA_E_ETMOUT:
10129 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
10130 fcn->name);
10131 return 1;
10132
10133 case HLUA_E_NOMEM:
10134 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
10135 fcn->name);
10136 return 1;
10137
10138 case HLUA_E_YIELD: /* unexpected */
10139 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
10140 fcn->name);
10141 return 1;
10142
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010143 case HLUA_E_ERR:
10144 /* Display log. */
10145 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
10146 fcn->name);
10147 return 1;
10148
10149 default:
10150 return 1;
10151 }
10152
10153 return 1;
10154}
10155
10156static void hlua_cli_io_release_fct(struct appctx *appctx)
10157{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010158 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010159 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010160}
10161
10162/* This function is an LUA binding used for registering
10163 * new keywords in the cli. It expects a list of keywords
10164 * which are the "path". It is limited to 5 keywords. A
10165 * description of the command, a function to be executed
10166 * for the parsing and a function for io handlers.
10167 */
10168__LJMP static int hlua_register_cli(lua_State *L)
10169{
10170 struct cli_kw_list *cli_kws;
10171 const char *message;
10172 int ref_io;
10173 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010174 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010175 int index;
10176 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +010010177 struct buffer *trash;
10178 const char *kw[5];
10179 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010180 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010181
10182 MAY_LJMP(check_args(L, 3, "register_cli"));
10183
10184 /* First argument : an array of maximum 5 keywords. */
10185 if (!lua_istable(L, 1))
10186 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
10187
10188 /* Second argument : string with contextual message. */
10189 message = MAY_LJMP(luaL_checkstring(L, 2));
10190
10191 /* Third and fourth argument : lua function. */
10192 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
10193
Thierry Fournierf67442e2020-11-28 20:41:07 +010010194 /* Check for CLI service already registered */
10195 trash = get_trash_chunk();
10196 index = 0;
10197 lua_pushnil(L);
10198 memset(kw, 0, sizeof(kw));
10199 while (lua_next(L, 1) != 0) {
10200 if (index >= CLI_PREFIX_KW_NB)
10201 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
10202 if (lua_type(L, -1) != LUA_TSTRING)
10203 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
10204 kw[index] = lua_tostring(L, -1);
10205 if (index == 0)
10206 chunk_printf(trash, "%s", kw[index]);
10207 else
10208 chunk_appendf(trash, " %s", kw[index]);
10209 index++;
10210 lua_pop(L, 1);
10211 }
10212 cli_kw = cli_find_kw_exact((char **)kw);
10213 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +010010214 fcn = cli_kw->private;
10215 if (fcn->function_ref[hlua_state_id] != -1) {
10216 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
10217 "This will become a hard error in version 2.5.\n", trash->area);
10218 }
10219 fcn->function_ref[hlua_state_id] = ref_io;
10220 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +010010221 }
10222
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010223 /* Allocate and fill the sample fetch keyword struct. */
10224 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010225 if (!cli_kws) {
10226 errmsg = "Lua out of memory error.";
10227 goto error;
10228 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +010010229 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010230 if (!fcn) {
10231 errmsg = "Lua out of memory error.";
10232 goto error;
10233 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010234
10235 /* Fill path. */
10236 index = 0;
10237 lua_pushnil(L);
10238 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010239 if (index >= 5) {
10240 errmsg = "1st argument must be a table with a maximum of 5 entries";
10241 goto error;
10242 }
10243 if (lua_type(L, -1) != LUA_TSTRING) {
10244 errmsg = "1st argument must be a table filled with strings";
10245 goto error;
10246 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010247 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010248 if (!cli_kws->kw[0].str_kw[index]) {
10249 errmsg = "Lua out of memory error.";
10250 goto error;
10251 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010252 index++;
10253 lua_pop(L, 1);
10254 }
10255
10256 /* Copy help message. */
10257 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010258 if (!cli_kws->kw[0].usage) {
10259 errmsg = "Lua out of memory error.";
10260 goto error;
10261 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010262
10263 /* Fill fcn io handler. */
10264 len = strlen("<lua.cli>") + 1;
10265 for (i = 0; i < index; i++)
10266 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
10267 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010268 if (!fcn->name) {
10269 errmsg = "Lua out of memory error.";
10270 goto error;
10271 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010272 strncat((char *)fcn->name, "<lua.cli", len);
10273 for (i = 0; i < index; i++) {
10274 strncat((char *)fcn->name, ".", len);
10275 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
10276 }
10277 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +010010278 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010279
10280 /* Fill last entries. */
10281 cli_kws->kw[0].private = fcn;
10282 cli_kws->kw[0].parse = hlua_cli_parse_fct;
10283 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
10284 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
10285
10286 /* Register this new converter */
10287 cli_register_kw(cli_kws);
10288
10289 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010290
10291 error:
10292 release_hlua_function(fcn);
10293 if (cli_kws) {
10294 for (i = 0; i < index; i++)
10295 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
10296 ha_free((char **)&(cli_kws->kw[0].usage));
10297 }
10298 ha_free(&cli_kws);
10299 WILL_LJMP(luaL_error(L, errmsg));
10300 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010301}
10302
Christopher Faulet69c581a2021-05-31 08:54:04 +020010303static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf)
10304{
10305 struct hlua_flt_config *conf = fconf->conf;
10306 lua_State *L;
10307 int error, pos, state_id, flt_ref;
10308
10309 state_id = reg_flt_to_stack_id(conf->reg);
10310 L = hlua_states[state_id];
10311 pos = lua_gettop(L);
10312
10313 /* The filter parsing function */
10314 lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]);
10315
10316 /* Push the filter class on the stack and resolve all callbacks */
10317 lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]);
10318
10319 /* Duplicate the filter class so each filter will have its own copy */
10320 lua_newtable(L);
10321 lua_pushnil(L);
10322
10323 while (lua_next(L, pos+2)) {
10324 lua_pushvalue(L, -2);
10325 lua_insert(L, -2);
10326 lua_settable(L, -4);
10327 }
10328 flt_ref = luaL_ref(L, LUA_REGISTRYINDEX);
10329
10330 /* Remove the original lua filter class from the stack */
10331 lua_pop(L, 1);
10332
10333 /* Push the copy on the stack */
10334 lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref);
10335
10336 /* extra args are pushed in a table */
10337 lua_newtable(L);
10338 for (pos = 0; conf->args[pos]; pos++) {
10339 /* Check stack available size. */
10340 if (!lua_checkstack(L, 1)) {
10341 ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name);
10342 goto error;
10343 }
10344 lua_pushstring(L, conf->args[pos]);
10345 lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
10346 }
10347
10348 error = lua_pcall(L, 2, LUA_MULTRET, 0);
10349 switch (error) {
10350 case LUA_OK:
10351 /* replace the filter ref */
10352 conf->ref[state_id] = flt_ref;
10353 break;
10354 case LUA_ERRRUN:
10355 ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1));
10356 goto error;
10357 case LUA_ERRMEM:
10358 ha_alert("Lua filter '%s' : out of memory error", conf->reg->name);
10359 goto error;
10360 case LUA_ERRERR:
10361 ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1));
10362 goto error;
10363#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
10364 case LUA_ERRGCMM:
10365 ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1));
10366 goto error;
10367#endif
10368 default:
Ilya Shipitsinff0f2782021-08-22 22:18:07 +050010369 ha_alert("Lua filter '%s' : unknown error : %s", conf->reg->name, lua_tostring(L, -1));
Christopher Faulet69c581a2021-05-31 08:54:04 +020010370 goto error;
10371 }
10372
10373 lua_settop(L, 0);
10374 return 0;
10375
10376 error:
10377 lua_settop(L, 0);
10378 return -1;
10379}
10380
10381static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
10382{
10383 struct hlua_flt_config *conf = fconf->conf;
10384 lua_State *L;
10385 int state_id;
10386
10387 if (!conf)
10388 return;
10389
10390 state_id = reg_flt_to_stack_id(conf->reg);
10391 L = hlua_states[state_id];
10392 luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]);
10393}
10394
10395static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf)
10396{
10397 struct hlua_flt_config *conf = fconf->conf;
10398 int state_id = reg_flt_to_stack_id(conf->reg);
10399
10400 /* Rely on per-thread init for global scripts */
10401 if (!state_id)
10402 return hlua_filter_init_per_thread(px, fconf);
10403 return 0;
10404}
10405
10406static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf)
10407{
10408
10409 if (fconf->conf) {
10410 struct hlua_flt_config *conf = fconf->conf;
10411 int state_id = reg_flt_to_stack_id(conf->reg);
10412 int pos;
10413
10414 /* Rely on per-thread deinit for global scripts */
10415 if (!state_id)
10416 hlua_filter_deinit_per_thread(px, fconf);
10417
10418 for (pos = 0; conf->args[pos]; pos++)
10419 free(conf->args[pos]);
10420 free(conf->args);
10421 }
10422 ha_free(&fconf->conf);
10423 ha_free((char **)&fconf->id);
10424 ha_free(&fconf->ops);
10425}
10426
10427static int hlua_filter_new(struct stream *s, struct filter *filter)
10428{
10429 struct hlua_flt_config *conf = FLT_CONF(filter);
10430 struct hlua_flt_ctx *flt_ctx = NULL;
10431 int ret = 1;
10432
10433 /* In the execution wrappers linked with a stream, the
10434 * Lua context can be not initialized. This behavior
10435 * permits to save performances because a systematic
10436 * Lua initialization cause 5% performances loss.
10437 */
10438 if (!s->hlua) {
10439 struct hlua *hlua;
10440
10441 hlua = pool_alloc(pool_head_hlua);
10442 if (!hlua) {
10443 SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n",
10444 conf->reg->name);
10445 ret = 0;
10446 goto end;
10447 }
10448 HLUA_INIT(hlua);
10449 s->hlua = hlua;
10450 if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) {
10451 SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n",
10452 conf->reg->name);
10453 ret = 0;
10454 goto end;
10455 }
10456 }
10457
10458 flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx);
10459 if (!flt_ctx) {
10460 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
10461 conf->reg->name);
10462 ret = 0;
10463 goto end;
10464 }
10465 flt_ctx->hlua[0] = pool_alloc(pool_head_hlua);
10466 flt_ctx->hlua[1] = pool_alloc(pool_head_hlua);
10467 if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) {
10468 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
10469 conf->reg->name);
10470 ret = 0;
10471 goto end;
10472 }
10473 HLUA_INIT(flt_ctx->hlua[0]);
10474 HLUA_INIT(flt_ctx->hlua[1]);
10475 if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) ||
10476 !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) {
10477 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
10478 conf->reg->name);
10479 ret = 0;
10480 goto end;
10481 }
10482
10483 if (!HLUA_IS_RUNNING(s->hlua)) {
10484 /* The following Lua calls can fail. */
10485 if (!SET_SAFE_LJMP(s->hlua)) {
10486 const char *error;
10487
10488 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
10489 error = lua_tostring(s->hlua->T, -1);
10490 else
10491 error = "critical error";
10492 SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error);
10493 ret = 0;
10494 goto end;
10495 }
10496
10497 /* Check stack size. */
10498 if (!lua_checkstack(s->hlua->T, 1)) {
10499 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
Tim Duesterhus22817382021-09-11 23:17:25 +020010500 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet69c581a2021-05-31 08:54:04 +020010501 ret = 0;
10502 goto end;
10503 }
10504
10505 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]);
10506 if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) {
10507 SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n",
10508 conf->reg->name);
10509 RESET_SAFE_LJMP(s->hlua);
10510 ret = 0;
10511 goto end;
10512 }
10513 lua_insert(s->hlua->T, -2);
10514
10515 /* Push the copy on the stack */
10516 s->hlua->nargs = 1;
10517
10518 /* We must initialize the execution timeouts. */
10519 s->hlua->max_time = hlua_timeout_session;
10520
10521 /* At this point the execution is safe. */
10522 RESET_SAFE_LJMP(s->hlua);
10523 }
10524
10525 switch (hlua_ctx_resume(s->hlua, 0)) {
10526 case HLUA_E_OK:
10527 /* Nothing returned or not a table, ignore the filter for current stream */
10528 if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) {
10529 ret = 0;
10530 goto end;
10531 }
10532
10533 /* Attached the filter pointer to the ctx */
10534 lua_pushstring(s->hlua->T, "__filter");
10535 lua_pushlightuserdata(s->hlua->T, filter);
10536 lua_settable(s->hlua->T, -3);
10537
10538 /* Save a ref on the filter ctx */
10539 lua_pushvalue(s->hlua->T, 1);
10540 flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX);
10541 filter->ctx = flt_ctx;
10542 break;
10543 case HLUA_E_ERRMSG:
10544 SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1));
10545 ret = -1;
10546 goto end;
10547 case HLUA_E_ETMOUT:
10548 SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name);
10549 ret = 0;
10550 goto end;
10551 case HLUA_E_NOMEM:
10552 SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name);
10553 ret = 0;
10554 goto end;
10555 case HLUA_E_AGAIN:
10556 case HLUA_E_YIELD:
10557 SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()"
10558 " are not allowed from 'new' function.\n", conf->reg->name);
10559 ret = 0;
10560 goto end;
10561 case HLUA_E_ERR:
10562 SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name);
10563 ret = 0;
10564 goto end;
10565 default:
10566 ret = 0;
10567 goto end;
10568 }
10569
10570 end:
10571 if (s->hlua)
10572 lua_settop(s->hlua->T, 0);
10573 if (ret <= 0) {
10574 if (flt_ctx) {
10575 hlua_ctx_destroy(flt_ctx->hlua[0]);
10576 hlua_ctx_destroy(flt_ctx->hlua[1]);
10577 pool_free(pool_head_hlua_flt_ctx, flt_ctx);
10578 }
10579 }
10580 return ret;
10581}
10582
10583static void hlua_filter_delete(struct stream *s, struct filter *filter)
10584{
10585 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10586
10587 luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref);
10588 hlua_ctx_destroy(flt_ctx->hlua[0]);
10589 hlua_ctx_destroy(flt_ctx->hlua[1]);
10590 pool_free(pool_head_hlua_flt_ctx, flt_ctx);
10591 filter->ctx = NULL;
10592}
10593
Christopher Faulet9f55a502020-02-25 15:21:02 +010010594static int hlua_filter_from_payload(struct filter *filter)
10595{
10596 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10597
10598 return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD));
10599}
10600
Christopher Fauletc404f112020-02-26 15:03:09 +010010601static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun,
10602 int dir, unsigned int flags)
10603{
10604 struct hlua *flt_hlua;
10605 struct hlua_flt_config *conf = FLT_CONF(filter);
10606 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10607 unsigned int hflags = HLUA_TXN_FLT_CTX;
10608 int ret = 1;
10609
10610 flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)];
10611 if (!flt_hlua)
10612 goto end;
10613
10614 if (!HLUA_IS_RUNNING(flt_hlua)) {
10615 int extra_idx = lua_gettop(flt_hlua->T);
10616
10617 /* The following Lua calls can fail. */
10618 if (!SET_SAFE_LJMP(flt_hlua)) {
10619 const char *error;
10620
10621 if (lua_type(flt_hlua->T, -1) == LUA_TSTRING)
10622 error = lua_tostring(flt_hlua->T, -1);
10623 else
10624 error = "critical error";
10625 SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error);
10626 goto end;
10627 }
10628
10629 /* Check stack size. */
10630 if (!lua_checkstack(flt_hlua->T, 3)) {
10631 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
10632 RESET_SAFE_LJMP(flt_hlua);
10633 goto end;
10634 }
10635
10636 lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref);
10637 if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) {
10638 RESET_SAFE_LJMP(flt_hlua);
10639 goto end;
10640 }
10641 lua_insert(flt_hlua->T, -2);
10642
10643 if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) {
10644 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
10645 RESET_SAFE_LJMP(flt_hlua);
10646 goto end;
10647 }
10648 flt_hlua->nargs = 2;
10649
10650 if (flags & HLUA_FLT_CB_ARG_CHN) {
10651 if (dir == SMP_OPT_DIR_REQ)
10652 lua_getfield(flt_hlua->T, -1, "req");
10653 else
10654 lua_getfield(flt_hlua->T, -1, "res");
10655 if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) {
10656 lua_pushstring(flt_hlua->T, "__filter");
10657 lua_pushlightuserdata(flt_hlua->T, filter);
10658 lua_settable(flt_hlua->T, -3);
10659 }
10660 flt_hlua->nargs++;
10661 }
10662 else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) {
Christopher Fauleteae8afa2020-02-26 17:15:48 +010010663 if (dir == SMP_OPT_DIR_REQ)
10664 lua_getfield(flt_hlua->T, -1, "http_req");
10665 else
10666 lua_getfield(flt_hlua->T, -1, "http_res");
10667 if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) {
10668 lua_pushstring(flt_hlua->T, "__filter");
10669 lua_pushlightuserdata(flt_hlua->T, filter);
10670 lua_settable(flt_hlua->T, -3);
10671 }
10672 flt_hlua->nargs++;
Christopher Fauletc404f112020-02-26 15:03:09 +010010673 }
10674
10675 /* Check stack size. */
10676 if (!lua_checkstack(flt_hlua->T, 1)) {
10677 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
10678 RESET_SAFE_LJMP(flt_hlua);
10679 goto end;
10680 }
10681
10682 while (extra_idx--) {
10683 lua_pushvalue(flt_hlua->T, 1);
10684 lua_remove(flt_hlua->T, 1);
10685 flt_hlua->nargs++;
10686 }
10687
10688 /* We must initialize the execution timeouts. */
10689 flt_hlua->max_time = hlua_timeout_session;
10690
10691 /* At this point the execution is safe. */
10692 RESET_SAFE_LJMP(flt_hlua);
10693 }
10694
10695 switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) {
10696 case HLUA_E_OK:
10697 /* Catch the return value if it required */
10698 if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) {
10699 ret = lua_tointeger(flt_hlua->T, -1);
10700 lua_settop(flt_hlua->T, 0); /* Empty the stack. */
10701 }
10702
10703 /* Set timeout in the required channel. */
10704 if (flt_hlua->wake_time != TICK_ETERNITY) {
10705 if (dir == SMP_OPT_DIR_REQ)
10706 s->req.analyse_exp = flt_hlua->wake_time;
10707 else
10708 s->res.analyse_exp = flt_hlua->wake_time;
10709 }
10710 break;
10711 case HLUA_E_AGAIN:
10712 /* Set timeout in the required channel. */
10713 if (flt_hlua->wake_time != TICK_ETERNITY) {
10714 if (dir == SMP_OPT_DIR_REQ)
10715 s->req.analyse_exp = flt_hlua->wake_time;
10716 else
10717 s->res.analyse_exp = flt_hlua->wake_time;
10718 }
10719 /* Some actions can be wake up when a "write" event
10720 * is detected on a response channel. This is useful
10721 * only for actions targeted on the requests.
10722 */
10723 if (HLUA_IS_WAKERESWR(flt_hlua))
10724 s->res.flags |= CF_WAKE_WRITE;
10725 if (HLUA_IS_WAKEREQWR(flt_hlua))
10726 s->req.flags |= CF_WAKE_WRITE;
10727 ret = 0;
10728 goto end;
10729 case HLUA_E_ERRMSG:
10730 SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1));
10731 ret = -1;
10732 goto end;
10733 case HLUA_E_ETMOUT:
10734 SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun);
10735 goto end;
10736 case HLUA_E_NOMEM:
10737 SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name);
10738 goto end;
10739 case HLUA_E_YIELD:
10740 SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()"
10741 " are not allowed from '%s' callback.\n", conf->reg->name, fun);
10742 goto end;
10743 case HLUA_E_ERR:
10744 SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun);
10745 goto end;
10746 default:
10747 goto end;
10748 }
10749
10750
10751 end:
10752 return ret;
10753}
10754
10755static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
10756{
10757 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10758
10759 flt_ctx->flags = 0;
10760 return hlua_filter_callback(s, filter, "start_analyze",
10761 (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES),
10762 (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN));
10763}
10764
10765static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
10766{
10767 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10768
10769 flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD;
10770 return hlua_filter_callback(s, filter, "end_analyze",
10771 (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES),
10772 (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN));
10773}
10774
Christopher Fauleteae8afa2020-02-26 17:15:48 +010010775static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
10776{
10777 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10778
10779 flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD;
10780 return hlua_filter_callback(s, filter, "http_headers",
10781 (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES),
10782 (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG));
10783}
10784
10785static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
10786 unsigned int offset, unsigned int len)
10787{
10788 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10789 struct hlua *flt_hlua;
10790 int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
10791 int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1);
10792 int ret;
10793
10794 flt_hlua = flt_ctx->hlua[idx];
10795 flt_ctx->cur_off[idx] = offset;
10796 flt_ctx->cur_len[idx] = len;
10797 flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD;
10798 ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG));
10799 if (ret != -1) {
10800 ret = flt_ctx->cur_len[idx];
10801 if (lua_gettop(flt_hlua->T) > 0) {
10802 ret = lua_tointeger(flt_hlua->T, -1);
10803 if (ret > flt_ctx->cur_len[idx])
10804 ret = flt_ctx->cur_len[idx];
10805 lua_settop(flt_hlua->T, 0); /* Empty the stack. */
10806 }
10807 }
10808 return ret;
10809}
10810
10811static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg)
10812{
10813 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10814
10815 flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD;
10816 return hlua_filter_callback(s, filter, "http_end",
10817 (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES),
10818 (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG));
10819}
10820
Christopher Fauletc404f112020-02-26 15:03:09 +010010821static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn,
10822 unsigned int offset, unsigned int len)
10823{
10824 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10825 struct hlua *flt_hlua;
10826 int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
10827 int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1);
10828 int ret;
10829
10830 flt_hlua = flt_ctx->hlua[idx];
10831 flt_ctx->cur_off[idx] = offset;
10832 flt_ctx->cur_len[idx] = len;
10833 flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD;
10834 ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN));
10835 if (ret != -1) {
10836 ret = flt_ctx->cur_len[idx];
10837 if (lua_gettop(flt_hlua->T) > 0) {
10838 ret = lua_tointeger(flt_hlua->T, -1);
10839 if (ret > flt_ctx->cur_len[idx])
10840 ret = flt_ctx->cur_len[idx];
10841 lua_settop(flt_hlua->T, 0); /* Empty the stack. */
10842 }
10843 }
10844 return ret;
10845}
10846
Christopher Faulet69c581a2021-05-31 08:54:04 +020010847static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px,
10848 struct flt_conf *fconf, char **err, void *private)
10849{
10850 struct hlua_reg_filter *reg_flt = private;
10851 lua_State *L;
10852 struct hlua_flt_config *conf = NULL;
10853 const char *flt_id = NULL;
10854 int state_id, pos, flt_flags = 0;
10855 struct flt_ops *hlua_flt_ops = NULL;
10856
10857 state_id = reg_flt_to_stack_id(reg_flt);
10858 L = hlua_states[state_id];
10859
10860 /* Initialize the filter ops with default callbacks */
10861 hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops));
Christopher Fauletc86bb872021-08-13 08:33:57 +020010862 if (!hlua_flt_ops)
10863 goto error;
Christopher Faulet69c581a2021-05-31 08:54:04 +020010864 hlua_flt_ops->init = hlua_filter_init;
10865 hlua_flt_ops->deinit = hlua_filter_deinit;
10866 if (state_id) {
10867 /* Set per-thread callback if script is loaded per-thread */
10868 hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread;
10869 hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread;
10870 }
10871 hlua_flt_ops->attach = hlua_filter_new;
10872 hlua_flt_ops->detach = hlua_filter_delete;
10873
10874 /* Push the filter class on the stack and resolve all callbacks */
10875 lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]);
10876
Christopher Fauletc404f112020-02-26 15:03:09 +010010877 if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION)
10878 hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze;
10879 lua_pop(L, 1);
10880 if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION)
10881 hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze;
10882 lua_pop(L, 1);
Christopher Fauleteae8afa2020-02-26 17:15:48 +010010883 if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION)
10884 hlua_flt_ops->http_headers = hlua_filter_http_headers;
10885 lua_pop(L, 1);
10886 if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION)
10887 hlua_flt_ops->http_payload = hlua_filter_http_payload;
10888 lua_pop(L, 1);
10889 if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION)
10890 hlua_flt_ops->http_end = hlua_filter_http_end;
10891 lua_pop(L, 1);
Christopher Fauletc404f112020-02-26 15:03:09 +010010892 if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION)
10893 hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload;
10894 lua_pop(L, 1);
Christopher Faulet69c581a2021-05-31 08:54:04 +020010895
10896 /* Get id and flags of the filter class */
10897 if (lua_getfield(L, -1, "id") == LUA_TSTRING)
10898 flt_id = lua_tostring(L, -1);
10899 lua_pop(L, 1);
10900 if (lua_getfield(L, -1, "flags") == LUA_TNUMBER)
10901 flt_flags = lua_tointeger(L, -1);
10902 lua_pop(L, 1);
10903
10904 /* Create the filter config */
10905 conf = calloc(1, sizeof(*conf));
Christopher Fauletc86bb872021-08-13 08:33:57 +020010906 if (!conf)
Christopher Faulet69c581a2021-05-31 08:54:04 +020010907 goto error;
Christopher Faulet69c581a2021-05-31 08:54:04 +020010908 conf->reg = reg_flt;
10909
10910 /* duplicate args */
10911 for (pos = 0; *args[*cur_arg + 1 + pos]; pos++);
10912 conf->args = calloc(pos + 1, sizeof(*conf->args));
Christopher Fauletc86bb872021-08-13 08:33:57 +020010913 if (!conf->args)
10914 goto error;
10915 for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) {
Christopher Faulet69c581a2021-05-31 08:54:04 +020010916 conf->args[pos] = strdup(args[*cur_arg + 1 + pos]);
Christopher Fauletc86bb872021-08-13 08:33:57 +020010917 if (!conf->args[pos])
10918 goto error;
10919 }
Christopher Faulet69c581a2021-05-31 08:54:04 +020010920 conf->args[pos] = NULL;
10921 *cur_arg += pos + 1;
10922
Christopher Fauletc86bb872021-08-13 08:33:57 +020010923 if (flt_id) {
10924 fconf->id = strdup(flt_id);
10925 if (!fconf->id)
10926 goto error;
10927 }
Christopher Faulet69c581a2021-05-31 08:54:04 +020010928 fconf->flags = flt_flags;
10929 fconf->conf = conf;
10930 fconf->ops = hlua_flt_ops;
10931
10932 lua_settop(L, 0);
10933 return 0;
10934
10935 error:
Christopher Fauletc86bb872021-08-13 08:33:57 +020010936 memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
Christopher Faulet69c581a2021-05-31 08:54:04 +020010937 free(hlua_flt_ops);
Christopher Fauletc86bb872021-08-13 08:33:57 +020010938 if (conf && conf->args) {
10939 for (pos = 0; conf->args[pos]; pos++)
10940 free(conf->args[pos]);
10941 free(conf->args);
10942 }
Christopher Faulet69c581a2021-05-31 08:54:04 +020010943 free(conf);
Christopher Fauletc86bb872021-08-13 08:33:57 +020010944 free((char *)fconf->id);
Christopher Faulet69c581a2021-05-31 08:54:04 +020010945 lua_settop(L, 0);
10946 return -1;
10947}
10948
Christopher Fauletc404f112020-02-26 15:03:09 +010010949__LJMP static int hlua_register_data_filter(lua_State *L)
10950{
10951 struct filter *filter;
10952 struct channel *chn;
10953
10954 MAY_LJMP(check_args(L, 2, "register_data_filter"));
10955 MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE));
10956 chn = MAY_LJMP(hlua_checkchannel(L, 2));
10957
10958 lua_getfield(L, 1, "__filter");
10959 MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA));
10960 filter = lua_touserdata (L, -1);
10961 lua_pop(L, 1);
10962
10963 register_data_filter(chn_strm(chn), chn, filter);
10964 return 1;
10965}
10966
10967__LJMP static int hlua_unregister_data_filter(lua_State *L)
10968{
10969 struct filter *filter;
10970 struct channel *chn;
10971
10972 MAY_LJMP(check_args(L, 2, "unregister_data_filter"));
10973 MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE));
10974 chn = MAY_LJMP(hlua_checkchannel(L, 2));
10975
10976 lua_getfield(L, 1, "__filter");
10977 MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA));
10978 filter = lua_touserdata (L, -1);
10979 lua_pop(L, 1);
10980
10981 unregister_data_filter(chn_strm(chn), chn, filter);
10982 return 1;
10983}
10984
Christopher Faulet69c581a2021-05-31 08:54:04 +020010985/* This function is an LUA binding used for registering a filter. It expects a
Ilya Shipitsinff0f2782021-08-22 22:18:07 +050010986 * filter name used in the haproxy configuration file and a LUA function to
Christopher Faulet69c581a2021-05-31 08:54:04 +020010987 * parse configuration arguments.
10988 */
10989__LJMP static int hlua_register_filter(lua_State *L)
10990{
10991 struct buffer *trash;
10992 struct flt_kw_list *fkl;
10993 struct flt_kw *fkw;
10994 const char *name;
10995 struct hlua_reg_filter *reg_flt= NULL;
10996 int flt_ref, fun_ref;
10997 int len;
10998
10999 MAY_LJMP(check_args(L, 3, "register_filter"));
11000
11001 /* First argument : filter name. */
11002 name = MAY_LJMP(luaL_checkstring(L, 1));
11003
11004 /* Second argument : The filter class */
11005 flt_ref = MAY_LJMP(hlua_checktable(L, 2));
11006
11007 /* Third argument : lua function. */
11008 fun_ref = MAY_LJMP(hlua_checkfunction(L, 3));
11009
11010 trash = get_trash_chunk();
11011 chunk_printf(trash, "lua.%s", name);
11012 fkw = flt_find_kw(trash->area);
11013 if (fkw != NULL) {
11014 reg_flt = fkw->private;
11015 if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) {
11016 ha_warning("Trying to register filter 'lua.%s' more than once. "
11017 "This will become a hard error in version 2.5.\n", name);
11018 }
11019 reg_flt->flt_ref[hlua_state_id] = flt_ref;
11020 reg_flt->fun_ref[hlua_state_id] = fun_ref;
11021 return 0;
11022 }
11023
11024 fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2);
11025 if (!fkl)
11026 goto alloc_error;
11027 fkl->scope = "HLUA";
11028
11029 reg_flt = new_hlua_reg_filter(name);
11030 if (!reg_flt)
11031 goto alloc_error;
11032
11033 reg_flt->flt_ref[hlua_state_id] = flt_ref;
11034 reg_flt->fun_ref[hlua_state_id] = fun_ref;
11035
11036 /* The filter keyword */
11037 len = strlen("lua.") + strlen(name) + 1;
11038 fkl->kw[0].kw = calloc(1, len);
11039 if (!fkl->kw[0].kw)
11040 goto alloc_error;
11041
11042 snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name);
11043
11044 fkl->kw[0].parse = hlua_filter_parse_fct;
11045 fkl->kw[0].private = reg_flt;
11046 memset(&fkl->kw[1], 0, sizeof(*fkl->kw));
11047
11048 /* Register this new filter */
11049 flt_register_keywords(fkl);
11050
11051 return 0;
11052
11053 alloc_error:
11054 release_hlua_reg_filter(reg_flt);
11055 ha_free(&fkl);
11056 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
11057 return 0; /* Never reached */
11058}
11059
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011060static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011061 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011062 char **err, unsigned int *timeout)
11063{
11064 const char *error;
11065
11066 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +020011067 if (error == PARSE_TIME_OVER) {
11068 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
11069 args[1], args[0]);
11070 return -1;
11071 }
11072 else if (error == PARSE_TIME_UNDER) {
11073 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
11074 args[1], args[0]);
11075 return -1;
11076 }
11077 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011078 memprintf(err, "%s: invalid timeout", args[0]);
11079 return -1;
11080 }
11081 return 0;
11082}
11083
11084static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011085 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011086 char **err)
11087{
11088 return hlua_read_timeout(args, section_type, curpx, defpx,
11089 file, line, err, &hlua_timeout_session);
11090}
11091
11092static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011093 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011094 char **err)
11095{
11096 return hlua_read_timeout(args, section_type, curpx, defpx,
11097 file, line, err, &hlua_timeout_task);
11098}
11099
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020011100static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011101 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020011102 char **err)
11103{
11104 return hlua_read_timeout(args, section_type, curpx, defpx,
11105 file, line, err, &hlua_timeout_applet);
11106}
11107
Thierry FOURNIERee9f8022015-03-03 17:37:37 +010011108static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011109 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +010011110 char **err)
11111{
11112 char *error;
11113
11114 hlua_nb_instruction = strtoll(args[1], &error, 10);
11115 if (*error != '\0') {
11116 memprintf(err, "%s: invalid number", args[0]);
11117 return -1;
11118 }
11119 return 0;
11120}
11121
Willy Tarreau32f61e22015-03-18 17:54:59 +010011122static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011123 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +010011124 char **err)
11125{
11126 char *error;
11127
11128 if (*(args[1]) == 0) {
11129 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
11130 return -1;
11131 }
11132 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
11133 if (*error != '\0') {
11134 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
11135 return -1;
11136 }
11137 return 0;
11138}
11139
11140
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011141/* This function is called by the main configuration key "lua-load". It loads and
11142 * execute an lua file during the parsing of the HAProxy configuration file. It is
11143 * the main lua entry point.
11144 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080011145 * This function runs with the HAProxy keywords API. It returns -1 if an error
11146 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011147 *
11148 * In some error case, LUA set an error message in top of the stack. This function
11149 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020011150 *
11151 * This function can fail with an abort() due to an Lua critical error.
11152 * We are in the configuration parsing process of HAProxy, this abort() is
11153 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011154 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011155static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011156{
11157 int error;
11158
11159 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011160 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011161 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011162 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
11163 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011164 return -1;
11165 }
11166
11167 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011168 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011169 switch (error) {
11170 case LUA_OK:
11171 break;
11172 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011173 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
11174 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011175 return -1;
11176 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +010011177 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011178 return -1;
11179 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011180 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
11181 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011182 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +020011183#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011184 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011185 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
11186 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011187 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +020011188#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011189 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011190 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
11191 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011192 return -1;
11193 }
11194
11195 return 0;
11196}
11197
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011198static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011199 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011200 char **err)
11201{
11202 if (*(args[1]) == 0) {
11203 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
11204 return -1;
11205 }
11206
Thierry Fournier59f11be2020-11-29 00:37:41 +010011207 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +010011208 hlua_state_id = 0;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011209 ha_set_thread(NULL);
Thierry Fournierafc63e22020-11-28 17:06:51 +010011210 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011211}
11212
Thierry Fournier59f11be2020-11-29 00:37:41 +010011213static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011214 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +010011215 char **err)
11216{
11217 int len;
11218
11219 if (*(args[1]) == 0) {
11220 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
11221 return -1;
11222 }
11223
11224 if (per_thread_load == NULL) {
11225 /* allocate the first entry large enough to store the final NULL */
11226 per_thread_load = calloc(1, sizeof(*per_thread_load));
11227 if (per_thread_load == NULL) {
11228 memprintf(err, "out of memory error");
11229 return -1;
11230 }
11231 }
11232
11233 /* count used entries */
11234 for (len = 0; per_thread_load[len] != NULL; len++)
11235 ;
11236
11237 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
11238 if (per_thread_load == NULL) {
11239 memprintf(err, "out of memory error");
11240 return -1;
11241 }
11242
11243 per_thread_load[len] = strdup(args[1]);
11244 per_thread_load[len + 1] = NULL;
11245
11246 if (per_thread_load[len] == NULL) {
11247 memprintf(err, "out of memory error");
11248 return -1;
11249 }
11250
11251 /* loading for thread 1 only */
11252 hlua_state_id = 1;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011253 ha_set_thread(NULL);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011254 return hlua_load_state(args[1], hlua_states[1], err);
11255}
11256
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +010011257/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
11258 * in the given <ctx>.
11259 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +010011260static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +010011261{
Thierry Fournier3fb9e512020-11-28 10:13:12 +010011262 lua_getglobal(L, "package"); /* push package variable */
11263 lua_pushstring(L, path); /* push given path */
11264 lua_pushstring(L, ";"); /* push semicolon */
11265 lua_getfield(L, -3, type); /* push old path */
11266 lua_concat(L, 3); /* concatenate to new path */
11267 lua_setfield(L, -2, type); /* store new path */
11268 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +010011269
11270 return 0;
11271}
11272
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011273static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011274 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011275 char **err)
11276{
11277 char *path;
11278 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011279 struct prepend_path *p = NULL;
Bertrand Jacquin7fbc7702021-11-24 21:16:06 +000011280 size_t i;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011281
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011282 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011283 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011284 }
11285
11286 if (!(*args[1])) {
11287 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011288 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011289 }
11290 path = args[1];
11291
11292 if (*args[2]) {
11293 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
11294 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011295 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011296 }
11297 type = args[2];
11298 }
11299
Thierry Fournier59f11be2020-11-29 00:37:41 +010011300 p = calloc(1, sizeof(*p));
11301 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +010011302 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011303 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011304 }
11305 p->path = strdup(path);
11306 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +010011307 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011308 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011309 }
11310 p->type = strdup(type);
11311 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +010011312 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011313 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011314 }
Willy Tarreau2b718102021-04-21 07:32:39 +020011315 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011316
Tim Duesterhus9e5e5862021-10-11 18:51:08 +020011317 /* Handle the global state and the per-thread state for the first
11318 * thread. The remaining threads will be initialized based on
11319 * prepend_path_list.
11320 */
Bertrand Jacquin7fbc7702021-11-24 21:16:06 +000011321 for (i = 0; i < 2; i++) {
Tim Duesterhus9e5e5862021-10-11 18:51:08 +020011322 lua_State *L = hlua_states[i];
11323 const char *error;
11324
11325 if (setjmp(safe_ljmp_env) != 0) {
11326 lua_atpanic(L, hlua_panic_safe);
11327 if (lua_type(L, -1) == LUA_TSTRING)
11328 error = lua_tostring(L, -1);
11329 else
11330 error = "critical error";
11331 fprintf(stderr, "lua-prepend-path: %s.\n", error);
11332 exit(1);
11333 } else {
11334 lua_atpanic(L, hlua_panic_ljmp);
11335 }
11336
11337 hlua_prepend_path(L, type, path);
11338
11339 lua_atpanic(L, hlua_panic_safe);
11340 }
11341
Thierry Fournier59f11be2020-11-29 00:37:41 +010011342 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011343
11344err2:
11345 free(p->type);
11346 free(p->path);
11347err:
11348 free(p);
11349 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011350}
11351
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011352/* configuration keywords declaration */
11353static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011354 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011355 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +010011356 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011357 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
11358 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +020011359 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +010011360 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +010011361 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011362 { 0, NULL, NULL },
11363}};
11364
Willy Tarreau0108d902018-11-25 19:14:37 +010011365INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11366
Christopher Fauletafd8f102018-11-08 11:34:21 +010011367
Thierry FOURNIERbabae282015-09-17 11:36:37 +020011368/* This function can fail with an abort() due to an Lua critical error.
11369 * We are in the initialisation process of HAProxy, this abort() is
11370 * tolerated.
11371 */
Thierry Fournierb8cef172020-11-28 15:37:17 +010011372int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011373{
11374 struct hlua_init_function *init;
11375 const char *msg;
11376 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +010011377 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +010011378 const char *kind;
11379 const char *trace;
11380 int return_status = 1;
11381#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
11382 int nres;
11383#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011384
Willy Tarreaucdb53462020-12-02 12:12:00 +010011385 /* disable memory limit checks if limit is not set */
11386 if (!hlua_global_allocator.limit)
11387 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
11388
Ilya Shipitsin856aabc2020-04-16 23:51:34 +050011389 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +010011390 if (setjmp(safe_ljmp_env) != 0) {
11391 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +010011392 if (lua_type(L, -1) == LUA_TSTRING)
11393 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +010011394 else
11395 error = "critical error";
11396 fprintf(stderr, "Lua post-init: %s.\n", error);
11397 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +010011398 } else {
11399 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +010011400 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +020011401
Thierry Fournierb8cef172020-11-28 15:37:17 +010011402 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +010011403
Thierry Fournierc7492592020-11-28 23:57:24 +010011404 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +010011405 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +010011406
11407#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +010011408 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +010011409#else
Thierry Fournierb8cef172020-11-28 15:37:17 +010011410 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +010011411#endif
11412 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011413 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +010011414
11415 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +010011416 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +010011417 break;
Thierry Fournier670db242020-11-28 10:49:59 +010011418
11419 case LUA_ERRERR:
11420 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -070011421 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +010011422 case LUA_ERRRUN:
11423 if (!kind)
11424 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +010011425 msg = lua_tostring(L, -1);
11426 lua_settop(L, 0); /* Empty the stack. */
11427 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +010011428 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +010011429 if (msg)
11430 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
11431 else
11432 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
11433 return_status = 0;
11434 break;
11435
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011436 default:
Thierry Fournier670db242020-11-28 10:49:59 +010011437 /* Unknown error */
11438 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -070011439 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +010011440 case LUA_YIELD:
11441 /* yield is not configured at this step, this state doesn't happen */
11442 if (!kind)
11443 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -070011444 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +010011445 case LUA_ERRMEM:
11446 if (!kind)
11447 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +010011448 lua_settop(L, 0);
11449 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +010011450 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +010011451 ha_alert("Lua init: %s: %s\n", kind, trace);
11452 return_status = 0;
11453 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011454 }
Thierry Fournier670db242020-11-28 10:49:59 +010011455 if (!return_status)
11456 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011457 }
Thierry Fournier3c539322020-11-28 16:05:05 +010011458
11459 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +010011460 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011461}
11462
Thierry Fournierb8cef172020-11-28 15:37:17 +010011463int hlua_post_init()
11464{
Thierry Fournier59f11be2020-11-29 00:37:41 +010011465 int ret;
11466 int i;
11467 int errors;
11468 char *err = NULL;
11469 struct hlua_function *fcn;
Christopher Faulet69c581a2021-05-31 08:54:04 +020011470 struct hlua_reg_filter *reg_flt;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011471
Willy Tarreaub1310492021-08-30 09:35:18 +020011472#if defined(USE_OPENSSL)
Thierry Fournierb8cef172020-11-28 15:37:17 +010011473 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010011474 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +010011475 int saved_used_backed = global.ssl_used_backend;
11476 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010011477 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +010011478 global.ssl_used_backend = saved_used_backed;
11479 }
11480#endif
11481
Thierry Fournierc7492592020-11-28 23:57:24 +010011482 /* Perform post init of common thread */
11483 hlua_state_id = 0;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011484 ha_set_thread(&ha_thread_info[0]);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011485 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
11486 if (ret == 0)
11487 return 0;
11488
11489 /* init remaining lua states and load files */
11490 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
11491
11492 /* set thread context */
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011493 ha_set_thread(&ha_thread_info[hlua_state_id - 1]);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011494
11495 /* Init lua state */
11496 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
11497
11498 /* Load lua files */
11499 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
11500 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
11501 if (ret != 0) {
11502 ha_alert("Lua init: %s\n", err);
11503 return 0;
11504 }
11505 }
11506 }
11507
11508 /* Reset thread context */
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011509 ha_set_thread(NULL);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011510
11511 /* Execute post init for all states */
11512 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
11513
11514 /* set thread context */
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011515 ha_set_thread(&ha_thread_info[hlua_state_id - 1]);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011516
11517 /* run post init */
11518 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
11519 if (ret == 0)
11520 return 0;
11521 }
11522
11523 /* Reset thread context */
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011524 ha_set_thread(NULL);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011525
11526 /* control functions registering. Each function must have:
11527 * - only the function_ref[0] set positive and all other to -1
11528 * - only the function_ref[0] set to -1 and all other positive
11529 * This ensure a same reference is not used both in shared
11530 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +050011531 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +010011532 * complicated to found for the end user.
11533 */
11534 errors = 0;
11535 list_for_each_entry(fcn, &referenced_functions, l) {
11536 ret = 0;
11537 for (i = 1; i < global.nbthread + 1; i++) {
11538 if (fcn->function_ref[i] == -1)
11539 ret--;
11540 else
11541 ret++;
11542 }
11543 if (abs(ret) != global.nbthread) {
11544 ha_alert("Lua function '%s' is not referenced in all thread. "
11545 "Expect function in all thread or in none thread.\n", fcn->name);
11546 errors++;
11547 continue;
11548 }
11549
11550 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +050011551 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
11552 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +010011553 "exclusive.\n", fcn->name);
11554 errors++;
11555 }
11556 }
11557
Christopher Faulet69c581a2021-05-31 08:54:04 +020011558 /* Do the same with registered filters */
11559 list_for_each_entry(reg_flt, &referenced_filters, l) {
11560 ret = 0;
11561 for (i = 1; i < global.nbthread + 1; i++) {
11562 if (reg_flt->flt_ref[i] == -1)
11563 ret--;
11564 else
11565 ret++;
11566 }
11567 if (abs(ret) != global.nbthread) {
11568 ha_alert("Lua filter '%s' is not referenced in all thread. "
11569 "Expect function in all thread or in none thread.\n", reg_flt->name);
11570 errors++;
11571 continue;
11572 }
11573
11574 if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) {
11575 ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) "
11576 "and per-thread Lua context (through lua-load-per-thread). these two context "
11577 "exclusive.\n", fcn->name);
11578 errors++;
11579 }
11580 }
11581
11582
Thierry Fournier59f11be2020-11-29 00:37:41 +010011583 if (errors > 0)
11584 return 0;
11585
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +050011586 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +010011587 * -1 in order to have probably a segfault if someone use it
11588 */
11589 hlua_state_id = -1;
11590
11591 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +010011592}
11593
Willy Tarreau32f61e22015-03-18 17:54:59 +010011594/* The memory allocator used by the Lua stack. <ud> is a pointer to the
11595 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
11596 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +010011597 * allocation. <nsize> is the requested new size. A new allocation is
11598 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +010011599 * zero. This one verifies that the limits are respected but is optimized
11600 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreaua5efdff2021-10-22 16:00:02 +020011601 *
11602 * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses
11603 * POSIX by making realloc(ptr,0) an effective free(), but others do not do
11604 * that and will simply allocate zero as if it were the result of malloc(0),
11605 * so mapping this onto realloc() will lead to memory leaks on non-glibc
11606 * systems.
Willy Tarreau32f61e22015-03-18 17:54:59 +010011607 */
11608static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
11609{
11610 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +010011611 size_t limit, old, new;
11612
11613 /* a limit of ~0 means unlimited and boot complete, so there's no need
11614 * for accounting anymore.
11615 */
Willy Tarreaua5efdff2021-10-22 16:00:02 +020011616 if (likely(~zone->limit == 0)) {
11617 if (!nsize)
11618 ha_free(&ptr);
11619 else
11620 ptr = realloc(ptr, nsize);
11621 return ptr;
11622 }
Willy Tarreau32f61e22015-03-18 17:54:59 +010011623
Willy Tarreaud36c7fa2020-12-02 12:26:29 +010011624 if (!ptr)
11625 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +010011626
Willy Tarreaucdb53462020-12-02 12:12:00 +010011627 /* enforce strict limits across all threads */
11628 limit = zone->limit;
11629 old = _HA_ATOMIC_LOAD(&zone->allocated);
11630 do {
11631 new = old + nsize - osize;
11632 if (unlikely(nsize && limit && new > limit))
11633 return NULL;
11634 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +010011635
Willy Tarreaua5efdff2021-10-22 16:00:02 +020011636 if (!nsize)
11637 ha_free(&ptr);
11638 else
11639 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +010011640
11641 if (unlikely(!ptr && nsize)) // failed
11642 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
11643
11644 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +010011645 return ptr;
11646}
11647
Thierry Fournierecb83c22020-11-28 15:49:44 +010011648/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020011649 * We are in the initialisation process of HAProxy, this abort() is
11650 * tolerated.
11651 */
Thierry Fournierecb83c22020-11-28 15:49:44 +010011652lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010011653{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011654 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011655 int idx;
11656 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011657 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011658 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +010011659 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +010011660 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011661 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011662 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011663
Thierry FOURNIER380d0932015-01-23 14:27:52 +010011664 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011665 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +010011666
Thierry Fournier4234dbd2020-11-28 13:18:23 +010011667 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011668 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +010011669 *context = NULL;
11670
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080011671 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +020011672 * the Lua function can fail with an abort. We are in the initialisation
11673 * process of HAProxy, this abort() is tolerated.
11674 */
11675
Thierry Fournier3c539322020-11-28 16:05:05 +010011676 /* Call post initialisation function in safe environment. */
11677 if (setjmp(safe_ljmp_env) != 0) {
11678 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011679 if (lua_type(L, -1) == LUA_TSTRING)
11680 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +010011681 else
11682 error_msg = "critical error";
11683 fprintf(stderr, "Lua init: %s.\n", error_msg);
11684 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +010011685 } else {
11686 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +010011687 }
11688
Thierry FOURNIER380d0932015-01-23 14:27:52 +010011689 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011690 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +010011691#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
11692#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
11693#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011694 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +010011695#endif
11696#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011697 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +010011698#endif
11699#undef HLUA_PREPEND_PATH_TOSTRING
11700#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011701
Thierry Fournier59f11be2020-11-29 00:37:41 +010011702 /* Apply configured prepend path */
11703 list_for_each_entry(pp, &prepend_path_list, l)
11704 hlua_prepend_path(L, pp->type, pp->path);
11705
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011706 /*
11707 *
11708 * Create "core" object.
11709 *
11710 */
11711
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +010011712 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011713 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011714
Thierry Fournierecb83c22020-11-28 15:49:44 +010011715 /* set the thread id */
11716 hlua_class_const_int(L, "thread", thread_num);
11717
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011718 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +010011719 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011720 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011721
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011722 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011723 hlua_class_function(L, "register_init", hlua_register_init);
11724 hlua_class_function(L, "register_task", hlua_register_task);
11725 hlua_class_function(L, "register_fetches", hlua_register_fetches);
11726 hlua_class_function(L, "register_converters", hlua_register_converters);
11727 hlua_class_function(L, "register_action", hlua_register_action);
11728 hlua_class_function(L, "register_service", hlua_register_service);
11729 hlua_class_function(L, "register_cli", hlua_register_cli);
Christopher Faulet69c581a2021-05-31 08:54:04 +020011730 hlua_class_function(L, "register_filter", hlua_register_filter);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011731 hlua_class_function(L, "yield", hlua_yield);
11732 hlua_class_function(L, "set_nice", hlua_set_nice);
11733 hlua_class_function(L, "sleep", hlua_sleep);
11734 hlua_class_function(L, "msleep", hlua_msleep);
11735 hlua_class_function(L, "add_acl", hlua_add_acl);
11736 hlua_class_function(L, "del_acl", hlua_del_acl);
11737 hlua_class_function(L, "set_map", hlua_set_map);
11738 hlua_class_function(L, "del_map", hlua_del_map);
11739 hlua_class_function(L, "tcp", hlua_socket_new);
William Lallemand3956c4e2021-09-21 16:25:15 +020011740 hlua_class_function(L, "httpclient", hlua_httpclient_new);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011741 hlua_class_function(L, "log", hlua_log);
11742 hlua_class_function(L, "Debug", hlua_log_debug);
11743 hlua_class_function(L, "Info", hlua_log_info);
11744 hlua_class_function(L, "Warning", hlua_log_warning);
11745 hlua_class_function(L, "Alert", hlua_log_alert);
11746 hlua_class_function(L, "done", hlua_done);
11747 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011748
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011749 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010011750
11751 /*
11752 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +010011753 * Create "act" object.
11754 *
11755 */
11756
11757 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011758 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +010011759
11760 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011761 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
11762 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
11763 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
11764 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
11765 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
11766 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
11767 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
11768 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +010011769
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011770 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +010011771
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011772 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +010011773
11774 /*
11775 *
Christopher Faulet69c581a2021-05-31 08:54:04 +020011776 * Create "Filter" object.
11777 *
11778 */
11779
11780 /* This table entry is the object "filter" base. */
11781 lua_newtable(L);
11782
11783 /* push flags and constants */
11784 hlua_class_const_int(L, "CONTINUE", 1);
11785 hlua_class_const_int(L, "WAIT", 0);
11786 hlua_class_const_int(L, "ERROR", -1);
11787
11788 hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX);
11789
Christopher Fauletc404f112020-02-26 15:03:09 +010011790 hlua_class_function(L, "wake_time", hlua_set_wake_time);
11791 hlua_class_function(L, "register_data_filter", hlua_register_data_filter);
11792 hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter);
11793
Christopher Faulet69c581a2021-05-31 08:54:04 +020011794 lua_setglobal(L, "filter");
11795
11796 /*
11797 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011798 * Register class Map
11799 *
11800 */
11801
11802 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011803 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011804
11805 /* register pattern types. */
11806 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011807 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +010011808 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011809 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011810 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +010011811 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011812
11813 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011814 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011815
11816 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011817 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011818
Ilya Shipitsind4259502020-04-08 01:07:56 +050011819 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011820 lua_pushstring(L, "__index");
11821 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011822
11823 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011824 hlua_class_function(L, "lookup", hlua_map_lookup);
11825 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011826
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011827 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011828
Thierry Fournier45e78d72016-02-19 18:34:46 +010011829 /* Register previous table in the registry with reference and named entry.
11830 * The function hlua_register_metatable() pops the stack, so we
11831 * previously create a copy of the table.
11832 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011833 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
11834 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011835
11836 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011837 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011838
11839 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011840 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011841
11842 /*
11843 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011844 * Register class Channel
11845 *
11846 */
11847
11848 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011849 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011850
Ilya Shipitsind4259502020-04-08 01:07:56 +050011851 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011852 lua_pushstring(L, "__index");
11853 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011854
11855 /* Register . */
Christopher Faulet6a79fc12021-08-06 16:02:36 +020011856 hlua_class_function(L, "data", hlua_channel_get_data);
11857 hlua_class_function(L, "line", hlua_channel_get_line);
11858 hlua_class_function(L, "set", hlua_channel_set_data);
11859 hlua_class_function(L, "remove", hlua_channel_del_data);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011860 hlua_class_function(L, "append", hlua_channel_append);
Christopher Faulet6a79fc12021-08-06 16:02:36 +020011861 hlua_class_function(L, "prepend", hlua_channel_prepend);
11862 hlua_class_function(L, "insert", hlua_channel_insert_data);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011863 hlua_class_function(L, "send", hlua_channel_send);
11864 hlua_class_function(L, "forward", hlua_channel_forward);
Christopher Faulet6a79fc12021-08-06 16:02:36 +020011865 hlua_class_function(L, "input", hlua_channel_get_in_len);
11866 hlua_class_function(L, "output", hlua_channel_get_out_len);
11867 hlua_class_function(L, "may_recv", hlua_channel_may_recv);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011868 hlua_class_function(L, "is_full", hlua_channel_is_full);
11869 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011870
Christopher Faulet6a79fc12021-08-06 16:02:36 +020011871 /* Deprecated API */
11872 hlua_class_function(L, "get", hlua_channel_get);
11873 hlua_class_function(L, "dup", hlua_channel_dup);
11874 hlua_class_function(L, "getline", hlua_channel_getline);
11875 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
11876 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
11877
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011878 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011879
11880 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011881 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011882
11883 /*
11884 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010011885 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010011886 *
11887 */
11888
11889 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011890 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010011891
Ilya Shipitsind4259502020-04-08 01:07:56 +050011892 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011893 lua_pushstring(L, "__index");
11894 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010011895
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011896 /* Browse existing fetches and create the associated
11897 * object method.
11898 */
11899 sf = NULL;
11900 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011901 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
11902 * by an underscore.
11903 */
Willy Tarreau5ef96562021-08-26 16:48:53 +020011904 strlcpy2(trash.area, sf->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011905 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011906 if (*p == '.' || *p == '-' || *p == '+')
11907 *p = '_';
11908
11909 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011910 lua_pushstring(L, trash.area);
11911 lua_pushlightuserdata(L, sf);
11912 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
11913 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011914 }
11915
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011916 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010011917
11918 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011919 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010011920
11921 /*
11922 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011923 * Register class Converters
11924 *
11925 */
11926
11927 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011928 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011929
11930 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011931 lua_pushstring(L, "__index");
11932 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011933
11934 /* Browse existing converters and create the associated
11935 * object method.
11936 */
11937 sc = NULL;
11938 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011939 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
11940 * by an underscore.
11941 */
Willy Tarreau5ef96562021-08-26 16:48:53 +020011942 strlcpy2(trash.area, sc->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011943 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011944 if (*p == '.' || *p == '-' || *p == '+')
11945 *p = '_';
11946
11947 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011948 lua_pushstring(L, trash.area);
11949 lua_pushlightuserdata(L, sc);
11950 lua_pushcclosure(L, hlua_run_sample_conv, 1);
11951 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011952 }
11953
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011954 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011955
11956 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011957 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011958
11959 /*
11960 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011961 * Register class HTTP
11962 *
11963 */
11964
11965 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011966 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011967
Ilya Shipitsind4259502020-04-08 01:07:56 +050011968 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011969 lua_pushstring(L, "__index");
11970 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011971
11972 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011973 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
11974 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
11975 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
11976 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
11977 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
11978 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
11979 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
11980 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
11981 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
11982 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011983
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011984 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
11985 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
11986 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
11987 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
11988 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
11989 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
11990 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011991
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011992 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011993
11994 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011995 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011996
Christopher Fauletdf97ac42020-02-26 16:57:19 +010011997 /*
11998 *
11999 * Register class HTTPMessage
12000 *
12001 */
12002
12003 /* Create and fill the metatable. */
12004 lua_newtable(L);
12005
Ilya Shipitsinff0f2782021-08-22 22:18:07 +050012006 /* Create and fill the __index entry. */
Christopher Fauletdf97ac42020-02-26 16:57:19 +010012007 lua_pushstring(L, "__index");
12008 lua_newtable(L);
12009
12010 /* Register Lua functions. */
12011 hlua_class_function(L, "is_resp", hlua_http_msg_is_resp);
12012 hlua_class_function(L, "get_stline", hlua_http_msg_get_stline);
12013 hlua_class_function(L, "get_headers", hlua_http_msg_get_headers);
12014 hlua_class_function(L, "del_header", hlua_http_msg_del_hdr);
12015 hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr);
12016 hlua_class_function(L, "rep_value", hlua_http_msg_rep_val);
12017 hlua_class_function(L, "add_header", hlua_http_msg_add_hdr);
12018 hlua_class_function(L, "set_header", hlua_http_msg_set_hdr);
12019 hlua_class_function(L, "set_method", hlua_http_msg_set_meth);
12020 hlua_class_function(L, "set_path", hlua_http_msg_set_path);
12021 hlua_class_function(L, "set_query", hlua_http_msg_set_query);
12022 hlua_class_function(L, "set_uri", hlua_http_msg_set_uri);
12023 hlua_class_function(L, "set_status", hlua_http_msg_set_status);
12024 hlua_class_function(L, "is_full", hlua_http_msg_is_full);
12025 hlua_class_function(L, "may_recv", hlua_http_msg_may_recv);
12026 hlua_class_function(L, "eom", hlua_http_msg_is_eom);
12027 hlua_class_function(L, "input", hlua_http_msg_get_in_len);
12028 hlua_class_function(L, "output", hlua_http_msg_get_out_len);
12029
12030 hlua_class_function(L, "body", hlua_http_msg_get_body);
12031 hlua_class_function(L, "set", hlua_http_msg_set_data);
12032 hlua_class_function(L, "remove", hlua_http_msg_del_data);
12033 hlua_class_function(L, "append", hlua_http_msg_append);
12034 hlua_class_function(L, "prepend", hlua_http_msg_prepend);
12035 hlua_class_function(L, "insert", hlua_http_msg_insert_data);
12036 hlua_class_function(L, "set_eom", hlua_http_msg_set_eom);
12037 hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom);
12038
12039 hlua_class_function(L, "send", hlua_http_msg_send);
12040 hlua_class_function(L, "forward", hlua_http_msg_forward);
12041
12042 lua_rawset(L, -3);
12043
12044 /* Register previous table in the registry with reference and named entry. */
12045 class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG);
William Lallemand3956c4e2021-09-21 16:25:15 +020012046
12047 /*
12048 *
12049 * Register class HTTPClient
12050 *
12051 */
12052
12053 /* Create and fill the metatable. */
12054 lua_newtable(L);
12055 lua_pushstring(L, "__index");
12056 lua_newtable(L);
12057 hlua_class_function(L, "get", hlua_httpclient_get);
William Lallemanddc2cc902021-10-26 11:43:26 +020012058 hlua_class_function(L, "head", hlua_httpclient_head);
12059 hlua_class_function(L, "put", hlua_httpclient_put);
12060 hlua_class_function(L, "post", hlua_httpclient_post);
12061 hlua_class_function(L, "delete", hlua_httpclient_delete);
William Lallemand3956c4e2021-09-21 16:25:15 +020012062 lua_settable(L, -3); /* Sets the __index entry. */
William Lallemandf77f1de2021-09-28 19:10:38 +020012063 /* Register the garbage collector entry. */
12064 lua_pushstring(L, "__gc");
12065 lua_pushcclosure(L, hlua_httpclient_gc, 0);
12066 lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */
12067
William Lallemand3956c4e2021-09-21 16:25:15 +020012068
12069
12070 class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010012071 /*
12072 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012073 * Register class AppletTCP
12074 *
12075 */
12076
12077 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012078 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012079
Ilya Shipitsind4259502020-04-08 01:07:56 +050012080 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012081 lua_pushstring(L, "__index");
12082 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012083
12084 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012085 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
12086 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
12087 hlua_class_function(L, "send", hlua_applet_tcp_send);
12088 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
12089 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
12090 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
12091 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
12092 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012093
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012094 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012095
12096 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012097 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012098
12099 /*
12100 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012101 * Register class AppletHTTP
12102 *
12103 */
12104
12105 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012106 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012107
Ilya Shipitsind4259502020-04-08 01:07:56 +050012108 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012109 lua_pushstring(L, "__index");
12110 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012111
12112 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012113 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
12114 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
12115 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
12116 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
12117 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
12118 hlua_class_function(L, "getline", hlua_applet_http_getline);
12119 hlua_class_function(L, "receive", hlua_applet_http_recv);
12120 hlua_class_function(L, "send", hlua_applet_http_send);
12121 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
12122 hlua_class_function(L, "set_status", hlua_applet_http_status);
12123 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012124
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012125 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012126
12127 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012128 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012129
12130 /*
12131 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010012132 * Register class TXN
12133 *
12134 */
12135
12136 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012137 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010012138
Ilya Shipitsind4259502020-04-08 01:07:56 +050012139 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012140 lua_pushstring(L, "__index");
12141 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010012142
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +010012143 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012144 hlua_class_function(L, "set_priv", hlua_set_priv);
12145 hlua_class_function(L, "get_priv", hlua_get_priv);
12146 hlua_class_function(L, "set_var", hlua_set_var);
12147 hlua_class_function(L, "unset_var", hlua_unset_var);
12148 hlua_class_function(L, "get_var", hlua_get_var);
12149 hlua_class_function(L, "done", hlua_txn_done);
12150 hlua_class_function(L, "reply", hlua_txn_reply_new);
12151 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
12152 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
12153 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
12154 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
12155 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
12156 hlua_class_function(L, "deflog", hlua_txn_deflog);
12157 hlua_class_function(L, "log", hlua_txn_log);
12158 hlua_class_function(L, "Debug", hlua_txn_log_debug);
12159 hlua_class_function(L, "Info", hlua_txn_log_info);
12160 hlua_class_function(L, "Warning", hlua_txn_log_warning);
12161 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +010012162
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012163 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010012164
12165 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012166 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012167
12168 /*
12169 *
Christopher Faulet700d9e82020-01-31 12:21:52 +010012170 * Register class reply
12171 *
12172 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012173 lua_newtable(L);
12174 lua_pushstring(L, "__index");
12175 lua_newtable(L);
12176 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
12177 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
12178 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
12179 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
12180 lua_settable(L, -3); /* Sets the __index entry. */
12181 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +010012182
12183
12184 /*
12185 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012186 * Register class Socket
12187 *
12188 */
12189
12190 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012191 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012192
Ilya Shipitsind4259502020-04-08 01:07:56 +050012193 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012194 lua_pushstring(L, "__index");
12195 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012196
Baptiste Assmann84bb4932015-03-02 21:40:06 +010012197#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012198 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +010012199#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012200 hlua_class_function(L, "connect", hlua_socket_connect);
12201 hlua_class_function(L, "send", hlua_socket_send);
12202 hlua_class_function(L, "receive", hlua_socket_receive);
12203 hlua_class_function(L, "close", hlua_socket_close);
12204 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
12205 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
12206 hlua_class_function(L, "setoption", hlua_socket_setoption);
12207 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012208
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012209 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012210
12211 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012212 lua_pushstring(L, "__gc");
12213 lua_pushcclosure(L, hlua_socket_gc, 0);
12214 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012215
12216 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012217 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012218
Thierry Fournieraafc7772020-12-04 11:47:47 +010012219 lua_atpanic(L, hlua_panic_safe);
12220
12221 return L;
12222}
12223
12224void hlua_init(void) {
12225 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010012226 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +010012227#ifdef USE_OPENSSL
12228 struct srv_kw *kw;
12229 int tmp_error;
12230 char *error;
12231 char *args[] = { /* SSL client configuration. */
12232 "ssl",
12233 "verify",
12234 "none",
12235 NULL
12236 };
12237#endif
12238
12239 /* Init post init function list head */
12240 for (i = 0; i < MAX_THREADS + 1; i++)
12241 LIST_INIT(&hlua_init_functions[i]);
12242
12243 /* Init state for common/shared lua parts */
12244 hlua_state_id = 0;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020012245 ha_set_thread(NULL);
Thierry Fournieraafc7772020-12-04 11:47:47 +010012246 hlua_states[0] = hlua_init_state(0);
12247
12248 /* Init state 1 for thread 0. We have at least one thread. */
12249 hlua_state_id = 1;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020012250 ha_set_thread(NULL);
Thierry Fournieraafc7772020-12-04 11:47:47 +010012251 hlua_states[1] = hlua_init_state(1);
12252
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012253 /* Proxy and server configuration initialisation. */
William Lallemand6bb77b92021-07-28 15:48:16 +020012254 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg);
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010012255 if (!socket_proxy) {
12256 fprintf(stderr, "Lua init: %s\n", errmsg);
12257 exit(1);
12258 }
12259 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012260
12261 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010012262 socket_tcp = new_server(socket_proxy);
12263 if (!socket_tcp) {
12264 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
12265 exit(1);
12266 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012267
12268#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012269 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010012270 socket_ssl = new_server(socket_proxy);
12271 if (!socket_ssl) {
12272 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
12273 exit(1);
12274 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012275
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010012276 socket_ssl->use_ssl = 1;
12277 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012278
Bertrand Jacquin80839ff2021-01-21 19:14:46 +000012279 for (i = 0; args[i] != NULL; i++) {
12280 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012281 /*
12282 *
12283 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080012284 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012285 * features like client certificates and ssl_verify.
12286 *
12287 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010012288 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012289 if (tmp_error != 0) {
12290 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
12291 abort(); /* This must be never arrives because the command line
12292 not editable by the user. */
12293 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +000012294 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012295 }
12296 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012297#endif
Thierry Fournier75933d42016-01-21 09:30:18 +010012298
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010012299}
Willy Tarreaubb57d942016-12-21 19:04:56 +010012300
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +020012301static void hlua_deinit()
12302{
Willy Tarreau186f3762020-12-04 11:48:12 +010012303 int thr;
Christopher Faulet69c581a2021-05-31 08:54:04 +020012304 struct hlua_reg_filter *reg_flt, *reg_flt_bck;
12305
12306 list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l)
12307 release_hlua_reg_filter(reg_flt);
Willy Tarreau186f3762020-12-04 11:48:12 +010012308
12309 for (thr = 0; thr < MAX_THREADS+1; thr++) {
12310 if (hlua_states[thr])
12311 lua_close(hlua_states[thr]);
12312 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +010012313
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +020012314 srv_drop(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +010012315
Willy Tarreau0f143af2021-03-05 10:41:48 +010012316#ifdef USE_OPENSSL
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +020012317 srv_drop(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +010012318#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010012319
12320 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +020012321}
12322
12323REGISTER_POST_DEINIT(hlua_deinit);
12324
Willy Tarreau80713382018-11-26 10:19:54 +010012325static void hlua_register_build_options(void)
12326{
Willy Tarreaubb57d942016-12-21 19:04:56 +010012327 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +010012328
Willy Tarreaubb57d942016-12-21 19:04:56 +010012329 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
12330 hap_register_build_opts(ptr, 1);
12331}
Willy Tarreau80713382018-11-26 10:19:54 +010012332
12333INITCALL0(STG_REGISTER, hlua_register_build_options);