blob: 8b42746d8266fc44eae7b3b173d672e82c87e7cd [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 Faulet95a61e82021-12-22 14:22:03 +01002687 appctx = cs_appctx(s->csf);
Christopher Faulet2da02ae2022-02-24 13:45:27 +01002688 ALREADY_CHECKED(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002689
2690 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002691 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002692 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002693 lua_pushinteger(L, 1);
2694 return 1;
2695 }
2696
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002697 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2698 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002699 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002700 }
2701 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002702 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002703 return 0;
2704}
2705
2706/* This function fail or initite the connection. */
2707__LJMP static int hlua_socket_connect(struct lua_State *L)
2708{
2709 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002710 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002711 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002712 struct hlua *hlua;
2713 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002714 int low, high;
2715 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002716 struct xref *peer;
2717 struct stream_interface *si;
2718 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002719
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002720 if (lua_gettop(L) < 2)
2721 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002722
2723 /* Get args. */
2724 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002725
2726 /* Check if we run on the same thread than the xreator thread.
2727 * We cannot access to the socket if the thread is different.
2728 */
2729 if (socket->tid != tid)
2730 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2731
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002732 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002733 if (lua_gettop(L) >= 3) {
2734 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002735 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002736
Tim Duesterhus6edab862018-01-06 19:04:45 +01002737 /* Force the ip to end with a colon, to support IPv6 addresses
2738 * that are not enclosed within square brackets.
2739 */
2740 if (port > 0) {
2741 luaL_buffinit(L, &b);
2742 luaL_addstring(&b, ip);
2743 luaL_addchar(&b, ':');
2744 luaL_pushresult(&b);
2745 ip = lua_tolstring(L, lua_gettop(L), NULL);
2746 }
2747 }
2748
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002749 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002750 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002751 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002752 lua_pushnil(L);
2753 return 1;
2754 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002755
2756 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002757 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 +02002758 if (!addr) {
2759 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002760 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002761 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002762
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002763 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002764 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002765 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002766 if (port == -1) {
2767 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002768 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002769 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002770 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2771 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002772 if (port == -1) {
2773 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002774 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002775 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002776 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002777 }
2778 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002779
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002780 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002781 si = cs_si(appctx->owner);
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002782 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002783
Christopher Faulet16f16af2021-10-27 09:34:56 +02002784 if (!sockaddr_alloc(&si_opposite(si)->dst, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002785 xref_unlock(&socket->xref, peer);
2786 WILL_LJMP(luaL_error(L, "connect: internal error"));
2787 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002788 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002789
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002790 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002791 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002792 if (!hlua)
2793 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002794
2795 /* inform the stream that we want to be notified whenever the
2796 * connection completes.
2797 */
Christopher Faulet436811f2021-12-23 13:39:38 +01002798 si_cant_get(cs_si(s->csf));
2799 si_rx_endp_more(cs_si(s->csf));
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002800 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002801
Willy Tarreauf31af932020-01-14 09:59:38 +01002802 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002803
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002804 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2805 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002806 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002807 }
2808 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002809
2810 task_wakeup(s->task, TASK_WOKEN_INIT);
2811 /* Return yield waiting for connection. */
2812
Willy Tarreau9635e032018-10-16 17:52:55 +02002813 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002814
2815 return 0;
2816}
2817
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002818#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002819__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2820{
2821 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002822 struct xref *peer;
2823 struct appctx *appctx;
2824 struct stream_interface *si;
2825 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002826
2827 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2828 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002829
2830 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002831 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002832 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002833 lua_pushnil(L);
2834 return 1;
2835 }
2836 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002837 si = cs_si(appctx->owner);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002838 s = si_strm(si);
2839
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002840 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002841 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002842 return MAY_LJMP(hlua_socket_connect(L));
2843}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002844#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002845
2846__LJMP static int hlua_socket_setoption(struct lua_State *L)
2847{
2848 return 0;
2849}
2850
2851__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2852{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002853 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002854 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002855 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002856 struct xref *peer;
2857 struct appctx *appctx;
2858 struct stream_interface *si;
2859 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002860
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002861 MAY_LJMP(check_args(L, 2, "settimeout"));
2862
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002863 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002864
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002865 /* convert the timeout to millis */
2866 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002867
Thierry Fournier17a921b2018-03-08 09:59:02 +01002868 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002869 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002870 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2871
Mark Lakes56cc1252018-03-27 09:48:06 +02002872 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002873 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002874
2875 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002876 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002877 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002878
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002879 /* Check if we run on the same thread than the xreator thread.
2880 * We cannot access to the socket if the thread is different.
2881 */
2882 if (socket->tid != tid)
2883 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2884
Mark Lakes56cc1252018-03-27 09:48:06 +02002885 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002886 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002887 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002888 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2889 WILL_LJMP(lua_error(L));
2890 return 0;
2891 }
2892 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Christopher Faulet86e1c332021-12-20 17:09:39 +01002893 si = cs_si(appctx->owner);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002894 s = si_strm(si);
2895
Cyril Bonté7bb63452018-08-17 23:51:02 +02002896 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002897 s->req.rto = tmout;
2898 s->req.wto = tmout;
2899 s->res.rto = tmout;
2900 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002901 s->req.rex = tick_add_ifset(now_ms, tmout);
2902 s->req.wex = tick_add_ifset(now_ms, tmout);
2903 s->res.rex = tick_add_ifset(now_ms, tmout);
2904 s->res.wex = tick_add_ifset(now_ms, tmout);
2905
2906 s->task->expire = tick_add_ifset(now_ms, tmout);
2907 task_queue(s->task);
2908
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002909 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002910
Thierry Fourniere9636f12018-03-08 09:54:32 +01002911 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002912 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002913}
2914
2915__LJMP static int hlua_socket_new(lua_State *L)
2916{
2917 struct hlua_socket *socket;
2918 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002919 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +01002920 struct conn_stream *cs;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002921 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002922
2923 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002924 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002925 hlua_pusherror(L, "socket: full stack");
2926 goto out_fail_conf;
2927 }
2928
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002929 /* Create the object: obj[0] = userdata. */
2930 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002931 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002932 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002933 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002934 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002935
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002936 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002937 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002938 hlua_pusherror(L, "socket: uninitialized pools.");
2939 goto out_fail_conf;
2940 }
2941
Willy Tarreau87b09662015-04-03 00:22:06 +02002942 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002943 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2944 lua_setmetatable(L, -2);
2945
Willy Tarreaud420a972015-04-06 00:39:18 +02002946 /* Create the applet context */
Willy Tarreaue6124462021-09-13 10:07:38 +02002947 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002948 if (!appctx) {
2949 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002950 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002951 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002952
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002953 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002954 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002955 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2956 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002957
Willy Tarreaud420a972015-04-06 00:39:18 +02002958 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002959 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002960 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002961 hlua_pusherror(L, "socket: out of memory");
Christopher Faulet13a35e52021-12-20 15:34:16 +01002962 goto out_fail_appctx;
2963 }
2964
Christopher Fauletcda94ac2021-12-23 17:28:17 +01002965 cs = cs_new();
Christopher Faulet13a35e52021-12-20 15:34:16 +01002966 if (!cs) {
2967 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002968 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002969 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +01002970 cs_attach_endp(cs, &appctx->obj_type, appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002971
Christopher Faulet13a35e52021-12-20 15:34:16 +01002972 strm = stream_new(sess, cs, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002973 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002974 hlua_pusherror(L, "socket: out of memory");
Christopher Faulet13a35e52021-12-20 15:34:16 +01002975 goto out_fail_cs;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002976 }
2977
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002978 /* Initialise cross reference between stream and Lua socket object. */
2979 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002980
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002981 /* Configure "right" stream interface. this "si" is used to connect
2982 * and retrieve data from the server. The connection is initialized
2983 * with the "struct server".
2984 */
Christopher Fauletcda94ac2021-12-23 17:28:17 +01002985 si_set_state(strm->csb->si, SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002986
2987 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002988 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002989 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002990
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002991 return 1;
2992
Christopher Faulet13a35e52021-12-20 15:34:16 +01002993 out_fail_cs:
2994 cs_free(cs);
Willy Tarreaud420a972015-04-06 00:39:18 +02002995 out_fail_sess:
Christopher Faulet13a35e52021-12-20 15:34:16 +01002996 session_free(sess);
2997 out_fail_appctx:
Willy Tarreaud420a972015-04-06 00:39:18 +02002998 appctx_free(appctx);
2999 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01003000 WILL_LJMP(lua_error(L));
3001 return 0;
3002}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003003
3004/*
3005 *
3006 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003007 * Class Channel
3008 *
3009 *
3010 */
3011
3012/* Returns the struct hlua_channel join to the class channel in the
3013 * stack entry "ud" or throws an argument error.
3014 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003015__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003017 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003018}
3019
Willy Tarreau47860ed2015-03-10 14:07:50 +01003020/* Pushes the channel onto the top of the stack. If the stask does not have a
3021 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01003023static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003025 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003026 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 return 0;
3028
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003029 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003030 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003031 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003032
3033 /* Pop a class sesison metatable and affect it to the userdata. */
3034 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
3035 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036 return 1;
3037}
3038
Christopher Faulet9f55a502020-02-25 15:21:02 +01003039/* Helper function returning a filter attached to a channel at the position <ud>
3040 * in the stack, filling the current offset and length of the filter. If no
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003041 * filter is attached, NULL is returned and <offset> and <len> are not
Christopher Faulet9f55a502020-02-25 15:21:02 +01003042 * initialized.
3043 */
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003044static 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 +01003045{
3046 struct filter *filter = NULL;
3047
3048 if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) {
3049 struct hlua_flt_ctx *flt_ctx;
3050
3051 filter = lua_touserdata (L, -1);
3052 flt_ctx = filter->ctx;
3053 if (hlua_filter_from_payload(filter)) {
3054 *offset = flt_ctx->cur_off[CHN_IDX(chn)];
3055 *len = flt_ctx->cur_len[CHN_IDX(chn)];
3056 }
3057 }
3058
3059 lua_pop(L, 1);
3060 return filter;
3061}
3062
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003063/* Copies <len> bytes of data present in the channel's buffer, starting at the
3064* offset <offset>, and put it in a LUA string variable. It is the caller
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003065* responsibility to ensure <len> and <offset> are valid. It always return the
3066* length of the built string. <len> may be 0, in this case, an empty string is
3067* created and 0 is returned.
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003068*/
3069static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003070{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003071 size_t block1, block2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072 luaL_Buffer b;
3073
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003074 block1 = len;
3075 if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)))
3076 block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset));
3077 block2 = len - block1;
3078
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003079 luaL_buffinit(L, &b);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003080 luaL_addlstring(&b, b_peek(&chn->buf, offset), block1);
3081 if (block2)
3082 luaL_addlstring(&b, b_orig(&chn->buf), block2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003083 luaL_pushresult(&b);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003084 return len;
3085}
3086
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003087/* Inserts the string <str> to the channel's buffer at the offset <offset>. This
3088 * function returns -1 if data cannot be copied. Otherwise, it returns the
3089 * number of bytes copied.
3090 */
3091static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset)
3092{
3093 int ret = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003094
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003095 /* Nothing to do, just return */
3096 if (unlikely(istlen(str) == 0))
3097 goto end;
3098
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003099 if (istlen(str) > c_room(chn)) {
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003100 ret = -1;
3101 goto end;
3102 }
3103 ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str));
3104
3105 end:
3106 return ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107}
3108
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003109/* Removes <len> bytes of data at the absolute position <offset>.
3110 */
3111static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len)
3112{
3113 size_t end = offset + len;
3114
3115 if (b_peek(&chn->buf, end) != b_tail(&chn->buf))
3116 b_move(&chn->buf, b_peek_ofs(&chn->buf, end),
3117 b_data(&chn->buf) - end, -len);
3118 b_sub(&chn->buf, len);
3119}
3120
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003121/* Copies input data in the channel's buffer. It is possible to set a specific
3122 * offset (0 by default) and a length (all remaining input data starting for the
3123 * offset by default). If there is not enough input data and more data can be
3124 * received, this function yields.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003125 *
3126 * From an action, All input data are considered. For a filter, the offset and
3127 * the length of input data to consider are retrieved from the filter context.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003128 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003129__LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003131 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003132 struct filter *filter;
3133 size_t input, output;
3134 int offset, len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003135
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003136 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003137
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003138 output = co_data(chn);
3139 input = ci_data(chn);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003140
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003141 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3142 if (filter && !hlua_filter_from_payload(filter))
3143 WILL_LJMP(lua_error(L));
3144
3145 offset = output;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003146 if (lua_gettop(L) > 1) {
3147 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3148 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003149 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003150 offset += output;
3151 if (offset < output || offset > input + output) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003152 lua_pushfstring(L, "offset out of range.");
3153 WILL_LJMP(lua_error(L));
3154 }
3155 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003156 len = output + input - offset;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003157 if (lua_gettop(L) == 3) {
3158 len = MAY_LJMP(luaL_checkinteger(L, 3));
3159 if (!len)
3160 goto dup;
3161 if (len == -1)
3162 len = global.tune.bufsize;
3163 if (len < 0) {
3164 lua_pushfstring(L, "length out of range.");
3165 WILL_LJMP(lua_error(L));
3166 }
3167 }
3168
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003169 if (offset + len > output + input) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003170 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3171 /* Yield waiting for more data, as requested */
3172 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0));
3173 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003174 len = output + input - offset;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003175 }
3176
3177 dup:
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003178 _hlua_channel_dup(chn, L, offset, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179 return 1;
3180}
3181
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003182/* Copies the first line (including the trailing LF) of input data in the
3183 * channel's buffer. It is possible to set a specific offset (0 by default) and
3184 * a length (all remaining input data starting for the offset by default). If
3185 * there is not enough input data and more data can be received, the function
3186 * yields. If a length is explicitly specified, no more data are
3187 * copied. Otherwise, if no LF is found and more data can be received, this
3188 * function yields.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003189 *
3190 * From an action, All input data are considered. For a filter, the offset and
3191 * the length of input data to consider are retrieved from the filter context.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003192 */
3193__LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003194{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003195 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003196 struct filter *filter;
3197 size_t l, input, output;
3198 int offset, len;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003199
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003200 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003201 output = co_data(chn);
3202 input = ci_data(chn);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003203
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003204 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3205 if (filter && !hlua_filter_from_payload(filter))
3206 WILL_LJMP(lua_error(L));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003207
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003208 offset = output;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003209 if (lua_gettop(L) > 1) {
3210 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3211 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003212 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003213 offset += output;
3214 if (offset < output || offset > input + output) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003215 lua_pushfstring(L, "offset out of range.");
3216 WILL_LJMP(lua_error(L));
3217 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003218 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003219
3220 len = output + input - offset;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003221 if (lua_gettop(L) == 3) {
3222 len = MAY_LJMP(luaL_checkinteger(L, 3));
3223 if (!len)
3224 goto dup;
3225 if (len == -1)
3226 len = global.tune.bufsize;
3227 if (len < 0) {
3228 lua_pushfstring(L, "length out of range.");
3229 WILL_LJMP(lua_error(L));
3230 }
3231 }
3232
3233 for (l = 0; l < len; l++) {
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003234 if (l + offset >= output + input)
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003235 break;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003236 if (*(b_peek(&chn->buf, offset + l)) == '\n') {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003237 len = l+1;
3238 goto dup;
3239 }
3240 }
3241
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003242 if (offset + len > output + input) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003243 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3244 /* Yield waiting for more data */
3245 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0));
3246 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003247 len = output + input - offset;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003248 }
3249
3250 dup:
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003251 _hlua_channel_dup(chn, L, offset, len);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003252 return 1;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003253}
3254
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003255/* [ DEPRECATED ]
3256 *
3257 * Duplicate all input data foud in the channel's buffer. The data are not
3258 * removed from the buffer. This function relies on _hlua_channel_dup().
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003259 *
3260 * From an action, All input data are considered. For a filter, the offset and
3261 * the length of input data to consider are retrieved from the filter context.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003262 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003263__LJMP static int hlua_channel_dup(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003264{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003265 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003266 struct filter *filter;
3267 size_t offset, len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003268
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003269 MAY_LJMP(check_args(L, 1, "dup"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003270 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003271 if (IS_HTX_STRM(chn_strm(chn))) {
3272 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3273 WILL_LJMP(lua_error(L));
3274 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003275
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003276 offset = co_data(chn);
3277 len = ci_data(chn);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003278
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003279 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3280 if (filter && !hlua_filter_from_payload(filter))
3281 WILL_LJMP(lua_error(L));
3282
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003283 if (!ci_data(chn) && channel_input_closed(chn)) {
3284 lua_pushnil(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003285 return 1;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003286 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003287
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003288 _hlua_channel_dup(chn, L, offset, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003289 return 1;
3290}
3291
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003292/* [ DEPRECATED ]
3293 *
3294 * Get all input data foud in the channel's buffer. The data are removed from
3295 * the buffer after the copy. This function relies on _hlua_channel_dup() and
3296 * _hlua_channel_delete().
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003297 *
3298 * From an action, All input data are considered. For a filter, the offset and
3299 * the length of input data to consider are retrieved from the filter context.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003300 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003301__LJMP static int hlua_channel_get(lua_State *L)
3302{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003303 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003304 struct filter *filter;
3305 size_t offset, len;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003306 int ret;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003307
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003308 MAY_LJMP(check_args(L, 1, "get"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003309 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3310 if (IS_HTX_STRM(chn_strm(chn))) {
3311 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3312 WILL_LJMP(lua_error(L));
3313 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003314
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003315 offset = co_data(chn);
3316 len = ci_data(chn);
3317
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003318 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3319 if (filter && !hlua_filter_from_payload(filter))
3320 WILL_LJMP(lua_error(L));
3321
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003322 if (!ci_data(chn) && channel_input_closed(chn)) {
3323 lua_pushnil(L);
3324 return 1;
3325 }
3326
3327 ret = _hlua_channel_dup(chn, L, offset, len);
3328 _hlua_channel_delete(chn, offset, ret);
3329 return 1;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003330}
3331
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003332/* This functions consumes and returns one line. If the channel is closed,
3333 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003334 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003335 * value.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003336 *
3337 * From an action, All input data are considered. For a filter, the offset and
3338 * the length of input data to consider are retrieved from the filter context.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003339 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003340__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003341{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003342 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003343 struct filter *filter;
3344 size_t l, offset, len;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003345 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003346
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003347 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003348
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003349 offset = co_data(chn);
3350 len = ci_data(chn);
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003351
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003352 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3353 if (filter && !hlua_filter_from_payload(filter))
3354 WILL_LJMP(lua_error(L));
3355
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003356 if (!ci_data(chn) && channel_input_closed(chn)) {
3357 lua_pushnil(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003358 return 1;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003359 }
3360
3361 for (l = 0; l < len; l++) {
3362 if (*(b_peek(&chn->buf, offset+l)) == '\n') {
3363 len = l+1;
3364 goto dup;
3365 }
3366 }
3367
3368 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3369 /* Yield waiting for more data */
3370 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
3371 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003372
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003373 dup:
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003374 ret = _hlua_channel_dup(chn, L, offset, len);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003375 _hlua_channel_delete(chn, offset, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003376 return 1;
3377}
3378
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003379/* [ DEPRECATED ]
3380 *
3381 * Check arguments for the function "hlua_channel_getline_yield".
3382 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003383__LJMP static int hlua_channel_getline(lua_State *L)
3384{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003385 struct channel *chn;
3386
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003387 MAY_LJMP(check_args(L, 1, "getline"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003388 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3389 if (IS_HTX_STRM(chn_strm(chn))) {
3390 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3391 WILL_LJMP(lua_error(L));
3392 }
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003393 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3394}
3395
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003396/* Retrieves a given amount of input data at the given offset. By default all
3397 * available input data are returned. The offset may be negactive to start from
3398 * the end of input data. The length may be -1 to set it to the maximum buffer
3399 * size.
3400 */
3401__LJMP static int hlua_channel_get_data(lua_State *L)
3402{
3403 struct channel *chn;
3404
3405 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3406 WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments"));
3407 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3408 if (IS_HTX_STRM(chn_strm(chn))) {
3409 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3410 WILL_LJMP(lua_error(L));
3411 }
3412 return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0));
3413}
3414
3415/* Retrieves a given amount of input data at the given offset. By default all
3416 * available input data are returned. The offset may be negactive to start from
3417 * the end of input data. The length may be -1 to set it to the maximum buffer
3418 * size.
3419 */
3420__LJMP static int hlua_channel_get_line(lua_State *L)
3421{
3422 struct channel *chn;
3423
3424 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3425 WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments"));
3426 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3427 if (IS_HTX_STRM(chn_strm(chn))) {
3428 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3429 WILL_LJMP(lua_error(L));
3430 }
3431 return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0));
3432}
3433
3434/* Appends a string into the input side of channel. It returns the length of the
3435 * written string, or -1 if the channel is closed or if the buffer size is too
3436 * little for the data. 0 may be returned if nothing is copied. This function
3437 * does not yield.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003438 *
3439 * For a filter, the context is updated on success.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003440 */
Christopher Faulet23976d92021-08-06 09:59:49 +02003441__LJMP static int hlua_channel_append(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003442{
Christopher Faulet23976d92021-08-06 09:59:49 +02003443 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003444 struct filter *filter;
Christopher Faulet23976d92021-08-06 09:59:49 +02003445 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003446 size_t sz, offset, len;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003447 int ret;
Christopher Faulet23976d92021-08-06 09:59:49 +02003448
3449 MAY_LJMP(check_args(L, 2, "append"));
3450 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003451 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003452 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003453 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003454 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003455 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003456
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003457 offset = co_data(chn);
3458 len = ci_data(chn);
3459
3460 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3461 if (filter && !hlua_filter_from_payload(filter))
3462 WILL_LJMP(lua_error(L));
3463
3464 ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset);
3465 if (ret > 0 && filter) {
3466 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3467
3468 flt_update_offsets(filter, chn, ret);
3469 flt_ctx->cur_len[CHN_IDX(chn)] += ret;
3470 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003471 lua_pushinteger(L, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003472 return 1;
3473}
3474
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003475/* Prepends a string into the input side of channel. It returns the length of the
3476 * written string, or -1 if the channel is closed or if the buffer size is too
3477 * little for the data. 0 may be returned if nothing is copied. This function
3478 * does not yield.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003479 *
3480 * For a filter, the context is updated on success.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003481 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003482__LJMP static int hlua_channel_prepend(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003483{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003484 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003485 struct filter *filter;
Christopher Faulet23976d92021-08-06 09:59:49 +02003486 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003487 size_t sz, offset, len;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003488 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003489
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003490 MAY_LJMP(check_args(L, 2, "prepend"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003491 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003492 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3493 if (IS_HTX_STRM(chn_strm(chn))) {
3494 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3495 WILL_LJMP(lua_error(L));
3496 }
3497
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003498 offset = co_data(chn);
3499 len = ci_data(chn);
3500
3501 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3502 if (filter && !hlua_filter_from_payload(filter))
3503 WILL_LJMP(lua_error(L));
3504
3505 ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset);
3506 if (ret > 0 && filter) {
3507 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3508
3509 flt_update_offsets(filter, chn, ret);
3510 flt_ctx->cur_len[CHN_IDX(chn)] += ret;
3511 }
3512
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003513 lua_pushinteger(L, ret);
3514 return 1;
3515}
3516
3517/* Inserts a given amount of input data at the given offset by a string
3518 * content. By default the string is appended at the end of input data. It
3519 * returns the length of the written string, or -1 if the channel is closed or
3520 * if the buffer size is too little for the data.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003521 *
3522 * For a filter, the context is updated on success.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003523 */
3524__LJMP static int hlua_channel_insert_data(lua_State *L)
3525{
3526 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003527 struct filter *filter;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003528 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003529 size_t sz, input, output;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003530 int ret, offset;
3531
3532 if (lua_gettop(L) < 2 || lua_gettop(L) > 3)
3533 WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments"));
3534 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3535 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003536
3537 output = co_data(chn);
3538 input = ci_data(chn);
3539
3540 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3541 if (filter && !hlua_filter_from_payload(filter))
3542 WILL_LJMP(lua_error(L));
3543
3544 offset = input + output;
3545 if (lua_gettop(L) > 2) {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003546 offset = MAY_LJMP(luaL_checkinteger(L, 3));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003547 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003548 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003549 offset += output;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003550 if (offset < output || offset > output + input) {
3551 lua_pushfstring(L, "offset out of range.");
3552 WILL_LJMP(lua_error(L));
3553 }
3554 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003555 if (IS_HTX_STRM(chn_strm(chn))) {
3556 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3557 WILL_LJMP(lua_error(L));
3558 }
3559
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003560 ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset);
3561 if (ret > 0 && filter) {
3562 struct hlua_flt_ctx *flt_ctx = filter->ctx;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003563
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003564 flt_update_offsets(filter, chn, ret);
3565 flt_ctx->cur_len[CHN_IDX(chn)] += ret;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003566 }
3567
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003568 lua_pushinteger(L, ret);
3569 return 1;
3570}
3571/* Replaces a given amount of input data at the given offset by a string
3572 * content. By default all remaining data are removed (offset = 0 and len =
3573 * -1). It returns the length of the written string, or -1 if the channel is
3574 * closed or if the buffer size is too little for the data.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003575 *
3576 * For a filter, the context is updated on success.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003577 */
3578__LJMP static int hlua_channel_set_data(lua_State *L)
3579{
3580 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003581 struct filter *filter;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003582 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003583 size_t sz, input, output;
3584 int ret, offset, len;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003585
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003586 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
3587 WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments"));
3588 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3589 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003590
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003591 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003592 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003593 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003594 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003595
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003596 output = co_data(chn);
3597 input = ci_data(chn);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003598
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003599 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3600 if (filter && !hlua_filter_from_payload(filter))
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003601 WILL_LJMP(lua_error(L));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003602
3603 offset = output;
3604 if (lua_gettop(L) > 2) {
3605 offset = MAY_LJMP(luaL_checkinteger(L, 3));
3606 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003607 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003608 offset += output;
3609 if (offset < output || offset > input + output) {
3610 lua_pushfstring(L, "offset out of range.");
3611 WILL_LJMP(lua_error(L));
3612 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003613 }
3614
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003615 len = output + input - offset;
3616 if (lua_gettop(L) == 4) {
3617 len = MAY_LJMP(luaL_checkinteger(L, 4));
3618 if (!len)
3619 goto set;
3620 if (len == -1)
3621 len = output + input - offset;
3622 if (len < 0 || offset + len > output + input) {
3623 lua_pushfstring(L, "length out of range.");
3624 WILL_LJMP(lua_error(L));
3625 }
3626 }
3627
3628 set:
Christopher Faulet23976d92021-08-06 09:59:49 +02003629 /* Be sure we can copied the string once input data will be removed. */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003630 if (sz > c_room(chn) + len)
Christopher Faulet23976d92021-08-06 09:59:49 +02003631 lua_pushinteger(L, -1);
3632 else {
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003633 _hlua_channel_delete(chn, offset, len);
3634 ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset);
3635 if (filter) {
3636 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3637
3638 len -= (ret > 0 ? ret : 0);
3639 flt_update_offsets(filter, chn, -len);
3640 flt_ctx->cur_len[CHN_IDX(chn)] -= len;
3641 }
3642
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003643 lua_pushinteger(L, ret);
Christopher Faulet23976d92021-08-06 09:59:49 +02003644 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003645 return 1;
3646}
3647
3648/* Removes a given amount of input data at the given offset. By default all
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003649 * input data are removed (offset = 0 and len = -1). It returns the amount of
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003650 * the removed data.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003651 *
3652 * For a filter, the context is updated on success.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003653 */
3654__LJMP static int hlua_channel_del_data(lua_State *L)
3655{
3656 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003657 struct filter *filter;
3658 size_t input, output;
3659 int offset, len;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003660
3661 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3662 WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments"));
3663 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003664
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003665 if (IS_HTX_STRM(chn_strm(chn))) {
3666 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3667 WILL_LJMP(lua_error(L));
3668 }
3669
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003670 output = co_data(chn);
3671 input = ci_data(chn);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003672
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003673 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3674 if (filter && !hlua_filter_from_payload(filter))
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003675 WILL_LJMP(lua_error(L));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003676
3677 offset = output;
3678 if (lua_gettop(L) > 2) {
3679 offset = MAY_LJMP(luaL_checkinteger(L, 3));
3680 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02003681 offset = MAX(0, (int)input + offset);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003682 offset += output;
3683 if (offset < output || offset > input + output) {
3684 lua_pushfstring(L, "offset out of range.");
3685 WILL_LJMP(lua_error(L));
3686 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003687 }
3688
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003689 len = output + input - offset;
3690 if (lua_gettop(L) == 4) {
3691 len = MAY_LJMP(luaL_checkinteger(L, 4));
3692 if (!len)
3693 goto end;
3694 if (len == -1)
3695 len = output + input - offset;
3696 if (len < 0 || offset + len > output + input) {
3697 lua_pushfstring(L, "length out of range.");
3698 WILL_LJMP(lua_error(L));
3699 }
3700 }
3701
3702 _hlua_channel_delete(chn, offset, len);
3703 if (filter) {
3704 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3705
3706 flt_update_offsets(filter, chn, -len);
3707 flt_ctx->cur_len[CHN_IDX(chn)] -= len;
3708 }
3709
3710 end:
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003711 lua_pushinteger(L, len);
Christopher Faulet23976d92021-08-06 09:59:49 +02003712 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003713}
3714
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003715/* Append data in the output side of the buffer. This data is immediately
3716 * sent. The function returns the amount of data written. If the buffer
3717 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003718 * if the channel is closed.
3719 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003720__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003721{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003722 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003723 struct filter *filter;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003724 const char *str;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003725 size_t offset, len, sz;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003726 int l, ret;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003727 struct hlua *hlua;
3728
3729 /* Get hlua struct, or NULL if we execute from main lua state */
3730 hlua = hlua_gethlua(L);
3731 if (!hlua) {
3732 lua_pushnil(L);
3733 return 1;
3734 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003735
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003736 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3737 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3738 l = MAY_LJMP(luaL_checkinteger(L, 3));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003739
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003740 offset = co_data(chn);
3741 len = ci_data(chn);
3742
3743 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3744 if (filter && !hlua_filter_from_payload(filter))
3745 WILL_LJMP(lua_error(L));
3746
3747
Willy Tarreau47860ed2015-03-10 14:07:50 +01003748 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003749 lua_pushinteger(L, -1);
3750 return 1;
3751 }
3752
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003753 len = c_room(chn);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003754 if (len > sz -l) {
3755 if (filter) {
3756 lua_pushinteger(L, -1);
3757 return 1;
3758 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003759 len = sz - l;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003760 }
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003761
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003762 ret = _hlua_channel_insert(chn, L, ist2(str, len), offset);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003763 if (ret == -1) {
3764 lua_pop(L, 1);
3765 lua_pushinteger(L, -1);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003766 return 1;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003767 }
3768 if (ret) {
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003769 if (filter) {
3770 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3771
3772
3773 flt_update_offsets(filter, chn, ret);
3774 FLT_OFF(filter, chn) += ret;
3775 flt_ctx->cur_off[CHN_IDX(chn)] += ret;
3776 }
3777 else
3778 c_adv(chn, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003779
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003780 l += ret;
3781 lua_pop(L, 1);
3782 lua_pushinteger(L, l);
3783 }
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003784
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003785 if (l < sz) {
3786 /* Yield only if the channel's output is not empty.
3787 * Otherwise it means we cannot add more data. */
3788 if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L)))
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003789 return 1;
3790
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003791 /* If we are waiting for space in the response buffer, we
3792 * must set the flag WAKERESWR. This flag required the task
3793 * wake up if any activity is detected on the response buffer.
3794 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003795 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003796 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003797 else
3798 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003799 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003800 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003801
3802 return 1;
3803}
3804
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003805/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003806 * yield the LUA process, and resume it without checking the
3807 * input arguments.
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003808 *
3809 * This function cannot be called from a filter.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003810 */
3811__LJMP static int hlua_channel_send(lua_State *L)
3812{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003813 struct channel *chn;
3814
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003815 MAY_LJMP(check_args(L, 2, "send"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003816 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3817 if (IS_HTX_STRM(chn_strm(chn))) {
3818 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3819 WILL_LJMP(lua_error(L));
3820 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003821 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003822 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003823}
3824
3825/* This function forward and amount of butes. The data pass from
3826 * the input side of the buffer to the output side, and can be
3827 * forwarded. This function never fails.
3828 *
3829 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003830 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003831 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003832__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003833{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003834 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003835 struct filter *filter;
3836 size_t offset, len, fwd;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003837 int l, max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003838 struct hlua *hlua;
3839
3840 /* Get hlua struct, or NULL if we execute from main lua state */
3841 hlua = hlua_gethlua(L);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003842 if (!hlua) {
3843 lua_pushnil(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003844 return 1;
Thierry Fournier77016da2020-08-15 14:35:51 +02003845 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003846
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003847 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003848 fwd = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003849 l = MAY_LJMP(luaL_checkinteger(L, -1));
3850
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003851 offset = co_data(chn);
3852 len = ci_data(chn);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003853
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003854 filter = hlua_channel_filter(L, 1, chn, &offset, &len);
3855 if (filter && !hlua_filter_from_payload(filter))
3856 WILL_LJMP(lua_error(L));
3857
3858 max = fwd - l;
3859 if (max > len)
3860 max = len;
3861
3862 if (filter) {
3863 struct hlua_flt_ctx *flt_ctx = filter->ctx;
3864
3865 FLT_OFF(filter, chn) += max;
3866 flt_ctx->cur_off[CHN_IDX(chn)] += max;
3867 flt_ctx->cur_len[CHN_IDX(chn)] -= max;
3868 }
3869 else
3870 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003871
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003872 l += max;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003873 lua_pop(L, 1);
3874 lua_pushinteger(L, l);
3875
3876 /* Check if it miss bytes to forward. */
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003877 if (l < fwd) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003878 /* The the input channel or the output channel are closed, we
3879 * must return the amount of data forwarded.
3880 */
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003881 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003882 return 1;
3883
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003884 /* If we are waiting for space data in the response buffer, we
3885 * must set the flag WAKERESWR. This flag required the task
3886 * wake up if any activity is detected on the response buffer.
3887 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003888 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003889 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003890 else
3891 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003892
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003893 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003894 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003895 }
3896
3897 return 1;
3898}
3899
3900/* Just check the input and prepare the stack for the previous
3901 * function "hlua_channel_forward_yield"
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003902 *
3903 * This function cannot be called from a filter.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003904 */
3905__LJMP static int hlua_channel_forward(lua_State *L)
3906{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003907 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003908
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003909 MAY_LJMP(check_args(L, 2, "forward"));
3910 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3911 if (IS_HTX_STRM(chn_strm(chn))) {
3912 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3913 WILL_LJMP(lua_error(L));
3914 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003915 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003916 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003917}
3918
3919/* Just returns the number of bytes available in the input
3920 * side of the buffer. This function never fails.
3921 */
3922__LJMP static int hlua_channel_get_in_len(lua_State *L)
3923{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003924 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003925 struct filter *filter;
3926 size_t output, input;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003927
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003928 MAY_LJMP(check_args(L, 1, "input"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003929 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003930
3931 output = co_data(chn);
3932 input = ci_data(chn);
3933 filter = hlua_channel_filter(L, 1, chn, &output, &input);
3934 if (filter || !IS_HTX_STRM(chn_strm(chn)))
3935 lua_pushinteger(L, input);
3936 else {
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003937 struct htx *htx = htxbuf(&chn->buf);
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003938
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003939 lua_pushinteger(L, htx->data - co_data(chn));
3940 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003941 return 1;
3942}
3943
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003944/* Returns true if the channel is full. */
3945__LJMP static int hlua_channel_is_full(lua_State *L)
3946{
3947 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003948
3949 MAY_LJMP(check_args(L, 1, "is_full"));
3950 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003951 /* ignore the reserve, we are not on a producer side (ie in an
3952 * applet).
3953 */
3954 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003955 return 1;
3956}
3957
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003958/* Returns true if the channel may still receive data. */
3959__LJMP static int hlua_channel_may_recv(lua_State *L)
3960{
3961 struct channel *chn;
3962
3963 MAY_LJMP(check_args(L, 1, "may_recv"));
3964 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3965 lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn)));
3966 return 1;
3967}
3968
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003969/* Returns true if the channel is the response channel. */
3970__LJMP static int hlua_channel_is_resp(lua_State *L)
3971{
3972 struct channel *chn;
3973
3974 MAY_LJMP(check_args(L, 1, "is_resp"));
3975 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3976
3977 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3978 return 1;
3979}
3980
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003981/* Just returns the number of bytes available in the output
3982 * side of the buffer. This function never fails.
3983 */
3984__LJMP static int hlua_channel_get_out_len(lua_State *L)
3985{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003986 struct channel *chn;
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003987 size_t output, input;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003988
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003989 MAY_LJMP(check_args(L, 1, "output"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003990 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta1ac5fb2021-08-09 10:22:46 +02003991
3992 output = co_data(chn);
3993 input = ci_data(chn);
3994 hlua_channel_filter(L, 1, chn, &output, &input);
3995
3996 lua_pushinteger(L, output);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003997 return 1;
3998}
3999
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004000/*
4001 *
4002 *
4003 * Class Fetches
4004 *
4005 *
4006 */
4007
4008/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004009 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004010 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004011__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004012{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004013 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004014}
4015
4016/* This function creates and push in the stack a fetch object according
4017 * with a current TXN.
4018 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004019static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004020{
Willy Tarreau7073c472015-04-06 11:15:40 +02004021 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004022
4023 /* Check stack size. */
4024 if (!lua_checkstack(L, 3))
4025 return 0;
4026
4027 /* Create the object: obj[0] = userdata.
4028 * Note that the base of the Fetches object is the
4029 * transaction object.
4030 */
4031 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02004032 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004033 lua_rawseti(L, -2, 0);
4034
Willy Tarreau7073c472015-04-06 11:15:40 +02004035 hsmp->s = txn->s;
4036 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01004037 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004038 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004039
4040 /* Pop a class sesison metatable and affect it to the userdata. */
4041 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
4042 lua_setmetatable(L, -2);
4043
4044 return 1;
4045}
4046
4047/* This function is an LUA binding. It is called with each sample-fetch.
4048 * It uses closure argument to store the associated sample-fetch. It
4049 * returns only one argument or throws an error. An error is thrown
4050 * only if an error is encountered during the argument parsing. If
4051 * the "sample-fetch" function fails, nil is returned.
4052 */
4053__LJMP static int hlua_run_sample_fetch(lua_State *L)
4054{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02004055 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01004056 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02004057 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004058 int i;
4059 struct sample smp;
4060
4061 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004062 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004063
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004064 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02004065 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004066
Thierry FOURNIERca988662015-12-20 18:43:03 +01004067 /* Check execution authorization. */
4068 if (f->use & SMP_USE_HTTP_ANY &&
4069 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
4070 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
4071 "is not available in Lua services", f->kw);
4072 WILL_LJMP(lua_error(L));
4073 }
4074
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004075 /* Get extra arguments. */
4076 for (i = 0; i < lua_gettop(L) - 1; i++) {
4077 if (i >= ARGM_NBARGS)
4078 break;
4079 hlua_lua2arg(L, i + 2, &args[i]);
4080 }
4081 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004082 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004083
4084 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02004085 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004086
4087 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01004088 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004089 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004090 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004091 }
4092
4093 /* Initialise the sample. */
4094 memset(&smp, 0, sizeof(smp));
4095
4096 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01004097 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004098 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004099 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004100 lua_pushstring(L, "");
4101 else
4102 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004103 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004104 }
4105
4106 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004107 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004108 hlua_smp2lua_str(L, &smp);
4109 else
4110 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004111
4112 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004113 free_args(args);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004114 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004115
4116 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004117 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004118 WILL_LJMP(lua_error(L));
4119 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004120}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01004121
4122/*
4123 *
4124 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004125 * Class Converters
4126 *
4127 *
4128 */
4129
4130/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004131 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004132 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004133__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004134{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004135 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004136}
4137
4138/* This function creates and push in the stack a Converters object
4139 * according with a current TXN.
4140 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004141static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004142{
Willy Tarreau7073c472015-04-06 11:15:40 +02004143 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004144
4145 /* Check stack size. */
4146 if (!lua_checkstack(L, 3))
4147 return 0;
4148
4149 /* Create the object: obj[0] = userdata.
4150 * Note that the base of the Converters object is the
4151 * same than the TXN object.
4152 */
4153 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02004154 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004155 lua_rawseti(L, -2, 0);
4156
Willy Tarreau7073c472015-04-06 11:15:40 +02004157 hsmp->s = txn->s;
4158 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01004159 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004160 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004161
Willy Tarreau87b09662015-04-03 00:22:06 +02004162 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004163 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
4164 lua_setmetatable(L, -2);
4165
4166 return 1;
4167}
4168
4169/* This function is an LUA binding. It is called with each converter.
4170 * It uses closure argument to store the associated converter. It
4171 * returns only one argument or throws an error. An error is thrown
4172 * only if an error is encountered during the argument parsing. If
4173 * the converter function function fails, nil is returned.
4174 */
4175__LJMP static int hlua_run_sample_conv(lua_State *L)
4176{
Willy Tarreauda5f1082015-04-06 11:17:13 +02004177 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004178 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02004179 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004180 int i;
4181 struct sample smp;
4182
4183 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004184 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004185
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004186 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02004187 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004188
4189 /* Get extra arguments. */
4190 for (i = 0; i < lua_gettop(L) - 2; i++) {
4191 if (i >= ARGM_NBARGS)
4192 break;
4193 hlua_lua2arg(L, i + 3, &args[i]);
4194 }
4195 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004196 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004197
4198 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02004199 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004200
4201 /* Run the special args checker. */
4202 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
4203 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004204 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004205 }
4206
4207 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004208 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004209 if (!hlua_lua2smp(L, 2, &smp)) {
4210 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004211 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004212 }
4213
Willy Tarreau1777ea62016-03-10 16:15:46 +01004214 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
4215
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004216 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004217 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004218 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004219 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004220 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004221 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02004222 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
4223 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004224 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004225 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004226 }
4227
4228 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004229 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004230 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004231 lua_pushstring(L, "");
4232 else
Willy Tarreaua678b432015-08-28 10:14:59 +02004233 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004234 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004235 }
4236
4237 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004238 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004239 hlua_smp2lua_str(L, &smp);
4240 else
4241 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004242 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004243 free_args(args);
Willy Tarreaua678b432015-08-28 10:14:59 +02004244 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004245
4246 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004247 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004248 WILL_LJMP(lua_error(L));
4249 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004250}
4251
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004252/*
4253 *
4254 *
4255 * Class AppletTCP
4256 *
4257 *
4258 */
4259
4260/* Returns a struct hlua_txn if the stack entry "ud" is
4261 * a class stream, otherwise it throws an error.
4262 */
4263__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
4264{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004265 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004266}
4267
4268/* This function creates and push in the stack an Applet object
4269 * according with a current TXN.
4270 */
4271static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
4272{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004273 struct hlua_appctx *luactx;
Christopher Faulet86e1c332021-12-20 17:09:39 +01004274 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004275 struct stream *s = si_strm(si);
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004276 struct proxy *p;
4277
4278 ALREADY_CHECKED(s);
4279 p = s->be;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004280
4281 /* Check stack size. */
4282 if (!lua_checkstack(L, 3))
4283 return 0;
4284
4285 /* Create the object: obj[0] = userdata.
4286 * Note that the base of the Converters object is the
4287 * same than the TXN object.
4288 */
4289 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004290 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004291 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004292 luactx->appctx = ctx;
4293 luactx->htxn.s = s;
4294 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004295
4296 /* Create the "f" field that contains a list of fetches. */
4297 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004298 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004299 return 0;
4300 lua_settable(L, -3);
4301
4302 /* Create the "sf" field that contains a list of stringsafe fetches. */
4303 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004304 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004305 return 0;
4306 lua_settable(L, -3);
4307
4308 /* Create the "c" field that contains a list of converters. */
4309 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004310 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004311 return 0;
4312 lua_settable(L, -3);
4313
4314 /* Create the "sc" field that contains a list of stringsafe converters. */
4315 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004316 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004317 return 0;
4318 lua_settable(L, -3);
4319
4320 /* Pop a class stream metatable and affect it to the table. */
4321 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
4322 lua_setmetatable(L, -2);
4323
4324 return 1;
4325}
4326
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004327__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
4328{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004329 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004330 struct stream *s;
4331 const char *name;
4332 size_t len;
4333 struct sample smp;
4334
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004335 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4336 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004337
4338 /* It is useles to retrieve the stream, but this function
4339 * runs only in a stream context.
4340 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004341 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004342 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004343 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004344
4345 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004346 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004347 hlua_lua2smp(L, 3, &smp);
4348
4349 /* Store the sample in a variable. */
4350 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004351
4352 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4353 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4354 else
4355 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4356
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004357 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004358}
4359
4360__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
4361{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004362 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004363 struct stream *s;
4364 const char *name;
4365 size_t len;
4366 struct sample smp;
4367
4368 MAY_LJMP(check_args(L, 2, "unset_var"));
4369
4370 /* It is useles to retrieve the stream, but this function
4371 * runs only in a stream context.
4372 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004373 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004374 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004375 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004376
4377 /* Unset the variable. */
4378 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004379 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4380 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004381}
4382
4383__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
4384{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004385 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004386 struct stream *s;
4387 const char *name;
4388 size_t len;
4389 struct sample smp;
4390
4391 MAY_LJMP(check_args(L, 2, "get_var"));
4392
4393 /* It is useles to retrieve the stream, but this function
4394 * runs only in a stream context.
4395 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004396 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004397 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004398 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004399
4400 smp_set_owner(&smp, s->be, s->sess, s, 0);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02004401 if (!vars_get_by_name(name, len, &smp, NULL)) {
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004402 lua_pushnil(L);
4403 return 1;
4404 }
4405
4406 return hlua_smp2lua(L, &smp);
4407}
4408
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004409__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
4410{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004411 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4412 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004413 struct hlua *hlua;
4414
4415 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004416 if (!s->hlua)
4417 return 0;
4418 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004419
4420 MAY_LJMP(check_args(L, 2, "set_priv"));
4421
4422 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004423 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004424
4425 /* Get and store new value. */
4426 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4427 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4428
4429 return 0;
4430}
4431
4432__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
4433{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004434 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4435 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004436 struct hlua *hlua;
4437
4438 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004439 if (!s->hlua) {
4440 lua_pushnil(L);
4441 return 1;
4442 }
4443 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004444
4445 /* Push configuration index in the stack. */
4446 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4447
4448 return 1;
4449}
4450
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004451/* If expected data not yet available, it returns a yield. This function
4452 * consumes the data in the buffer. It returns a string containing the
4453 * data. This string can be empty.
4454 */
4455__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
4456{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004457 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01004458 struct stream_interface *si = cs_si(luactx->appctx->owner);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004459 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004460 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004461 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004462 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004463 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004464
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004465 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004466 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004467
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004468 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004469 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004470 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004471 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004472 }
4473
4474 /* End of data: commit the total strings and return. */
4475 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004476 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004477 return 1;
4478 }
4479
4480 /* Ensure that the block 2 length is usable. */
4481 if (ret == 1)
4482 len2 = 0;
4483
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004484 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004485 luaL_addlstring(&luactx->b, blk1, len1);
4486 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004487
4488 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004489 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004490 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004491 return 1;
4492}
4493
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004494/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004495__LJMP static int hlua_applet_tcp_getline(lua_State *L)
4496{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004497 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004498
4499 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004500 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004501
4502 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
4503}
4504
4505/* If expected data not yet available, it returns a yield. This function
4506 * consumes the data in the buffer. It returns a string containing the
4507 * data. This string can be empty.
4508 */
4509__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
4510{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004511 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01004512 struct stream_interface *si = cs_si(luactx->appctx->owner);
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004513 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004514 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004515 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004516 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004517 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004518 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004519
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004520 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004521 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004522
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004523 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004524 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004525 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004526 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004527 }
4528
4529 /* End of data: commit the total strings and return. */
4530 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004531 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004532 return 1;
4533 }
4534
4535 /* Ensure that the block 2 length is usable. */
4536 if (ret == 1)
4537 len2 = 0;
4538
4539 if (len == -1) {
4540
4541 /* If len == -1, catenate all the data avalaile and
4542 * yield because we want to get all the data until
4543 * the end of data stream.
4544 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004545 luaL_addlstring(&luactx->b, blk1, len1);
4546 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004547 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004548 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004549 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004550
4551 } else {
4552
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004553 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004554 if (len1 > len)
4555 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004556 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004557 len -= len1;
4558
4559 /* Copy the second block. */
4560 if (len2 > len)
4561 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004562 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004563 len -= len2;
4564
4565 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004566 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004567
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004568 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004569 if (len > 0) {
4570 lua_pushinteger(L, len);
4571 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004572 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004573 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004574 }
4575
4576 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004577 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004578 return 1;
4579 }
4580
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004581 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004582 hlua_pusherror(L, "Lua: internal error");
4583 WILL_LJMP(lua_error(L));
4584 return 0;
4585}
4586
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004587/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004588__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4589{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004590 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004591 int len = -1;
4592
4593 if (lua_gettop(L) > 2)
4594 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4595 if (lua_gettop(L) >= 2) {
4596 len = MAY_LJMP(luaL_checkinteger(L, 2));
4597 lua_pop(L, 1);
4598 }
4599
4600 /* Confirm or set the required length */
4601 lua_pushinteger(L, len);
4602
4603 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004604 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004605
4606 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4607}
4608
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004609/* Append data in the output side of the buffer. This data is immediately
4610 * sent. The function returns the amount of data written. If the buffer
4611 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004612 * if the channel is closed.
4613 */
4614__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4615{
4616 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004617 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004618 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4619 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Christopher Faulet86e1c332021-12-20 17:09:39 +01004620 struct stream_interface *si = cs_si(luactx->appctx->owner);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004621 struct channel *chn = si_ic(si);
4622 int max;
4623
4624 /* Get the max amount of data which can write as input in the channel. */
4625 max = channel_recv_max(chn);
4626 if (max > (len - l))
4627 max = len - l;
4628
4629 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004630 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004631
4632 /* update counters. */
4633 l += max;
4634 lua_pop(L, 1);
4635 lua_pushinteger(L, l);
4636
4637 /* If some data is not send, declares the situation to the
4638 * applet, and returns a yield.
4639 */
4640 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004641 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004642 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004643 }
4644
4645 return 1;
4646}
4647
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004648/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004649 * yield the LUA process, and resume it without checking the
4650 * input arguments.
4651 */
4652__LJMP static int hlua_applet_tcp_send(lua_State *L)
4653{
4654 MAY_LJMP(check_args(L, 2, "send"));
4655 lua_pushinteger(L, 0);
4656
4657 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4658}
4659
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004660/*
4661 *
4662 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004663 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004664 *
4665 *
4666 */
4667
4668/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004669 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004670 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004671__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004672{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004673 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004674}
4675
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004676/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004677 * according with a current TXN.
4678 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004679static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004680{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004681 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004682 struct hlua_txn htxn;
Christopher Faulet86e1c332021-12-20 17:09:39 +01004683 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004684 struct stream *s = si_strm(si);
4685 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004686 struct htx *htx;
4687 struct htx_blk *blk;
4688 struct htx_sl *sl;
4689 struct ist path;
4690 unsigned long long len = 0;
4691 int32_t pos;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004692 struct http_uri_parser parser;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004693
4694 /* Check stack size. */
4695 if (!lua_checkstack(L, 3))
4696 return 0;
4697
4698 /* Create the object: obj[0] = userdata.
4699 * Note that the base of the Converters object is the
4700 * same than the TXN object.
4701 */
4702 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004703 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004704 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004705 luactx->appctx = ctx;
4706 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4707 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4708 luactx->htxn.s = s;
4709 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004710
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004711 /* Create the "f" field that contains a list of fetches. */
4712 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004713 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004714 return 0;
4715 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004716
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004717 /* Create the "sf" field that contains a list of stringsafe fetches. */
4718 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004719 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004720 return 0;
4721 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004722
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004723 /* Create the "c" field that contains a list of converters. */
4724 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004725 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004726 return 0;
4727 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004728
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004729 /* Create the "sc" field that contains a list of stringsafe converters. */
4730 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004731 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004732 return 0;
4733 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004734
Christopher Fauleta2097962019-07-15 16:25:33 +02004735 htx = htxbuf(&s->req.buf);
4736 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004737 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004738 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004739
Christopher Fauleta2097962019-07-15 16:25:33 +02004740 /* Stores the request method. */
4741 lua_pushstring(L, "method");
4742 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4743 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004744
Christopher Fauleta2097962019-07-15 16:25:33 +02004745 /* Stores the http version. */
4746 lua_pushstring(L, "version");
4747 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4748 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004749
Christopher Fauleta2097962019-07-15 16:25:33 +02004750 /* creates an array of headers. hlua_http_get_headers() crates and push
4751 * the array on the top of the stack.
4752 */
4753 lua_pushstring(L, "headers");
4754 htxn.s = s;
4755 htxn.p = px;
4756 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004757 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004758 return 0;
4759 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004760
Amaury Denoyellec453f952021-07-06 11:40:12 +02004761 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4762 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004763 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004764 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004765
Christopher Fauleta2097962019-07-15 16:25:33 +02004766 p = path.ptr;
Tim Duesterhus4c8f75f2021-11-06 15:14:44 +01004767 end = istend(path);
Christopher Fauleta2097962019-07-15 16:25:33 +02004768 q = p;
4769 while (q < end && *q != '?')
4770 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004771
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004772 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004773 lua_pushstring(L, "path");
4774 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004775 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004776
Christopher Fauleta2097962019-07-15 16:25:33 +02004777 /* Stores the query string. */
4778 lua_pushstring(L, "qs");
4779 if (*q == '?')
4780 q++;
4781 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004782 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004783 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004784
Christopher Fauleta2097962019-07-15 16:25:33 +02004785 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4786 struct htx_blk *blk = htx_get_blk(htx, pos);
4787 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004788
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004789 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004790 break;
4791 if (type == HTX_BLK_DATA)
4792 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004793 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004794 if (htx->extra != ULLONG_MAX)
4795 len += htx->extra;
4796
4797 /* Stores the request path. */
4798 lua_pushstring(L, "length");
4799 lua_pushinteger(L, len);
4800 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004801
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004802 /* Create an empty array of HTTP request headers. */
4803 lua_pushstring(L, "response");
4804 lua_newtable(L);
4805 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004806
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004807 /* Pop a class stream metatable and affect it to the table. */
4808 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4809 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004810
4811 return 1;
4812}
4813
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004814__LJMP static int hlua_applet_http_set_var(lua_State *L)
4815{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004816 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004817 struct stream *s;
4818 const char *name;
4819 size_t len;
4820 struct sample smp;
4821
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004822 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4823 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004824
4825 /* It is useles to retrieve the stream, but this function
4826 * runs only in a stream context.
4827 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004828 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004829 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004830 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004831
4832 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004833 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004834 hlua_lua2smp(L, 3, &smp);
4835
4836 /* Store the sample in a variable. */
4837 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004838
4839 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4840 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4841 else
4842 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4843
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004844 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004845}
4846
4847__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4848{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004849 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004850 struct stream *s;
4851 const char *name;
4852 size_t len;
4853 struct sample smp;
4854
4855 MAY_LJMP(check_args(L, 2, "unset_var"));
4856
4857 /* It is useles to retrieve the stream, but this function
4858 * runs only in a stream context.
4859 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004860 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004861 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004862 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004863
4864 /* Unset the variable. */
4865 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004866 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4867 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004868}
4869
4870__LJMP static int hlua_applet_http_get_var(lua_State *L)
4871{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004872 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004873 struct stream *s;
4874 const char *name;
4875 size_t len;
4876 struct sample smp;
4877
4878 MAY_LJMP(check_args(L, 2, "get_var"));
4879
4880 /* It is useles to retrieve the stream, but this function
4881 * runs only in a stream context.
4882 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004883 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004884 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004885 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004886
4887 smp_set_owner(&smp, s->be, s->sess, s, 0);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02004888 if (!vars_get_by_name(name, len, &smp, NULL)) {
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004889 lua_pushnil(L);
4890 return 1;
4891 }
4892
4893 return hlua_smp2lua(L, &smp);
4894}
4895
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004896__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4897{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004898 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4899 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004900 struct hlua *hlua;
4901
4902 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004903 if (!s->hlua)
4904 return 0;
4905 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004906
4907 MAY_LJMP(check_args(L, 2, "set_priv"));
4908
4909 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004910 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004911
4912 /* Get and store new value. */
4913 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4914 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4915
4916 return 0;
4917}
4918
4919__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4920{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004921 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4922 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004923 struct hlua *hlua;
4924
4925 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004926 if (!s->hlua) {
4927 lua_pushnil(L);
4928 return 1;
4929 }
4930 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004931
4932 /* Push configuration index in the stack. */
4933 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4934
4935 return 1;
4936}
4937
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004938/* If expected data not yet available, it returns a yield. This function
4939 * consumes the data in the buffer. It returns a string containing the
4940 * data. This string can be empty.
4941 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004942__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004943{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004944 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01004945 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004946 struct channel *req = si_oc(si);
4947 struct htx *htx;
4948 struct htx_blk *blk;
4949 size_t count;
4950 int stop = 0;
4951
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004952 htx = htx_from_buf(&req->buf);
4953 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004954 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004955
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004956 while (count && !stop && blk) {
4957 enum htx_blk_type type = htx_get_blk_type(blk);
4958 uint32_t sz = htx_get_blksz(blk);
4959 struct ist v;
4960 uint32_t vlen;
4961 char *nl;
4962
4963 vlen = sz;
4964 if (vlen > count) {
4965 if (type != HTX_BLK_DATA)
4966 break;
4967 vlen = count;
4968 }
4969
4970 switch (type) {
4971 case HTX_BLK_UNUSED:
4972 break;
4973
4974 case HTX_BLK_DATA:
4975 v = htx_get_blk_value(htx, blk);
4976 v.len = vlen;
4977 nl = istchr(v, '\n');
4978 if (nl != NULL) {
4979 stop = 1;
4980 vlen = nl - v.ptr + 1;
4981 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004982 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004983 break;
4984
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004985 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004986 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004987 stop = 1;
4988 break;
4989
4990 default:
4991 break;
4992 }
4993
4994 co_set_data(req, co_data(req) - vlen);
4995 count -= vlen;
4996 if (sz == vlen)
4997 blk = htx_remove_blk(htx, blk);
4998 else {
4999 htx_cut_data_blk(htx, blk, vlen);
5000 break;
5001 }
5002 }
5003
Christopher Fauleteccb31c2021-04-02 14:24:56 +02005004 /* The message was fully consumed and no more data are expected
5005 * (EOM flag set).
5006 */
5007 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
5008 stop = 1;
5009
Christopher Faulet0ae79d02019-02-27 21:36:59 +01005010 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005011 if (!stop) {
5012 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02005013 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005014 }
5015
5016 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005017 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005018 return 1;
5019}
5020
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005021
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005022/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005023__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005024{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005025 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005026
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005027 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005028 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005029
Christopher Fauleta2097962019-07-15 16:25:33 +02005030 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005031}
5032
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005033/* If expected data not yet available, it returns a yield. This function
5034 * consumes the data in the buffer. It returns a string containing the
5035 * data. This string can be empty.
5036 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005037__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005038{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005039 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01005040 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005041 struct channel *req = si_oc(si);
5042 struct htx *htx;
5043 struct htx_blk *blk;
5044 size_t count;
5045 int len;
5046
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005047 htx = htx_from_buf(&req->buf);
5048 len = MAY_LJMP(luaL_checkinteger(L, 2));
5049 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02005050 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005051 while (count && len && blk) {
5052 enum htx_blk_type type = htx_get_blk_type(blk);
5053 uint32_t sz = htx_get_blksz(blk);
5054 struct ist v;
5055 uint32_t vlen;
5056
5057 vlen = sz;
5058 if (len > 0 && vlen > len)
5059 vlen = len;
5060 if (vlen > count) {
5061 if (type != HTX_BLK_DATA)
5062 break;
5063 vlen = count;
5064 }
5065
5066 switch (type) {
5067 case HTX_BLK_UNUSED:
5068 break;
5069
5070 case HTX_BLK_DATA:
5071 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02005072 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005073 break;
5074
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005075 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005076 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005077 len = 0;
5078 break;
5079
5080 default:
5081 break;
5082 }
5083
5084 co_set_data(req, co_data(req) - vlen);
5085 count -= vlen;
5086 if (len > 0)
5087 len -= vlen;
5088 if (sz == vlen)
5089 blk = htx_remove_blk(htx, blk);
5090 else {
5091 htx_cut_data_blk(htx, blk, vlen);
5092 break;
5093 }
5094 }
5095
Christopher Fauleteccb31c2021-04-02 14:24:56 +02005096 /* The message was fully consumed and no more data are expected
5097 * (EOM flag set).
5098 */
5099 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
5100 len = 0;
5101
Christopher Faulet0ae79d02019-02-27 21:36:59 +01005102 htx_to_buf(htx, &req->buf);
5103
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005104 /* If we are no other data available, yield waiting for new data. */
5105 if (len) {
5106 if (len > 0) {
5107 lua_pushinteger(L, len);
5108 lua_replace(L, 2);
5109 }
5110 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005111 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005112 }
5113
5114 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005115 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005116 return 1;
5117}
5118
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005119/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005120__LJMP static int hlua_applet_http_recv(lua_State *L)
5121{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005122 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005123 int len = -1;
5124
5125 /* Check arguments. */
5126 if (lua_gettop(L) > 2)
5127 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
5128 if (lua_gettop(L) >= 2) {
5129 len = MAY_LJMP(luaL_checkinteger(L, 2));
5130 lua_pop(L, 1);
5131 }
5132
Christopher Fauleta2097962019-07-15 16:25:33 +02005133 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005134
Christopher Fauleta2097962019-07-15 16:25:33 +02005135 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005136 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005137
Christopher Fauleta2097962019-07-15 16:25:33 +02005138 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005139}
5140
5141/* Append data in the output side of the buffer. This data is immediately
5142 * sent. The function returns the amount of data written. If the buffer
5143 * cannot contain the data, the function yields. The function returns -1
5144 * if the channel is closed.
5145 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005146__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005147{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005148 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01005149 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005150 struct channel *res = si_ic(si);
5151 struct htx *htx = htx_from_buf(&res->buf);
5152 const char *data;
5153 size_t len;
5154 int l = MAY_LJMP(luaL_checkinteger(L, 3));
5155 int max;
5156
Christopher Faulet9060fc02019-07-03 11:39:30 +02005157 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01005158 if (!max)
5159 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005160
5161 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
5162
5163 /* Get the max amount of data which can write as input in the channel. */
5164 if (max > (len - l))
5165 max = len - l;
5166
5167 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005168 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01005169 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005170
5171 /* update counters. */
5172 l += max;
5173 lua_pop(L, 1);
5174 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005175
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005176 /* If some data is not send, declares the situation to the
5177 * applet, and returns a yield.
5178 */
5179 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01005180 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01005181 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005182 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005183 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005184 }
5185
Christopher Fauleta2097962019-07-15 16:25:33 +02005186 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005187 return 1;
5188}
5189
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005190/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005191 * yield the LUA process, and resume it without checking the
5192 * input arguments.
5193 */
5194__LJMP static int hlua_applet_http_send(lua_State *L)
5195{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005196 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005197
5198 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005199 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005200 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
5201 WILL_LJMP(lua_error(L));
5202 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005203
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005204 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02005205 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005206
Christopher Fauleta2097962019-07-15 16:25:33 +02005207 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005208}
5209
5210__LJMP static int hlua_applet_http_addheader(lua_State *L)
5211{
5212 const char *name;
5213 int ret;
5214
5215 MAY_LJMP(hlua_checkapplet_http(L, 1));
5216 name = MAY_LJMP(luaL_checkstring(L, 2));
5217 MAY_LJMP(luaL_checkstring(L, 3));
5218
5219 /* Push in the stack the "response" entry. */
5220 ret = lua_getfield(L, 1, "response");
5221 if (ret != LUA_TTABLE) {
5222 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
5223 "is expected as an array. %s found", lua_typename(L, ret));
5224 WILL_LJMP(lua_error(L));
5225 }
5226
5227 /* check if the header is already registered if it is not
5228 * the case, register it.
5229 */
5230 ret = lua_getfield(L, -1, name);
5231 if (ret == LUA_TNIL) {
5232
5233 /* Entry not found. */
5234 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
5235
5236 /* Insert the new header name in the array in the top of the stack.
5237 * It left the new array in the top of the stack.
5238 */
5239 lua_newtable(L);
5240 lua_pushvalue(L, 2);
5241 lua_pushvalue(L, -2);
5242 lua_settable(L, -4);
5243
5244 } else if (ret != LUA_TTABLE) {
5245
5246 /* corruption error. */
5247 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
5248 "is expected as an array. %s found", name, lua_typename(L, ret));
5249 WILL_LJMP(lua_error(L));
5250 }
5251
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07005252 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005253 * the header value as new entry.
5254 */
5255 lua_pushvalue(L, 3);
5256 ret = lua_rawlen(L, -2);
5257 lua_rawseti(L, -2, ret + 1);
5258 lua_pushboolean(L, 1);
5259 return 1;
5260}
5261
5262__LJMP static int hlua_applet_http_status(lua_State *L)
5263{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005264 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005265 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005266 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005267
5268 if (status < 100 || status > 599) {
5269 lua_pushboolean(L, 0);
5270 return 1;
5271 }
5272
Willy Tarreau7e702d12021-04-28 17:59:21 +02005273 luactx->appctx->ctx.hlua_apphttp.status = status;
5274 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005275 lua_pushboolean(L, 1);
5276 return 1;
5277}
5278
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005279
Christopher Fauleta2097962019-07-15 16:25:33 +02005280__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005281{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005282 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01005283 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005284 struct channel *res = si_ic(si);
5285 struct htx *htx;
5286 struct htx_sl *sl;
5287 struct h1m h1m;
5288 const char *status, *reason;
5289 const char *name, *value;
5290 size_t nlen, vlen;
5291 unsigned int flags;
5292
5293 /* Send the message at once. */
5294 htx = htx_from_buf(&res->buf);
5295 h1m_init_res(&h1m);
5296
5297 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005298 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
5299 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005300 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02005301 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
5302 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005303 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5304 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
5305 }
5306 else {
5307 flags = HTX_SL_F_IS_RESP;
5308 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
5309 }
5310 if (!sl) {
5311 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005312 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005313 WILL_LJMP(lua_error(L));
5314 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02005315 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005316
5317 /* Get the array associated to the field "response" in the object AppletHTTP. */
5318 lua_pushvalue(L, 0);
5319 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5320 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005321 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005322 WILL_LJMP(lua_error(L));
5323 }
5324
5325 /* Browse the list of headers. */
5326 lua_pushnil(L);
5327 while(lua_next(L, -2) != 0) {
5328 /* We expect a string as -2. */
5329 if (lua_type(L, -2) != LUA_TSTRING) {
5330 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005331 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005332 lua_typename(L, lua_type(L, -2)));
5333 WILL_LJMP(lua_error(L));
5334 }
5335 name = lua_tolstring(L, -2, &nlen);
5336
5337 /* We expect an array as -1. */
5338 if (lua_type(L, -1) != LUA_TTABLE) {
5339 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 +02005340 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005341 name,
5342 lua_typename(L, lua_type(L, -1)));
5343 WILL_LJMP(lua_error(L));
5344 }
5345
5346 /* Browse the table who is on the top of the stack. */
5347 lua_pushnil(L);
5348 while(lua_next(L, -2) != 0) {
5349 int id;
5350
5351 /* We expect a number as -2. */
5352 if (lua_type(L, -2) != LUA_TNUMBER) {
5353 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 +02005354 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005355 name,
5356 lua_typename(L, lua_type(L, -2)));
5357 WILL_LJMP(lua_error(L));
5358 }
5359 id = lua_tointeger(L, -2);
5360
5361 /* We expect a string as -2. */
5362 if (lua_type(L, -1) != LUA_TSTRING) {
5363 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 +02005364 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005365 name, id,
5366 lua_typename(L, lua_type(L, -1)));
5367 WILL_LJMP(lua_error(L));
5368 }
5369 value = lua_tolstring(L, -1, &vlen);
5370
5371 /* Simple Protocol checks. */
Christopher Faulet545fbba2021-09-28 09:36:25 +02005372 if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) {
5373 int ret;
5374
5375 ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
5376 if (ret < 0) {
5377 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
5378 luactx->appctx->rule->arg.hlua_rule->fcn->name,
5379 name);
5380 WILL_LJMP(lua_error(L));
5381 }
5382 else if (ret == 0)
5383 goto next; /* Skip it */
5384 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005385 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
5386 struct ist v = ist2(value, vlen);
5387 int ret;
5388
5389 ret = h1_parse_cont_len_header(&h1m, &v);
5390 if (ret < 0) {
5391 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005392 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005393 name);
5394 WILL_LJMP(lua_error(L));
5395 }
5396 else if (ret == 0)
5397 goto next; /* Skip it */
5398 }
5399
5400 /* Add a new header */
5401 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
5402 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005403 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005404 name);
5405 WILL_LJMP(lua_error(L));
5406 }
5407 next:
5408 /* Remove the array from the stack, and get next element with a remaining string. */
5409 lua_pop(L, 1);
5410 }
5411
5412 /* Remove the array from the stack, and get next element with a remaining string. */
5413 lua_pop(L, 1);
5414 }
5415
5416 if (h1m.flags & H1_MF_CHNK)
5417 h1m.flags &= ~H1_MF_CLEN;
5418 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5419 h1m.flags |= H1_MF_XFER_LEN;
5420
5421 /* Uset HTX start-line flags */
5422 if (h1m.flags & H1_MF_XFER_ENC)
5423 flags |= HTX_SL_F_XFER_ENC;
5424 if (h1m.flags & H1_MF_XFER_LEN) {
5425 flags |= HTX_SL_F_XFER_LEN;
5426 if (h1m.flags & H1_MF_CHNK)
5427 flags |= HTX_SL_F_CHNK;
5428 else if (h1m.flags & H1_MF_CLEN)
5429 flags |= HTX_SL_F_CLEN;
5430 if (h1m.body_len == 0)
5431 flags |= HTX_SL_F_BODYLESS;
5432 }
5433 sl->flags |= flags;
5434
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07005435 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005436 * and the status code implies the presence of a message body, we must
5437 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005438 * for the keepalive compliance. If the applet announces a transfer-encoding
5439 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005440 */
5441 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02005442 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
5443 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
5444 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005445 /* Add a new header */
5446 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
5447 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
5448 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005449 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005450 WILL_LJMP(lua_error(L));
5451 }
5452 }
5453
5454 /* Finalize headers. */
5455 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
5456 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005457 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005458 WILL_LJMP(lua_error(L));
5459 }
5460
5461 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
5462 b_reset(&res->buf);
5463 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5464 WILL_LJMP(lua_error(L));
5465 }
5466
5467 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01005468 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005469
5470 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005471 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005472 return 0;
5473
5474}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005475/* We will build the status line and the headers of the HTTP response.
5476 * We will try send at once if its not possible, we give back the hand
5477 * waiting for more room.
5478 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005479__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005480{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005481 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Christopher Faulet86e1c332021-12-20 17:09:39 +01005482 struct stream_interface *si = cs_si(luactx->appctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005483 struct channel *res = si_ic(si);
5484
5485 if (co_data(res)) {
5486 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02005487 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005488 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005489 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005490}
5491
5492
Christopher Fauleta2097962019-07-15 16:25:33 +02005493__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005494{
Christopher Fauleta2097962019-07-15 16:25:33 +02005495 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005496}
5497
Christopher Fauleta2097962019-07-15 16:25:33 +02005498/*
5499 *
5500 *
5501 * Class HTTP
5502 *
5503 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005504 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005505
5506/* Returns a struct hlua_txn if the stack entry "ud" is
5507 * a class stream, otherwise it throws an error.
5508 */
5509__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005510{
Christopher Fauleta2097962019-07-15 16:25:33 +02005511 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
5512}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005513
Christopher Fauleta2097962019-07-15 16:25:33 +02005514/* This function creates and push in the stack a HTTP object
5515 * according with a current TXN.
5516 */
5517static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5518{
5519 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005520
Christopher Fauleta2097962019-07-15 16:25:33 +02005521 /* Check stack size. */
5522 if (!lua_checkstack(L, 3))
5523 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005524
Christopher Fauleta2097962019-07-15 16:25:33 +02005525 /* Create the object: obj[0] = userdata.
5526 * Note that the base of the Converters object is the
5527 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005528 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005529 lua_newtable(L);
5530 htxn = lua_newuserdata(L, sizeof(*htxn));
5531 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005532
5533 htxn->s = txn->s;
5534 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005535 htxn->dir = txn->dir;
5536 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005537
5538 /* Pop a class stream metatable and affect it to the table. */
5539 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5540 lua_setmetatable(L, -2);
5541
5542 return 1;
5543}
5544
Christopher Fauletdf97ac42020-02-26 16:57:19 +01005545/* This function creates and returns an array containing the status-line
5546 * elements. This function does not fails.
5547 */
5548__LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl)
5549{
5550 /* Create the table. */
5551 lua_newtable(L);
5552
5553 if (sl->flags & HTX_SL_F_IS_RESP) {
5554 lua_pushstring(L, "version");
5555 lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl));
5556 lua_settable(L, -3);
5557 lua_pushstring(L, "code");
5558 lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl));
5559 lua_settable(L, -3);
5560 lua_pushstring(L, "reason");
5561 lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl));
5562 lua_settable(L, -3);
5563 }
5564 else {
5565 lua_pushstring(L, "method");
5566 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
5567 lua_settable(L, -3);
5568 lua_pushstring(L, "uri");
5569 lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl));
5570 lua_settable(L, -3);
5571 lua_pushstring(L, "version");
5572 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
5573 lua_settable(L, -3);
5574 }
5575 return 1;
5576}
5577
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005578/* This function creates ans returns an array of HTTP headers.
5579 * This function does not fails. It is used as wrapper with the
5580 * 2 following functions.
5581 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005582__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005583{
Christopher Fauleta2097962019-07-15 16:25:33 +02005584 struct htx *htx;
5585 int32_t pos;
5586
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005587 /* Create the table. */
5588 lua_newtable(L);
5589
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005590
Christopher Fauleta2097962019-07-15 16:25:33 +02005591 htx = htxbuf(&msg->chn->buf);
5592 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5593 struct htx_blk *blk = htx_get_blk(htx, pos);
5594 enum htx_blk_type type = htx_get_blk_type(blk);
5595 struct ist n, v;
5596 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005597
Christopher Fauleta2097962019-07-15 16:25:33 +02005598 if (type == HTX_BLK_HDR) {
5599 n = htx_get_blk_name(htx,blk);
5600 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005601 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005602 else if (type == HTX_BLK_EOH)
5603 break;
5604 else
5605 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005606
Christopher Fauleta2097962019-07-15 16:25:33 +02005607 /* Check for existing entry:
5608 * assume that the table is on the top of the stack, and
5609 * push the key in the stack, the function lua_gettable()
5610 * perform the lookup.
5611 */
5612 lua_pushlstring(L, n.ptr, n.len);
5613 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005614
Christopher Fauleta2097962019-07-15 16:25:33 +02005615 switch (lua_type(L, -1)) {
5616 case LUA_TNIL:
5617 /* Table not found, create it. */
5618 lua_pop(L, 1); /* remove the nil value. */
5619 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5620 lua_newtable(L); /* create and push empty table. */
5621 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5622 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5623 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005624 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005625
Christopher Fauleta2097962019-07-15 16:25:33 +02005626 case LUA_TTABLE:
5627 /* Entry found: push the value in the table. */
5628 len = lua_rawlen(L, -1);
5629 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5630 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5631 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5632 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005633
Christopher Fauleta2097962019-07-15 16:25:33 +02005634 default:
5635 /* Other cases are errors. */
5636 hlua_pusherror(L, "internal error during the parsing of headers.");
5637 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005638 }
5639 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005640 return 1;
5641}
5642
5643__LJMP static int hlua_http_req_get_headers(lua_State *L)
5644{
5645 struct hlua_txn *htxn;
5646
5647 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5648 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5649
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005650 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005651 WILL_LJMP(lua_error(L));
5652
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005653 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005654}
5655
5656__LJMP static int hlua_http_res_get_headers(lua_State *L)
5657{
5658 struct hlua_txn *htxn;
5659
5660 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5661 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5662
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005663 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005664 WILL_LJMP(lua_error(L));
5665
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005666 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005667}
5668
5669/* This function replace full header, or just a value in
5670 * the request or in the response. It is a wrapper fir the
5671 * 4 following functions.
5672 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005673__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005674{
5675 size_t name_len;
5676 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5677 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5678 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005679 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005680 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005681
Dragan Dosen26743032019-04-30 15:54:36 +02005682 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005683 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5684
Christopher Fauleta2097962019-07-15 16:25:33 +02005685 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005686 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005687 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005688 return 0;
5689}
5690
5691__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5692{
5693 struct hlua_txn *htxn;
5694
5695 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5696 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5697
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005698 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005699 WILL_LJMP(lua_error(L));
5700
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005701 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005702}
5703
5704__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5705{
5706 struct hlua_txn *htxn;
5707
5708 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5709 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5710
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005711 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005712 WILL_LJMP(lua_error(L));
5713
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005714 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005715}
5716
5717__LJMP static int hlua_http_req_rep_val(lua_State *L)
5718{
5719 struct hlua_txn *htxn;
5720
5721 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5722 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5723
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005724 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005725 WILL_LJMP(lua_error(L));
5726
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005727 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005728}
5729
5730__LJMP static int hlua_http_res_rep_val(lua_State *L)
5731{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005732 struct hlua_txn *htxn;
5733
5734 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5735 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5736
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005737 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005738 WILL_LJMP(lua_error(L));
5739
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005740 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005741}
5742
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005743/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005744 * It is a wrapper for the 2 following functions.
5745 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005746__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005747{
5748 size_t len;
5749 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005750 struct htx *htx = htxbuf(&msg->chn->buf);
5751 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005752
Christopher Fauleta2097962019-07-15 16:25:33 +02005753 ctx.blk = NULL;
5754 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5755 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005756 return 0;
5757}
5758
5759__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5760{
5761 struct hlua_txn *htxn;
5762
5763 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5764 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5765
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005766 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005767 WILL_LJMP(lua_error(L));
5768
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005769 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005770}
5771
5772__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5773{
5774 struct hlua_txn *htxn;
5775
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005776 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005777 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5778
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005779 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005780 WILL_LJMP(lua_error(L));
5781
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005782 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005783}
5784
5785/* This function adds an header. It is a wrapper used by
5786 * the 2 following functions.
5787 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005788__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005789{
5790 size_t name_len;
5791 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5792 size_t value_len;
5793 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005794 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005795
Christopher Fauleta2097962019-07-15 16:25:33 +02005796 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5797 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005798 return 0;
5799}
5800
Christopher Fauletdf97ac42020-02-26 16:57:19 +01005801__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5802{
5803 struct hlua_txn *htxn;
5804
5805 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5806 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5807
5808 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5809 WILL_LJMP(lua_error(L));
5810
5811 return hlua_http_add_hdr(L, &htxn->s->txn->req);
5812}
5813
5814__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5815{
5816 struct hlua_txn *htxn;
5817
5818 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5819 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5820
5821 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
5822 WILL_LJMP(lua_error(L));
5823
5824 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
5825}
5826
5827static int hlua_http_req_set_hdr(lua_State *L)
5828{
5829 struct hlua_txn *htxn;
5830
5831 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5832 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5833
5834 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5835 WILL_LJMP(lua_error(L));
5836
5837 hlua_http_del_hdr(L, &htxn->s->txn->req);
5838 return hlua_http_add_hdr(L, &htxn->s->txn->req);
5839}
5840
5841static int hlua_http_res_set_hdr(lua_State *L)
5842{
5843 struct hlua_txn *htxn;
5844
5845 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5846 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5847
5848 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
5849 WILL_LJMP(lua_error(L));
5850
5851 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5852 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
5853}
5854
5855/* This function set the method. */
5856static int hlua_http_req_set_meth(lua_State *L)
5857{
5858 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5859 size_t name_len;
5860 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5861
5862 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5863 WILL_LJMP(lua_error(L));
5864
5865 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
5866 return 1;
5867}
5868
5869/* This function set the method. */
5870static int hlua_http_req_set_path(lua_State *L)
5871{
5872 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5873 size_t name_len;
5874 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5875
5876 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5877 WILL_LJMP(lua_error(L));
5878
5879 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
5880 return 1;
5881}
5882
5883/* This function set the query-string. */
5884static int hlua_http_req_set_query(lua_State *L)
5885{
5886 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5887 size_t name_len;
5888 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5889
5890 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5891 WILL_LJMP(lua_error(L));
5892
5893 /* Check length. */
5894 if (name_len > trash.size - 1) {
5895 lua_pushboolean(L, 0);
5896 return 1;
5897 }
5898
5899 /* Add the mark question as prefix. */
5900 chunk_reset(&trash);
5901 trash.area[trash.data++] = '?';
5902 memcpy(trash.area + trash.data, name, name_len);
5903 trash.data += name_len;
5904
5905 lua_pushboolean(L,
5906 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
5907 return 1;
5908}
5909
5910/* This function set the uri. */
5911static int hlua_http_req_set_uri(lua_State *L)
5912{
5913 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5914 size_t name_len;
5915 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5916
5917 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
5918 WILL_LJMP(lua_error(L));
5919
5920 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
5921 return 1;
5922}
5923
5924/* This function set the response code & optionally reason. */
5925static int hlua_http_res_set_status(lua_State *L)
5926{
5927 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5928 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
5929 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5930 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
5931
5932 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
5933 WILL_LJMP(lua_error(L));
5934
5935 http_res_set_status(code, reason, htxn->s);
5936 return 0;
5937}
5938
5939/*
5940 *
5941 *
5942 * Class HTTPMessage
5943 *
5944 *
5945 */
5946
5947/* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage,
5948 * otherwise it throws an error.
5949 */
5950__LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud)
5951{
5952 return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref));
5953}
5954
5955/* Creates and pushes on the stack a HTTP object according with a current TXN.
5956 */
Christopher Faulet78c35472020-02-26 17:14:08 +01005957static int hlua_http_msg_new(lua_State *L, struct http_msg *msg)
Christopher Fauletdf97ac42020-02-26 16:57:19 +01005958{
5959 /* Check stack size. */
5960 if (!lua_checkstack(L, 3))
5961 return 0;
5962
5963 lua_newtable(L);
5964 lua_pushlightuserdata(L, msg);
5965 lua_rawseti(L, -2, 0);
5966
5967 /* Create the "channel" field that contains the request channel object. */
5968 lua_pushstring(L, "channel");
5969 if (!hlua_channel_new(L, msg->chn))
5970 return 0;
5971 lua_rawset(L, -3);
5972
5973 /* Pop a class stream metatable and affect it to the table. */
5974 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref);
5975 lua_setmetatable(L, -2);
5976
5977 return 1;
5978}
5979
5980/* Helper function returning a filter attached to the HTTP message at the
5981 * position <ud> in the stack, filling the current offset and length of the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05005982 * filter. If no filter is attached, NULL is returned and <offset> and <len> are
Christopher Fauletdf97ac42020-02-26 16:57:19 +01005983 * filled with output and input length respectively.
5984 */
5985static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len)
5986{
5987 struct channel *chn = msg->chn;
5988 struct htx *htx = htxbuf(&chn->buf);
5989 struct filter *filter = NULL;
5990
5991 *offset = co_data(msg->chn);
5992 *len = htx->data - co_data(msg->chn);
5993
5994 if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) {
5995 filter = lua_touserdata (L, -1);
5996 if (msg->msg_state >= HTTP_MSG_DATA) {
5997 struct hlua_flt_ctx *flt_ctx = filter->ctx;
5998
5999 *offset = flt_ctx->cur_off[CHN_IDX(chn)];
6000 *len = flt_ctx->cur_len[CHN_IDX(chn)];
6001 }
6002 }
6003
6004 lua_pop(L, 1);
6005 return filter;
6006}
6007
6008/* Returns true if the channel attached to the HTTP message is the response
6009 * channel.
6010 */
6011__LJMP static int hlua_http_msg_is_resp(lua_State *L)
6012{
6013 struct http_msg *msg;
6014
6015 MAY_LJMP(check_args(L, 1, "is_resp"));
6016 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6017
6018 lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP));
6019 return 1;
6020}
6021
6022/* Returns an array containing the elements status-line of the HTTP message. It relies
6023 * on hlua_http_get_stline().
6024 */
6025__LJMP static int hlua_http_msg_get_stline(lua_State *L)
6026{
6027 struct http_msg *msg;
6028 struct htx *htx;
6029 struct htx_sl *sl;
6030
6031 MAY_LJMP(check_args(L, 1, "get_stline"));
6032 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6033
6034 if (msg->msg_state > HTTP_MSG_BODY)
6035 WILL_LJMP(lua_error(L));
6036
6037 htx = htxbuf(&msg->chn->buf);
6038 sl = http_get_stline(htx);
6039 if (!sl)
6040 return 0;
6041 return hlua_http_get_stline(L, sl);
6042}
6043
6044/* Returns an array containing all headers of the HTTP message. it relies on
6045 * hlua_http_get_headers().
6046 */
6047__LJMP static int hlua_http_msg_get_headers(lua_State *L)
6048{
6049 struct http_msg *msg;
6050
6051 MAY_LJMP(check_args(L, 1, "get_headers"));
6052 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6053
6054 if (msg->msg_state > HTTP_MSG_BODY)
6055 WILL_LJMP(lua_error(L));
6056
6057 return hlua_http_get_headers(L, msg);
6058}
6059
6060/* Deletes all occurrences of an header in the HTTP message matching on its
6061 * name. It relies on hlua_http_del_hdr().
6062 */
6063__LJMP static int hlua_http_msg_del_hdr(lua_State *L)
6064{
6065 struct http_msg *msg;
6066
6067 MAY_LJMP(check_args(L, 2, "del_header"));
6068 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6069
6070 if (msg->msg_state > HTTP_MSG_BODY)
6071 WILL_LJMP(lua_error(L));
6072
6073 return hlua_http_del_hdr(L, msg);
6074}
6075
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006076/* Matches the full value line of all occurrences of an header in the HTTP
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006077 * message given its name against a regex and replaces it if it matches. It
6078 * relies on hlua_http_rep_hdr().
6079 */
6080__LJMP static int hlua_http_msg_rep_hdr(lua_State *L)
6081{
6082 struct http_msg *msg;
6083
6084 MAY_LJMP(check_args(L, 4, "rep_header"));
6085 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6086
6087 if (msg->msg_state > HTTP_MSG_BODY)
6088 WILL_LJMP(lua_error(L));
6089
6090 return hlua_http_rep_hdr(L, msg, 1);
6091}
6092
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05006093/* Matches all comma-separated values of all occurrences of an header in the HTTP
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006094 * message given its name against a regex and replaces it if it matches. It
6095 * relies on hlua_http_rep_hdr().
6096 */
6097__LJMP static int hlua_http_msg_rep_val(lua_State *L)
6098{
6099 struct http_msg *msg;
6100
6101 MAY_LJMP(check_args(L, 4, "rep_value"));
6102 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6103
6104 if (msg->msg_state > HTTP_MSG_BODY)
6105 WILL_LJMP(lua_error(L));
6106
6107 return hlua_http_rep_hdr(L, msg, 0);
6108}
6109
6110/* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */
6111__LJMP static int hlua_http_msg_add_hdr(lua_State *L)
6112{
6113 struct http_msg *msg;
6114
6115 MAY_LJMP(check_args(L, 3, "add_header"));
6116 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6117
6118 if (msg->msg_state > HTTP_MSG_BODY)
6119 WILL_LJMP(lua_error(L));
6120
6121 return hlua_http_add_hdr(L, msg);
6122}
6123
6124/* Add an header in the HTTP message removing existing headers with the same
6125 * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr().
6126 */
6127__LJMP static int hlua_http_msg_set_hdr(lua_State *L)
6128{
6129 struct http_msg *msg;
6130
6131 MAY_LJMP(check_args(L, 3, "set_header"));
6132 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6133
6134 if (msg->msg_state > HTTP_MSG_BODY)
6135 WILL_LJMP(lua_error(L));
6136
6137 hlua_http_del_hdr(L, msg);
6138 return hlua_http_add_hdr(L, msg);
6139}
6140
6141/* Rewrites the request method. It relies on http_req_replace_stline(). */
6142__LJMP static int hlua_http_msg_set_meth(lua_State *L)
6143{
6144 struct stream *s;
6145 struct http_msg *msg;
6146 const char *name;
6147 size_t name_len;
6148
6149 MAY_LJMP(check_args(L, 2, "set_method"));
6150 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6151 name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
6152
6153 if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6154 WILL_LJMP(lua_error(L));
6155
6156 s = chn_strm(msg->chn);
6157 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1);
6158 return 1;
6159}
6160
6161/* Rewrites the request path. It relies on http_req_replace_stline(). */
6162__LJMP static int hlua_http_msg_set_path(lua_State *L)
6163{
6164 struct stream *s;
6165 struct http_msg *msg;
6166 const char *name;
6167 size_t name_len;
6168
6169 MAY_LJMP(check_args(L, 2, "set_path"));
6170 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6171 name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
6172
6173 if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6174 WILL_LJMP(lua_error(L));
6175
6176 s = chn_strm(msg->chn);
6177 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1);
6178 return 1;
6179}
6180
6181/* Rewrites the request query-string. It relies on http_req_replace_stline(). */
6182__LJMP static int hlua_http_msg_set_query(lua_State *L)
6183{
6184 struct stream *s;
6185 struct http_msg *msg;
6186 const char *name;
6187 size_t name_len;
6188
6189 MAY_LJMP(check_args(L, 2, "set_query"));
6190 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6191 name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
6192
6193 if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6194 WILL_LJMP(lua_error(L));
6195
6196 /* Check length. */
6197 if (name_len > trash.size - 1) {
6198 lua_pushboolean(L, 0);
6199 return 1;
6200 }
6201
6202 /* Add the mark question as prefix. */
6203 chunk_reset(&trash);
6204 trash.area[trash.data++] = '?';
6205 memcpy(trash.area + trash.data, name, name_len);
6206 trash.data += name_len;
6207
6208 s = chn_strm(msg->chn);
6209 lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1);
6210 return 1;
6211}
6212
6213/* Rewrites the request URI. It relies on http_req_replace_stline(). */
6214__LJMP static int hlua_http_msg_set_uri(lua_State *L)
6215{
6216 struct stream *s;
6217 struct http_msg *msg;
6218 const char *name;
6219 size_t name_len;
6220
6221 MAY_LJMP(check_args(L, 2, "set_uri"));
6222 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6223 name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
6224
6225 if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6226 WILL_LJMP(lua_error(L));
6227
6228 s = chn_strm(msg->chn);
6229 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1);
6230 return 1;
6231}
6232
6233/* Rewrites the response status code. It relies on http_res_set_status(). */
6234__LJMP static int hlua_http_msg_set_status(lua_State *L)
6235{
6236 struct http_msg *msg;
6237 unsigned int code;
6238 const char *reason;
6239 size_t reason_len;
6240
6241 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6242 code = MAY_LJMP(luaL_checkinteger(L, 2));
6243 reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len));
6244
6245 if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY)
6246 WILL_LJMP(lua_error(L));
6247
6248 lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1);
6249 return 1;
6250}
6251
6252/* Returns true if the HTTP message is full. */
6253__LJMP static int hlua_http_msg_is_full(lua_State *L)
6254{
6255 struct http_msg *msg;
6256
6257 MAY_LJMP(check_args(L, 1, "is_full"));
6258 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6259 lua_pushboolean(L, channel_full(msg->chn, 0));
6260 return 1;
6261}
6262
6263/* Returns true if the HTTP message may still receive data. */
6264__LJMP static int hlua_http_msg_may_recv(lua_State *L)
6265{
6266 struct http_msg *msg;
6267 struct htx *htx;
6268
6269 MAY_LJMP(check_args(L, 1, "may_recv"));
6270 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6271 htx = htxbuf(&msg->chn->buf);
6272 lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn)));
6273 return 1;
6274}
6275
6276/* Returns true if the HTTP message EOM was received */
6277__LJMP static int hlua_http_msg_is_eom(lua_State *L)
6278{
6279 struct http_msg *msg;
6280 struct htx *htx;
6281
6282 MAY_LJMP(check_args(L, 1, "may_recv"));
6283 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6284 htx = htxbuf(&msg->chn->buf);
6285 lua_pushboolean(L, !htx_expect_more(htx));
6286 return 1;
6287}
6288
6289/* Returns the number of bytes available in the input side of the HTTP
6290 * message. This function never fails.
6291 */
6292__LJMP static int hlua_http_msg_get_in_len(lua_State *L)
6293{
6294 struct http_msg *msg;
Christopher Fauleteae8afa2020-02-26 17:15:48 +01006295 size_t output, input;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006296
6297 MAY_LJMP(check_args(L, 1, "input"));
6298 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Christopher Fauleteae8afa2020-02-26 17:15:48 +01006299 hlua_http_msg_filter(L, 1, msg, &output, &input);
6300 lua_pushinteger(L, input);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006301 return 1;
6302}
6303
6304/* Returns the number of bytes available in the output side of the HTTP
6305 * message. This function never fails.
6306 */
6307__LJMP static int hlua_http_msg_get_out_len(lua_State *L)
6308{
6309 struct http_msg *msg;
Christopher Fauleteae8afa2020-02-26 17:15:48 +01006310 size_t output, input;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006311
6312 MAY_LJMP(check_args(L, 1, "output"));
6313 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Christopher Fauleteae8afa2020-02-26 17:15:48 +01006314 hlua_http_msg_filter(L, 1, msg, &output, &input);
6315 lua_pushinteger(L, output);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006316 return 1;
6317}
6318
6319/* Copies at most <len> bytes of DATA blocks from the HTTP message <msg>
6320 * starting at the offset <offset> and put it in a string LUA variables. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006321 * returns the built string length. It stops on the first non-DATA HTX
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006322 * block. This function is called during the payload filtering, so the headers
6323 * are already scheduled for output (from the filter point of view).
6324 */
6325static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len)
6326{
6327 struct htx *htx = htxbuf(&msg->chn->buf);
6328 struct htx_blk *blk;
6329 struct htx_ret htxret;
6330 luaL_Buffer b;
6331 int ret = 0;
6332
6333 luaL_buffinit(L, &b);
6334 htxret = htx_find_offset(htx, offset);
6335 for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) {
6336 enum htx_blk_type type = htx_get_blk_type(blk);
6337 struct ist v;
6338
6339 switch (type) {
6340 case HTX_BLK_UNUSED:
6341 break;
6342
6343 case HTX_BLK_DATA:
6344 v = htx_get_blk_value(htx, blk);
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02006345 v = istadv(v, offset);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01006346 v = isttrim(v, len);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006347
6348 luaL_addlstring(&b, v.ptr, v.len);
6349 ret += v.len;
6350 break;
6351
6352 default:
vishnu0af4bd72021-10-24 06:46:24 +05306353 if (!ret)
6354 goto no_data;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006355 goto end;
6356 }
6357 offset = 0;
6358 }
6359
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006360end:
vishnu0af4bd72021-10-24 06:46:24 +05306361 if (!ret && (htx->flags & HTX_FL_EOM))
6362 goto no_data;
6363 luaL_pushresult(&b);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006364 return ret;
vishnu0af4bd72021-10-24 06:46:24 +05306365
6366 no_data:
6367 /* Remove the empty string and push nil on the stack */
6368 lua_pop(L, 1);
6369 lua_pushnil(L);
6370 return 0;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006371}
6372
6373/* Copies the string <str> to the HTTP message <msg> at the offset
6374 * <offset>. This function returns -1 if data cannot be copied. Otherwise, it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006375 * returns the amount of data written. This function is responsible to update
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006376 * the filter context.
6377 */
6378static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset)
6379{
6380 struct htx *htx = htx_from_buf(&msg->chn->buf);
6381 struct htx_ret htxret;
6382 int /*max, */ret = 0;
6383
6384 /* Nothing to do, just return */
6385 if (unlikely(istlen(str) == 0))
6386 goto end;
6387
6388 if (istlen(str) > htx_free_data_space(htx)) {
6389 ret = -1;
6390 goto end;
6391 }
6392
6393 htxret = htx_find_offset(htx, offset);
6394 if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) {
6395 if (!htx_add_last_data(htx, str))
6396 goto end;
6397 }
6398 else {
6399 struct ist v = htx_get_blk_value(htx, htxret.blk);
6400 v.ptr += htxret.ret;
6401 v.len = 0;
6402 if (!htx_replace_blk_value(htx, htxret.blk, v, str))
6403 goto end;
6404 }
6405 ret = str.len;
6406 if (ret) {
6407 struct hlua_flt_ctx *flt_ctx = filter->ctx;
6408 flt_update_offsets(filter, msg->chn, ret);
6409 flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret;
6410 }
6411
6412 end:
6413 htx_to_buf(htx, &msg->chn->buf);
6414 return ret;
6415}
6416
6417/* Helper function removing at most <len> bytes of DATA blocks at the absolute
6418 * position <offset>. It stops on the first non-DATA HTX block. This function is
6419 * called during the payload filtering, so the headers are already scheduled for
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006420 * output (from the filter point of view). This function is responsible to
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006421 * update the filter context.
6422 */
6423static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len)
6424{
6425 struct hlua_flt_ctx *flt_ctx = filter->ctx;
6426 struct htx *htx = htx_from_buf(&msg->chn->buf);
6427 struct htx_blk *blk;
6428 struct htx_ret htxret;
6429 size_t ret = 0;
6430
6431 /* Be sure <len> is always the amount of DATA to remove */
6432 if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) {
6433 htx_truncate(htx, offset);
6434 ret = len;
6435 goto end;
6436 }
6437
6438 htxret = htx_find_offset(htx, offset);
6439 blk = htxret.blk;
6440 if (htxret.ret) {
6441 struct ist v;
6442
6443 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
6444 goto end;
6445 v = htx_get_blk_value(htx, blk);
6446 v.ptr += htxret.ret;
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01006447 v = isttrim(v, len);
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02006448 blk = htx_replace_blk_value(htx, blk, v, IST_NULL);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006449 len -= v.len;
6450 ret += v.len;
6451 }
6452
6453
6454 while (blk && len) {
6455 enum htx_blk_type type = htx_get_blk_type(blk);
6456 uint32_t sz = htx_get_blksz(blk);
6457
6458 switch (type) {
6459 case HTX_BLK_UNUSED:
6460 break;
6461
6462 case HTX_BLK_DATA:
6463 if (len < sz) {
6464 htx_cut_data_blk(htx, blk, len);
6465 ret += len;
6466 goto end;
6467 }
6468 break;
6469
6470 default:
6471 goto end;
6472 }
6473
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006474 /* Remove all the data block */
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006475 len -= sz;
6476 ret += sz;
6477 blk = htx_remove_blk(htx, blk);
6478 }
6479
6480end:
6481 flt_update_offsets(filter, msg->chn, -ret);
6482 flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret;
6483 /* WARNING: we don't call htx_to_buf() on purpose, because we don't want
6484 * to loose the EOM flag if the message is empty.
6485 */
6486}
6487
6488/* Copies input data found in an HTTP message. Unlike the channel function used
6489 * to duplicate raw data, this one can only be called inside a filter, from
6490 * http_payload callback. So it cannot yield. An exception is returned if it is
6491 * called from another callback. If nothing was copied, a nil value is pushed on
6492 * the stack.
6493 */
6494__LJMP static int hlua_http_msg_get_body(lua_State *L)
6495{
6496 struct http_msg *msg;
6497 struct filter *filter;
6498 size_t output, input;
6499 int offset, len;
6500
6501 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
6502 WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments"));
6503 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6504
6505 if (msg->msg_state < HTTP_MSG_DATA)
6506 WILL_LJMP(lua_error(L));
6507
6508 filter = hlua_http_msg_filter(L, 1, msg, &output, &input);
6509 if (!filter || !hlua_filter_from_payload(filter))
6510 WILL_LJMP(lua_error(L));
6511
6512 if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) {
6513 lua_pushnil(L);
6514 return 1;
6515 }
6516
6517 offset = output;
6518 if (lua_gettop(L) > 1) {
6519 offset = MAY_LJMP(luaL_checkinteger(L, 2));
6520 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02006521 offset = MAX(0, (int)input + offset);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006522 offset += output;
6523 if (offset < output || offset > input + output) {
6524 lua_pushfstring(L, "offset out of range.");
6525 WILL_LJMP(lua_error(L));
6526 }
6527 }
6528 len = output + input - offset;
6529 if (lua_gettop(L) == 3) {
6530 len = MAY_LJMP(luaL_checkinteger(L, 3));
6531 if (!len)
6532 goto dup;
6533 if (len == -1)
6534 len = global.tune.bufsize;
6535 if (len < 0) {
6536 lua_pushfstring(L, "length out of range.");
6537 WILL_LJMP(lua_error(L));
6538 }
6539 }
6540
6541 dup:
6542 _hlua_http_msg_dup(msg, L, offset, len);
6543 return 1;
6544}
6545
6546/* Appends a string to the HTTP message, after all existing DATA blocks but
6547 * before the trailers, if any. It returns the amount of data written or -1 if
6548 * nothing was copied. Unlike the channel function used to append data, this one
6549 * can only be called inside a filter, from http_payload callback. So it cannot
6550 * yield. An exception is returned if it is called from another callback.
6551 */
6552__LJMP static int hlua_http_msg_append(lua_State *L)
6553{
6554 struct http_msg *msg;
6555 struct filter *filter;
6556 const char *str;
6557 size_t offset, len, sz;
6558 int ret;
6559
6560 MAY_LJMP(check_args(L, 2, "append"));
6561 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6562
6563 if (msg->msg_state < HTTP_MSG_DATA)
6564 WILL_LJMP(lua_error(L));
6565
6566 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6567 filter = hlua_http_msg_filter(L, 1, msg, &offset, &len);
6568 if (!filter || !hlua_filter_from_payload(filter))
6569 WILL_LJMP(lua_error(L));
6570
6571 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len);
6572 lua_pushinteger(L, ret);
6573 return 1;
6574}
6575
6576/* Prepends a string to the HTTP message, before all existing DATA blocks. It
6577 * returns the amount of data written or -1 if nothing was copied. Unlike the
6578 * channel function used to prepend data, this one can only be called inside a
6579 * filter, from http_payload callback. So it cannot yield. An exception is
6580 * returned if it is called from another callback.
6581 */
6582__LJMP static int hlua_http_msg_prepend(lua_State *L)
6583{
6584 struct http_msg *msg;
6585 struct filter *filter;
6586 const char *str;
6587 size_t offset, len, sz;
6588 int ret;
6589
6590 MAY_LJMP(check_args(L, 2, "prepend"));
6591 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006592
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006593 if (msg->msg_state < HTTP_MSG_DATA)
6594 WILL_LJMP(lua_error(L));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006595
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006596 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6597 filter = hlua_http_msg_filter(L, 1, msg, &offset, &len);
6598 if (!filter || !hlua_filter_from_payload(filter))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006599 WILL_LJMP(lua_error(L));
6600
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006601 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset);
6602 lua_pushinteger(L, ret);
6603 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006604}
6605
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006606/* Inserts a string to the HTTP message at a given offset. By default the string
6607 * is appended at the end of DATA blocks. It returns the amount of data written
6608 * or -1 if nothing was copied. Unlike the channel function used to insert data,
6609 * this one can only be called inside a filter, from http_payload callback. So
6610 * it cannot yield. An exception is returned if it is called from another
6611 * callback.
6612 */
6613__LJMP static int hlua_http_msg_insert_data(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006614{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006615 struct http_msg *msg;
6616 struct filter *filter;
6617 const char *str;
6618 size_t input, output, sz;
6619 int offset;
6620 int ret;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006621
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006622 if (lua_gettop(L) < 2 || lua_gettop(L) > 3)
6623 WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments"));
6624 MAY_LJMP(check_args(L, 2, "insert"));
6625 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006626
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006627 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006628 WILL_LJMP(lua_error(L));
6629
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006630 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6631 filter = hlua_http_msg_filter(L, 1, msg, &input, &output);
6632 if (!filter || !hlua_filter_from_payload(filter))
6633 WILL_LJMP(lua_error(L));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006634
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006635 offset = input + output;
6636 if (lua_gettop(L) > 2) {
6637 offset = MAY_LJMP(luaL_checkinteger(L, 3));
6638 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02006639 offset = MAX(0, (int)input + offset);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006640 offset += output;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006641 if (offset < output || offset > output + input) {
6642 lua_pushfstring(L, "offset out of range.");
6643 WILL_LJMP(lua_error(L));
6644 }
6645 }
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006646
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006647 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset);
6648 lua_pushinteger(L, ret);
6649 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006650}
6651
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006652/* Removes a given amount of data from the HTTP message at a given offset. By
6653 * default all DATA blocks are removed. It returns the amount of data
6654 * removed. Unlike the channel function used to remove data, this one can only
6655 * be called inside a filter, from http_payload callback. So it cannot yield. An
6656 * exception is returned if it is called from another callback.
6657 */
6658__LJMP static int hlua_http_msg_del_data(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006659{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006660 struct http_msg *msg;
6661 struct filter *filter;
6662 size_t input, output;
6663 int offset, len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006664
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006665 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
6666 WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments"));
6667 MAY_LJMP(check_args(L, 2, "insert"));
6668 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006669
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006670 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006671 WILL_LJMP(lua_error(L));
6672
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006673 filter = hlua_http_msg_filter(L, 1, msg, &input, &output);
6674 if (!filter || !hlua_filter_from_payload(filter))
6675 WILL_LJMP(lua_error(L));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006676
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006677 offset = input + output;
6678 if (lua_gettop(L) > 2) {
6679 offset = MAY_LJMP(luaL_checkinteger(L, 3));
6680 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02006681 offset = MAX(0, (int)input + offset);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006682 offset += output;
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006683 if (offset < output || offset > output + input) {
6684 lua_pushfstring(L, "offset out of range.");
6685 WILL_LJMP(lua_error(L));
6686 }
6687 }
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006688
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006689 len = output + input - offset;
6690 if (lua_gettop(L) == 4) {
6691 len = MAY_LJMP(luaL_checkinteger(L, 4));
6692 if (!len)
6693 goto end;
6694 if (len == -1)
6695 len = output + input - offset;
6696 if (len < 0 || offset + len > output + input) {
6697 lua_pushfstring(L, "length out of range.");
6698 WILL_LJMP(lua_error(L));
6699 }
6700 }
6701
6702 _hlua_http_msg_delete(msg, filter, offset, len);
6703
6704 end:
6705 lua_pushinteger(L, len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006706 return 1;
6707}
6708
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05006709/* Replaces a given amount of data at the given offset by a string. By default,
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006710 * all remaining data are removed, accordingly to the filter context. It returns
6711 * the amount of data written or -1 if nothing was copied. Unlike the channel
6712 * function used to replace data, this one can only be called inside a filter,
6713 * from http_payload callback. So it cannot yield. An exception is returned if
6714 * it is called from another callback.
6715 */
6716__LJMP static int hlua_http_msg_set_data(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006717{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006718 struct http_msg *msg;
6719 struct filter *filter;
6720 struct htx *htx;
6721 const char *str;
6722 size_t input, output, sz;
6723 int offset, len;
6724 int ret;
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02006725
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006726 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
6727 WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments"));
6728 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6729
6730 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006731 WILL_LJMP(lua_error(L));
6732
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006733 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6734 filter = hlua_http_msg_filter(L, 1, msg, &output, &input);
6735 if (!filter || !hlua_filter_from_payload(filter))
6736 WILL_LJMP(lua_error(L));
6737
6738 offset = output;
6739 if (lua_gettop(L) > 2) {
6740 offset = MAY_LJMP(luaL_checkinteger(L, 3));
6741 if (offset < 0)
Christopher Faulet70c43452021-08-13 08:11:00 +02006742 offset = MAX(0, (int)input + offset);
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006743 offset += output;
6744 if (offset < output || offset > input + output) {
6745 lua_pushfstring(L, "offset out of range.");
6746 WILL_LJMP(lua_error(L));
6747 }
6748 }
6749
6750 len = output + input - offset;
6751 if (lua_gettop(L) == 4) {
6752 len = MAY_LJMP(luaL_checkinteger(L, 4));
6753 if (!len)
6754 goto set;
6755 if (len == -1)
6756 len = output + input - offset;
6757 if (len < 0 || offset + len > output + input) {
6758 lua_pushfstring(L, "length out of range.");
6759 WILL_LJMP(lua_error(L));
6760 }
6761 }
6762
6763 set:
6764 /* Be sure we can copied the string once input data will be removed. */
6765 htx = htx_from_buf(&msg->chn->buf);
6766 if (sz > htx_free_data_space(htx) + len)
6767 lua_pushinteger(L, -1);
6768 else {
6769 _hlua_http_msg_delete(msg, filter, offset, len);
6770 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset);
6771 lua_pushinteger(L, ret);
6772 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006773 return 1;
6774}
6775
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006776/* Prepends data into an HTTP message and forward it, from the filter point of
6777 * view. It returns the amount of data written or -1 if nothing was sent. Unlike
6778 * the channel function used to send data, this one can only be called inside a
6779 * filter, from http_payload callback. So it cannot yield. An exception is
6780 * returned if it is called from another callback.
6781 */
6782__LJMP static int hlua_http_msg_send(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006783{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006784 struct http_msg *msg;
6785 struct filter *filter;
6786 struct htx *htx;
6787 const char *str;
6788 size_t offset, len, sz;
6789 int ret;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006790
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006791 MAY_LJMP(check_args(L, 2, "send"));
6792 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6793
6794 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006795 WILL_LJMP(lua_error(L));
6796
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006797 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
6798 filter = hlua_http_msg_filter(L, 1, msg, &offset, &len);
6799 if (!filter || !hlua_filter_from_payload(filter))
6800 WILL_LJMP(lua_error(L));
6801
6802 /* Return an error if the channel's output is closed */
6803 if (unlikely(channel_output_closed(msg->chn))) {
6804 lua_pushinteger(L, -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006805 return 1;
6806 }
6807
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006808 htx = htx_from_buf(&msg->chn->buf);
6809 if (sz > htx_free_data_space(htx)) {
6810 lua_pushinteger(L, -1);
6811 return 1;
6812 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006813
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006814 ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset);
6815 if (ret > 0) {
6816 struct hlua_flt_ctx *flt_ctx = filter->ctx;
6817
6818 FLT_OFF(filter, msg->chn) += ret;
6819 flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret;
6820 flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret;
6821 }
6822
6823 lua_pushinteger(L, ret);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006824 return 1;
6825}
6826
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006827/* Forwards a given amount of bytes. It return -1 if the channel's output is
6828 * closed. Otherwise, it returns the number of bytes forwarded. Unlike the
6829 * channel function used to forward data, this one can only be called inside a
6830 * filter, from http_payload callback. So it cannot yield. An exception is
6831 * returned if it is called from another callback. All other functions deal with
6832 * DATA block, this one not.
6833*/
6834__LJMP static int hlua_http_msg_forward(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006835{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006836 struct http_msg *msg;
6837 struct filter *filter;
6838 size_t offset, len;
6839 int fwd, ret = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006840
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006841 MAY_LJMP(check_args(L, 2, "forward"));
6842 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6843
6844 if (msg->msg_state < HTTP_MSG_DATA)
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006845 WILL_LJMP(lua_error(L));
6846
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006847 fwd = MAY_LJMP(luaL_checkinteger(L, 2));
6848 filter = hlua_http_msg_filter(L, 1, msg, &offset, &len);
6849 if (!filter || !hlua_filter_from_payload(filter))
6850 WILL_LJMP(lua_error(L));
6851
6852 /* Nothing to do, just return */
6853 if (!fwd)
6854 goto end;
6855
6856 /* Return an error if the channel's output is closed */
6857 if (unlikely(channel_output_closed(msg->chn))) {
6858 ret = -1;
6859 goto end;
6860 }
6861
6862 ret = fwd;
6863 if (ret > len)
6864 ret = len;
6865
6866 if (ret) {
6867 struct hlua_flt_ctx *flt_ctx = filter->ctx;
6868
6869 FLT_OFF(filter, msg->chn) += ret;
6870 flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret;
6871 flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret;
6872 }
6873
6874 end:
6875 lua_pushinteger(L, ret);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006876 return 1;
6877}
6878
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006879/* Set EOM flag on the HTX message.
6880 *
6881 * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't
6882 * really know how to do without this feature.
6883 */
6884__LJMP static int hlua_http_msg_set_eom(lua_State *L)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02006885{
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006886 struct http_msg *msg;
6887 struct htx *htx;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02006888
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006889 MAY_LJMP(check_args(L, 1, "set_eom"));
6890 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6891 htx = htxbuf(&msg->chn->buf);
6892 htx->flags |= HTX_FL_EOM;
6893 return 0;
6894}
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02006895
Christopher Fauletdf97ac42020-02-26 16:57:19 +01006896/* Unset EOM flag on the HTX message.
6897 *
6898 * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't
6899 * really know how to do without this feature.
6900 */
6901__LJMP static int hlua_http_msg_unset_eom(lua_State *L)
6902{
6903 struct http_msg *msg;
6904 struct htx *htx;
6905
6906 MAY_LJMP(check_args(L, 1, "set_eom"));
6907 msg = MAY_LJMP(hlua_checkhttpmsg(L, 1));
6908 htx = htxbuf(&msg->chn->buf);
6909 htx->flags &= ~HTX_FL_EOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02006910 return 0;
6911}
6912
Thierry FOURNIER08504f42015-03-16 14:17:08 +01006913/*
6914 *
6915 *
William Lallemand3956c4e2021-09-21 16:25:15 +02006916 * Class HTTPClient
6917 *
6918 *
6919 */
6920__LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud)
6921{
6922 return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref));
6923}
6924
William Lallemandf77f1de2021-09-28 19:10:38 +02006925
6926/* stops the httpclient and ask it to kill itself */
6927__LJMP static int hlua_httpclient_gc(lua_State *L)
6928{
6929 struct hlua_httpclient *hlua_hc;
6930
6931 MAY_LJMP(check_args(L, 1, "__gc"));
6932
6933 hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1));
6934
6935 httpclient_stop_and_destroy(hlua_hc->hc);
6936
6937 hlua_hc->hc = NULL;
6938
6939
6940 return 0;
6941}
6942
6943
William Lallemand3956c4e2021-09-21 16:25:15 +02006944__LJMP static int hlua_httpclient_new(lua_State *L)
6945{
6946 struct hlua_httpclient *hlua_hc;
6947 struct hlua *hlua;
6948
6949 /* Get hlua struct, or NULL if we execute from main lua state */
6950 hlua = hlua_gethlua(L);
6951 if (!hlua)
6952 return 0;
6953
6954 /* Check stack size. */
6955 if (!lua_checkstack(L, 3)) {
6956 hlua_pusherror(L, "httpclient: full stack");
6957 goto err;
6958 }
6959 /* Create the object: obj[0] = userdata. */
6960 lua_newtable(L);
6961 hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc)));
6962 lua_rawseti(L, -2, 0);
6963 memset(hlua_hc, 0, sizeof(*hlua_hc));
6964
6965 hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL);
6966 if (!hlua_hc->hc)
6967 goto err;
6968
6969 /* Pop a class stream metatable and affect it to the userdata. */
6970 lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref);
6971 lua_setmetatable(L, -2);
6972
6973 return 1;
6974
6975 err:
6976 WILL_LJMP(lua_error(L));
6977 return 0;
6978}
6979
6980
6981/*
6982 * Callback of the httpclient, this callback wakes the lua task up, once the
6983 * httpclient receives some data
6984 *
6985 */
6986
William Lallemandbd5739e2021-10-28 15:41:38 +02006987static void hlua_httpclient_cb(struct httpclient *hc)
William Lallemand3956c4e2021-09-21 16:25:15 +02006988{
6989 struct hlua *hlua = hc->caller;
6990
6991 if (!hlua || !hlua->task)
6992 return;
6993
6994 task_wakeup(hlua->task, TASK_WOKEN_MSG);
6995}
6996
6997/*
William Lallemandd7df73a2021-09-23 17:54:00 +02006998 * Fill the lua stack with headers from the httpclient response
6999 * This works the same way as the hlua_http_get_headers() function
7000 */
7001__LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc)
7002{
7003 struct http_hdr *hdr;
7004
7005 lua_newtable(L);
7006
William Lallemandef574b22021-10-05 16:19:31 +02007007 for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) {
William Lallemandd7df73a2021-09-23 17:54:00 +02007008 struct ist n, v;
7009 int len;
7010
7011 n = hdr->n;
7012 v = hdr->v;
7013
7014 /* Check for existing entry:
7015 * assume that the table is on the top of the stack, and
7016 * push the key in the stack, the function lua_gettable()
7017 * perform the lookup.
7018 */
7019
7020 lua_pushlstring(L, n.ptr, n.len);
7021 lua_gettable(L, -2);
7022
7023 switch (lua_type(L, -1)) {
7024 case LUA_TNIL:
7025 /* Table not found, create it. */
7026 lua_pop(L, 1); /* remove the nil value. */
7027 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
7028 lua_newtable(L); /* create and push empty table. */
7029 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
7030 lua_rawseti(L, -2, 0); /* index header value (pop it). */
7031 lua_rawset(L, -3); /* index new table with header name (pop the values). */
7032 break;
7033
7034 case LUA_TTABLE:
7035 /* Entry found: push the value in the table. */
7036 len = lua_rawlen(L, -1);
7037 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
7038 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
7039 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
7040 break;
7041
7042 default:
7043 /* Other cases are errors. */
7044 hlua_pusherror(L, "internal error during the parsing of headers.");
7045 WILL_LJMP(lua_error(L));
7046 }
7047 }
7048 return 1;
7049}
7050
7051/*
William Lallemand746e6f32021-10-06 10:57:44 +02007052 * Allocate and return an array of http_hdr ist extracted from the <headers> lua table
7053 *
7054 * Caller must free the result
7055 */
7056struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L)
7057{
7058 struct http_hdr hdrs[global.tune.max_http_hdr];
7059 struct http_hdr *result = NULL;
7060 uint32_t hdr_num = 0;
7061
7062 lua_pushnil(L);
7063 while (lua_next(L, -2) != 0) {
7064 struct ist name, value;
7065 const char *n, *v;
7066 size_t nlen, vlen;
7067
7068 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
7069 /* Skip element if the key is not a string or if the value is not a table */
7070 goto next_hdr;
7071 }
7072
7073 n = lua_tolstring(L, -2, &nlen);
7074 name = ist2(n, nlen);
7075
7076 /* Loop on header's values */
7077 lua_pushnil(L);
7078 while (lua_next(L, -2)) {
7079 if (!lua_isstring(L, -1)) {
7080 /* Skip the value if it is not a string */
7081 goto next_value;
7082 }
7083
7084 v = lua_tolstring(L, -1, &vlen);
7085 value = ist2(v, vlen);
7086 name = ist2(n, nlen);
7087
7088 hdrs[hdr_num].n = istdup(name);
7089 hdrs[hdr_num].v = istdup(value);
7090
7091 hdr_num++;
7092
7093 next_value:
7094 lua_pop(L, 1);
7095 }
7096
7097 next_hdr:
7098 lua_pop(L, 1);
7099
7100 }
7101
7102 if (hdr_num) {
7103 /* alloc and copy the headers in the httpclient struct */
Tim Duesterhus16cc16d2021-11-06 15:14:45 +01007104 result = calloc((hdr_num + 1), sizeof(*result));
William Lallemand746e6f32021-10-06 10:57:44 +02007105 if (!result)
7106 goto skip_headers;
7107 memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1));
7108
7109 result[hdr_num].n = IST_NULL;
7110 result[hdr_num].v = IST_NULL;
7111 }
7112
7113skip_headers:
William Lallemand746e6f32021-10-06 10:57:44 +02007114
7115 return result;
7116}
7117
7118
William Lallemandbd5739e2021-10-28 15:41:38 +02007119/*
7120 * For each yield, checks if there is some data in the httpclient and push them
7121 * in the lua buffer, once the httpclient finished its job, push the result on
7122 * the stack
7123 */
7124__LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx)
7125{
7126 struct buffer *tr;
7127 int res;
7128 struct hlua *hlua = hlua_gethlua(L);
7129 struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1);
7130
7131
7132 tr = get_trash_chunk();
7133
7134 res = httpclient_res_xfer(hlua_hc->hc, tr);
7135 luaL_addlstring(&hlua_hc->b, b_orig(tr), res);
7136
7137 if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) {
7138
7139 luaL_pushresult(&hlua_hc->b);
7140 lua_settable(L, -3);
7141
7142 lua_pushstring(L, "status");
7143 lua_pushinteger(L, hlua_hc->hc->res.status);
7144 lua_settable(L, -3);
7145
7146
7147 lua_pushstring(L, "reason");
7148 lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len);
7149 lua_settable(L, -3);
7150
7151 lua_pushstring(L, "headers");
7152 hlua_httpclient_get_headers(L, hlua_hc);
7153 lua_settable(L, -3);
7154
7155 return 1;
7156 }
7157
7158 if (httpclient_data(hlua_hc->hc))
7159 task_wakeup(hlua->task, TASK_WOKEN_MSG);
7160
7161 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0));
7162
7163 return 0;
7164}
7165
7166/*
7167 * Call this when trying to stream a body during a request
7168 */
7169__LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx)
7170{
7171 struct hlua *hlua;
7172 struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1);
7173 const char *body_str = NULL;
7174 int ret;
7175 int end = 0;
7176 size_t buf_len;
7177 size_t to_send = 0;
7178
7179 hlua = hlua_gethlua(L);
7180
7181 if (!hlua || !hlua->task)
7182 WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in "
7183 "'frontend', 'backend' or 'task'"));
7184
7185 ret = lua_getfield(L, -1, "body");
7186 if (ret != LUA_TSTRING)
7187 goto rcv;
7188
7189 body_str = lua_tolstring(L, -1, &buf_len);
7190 lua_pop(L, 1);
7191
Christopher Fauletfc591292022-01-12 14:46:03 +01007192 to_send = buf_len - hlua_hc->sent;
William Lallemandbd5739e2021-10-28 15:41:38 +02007193
7194 if ((hlua_hc->sent + to_send) >= buf_len)
7195 end = 1;
7196
7197 /* the end flag is always set since we are using the whole remaining size */
7198 hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end);
7199
7200 if (buf_len > hlua_hc->sent) {
7201 /* still need to process the buffer */
7202 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0));
7203 } else {
7204 goto rcv;
7205 /* we sent the whole request buffer we can recv */
7206 }
7207 return 0;
7208
7209rcv:
7210
7211 /* we return a "res" object */
7212 lua_newtable(L);
7213
William Lallemandbd5739e2021-10-28 15:41:38 +02007214 lua_pushstring(L, "body");
William Lallemandd1187eb2021-11-02 10:40:06 +01007215 luaL_buffinit(L, &hlua_hc->b);
William Lallemandbd5739e2021-10-28 15:41:38 +02007216
William Lallemand933fe392021-11-04 09:45:58 +01007217 task_wakeup(hlua->task, TASK_WOKEN_MSG);
William Lallemandbd5739e2021-10-28 15:41:38 +02007218 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0));
7219
7220 return 1;
7221}
William Lallemand746e6f32021-10-06 10:57:44 +02007222
William Lallemand3956c4e2021-09-21 16:25:15 +02007223/*
William Lallemanddc2cc902021-10-26 11:43:26 +02007224 * Send an HTTP request and wait for a response
William Lallemand3956c4e2021-09-21 16:25:15 +02007225 */
7226
William Lallemanddc2cc902021-10-26 11:43:26 +02007227__LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
William Lallemand3956c4e2021-09-21 16:25:15 +02007228{
7229 struct hlua_httpclient *hlua_hc;
William Lallemand746e6f32021-10-06 10:57:44 +02007230 struct http_hdr *hdrs = NULL;
7231 struct http_hdr *hdrs_i = NULL;
William Lallemand3956c4e2021-09-21 16:25:15 +02007232 struct hlua *hlua;
William Lallemand746e6f32021-10-06 10:57:44 +02007233 const char *url_str = NULL;
William Lallemanddec25c32021-10-25 19:48:37 +02007234 const char *body_str = NULL;
William Lallemandb4a4ef62022-02-23 14:18:16 +01007235 int timeout;
William Lallemandbd5739e2021-10-28 15:41:38 +02007236 size_t buf_len;
William Lallemand746e6f32021-10-06 10:57:44 +02007237 int ret;
William Lallemand3956c4e2021-09-21 16:25:15 +02007238
7239 hlua = hlua_gethlua(L);
7240
7241 if (!hlua || !hlua->task)
7242 WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in "
7243 "'frontend', 'backend' or 'task'"));
7244
William Lallemand746e6f32021-10-06 10:57:44 +02007245 if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE)
7246 WILL_LJMP(luaL_error(L, "'get' needs a table as argument"));
7247
William Lallemand4f4f2b72022-02-17 20:00:23 +01007248 hlua_hc = hlua_checkhttpclient(L, 1);
7249
7250 ret = lua_getfield(L, -1, "dst");
7251 if (ret == LUA_TSTRING) {
7252 if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0)
7253 WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument"));
7254 }
7255 lua_pop(L, 1);
7256
William Lallemand746e6f32021-10-06 10:57:44 +02007257 ret = lua_getfield(L, -1, "url");
7258 if (ret == LUA_TSTRING) {
7259 url_str = lua_tostring(L, -1);
7260 }
7261 lua_pop(L, 1);
William Lallemand3956c4e2021-09-21 16:25:15 +02007262
William Lallemandb4a4ef62022-02-23 14:18:16 +01007263 ret = lua_getfield(L, -1, "timeout");
7264 if (ret == LUA_TNUMBER) {
7265 timeout = lua_tointeger(L, -1);
7266 httpclient_set_timeout(hlua_hc->hc, timeout);
7267 }
William Lallemandab90ee82022-02-23 15:16:08 +01007268 lua_pop(L, 1);
William Lallemandb4a4ef62022-02-23 14:18:16 +01007269
William Lallemand746e6f32021-10-06 10:57:44 +02007270 ret = lua_getfield(L, -1, "headers");
7271 if (ret == LUA_TTABLE) {
7272 hdrs = hlua_httpclient_table_to_hdrs(L);
7273 }
7274 lua_pop(L, 1);
William Lallemand79416cb2021-09-24 14:51:44 +02007275
William Lallemanddec25c32021-10-25 19:48:37 +02007276 ret = lua_getfield(L, -1, "body");
7277 if (ret == LUA_TSTRING) {
William Lallemandbd5739e2021-10-28 15:41:38 +02007278 body_str = lua_tolstring(L, -1, &buf_len);
William Lallemanddec25c32021-10-25 19:48:37 +02007279 }
William Lallemandbd5739e2021-10-28 15:41:38 +02007280
William Lallemanddec25c32021-10-25 19:48:37 +02007281 lua_pop(L, 1);
7282
William Lallemand746e6f32021-10-06 10:57:44 +02007283 if (!url_str) {
7284 WILL_LJMP(luaL_error(L, "'get' need a 'url' argument"));
7285 return 0;
7286 }
William Lallemand3956c4e2021-09-21 16:25:15 +02007287
William Lallemand3956c4e2021-09-21 16:25:15 +02007288
7289 hlua_hc->hc->req.url = istdup(ist(url_str));
William Lallemanddc2cc902021-10-26 11:43:26 +02007290 hlua_hc->hc->req.meth = meth;
William Lallemand3956c4e2021-09-21 16:25:15 +02007291
7292 /* update the httpclient callbacks */
William Lallemandbd5739e2021-10-28 15:41:38 +02007293 hlua_hc->hc->ops.res_stline = hlua_httpclient_cb;
7294 hlua_hc->hc->ops.res_headers = hlua_httpclient_cb;
7295 hlua_hc->hc->ops.res_payload = hlua_httpclient_cb;
William Lallemand3956c4e2021-09-21 16:25:15 +02007296
William Lallemandbd5739e2021-10-28 15:41:38 +02007297 /* a body is available, it will use the request callback */
7298 if (body_str) {
7299 hlua_hc->hc->ops.req_payload = hlua_httpclient_cb;
7300 }
William Lallemand3956c4e2021-09-21 16:25:15 +02007301
William Lallemandbd5739e2021-10-28 15:41:38 +02007302 ret = httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, IST_NULL);
William Lallemand3956c4e2021-09-21 16:25:15 +02007303
William Lallemand746e6f32021-10-06 10:57:44 +02007304 /* free the temporary headers array */
7305 hdrs_i = hdrs;
7306 while (hdrs_i && isttest(hdrs_i->n)) {
7307 istfree(&hdrs_i->n);
7308 istfree(&hdrs_i->v);
7309 hdrs_i++;
7310 }
7311 ha_free(&hdrs);
7312
7313
William Lallemand6137a9e2021-10-26 15:01:53 +02007314 if (ret != ERR_NONE) {
7315 WILL_LJMP(luaL_error(L, "Can't generate the HTTP request"));
7316 return 0;
7317 }
7318
William Lallemand6137a9e2021-10-26 15:01:53 +02007319 httpclient_start(hlua_hc->hc);
7320
William Lallemandbd5739e2021-10-28 15:41:38 +02007321 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0));
William Lallemand3956c4e2021-09-21 16:25:15 +02007322
William Lallemand3956c4e2021-09-21 16:25:15 +02007323 return 0;
7324}
7325
7326/*
William Lallemanddc2cc902021-10-26 11:43:26 +02007327 * Sends an HTTP HEAD request and wait for a response
7328 *
7329 * httpclient:head(url, headers, payload)
7330 */
7331__LJMP static int hlua_httpclient_head(lua_State *L)
7332{
7333 return hlua_httpclient_send(L, HTTP_METH_HEAD);
7334}
7335
7336/*
7337 * Send an HTTP GET request and wait for a response
7338 *
7339 * httpclient:get(url, headers, payload)
7340 */
7341__LJMP static int hlua_httpclient_get(lua_State *L)
7342{
7343 return hlua_httpclient_send(L, HTTP_METH_GET);
7344
7345}
7346
7347/*
7348 * Sends an HTTP PUT request and wait for a response
7349 *
7350 * httpclient:put(url, headers, payload)
7351 */
7352__LJMP static int hlua_httpclient_put(lua_State *L)
7353{
7354 return hlua_httpclient_send(L, HTTP_METH_PUT);
7355}
7356
7357/*
7358 * Send an HTTP POST request and wait for a response
7359 *
7360 * httpclient:post(url, headers, payload)
7361 */
7362__LJMP static int hlua_httpclient_post(lua_State *L)
7363{
7364 return hlua_httpclient_send(L, HTTP_METH_POST);
7365}
7366
7367
7368/*
7369 * Sends an HTTP DELETE request and wait for a response
7370 *
7371 * httpclient:delete(url, headers, payload)
7372 */
7373__LJMP static int hlua_httpclient_delete(lua_State *L)
7374{
7375 return hlua_httpclient_send(L, HTTP_METH_DELETE);
7376}
7377
7378/*
William Lallemand3956c4e2021-09-21 16:25:15 +02007379 *
7380 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007381 * Class TXN
7382 *
7383 *
7384 */
7385
7386/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02007387 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007388 */
7389__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
7390{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007391 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007392}
7393
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007394__LJMP static int hlua_set_var(lua_State *L)
7395{
7396 struct hlua_txn *htxn;
7397 const char *name;
7398 size_t len;
7399 struct sample smp;
7400
Tim Duesterhus4e172c92020-05-19 13:49:42 +02007401 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7402 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007403
7404 /* It is useles to retrieve the stream, but this function
7405 * runs only in a stream context.
7406 */
7407 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7408 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
7409
7410 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01007411 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007412 hlua_lua2smp(L, 3, &smp);
7413
7414 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01007415 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02007416
7417 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
7418 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
7419 else
7420 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
7421
Tim Duesterhus84ebc132020-05-19 13:49:41 +02007422 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007423}
7424
Christopher Faulet85d79c92016-11-09 16:54:56 +01007425__LJMP static int hlua_unset_var(lua_State *L)
7426{
7427 struct hlua_txn *htxn;
7428 const char *name;
7429 size_t len;
7430 struct sample smp;
7431
7432 MAY_LJMP(check_args(L, 2, "unset_var"));
7433
7434 /* It is useles to retrieve the stream, but this function
7435 * runs only in a stream context.
7436 */
7437 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7438 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
7439
7440 /* Unset the variable. */
7441 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02007442 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
7443 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01007444}
7445
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007446__LJMP static int hlua_get_var(lua_State *L)
7447{
7448 struct hlua_txn *htxn;
7449 const char *name;
7450 size_t len;
7451 struct sample smp;
7452
7453 MAY_LJMP(check_args(L, 2, "get_var"));
7454
7455 /* It is useles to retrieve the stream, but this function
7456 * runs only in a stream context.
7457 */
7458 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7459 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
7460
Willy Tarreau7560dd42016-03-10 16:28:58 +01007461 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreaue352b9d2021-09-03 11:52:38 +02007462 if (!vars_get_by_name(name, len, &smp, NULL)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007463 lua_pushnil(L);
7464 return 1;
7465 }
7466
7467 return hlua_smp2lua(L, &smp);
7468}
7469
Willy Tarreau59551662015-03-10 14:23:13 +01007470__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007471{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007472 struct hlua *hlua;
7473
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007474 MAY_LJMP(check_args(L, 2, "set_priv"));
7475
Willy Tarreau87b09662015-04-03 00:22:06 +02007476 /* It is useles to retrieve the stream, but this function
7477 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007478 */
7479 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007480
7481 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007482 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007483 if (!hlua)
7484 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007485
7486 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02007487 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007488
7489 /* Get and store new value. */
7490 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
7491 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
7492
7493 return 0;
7494}
7495
Willy Tarreau59551662015-03-10 14:23:13 +01007496__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007497{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007498 struct hlua *hlua;
7499
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007500 MAY_LJMP(check_args(L, 1, "get_priv"));
7501
Willy Tarreau87b09662015-04-03 00:22:06 +02007502 /* It is useles to retrieve the stream, but this function
7503 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007504 */
7505 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007506
7507 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007508 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007509 if (!hlua) {
7510 lua_pushnil(L);
7511 return 1;
7512 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007513
7514 /* Push configuration index in the stack. */
7515 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
7516
7517 return 1;
7518}
7519
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007520/* Create stack entry containing a class TXN. This function
7521 * return 0 if the stack does not contains free slots,
7522 * otherwise it returns 1.
7523 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02007524static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007525{
Willy Tarreaude491382015-04-06 11:04:28 +02007526 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007527
7528 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01007529 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007530 return 0;
7531
7532 /* NOTE: The allocation never fails. The failure
7533 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007534 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007535 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01007536 /* Create the object: obj[0] = userdata. */
7537 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02007538 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01007539 lua_rawseti(L, -2, 0);
7540
Willy Tarreaude491382015-04-06 11:04:28 +02007541 htxn->s = s;
7542 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01007543 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02007544 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007545
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007546 /* Create the "f" field that contains a list of fetches. */
7547 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01007548 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01007549 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007550 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01007551
7552 /* Create the "sf" field that contains a list of stringsafe fetches. */
7553 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01007554 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007555 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007556 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007557
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007558 /* Create the "c" field that contains a list of converters. */
7559 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02007560 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01007561 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007562 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01007563
7564 /* Create the "sc" field that contains a list of stringsafe converters. */
7565 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01007566 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007567 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007568 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007569
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007570 /* Create the "req" field that contains the request channel object. */
7571 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01007572 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007573 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007574 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007575
7576 /* Create the "res" field that contains the response channel object. */
7577 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01007578 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007579 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007580 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01007581
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007582 /* Creates the HTTP object is the current proxy allows http. */
7583 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01007584 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02007585 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007586 return 0;
7587 }
7588 else
7589 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007590 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007591
Christopher Faulet78c35472020-02-26 17:14:08 +01007592 if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) {
7593 /* HTTPMessage object are created when a lua TXN is created from
7594 * a filter context only
7595 */
7596
7597 /* Creates the HTTP-Request object is the current proxy allows http. */
7598 lua_pushstring(L, "http_req");
7599 if (p->mode == PR_MODE_HTTP) {
7600 if (!hlua_http_msg_new(L, &s->txn->req))
7601 return 0;
7602 }
7603 else
7604 lua_pushnil(L);
7605 lua_rawset(L, -3);
7606
7607 /* Creates the HTTP-Response object is the current proxy allows http. */
7608 lua_pushstring(L, "http_res");
7609 if (p->mode == PR_MODE_HTTP) {
7610 if (!hlua_http_msg_new(L, &s->txn->rsp))
7611 return 0;
7612 }
7613 else
7614 lua_pushnil(L);
7615 lua_rawset(L, -3);
7616 }
7617
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007618 /* Pop a class sesison metatable and affect it to the userdata. */
7619 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
7620 lua_setmetatable(L, -2);
7621
7622 return 1;
7623}
7624
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007625__LJMP static int hlua_txn_deflog(lua_State *L)
7626{
7627 const char *msg;
7628 struct hlua_txn *htxn;
7629
7630 MAY_LJMP(check_args(L, 2, "deflog"));
7631 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7632 msg = MAY_LJMP(luaL_checkstring(L, 2));
7633
7634 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
7635 return 0;
7636}
7637
7638__LJMP static int hlua_txn_log(lua_State *L)
7639{
7640 int level;
7641 const char *msg;
7642 struct hlua_txn *htxn;
7643
7644 MAY_LJMP(check_args(L, 3, "log"));
7645 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7646 level = MAY_LJMP(luaL_checkinteger(L, 2));
7647 msg = MAY_LJMP(luaL_checkstring(L, 3));
7648
7649 if (level < 0 || level >= NB_LOG_LEVELS)
7650 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
7651
7652 hlua_sendlog(htxn->s->be, level, msg);
7653 return 0;
7654}
7655
7656__LJMP static int hlua_txn_log_debug(lua_State *L)
7657{
7658 const char *msg;
7659 struct hlua_txn *htxn;
7660
7661 MAY_LJMP(check_args(L, 2, "Debug"));
7662 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7663 msg = MAY_LJMP(luaL_checkstring(L, 2));
7664 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
7665 return 0;
7666}
7667
7668__LJMP static int hlua_txn_log_info(lua_State *L)
7669{
7670 const char *msg;
7671 struct hlua_txn *htxn;
7672
7673 MAY_LJMP(check_args(L, 2, "Info"));
7674 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7675 msg = MAY_LJMP(luaL_checkstring(L, 2));
7676 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
7677 return 0;
7678}
7679
7680__LJMP static int hlua_txn_log_warning(lua_State *L)
7681{
7682 const char *msg;
7683 struct hlua_txn *htxn;
7684
7685 MAY_LJMP(check_args(L, 2, "Warning"));
7686 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7687 msg = MAY_LJMP(luaL_checkstring(L, 2));
7688 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
7689 return 0;
7690}
7691
7692__LJMP static int hlua_txn_log_alert(lua_State *L)
7693{
7694 const char *msg;
7695 struct hlua_txn *htxn;
7696
7697 MAY_LJMP(check_args(L, 2, "Alert"));
7698 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7699 msg = MAY_LJMP(luaL_checkstring(L, 2));
7700 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
7701 return 0;
7702}
7703
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007704__LJMP static int hlua_txn_set_loglevel(lua_State *L)
7705{
7706 struct hlua_txn *htxn;
7707 int ll;
7708
7709 MAY_LJMP(check_args(L, 2, "set_loglevel"));
7710 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7711 ll = MAY_LJMP(luaL_checkinteger(L, 2));
7712
7713 if (ll < 0 || ll > 7)
7714 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
7715
7716 htxn->s->logs.level = ll;
7717 return 0;
7718}
7719
7720__LJMP static int hlua_txn_set_tos(lua_State *L)
7721{
7722 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007723 int tos;
7724
7725 MAY_LJMP(check_args(L, 2, "set_tos"));
7726 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7727 tos = MAY_LJMP(luaL_checkinteger(L, 2));
7728
Willy Tarreau1a18b542018-12-11 16:37:42 +01007729 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007730 return 0;
7731}
7732
7733__LJMP static int hlua_txn_set_mark(lua_State *L)
7734{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007735 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007736 int mark;
7737
7738 MAY_LJMP(check_args(L, 2, "set_mark"));
7739 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7740 mark = MAY_LJMP(luaL_checkinteger(L, 2));
7741
Lukas Tribus579e3e32019-08-11 18:03:45 +02007742 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007743 return 0;
7744}
7745
Patrick Hemmer268a7072018-05-11 12:52:31 -04007746__LJMP static int hlua_txn_set_priority_class(lua_State *L)
7747{
7748 struct hlua_txn *htxn;
7749
7750 MAY_LJMP(check_args(L, 2, "set_priority_class"));
7751 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7752 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
7753 return 0;
7754}
7755
7756__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
7757{
7758 struct hlua_txn *htxn;
7759
7760 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
7761 htxn = MAY_LJMP(hlua_checktxn(L, 1));
7762 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
7763 return 0;
7764}
7765
Christopher Faulet700d9e82020-01-31 12:21:52 +01007766/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007767 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01007768 * message and terminate the transaction. It returns 1 on success and 0 on
7769 * error. The Reply must be on top of the stack.
7770 */
7771__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
7772{
7773 struct htx *htx;
7774 struct htx_sl *sl;
7775 struct h1m h1m;
7776 const char *status, *reason, *body;
7777 size_t status_len, reason_len, body_len;
7778 int ret, code, flags;
7779
7780 code = 200;
7781 status = "200";
7782 status_len = 3;
7783 ret = lua_getfield(L, -1, "status");
7784 if (ret == LUA_TNUMBER) {
7785 code = lua_tointeger(L, -1);
7786 status = lua_tolstring(L, -1, &status_len);
7787 }
7788 lua_pop(L, 1);
7789
7790 reason = http_get_reason(code);
7791 reason_len = strlen(reason);
7792 ret = lua_getfield(L, -1, "reason");
7793 if (ret == LUA_TSTRING)
7794 reason = lua_tolstring(L, -1, &reason_len);
7795 lua_pop(L, 1);
7796
7797 body = NULL;
7798 body_len = 0;
7799 ret = lua_getfield(L, -1, "body");
7800 if (ret == LUA_TSTRING)
7801 body = lua_tolstring(L, -1, &body_len);
7802 lua_pop(L, 1);
7803
7804 /* Prepare the response before inserting the headers */
7805 h1m_init_res(&h1m);
7806 htx = htx_from_buf(&s->res.buf);
7807 channel_htx_truncate(&s->res, htx);
7808 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
7809 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
7810 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
7811 ist2(status, status_len), ist2(reason, reason_len));
7812 }
7813 else {
7814 flags = HTX_SL_F_IS_RESP;
7815 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
7816 ist2(status, status_len), ist2(reason, reason_len));
7817 }
7818 if (!sl)
7819 goto fail;
7820 sl->info.res.status = code;
7821
7822 /* Push in the stack the "headers" entry. */
7823 ret = lua_getfield(L, -1, "headers");
7824 if (ret != LUA_TTABLE)
7825 goto skip_headers;
7826
7827 lua_pushnil(L);
7828 while (lua_next(L, -2) != 0) {
7829 struct ist name, value;
7830 const char *n, *v;
7831 size_t nlen, vlen;
7832
7833 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
7834 /* Skip element if the key is not a string or if the value is not a table */
7835 goto next_hdr;
7836 }
7837
7838 n = lua_tolstring(L, -2, &nlen);
7839 name = ist2(n, nlen);
7840 if (isteqi(name, ist("content-length"))) {
7841 /* Always skip content-length header. It will be added
7842 * later with the correct len
7843 */
7844 goto next_hdr;
7845 }
7846
7847 /* Loop on header's values */
7848 lua_pushnil(L);
7849 while (lua_next(L, -2)) {
7850 if (!lua_isstring(L, -1)) {
7851 /* Skip the value if it is not a string */
7852 goto next_value;
7853 }
7854
7855 v = lua_tolstring(L, -1, &vlen);
7856 value = ist2(v, vlen);
7857
7858 if (isteqi(name, ist("transfer-encoding")))
7859 h1_parse_xfer_enc_header(&h1m, value);
7860 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
7861 goto fail;
7862
7863 next_value:
7864 lua_pop(L, 1);
7865 }
7866
7867 next_hdr:
7868 lua_pop(L, 1);
7869 }
7870 skip_headers:
7871 lua_pop(L, 1);
7872
7873 /* Update h1m flags: CLEN is set if CHNK is not present */
7874 if (!(h1m.flags & H1_MF_CHNK)) {
7875 const char *clen = ultoa(body_len);
7876
7877 h1m.flags |= H1_MF_CLEN;
7878 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
7879 goto fail;
7880 }
7881 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
7882 h1m.flags |= H1_MF_XFER_LEN;
7883
7884 /* Update HTX start-line flags */
7885 if (h1m.flags & H1_MF_XFER_ENC)
7886 flags |= HTX_SL_F_XFER_ENC;
7887 if (h1m.flags & H1_MF_XFER_LEN) {
7888 flags |= HTX_SL_F_XFER_LEN;
7889 if (h1m.flags & H1_MF_CHNK)
7890 flags |= HTX_SL_F_CHNK;
7891 else if (h1m.flags & H1_MF_CLEN)
7892 flags |= HTX_SL_F_CLEN;
7893 if (h1m.body_len == 0)
7894 flags |= HTX_SL_F_BODYLESS;
7895 }
7896 sl->flags |= flags;
7897
7898
7899 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007900 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01007901 goto fail;
7902
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007903 htx->flags |= HTX_FL_EOM;
7904
Christopher Faulet700d9e82020-01-31 12:21:52 +01007905 /* Now, forward the response and terminate the transaction */
7906 s->txn->status = code;
7907 htx_to_buf(htx, &s->res.buf);
7908 if (!http_forward_proxy_resp(s, 1))
7909 goto fail;
7910
7911 return 1;
7912
7913 fail:
7914 channel_htx_truncate(&s->res, htx);
7915 return 0;
7916}
7917
7918/* Terminate a transaction if called from a lua action. For TCP streams,
7919 * processing is just aborted. Nothing is returned to the client and all
7920 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
7921 * is forwarded to the client before terminating the transaction. On success,
7922 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
7923 * with ACT_RET_ERR code. If this function is not called from a lua action, it
7924 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007925 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007926__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007927{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02007928 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01007929 struct stream *s;
7930 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007931
Willy Tarreaub2ccb562015-04-06 11:11:15 +02007932 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007933
Christopher Faulet700d9e82020-01-31 12:21:52 +01007934 /* If the flags NOTERM is set, we cannot terminate the session, so we
7935 * just end the execution of the current lua code. */
7936 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02007937 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02007938
Christopher Faulet700d9e82020-01-31 12:21:52 +01007939 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01007940 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01007941 struct channel *req = &s->req;
7942 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01007943
Christopher Faulet700d9e82020-01-31 12:21:52 +01007944 channel_auto_read(req);
7945 channel_abort(req);
7946 channel_auto_close(req);
7947 channel_erase(req);
7948
7949 res->wex = tick_add_ifset(now_ms, res->wto);
7950 channel_auto_read(res);
7951 channel_auto_close(res);
7952 channel_shutr_now(res);
7953
7954 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
7955 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02007956 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02007957
Christopher Faulet700d9e82020-01-31 12:21:52 +01007958 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
7959 /* No reply or invalid reply */
7960 s->txn->status = 0;
7961 http_reply_and_close(s, 0, NULL);
7962 }
7963 else {
7964 /* Remove extra args to have the reply on top of the stack */
7965 if (lua_gettop(L) > 2)
7966 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01007967
Christopher Faulet700d9e82020-01-31 12:21:52 +01007968 if (!hlua_txn_forward_reply(L, s)) {
7969 if (!(s->flags & SF_ERR_MASK))
7970 s->flags |= SF_ERR_PRXCOND;
7971 lua_pushinteger(L, ACT_RET_ERR);
7972 WILL_LJMP(hlua_done(L));
7973 return 0; /* Never reached */
7974 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02007975 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007976
Christopher Faulet700d9e82020-01-31 12:21:52 +01007977 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
7978 if (htxn->dir == SMP_OPT_DIR_REQ) {
7979 /* let's log the request time */
7980 s->logs.tv_request = now;
7981 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02007982 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01007983 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02007984
Christopher Faulet700d9e82020-01-31 12:21:52 +01007985 done:
7986 if (!(s->flags & SF_ERR_MASK))
7987 s->flags |= SF_ERR_LOCAL;
7988 if (!(s->flags & SF_FINST_MASK))
7989 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02007990
Christopher Faulete48d1dc2021-08-13 14:11:17 +02007991 if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX)
7992 lua_pushinteger(L, -1);
7993 else
7994 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007995 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007996 return 0;
7997}
7998
Christopher Faulet700d9e82020-01-31 12:21:52 +01007999/*
8000 *
8001 *
8002 * Class REPLY
8003 *
8004 *
8005 */
8006
8007/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
8008 * free slots, the function fails and returns 0;
8009 */
8010static int hlua_txn_reply_new(lua_State *L)
8011{
8012 struct hlua_txn *htxn;
8013 const char *reason, *body = NULL;
8014 int ret, status;
8015
8016 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01008017 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01008018 hlua_pusherror(L, "txn object is not an HTTP transaction.");
8019 WILL_LJMP(lua_error(L));
8020 }
8021
8022 /* Default value */
8023 status = 200;
8024 reason = http_get_reason(status);
8025
8026 if (lua_istable(L, 2)) {
8027 /* load status and reason from the table argument at index 2 */
8028 ret = lua_getfield(L, 2, "status");
8029 if (ret == LUA_TNIL)
8030 goto reason;
8031 else if (ret != LUA_TNUMBER) {
8032 /* invalid status: ignore the reason */
8033 goto body;
8034 }
8035 status = lua_tointeger(L, -1);
8036
8037 reason:
8038 lua_pop(L, 1); /* restore the stack: remove status */
8039 ret = lua_getfield(L, 2, "reason");
8040 if (ret == LUA_TSTRING)
8041 reason = lua_tostring(L, -1);
8042
8043 body:
8044 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
8045 ret = lua_getfield(L, 2, "body");
8046 if (ret == LUA_TSTRING)
8047 body = lua_tostring(L, -1);
8048 lua_pop(L, 1); /* restore the stack: remove body */
8049 }
8050
8051 /* Create the Reply table */
8052 lua_newtable(L);
8053
8054 /* Add status element */
8055 lua_pushstring(L, "status");
8056 lua_pushinteger(L, status);
8057 lua_settable(L, -3);
8058
8059 /* Add reason element */
8060 reason = http_get_reason(status);
8061 lua_pushstring(L, "reason");
8062 lua_pushstring(L, reason);
8063 lua_settable(L, -3);
8064
8065 /* Add body element, nil if undefined */
8066 lua_pushstring(L, "body");
8067 if (body)
8068 lua_pushstring(L, body);
8069 else
8070 lua_pushnil(L);
8071 lua_settable(L, -3);
8072
8073 /* Add headers element */
8074 lua_pushstring(L, "headers");
8075 lua_newtable(L);
8076
8077 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
8078 if (lua_istable(L, 2)) {
8079 /* load headers from the table argument at index 2. If it is a table, copy it. */
8080 ret = lua_getfield(L, 2, "headers");
8081 if (ret == LUA_TTABLE) {
8082 /* stack: [ ... <headers:table>, <table> ] */
8083 lua_pushnil(L);
8084 while (lua_next(L, -2) != 0) {
8085 /* stack: [ ... <headers:table>, <table>, k, v] */
8086 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
8087 /* invalid value type, skip it */
8088 lua_pop(L, 1);
8089 continue;
8090 }
8091
8092
8093 /* Duplicate the key and swap it with the value. */
8094 lua_pushvalue(L, -2);
8095 lua_insert(L, -2);
8096 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
8097
8098 lua_newtable(L);
8099 lua_insert(L, -2);
8100 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
8101
8102 if (lua_isstring(L, -1)) {
8103 /* push the value in the inner table */
8104 lua_rawseti(L, -2, 1);
8105 }
8106 else { /* table */
8107 lua_pushnil(L);
8108 while (lua_next(L, -2) != 0) {
8109 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
8110 if (!lua_isstring(L, -1)) {
8111 /* invalid value type, skip it*/
8112 lua_pop(L, 1);
8113 continue;
8114 }
8115 /* push the value in the inner table */
8116 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
8117 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
8118 }
8119 lua_pop(L, 1);
8120 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
8121 }
8122
8123 /* push (k,v) on the stack in the headers table:
8124 * stack: [ ... <headers:table>, <table>, k, k, v ]
8125 */
8126 lua_settable(L, -5);
8127 /* stack: [ ... <headers:table>, <table>, k ] */
8128 }
8129 }
8130 lua_pop(L, 1);
8131 }
8132 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
8133 lua_settable(L, -3);
8134 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
8135
8136 /* Pop a class sesison metatable and affect it to the userdata. */
8137 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
8138 lua_setmetatable(L, -2);
8139 return 1;
8140}
8141
8142/* Set the reply status code, and optionally the reason. If no reason is
8143 * provided, the default one corresponding to the status code is used.
8144 */
8145__LJMP static int hlua_txn_reply_set_status(lua_State *L)
8146{
8147 int status = MAY_LJMP(luaL_checkinteger(L, 2));
8148 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
8149
8150 /* First argument (self) must be a table */
8151 luaL_checktype(L, 1, LUA_TTABLE);
8152
8153 if (status < 100 || status > 599) {
8154 lua_pushboolean(L, 0);
8155 return 1;
8156 }
8157 if (!reason)
8158 reason = http_get_reason(status);
8159
8160 lua_pushinteger(L, status);
8161 lua_setfield(L, 1, "status");
8162
8163 lua_pushstring(L, reason);
8164 lua_setfield(L, 1, "reason");
8165
8166 lua_pushboolean(L, 1);
8167 return 1;
8168}
8169
8170/* Add a header into the reply object. Each header name is associated to an
8171 * array of values in the "headers" table. If the header name is not found, a
8172 * new entry is created.
8173 */
8174__LJMP static int hlua_txn_reply_add_header(lua_State *L)
8175{
8176 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
8177 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
8178 int ret;
8179
8180 /* First argument (self) must be a table */
8181 luaL_checktype(L, 1, LUA_TTABLE);
8182
8183 /* Push in the stack the "headers" entry. */
8184 ret = lua_getfield(L, 1, "headers");
8185 if (ret != LUA_TTABLE) {
8186 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
8187 WILL_LJMP(lua_error(L));
8188 }
8189
8190 /* check if the header is already registered. If not, register it. */
8191 ret = lua_getfield(L, -1, name);
8192 if (ret == LUA_TNIL) {
8193 /* Entry not found. */
8194 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
8195
8196 /* Insert the new header name in the array in the top of the stack.
8197 * It left the new array in the top of the stack.
8198 */
8199 lua_newtable(L);
8200 lua_pushstring(L, name);
8201 lua_pushvalue(L, -2);
8202 lua_settable(L, -4);
8203 }
8204 else if (ret != LUA_TTABLE) {
8205 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
8206 WILL_LJMP(lua_error(L));
8207 }
8208
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008209 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01008210 * the header value as new entry.
8211 */
8212 lua_pushstring(L, value);
8213 ret = lua_rawlen(L, -2);
8214 lua_rawseti(L, -2, ret + 1);
8215
8216 lua_pushboolean(L, 1);
8217 return 1;
8218}
8219
8220/* Remove all occurrences of a given header name. */
8221__LJMP static int hlua_txn_reply_del_header(lua_State *L)
8222{
8223 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
8224 int ret;
8225
8226 /* First argument (self) must be a table */
8227 luaL_checktype(L, 1, LUA_TTABLE);
8228
8229 /* Push in the stack the "headers" entry. */
8230 ret = lua_getfield(L, 1, "headers");
8231 if (ret != LUA_TTABLE) {
8232 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
8233 WILL_LJMP(lua_error(L));
8234 }
8235
8236 lua_pushstring(L, name);
8237 lua_pushnil(L);
8238 lua_settable(L, -3);
8239
8240 lua_pushboolean(L, 1);
8241 return 1;
8242}
8243
8244/* Set the reply's body. Overwrite any existing entry. */
8245__LJMP static int hlua_txn_reply_set_body(lua_State *L)
8246{
8247 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
8248
8249 /* First argument (self) must be a table */
8250 luaL_checktype(L, 1, LUA_TTABLE);
8251
8252 lua_pushstring(L, payload);
8253 lua_setfield(L, 1, "body");
8254
8255 lua_pushboolean(L, 1);
8256 return 1;
8257}
8258
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008259__LJMP static int hlua_log(lua_State *L)
8260{
8261 int level;
8262 const char *msg;
8263
8264 MAY_LJMP(check_args(L, 2, "log"));
8265 level = MAY_LJMP(luaL_checkinteger(L, 1));
8266 msg = MAY_LJMP(luaL_checkstring(L, 2));
8267
8268 if (level < 0 || level >= NB_LOG_LEVELS)
8269 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
8270
8271 hlua_sendlog(NULL, level, msg);
8272 return 0;
8273}
8274
8275__LJMP static int hlua_log_debug(lua_State *L)
8276{
8277 const char *msg;
8278
8279 MAY_LJMP(check_args(L, 1, "debug"));
8280 msg = MAY_LJMP(luaL_checkstring(L, 1));
8281 hlua_sendlog(NULL, LOG_DEBUG, msg);
8282 return 0;
8283}
8284
8285__LJMP static int hlua_log_info(lua_State *L)
8286{
8287 const char *msg;
8288
8289 MAY_LJMP(check_args(L, 1, "info"));
8290 msg = MAY_LJMP(luaL_checkstring(L, 1));
8291 hlua_sendlog(NULL, LOG_INFO, msg);
8292 return 0;
8293}
8294
8295__LJMP static int hlua_log_warning(lua_State *L)
8296{
8297 const char *msg;
8298
8299 MAY_LJMP(check_args(L, 1, "warning"));
8300 msg = MAY_LJMP(luaL_checkstring(L, 1));
8301 hlua_sendlog(NULL, LOG_WARNING, msg);
8302 return 0;
8303}
8304
8305__LJMP static int hlua_log_alert(lua_State *L)
8306{
8307 const char *msg;
8308
8309 MAY_LJMP(check_args(L, 1, "alert"));
8310 msg = MAY_LJMP(luaL_checkstring(L, 1));
8311 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01008312 return 0;
8313}
8314
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01008315__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008316{
8317 int wakeup_ms = lua_tointeger(L, -1);
Willy Tarreau12c02702021-09-30 16:12:31 +02008318 if (!tick_is_expired(wakeup_ms, now_ms))
Willy Tarreau9635e032018-10-16 17:52:55 +02008319 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008320 return 0;
8321}
8322
8323__LJMP static int hlua_sleep(lua_State *L)
8324{
8325 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008326 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008327
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008328 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008329
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01008330 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008331 wakeup_ms = tick_add(now_ms, delay);
8332 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008333
Willy Tarreau9635e032018-10-16 17:52:55 +02008334 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008335 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008336}
8337
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008338__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008339{
8340 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008341 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008342
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008343 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008344
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01008345 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008346 wakeup_ms = tick_add(now_ms, delay);
8347 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008348
Willy Tarreau9635e032018-10-16 17:52:55 +02008349 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008350 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008351}
8352
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008353/* This functionis an LUA binding. it permits to give back
8354 * the hand at the HAProxy scheduler. It is used when the
8355 * LUA processing consumes a lot of time.
8356 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01008357__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008358{
8359 return 0;
8360}
8361
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008362__LJMP static int hlua_yield(lua_State *L)
8363{
Willy Tarreau9635e032018-10-16 17:52:55 +02008364 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01008365 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008366}
8367
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008368/* This function change the nice of the currently executed
8369 * task. It is used set low or high priority at the current
8370 * task.
8371 */
Willy Tarreau59551662015-03-10 14:23:13 +01008372__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008373{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008374 struct hlua *hlua;
8375 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008376
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008377 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008378 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008379
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008380 /* Get hlua struct, or NULL if we execute from main lua state */
8381 hlua = hlua_gethlua(L);
8382
8383 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008384 if (!hlua || !hlua->task)
8385 return 0;
8386
8387 if (nice < -1024)
8388 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008389 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01008390 nice = 1024;
8391
8392 hlua->task->nice = nice;
8393 return 0;
8394}
8395
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008396/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008397 * HAProxy task subsystem when the task is awaked. The LUA runtime can
8398 * return an E_AGAIN signal, the emmiter of this signal must set a
8399 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008400 *
8401 * Task wrapper are longjmp safe because the only one Lua code
8402 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008403 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01008404struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008405{
Olivier Houchard9f6af332018-05-25 14:04:04 +02008406 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008407 enum hlua_exec status;
8408
Christopher Faulet5bc99722018-04-25 10:34:45 +02008409 if (task->thread_mask == MAX_THREADS_MASK)
8410 task_set_affinity(task, tid_bit);
8411
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008412 /* If it is the first call to the task, we must initialize the
8413 * execution timeouts.
8414 */
8415 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02008416 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008417
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008418 /* Execute the Lua code. */
8419 status = hlua_ctx_resume(hlua, 1);
8420
8421 switch (status) {
8422 /* finished or yield */
8423 case HLUA_E_OK:
8424 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02008425 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02008426 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008427 break;
8428
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01008429 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01008430 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02008431 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008432 break;
8433
8434 /* finished with error. */
8435 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008436 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008437 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02008438 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02008439 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008440 break;
8441
8442 case HLUA_E_ERR:
8443 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008444 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008445 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02008446 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02008447 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008448 break;
8449 }
Emeric Brun253e53e2017-10-17 18:58:40 +02008450 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008451}
8452
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008453/* This function is an LUA binding that register LUA function to be
8454 * executed after the HAProxy configuration parsing and before the
8455 * HAProxy scheduler starts. This function expect only one LUA
8456 * argument that is a function. This function returns nothing, but
8457 * throws if an error is encountered.
8458 */
8459__LJMP static int hlua_register_init(lua_State *L)
8460{
8461 struct hlua_init_function *init;
8462 int ref;
8463
8464 MAY_LJMP(check_args(L, 1, "register_init"));
8465
8466 ref = MAY_LJMP(hlua_checkfunction(L, 1));
8467
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008468 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008469 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008470 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008471
8472 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02008473 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008474 return 0;
8475}
8476
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008477/* This functio is an LUA binding. It permits to register a task
8478 * executed in parallel of the main HAroxy activity. The task is
8479 * created and it is set in the HAProxy scheduler. It can be called
8480 * from the "init" section, "post init" or during the runtime.
8481 *
8482 * Lua prototype:
8483 *
8484 * <none> core.register_task(<function>)
8485 */
8486static int hlua_register_task(lua_State *L)
8487{
Christopher Faulet5294ec02021-04-12 12:24:47 +02008488 struct hlua *hlua = NULL;
8489 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008490 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01008491 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008492
8493 MAY_LJMP(check_args(L, 1, "register_task"));
8494
8495 ref = MAY_LJMP(hlua_checkfunction(L, 1));
8496
Thierry Fournier75fc0292020-11-28 13:18:56 +01008497 /* Get the reference state. If the reference is NULL, L is the master
8498 * state, otherwise hlua->T is.
8499 */
8500 hlua = hlua_gethlua(L);
8501 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01008502 /* we are in runtime processing */
8503 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01008504 else
Thierry Fournier021d9862020-11-28 23:42:03 +01008505 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01008506 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01008507
Willy Tarreaubafbe012017-11-24 17:34:44 +01008508 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008509 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02008510 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008511 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008512
Thierry Fournier59f11be2020-11-29 00:37:41 +01008513 /* We are in the common lua state, execute the task anywhere,
8514 * otherwise, inherit the current thread identifier
8515 */
8516 if (state_id == 0)
Willy Tarreaubeeabf52021-10-01 18:23:30 +02008517 task = task_new_anywhere();
Thierry Fournier59f11be2020-11-29 00:37:41 +01008518 else
Willy Tarreaubeeabf52021-10-01 18:23:30 +02008519 task = task_new_here();
Willy Tarreaue09101e2018-10-16 17:37:12 +02008520 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02008521 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02008522
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008523 task->context = hlua;
8524 task->process = hlua_process_task;
8525
Thierry Fournier021d9862020-11-28 23:42:03 +01008526 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02008527 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008528
8529 /* Restore the function in the stack. */
8530 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
8531 hlua->nargs = 0;
8532
8533 /* Schedule task. */
Willy Tarreaue3957f82021-09-30 16:17:37 +02008534 task_wakeup(task, TASK_WOKEN_INIT);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008535
8536 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02008537
8538 alloc_error:
8539 task_destroy(task);
8540 hlua_ctx_destroy(hlua);
8541 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8542 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008543}
8544
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008545/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
8546 * doesn't allow "yield" functions because the HAProxy engine cannot
8547 * resume converters.
8548 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008549static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008550{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02008551 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008552 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008553 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008554
Willy Tarreaube508f12016-03-10 11:47:01 +01008555 if (!stream)
8556 return 0;
8557
Willy Tarreau87b09662015-04-03 00:22:06 +02008558 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01008559 * Lua context can be not initialized. This behavior
8560 * permits to save performances because a systematic
8561 * Lua initialization cause 5% performances loss.
8562 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008563 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008564 struct hlua *hlua;
8565
8566 hlua = pool_alloc(pool_head_hlua);
8567 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008568 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
8569 return 0;
8570 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008571 HLUA_INIT(hlua);
8572 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01008573 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008574 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
8575 return 0;
8576 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01008577 }
8578
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008579 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008580 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008581
8582 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008583 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008584 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
8585 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01008586 else
8587 error = "critical error";
8588 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008589 return 0;
8590 }
8591
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008592 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008593 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008594 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008595 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008596 return 0;
8597 }
8598
8599 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008600 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008601
8602 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008603 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008604 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008605 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008606 return 0;
8607 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008608 hlua_smp2lua(stream->hlua->T, smp);
8609 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008610
8611 /* push keywords in the stack. */
8612 if (arg_p) {
8613 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008614 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008615 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008616 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008617 return 0;
8618 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008619 hlua_arg2lua(stream->hlua->T, arg_p);
8620 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008621 }
8622 }
8623
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008624 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008625 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008626
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008627 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008628 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008629 }
8630
8631 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008632 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008633 /* finished. */
8634 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02008635 /* If the stack is empty, the function fails. */
8636 if (lua_gettop(stream->hlua->T) <= 0)
8637 return 0;
8638
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008639 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008640 hlua_lua2smp(stream->hlua->T, -1, smp);
8641 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008642 return 1;
8643
8644 /* yield. */
8645 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008646 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008647 return 0;
8648
8649 /* finished with error. */
8650 case HLUA_E_ERRMSG:
8651 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008652 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008653 fcn->name, lua_tostring(stream->hlua->T, -1));
8654 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008655 return 0;
8656
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008657 case HLUA_E_ETMOUT:
8658 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
8659 return 0;
8660
8661 case HLUA_E_NOMEM:
8662 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
8663 return 0;
8664
8665 case HLUA_E_YIELD:
8666 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
8667 return 0;
8668
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008669 case HLUA_E_ERR:
8670 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008671 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02008672 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008673
8674 default:
8675 return 0;
8676 }
8677}
8678
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008679/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
8680 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01008681 * resume sample-fetches. This function will be called by the sample
8682 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008683 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008684static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
8685 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008686{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02008687 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008688 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008689 const char *error;
Christopher Faulet8c9e6bb2021-08-06 16:29:41 +02008690 unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008691
Willy Tarreaube508f12016-03-10 11:47:01 +01008692 if (!stream)
8693 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01008694
Willy Tarreau87b09662015-04-03 00:22:06 +02008695 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01008696 * Lua context can be not initialized. This behavior
8697 * permits to save performances because a systematic
8698 * Lua initialization cause 5% performances loss.
8699 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008700 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008701 struct hlua *hlua;
8702
8703 hlua = pool_alloc(pool_head_hlua);
8704 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008705 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
8706 return 0;
8707 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01008708 hlua->T = NULL;
8709 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01008710 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008711 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
8712 return 0;
8713 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01008714 }
8715
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008716 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008717 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008718
8719 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008720 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008721 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
8722 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01008723 else
8724 error = "critical error";
8725 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008726 return 0;
8727 }
8728
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008729 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008730 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008731 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008732 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008733 return 0;
8734 }
8735
8736 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008737 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008738
8739 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02008740 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008741 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008742 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008743 return 0;
8744 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008745 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008746
8747 /* push keywords in the stack. */
8748 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
8749 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008750 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008751 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008752 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008753 return 0;
8754 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008755 hlua_arg2lua(stream->hlua->T, arg_p);
8756 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008757 }
8758
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008759 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008760 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008761
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008762 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008763 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008764 }
8765
8766 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008767 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008768 /* finished. */
8769 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02008770 /* If the stack is empty, the function fails. */
8771 if (lua_gettop(stream->hlua->T) <= 0)
8772 return 0;
8773
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008774 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008775 hlua_lua2smp(stream->hlua->T, -1, smp);
8776 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008777
8778 /* Set the end of execution flag. */
8779 smp->flags &= ~SMP_F_MAY_CHANGE;
8780 return 1;
8781
8782 /* yield. */
8783 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008784 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008785 return 0;
8786
8787 /* finished with error. */
8788 case HLUA_E_ERRMSG:
8789 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008790 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01008791 fcn->name, lua_tostring(stream->hlua->T, -1));
8792 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008793 return 0;
8794
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008795 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008796 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
8797 return 0;
8798
8799 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008800 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
8801 return 0;
8802
8803 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008804 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
8805 return 0;
8806
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008807 case HLUA_E_ERR:
8808 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02008809 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02008810 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008811
8812 default:
8813 return 0;
8814 }
8815}
8816
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008817/* This function is an LUA binding used for registering
8818 * "sample-conv" functions. It expects a converter name used
8819 * in the haproxy configuration file, and an LUA function.
8820 */
8821__LJMP static int hlua_register_converters(lua_State *L)
8822{
8823 struct sample_conv_kw_list *sck;
8824 const char *name;
8825 int ref;
8826 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02008827 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008828 struct sample_conv *sc;
8829 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008830
8831 MAY_LJMP(check_args(L, 2, "register_converters"));
8832
8833 /* First argument : converter name. */
8834 name = MAY_LJMP(luaL_checkstring(L, 1));
8835
8836 /* Second argument : lua function. */
8837 ref = MAY_LJMP(hlua_checkfunction(L, 2));
8838
Thierry Fournierf67442e2020-11-28 20:41:07 +01008839 /* Check if the converter is already registered */
8840 trash = get_trash_chunk();
8841 chunk_printf(trash, "lua.%s", name);
8842 sc = find_sample_conv(trash->area, trash->data);
8843 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008844 fcn = sc->private;
8845 if (fcn->function_ref[hlua_state_id] != -1) {
8846 ha_warning("Trying to register converter 'lua.%s' more than once. "
8847 "This will become a hard error in version 2.5.\n", name);
8848 }
8849 fcn->function_ref[hlua_state_id] = ref;
8850 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008851 }
8852
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008853 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008854 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008855 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02008856 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008857 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008858 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02008859 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008860
8861 /* Fill fcn. */
8862 fcn->name = strdup(name);
8863 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02008864 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01008865 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008866
8867 /* List head */
8868 sck->list.n = sck->list.p = NULL;
8869
8870 /* converter keyword. */
8871 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008872 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008873 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02008874 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008875
8876 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
8877 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01008878 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 +01008879 sck->kw[0].val_args = NULL;
8880 sck->kw[0].in_type = SMP_T_STR;
8881 sck->kw[0].out_type = SMP_T_STR;
8882 sck->kw[0].private = fcn;
8883
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008884 /* Register this new converter */
8885 sample_register_convs(sck);
8886
8887 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02008888
8889 alloc_error:
8890 release_hlua_function(fcn);
8891 ha_free(&sck);
8892 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8893 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008894}
8895
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008896/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008897 * "sample-fetch" functions. It expects a converter name used
8898 * in the haproxy configuration file, and an LUA function.
8899 */
8900__LJMP static int hlua_register_fetches(lua_State *L)
8901{
8902 const char *name;
8903 int ref;
8904 int len;
8905 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02008906 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008907 struct sample_fetch *sf;
8908 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008909
8910 MAY_LJMP(check_args(L, 2, "register_fetches"));
8911
8912 /* First argument : sample-fetch name. */
8913 name = MAY_LJMP(luaL_checkstring(L, 1));
8914
8915 /* Second argument : lua function. */
8916 ref = MAY_LJMP(hlua_checkfunction(L, 2));
8917
Thierry Fournierf67442e2020-11-28 20:41:07 +01008918 /* Check if the sample-fetch is already registered */
8919 trash = get_trash_chunk();
8920 chunk_printf(trash, "lua.%s", name);
8921 sf = find_sample_fetch(trash->area, trash->data);
8922 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008923 fcn = sf->private;
8924 if (fcn->function_ref[hlua_state_id] != -1) {
8925 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
8926 "This will become a hard error in version 2.5.\n", name);
8927 }
8928 fcn->function_ref[hlua_state_id] = ref;
8929 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008930 }
8931
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008932 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008933 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008934 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02008935 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008936 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008937 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02008938 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008939
8940 /* Fill fcn. */
8941 fcn->name = strdup(name);
8942 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02008943 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01008944 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008945
8946 /* List head */
8947 sfk->list.n = sfk->list.p = NULL;
8948
8949 /* sample-fetch keyword. */
8950 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008951 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008952 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02008953 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008954
8955 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
8956 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01008957 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 +01008958 sfk->kw[0].val_args = NULL;
8959 sfk->kw[0].out_type = SMP_T_STR;
8960 sfk->kw[0].use = SMP_USE_HTTP_ANY;
8961 sfk->kw[0].val = 0;
8962 sfk->kw[0].private = fcn;
8963
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008964 /* Register this new fetch. */
8965 sample_register_fetches(sfk);
8966
8967 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02008968
8969 alloc_error:
8970 release_hlua_function(fcn);
8971 ha_free(&sfk);
8972 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8973 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008974}
8975
Christopher Faulet501465d2020-02-26 14:54:16 +01008976/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008977 */
Christopher Faulet501465d2020-02-26 14:54:16 +01008978__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008979{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008980 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008981 unsigned int delay;
8982 unsigned int wakeup_ms;
8983
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008984 /* Get hlua struct, or NULL if we execute from main lua state */
8985 hlua = hlua_gethlua(L);
8986 if (!hlua) {
8987 return 0;
8988 }
8989
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008990 MAY_LJMP(check_args(L, 1, "wake_time"));
8991
8992 delay = MAY_LJMP(luaL_checkinteger(L, 1));
8993 wakeup_ms = tick_add(now_ms, delay);
8994 hlua->wake_time = wakeup_ms;
8995 return 0;
8996}
8997
Christopher Faulet7716cdf2020-01-29 11:53:30 +01008998/* This function is a wrapper to execute each LUA function declared as an action
8999 * wrapper during the initialisation period. This function may return any
9000 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
9001 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
9002 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009003 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009004static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02009005 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009006{
9007 char **arg;
Christopher Faulet8c9e6bb2021-08-06 16:29:41 +02009008 unsigned int hflags = HLUA_TXN_ACT_CTX;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009009 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01009010 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009011
9012 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01009013 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
9014 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
9015 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
9016 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009017 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009018 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009019 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009020 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009021
Willy Tarreau87b09662015-04-03 00:22:06 +02009022 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01009023 * Lua context can be not initialized. This behavior
9024 * permits to save performances because a systematic
9025 * Lua initialization cause 5% performances loss.
9026 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009027 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01009028 struct hlua *hlua;
9029
9030 hlua = pool_alloc(pool_head_hlua);
9031 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009032 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009033 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009034 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009035 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01009036 HLUA_INIT(hlua);
9037 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01009038 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 +01009039 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009040 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009041 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009042 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01009043 }
9044
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009045 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009046 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009047
9048 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009049 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009050 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
9051 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01009052 else
9053 error = "critical error";
9054 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009055 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009056 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009057 }
9058
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009059 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009060 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009061 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009062 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009063 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009064 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009065 }
9066
9067 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01009068 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 +01009069
Willy Tarreau87b09662015-04-03 00:22:06 +02009070 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02009071 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009072 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009073 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009074 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009075 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009076 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009077 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009078
9079 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009080 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009081 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009082 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009083 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009084 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009085 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009086 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009087 lua_pushstring(s->hlua->T, *arg);
9088 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009089 }
9090
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009091 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009092 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009093
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009094 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009095 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009096 }
9097
9098 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01009099 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009100 /* finished. */
9101 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009102 /* Catch the return value */
9103 if (lua_gettop(s->hlua->T) > 0)
9104 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009105
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01009106 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02009107 if (act_ret == ACT_RET_YIELD) {
9108 if (flags & ACT_OPT_FINAL)
9109 goto err_yield;
9110
Christopher Faulet8f587ea2020-07-28 12:01:55 +02009111 if (dir == SMP_OPT_DIR_REQ)
9112 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
9113 s->hlua->wake_time);
9114 else
9115 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
9116 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01009117 }
9118 goto end;
9119
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009120 /* yield. */
9121 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01009122 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02009123 if (dir == SMP_OPT_DIR_REQ)
9124 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
9125 s->hlua->wake_time);
9126 else
9127 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
9128 s->hlua->wake_time);
9129
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009130 /* Some actions can be wake up when a "write" event
9131 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009132 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009133 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02009134 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009135 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009136 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009137 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009138 act_ret = ACT_RET_YIELD;
9139 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009140
9141 /* finished with error. */
9142 case HLUA_E_ERRMSG:
9143 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009144 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009145 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01009146 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009147 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009148
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009149 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01009150 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009151 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009152
9153 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01009154 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009155 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009156
9157 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02009158 err_yield:
9159 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009160 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009161 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009162 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009163
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009164 case HLUA_E_ERR:
9165 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02009166 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009167 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009168
9169 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009170 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009171 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009172
9173 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02009174 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02009175 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01009176 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009177}
9178
Willy Tarreau144f84a2021-03-02 16:09:26 +01009179struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009180{
Olivier Houchard9f6af332018-05-25 14:04:04 +02009181 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009182
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009183 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02009184 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02009185 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009186}
9187
9188static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
9189{
Christopher Faulet86e1c332021-12-20 17:09:39 +01009190 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009191 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009192 struct task *task;
9193 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01009194 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009195
Willy Tarreaubafbe012017-11-24 17:34:44 +01009196 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009197 if (!hlua) {
9198 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009199 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009200 return 0;
9201 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009202 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009203 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009204 ctx->ctx.hlua_apptcp.flags = 0;
9205
9206 /* Create task used by signal to wakeup applets. */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02009207 task = task_new_here();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009208 if (!task) {
9209 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009210 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009211 return 0;
9212 }
9213 task->nice = 0;
9214 task->context = ctx;
9215 task->process = hlua_applet_wakeup;
9216 ctx->ctx.hlua_apptcp.task = task;
9217
9218 /* In the execution wrappers linked with a stream, the
9219 * Lua context can be not initialized. This behavior
9220 * permits to save performances because a systematic
9221 * Lua initialization cause 5% performances loss.
9222 */
Thierry Fournierc7492592020-11-28 23:57:24 +01009223 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 +02009224 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009225 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009226 return 0;
9227 }
9228
9229 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02009230 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009231
9232 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009233 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01009234 if (lua_type(hlua->T, -1) == LUA_TSTRING)
9235 error = lua_tostring(hlua->T, -1);
9236 else
9237 error = "critical error";
9238 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009239 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009240 return 0;
9241 }
9242
9243 /* Check stack available size. */
9244 if (!lua_checkstack(hlua->T, 1)) {
9245 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009246 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009247 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009248 return 0;
9249 }
9250
9251 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01009252 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009253
9254 /* Create and and push object stream in the stack. */
9255 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
9256 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009257 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009258 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009259 return 0;
9260 }
9261 hlua->nargs = 1;
9262
9263 /* push keywords in the stack. */
9264 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
9265 if (!lua_checkstack(hlua->T, 1)) {
9266 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009267 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009268 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009269 return 0;
9270 }
9271 lua_pushstring(hlua->T, *arg);
9272 hlua->nargs++;
9273 }
9274
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009275 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009276
9277 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01009278 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01009279 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009280
9281 return 1;
9282}
9283
Willy Tarreau60409db2019-08-21 14:14:50 +02009284void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009285{
Christopher Faulet86e1c332021-12-20 17:09:39 +01009286 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009287 struct stream *strm = si_strm(si);
9288 struct channel *res = si_ic(si);
9289 struct act_rule *rule = ctx->rule;
9290 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009291 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009292
9293 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02009294 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
9295 /* eat the whole request */
9296 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009297 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02009298 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009299
9300 /* If the stream is disconnect or closed, ldo nothing. */
9301 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
9302 return;
9303
9304 /* Execute the function. */
9305 switch (hlua_ctx_resume(hlua, 1)) {
9306 /* finished. */
9307 case HLUA_E_OK:
9308 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
9309
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009310 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02009311 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009312 res->flags |= CF_READ_NULL;
9313 si_shutr(si);
9314 return;
9315
9316 /* yield. */
9317 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01009318 if (hlua->wake_time != TICK_ETERNITY)
9319 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009320 return;
9321
9322 /* finished with error. */
9323 case HLUA_E_ERRMSG:
9324 /* Display log. */
9325 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009326 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009327 lua_pop(hlua->T, 1);
9328 goto error;
9329
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009330 case HLUA_E_ETMOUT:
9331 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009332 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009333 goto error;
9334
9335 case HLUA_E_NOMEM:
9336 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009337 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009338 goto error;
9339
9340 case HLUA_E_YIELD: /* unexpected */
9341 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009342 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02009343 goto error;
9344
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009345 case HLUA_E_ERR:
9346 /* Display log. */
9347 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009348 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009349 goto error;
9350
9351 default:
9352 goto error;
9353 }
9354
9355error:
9356
9357 /* For all other cases, just close the stream. */
9358 si_shutw(si);
9359 si_shutr(si);
9360 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
9361}
9362
9363static void hlua_applet_tcp_release(struct appctx *ctx)
9364{
Olivier Houchard3f795f72019-04-17 22:51:06 +02009365 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009366 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009367 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009368 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009369}
9370
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009371/* The function returns 1 if the initialisation is complete, 0 if
9372 * an errors occurs and -1 if more data are required for initializing
9373 * the applet.
9374 */
9375static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
9376{
Christopher Faulet86e1c332021-12-20 17:09:39 +01009377 struct stream_interface *si = cs_si(ctx->owner);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009378 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009379 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009380 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009381 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01009382 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009383
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009384 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01009385 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009386 if (!hlua) {
9387 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009388 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009389 return 0;
9390 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009391 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009392 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009393 ctx->ctx.hlua_apphttp.left_bytes = -1;
9394 ctx->ctx.hlua_apphttp.flags = 0;
9395
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01009396 if (txn->req.flags & HTTP_MSGF_VER_11)
9397 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
9398
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009399 /* Create task used by signal to wakeup applets. */
Willy Tarreaubeeabf52021-10-01 18:23:30 +02009400 task = task_new_here();
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009401 if (!task) {
9402 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009403 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009404 return 0;
9405 }
9406 task->nice = 0;
9407 task->context = ctx;
9408 task->process = hlua_applet_wakeup;
9409 ctx->ctx.hlua_apphttp.task = task;
9410
9411 /* In the execution wrappers linked with a stream, the
9412 * Lua context can be not initialized. This behavior
9413 * permits to save performances because a systematic
9414 * Lua initialization cause 5% performances loss.
9415 */
Thierry Fournierc7492592020-11-28 23:57:24 +01009416 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 +02009417 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009418 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009419 return 0;
9420 }
9421
9422 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02009423 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009424
9425 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009426 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01009427 if (lua_type(hlua->T, -1) == LUA_TSTRING)
9428 error = lua_tostring(hlua->T, -1);
9429 else
9430 error = "critical error";
9431 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009432 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009433 return 0;
9434 }
9435
9436 /* Check stack available size. */
9437 if (!lua_checkstack(hlua->T, 1)) {
9438 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009439 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009440 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009441 return 0;
9442 }
9443
9444 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01009445 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009446
9447 /* Create and and push object stream in the stack. */
9448 if (!hlua_applet_http_new(hlua->T, ctx)) {
9449 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009450 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009451 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009452 return 0;
9453 }
9454 hlua->nargs = 1;
9455
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009456 /* push keywords in the stack. */
9457 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
9458 if (!lua_checkstack(hlua->T, 1)) {
9459 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009460 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009461 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009462 return 0;
9463 }
9464 lua_pushstring(hlua->T, *arg);
9465 hlua->nargs++;
9466 }
9467
Thierry Fournier7cbe5042020-11-28 17:02:21 +01009468 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009469
9470 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01009471 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009472
9473 return 1;
9474}
9475
Willy Tarreau60409db2019-08-21 14:14:50 +02009476void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009477{
Christopher Faulet86e1c332021-12-20 17:09:39 +01009478 struct stream_interface *si = cs_si(ctx->owner);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009479 struct stream *strm = si_strm(si);
9480 struct channel *req = si_oc(si);
9481 struct channel *res = si_ic(si);
9482 struct act_rule *rule = ctx->rule;
9483 struct proxy *px = strm->be;
9484 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
9485 struct htx *req_htx, *res_htx;
9486
9487 res_htx = htx_from_buf(&res->buf);
9488
9489 /* If the stream is disconnect or closed, ldo nothing. */
9490 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
9491 goto out;
9492
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009493 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009494 if (!b_size(&res->buf)) {
9495 si_rx_room_blk(si);
9496 goto out;
9497 }
9498 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009499 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009500 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
9501
9502 /* Set the currently running flag. */
9503 if (!HLUA_IS_RUNNING(hlua) &&
9504 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02009505 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009506 si_cant_get(si);
9507 goto out;
9508 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009509 }
9510
9511 /* Executes The applet if it is not done. */
9512 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
9513
9514 /* Execute the function. */
9515 switch (hlua_ctx_resume(hlua, 1)) {
9516 /* finished. */
9517 case HLUA_E_OK:
9518 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
9519 break;
9520
9521 /* yield. */
9522 case HLUA_E_AGAIN:
9523 if (hlua->wake_time != TICK_ETERNITY)
9524 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009525 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009526
9527 /* finished with error. */
9528 case HLUA_E_ERRMSG:
9529 /* Display log. */
9530 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009531 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009532 lua_pop(hlua->T, 1);
9533 goto error;
9534
9535 case HLUA_E_ETMOUT:
9536 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009537 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009538 goto error;
9539
9540 case HLUA_E_NOMEM:
9541 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009542 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009543 goto error;
9544
9545 case HLUA_E_YIELD: /* unexpected */
9546 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009547 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009548 goto error;
9549
9550 case HLUA_E_ERR:
9551 /* Display log. */
9552 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01009553 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009554 goto error;
9555
9556 default:
9557 goto error;
9558 }
9559 }
9560
9561 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009562 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
9563 goto done;
9564
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009565 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
9566 goto error;
9567
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01009568 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
9569 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009570 strm->txn->status = ctx->ctx.hlua_apphttp.status;
9571 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009572 }
9573
9574 done:
9575 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009576 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009577 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01009578 si_shutr(si);
9579 }
9580
9581 /* eat the whole request */
9582 if (co_data(req)) {
9583 req_htx = htx_from_buf(&req->buf);
9584 co_htx_skip(req, req_htx, co_data(req));
9585 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009586 }
9587 }
9588
9589 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009590 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009591 return;
9592
9593 error:
9594
9595 /* If we are in HTTP mode, and we are not send any
9596 * data, return a 500 server error in best effort:
9597 * if there is no room available in the buffer,
9598 * just close the connection.
9599 */
9600 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02009601 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009602
9603 channel_erase(res);
9604 res->buf.data = b_data(err);
9605 memcpy(res->buf.area, b_head(err), b_data(err));
9606 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01009607 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01009608 }
9609 if (!(strm->flags & SF_ERR_MASK))
9610 strm->flags |= SF_ERR_RESOURCE;
9611 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
9612 goto done;
9613}
9614
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009615static void hlua_applet_http_release(struct appctx *ctx)
9616{
Olivier Houchard3f795f72019-04-17 22:51:06 +02009617 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009618 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009619 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01009620 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009621}
9622
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009623/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009624 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009625 *
9626 * This function can fail with an abort() due to an Lua critical error.
9627 * We are in the configuration parsing process of HAProxy, this abort() is
9628 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009629 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009630static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
9631 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009632{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009633 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009634 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009635
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009636 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02009637 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009638 if (!rule->arg.hlua_rule) {
9639 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02009640 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009641 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009642
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009643 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02009644 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
9645 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009646 if (!rule->arg.hlua_rule->args) {
9647 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02009648 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009649 }
9650
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009651 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01009652 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009653
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009654 /* Expect some arguments */
9655 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01009656 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009657 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02009658 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009659 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01009660 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009661 if (!rule->arg.hlua_rule->args[i]) {
9662 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02009663 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009664 }
9665 (*cur_arg)++;
9666 }
9667 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009668
Thierry FOURNIER42148732015-09-02 17:17:33 +02009669 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02009670 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009671 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02009672
9673 error:
9674 if (rule->arg.hlua_rule) {
9675 if (rule->arg.hlua_rule->args) {
9676 for (i = 0; i < fcn->nargs; i++)
9677 ha_free(&rule->arg.hlua_rule->args[i]);
9678 ha_free(&rule->arg.hlua_rule->args);
9679 }
9680 ha_free(&rule->arg.hlua_rule);
9681 }
9682 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01009683}
9684
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009685static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
9686 struct act_rule *rule, char **err)
9687{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009688 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009689
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01009690 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009691 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01009692 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009693 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01009694 * the call of this analyzer.
9695 */
9696 if (rule->from != ACT_F_HTTP_REQ) {
9697 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
9698 return ACT_RET_PRS_ERR;
9699 }
9700
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009701 /* Memory for the rule. */
9702 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
9703 if (!rule->arg.hlua_rule) {
9704 memprintf(err, "out of memory error");
9705 return ACT_RET_PRS_ERR;
9706 }
9707
9708 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01009709 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009710
9711 /* TODO: later accept arguments. */
9712 rule->arg.hlua_rule->args = NULL;
9713
9714 /* Add applet pointer in the rule. */
9715 rule->applet.obj_type = OBJ_TYPE_APPLET;
9716 rule->applet.name = fcn->name;
9717 rule->applet.init = hlua_applet_http_init;
9718 rule->applet.fct = hlua_applet_http_fct;
9719 rule->applet.release = hlua_applet_http_release;
9720 rule->applet.timeout = hlua_timeout_applet;
9721
9722 return ACT_RET_PRS_OK;
9723}
9724
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009725/* This function is an LUA binding used for registering
9726 * "sample-conv" functions. It expects a converter name used
9727 * in the haproxy configuration file, and an LUA function.
9728 */
9729__LJMP static int hlua_register_action(lua_State *L)
9730{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009731 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009732 const char *name;
9733 int ref;
9734 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009735 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009736 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01009737 struct buffer *trash;
9738 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009739
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009740 /* Initialise the number of expected arguments at 0. */
9741 nargs = 0;
9742
9743 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
9744 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009745
9746 /* First argument : converter name. */
9747 name = MAY_LJMP(luaL_checkstring(L, 1));
9748
9749 /* Second argument : environment. */
9750 if (lua_type(L, 2) != LUA_TTABLE)
9751 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
9752
9753 /* Third argument : lua function. */
9754 ref = MAY_LJMP(hlua_checkfunction(L, 3));
9755
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009756 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009757 if (lua_gettop(L) >= 4)
9758 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
9759
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009760 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009761 lua_pushnil(L);
9762 while (lua_next(L, 2) != 0) {
9763 if (lua_type(L, -1) != LUA_TSTRING)
9764 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
9765
Thierry Fournierf67442e2020-11-28 20:41:07 +01009766 /* Check if action exists */
9767 trash = get_trash_chunk();
9768 chunk_printf(trash, "lua.%s", name);
9769 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
9770 akw = tcp_req_cont_action(trash->area);
9771 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
9772 akw = tcp_res_cont_action(trash->area);
9773 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
9774 akw = action_http_req_custom(trash->area);
9775 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
9776 akw = action_http_res_custom(trash->area);
9777 } else {
9778 akw = NULL;
9779 }
9780 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01009781 fcn = akw->private;
9782 if (fcn->function_ref[hlua_state_id] != -1) {
9783 ha_warning("Trying to register action 'lua.%s' more than once. "
9784 "This will become a hard error in version 2.5.\n", name);
9785 }
9786 fcn->function_ref[hlua_state_id] = ref;
9787
9788 /* pop the environment string. */
9789 lua_pop(L, 1);
9790 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01009791 }
9792
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009793 /* Check required environment. Only accepted "http" or "tcp". */
9794 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02009795 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009796 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009797 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01009798 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009799 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009800 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009801
9802 /* Fill fcn. */
9803 fcn->name = strdup(name);
9804 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009805 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01009806 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009807
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07009808 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01009809 fcn->nargs = nargs;
9810
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009811 /* List head */
9812 akl->list.n = akl->list.p = NULL;
9813
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009814 /* action keyword. */
9815 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02009816 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009817 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009818 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009819
9820 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
9821
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02009822 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009823 akl->kw[0].private = fcn;
9824 akl->kw[0].parse = action_register_lua;
9825
9826 /* select the action registering point. */
9827 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
9828 tcp_req_cont_keywords_register(akl);
9829 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
9830 tcp_res_cont_keywords_register(akl);
9831 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
9832 http_req_keywords_register(akl);
9833 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
9834 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009835 else {
9836 release_hlua_function(fcn);
9837 if (akl)
9838 ha_free((char **)&(akl->kw[0].kw));
9839 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01009840 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009841 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
9842 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009843 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009844
9845 /* pop the environment string. */
9846 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009847
9848 /* reset for next loop */
9849 akl = NULL;
9850 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009851 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009852 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02009853
9854 alloc_error:
9855 release_hlua_function(fcn);
9856 ha_free(&akl);
9857 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
9858 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009859}
9860
9861static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
9862 struct act_rule *rule, char **err)
9863{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009864 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009865
Christopher Faulet280f85b2019-07-15 15:02:04 +02009866 if (px->mode == PR_MODE_HTTP) {
9867 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01009868 return ACT_RET_PRS_ERR;
9869 }
9870
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009871 /* Memory for the rule. */
9872 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
9873 if (!rule->arg.hlua_rule) {
9874 memprintf(err, "out of memory error");
9875 return ACT_RET_PRS_ERR;
9876 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009877
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009878 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01009879 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009880
9881 /* TODO: later accept arguments. */
9882 rule->arg.hlua_rule->args = NULL;
9883
9884 /* Add applet pointer in the rule. */
9885 rule->applet.obj_type = OBJ_TYPE_APPLET;
9886 rule->applet.name = fcn->name;
9887 rule->applet.init = hlua_applet_tcp_init;
9888 rule->applet.fct = hlua_applet_tcp_fct;
9889 rule->applet.release = hlua_applet_tcp_release;
9890 rule->applet.timeout = hlua_timeout_applet;
9891
9892 return 0;
9893}
9894
9895/* This function is an LUA binding used for registering
9896 * "sample-conv" functions. It expects a converter name used
9897 * in the haproxy configuration file, and an LUA function.
9898 */
9899__LJMP static int hlua_register_service(lua_State *L)
9900{
9901 struct action_kw_list *akl;
9902 const char *name;
9903 const char *env;
9904 int ref;
9905 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02009906 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01009907 struct buffer *trash;
9908 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009909
9910 MAY_LJMP(check_args(L, 3, "register_service"));
9911
9912 /* First argument : converter name. */
9913 name = MAY_LJMP(luaL_checkstring(L, 1));
9914
9915 /* Second argument : environment. */
9916 env = MAY_LJMP(luaL_checkstring(L, 2));
9917
9918 /* Third argument : lua function. */
9919 ref = MAY_LJMP(hlua_checkfunction(L, 3));
9920
Thierry Fournierf67442e2020-11-28 20:41:07 +01009921 /* Check for service already registered */
9922 trash = get_trash_chunk();
9923 chunk_printf(trash, "lua.%s", name);
9924 akw = service_find(trash->area);
9925 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01009926 fcn = akw->private;
9927 if (fcn->function_ref[hlua_state_id] != -1) {
9928 ha_warning("Trying to register service 'lua.%s' more than once. "
9929 "This will become a hard error in version 2.5.\n", name);
9930 }
9931 fcn->function_ref[hlua_state_id] = ref;
9932 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01009933 }
9934
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009935 /* Allocate and fill the sample fetch keyword struct. */
9936 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
9937 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02009938 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01009939 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009940 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02009941 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009942
9943 /* Fill fcn. */
9944 len = strlen("<lua.>") + strlen(name) + 1;
9945 fcn->name = calloc(1, len);
9946 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02009947 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009948 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01009949 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009950
9951 /* List head */
9952 akl->list.n = akl->list.p = NULL;
9953
9954 /* converter keyword. */
9955 len = strlen("lua.") + strlen(name) + 1;
9956 akl->kw[0].kw = calloc(1, len);
9957 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02009958 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009959
9960 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
9961
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01009962 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009963 if (strcmp(env, "tcp") == 0)
9964 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009965 else if (strcmp(env, "http") == 0)
9966 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02009967 else {
9968 release_hlua_function(fcn);
9969 if (akl)
9970 ha_free((char **)&(akl->kw[0].kw));
9971 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01009972 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01009973 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02009974 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009975
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02009976 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009977 akl->kw[0].private = fcn;
9978
9979 /* End of array. */
9980 memset(&akl->kw[1], 0, sizeof(*akl->kw));
9981
9982 /* Register this new converter */
9983 service_keywords_register(akl);
9984
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009985 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02009986
9987 alloc_error:
9988 release_hlua_function(fcn);
9989 ha_free(&akl);
9990 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
9991 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02009992}
9993
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01009994/* This function initialises Lua cli handler. It copies the
9995 * arguments in the Lua stack and create channel IO objects.
9996 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009997static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01009998{
9999 struct hlua *hlua;
10000 struct hlua_function *fcn;
10001 int i;
10002 const char *error;
10003
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010004 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010005 appctx->ctx.hlua_cli.fcn = private;
10006
Willy Tarreaubafbe012017-11-24 17:34:44 +010010007 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010008 if (!hlua) {
10009 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +010010010 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010011 }
10012 HLUA_INIT(hlua);
10013 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010014
10015 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +050010016 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010017 * applet_http. It is absolutely compatible.
10018 */
Willy Tarreaubeeabf52021-10-01 18:23:30 +020010019 appctx->ctx.hlua_cli.task = task_new_here();
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010020 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +010010021 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +010010022 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010023 }
10024 appctx->ctx.hlua_cli.task->nice = 0;
10025 appctx->ctx.hlua_cli.task->context = appctx;
10026 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
10027
10028 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +010010029 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 +010010030 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +010010031 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010032 }
10033
10034 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +010010035 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010036 if (lua_type(hlua->T, -1) == LUA_TSTRING)
10037 error = lua_tostring(hlua->T, -1);
10038 else
10039 error = "critical error";
10040 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
10041 goto error;
10042 }
10043
10044 /* Check stack available size. */
10045 if (!lua_checkstack(hlua->T, 2)) {
10046 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
10047 goto error;
10048 }
10049
10050 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +010010051 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010052
10053 /* Once the arguments parsed, the CLI is like an AppletTCP,
10054 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010055 */
10056 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
10057 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
10058 goto error;
10059 }
10060 hlua->nargs = 1;
10061
10062 /* push keywords in the stack. */
10063 for (i = 0; *args[i]; i++) {
10064 /* Check stack available size. */
10065 if (!lua_checkstack(hlua->T, 1)) {
10066 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
10067 goto error;
10068 }
10069 lua_pushstring(hlua->T, args[i]);
10070 hlua->nargs++;
10071 }
10072
10073 /* We must initialize the execution timeouts. */
10074 hlua->max_time = hlua_timeout_session;
10075
10076 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +010010077 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010078
10079 /* It's ok */
10080 return 0;
10081
10082 /* It's not ok. */
10083error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +010010084 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010085 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +010010086 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010087 return 1;
10088}
10089
10090static int hlua_cli_io_handler_fct(struct appctx *appctx)
10091{
10092 struct hlua *hlua;
10093 struct stream_interface *si;
10094 struct hlua_function *fcn;
10095
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010096 hlua = appctx->ctx.hlua_cli.hlua;
Christopher Faulet86e1c332021-12-20 17:09:39 +010010097 si = cs_si(appctx->owner);
Willy Tarreau8ae4f752016-12-14 15:41:45 +010010098 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010099
10100 /* If the stream is disconnect or closed, ldo nothing. */
10101 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
10102 return 1;
10103
10104 /* Execute the function. */
10105 switch (hlua_ctx_resume(hlua, 1)) {
10106
10107 /* finished. */
10108 case HLUA_E_OK:
10109 return 1;
10110
10111 /* yield. */
10112 case HLUA_E_AGAIN:
10113 /* We want write. */
10114 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +010010115 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010116 /* Set the timeout. */
10117 if (hlua->wake_time != TICK_ETERNITY)
10118 task_schedule(hlua->task, hlua->wake_time);
10119 return 0;
10120
10121 /* finished with error. */
10122 case HLUA_E_ERRMSG:
10123 /* Display log. */
10124 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
10125 fcn->name, lua_tostring(hlua->T, -1));
10126 lua_pop(hlua->T, 1);
10127 return 1;
10128
Thierry Fournierd5b073c2018-05-21 19:42:47 +020010129 case HLUA_E_ETMOUT:
10130 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
10131 fcn->name);
10132 return 1;
10133
10134 case HLUA_E_NOMEM:
10135 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
10136 fcn->name);
10137 return 1;
10138
10139 case HLUA_E_YIELD: /* unexpected */
10140 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
10141 fcn->name);
10142 return 1;
10143
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010144 case HLUA_E_ERR:
10145 /* Display log. */
10146 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
10147 fcn->name);
10148 return 1;
10149
10150 default:
10151 return 1;
10152 }
10153
10154 return 1;
10155}
10156
10157static void hlua_cli_io_release_fct(struct appctx *appctx)
10158{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010159 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +010010160 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010161}
10162
10163/* This function is an LUA binding used for registering
10164 * new keywords in the cli. It expects a list of keywords
10165 * which are the "path". It is limited to 5 keywords. A
10166 * description of the command, a function to be executed
10167 * for the parsing and a function for io handlers.
10168 */
10169__LJMP static int hlua_register_cli(lua_State *L)
10170{
10171 struct cli_kw_list *cli_kws;
10172 const char *message;
10173 int ref_io;
10174 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010175 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010176 int index;
10177 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +010010178 struct buffer *trash;
10179 const char *kw[5];
10180 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010181 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010182
10183 MAY_LJMP(check_args(L, 3, "register_cli"));
10184
10185 /* First argument : an array of maximum 5 keywords. */
10186 if (!lua_istable(L, 1))
10187 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
10188
10189 /* Second argument : string with contextual message. */
10190 message = MAY_LJMP(luaL_checkstring(L, 2));
10191
10192 /* Third and fourth argument : lua function. */
10193 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
10194
Thierry Fournierf67442e2020-11-28 20:41:07 +010010195 /* Check for CLI service already registered */
10196 trash = get_trash_chunk();
10197 index = 0;
10198 lua_pushnil(L);
10199 memset(kw, 0, sizeof(kw));
10200 while (lua_next(L, 1) != 0) {
10201 if (index >= CLI_PREFIX_KW_NB)
10202 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
10203 if (lua_type(L, -1) != LUA_TSTRING)
10204 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
10205 kw[index] = lua_tostring(L, -1);
10206 if (index == 0)
10207 chunk_printf(trash, "%s", kw[index]);
10208 else
10209 chunk_appendf(trash, " %s", kw[index]);
10210 index++;
10211 lua_pop(L, 1);
10212 }
10213 cli_kw = cli_find_kw_exact((char **)kw);
10214 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +010010215 fcn = cli_kw->private;
10216 if (fcn->function_ref[hlua_state_id] != -1) {
10217 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
10218 "This will become a hard error in version 2.5.\n", trash->area);
10219 }
10220 fcn->function_ref[hlua_state_id] = ref_io;
10221 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +010010222 }
10223
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010224 /* Allocate and fill the sample fetch keyword struct. */
10225 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010226 if (!cli_kws) {
10227 errmsg = "Lua out of memory error.";
10228 goto error;
10229 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +010010230 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010231 if (!fcn) {
10232 errmsg = "Lua out of memory error.";
10233 goto error;
10234 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010235
10236 /* Fill path. */
10237 index = 0;
10238 lua_pushnil(L);
10239 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010240 if (index >= 5) {
10241 errmsg = "1st argument must be a table with a maximum of 5 entries";
10242 goto error;
10243 }
10244 if (lua_type(L, -1) != LUA_TSTRING) {
10245 errmsg = "1st argument must be a table filled with strings";
10246 goto error;
10247 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010248 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010249 if (!cli_kws->kw[0].str_kw[index]) {
10250 errmsg = "Lua out of memory error.";
10251 goto error;
10252 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010253 index++;
10254 lua_pop(L, 1);
10255 }
10256
10257 /* Copy help message. */
10258 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010259 if (!cli_kws->kw[0].usage) {
10260 errmsg = "Lua out of memory error.";
10261 goto error;
10262 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010263
10264 /* Fill fcn io handler. */
10265 len = strlen("<lua.cli>") + 1;
10266 for (i = 0; i < index; i++)
10267 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
10268 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010269 if (!fcn->name) {
10270 errmsg = "Lua out of memory error.";
10271 goto error;
10272 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010273 strncat((char *)fcn->name, "<lua.cli", len);
10274 for (i = 0; i < index; i++) {
10275 strncat((char *)fcn->name, ".", len);
10276 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
10277 }
10278 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +010010279 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010280
10281 /* Fill last entries. */
10282 cli_kws->kw[0].private = fcn;
10283 cli_kws->kw[0].parse = hlua_cli_parse_fct;
10284 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
10285 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
10286
10287 /* Register this new converter */
10288 cli_register_kw(cli_kws);
10289
10290 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +020010291
10292 error:
10293 release_hlua_function(fcn);
10294 if (cli_kws) {
10295 for (i = 0; i < index; i++)
10296 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
10297 ha_free((char **)&(cli_kws->kw[0].usage));
10298 }
10299 ha_free(&cli_kws);
10300 WILL_LJMP(luaL_error(L, errmsg));
10301 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +010010302}
10303
Christopher Faulet69c581a2021-05-31 08:54:04 +020010304static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf)
10305{
10306 struct hlua_flt_config *conf = fconf->conf;
10307 lua_State *L;
10308 int error, pos, state_id, flt_ref;
10309
10310 state_id = reg_flt_to_stack_id(conf->reg);
10311 L = hlua_states[state_id];
10312 pos = lua_gettop(L);
10313
10314 /* The filter parsing function */
10315 lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]);
10316
10317 /* Push the filter class on the stack and resolve all callbacks */
10318 lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]);
10319
10320 /* Duplicate the filter class so each filter will have its own copy */
10321 lua_newtable(L);
10322 lua_pushnil(L);
10323
10324 while (lua_next(L, pos+2)) {
10325 lua_pushvalue(L, -2);
10326 lua_insert(L, -2);
10327 lua_settable(L, -4);
10328 }
10329 flt_ref = luaL_ref(L, LUA_REGISTRYINDEX);
10330
10331 /* Remove the original lua filter class from the stack */
10332 lua_pop(L, 1);
10333
10334 /* Push the copy on the stack */
10335 lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref);
10336
10337 /* extra args are pushed in a table */
10338 lua_newtable(L);
10339 for (pos = 0; conf->args[pos]; pos++) {
10340 /* Check stack available size. */
10341 if (!lua_checkstack(L, 1)) {
10342 ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name);
10343 goto error;
10344 }
10345 lua_pushstring(L, conf->args[pos]);
10346 lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
10347 }
10348
10349 error = lua_pcall(L, 2, LUA_MULTRET, 0);
10350 switch (error) {
10351 case LUA_OK:
10352 /* replace the filter ref */
10353 conf->ref[state_id] = flt_ref;
10354 break;
10355 case LUA_ERRRUN:
10356 ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1));
10357 goto error;
10358 case LUA_ERRMEM:
10359 ha_alert("Lua filter '%s' : out of memory error", conf->reg->name);
10360 goto error;
10361 case LUA_ERRERR:
10362 ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1));
10363 goto error;
10364#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
10365 case LUA_ERRGCMM:
10366 ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1));
10367 goto error;
10368#endif
10369 default:
Ilya Shipitsinff0f2782021-08-22 22:18:07 +050010370 ha_alert("Lua filter '%s' : unknown error : %s", conf->reg->name, lua_tostring(L, -1));
Christopher Faulet69c581a2021-05-31 08:54:04 +020010371 goto error;
10372 }
10373
10374 lua_settop(L, 0);
10375 return 0;
10376
10377 error:
10378 lua_settop(L, 0);
10379 return -1;
10380}
10381
10382static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
10383{
10384 struct hlua_flt_config *conf = fconf->conf;
10385 lua_State *L;
10386 int state_id;
10387
10388 if (!conf)
10389 return;
10390
10391 state_id = reg_flt_to_stack_id(conf->reg);
10392 L = hlua_states[state_id];
10393 luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]);
10394}
10395
10396static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf)
10397{
10398 struct hlua_flt_config *conf = fconf->conf;
10399 int state_id = reg_flt_to_stack_id(conf->reg);
10400
10401 /* Rely on per-thread init for global scripts */
10402 if (!state_id)
10403 return hlua_filter_init_per_thread(px, fconf);
10404 return 0;
10405}
10406
10407static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf)
10408{
10409
10410 if (fconf->conf) {
10411 struct hlua_flt_config *conf = fconf->conf;
10412 int state_id = reg_flt_to_stack_id(conf->reg);
10413 int pos;
10414
10415 /* Rely on per-thread deinit for global scripts */
10416 if (!state_id)
10417 hlua_filter_deinit_per_thread(px, fconf);
10418
10419 for (pos = 0; conf->args[pos]; pos++)
10420 free(conf->args[pos]);
10421 free(conf->args);
10422 }
10423 ha_free(&fconf->conf);
10424 ha_free((char **)&fconf->id);
10425 ha_free(&fconf->ops);
10426}
10427
10428static int hlua_filter_new(struct stream *s, struct filter *filter)
10429{
10430 struct hlua_flt_config *conf = FLT_CONF(filter);
10431 struct hlua_flt_ctx *flt_ctx = NULL;
10432 int ret = 1;
10433
10434 /* In the execution wrappers linked with a stream, the
10435 * Lua context can be not initialized. This behavior
10436 * permits to save performances because a systematic
10437 * Lua initialization cause 5% performances loss.
10438 */
10439 if (!s->hlua) {
10440 struct hlua *hlua;
10441
10442 hlua = pool_alloc(pool_head_hlua);
10443 if (!hlua) {
10444 SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n",
10445 conf->reg->name);
10446 ret = 0;
10447 goto end;
10448 }
10449 HLUA_INIT(hlua);
10450 s->hlua = hlua;
10451 if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) {
10452 SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n",
10453 conf->reg->name);
10454 ret = 0;
10455 goto end;
10456 }
10457 }
10458
10459 flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx);
10460 if (!flt_ctx) {
10461 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
10462 conf->reg->name);
10463 ret = 0;
10464 goto end;
10465 }
10466 flt_ctx->hlua[0] = pool_alloc(pool_head_hlua);
10467 flt_ctx->hlua[1] = pool_alloc(pool_head_hlua);
10468 if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) {
10469 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
10470 conf->reg->name);
10471 ret = 0;
10472 goto end;
10473 }
10474 HLUA_INIT(flt_ctx->hlua[0]);
10475 HLUA_INIT(flt_ctx->hlua[1]);
10476 if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) ||
10477 !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) {
10478 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
10479 conf->reg->name);
10480 ret = 0;
10481 goto end;
10482 }
10483
10484 if (!HLUA_IS_RUNNING(s->hlua)) {
10485 /* The following Lua calls can fail. */
10486 if (!SET_SAFE_LJMP(s->hlua)) {
10487 const char *error;
10488
10489 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
10490 error = lua_tostring(s->hlua->T, -1);
10491 else
10492 error = "critical error";
10493 SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error);
10494 ret = 0;
10495 goto end;
10496 }
10497
10498 /* Check stack size. */
10499 if (!lua_checkstack(s->hlua->T, 1)) {
10500 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
Tim Duesterhus22817382021-09-11 23:17:25 +020010501 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet69c581a2021-05-31 08:54:04 +020010502 ret = 0;
10503 goto end;
10504 }
10505
10506 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]);
10507 if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) {
10508 SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n",
10509 conf->reg->name);
10510 RESET_SAFE_LJMP(s->hlua);
10511 ret = 0;
10512 goto end;
10513 }
10514 lua_insert(s->hlua->T, -2);
10515
10516 /* Push the copy on the stack */
10517 s->hlua->nargs = 1;
10518
10519 /* We must initialize the execution timeouts. */
10520 s->hlua->max_time = hlua_timeout_session;
10521
10522 /* At this point the execution is safe. */
10523 RESET_SAFE_LJMP(s->hlua);
10524 }
10525
10526 switch (hlua_ctx_resume(s->hlua, 0)) {
10527 case HLUA_E_OK:
10528 /* Nothing returned or not a table, ignore the filter for current stream */
10529 if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) {
10530 ret = 0;
10531 goto end;
10532 }
10533
10534 /* Attached the filter pointer to the ctx */
10535 lua_pushstring(s->hlua->T, "__filter");
10536 lua_pushlightuserdata(s->hlua->T, filter);
10537 lua_settable(s->hlua->T, -3);
10538
10539 /* Save a ref on the filter ctx */
10540 lua_pushvalue(s->hlua->T, 1);
10541 flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX);
10542 filter->ctx = flt_ctx;
10543 break;
10544 case HLUA_E_ERRMSG:
10545 SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1));
10546 ret = -1;
10547 goto end;
10548 case HLUA_E_ETMOUT:
10549 SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name);
10550 ret = 0;
10551 goto end;
10552 case HLUA_E_NOMEM:
10553 SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name);
10554 ret = 0;
10555 goto end;
10556 case HLUA_E_AGAIN:
10557 case HLUA_E_YIELD:
10558 SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()"
10559 " are not allowed from 'new' function.\n", conf->reg->name);
10560 ret = 0;
10561 goto end;
10562 case HLUA_E_ERR:
10563 SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name);
10564 ret = 0;
10565 goto end;
10566 default:
10567 ret = 0;
10568 goto end;
10569 }
10570
10571 end:
10572 if (s->hlua)
10573 lua_settop(s->hlua->T, 0);
10574 if (ret <= 0) {
10575 if (flt_ctx) {
10576 hlua_ctx_destroy(flt_ctx->hlua[0]);
10577 hlua_ctx_destroy(flt_ctx->hlua[1]);
10578 pool_free(pool_head_hlua_flt_ctx, flt_ctx);
10579 }
10580 }
10581 return ret;
10582}
10583
10584static void hlua_filter_delete(struct stream *s, struct filter *filter)
10585{
10586 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10587
10588 luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref);
10589 hlua_ctx_destroy(flt_ctx->hlua[0]);
10590 hlua_ctx_destroy(flt_ctx->hlua[1]);
10591 pool_free(pool_head_hlua_flt_ctx, flt_ctx);
10592 filter->ctx = NULL;
10593}
10594
Christopher Faulet9f55a502020-02-25 15:21:02 +010010595static int hlua_filter_from_payload(struct filter *filter)
10596{
10597 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10598
10599 return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD));
10600}
10601
Christopher Fauletc404f112020-02-26 15:03:09 +010010602static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun,
10603 int dir, unsigned int flags)
10604{
10605 struct hlua *flt_hlua;
10606 struct hlua_flt_config *conf = FLT_CONF(filter);
10607 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10608 unsigned int hflags = HLUA_TXN_FLT_CTX;
10609 int ret = 1;
10610
10611 flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)];
10612 if (!flt_hlua)
10613 goto end;
10614
10615 if (!HLUA_IS_RUNNING(flt_hlua)) {
10616 int extra_idx = lua_gettop(flt_hlua->T);
10617
10618 /* The following Lua calls can fail. */
10619 if (!SET_SAFE_LJMP(flt_hlua)) {
10620 const char *error;
10621
10622 if (lua_type(flt_hlua->T, -1) == LUA_TSTRING)
10623 error = lua_tostring(flt_hlua->T, -1);
10624 else
10625 error = "critical error";
10626 SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error);
10627 goto end;
10628 }
10629
10630 /* Check stack size. */
10631 if (!lua_checkstack(flt_hlua->T, 3)) {
10632 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
10633 RESET_SAFE_LJMP(flt_hlua);
10634 goto end;
10635 }
10636
10637 lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref);
10638 if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) {
10639 RESET_SAFE_LJMP(flt_hlua);
10640 goto end;
10641 }
10642 lua_insert(flt_hlua->T, -2);
10643
10644 if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) {
10645 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
10646 RESET_SAFE_LJMP(flt_hlua);
10647 goto end;
10648 }
10649 flt_hlua->nargs = 2;
10650
10651 if (flags & HLUA_FLT_CB_ARG_CHN) {
10652 if (dir == SMP_OPT_DIR_REQ)
10653 lua_getfield(flt_hlua->T, -1, "req");
10654 else
10655 lua_getfield(flt_hlua->T, -1, "res");
10656 if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) {
10657 lua_pushstring(flt_hlua->T, "__filter");
10658 lua_pushlightuserdata(flt_hlua->T, filter);
10659 lua_settable(flt_hlua->T, -3);
10660 }
10661 flt_hlua->nargs++;
10662 }
10663 else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) {
Christopher Fauleteae8afa2020-02-26 17:15:48 +010010664 if (dir == SMP_OPT_DIR_REQ)
10665 lua_getfield(flt_hlua->T, -1, "http_req");
10666 else
10667 lua_getfield(flt_hlua->T, -1, "http_res");
10668 if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) {
10669 lua_pushstring(flt_hlua->T, "__filter");
10670 lua_pushlightuserdata(flt_hlua->T, filter);
10671 lua_settable(flt_hlua->T, -3);
10672 }
10673 flt_hlua->nargs++;
Christopher Fauletc404f112020-02-26 15:03:09 +010010674 }
10675
10676 /* Check stack size. */
10677 if (!lua_checkstack(flt_hlua->T, 1)) {
10678 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
10679 RESET_SAFE_LJMP(flt_hlua);
10680 goto end;
10681 }
10682
10683 while (extra_idx--) {
10684 lua_pushvalue(flt_hlua->T, 1);
10685 lua_remove(flt_hlua->T, 1);
10686 flt_hlua->nargs++;
10687 }
10688
10689 /* We must initialize the execution timeouts. */
10690 flt_hlua->max_time = hlua_timeout_session;
10691
10692 /* At this point the execution is safe. */
10693 RESET_SAFE_LJMP(flt_hlua);
10694 }
10695
10696 switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) {
10697 case HLUA_E_OK:
10698 /* Catch the return value if it required */
10699 if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) {
10700 ret = lua_tointeger(flt_hlua->T, -1);
10701 lua_settop(flt_hlua->T, 0); /* Empty the stack. */
10702 }
10703
10704 /* Set timeout in the required channel. */
10705 if (flt_hlua->wake_time != TICK_ETERNITY) {
10706 if (dir == SMP_OPT_DIR_REQ)
10707 s->req.analyse_exp = flt_hlua->wake_time;
10708 else
10709 s->res.analyse_exp = flt_hlua->wake_time;
10710 }
10711 break;
10712 case HLUA_E_AGAIN:
10713 /* Set timeout in the required channel. */
10714 if (flt_hlua->wake_time != TICK_ETERNITY) {
10715 if (dir == SMP_OPT_DIR_REQ)
10716 s->req.analyse_exp = flt_hlua->wake_time;
10717 else
10718 s->res.analyse_exp = flt_hlua->wake_time;
10719 }
10720 /* Some actions can be wake up when a "write" event
10721 * is detected on a response channel. This is useful
10722 * only for actions targeted on the requests.
10723 */
10724 if (HLUA_IS_WAKERESWR(flt_hlua))
10725 s->res.flags |= CF_WAKE_WRITE;
10726 if (HLUA_IS_WAKEREQWR(flt_hlua))
10727 s->req.flags |= CF_WAKE_WRITE;
10728 ret = 0;
10729 goto end;
10730 case HLUA_E_ERRMSG:
10731 SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1));
10732 ret = -1;
10733 goto end;
10734 case HLUA_E_ETMOUT:
10735 SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun);
10736 goto end;
10737 case HLUA_E_NOMEM:
10738 SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name);
10739 goto end;
10740 case HLUA_E_YIELD:
10741 SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()"
10742 " are not allowed from '%s' callback.\n", conf->reg->name, fun);
10743 goto end;
10744 case HLUA_E_ERR:
10745 SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun);
10746 goto end;
10747 default:
10748 goto end;
10749 }
10750
10751
10752 end:
10753 return ret;
10754}
10755
10756static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
10757{
10758 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10759
10760 flt_ctx->flags = 0;
10761 return hlua_filter_callback(s, filter, "start_analyze",
10762 (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES),
10763 (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN));
10764}
10765
10766static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
10767{
10768 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10769
10770 flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD;
10771 return hlua_filter_callback(s, filter, "end_analyze",
10772 (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES),
10773 (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN));
10774}
10775
Christopher Fauleteae8afa2020-02-26 17:15:48 +010010776static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
10777{
10778 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10779
10780 flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD;
10781 return hlua_filter_callback(s, filter, "http_headers",
10782 (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES),
10783 (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG));
10784}
10785
10786static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
10787 unsigned int offset, unsigned int len)
10788{
10789 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10790 struct hlua *flt_hlua;
10791 int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
10792 int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1);
10793 int ret;
10794
10795 flt_hlua = flt_ctx->hlua[idx];
10796 flt_ctx->cur_off[idx] = offset;
10797 flt_ctx->cur_len[idx] = len;
10798 flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD;
10799 ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG));
10800 if (ret != -1) {
10801 ret = flt_ctx->cur_len[idx];
10802 if (lua_gettop(flt_hlua->T) > 0) {
10803 ret = lua_tointeger(flt_hlua->T, -1);
10804 if (ret > flt_ctx->cur_len[idx])
10805 ret = flt_ctx->cur_len[idx];
10806 lua_settop(flt_hlua->T, 0); /* Empty the stack. */
10807 }
10808 }
10809 return ret;
10810}
10811
10812static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg)
10813{
10814 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10815
10816 flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD;
10817 return hlua_filter_callback(s, filter, "http_end",
10818 (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES),
10819 (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG));
10820}
10821
Christopher Fauletc404f112020-02-26 15:03:09 +010010822static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn,
10823 unsigned int offset, unsigned int len)
10824{
10825 struct hlua_flt_ctx *flt_ctx = filter->ctx;
10826 struct hlua *flt_hlua;
10827 int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
10828 int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1);
10829 int ret;
10830
10831 flt_hlua = flt_ctx->hlua[idx];
10832 flt_ctx->cur_off[idx] = offset;
10833 flt_ctx->cur_len[idx] = len;
10834 flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD;
10835 ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN));
10836 if (ret != -1) {
10837 ret = flt_ctx->cur_len[idx];
10838 if (lua_gettop(flt_hlua->T) > 0) {
10839 ret = lua_tointeger(flt_hlua->T, -1);
10840 if (ret > flt_ctx->cur_len[idx])
10841 ret = flt_ctx->cur_len[idx];
10842 lua_settop(flt_hlua->T, 0); /* Empty the stack. */
10843 }
10844 }
10845 return ret;
10846}
10847
Christopher Faulet69c581a2021-05-31 08:54:04 +020010848static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px,
10849 struct flt_conf *fconf, char **err, void *private)
10850{
10851 struct hlua_reg_filter *reg_flt = private;
10852 lua_State *L;
10853 struct hlua_flt_config *conf = NULL;
10854 const char *flt_id = NULL;
10855 int state_id, pos, flt_flags = 0;
10856 struct flt_ops *hlua_flt_ops = NULL;
10857
10858 state_id = reg_flt_to_stack_id(reg_flt);
10859 L = hlua_states[state_id];
10860
10861 /* Initialize the filter ops with default callbacks */
10862 hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops));
Christopher Fauletc86bb872021-08-13 08:33:57 +020010863 if (!hlua_flt_ops)
10864 goto error;
Christopher Faulet69c581a2021-05-31 08:54:04 +020010865 hlua_flt_ops->init = hlua_filter_init;
10866 hlua_flt_ops->deinit = hlua_filter_deinit;
10867 if (state_id) {
10868 /* Set per-thread callback if script is loaded per-thread */
10869 hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread;
10870 hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread;
10871 }
10872 hlua_flt_ops->attach = hlua_filter_new;
10873 hlua_flt_ops->detach = hlua_filter_delete;
10874
10875 /* Push the filter class on the stack and resolve all callbacks */
10876 lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]);
10877
Christopher Fauletc404f112020-02-26 15:03:09 +010010878 if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION)
10879 hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze;
10880 lua_pop(L, 1);
10881 if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION)
10882 hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze;
10883 lua_pop(L, 1);
Christopher Fauleteae8afa2020-02-26 17:15:48 +010010884 if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION)
10885 hlua_flt_ops->http_headers = hlua_filter_http_headers;
10886 lua_pop(L, 1);
10887 if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION)
10888 hlua_flt_ops->http_payload = hlua_filter_http_payload;
10889 lua_pop(L, 1);
10890 if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION)
10891 hlua_flt_ops->http_end = hlua_filter_http_end;
10892 lua_pop(L, 1);
Christopher Fauletc404f112020-02-26 15:03:09 +010010893 if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION)
10894 hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload;
10895 lua_pop(L, 1);
Christopher Faulet69c581a2021-05-31 08:54:04 +020010896
10897 /* Get id and flags of the filter class */
10898 if (lua_getfield(L, -1, "id") == LUA_TSTRING)
10899 flt_id = lua_tostring(L, -1);
10900 lua_pop(L, 1);
10901 if (lua_getfield(L, -1, "flags") == LUA_TNUMBER)
10902 flt_flags = lua_tointeger(L, -1);
10903 lua_pop(L, 1);
10904
10905 /* Create the filter config */
10906 conf = calloc(1, sizeof(*conf));
Christopher Fauletc86bb872021-08-13 08:33:57 +020010907 if (!conf)
Christopher Faulet69c581a2021-05-31 08:54:04 +020010908 goto error;
Christopher Faulet69c581a2021-05-31 08:54:04 +020010909 conf->reg = reg_flt;
10910
10911 /* duplicate args */
10912 for (pos = 0; *args[*cur_arg + 1 + pos]; pos++);
10913 conf->args = calloc(pos + 1, sizeof(*conf->args));
Christopher Fauletc86bb872021-08-13 08:33:57 +020010914 if (!conf->args)
10915 goto error;
10916 for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) {
Christopher Faulet69c581a2021-05-31 08:54:04 +020010917 conf->args[pos] = strdup(args[*cur_arg + 1 + pos]);
Christopher Fauletc86bb872021-08-13 08:33:57 +020010918 if (!conf->args[pos])
10919 goto error;
10920 }
Christopher Faulet69c581a2021-05-31 08:54:04 +020010921 conf->args[pos] = NULL;
10922 *cur_arg += pos + 1;
10923
Christopher Fauletc86bb872021-08-13 08:33:57 +020010924 if (flt_id) {
10925 fconf->id = strdup(flt_id);
10926 if (!fconf->id)
10927 goto error;
10928 }
Christopher Faulet69c581a2021-05-31 08:54:04 +020010929 fconf->flags = flt_flags;
10930 fconf->conf = conf;
10931 fconf->ops = hlua_flt_ops;
10932
10933 lua_settop(L, 0);
10934 return 0;
10935
10936 error:
Christopher Fauletc86bb872021-08-13 08:33:57 +020010937 memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
Christopher Faulet69c581a2021-05-31 08:54:04 +020010938 free(hlua_flt_ops);
Christopher Fauletc86bb872021-08-13 08:33:57 +020010939 if (conf && conf->args) {
10940 for (pos = 0; conf->args[pos]; pos++)
10941 free(conf->args[pos]);
10942 free(conf->args);
10943 }
Christopher Faulet69c581a2021-05-31 08:54:04 +020010944 free(conf);
Christopher Fauletc86bb872021-08-13 08:33:57 +020010945 free((char *)fconf->id);
Christopher Faulet69c581a2021-05-31 08:54:04 +020010946 lua_settop(L, 0);
10947 return -1;
10948}
10949
Christopher Fauletc404f112020-02-26 15:03:09 +010010950__LJMP static int hlua_register_data_filter(lua_State *L)
10951{
10952 struct filter *filter;
10953 struct channel *chn;
10954
10955 MAY_LJMP(check_args(L, 2, "register_data_filter"));
10956 MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE));
10957 chn = MAY_LJMP(hlua_checkchannel(L, 2));
10958
10959 lua_getfield(L, 1, "__filter");
10960 MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA));
10961 filter = lua_touserdata (L, -1);
10962 lua_pop(L, 1);
10963
10964 register_data_filter(chn_strm(chn), chn, filter);
10965 return 1;
10966}
10967
10968__LJMP static int hlua_unregister_data_filter(lua_State *L)
10969{
10970 struct filter *filter;
10971 struct channel *chn;
10972
10973 MAY_LJMP(check_args(L, 2, "unregister_data_filter"));
10974 MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE));
10975 chn = MAY_LJMP(hlua_checkchannel(L, 2));
10976
10977 lua_getfield(L, 1, "__filter");
10978 MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA));
10979 filter = lua_touserdata (L, -1);
10980 lua_pop(L, 1);
10981
10982 unregister_data_filter(chn_strm(chn), chn, filter);
10983 return 1;
10984}
10985
Christopher Faulet69c581a2021-05-31 08:54:04 +020010986/* This function is an LUA binding used for registering a filter. It expects a
Ilya Shipitsinff0f2782021-08-22 22:18:07 +050010987 * filter name used in the haproxy configuration file and a LUA function to
Christopher Faulet69c581a2021-05-31 08:54:04 +020010988 * parse configuration arguments.
10989 */
10990__LJMP static int hlua_register_filter(lua_State *L)
10991{
10992 struct buffer *trash;
10993 struct flt_kw_list *fkl;
10994 struct flt_kw *fkw;
10995 const char *name;
10996 struct hlua_reg_filter *reg_flt= NULL;
10997 int flt_ref, fun_ref;
10998 int len;
10999
11000 MAY_LJMP(check_args(L, 3, "register_filter"));
11001
11002 /* First argument : filter name. */
11003 name = MAY_LJMP(luaL_checkstring(L, 1));
11004
11005 /* Second argument : The filter class */
11006 flt_ref = MAY_LJMP(hlua_checktable(L, 2));
11007
11008 /* Third argument : lua function. */
11009 fun_ref = MAY_LJMP(hlua_checkfunction(L, 3));
11010
11011 trash = get_trash_chunk();
11012 chunk_printf(trash, "lua.%s", name);
11013 fkw = flt_find_kw(trash->area);
11014 if (fkw != NULL) {
11015 reg_flt = fkw->private;
11016 if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) {
11017 ha_warning("Trying to register filter 'lua.%s' more than once. "
11018 "This will become a hard error in version 2.5.\n", name);
11019 }
11020 reg_flt->flt_ref[hlua_state_id] = flt_ref;
11021 reg_flt->fun_ref[hlua_state_id] = fun_ref;
11022 return 0;
11023 }
11024
11025 fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2);
11026 if (!fkl)
11027 goto alloc_error;
11028 fkl->scope = "HLUA";
11029
11030 reg_flt = new_hlua_reg_filter(name);
11031 if (!reg_flt)
11032 goto alloc_error;
11033
11034 reg_flt->flt_ref[hlua_state_id] = flt_ref;
11035 reg_flt->fun_ref[hlua_state_id] = fun_ref;
11036
11037 /* The filter keyword */
11038 len = strlen("lua.") + strlen(name) + 1;
11039 fkl->kw[0].kw = calloc(1, len);
11040 if (!fkl->kw[0].kw)
11041 goto alloc_error;
11042
11043 snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name);
11044
11045 fkl->kw[0].parse = hlua_filter_parse_fct;
11046 fkl->kw[0].private = reg_flt;
11047 memset(&fkl->kw[1], 0, sizeof(*fkl->kw));
11048
11049 /* Register this new filter */
11050 flt_register_keywords(fkl);
11051
11052 return 0;
11053
11054 alloc_error:
11055 release_hlua_reg_filter(reg_flt);
11056 ha_free(&fkl);
11057 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
11058 return 0; /* Never reached */
11059}
11060
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011061static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011062 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011063 char **err, unsigned int *timeout)
11064{
11065 const char *error;
11066
11067 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +020011068 if (error == PARSE_TIME_OVER) {
11069 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
11070 args[1], args[0]);
11071 return -1;
11072 }
11073 else if (error == PARSE_TIME_UNDER) {
11074 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
11075 args[1], args[0]);
11076 return -1;
11077 }
11078 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011079 memprintf(err, "%s: invalid timeout", args[0]);
11080 return -1;
11081 }
11082 return 0;
11083}
11084
11085static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011086 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011087 char **err)
11088{
11089 return hlua_read_timeout(args, section_type, curpx, defpx,
11090 file, line, err, &hlua_timeout_session);
11091}
11092
11093static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011094 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011095 char **err)
11096{
11097 return hlua_read_timeout(args, section_type, curpx, defpx,
11098 file, line, err, &hlua_timeout_task);
11099}
11100
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020011101static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011102 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020011103 char **err)
11104{
11105 return hlua_read_timeout(args, section_type, curpx, defpx,
11106 file, line, err, &hlua_timeout_applet);
11107}
11108
Thierry FOURNIERee9f8022015-03-03 17:37:37 +010011109static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011110 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +010011111 char **err)
11112{
11113 char *error;
11114
11115 hlua_nb_instruction = strtoll(args[1], &error, 10);
11116 if (*error != '\0') {
11117 memprintf(err, "%s: invalid number", args[0]);
11118 return -1;
11119 }
11120 return 0;
11121}
11122
Willy Tarreau32f61e22015-03-18 17:54:59 +010011123static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011124 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +010011125 char **err)
11126{
11127 char *error;
11128
11129 if (*(args[1]) == 0) {
11130 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
11131 return -1;
11132 }
11133 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
11134 if (*error != '\0') {
11135 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
11136 return -1;
11137 }
11138 return 0;
11139}
11140
11141
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011142/* This function is called by the main configuration key "lua-load". It loads and
11143 * execute an lua file during the parsing of the HAProxy configuration file. It is
11144 * the main lua entry point.
11145 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080011146 * This function runs with the HAProxy keywords API. It returns -1 if an error
11147 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011148 *
11149 * In some error case, LUA set an error message in top of the stack. This function
11150 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020011151 *
11152 * This function can fail with an abort() due to an Lua critical error.
11153 * We are in the configuration parsing process of HAProxy, this abort() is
11154 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011155 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011156static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011157{
11158 int error;
11159
11160 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011161 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011162 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011163 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
11164 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011165 return -1;
11166 }
11167
11168 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011169 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011170 switch (error) {
11171 case LUA_OK:
11172 break;
11173 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011174 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
11175 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011176 return -1;
11177 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +010011178 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011179 return -1;
11180 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011181 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
11182 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011183 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +020011184#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011185 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011186 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
11187 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011188 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +020011189#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011190 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011191 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
11192 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011193 return -1;
11194 }
11195
11196 return 0;
11197}
11198
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011199static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011200 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011201 char **err)
11202{
11203 if (*(args[1]) == 0) {
11204 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
11205 return -1;
11206 }
11207
Thierry Fournier59f11be2020-11-29 00:37:41 +010011208 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +010011209 hlua_state_id = 0;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011210 ha_set_thread(NULL);
Thierry Fournierafc63e22020-11-28 17:06:51 +010011211 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +010011212}
11213
Thierry Fournier59f11be2020-11-29 00:37:41 +010011214static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011215 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +010011216 char **err)
11217{
11218 int len;
11219
11220 if (*(args[1]) == 0) {
11221 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
11222 return -1;
11223 }
11224
11225 if (per_thread_load == NULL) {
11226 /* allocate the first entry large enough to store the final NULL */
11227 per_thread_load = calloc(1, sizeof(*per_thread_load));
11228 if (per_thread_load == NULL) {
11229 memprintf(err, "out of memory error");
11230 return -1;
11231 }
11232 }
11233
11234 /* count used entries */
11235 for (len = 0; per_thread_load[len] != NULL; len++)
11236 ;
11237
11238 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
11239 if (per_thread_load == NULL) {
11240 memprintf(err, "out of memory error");
11241 return -1;
11242 }
11243
11244 per_thread_load[len] = strdup(args[1]);
11245 per_thread_load[len + 1] = NULL;
11246
11247 if (per_thread_load[len] == NULL) {
11248 memprintf(err, "out of memory error");
11249 return -1;
11250 }
11251
11252 /* loading for thread 1 only */
11253 hlua_state_id = 1;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011254 ha_set_thread(NULL);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011255 return hlua_load_state(args[1], hlua_states[1], err);
11256}
11257
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +010011258/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
11259 * in the given <ctx>.
11260 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +010011261static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +010011262{
Thierry Fournier3fb9e512020-11-28 10:13:12 +010011263 lua_getglobal(L, "package"); /* push package variable */
11264 lua_pushstring(L, path); /* push given path */
11265 lua_pushstring(L, ";"); /* push semicolon */
11266 lua_getfield(L, -3, type); /* push old path */
11267 lua_concat(L, 3); /* concatenate to new path */
11268 lua_setfield(L, -2, type); /* store new path */
11269 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +010011270
11271 return 0;
11272}
11273
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011274static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010011275 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011276 char **err)
11277{
11278 char *path;
11279 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011280 struct prepend_path *p = NULL;
Bertrand Jacquin7fbc7702021-11-24 21:16:06 +000011281 size_t i;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011282
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011283 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011284 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011285 }
11286
11287 if (!(*args[1])) {
11288 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011289 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011290 }
11291 path = args[1];
11292
11293 if (*args[2]) {
11294 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
11295 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011296 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011297 }
11298 type = args[2];
11299 }
11300
Thierry Fournier59f11be2020-11-29 00:37:41 +010011301 p = calloc(1, sizeof(*p));
11302 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +010011303 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011304 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011305 }
11306 p->path = strdup(path);
11307 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +010011308 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011309 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011310 }
11311 p->type = strdup(type);
11312 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +010011313 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011314 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011315 }
Willy Tarreau2b718102021-04-21 07:32:39 +020011316 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011317
Tim Duesterhus9e5e5862021-10-11 18:51:08 +020011318 /* Handle the global state and the per-thread state for the first
11319 * thread. The remaining threads will be initialized based on
11320 * prepend_path_list.
11321 */
Bertrand Jacquin7fbc7702021-11-24 21:16:06 +000011322 for (i = 0; i < 2; i++) {
Tim Duesterhus9e5e5862021-10-11 18:51:08 +020011323 lua_State *L = hlua_states[i];
11324 const char *error;
11325
11326 if (setjmp(safe_ljmp_env) != 0) {
11327 lua_atpanic(L, hlua_panic_safe);
11328 if (lua_type(L, -1) == LUA_TSTRING)
11329 error = lua_tostring(L, -1);
11330 else
11331 error = "critical error";
11332 fprintf(stderr, "lua-prepend-path: %s.\n", error);
11333 exit(1);
11334 } else {
11335 lua_atpanic(L, hlua_panic_ljmp);
11336 }
11337
11338 hlua_prepend_path(L, type, path);
11339
11340 lua_atpanic(L, hlua_panic_safe);
11341 }
11342
Thierry Fournier59f11be2020-11-29 00:37:41 +010011343 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +010011344
11345err2:
11346 free(p->type);
11347 free(p->path);
11348err:
11349 free(p);
11350 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011351}
11352
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011353/* configuration keywords declaration */
11354static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +010011355 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011356 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +010011357 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +010011358 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
11359 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +020011360 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +010011361 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +010011362 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +010011363 { 0, NULL, NULL },
11364}};
11365
Willy Tarreau0108d902018-11-25 19:14:37 +010011366INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11367
Christopher Fauletafd8f102018-11-08 11:34:21 +010011368
Thierry FOURNIERbabae282015-09-17 11:36:37 +020011369/* This function can fail with an abort() due to an Lua critical error.
11370 * We are in the initialisation process of HAProxy, this abort() is
11371 * tolerated.
11372 */
Thierry Fournierb8cef172020-11-28 15:37:17 +010011373int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011374{
11375 struct hlua_init_function *init;
11376 const char *msg;
11377 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +010011378 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +010011379 const char *kind;
11380 const char *trace;
11381 int return_status = 1;
11382#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
11383 int nres;
11384#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011385
Willy Tarreaucdb53462020-12-02 12:12:00 +010011386 /* disable memory limit checks if limit is not set */
11387 if (!hlua_global_allocator.limit)
11388 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
11389
Ilya Shipitsin856aabc2020-04-16 23:51:34 +050011390 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +010011391 if (setjmp(safe_ljmp_env) != 0) {
11392 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +010011393 if (lua_type(L, -1) == LUA_TSTRING)
11394 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +010011395 else
11396 error = "critical error";
11397 fprintf(stderr, "Lua post-init: %s.\n", error);
11398 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +010011399 } else {
11400 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +010011401 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +020011402
Thierry Fournierb8cef172020-11-28 15:37:17 +010011403 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +010011404
Thierry Fournierc7492592020-11-28 23:57:24 +010011405 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +010011406 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +010011407
11408#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +010011409 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +010011410#else
Thierry Fournierb8cef172020-11-28 15:37:17 +010011411 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +010011412#endif
11413 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011414 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +010011415
11416 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +010011417 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +010011418 break;
Thierry Fournier670db242020-11-28 10:49:59 +010011419
11420 case LUA_ERRERR:
11421 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -070011422 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +010011423 case LUA_ERRRUN:
11424 if (!kind)
11425 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +010011426 msg = lua_tostring(L, -1);
11427 lua_settop(L, 0); /* Empty the stack. */
11428 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +010011429 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +010011430 if (msg)
11431 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
11432 else
11433 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
11434 return_status = 0;
11435 break;
11436
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011437 default:
Thierry Fournier670db242020-11-28 10:49:59 +010011438 /* Unknown error */
11439 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -070011440 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +010011441 case LUA_YIELD:
11442 /* yield is not configured at this step, this state doesn't happen */
11443 if (!kind)
11444 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -070011445 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +010011446 case LUA_ERRMEM:
11447 if (!kind)
11448 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +010011449 lua_settop(L, 0);
11450 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +010011451 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +010011452 ha_alert("Lua init: %s: %s\n", kind, trace);
11453 return_status = 0;
11454 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011455 }
Thierry Fournier670db242020-11-28 10:49:59 +010011456 if (!return_status)
11457 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011458 }
Thierry Fournier3c539322020-11-28 16:05:05 +010011459
11460 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +010011461 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011462}
11463
Thierry Fournierb8cef172020-11-28 15:37:17 +010011464int hlua_post_init()
11465{
Thierry Fournier59f11be2020-11-29 00:37:41 +010011466 int ret;
11467 int i;
11468 int errors;
11469 char *err = NULL;
11470 struct hlua_function *fcn;
Christopher Faulet69c581a2021-05-31 08:54:04 +020011471 struct hlua_reg_filter *reg_flt;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011472
Willy Tarreaub1310492021-08-30 09:35:18 +020011473#if defined(USE_OPENSSL)
Thierry Fournierb8cef172020-11-28 15:37:17 +010011474 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010011475 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +010011476 int saved_used_backed = global.ssl_used_backend;
11477 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010011478 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +010011479 global.ssl_used_backend = saved_used_backed;
11480 }
11481#endif
11482
Thierry Fournierc7492592020-11-28 23:57:24 +010011483 /* Perform post init of common thread */
11484 hlua_state_id = 0;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011485 ha_set_thread(&ha_thread_info[0]);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011486 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
11487 if (ret == 0)
11488 return 0;
11489
11490 /* init remaining lua states and load files */
11491 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
11492
11493 /* set thread context */
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011494 ha_set_thread(&ha_thread_info[hlua_state_id - 1]);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011495
11496 /* Init lua state */
11497 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
11498
11499 /* Load lua files */
11500 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
11501 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
11502 if (ret != 0) {
11503 ha_alert("Lua init: %s\n", err);
11504 return 0;
11505 }
11506 }
11507 }
11508
11509 /* Reset thread context */
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011510 ha_set_thread(NULL);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011511
11512 /* Execute post init for all states */
11513 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
11514
11515 /* set thread context */
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011516 ha_set_thread(&ha_thread_info[hlua_state_id - 1]);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011517
11518 /* run post init */
11519 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
11520 if (ret == 0)
11521 return 0;
11522 }
11523
11524 /* Reset thread context */
Willy Tarreau43ab05b2021-09-28 09:43:11 +020011525 ha_set_thread(NULL);
Thierry Fournier59f11be2020-11-29 00:37:41 +010011526
11527 /* control functions registering. Each function must have:
11528 * - only the function_ref[0] set positive and all other to -1
11529 * - only the function_ref[0] set to -1 and all other positive
11530 * This ensure a same reference is not used both in shared
11531 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +050011532 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +010011533 * complicated to found for the end user.
11534 */
11535 errors = 0;
11536 list_for_each_entry(fcn, &referenced_functions, l) {
11537 ret = 0;
11538 for (i = 1; i < global.nbthread + 1; i++) {
11539 if (fcn->function_ref[i] == -1)
11540 ret--;
11541 else
11542 ret++;
11543 }
11544 if (abs(ret) != global.nbthread) {
11545 ha_alert("Lua function '%s' is not referenced in all thread. "
11546 "Expect function in all thread or in none thread.\n", fcn->name);
11547 errors++;
11548 continue;
11549 }
11550
11551 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +050011552 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
11553 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +010011554 "exclusive.\n", fcn->name);
11555 errors++;
11556 }
11557 }
11558
Christopher Faulet69c581a2021-05-31 08:54:04 +020011559 /* Do the same with registered filters */
11560 list_for_each_entry(reg_flt, &referenced_filters, l) {
11561 ret = 0;
11562 for (i = 1; i < global.nbthread + 1; i++) {
11563 if (reg_flt->flt_ref[i] == -1)
11564 ret--;
11565 else
11566 ret++;
11567 }
11568 if (abs(ret) != global.nbthread) {
11569 ha_alert("Lua filter '%s' is not referenced in all thread. "
11570 "Expect function in all thread or in none thread.\n", reg_flt->name);
11571 errors++;
11572 continue;
11573 }
11574
11575 if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) {
11576 ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) "
11577 "and per-thread Lua context (through lua-load-per-thread). these two context "
11578 "exclusive.\n", fcn->name);
11579 errors++;
11580 }
11581 }
11582
11583
Thierry Fournier59f11be2020-11-29 00:37:41 +010011584 if (errors > 0)
11585 return 0;
11586
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +050011587 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +010011588 * -1 in order to have probably a segfault if someone use it
11589 */
11590 hlua_state_id = -1;
11591
11592 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +010011593}
11594
Willy Tarreau32f61e22015-03-18 17:54:59 +010011595/* The memory allocator used by the Lua stack. <ud> is a pointer to the
11596 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
11597 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +010011598 * allocation. <nsize> is the requested new size. A new allocation is
11599 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +010011600 * zero. This one verifies that the limits are respected but is optimized
11601 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreaua5efdff2021-10-22 16:00:02 +020011602 *
11603 * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses
11604 * POSIX by making realloc(ptr,0) an effective free(), but others do not do
11605 * that and will simply allocate zero as if it were the result of malloc(0),
11606 * so mapping this onto realloc() will lead to memory leaks on non-glibc
11607 * systems.
Willy Tarreau32f61e22015-03-18 17:54:59 +010011608 */
11609static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
11610{
11611 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +010011612 size_t limit, old, new;
11613
11614 /* a limit of ~0 means unlimited and boot complete, so there's no need
11615 * for accounting anymore.
11616 */
Willy Tarreaua5efdff2021-10-22 16:00:02 +020011617 if (likely(~zone->limit == 0)) {
11618 if (!nsize)
11619 ha_free(&ptr);
11620 else
11621 ptr = realloc(ptr, nsize);
11622 return ptr;
11623 }
Willy Tarreau32f61e22015-03-18 17:54:59 +010011624
Willy Tarreaud36c7fa2020-12-02 12:26:29 +010011625 if (!ptr)
11626 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +010011627
Willy Tarreaucdb53462020-12-02 12:12:00 +010011628 /* enforce strict limits across all threads */
11629 limit = zone->limit;
11630 old = _HA_ATOMIC_LOAD(&zone->allocated);
11631 do {
11632 new = old + nsize - osize;
11633 if (unlikely(nsize && limit && new > limit))
11634 return NULL;
11635 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +010011636
Willy Tarreaua5efdff2021-10-22 16:00:02 +020011637 if (!nsize)
11638 ha_free(&ptr);
11639 else
11640 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +010011641
11642 if (unlikely(!ptr && nsize)) // failed
11643 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
11644
11645 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +010011646 return ptr;
11647}
11648
Thierry Fournierecb83c22020-11-28 15:49:44 +010011649/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020011650 * We are in the initialisation process of HAProxy, this abort() is
11651 * tolerated.
11652 */
Thierry Fournierecb83c22020-11-28 15:49:44 +010011653lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010011654{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011655 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011656 int idx;
11657 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011658 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011659 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +010011660 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +010011661 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011662 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +010011663 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011664
Thierry FOURNIER380d0932015-01-23 14:27:52 +010011665 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011666 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +010011667
Thierry Fournier4234dbd2020-11-28 13:18:23 +010011668 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011669 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +010011670 *context = NULL;
11671
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080011672 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +020011673 * the Lua function can fail with an abort. We are in the initialisation
11674 * process of HAProxy, this abort() is tolerated.
11675 */
11676
Thierry Fournier3c539322020-11-28 16:05:05 +010011677 /* Call post initialisation function in safe environment. */
11678 if (setjmp(safe_ljmp_env) != 0) {
11679 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011680 if (lua_type(L, -1) == LUA_TSTRING)
11681 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +010011682 else
11683 error_msg = "critical error";
11684 fprintf(stderr, "Lua init: %s.\n", error_msg);
11685 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +010011686 } else {
11687 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +010011688 }
11689
Thierry FOURNIER380d0932015-01-23 14:27:52 +010011690 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011691 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +010011692#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
11693#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
11694#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011695 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +010011696#endif
11697#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011698 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +010011699#endif
11700#undef HLUA_PREPEND_PATH_TOSTRING
11701#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011702
Thierry Fournier59f11be2020-11-29 00:37:41 +010011703 /* Apply configured prepend path */
11704 list_for_each_entry(pp, &prepend_path_list, l)
11705 hlua_prepend_path(L, pp->type, pp->path);
11706
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011707 /*
11708 *
11709 * Create "core" object.
11710 *
11711 */
11712
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +010011713 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011714 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011715
Thierry Fournierecb83c22020-11-28 15:49:44 +010011716 /* set the thread id */
11717 hlua_class_const_int(L, "thread", thread_num);
11718
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011719 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +010011720 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011721 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010011722
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011723 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011724 hlua_class_function(L, "register_init", hlua_register_init);
11725 hlua_class_function(L, "register_task", hlua_register_task);
11726 hlua_class_function(L, "register_fetches", hlua_register_fetches);
11727 hlua_class_function(L, "register_converters", hlua_register_converters);
11728 hlua_class_function(L, "register_action", hlua_register_action);
11729 hlua_class_function(L, "register_service", hlua_register_service);
11730 hlua_class_function(L, "register_cli", hlua_register_cli);
Christopher Faulet69c581a2021-05-31 08:54:04 +020011731 hlua_class_function(L, "register_filter", hlua_register_filter);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011732 hlua_class_function(L, "yield", hlua_yield);
11733 hlua_class_function(L, "set_nice", hlua_set_nice);
11734 hlua_class_function(L, "sleep", hlua_sleep);
11735 hlua_class_function(L, "msleep", hlua_msleep);
11736 hlua_class_function(L, "add_acl", hlua_add_acl);
11737 hlua_class_function(L, "del_acl", hlua_del_acl);
11738 hlua_class_function(L, "set_map", hlua_set_map);
11739 hlua_class_function(L, "del_map", hlua_del_map);
11740 hlua_class_function(L, "tcp", hlua_socket_new);
William Lallemand3956c4e2021-09-21 16:25:15 +020011741 hlua_class_function(L, "httpclient", hlua_httpclient_new);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011742 hlua_class_function(L, "log", hlua_log);
11743 hlua_class_function(L, "Debug", hlua_log_debug);
11744 hlua_class_function(L, "Info", hlua_log_info);
11745 hlua_class_function(L, "Warning", hlua_log_warning);
11746 hlua_class_function(L, "Alert", hlua_log_alert);
11747 hlua_class_function(L, "done", hlua_done);
11748 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010011749
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011750 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010011751
11752 /*
11753 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +010011754 * Create "act" object.
11755 *
11756 */
11757
11758 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011759 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +010011760
11761 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011762 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
11763 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
11764 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
11765 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
11766 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
11767 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
11768 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
11769 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +010011770
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011771 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +010011772
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011773 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +010011774
11775 /*
11776 *
Christopher Faulet69c581a2021-05-31 08:54:04 +020011777 * Create "Filter" object.
11778 *
11779 */
11780
11781 /* This table entry is the object "filter" base. */
11782 lua_newtable(L);
11783
11784 /* push flags and constants */
11785 hlua_class_const_int(L, "CONTINUE", 1);
11786 hlua_class_const_int(L, "WAIT", 0);
11787 hlua_class_const_int(L, "ERROR", -1);
11788
11789 hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX);
11790
Christopher Fauletc404f112020-02-26 15:03:09 +010011791 hlua_class_function(L, "wake_time", hlua_set_wake_time);
11792 hlua_class_function(L, "register_data_filter", hlua_register_data_filter);
11793 hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter);
11794
Christopher Faulet69c581a2021-05-31 08:54:04 +020011795 lua_setglobal(L, "filter");
11796
11797 /*
11798 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011799 * Register class Map
11800 *
11801 */
11802
11803 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011804 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011805
11806 /* register pattern types. */
11807 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011808 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +010011809 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011810 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011811 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +010011812 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011813
11814 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011815 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011816
11817 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011818 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011819
Ilya Shipitsind4259502020-04-08 01:07:56 +050011820 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011821 lua_pushstring(L, "__index");
11822 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011823
11824 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011825 hlua_class_function(L, "lookup", hlua_map_lookup);
11826 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011827
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011828 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011829
Thierry Fournier45e78d72016-02-19 18:34:46 +010011830 /* Register previous table in the registry with reference and named entry.
11831 * The function hlua_register_metatable() pops the stack, so we
11832 * previously create a copy of the table.
11833 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011834 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
11835 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011836
11837 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011838 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011839
11840 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011841 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +020011842
11843 /*
11844 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011845 * Register class Channel
11846 *
11847 */
11848
11849 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011850 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011851
Ilya Shipitsind4259502020-04-08 01:07:56 +050011852 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011853 lua_pushstring(L, "__index");
11854 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011855
11856 /* Register . */
Christopher Faulet6a79fc12021-08-06 16:02:36 +020011857 hlua_class_function(L, "data", hlua_channel_get_data);
11858 hlua_class_function(L, "line", hlua_channel_get_line);
11859 hlua_class_function(L, "set", hlua_channel_set_data);
11860 hlua_class_function(L, "remove", hlua_channel_del_data);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011861 hlua_class_function(L, "append", hlua_channel_append);
Christopher Faulet6a79fc12021-08-06 16:02:36 +020011862 hlua_class_function(L, "prepend", hlua_channel_prepend);
11863 hlua_class_function(L, "insert", hlua_channel_insert_data);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011864 hlua_class_function(L, "send", hlua_channel_send);
11865 hlua_class_function(L, "forward", hlua_channel_forward);
Christopher Faulet6a79fc12021-08-06 16:02:36 +020011866 hlua_class_function(L, "input", hlua_channel_get_in_len);
11867 hlua_class_function(L, "output", hlua_channel_get_out_len);
11868 hlua_class_function(L, "may_recv", hlua_channel_may_recv);
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011869 hlua_class_function(L, "is_full", hlua_channel_is_full);
11870 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011871
Christopher Faulet6a79fc12021-08-06 16:02:36 +020011872 /* Deprecated API */
11873 hlua_class_function(L, "get", hlua_channel_get);
11874 hlua_class_function(L, "dup", hlua_channel_dup);
11875 hlua_class_function(L, "getline", hlua_channel_getline);
11876 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
11877 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
11878
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011879 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011880
11881 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011882 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010011883
11884 /*
11885 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010011886 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010011887 *
11888 */
11889
11890 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011891 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010011892
Ilya Shipitsind4259502020-04-08 01:07:56 +050011893 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011894 lua_pushstring(L, "__index");
11895 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010011896
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011897 /* Browse existing fetches and create the associated
11898 * object method.
11899 */
11900 sf = NULL;
11901 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011902 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
11903 * by an underscore.
11904 */
Willy Tarreau5ef96562021-08-26 16:48:53 +020011905 strlcpy2(trash.area, sf->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011906 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011907 if (*p == '.' || *p == '-' || *p == '+')
11908 *p = '_';
11909
11910 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011911 lua_pushstring(L, trash.area);
11912 lua_pushlightuserdata(L, sf);
11913 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
11914 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010011915 }
11916
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011917 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010011918
11919 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011920 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010011921
11922 /*
11923 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011924 * Register class Converters
11925 *
11926 */
11927
11928 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011929 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011930
11931 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011932 lua_pushstring(L, "__index");
11933 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011934
11935 /* Browse existing converters and create the associated
11936 * object method.
11937 */
11938 sc = NULL;
11939 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011940 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
11941 * by an underscore.
11942 */
Willy Tarreau5ef96562021-08-26 16:48:53 +020011943 strlcpy2(trash.area, sc->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011944 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011945 if (*p == '.' || *p == '-' || *p == '+')
11946 *p = '_';
11947
11948 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011949 lua_pushstring(L, trash.area);
11950 lua_pushlightuserdata(L, sc);
11951 lua_pushcclosure(L, hlua_run_sample_conv, 1);
11952 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011953 }
11954
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011955 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011956
11957 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011958 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +010011959
11960 /*
11961 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011962 * Register class HTTP
11963 *
11964 */
11965
11966 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011967 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011968
Ilya Shipitsind4259502020-04-08 01:07:56 +050011969 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011970 lua_pushstring(L, "__index");
11971 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011972
11973 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011974 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
11975 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
11976 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
11977 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
11978 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
11979 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
11980 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
11981 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
11982 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
11983 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011984
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011985 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
11986 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
11987 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
11988 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
11989 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
11990 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
11991 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011992
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011993 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011994
11995 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010011996 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010011997
Christopher Fauletdf97ac42020-02-26 16:57:19 +010011998 /*
11999 *
12000 * Register class HTTPMessage
12001 *
12002 */
12003
12004 /* Create and fill the metatable. */
12005 lua_newtable(L);
12006
Ilya Shipitsinff0f2782021-08-22 22:18:07 +050012007 /* Create and fill the __index entry. */
Christopher Fauletdf97ac42020-02-26 16:57:19 +010012008 lua_pushstring(L, "__index");
12009 lua_newtable(L);
12010
12011 /* Register Lua functions. */
12012 hlua_class_function(L, "is_resp", hlua_http_msg_is_resp);
12013 hlua_class_function(L, "get_stline", hlua_http_msg_get_stline);
12014 hlua_class_function(L, "get_headers", hlua_http_msg_get_headers);
12015 hlua_class_function(L, "del_header", hlua_http_msg_del_hdr);
12016 hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr);
12017 hlua_class_function(L, "rep_value", hlua_http_msg_rep_val);
12018 hlua_class_function(L, "add_header", hlua_http_msg_add_hdr);
12019 hlua_class_function(L, "set_header", hlua_http_msg_set_hdr);
12020 hlua_class_function(L, "set_method", hlua_http_msg_set_meth);
12021 hlua_class_function(L, "set_path", hlua_http_msg_set_path);
12022 hlua_class_function(L, "set_query", hlua_http_msg_set_query);
12023 hlua_class_function(L, "set_uri", hlua_http_msg_set_uri);
12024 hlua_class_function(L, "set_status", hlua_http_msg_set_status);
12025 hlua_class_function(L, "is_full", hlua_http_msg_is_full);
12026 hlua_class_function(L, "may_recv", hlua_http_msg_may_recv);
12027 hlua_class_function(L, "eom", hlua_http_msg_is_eom);
12028 hlua_class_function(L, "input", hlua_http_msg_get_in_len);
12029 hlua_class_function(L, "output", hlua_http_msg_get_out_len);
12030
12031 hlua_class_function(L, "body", hlua_http_msg_get_body);
12032 hlua_class_function(L, "set", hlua_http_msg_set_data);
12033 hlua_class_function(L, "remove", hlua_http_msg_del_data);
12034 hlua_class_function(L, "append", hlua_http_msg_append);
12035 hlua_class_function(L, "prepend", hlua_http_msg_prepend);
12036 hlua_class_function(L, "insert", hlua_http_msg_insert_data);
12037 hlua_class_function(L, "set_eom", hlua_http_msg_set_eom);
12038 hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom);
12039
12040 hlua_class_function(L, "send", hlua_http_msg_send);
12041 hlua_class_function(L, "forward", hlua_http_msg_forward);
12042
12043 lua_rawset(L, -3);
12044
12045 /* Register previous table in the registry with reference and named entry. */
12046 class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG);
William Lallemand3956c4e2021-09-21 16:25:15 +020012047
12048 /*
12049 *
12050 * Register class HTTPClient
12051 *
12052 */
12053
12054 /* Create and fill the metatable. */
12055 lua_newtable(L);
12056 lua_pushstring(L, "__index");
12057 lua_newtable(L);
12058 hlua_class_function(L, "get", hlua_httpclient_get);
William Lallemanddc2cc902021-10-26 11:43:26 +020012059 hlua_class_function(L, "head", hlua_httpclient_head);
12060 hlua_class_function(L, "put", hlua_httpclient_put);
12061 hlua_class_function(L, "post", hlua_httpclient_post);
12062 hlua_class_function(L, "delete", hlua_httpclient_delete);
William Lallemand3956c4e2021-09-21 16:25:15 +020012063 lua_settable(L, -3); /* Sets the __index entry. */
William Lallemandf77f1de2021-09-28 19:10:38 +020012064 /* Register the garbage collector entry. */
12065 lua_pushstring(L, "__gc");
12066 lua_pushcclosure(L, hlua_httpclient_gc, 0);
12067 lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */
12068
William Lallemand3956c4e2021-09-21 16:25:15 +020012069
12070
12071 class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT);
Thierry FOURNIER08504f42015-03-16 14:17:08 +010012072 /*
12073 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012074 * Register class AppletTCP
12075 *
12076 */
12077
12078 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012079 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012080
Ilya Shipitsind4259502020-04-08 01:07:56 +050012081 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012082 lua_pushstring(L, "__index");
12083 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012084
12085 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012086 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
12087 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
12088 hlua_class_function(L, "send", hlua_applet_tcp_send);
12089 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
12090 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
12091 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
12092 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
12093 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012094
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012095 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012096
12097 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012098 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020012099
12100 /*
12101 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012102 * Register class AppletHTTP
12103 *
12104 */
12105
12106 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012107 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012108
Ilya Shipitsind4259502020-04-08 01:07:56 +050012109 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012110 lua_pushstring(L, "__index");
12111 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012112
12113 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012114 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
12115 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
12116 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
12117 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
12118 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
12119 hlua_class_function(L, "getline", hlua_applet_http_getline);
12120 hlua_class_function(L, "receive", hlua_applet_http_recv);
12121 hlua_class_function(L, "send", hlua_applet_http_send);
12122 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
12123 hlua_class_function(L, "set_status", hlua_applet_http_status);
12124 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012125
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012126 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012127
12128 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012129 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020012130
12131 /*
12132 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010012133 * Register class TXN
12134 *
12135 */
12136
12137 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012138 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010012139
Ilya Shipitsind4259502020-04-08 01:07:56 +050012140 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012141 lua_pushstring(L, "__index");
12142 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010012143
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +010012144 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012145 hlua_class_function(L, "set_priv", hlua_set_priv);
12146 hlua_class_function(L, "get_priv", hlua_get_priv);
12147 hlua_class_function(L, "set_var", hlua_set_var);
12148 hlua_class_function(L, "unset_var", hlua_unset_var);
12149 hlua_class_function(L, "get_var", hlua_get_var);
12150 hlua_class_function(L, "done", hlua_txn_done);
12151 hlua_class_function(L, "reply", hlua_txn_reply_new);
12152 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
12153 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
12154 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
12155 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
12156 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
12157 hlua_class_function(L, "deflog", hlua_txn_deflog);
12158 hlua_class_function(L, "log", hlua_txn_log);
12159 hlua_class_function(L, "Debug", hlua_txn_log_debug);
12160 hlua_class_function(L, "Info", hlua_txn_log_info);
12161 hlua_class_function(L, "Warning", hlua_txn_log_warning);
12162 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +010012163
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012164 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010012165
12166 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012167 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012168
12169 /*
12170 *
Christopher Faulet700d9e82020-01-31 12:21:52 +010012171 * Register class reply
12172 *
12173 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012174 lua_newtable(L);
12175 lua_pushstring(L, "__index");
12176 lua_newtable(L);
12177 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
12178 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
12179 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
12180 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
12181 lua_settable(L, -3); /* Sets the __index entry. */
12182 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +010012183
12184
12185 /*
12186 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012187 * Register class Socket
12188 *
12189 */
12190
12191 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012192 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012193
Ilya Shipitsind4259502020-04-08 01:07:56 +050012194 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012195 lua_pushstring(L, "__index");
12196 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012197
Baptiste Assmann84bb4932015-03-02 21:40:06 +010012198#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012199 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +010012200#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012201 hlua_class_function(L, "connect", hlua_socket_connect);
12202 hlua_class_function(L, "send", hlua_socket_send);
12203 hlua_class_function(L, "receive", hlua_socket_receive);
12204 hlua_class_function(L, "close", hlua_socket_close);
12205 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
12206 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
12207 hlua_class_function(L, "setoption", hlua_socket_setoption);
12208 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012209
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012210 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012211
12212 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012213 lua_pushstring(L, "__gc");
12214 lua_pushcclosure(L, hlua_socket_gc, 0);
12215 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012216
12217 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010012218 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012219
Thierry Fournieraafc7772020-12-04 11:47:47 +010012220 lua_atpanic(L, hlua_panic_safe);
12221
12222 return L;
12223}
12224
12225void hlua_init(void) {
12226 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010012227 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +010012228#ifdef USE_OPENSSL
12229 struct srv_kw *kw;
12230 int tmp_error;
12231 char *error;
12232 char *args[] = { /* SSL client configuration. */
12233 "ssl",
12234 "verify",
12235 "none",
12236 NULL
12237 };
12238#endif
12239
12240 /* Init post init function list head */
12241 for (i = 0; i < MAX_THREADS + 1; i++)
12242 LIST_INIT(&hlua_init_functions[i]);
12243
12244 /* Init state for common/shared lua parts */
12245 hlua_state_id = 0;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020012246 ha_set_thread(NULL);
Thierry Fournieraafc7772020-12-04 11:47:47 +010012247 hlua_states[0] = hlua_init_state(0);
12248
12249 /* Init state 1 for thread 0. We have at least one thread. */
12250 hlua_state_id = 1;
Willy Tarreau43ab05b2021-09-28 09:43:11 +020012251 ha_set_thread(NULL);
Thierry Fournieraafc7772020-12-04 11:47:47 +010012252 hlua_states[1] = hlua_init_state(1);
12253
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012254 /* Proxy and server configuration initialisation. */
William Lallemand6bb77b92021-07-28 15:48:16 +020012255 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg);
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010012256 if (!socket_proxy) {
12257 fprintf(stderr, "Lua init: %s\n", errmsg);
12258 exit(1);
12259 }
12260 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012261
12262 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010012263 socket_tcp = new_server(socket_proxy);
12264 if (!socket_tcp) {
12265 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
12266 exit(1);
12267 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012268
12269#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012270 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010012271 socket_ssl = new_server(socket_proxy);
12272 if (!socket_ssl) {
12273 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
12274 exit(1);
12275 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012276
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010012277 socket_ssl->use_ssl = 1;
12278 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012279
Bertrand Jacquin80839ff2021-01-21 19:14:46 +000012280 for (i = 0; args[i] != NULL; i++) {
12281 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012282 /*
12283 *
12284 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080012285 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012286 * features like client certificates and ssl_verify.
12287 *
12288 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010012289 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012290 if (tmp_error != 0) {
12291 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
12292 abort(); /* This must be never arrives because the command line
12293 not editable by the user. */
12294 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +000012295 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012296 }
12297 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010012298#endif
Thierry Fournier75933d42016-01-21 09:30:18 +010012299
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010012300}
Willy Tarreaubb57d942016-12-21 19:04:56 +010012301
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +020012302static void hlua_deinit()
12303{
Willy Tarreau186f3762020-12-04 11:48:12 +010012304 int thr;
Christopher Faulet69c581a2021-05-31 08:54:04 +020012305 struct hlua_reg_filter *reg_flt, *reg_flt_bck;
12306
12307 list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l)
12308 release_hlua_reg_filter(reg_flt);
Willy Tarreau186f3762020-12-04 11:48:12 +010012309
12310 for (thr = 0; thr < MAX_THREADS+1; thr++) {
12311 if (hlua_states[thr])
12312 lua_close(hlua_states[thr]);
12313 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +010012314
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +020012315 srv_drop(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +010012316
Willy Tarreau0f143af2021-03-05 10:41:48 +010012317#ifdef USE_OPENSSL
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +020012318 srv_drop(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +010012319#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010012320
12321 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +020012322}
12323
12324REGISTER_POST_DEINIT(hlua_deinit);
12325
Willy Tarreau80713382018-11-26 10:19:54 +010012326static void hlua_register_build_options(void)
12327{
Willy Tarreaubb57d942016-12-21 19:04:56 +010012328 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +010012329
Willy Tarreaubb57d942016-12-21 19:04:56 +010012330 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
12331 hap_register_build_opts(ptr, 1);
12332}
Willy Tarreau80713382018-11-26 10:19:54 +010012333
12334INITCALL0(STG_REGISTER, hlua_register_build_options);