blob: 874da4ed5b01fb6e94906b4e02dad08e5c8d1910 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Bertrand Jacquinf4c12d42021-01-21 21:14:07 +000013#define _GNU_SOURCE
14
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020016#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010017
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010018#include <lauxlib.h>
19#include <lua.h>
20#include <lualib.h>
21
Thierry FOURNIER463119c2015-03-10 00:35:36 +010022#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
23#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010024#endif
25
Willy Tarreau8d2b7772020-05-27 10:58:19 +020026#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010027
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/api.h>
29#include <haproxy/applet.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020030#include <haproxy/arg.h>
Christopher Fauletd25d9262020-08-06 11:04:46 +020031#include <haproxy/auth.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020032#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020033#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020035#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020036#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020037#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020038#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020039#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020040#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020042#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020043#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020044#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020045#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020046#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020047#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020048#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020050#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020051#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020052#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020053#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020054#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020055#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020056#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020057#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020059#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020060#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020061#include <haproxy/xref.h>
62
Thierry FOURNIER380d0932015-01-23 14:27:52 +010063
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010064/* Lua uses longjmp to perform yield or throwing errors. This
65 * macro is used only for identifying the function that can
66 * not return because a longjmp is executed.
67 * __LJMP marks a prototype of hlua file that can use longjmp.
68 * WILL_LJMP() marks an lua function that will use longjmp.
69 * MAY_LJMP() marks an lua function that may use longjmp.
70 */
71#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020072#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010073#define MAY_LJMP(func) func
74
Thierry FOURNIERbabae282015-09-17 11:36:37 +020075/* This couple of function executes securely some Lua calls outside of
76 * the lua runtime environment. Each Lua call can return a longjmp
77 * if it encounter a memory error.
78 *
79 * Lua documentation extract:
80 *
81 * If an error happens outside any protected environment, Lua calls
82 * a panic function (see lua_atpanic) and then calls abort, thus
83 * exiting the host application. Your panic function can avoid this
84 * exit by never returning (e.g., doing a long jump to your own
85 * recovery point outside Lua).
86 *
87 * The panic function runs as if it were a message handler (see
88 * §2.3); in particular, the error message is at the top of the
89 * stack. However, there is no guarantee about stack space. To push
90 * anything on the stack, the panic function must first check the
91 * available space (see §4.2).
92 *
93 * We must check all the Lua entry point. This includes:
94 * - The include/proto/hlua.h exported functions
95 * - the task wrapper function
96 * - The action wrapper function
97 * - The converters wrapper function
98 * - The sample-fetch wrapper functions
99 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500100 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800101 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200102 *
103 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
104 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
105 * because they must be exists in the program stack when the longjmp
106 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200107 *
108 * Note that the Lua processing is not really thread safe. It provides
109 * heavy system which consists to add our own lock function in the Lua
110 * code and recompile the library. This system will probably not accepted
111 * by maintainers of various distribs.
112 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500113 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200114 * quick looking on the Lua sources displays a lua_lock() a the start
115 * of function and a lua_unlock() at the end of the function. So I
116 * conclude that the Lua thread safe mode just perform a mutex around
117 * all execution. So I prefer to do this in the HAProxy code, it will be
118 * easier for distro maintainers.
119 *
120 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
121 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
122 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100124__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200125THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100127static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100129/* This is the chained list of struct hlua_function referenced
130 * for haproxy action, sample-fetches, converters, cli and
131 * applet bindings. It is used for a post-initialisation control.
132 */
133static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions);
134
Thierry Fournierc7492592020-11-28 23:57:24 +0100135/* This variable is used only during initialization to identify the Lua state
136 * currently being initialized. 0 is the common lua state, 1 to n are the Lua
137 * states dedicated to each thread (in this case hlua_state_id==tid+1).
138 */
139static int hlua_state_id;
140
Thierry Fournier59f11be2020-11-29 00:37:41 +0100141/* This is a NULL-terminated list of lua file which are referenced to load per thread */
142static char **per_thread_load = NULL;
143
144lua_State *hlua_init_state(int thread_id);
145
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100146#define SET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200147 ({ \
148 int ret; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100149 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100150 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 if (setjmp(safe_ljmp_env) != 0) { \
152 lua_atpanic(__L, hlua_panic_safe); \
153 ret = 0; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100154 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100155 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200156 } else { \
157 lua_atpanic(__L, hlua_panic_ljmp); \
158 ret = 1; \
159 } \
160 ret; \
161 })
162
163/* If we are the last function catching Lua errors, we
164 * must reset the panic function.
165 */
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100166#define RESET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200167 do { \
168 lua_atpanic(__L, hlua_panic_safe); \
Thierry Fournier021d9862020-11-28 23:42:03 +0100169 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100170 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200171 } while(0)
172
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100173#define SET_SAFE_LJMP(__HLUA) \
174 SET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
175
176#define RESET_SAFE_LJMP(__HLUA) \
177 RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
178
179#define SET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100180 SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100181
182#define RESET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100183 RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100184
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200185/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200186#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100187/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200189/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100190#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100191#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200192
Thierry Fournierafc63e22020-11-28 17:06:51 +0100193/* The main Lua execution context. The 0 index is the
194 * common state shared by all threads.
195 */
Willy Tarreau186f3762020-12-04 11:48:12 +0100196static lua_State *hlua_states[MAX_THREADS + 1];
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100197
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100198/* This is the memory pool containing struct lua for applets
199 * (including cli).
200 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100201DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100202
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100203/* Used for Socket connection. */
204static struct proxy socket_proxy;
205static struct server socket_tcp;
206#ifdef USE_OPENSSL
207static struct server socket_ssl;
208#endif
209
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100210/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100211struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100212
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100213/* The following variables contains the reference of the different
214 * Lua classes. These references are useful for identify metadata
215 * associated with an object.
216 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100217static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100218static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100219static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100220static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100221static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100222static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200223static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200224static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200225static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100226static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100227
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100228/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200229 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100230 * short timeout. Lua linked with tasks doesn't have a timeout
231 * because a task may remain alive during all the haproxy execution.
232 */
233static unsigned int hlua_timeout_session = 4000; /* session timeout. */
234static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200235static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100236
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100237/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
238 * it is used for preventing infinite loops.
239 *
240 * I test the scheer with an infinite loop containing one incrementation
241 * and one test. I run this loop between 10 seconds, I raise a ceil of
242 * 710M loops from one interrupt each 9000 instructions, so I fix the value
243 * to one interrupt each 10 000 instructions.
244 *
245 * configured | Number of
246 * instructions | loops executed
247 * between two | in milions
248 * forced yields |
249 * ---------------+---------------
250 * 10 | 160
251 * 500 | 670
252 * 1000 | 680
253 * 5000 | 700
254 * 7000 | 700
255 * 8000 | 700
256 * 9000 | 710 <- ceil
257 * 10000 | 710
258 * 100000 | 710
259 * 1000000 | 710
260 *
261 */
262static unsigned int hlua_nb_instruction = 10000;
263
Willy Tarreaucdb53462020-12-02 12:12:00 +0100264/* Descriptor for the memory allocation state. The limit is pre-initialised to
265 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
266 * is replaced with ~0 during post_init after everything was loaded. This way
267 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
268 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100269 */
270struct hlua_mem_allocator {
271 size_t allocated;
272 size_t limit;
273};
274
Willy Tarreaucdb53462020-12-02 12:12:00 +0100275static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100276
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100277/* These functions converts types between HAProxy internal args or
278 * sample and LUA types. Another function permits to check if the
279 * LUA stack contains arguments according with an required ARG_T
280 * format.
281 */
282static int hlua_arg2lua(lua_State *L, const struct arg *arg);
283static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100284__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100285 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100286static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100287static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100288static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
289
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100290__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200291
Thierry Fournier59f11be2020-11-29 00:37:41 +0100292struct prepend_path {
293 struct list l;
294 char *type;
295 char *path;
296};
297
298static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
299
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200300#define SEND_ERR(__be, __fmt, __args...) \
301 do { \
302 send_log(__be, LOG_ERR, __fmt, ## __args); \
303 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100304 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200305 } while (0)
306
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100307static inline struct hlua_function *new_hlua_function()
308{
309 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100310 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100311
312 fcn = calloc(1, sizeof(*fcn));
313 if (!fcn)
314 return NULL;
315 LIST_ADDQ(&referenced_functions, &fcn->l);
Thierry Fournierc7492592020-11-28 23:57:24 +0100316 for (i = 0; i < MAX_THREADS + 1; i++)
317 fcn->function_ref[i] = -1;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100318 return fcn;
319}
320
Thierry Fournierc7492592020-11-28 23:57:24 +0100321/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
322static inline int fcn_ref_to_stack_id(struct hlua_function *fcn)
323{
324 if (fcn->function_ref[0] == -1)
325 return tid + 1;
326 return 0;
327}
328
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100329/* Used to check an Lua function type in the stack. It creates and
330 * returns a reference of the function. This function throws an
331 * error if the rgument is not a "function".
332 */
333__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
334{
335 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100336 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100337 WILL_LJMP(luaL_argerror(L, argno, msg));
338 }
339 lua_pushvalue(L, argno);
340 return luaL_ref(L, LUA_REGISTRYINDEX);
341}
342
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200343/* Return the string that is of the top of the stack. */
344const char *hlua_get_top_error_string(lua_State *L)
345{
346 if (lua_gettop(L) < 1)
347 return "unknown error";
348 if (lua_type(L, -1) != LUA_TSTRING)
349 return "unknown error";
350 return lua_tostring(L, -1);
351}
352
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200353__LJMP static const char *hlua_traceback(lua_State *L)
354{
355 lua_Debug ar;
356 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200357 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200358 int filled = 0;
359
360 while (lua_getstack(L, level++, &ar)) {
361
362 /* Add separator */
363 if (filled)
364 chunk_appendf(msg, ", ");
365 filled = 1;
366
367 /* Fill fields:
368 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
369 * 'l': fills in the field currentline;
370 * 'n': fills in the field name and namewhat;
371 * 't': fills in the field istailcall;
372 */
373 lua_getinfo(L, "Slnt", &ar);
374
375 /* Append code localisation */
376 if (ar.currentline > 0)
377 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
378 else
379 chunk_appendf(msg, "%s ", ar.short_src);
380
381 /*
382 * Get function name
383 *
384 * if namewhat is no empty, name is defined.
385 * what contains "Lua" for Lua function, "C" for C function,
386 * or "main" for main code.
387 */
388 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
389 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
390
391 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
392 chunk_appendf(msg, "main chunk");
393
394 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
395 chunk_appendf(msg, "C function line %d", ar.linedefined);
396
397 else /* nothing left... */
398 chunk_appendf(msg, "?");
399
400
401 /* Display tailed call */
402 if (ar.istailcall)
403 chunk_appendf(msg, " ...");
404 }
405
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200406 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200407}
408
409
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100410/* This function check the number of arguments available in the
411 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500412 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100413 */
414__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
415{
416 if (lua_gettop(L) == nb)
417 return;
418 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
419}
420
Mark Lakes22154b42018-01-29 14:38:40 -0800421/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100422 * and the line number where the error is encountered.
423 */
424static int hlua_pusherror(lua_State *L, const char *fmt, ...)
425{
426 va_list argp;
427 va_start(argp, fmt);
428 luaL_where(L, 1);
429 lua_pushvfstring(L, fmt, argp);
430 va_end(argp);
431 lua_concat(L, 2);
432 return 1;
433}
434
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100435/* This functions is used with sample fetch and converters. It
436 * converts the HAProxy configuration argument in a lua stack
437 * values.
438 *
439 * It takes an array of "arg", and each entry of the array is
440 * converted and pushed in the LUA stack.
441 */
442static int hlua_arg2lua(lua_State *L, const struct arg *arg)
443{
444 switch (arg->type) {
445 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case ARGT_TIME:
447 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100448 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 break;
450
451 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 break;
454
455 case ARGT_IPV4:
456 case ARGT_IPV6:
457 case ARGT_MSK4:
458 case ARGT_MSK6:
459 case ARGT_FE:
460 case ARGT_BE:
461 case ARGT_TAB:
462 case ARGT_SRV:
463 case ARGT_USR:
464 case ARGT_MAP:
465 default:
466 lua_pushnil(L);
467 break;
468 }
469 return 1;
470}
471
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500472/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100473 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500474 * with sample fetch wrappers. The input arguments are given to the
475 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100476 */
477static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
478{
479 switch (lua_type(L, ud)) {
480
481 case LUA_TNUMBER:
482 case LUA_TBOOLEAN:
483 arg->type = ARGT_SINT;
484 arg->data.sint = lua_tointeger(L, ud);
485 break;
486
487 case LUA_TSTRING:
488 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200489 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200490 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200491 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200492 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100493 break;
494
495 case LUA_TUSERDATA:
496 case LUA_TNIL:
497 case LUA_TTABLE:
498 case LUA_TFUNCTION:
499 case LUA_TTHREAD:
500 case LUA_TLIGHTUSERDATA:
501 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200502 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100503 break;
504 }
505 return 1;
506}
507
508/* the following functions are used to convert a struct sample
509 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500510 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100511 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100512static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100513{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200514 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100515 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100516 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200517 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100518 break;
519
520 case SMP_T_BIN:
521 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200522 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100523 break;
524
525 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200526 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100527 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
528 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
529 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
530 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
531 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
532 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
533 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
534 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
535 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200536 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100537 break;
538 default:
539 lua_pushnil(L);
540 break;
541 }
542 break;
543
544 case SMP_T_IPV4:
545 case SMP_T_IPV6:
546 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200547 if (sample_casts[smp->data.type][SMP_T_STR] &&
548 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200549 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100550 else
551 lua_pushnil(L);
552 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100553 default:
554 lua_pushnil(L);
555 break;
556 }
557 return 1;
558}
559
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100560/* the following functions are used to convert a struct sample
561 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500562 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100563 */
564static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
565{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200566 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100567
568 case SMP_T_BIN:
569 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200570 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100571 break;
572
573 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200574 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100575 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
576 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
577 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
578 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
579 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
580 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
581 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
582 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
583 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200584 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100585 break;
586 default:
587 lua_pushstring(L, "");
588 break;
589 }
590 break;
591
592 case SMP_T_SINT:
593 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100594 case SMP_T_IPV4:
595 case SMP_T_IPV6:
596 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200597 if (sample_casts[smp->data.type][SMP_T_STR] &&
598 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200599 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100600 else
601 lua_pushstring(L, "");
602 break;
603 default:
604 lua_pushstring(L, "");
605 break;
606 }
607 return 1;
608}
609
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610/* the following functions are used to convert an Lua type in a
611 * struct sample. This is useful to provide data from a converter
612 * to the LUA code.
613 */
614static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
615{
616 switch (lua_type(L, ud)) {
617
618 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200619 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200620 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100621 break;
622
623
624 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200625 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200626 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100627 break;
628
629 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200630 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100631 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200632 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200633 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200634 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200635 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100636 break;
637
638 case LUA_TUSERDATA:
639 case LUA_TNIL:
640 case LUA_TTABLE:
641 case LUA_TFUNCTION:
642 case LUA_TTHREAD:
643 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200644 case LUA_TNONE:
645 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200646 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200647 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100648 break;
649 }
650 return 1;
651}
652
Ilya Shipitsind4259502020-04-08 01:07:56 +0500653/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800654 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100655 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100656 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500657 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100658 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100659 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100660__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100661 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100662{
663 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200664 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100665 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200666 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200667 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200668 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200669 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100670
671 idx = 0;
672 min_arg = ARGM(mask);
673 mask >>= ARGM_BITS;
674
675 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200676 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100677
678 /* Check oversize. */
679 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200680 msg = "Malformed argument mask";
681 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100682 }
683
684 /* Check for mandatory arguments. */
685 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100686 if (idx < min_arg) {
687
688 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200689 if (idx > 0) {
690 msg = "Mandatory argument expected";
691 goto error;
692 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100693
694 /* If first argument have a certain type, some default values
695 * may be used. See the function smp_resolve_args().
696 */
697 switch (mask & ARGT_MASK) {
698
699 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200700 if (!(p->cap & PR_CAP_FE)) {
701 msg = "Mandatory argument expected";
702 goto error;
703 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100704 argp[idx].data.prx = p;
705 argp[idx].type = ARGT_FE;
706 argp[idx+1].type = ARGT_STOP;
707 break;
708
709 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200710 if (!(p->cap & PR_CAP_BE)) {
711 msg = "Mandatory argument expected";
712 goto error;
713 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100714 argp[idx].data.prx = p;
715 argp[idx].type = ARGT_BE;
716 argp[idx+1].type = ARGT_STOP;
717 break;
718
719 case ARGT_TAB:
720 argp[idx].data.prx = p;
721 argp[idx].type = ARGT_TAB;
722 argp[idx+1].type = ARGT_STOP;
723 break;
724
725 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200726 msg = "Mandatory argument expected";
727 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100728 break;
729 }
730 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200731 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100732 }
733
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500734 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100735 if ((mask & ARGT_MASK) == ARGT_STOP &&
736 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200737 msg = "Last argument expected";
738 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100739 }
740
741 if ((mask & ARGT_MASK) == ARGT_STOP &&
742 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200743 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100744 }
745
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200746 /* Convert some argument types. All string in argp[] are for not
747 * duplicated yet.
748 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100749 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100750 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200751 if (argp[idx].type != ARGT_SINT) {
752 msg = "integer expected";
753 goto error;
754 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100755 argp[idx].type = ARGT_SINT;
756 break;
757
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100758 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200759 if (argp[idx].type != ARGT_SINT) {
760 msg = "integer expected";
761 goto error;
762 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200763 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100764 break;
765
766 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200767 if (argp[idx].type != ARGT_SINT) {
768 msg = "integer expected";
769 goto error;
770 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200771 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100772 break;
773
774 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200775 if (argp[idx].type != ARGT_STR) {
776 msg = "string expected";
777 goto error;
778 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200779 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200780 if (!argp[idx].data.prx) {
781 msg = "frontend doesn't exist";
782 goto error;
783 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100784 argp[idx].type = ARGT_FE;
785 break;
786
787 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200788 if (argp[idx].type != ARGT_STR) {
789 msg = "string expected";
790 goto error;
791 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200792 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200793 if (!argp[idx].data.prx) {
794 msg = "backend doesn't exist";
795 goto error;
796 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100797 argp[idx].type = ARGT_BE;
798 break;
799
800 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200801 if (argp[idx].type != ARGT_STR) {
802 msg = "string expected";
803 goto error;
804 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200805 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200806 if (!argp[idx].data.t) {
807 msg = "table doesn't exist";
808 goto error;
809 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100810 argp[idx].type = ARGT_TAB;
811 break;
812
813 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200814 if (argp[idx].type != ARGT_STR) {
815 msg = "string expected";
816 goto error;
817 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200818 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100819 if (sname) {
820 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200821 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200822 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200823 if (!px) {
824 msg = "backend doesn't exist";
825 goto error;
826 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100827 }
828 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200829 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100830 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100831 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100832 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200833 if (!argp[idx].data.srv) {
834 msg = "server doesn't exist";
835 goto error;
836 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100837 argp[idx].type = ARGT_SRV;
838 break;
839
840 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200841 if (argp[idx].type != ARGT_STR) {
842 msg = "string expected";
843 goto error;
844 }
845 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
846 msg = "invalid IPv4 address";
847 goto error;
848 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100849 argp[idx].type = ARGT_IPV4;
850 break;
851
852 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200853 if (argp[idx].type == ARGT_SINT)
854 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
855 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200856 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
857 msg = "invalid IPv4 mask";
858 goto error;
859 }
860 }
861 else {
862 msg = "integer or string expected";
863 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200864 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100865 argp[idx].type = ARGT_MSK4;
866 break;
867
868 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200869 if (argp[idx].type != ARGT_STR) {
870 msg = "string expected";
871 goto error;
872 }
873 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
874 msg = "invalid IPv6 address";
875 goto error;
876 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100877 argp[idx].type = ARGT_IPV6;
878 break;
879
880 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200881 if (argp[idx].type == ARGT_SINT)
882 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
883 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200884 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
885 msg = "invalid IPv6 mask";
886 goto error;
887 }
888 }
889 else {
890 msg = "integer or string expected";
891 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200892 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100893 argp[idx].type = ARGT_MSK6;
894 break;
895
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200896 case ARGT_REG:
897 if (argp[idx].type != ARGT_STR) {
898 msg = "string expected";
899 goto error;
900 }
901 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
902 if (!reg) {
903 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
904 argp[idx].data.str.area, err);
905 free(err);
906 goto error;
907 }
908 argp[idx].type = ARGT_REG;
909 argp[idx].data.reg = reg;
910 break;
911
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100912 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200913 if (argp[idx].type != ARGT_STR) {
914 msg = "string expected";
915 goto error;
916 }
917 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100918 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200919 ul = p->uri_auth->userlist;
920 else
921 ul = auth_find_userlist(argp[idx].data.str.area);
922
923 if (!ul) {
924 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
925 goto error;
926 }
927 argp[idx].type = ARGT_USR;
928 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200929 break;
930
931 case ARGT_STR:
932 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
933 msg = "unable to duplicate string arg";
934 goto error;
935 }
936 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100937 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200938
Christopher Fauletd25d9262020-08-06 11:04:46 +0200939 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200940 msg = "type not yet supported";
941 goto error;
942 break;
943
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100944 }
945
946 /* Check for type of argument. */
947 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200948 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
949 arg_type_names[(mask & ARGT_MASK)],
950 arg_type_names[argp[idx].type & ARGT_MASK]);
951 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100952 }
953
954 /* Next argument. */
955 mask >>= ARGT_BITS;
956 idx++;
957 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200958 return 0;
959
960 error:
961 for (i = 0; i < idx; i++) {
962 if (argp[i].type == ARGT_STR)
963 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200964 else if (argp[i].type == ARGT_REG)
965 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200966 }
967 WILL_LJMP(luaL_argerror(L, first + idx, msg));
968 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100969}
970
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100971/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500972 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100973 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100974 *
975 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100976 * - hlua_sethlua : create the association between hlua context and lua_state.
977 */
978static inline struct hlua *hlua_gethlua(lua_State *L)
979{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100980 struct hlua **hlua = lua_getextraspace(L);
981 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100982}
983static inline void hlua_sethlua(struct hlua *hlua)
984{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100985 struct hlua **hlua_store = lua_getextraspace(hlua->T);
986 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100987}
988
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100989/* This function is used to send logs. It try to send on screen (stderr)
990 * and on the default syslog server.
991 */
992static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
993{
994 struct tm tm;
995 char *p;
996
997 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200998 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100999 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001000 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001001 /* Break the message if exceed the buffer size. */
1002 *(p-4) = ' ';
1003 *(p-3) = '.';
1004 *(p-2) = '.';
1005 *(p-1) = '.';
1006 break;
1007 }
Willy Tarreau90807112020-02-25 08:16:33 +01001008 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001009 *p = *msg;
1010 else
1011 *p = '.';
1012 }
1013 *p = '\0';
1014
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001015 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001016 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001017 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1018 return;
1019
Willy Tarreaua678b432015-08-28 10:14:59 +02001020 get_localtime(date.tv_sec, &tm);
1021 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001022 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001023 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001024 fflush(stderr);
1025 }
1026}
1027
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001028/* This function just ensure that the yield will be always
1029 * returned with a timeout and permit to set some flags
1030 */
1031__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001032 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001033{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001034 struct hlua *hlua;
1035
1036 /* Get hlua struct, or NULL if we execute from main lua state */
1037 hlua = hlua_gethlua(L);
1038 if (!hlua) {
1039 return;
1040 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001041
1042 /* Set the wake timeout. If timeout is required, we set
1043 * the expiration time.
1044 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001045 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001046
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001047 hlua->flags |= flags;
1048
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001049 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001050 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001051}
1052
Willy Tarreau87b09662015-04-03 00:22:06 +02001053/* This function initialises the Lua environment stored in the stream.
1054 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001056 *
1057 * This function is particular. it initialises a new Lua thread. If the
1058 * initialisation fails (example: out of memory error), the lua function
1059 * throws an error (longjmp).
1060 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001061 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001062 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001063 * threads appear, the safe environment set a lock to ensure only one
1064 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001065 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001066 *
1067 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001068 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001069 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001070 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001071 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001072 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001073 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001074 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001075int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001076{
1077 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001078 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001079 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001080 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001081 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001082 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001083 if (!already_safe) {
1084 if (!SET_SAFE_LJMP_PARENT(lua)) {
1085 lua->Tref = LUA_REFNIL;
1086 return 0;
1087 }
1088 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001089 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 if (!lua->T) {
1091 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001092 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001093 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001094 return 0;
1095 }
1096 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001097 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001098 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001099 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001100 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001101 return 1;
1102}
1103
Willy Tarreau87b09662015-04-03 00:22:06 +02001104/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001105 * is destroyed. The destroy also the memory context. The struct "lua"
1106 * is not freed.
1107 */
1108void hlua_ctx_destroy(struct hlua *lua)
1109{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001110 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001111 return;
1112
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001113 if (!lua->T)
1114 goto end;
1115
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001116 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001117 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001118
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001119 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001120 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001121 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001122 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001123
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001124 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001125 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001126 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001127 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001128 /* Forces a garbage collecting process. If the Lua program is finished
1129 * without error, we run the GC on the thread pointer. Its freed all
1130 * the unused memory.
1131 * If the thread is finnish with an error or is currently yielded,
1132 * it seems that the GC applied on the thread doesn't clean anything,
1133 * so e run the GC on the main thread.
1134 * NOTE: maybe this action locks all the Lua threads untiml the en of
1135 * the garbage collection.
1136 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001137 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001138 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001139 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001140 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001141 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001142 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001143
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001144 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001145
1146end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001147 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001148}
1149
1150/* This function is used to restore the Lua context when a coroutine
1151 * fails. This function copy the common memory between old coroutine
1152 * and the new coroutine. The old coroutine is destroyed, and its
1153 * replaced by the new coroutine.
1154 * If the flag "keep_msg" is set, the last entry of the old is assumed
1155 * as string error message and it is copied in the new stack.
1156 */
1157static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1158{
1159 lua_State *T;
1160 int new_ref;
1161
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001163 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 if (!T)
1165 return 0;
1166
1167 /* Copy last error message. */
1168 if (keep_msg)
1169 lua_xmove(lua->T, T, 1);
1170
1171 /* Copy data between the coroutines. */
1172 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1173 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001174 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001175
1176 /* Destroy old data. */
1177 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1178
1179 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001180 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001181
1182 /* Fill the struct with the new coroutine values. */
1183 lua->Mref = new_ref;
1184 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001185 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001186
1187 /* Set context. */
1188 hlua_sethlua(lua);
1189
1190 return 1;
1191}
1192
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001193void hlua_hook(lua_State *L, lua_Debug *ar)
1194{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001195 struct hlua *hlua;
1196
1197 /* Get hlua struct, or NULL if we execute from main lua state */
1198 hlua = hlua_gethlua(L);
1199 if (!hlua)
1200 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001201
1202 /* Lua cannot yield when its returning from a function,
1203 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001204 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001205 */
1206 if (lua_gethookmask(L) & LUA_MASKRET) {
1207 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1208 return;
1209 }
1210
1211 /* restore the interrupt condition. */
1212 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1213
1214 /* If we interrupt the Lua processing in yieldable state, we yield.
1215 * If the state is not yieldable, trying yield causes an error.
1216 */
1217 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001218 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001219
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001220 /* If we cannot yield, update the clock and check the timeout. */
1221 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001222 hlua->run_time += now_ms - hlua->start_time;
1223 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001224 lua_pushfstring(L, "execution timeout");
1225 WILL_LJMP(lua_error(L));
1226 }
1227
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001228 /* Update the start time. */
1229 hlua->start_time = now_ms;
1230
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001231 /* Try to interrupt the process at the end of the current
1232 * unyieldable function.
1233 */
1234 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001235}
1236
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001237/* This function start or resumes the Lua stack execution. If the flag
1238 * "yield_allowed" if no set and the LUA stack execution returns a yield
1239 * The function return an error.
1240 *
1241 * The function can returns 4 values:
1242 * - HLUA_E_OK : The execution is terminated without any errors.
1243 * - HLUA_E_AGAIN : The execution must continue at the next associated
1244 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001245 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001246 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001247 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001248 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001249 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001250 * LUA code.
1251 */
1252static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1253{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001254#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1255 int nres;
1256#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001257 int ret;
1258 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001259 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001260
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001261 /* Initialise run time counter. */
1262 if (!HLUA_IS_RUNNING(lua))
1263 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001264
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001265 /* Lock the whole Lua execution. This lock must be before the
1266 * label "resume_execution".
1267 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001268 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001269 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001270
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001271resume_execution:
1272
1273 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1274 * instructions. it is used for preventing infinite loops.
1275 */
1276 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1277
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001278 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001279 HLUA_SET_RUN(lua);
1280 HLUA_CLR_CTRLYIELD(lua);
1281 HLUA_CLR_WAKERESWR(lua);
1282 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001283
Christopher Fauletbc275a92020-02-26 14:55:16 +01001284 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001285 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001286 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001287
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001288 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001289#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001290 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001291#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001292 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001293#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001294 switch (ret) {
1295
1296 case LUA_OK:
1297 ret = HLUA_E_OK;
1298 break;
1299
1300 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001301 /* Check if the execution timeout is expired. It it is the case, we
1302 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001303 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001304 tv_update_date(0, 1);
1305 lua->run_time += now_ms - lua->start_time;
1306 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001307 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001308 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001309 break;
1310 }
1311 /* Process the forced yield. if the general yield is not allowed or
1312 * if no task were associated this the current Lua execution
1313 * coroutine, we resume the execution. Else we want to return in the
1314 * scheduler and we want to be waked up again, to continue the
1315 * current Lua execution. So we schedule our own task.
1316 */
1317 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001318 if (!yield_allowed || !lua->task)
1319 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001320 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001321 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001322 if (!yield_allowed) {
1323 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001324 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001325 break;
1326 }
1327 ret = HLUA_E_AGAIN;
1328 break;
1329
1330 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001331
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001332 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001333 * because the errors ares the only one mean to return immediately
1334 * from and lua execution.
1335 */
1336 if (lua->flags & HLUA_EXIT) {
1337 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001338 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001339 break;
1340 }
1341
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001342 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001343 if (!lua_checkstack(lua->T, 1)) {
1344 ret = HLUA_E_ERR;
1345 break;
1346 }
1347 msg = lua_tostring(lua->T, -1);
1348 lua_settop(lua->T, 0); /* Empty the stack. */
1349 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001350 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001351 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001352 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001353 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001354 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001355 ret = HLUA_E_ERRMSG;
1356 break;
1357
1358 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001359 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001360 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001361 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001362 break;
1363
1364 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001365 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001366 if (!lua_checkstack(lua->T, 1)) {
1367 ret = HLUA_E_ERR;
1368 break;
1369 }
1370 msg = lua_tostring(lua->T, -1);
1371 lua_settop(lua->T, 0); /* Empty the stack. */
1372 lua_pop(lua->T, 1);
1373 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001374 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001375 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001376 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001377 ret = HLUA_E_ERRMSG;
1378 break;
1379
1380 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001381 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001382 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001383 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001384 break;
1385 }
1386
1387 switch (ret) {
1388 case HLUA_E_AGAIN:
1389 break;
1390
1391 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001392 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001393 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001394 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001395 break;
1396
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001397 case HLUA_E_ETMOUT:
1398 case HLUA_E_NOMEM:
1399 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001400 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001401 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001402 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001403 hlua_ctx_renew(lua, 0);
1404 break;
1405
1406 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001407 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001408 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001409 break;
1410 }
1411
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001412 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001413 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001414 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001415
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001416 return ret;
1417}
1418
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001419/* This function exit the current code. */
1420__LJMP static int hlua_done(lua_State *L)
1421{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001422 struct hlua *hlua;
1423
1424 /* Get hlua struct, or NULL if we execute from main lua state */
1425 hlua = hlua_gethlua(L);
1426 if (!hlua)
1427 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001428
1429 hlua->flags |= HLUA_EXIT;
1430 WILL_LJMP(lua_error(L));
1431
1432 return 0;
1433}
1434
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001435/* This function is an LUA binding. It provides a function
1436 * for deleting ACL from a referenced ACL file.
1437 */
1438__LJMP static int hlua_del_acl(lua_State *L)
1439{
1440 const char *name;
1441 const char *key;
1442 struct pat_ref *ref;
1443
1444 MAY_LJMP(check_args(L, 2, "del_acl"));
1445
1446 name = MAY_LJMP(luaL_checkstring(L, 1));
1447 key = MAY_LJMP(luaL_checkstring(L, 2));
1448
1449 ref = pat_ref_lookup(name);
1450 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001451 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001452
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001453 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001454 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001455 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001456 return 0;
1457}
1458
1459/* This function is an LUA binding. It provides a function
1460 * for deleting map entry from a referenced map file.
1461 */
1462static int hlua_del_map(lua_State *L)
1463{
1464 const char *name;
1465 const char *key;
1466 struct pat_ref *ref;
1467
1468 MAY_LJMP(check_args(L, 2, "del_map"));
1469
1470 name = MAY_LJMP(luaL_checkstring(L, 1));
1471 key = MAY_LJMP(luaL_checkstring(L, 2));
1472
1473 ref = pat_ref_lookup(name);
1474 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001475 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001476
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001477 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001478 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001479 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001480 return 0;
1481}
1482
1483/* This function is an LUA binding. It provides a function
1484 * for adding ACL pattern from a referenced ACL file.
1485 */
1486static int hlua_add_acl(lua_State *L)
1487{
1488 const char *name;
1489 const char *key;
1490 struct pat_ref *ref;
1491
1492 MAY_LJMP(check_args(L, 2, "add_acl"));
1493
1494 name = MAY_LJMP(luaL_checkstring(L, 1));
1495 key = MAY_LJMP(luaL_checkstring(L, 2));
1496
1497 ref = pat_ref_lookup(name);
1498 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001499 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001500
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001501 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001502 if (pat_ref_find_elt(ref, key) == NULL)
1503 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001504 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001505 return 0;
1506}
1507
1508/* This function is an LUA binding. It provides a function
1509 * for setting map pattern and sample from a referenced map
1510 * file.
1511 */
1512static int hlua_set_map(lua_State *L)
1513{
1514 const char *name;
1515 const char *key;
1516 const char *value;
1517 struct pat_ref *ref;
1518
1519 MAY_LJMP(check_args(L, 3, "set_map"));
1520
1521 name = MAY_LJMP(luaL_checkstring(L, 1));
1522 key = MAY_LJMP(luaL_checkstring(L, 2));
1523 value = MAY_LJMP(luaL_checkstring(L, 3));
1524
1525 ref = pat_ref_lookup(name);
1526 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001527 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001528
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001529 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001530 if (pat_ref_find_elt(ref, key) != NULL)
1531 pat_ref_set(ref, key, value, NULL);
1532 else
1533 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001534 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001535 return 0;
1536}
1537
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001538/* A class is a lot of memory that contain data. This data can be a table,
1539 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001540 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001541 * the name of the object (_G[<name>] = <metable> ).
1542 *
1543 * A metable is a table that modify the standard behavior of a standard
1544 * access to the associated data. The entries of this new metatable are
1545 * defined as is:
1546 *
1547 * http://lua-users.org/wiki/MetatableEvents
1548 *
1549 * __index
1550 *
1551 * we access an absent field in a table, the result is nil. This is
1552 * true, but it is not the whole truth. Actually, such access triggers
1553 * the interpreter to look for an __index metamethod: If there is no
1554 * such method, as usually happens, then the access results in nil;
1555 * otherwise, the metamethod will provide the result.
1556 *
1557 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1558 * the key does not appear in the table, but the metatable has an __index
1559 * property:
1560 *
1561 * - if the value is a function, the function is called, passing in the
1562 * table and the key; the return value of that function is returned as
1563 * the result.
1564 *
1565 * - if the value is another table, the value of the key in that table is
1566 * asked for and returned (and if it doesn't exist in that table, but that
1567 * table's metatable has an __index property, then it continues on up)
1568 *
1569 * - Use "rawget(myTable,key)" to skip this metamethod.
1570 *
1571 * http://www.lua.org/pil/13.4.1.html
1572 *
1573 * __newindex
1574 *
1575 * Like __index, but control property assignment.
1576 *
1577 * __mode - Control weak references. A string value with one or both
1578 * of the characters 'k' and 'v' which specifies that the the
1579 * keys and/or values in the table are weak references.
1580 *
1581 * __call - Treat a table like a function. When a table is followed by
1582 * parenthesis such as "myTable( 'foo' )" and the metatable has
1583 * a __call key pointing to a function, that function is invoked
1584 * (passing any specified arguments) and the return value is
1585 * returned.
1586 *
1587 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1588 * called, if the metatable for myTable has a __metatable
1589 * key, the value of that key is returned instead of the
1590 * actual metatable.
1591 *
1592 * __tostring - Control string representation. When the builtin
1593 * "tostring( myTable )" function is called, if the metatable
1594 * for myTable has a __tostring property set to a function,
1595 * that function is invoked (passing myTable to it) and the
1596 * return value is used as the string representation.
1597 *
1598 * __len - Control table length. When the table length is requested using
1599 * the length operator ( '#' ), if the metatable for myTable has
1600 * a __len key pointing to a function, that function is invoked
1601 * (passing myTable to it) and the return value used as the value
1602 * of "#myTable".
1603 *
1604 * __gc - Userdata finalizer code. When userdata is set to be garbage
1605 * collected, if the metatable has a __gc field pointing to a
1606 * function, that function is first invoked, passing the userdata
1607 * to it. The __gc metamethod is not called for tables.
1608 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1609 *
1610 * Special metamethods for redefining standard operators:
1611 * http://www.lua.org/pil/13.1.html
1612 *
1613 * __add "+"
1614 * __sub "-"
1615 * __mul "*"
1616 * __div "/"
1617 * __unm "!"
1618 * __pow "^"
1619 * __concat ".."
1620 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001621 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001622 * http://www.lua.org/pil/13.2.html
1623 *
1624 * __eq "=="
1625 * __lt "<"
1626 * __le "<="
1627 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628
1629/*
1630 *
1631 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001632 * Class Map
1633 *
1634 *
1635 */
1636
1637/* Returns a struct hlua_map if the stack entry "ud" is
1638 * a class session, otherwise it throws an error.
1639 */
1640__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1641{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001642 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001643}
1644
1645/* This function is the map constructor. It don't need
1646 * the class Map object. It creates and return a new Map
1647 * object. It must be called only during "body" or "init"
1648 * context because it process some filesystem accesses.
1649 */
1650__LJMP static int hlua_map_new(struct lua_State *L)
1651{
1652 const char *fn;
1653 int match = PAT_MATCH_STR;
1654 struct sample_conv conv;
1655 const char *file = "";
1656 int line = 0;
1657 lua_Debug ar;
1658 char *err = NULL;
1659 struct arg args[2];
1660
1661 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1662 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1663
1664 fn = MAY_LJMP(luaL_checkstring(L, 1));
1665
1666 if (lua_gettop(L) >= 2) {
1667 match = MAY_LJMP(luaL_checkinteger(L, 2));
1668 if (match < 0 || match >= PAT_MATCH_NUM)
1669 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1670 }
1671
1672 /* Get Lua filename and line number. */
1673 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1674 lua_getinfo(L, "Sl", &ar); /* get info about it */
1675 if (ar.currentline > 0) { /* is there info? */
1676 file = ar.short_src;
1677 line = ar.currentline;
1678 }
1679 }
1680
1681 /* fill fake sample_conv struct. */
1682 conv.kw = ""; /* unused. */
1683 conv.process = NULL; /* unused. */
1684 conv.arg_mask = 0; /* unused. */
1685 conv.val_args = NULL; /* unused. */
1686 conv.out_type = SMP_T_STR;
1687 conv.private = (void *)(long)match;
1688 switch (match) {
1689 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1690 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1691 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1692 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1693 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1694 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1695 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001696 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001697 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1698 default:
1699 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1700 }
1701
1702 /* fill fake args. */
1703 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001704 args[0].data.str.area = strdup(fn);
1705 args[0].data.str.data = strlen(fn);
1706 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001707 args[1].type = ARGT_STOP;
1708
1709 /* load the map. */
1710 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001711 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001712 * free the err variable.
1713 */
1714 luaL_where(L, 1);
1715 lua_pushfstring(L, "'new': %s.", err);
1716 lua_concat(L, 2);
1717 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001718 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001719 WILL_LJMP(lua_error(L));
1720 }
1721
1722 /* create the lua object. */
1723 lua_newtable(L);
1724 lua_pushlightuserdata(L, args[0].data.map);
1725 lua_rawseti(L, -2, 0);
1726
1727 /* Pop a class Map metatable and affect it to the userdata. */
1728 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1729 lua_setmetatable(L, -2);
1730
1731
1732 return 1;
1733}
1734
1735__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1736{
1737 struct map_descriptor *desc;
1738 struct pattern *pat;
1739 struct sample smp;
1740
1741 MAY_LJMP(check_args(L, 2, "lookup"));
1742 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001743 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001744 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001745 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001746 }
1747 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001748 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001749 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001750 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 +01001751 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001752 }
1753
1754 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001755 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001756 if (str)
1757 lua_pushstring(L, "");
1758 else
1759 lua_pushnil(L);
1760 return 1;
1761 }
1762
1763 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001764 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001765 return 1;
1766}
1767
1768__LJMP static int hlua_map_lookup(struct lua_State *L)
1769{
1770 return _hlua_map_lookup(L, 0);
1771}
1772
1773__LJMP static int hlua_map_slookup(struct lua_State *L)
1774{
1775 return _hlua_map_lookup(L, 1);
1776}
1777
1778/*
1779 *
1780 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001781 * Class Socket
1782 *
1783 *
1784 */
1785
1786__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1787{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001788 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789}
1790
1791/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001792 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 * received.
1794 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001795static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001796{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001797 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001798
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001799 if (appctx->ctx.hlua_cosocket.die) {
1800 si_shutw(si);
1801 si_shutr(si);
1802 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001803 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1804 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001805 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1806 }
1807
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001808 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001809 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001810 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001811
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001812 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001813 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001814 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001815
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001816 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001817 * to be notified whenever the connection completes.
1818 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001819 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001820 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001821 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001822 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001823 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001824 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825
1826 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001827 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001829 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001830 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001831 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832
1833 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001834 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001835 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001836
1837 /* Some data were injected in the buffer, notify the stream
1838 * interface.
1839 */
1840 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001841 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001842
1843 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001844 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001845 */
1846 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001847 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001848}
1849
Willy Tarreau87b09662015-04-03 00:22:06 +02001850/* This function is called when the "struct stream" is destroyed.
1851 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001852 * Wake all the pending signals.
1853 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001854static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001856 struct xref *peer;
1857
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001859 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1860 if (peer)
1861 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001862
1863 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001864 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1865 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866}
1867
1868/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001869 * uses this object. If the stream does not exists, just quit.
1870 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001871 * pending signal can rest in the read and write lists. destroy
1872 * it.
1873 */
1874__LJMP static int hlua_socket_gc(lua_State *L)
1875{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001876 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001878 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001879
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001880 MAY_LJMP(check_args(L, 1, "__gc"));
1881
1882 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001883 peer = xref_get_peer_and_lock(&socket->xref);
1884 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001886 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001887
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001888 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001889 appctx->ctx.hlua_cosocket.die = 1;
1890 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001892 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001893 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894 return 0;
1895}
1896
1897/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001898 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899 */
sada05ed3302018-05-11 11:48:18 -07001900__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001902 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001904 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001905 struct hlua *hlua;
1906
1907 /* Get hlua struct, or NULL if we execute from main lua state */
1908 hlua = hlua_gethlua(L);
1909 if (!hlua)
1910 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001912 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001913
1914 /* Check if we run on the same thread than the xreator thread.
1915 * We cannot access to the socket if the thread is different.
1916 */
1917 if (socket->tid != tid)
1918 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1919
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001920 peer = xref_get_peer_and_lock(&socket->xref);
1921 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001922 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001923
1924 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001925 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001926
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001927 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001928 appctx->ctx.hlua_cosocket.die = 1;
1929 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001931 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001932 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933 return 0;
1934}
1935
sada05ed3302018-05-11 11:48:18 -07001936/* The close function calls close_helper.
1937 */
1938__LJMP static int hlua_socket_close(lua_State *L)
1939{
1940 MAY_LJMP(check_args(L, 1, "close"));
1941 return hlua_socket_close_helper(L);
1942}
1943
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001944/* This Lua function assumes that the stack contain three parameters.
1945 * 1 - USERDATA containing a struct socket
1946 * 2 - INTEGER with values of the macro defined below
1947 * If the integer is -1, we must read at most one line.
1948 * If the integer is -2, we ust read all the data until the
1949 * end of the stream.
1950 * If the integer is positive value, we must read a number of
1951 * bytes corresponding to this value.
1952 */
1953#define HLSR_READ_LINE (-1)
1954#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001955__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956{
1957 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1958 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001959 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001961 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001963 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001964 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001965 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001966 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001967 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001968 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001969 struct stream_interface *si;
1970 struct stream *s;
1971 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001972 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001974 /* Get hlua struct, or NULL if we execute from main lua state */
1975 hlua = hlua_gethlua(L);
1976
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977 /* Check if this lua stack is schedulable. */
1978 if (!hlua || !hlua->task)
1979 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1980 "'frontend', 'backend' or 'task'"));
1981
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001982 /* Check if we run on the same thread than the xreator thread.
1983 * We cannot access to the socket if the thread is different.
1984 */
1985 if (socket->tid != tid)
1986 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1987
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001988 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001989 peer = xref_get_peer_and_lock(&socket->xref);
1990 if (!peer)
1991 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001992 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1993 si = appctx->owner;
1994 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001995
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001996 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001998 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001999 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000 if (nblk < 0) /* Connection close. */
2001 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002002 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002004
2005 /* remove final \r\n. */
2006 if (nblk == 1) {
2007 if (blk1[len1-1] == '\n') {
2008 len1--;
2009 skip_at_end++;
2010 if (blk1[len1-1] == '\r') {
2011 len1--;
2012 skip_at_end++;
2013 }
2014 }
2015 }
2016 else {
2017 if (blk2[len2-1] == '\n') {
2018 len2--;
2019 skip_at_end++;
2020 if (blk2[len2-1] == '\r') {
2021 len2--;
2022 skip_at_end++;
2023 }
2024 }
2025 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 }
2027
2028 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002030 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002031 if (nblk < 0) /* Connection close. */
2032 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002033 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034 goto connection_empty;
2035 }
2036
2037 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002038 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002039 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002040 if (nblk < 0) /* Connection close. */
2041 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002042 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043 goto connection_empty;
2044
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002045 missing_bytes = wanted - socket->b.n;
2046 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002047 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002048 len1 = missing_bytes;
2049 } if (nblk == 2 && len1 + len2 > missing_bytes)
2050 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051 }
2052
2053 len = len1;
2054
2055 luaL_addlstring(&socket->b, blk1, len1);
2056 if (nblk == 2) {
2057 len += len2;
2058 luaL_addlstring(&socket->b, blk2, len2);
2059 }
2060
2061 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002062 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063
2064 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002065 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066
2067 /* If the pattern reclaim to read all the data
2068 * in the connection, got out.
2069 */
2070 if (wanted == HLSR_READ_ALL)
2071 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002072 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073 goto connection_empty;
2074
2075 /* Return result. */
2076 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002077 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078 return 1;
2079
2080connection_closed:
2081
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002082 xref_unlock(&socket->xref, peer);
2083
2084no_peer:
2085
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086 /* If the buffer containds data. */
2087 if (socket->b.n > 0) {
2088 luaL_pushresult(&socket->b);
2089 return 1;
2090 }
2091 lua_pushnil(L);
2092 lua_pushstring(L, "connection closed.");
2093 return 2;
2094
2095connection_empty:
2096
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002097 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2098 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002099 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002100 }
2101 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002102 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002103 return 0;
2104}
2105
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002106/* This Lua function gets two parameters. The first one can be string
2107 * or a number. If the string is "*l", the user requires one line. If
2108 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002109 * If the value is a number, the user require a number of bytes equal
2110 * to the value. The default value is "*l" (a line).
2111 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002112 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002113 * integer takes this values:
2114 * -1 : read a line
2115 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002116 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002117 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002118 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119 * concatenated with the read data.
2120 */
2121__LJMP static int hlua_socket_receive(struct lua_State *L)
2122{
2123 int wanted = HLSR_READ_LINE;
2124 const char *pattern;
2125 int type;
2126 char *error;
2127 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002128 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002129
2130 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2131 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2132
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002133 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002134
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002135 /* Check if we run on the same thread than the xreator thread.
2136 * We cannot access to the socket if the thread is different.
2137 */
2138 if (socket->tid != tid)
2139 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2140
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002141 /* check for pattern. */
2142 if (lua_gettop(L) >= 2) {
2143 type = lua_type(L, 2);
2144 if (type == LUA_TSTRING) {
2145 pattern = lua_tostring(L, 2);
2146 if (strcmp(pattern, "*a") == 0)
2147 wanted = HLSR_READ_ALL;
2148 else if (strcmp(pattern, "*l") == 0)
2149 wanted = HLSR_READ_LINE;
2150 else {
2151 wanted = strtoll(pattern, &error, 10);
2152 if (*error != '\0')
2153 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2154 }
2155 }
2156 else if (type == LUA_TNUMBER) {
2157 wanted = lua_tointeger(L, 2);
2158 if (wanted < 0)
2159 WILL_LJMP(luaL_error(L, "Unsupported size."));
2160 }
2161 }
2162
2163 /* Set pattern. */
2164 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002165
2166 /* Check if we would replace the top by itself. */
2167 if (lua_gettop(L) != 2)
2168 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002170 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171 luaL_buffinit(L, &socket->b);
2172
2173 /* Check prefix. */
2174 if (lua_gettop(L) >= 3) {
2175 if (lua_type(L, 3) != LUA_TSTRING)
2176 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2177 pattern = lua_tolstring(L, 3, &len);
2178 luaL_addlstring(&socket->b, pattern, len);
2179 }
2180
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002181 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002182}
2183
2184/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002185 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002186 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002187static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002188{
2189 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002190 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002191 struct appctx *appctx;
2192 size_t buf_len;
2193 const char *buf;
2194 int len;
2195 int send_len;
2196 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002197 struct xref *peer;
2198 struct stream_interface *si;
2199 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002200
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002201 /* Get hlua struct, or NULL if we execute from main lua state */
2202 hlua = hlua_gethlua(L);
2203
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204 /* Check if this lua stack is schedulable. */
2205 if (!hlua || !hlua->task)
2206 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2207 "'frontend', 'backend' or 'task'"));
2208
2209 /* Get object */
2210 socket = MAY_LJMP(hlua_checksocket(L, 1));
2211 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002212 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002213
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002214 /* Check if we run on the same thread than the xreator thread.
2215 * We cannot access to the socket if the thread is different.
2216 */
2217 if (socket->tid != tid)
2218 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2219
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002220 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002221 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002222 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002223 lua_pushinteger(L, -1);
2224 return 1;
2225 }
2226 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2227 si = appctx->owner;
2228 s = si_strm(si);
2229
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002231 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002232 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233 lua_pushinteger(L, -1);
2234 return 1;
2235 }
2236
2237 /* Update the input buffer data. */
2238 buf += sent;
2239 send_len = buf_len - sent;
2240
2241 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002242 if (sent >= buf_len) {
2243 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002245 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002246
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002247 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002248 * the request buffer if its not required.
2249 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002250 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002251 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002252 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002253 }
2254
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002255 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002256 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002257 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002259 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002260
2261 /* send data */
2262 if (len < send_len)
2263 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002264 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265
2266 /* "Not enough space" (-1), "Buffer too little to contain
2267 * the data" (-2) are not expected because the available length
2268 * is tested.
2269 * Other unknown error are also not expected.
2270 */
2271 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002272 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002273 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002274
sada05ed3302018-05-11 11:48:18 -07002275 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002276 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002277 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002278 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279 return 1;
2280 }
2281
2282 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002283 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002284
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002285 s->req.rex = TICK_ETERNITY;
2286 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287
2288 /* Update length sent. */
2289 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002290 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291
2292 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002293 if (sent + len >= buf_len) {
2294 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002295 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002296 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297
2298hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002299 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2300 xref_unlock(&socket->xref, peer);
2301 WILL_LJMP(luaL_error(L, "out of memory"));
2302 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002303 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002304 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305 return 0;
2306}
2307
2308/* This function initiate the send of data. It just check the input
2309 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002310 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002311 * "hlua_socket_write_yield" that can yield.
2312 *
2313 * The Lua function gets between 3 and 4 parameters. The first one is
2314 * the associated object. The second is a string buffer. The third is
2315 * a facultative integer that represents where is the buffer position
2316 * of the start of the data that can send. The first byte is the
2317 * position "1". The default value is "1". The fourth argument is a
2318 * facultative integer that represents where is the buffer position
2319 * of the end of the data that can send. The default is the last byte.
2320 */
2321static int hlua_socket_send(struct lua_State *L)
2322{
2323 int i;
2324 int j;
2325 const char *buf;
2326 size_t buf_len;
2327
2328 /* Check number of arguments. */
2329 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2330 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2331
2332 /* Get the string. */
2333 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2334
2335 /* Get and check j. */
2336 if (lua_gettop(L) == 4) {
2337 j = MAY_LJMP(luaL_checkinteger(L, 4));
2338 if (j < 0)
2339 j = buf_len + j + 1;
2340 if (j > buf_len)
2341 j = buf_len + 1;
2342 lua_pop(L, 1);
2343 }
2344 else
2345 j = buf_len;
2346
2347 /* Get and check i. */
2348 if (lua_gettop(L) == 3) {
2349 i = MAY_LJMP(luaL_checkinteger(L, 3));
2350 if (i < 0)
2351 i = buf_len + i + 1;
2352 if (i > buf_len)
2353 i = buf_len + 1;
2354 lua_pop(L, 1);
2355 } else
2356 i = 1;
2357
2358 /* Check bth i and j. */
2359 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002360 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361 return 1;
2362 }
2363 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002364 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002365 return 1;
2366 }
2367 if (i == 0)
2368 i = 1;
2369 if (j == 0)
2370 j = 1;
2371
2372 /* Pop the string. */
2373 lua_pop(L, 1);
2374
2375 /* Update the buffer length. */
2376 buf += i - 1;
2377 buf_len = j - i + 1;
2378 lua_pushlstring(L, buf, buf_len);
2379
2380 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002381 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002383 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002384}
2385
Willy Tarreau22b0a682015-06-17 19:43:49 +02002386#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2388{
2389 static char buffer[SOCKET_INFO_MAX_LEN];
2390 int ret;
2391 int len;
2392 char *p;
2393
2394 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2395 if (ret <= 0) {
2396 lua_pushnil(L);
2397 return 1;
2398 }
2399
2400 if (ret == AF_UNIX) {
2401 lua_pushstring(L, buffer+1);
2402 return 1;
2403 }
2404 else if (ret == AF_INET6) {
2405 buffer[0] = '[';
2406 len = strlen(buffer);
2407 buffer[len] = ']';
2408 len++;
2409 buffer[len] = ':';
2410 len++;
2411 p = buffer;
2412 }
2413 else if (ret == AF_INET) {
2414 p = buffer + 1;
2415 len = strlen(p);
2416 p[len] = ':';
2417 len++;
2418 }
2419 else {
2420 lua_pushnil(L);
2421 return 1;
2422 }
2423
2424 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2425 lua_pushnil(L);
2426 return 1;
2427 }
2428
2429 lua_pushstring(L, p);
2430 return 1;
2431}
2432
2433/* Returns information about the peer of the connection. */
2434__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2435{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002436 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002437 struct xref *peer;
2438 struct appctx *appctx;
2439 struct stream_interface *si;
2440 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002441 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002442
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002443 MAY_LJMP(check_args(L, 1, "getpeername"));
2444
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002445 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002446
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002447 /* Check if we run on the same thread than the xreator thread.
2448 * We cannot access to the socket if the thread is different.
2449 */
2450 if (socket->tid != tid)
2451 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2452
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002453 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002454 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002455 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002456 lua_pushnil(L);
2457 return 1;
2458 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002459 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2460 si = appctx->owner;
2461 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002462
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002463 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002464 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002465 lua_pushnil(L);
2466 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002467 }
2468
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002469 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002470 xref_unlock(&socket->xref, peer);
2471 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472}
2473
2474/* Returns information about my connection side. */
2475static int hlua_socket_getsockname(struct lua_State *L)
2476{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002477 struct hlua_socket *socket;
2478 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002479 struct appctx *appctx;
2480 struct xref *peer;
2481 struct stream_interface *si;
2482 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002483 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002484
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002485 MAY_LJMP(check_args(L, 1, "getsockname"));
2486
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002487 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002489 /* Check if we run on the same thread than the xreator thread.
2490 * We cannot access to the socket if the thread is different.
2491 */
2492 if (socket->tid != tid)
2493 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2494
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002495 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002496 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002497 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002498 lua_pushnil(L);
2499 return 1;
2500 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002501 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2502 si = appctx->owner;
2503 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002504
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002505 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002506 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002507 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002508 lua_pushnil(L);
2509 return 1;
2510 }
2511
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002512 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002513 xref_unlock(&socket->xref, peer);
2514 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002515}
2516
2517/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002518static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519 .obj_type = OBJ_TYPE_APPLET,
2520 .name = "<LUA_TCP>",
2521 .fct = hlua_socket_handler,
2522 .release = hlua_socket_release,
2523};
2524
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002525__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002526{
2527 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002528 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002529 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002530 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002531 struct stream_interface *si;
2532 struct stream *s;
2533
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002534 /* Get hlua struct, or NULL if we execute from main lua state */
2535 hlua = hlua_gethlua(L);
2536 if (!hlua)
2537 return 0;
2538
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002539 /* Check if we run on the same thread than the xreator thread.
2540 * We cannot access to the socket if the thread is different.
2541 */
2542 if (socket->tid != tid)
2543 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2544
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002545 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002546 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002547 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002548 lua_pushnil(L);
2549 lua_pushstring(L, "Can't connect");
2550 return 2;
2551 }
2552 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2553 si = appctx->owner;
2554 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002555
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002556 /* Check if we run on the same thread than the xreator thread.
2557 * We cannot access to the socket if the thread is different.
2558 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002559 if (socket->tid != tid) {
2560 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002561 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002562 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002563
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002564 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002565 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002566 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002567 lua_pushnil(L);
2568 lua_pushstring(L, "Can't connect");
2569 return 2;
2570 }
2571
Willy Tarreaue09101e2018-10-16 17:37:12 +02002572 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002573
2574 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002575 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002576 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002577 lua_pushinteger(L, 1);
2578 return 1;
2579 }
2580
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002581 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2582 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002583 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002584 }
2585 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002586 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587 return 0;
2588}
2589
2590/* This function fail or initite the connection. */
2591__LJMP static int hlua_socket_connect(struct lua_State *L)
2592{
2593 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002594 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002595 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002596 struct hlua *hlua;
2597 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002598 int low, high;
2599 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002600 struct xref *peer;
2601 struct stream_interface *si;
2602 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002603
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002604 if (lua_gettop(L) < 2)
2605 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
2607 /* Get args. */
2608 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002609
2610 /* Check if we run on the same thread than the xreator thread.
2611 * We cannot access to the socket if the thread is different.
2612 */
2613 if (socket->tid != tid)
2614 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2615
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002617 if (lua_gettop(L) >= 3) {
2618 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002619 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620
Tim Duesterhus6edab862018-01-06 19:04:45 +01002621 /* Force the ip to end with a colon, to support IPv6 addresses
2622 * that are not enclosed within square brackets.
2623 */
2624 if (port > 0) {
2625 luaL_buffinit(L, &b);
2626 luaL_addstring(&b, ip);
2627 luaL_addchar(&b, ':');
2628 luaL_pushresult(&b);
2629 ip = lua_tolstring(L, lua_gettop(L), NULL);
2630 }
2631 }
2632
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002633 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002634 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002635 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002636 lua_pushnil(L);
2637 return 1;
2638 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002639
2640 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002641 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 +02002642 if (!addr) {
2643 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002644 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002645 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002646
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002648 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002649 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002650 if (port == -1) {
2651 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002652 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002653 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002654 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2655 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002656 if (port == -1) {
2657 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002658 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002659 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002660 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002661 }
2662 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002663
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002664 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2665 si = appctx->owner;
2666 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002667
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002668 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002669 xref_unlock(&socket->xref, peer);
2670 WILL_LJMP(luaL_error(L, "connect: internal error"));
2671 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002672 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002673
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002674 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002675 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002676 if (!hlua)
2677 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002678
2679 /* inform the stream that we want to be notified whenever the
2680 * connection completes.
2681 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002682 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002683 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002684 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002685
Willy Tarreauf31af932020-01-14 09:59:38 +01002686 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002687
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002688 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2689 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002690 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002691 }
2692 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002693
2694 task_wakeup(s->task, TASK_WOKEN_INIT);
2695 /* Return yield waiting for connection. */
2696
Willy Tarreau9635e032018-10-16 17:52:55 +02002697 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002698
2699 return 0;
2700}
2701
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002702#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002703__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2704{
2705 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002706 struct xref *peer;
2707 struct appctx *appctx;
2708 struct stream_interface *si;
2709 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710
2711 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2712 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002713
2714 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002715 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002716 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002717 lua_pushnil(L);
2718 return 1;
2719 }
2720 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2721 si = appctx->owner;
2722 s = si_strm(si);
2723
2724 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002725 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002726 return MAY_LJMP(hlua_socket_connect(L));
2727}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002728#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002729
2730__LJMP static int hlua_socket_setoption(struct lua_State *L)
2731{
2732 return 0;
2733}
2734
2735__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2736{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002737 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002738 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002739 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002740 struct xref *peer;
2741 struct appctx *appctx;
2742 struct stream_interface *si;
2743 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002744
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002745 MAY_LJMP(check_args(L, 2, "settimeout"));
2746
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002747 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002748
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002749 /* convert the timeout to millis */
2750 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002751
Thierry Fournier17a921b2018-03-08 09:59:02 +01002752 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002753 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002754 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2755
Mark Lakes56cc1252018-03-27 09:48:06 +02002756 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002757 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002758
2759 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002760 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002761 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002762
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002763 /* Check if we run on the same thread than the xreator thread.
2764 * We cannot access to the socket if the thread is different.
2765 */
2766 if (socket->tid != tid)
2767 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2768
Mark Lakes56cc1252018-03-27 09:48:06 +02002769 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002770 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002771 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002772 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2773 WILL_LJMP(lua_error(L));
2774 return 0;
2775 }
2776 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2777 si = appctx->owner;
2778 s = si_strm(si);
2779
Cyril Bonté7bb63452018-08-17 23:51:02 +02002780 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002781 s->req.rto = tmout;
2782 s->req.wto = tmout;
2783 s->res.rto = tmout;
2784 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002785 s->req.rex = tick_add_ifset(now_ms, tmout);
2786 s->req.wex = tick_add_ifset(now_ms, tmout);
2787 s->res.rex = tick_add_ifset(now_ms, tmout);
2788 s->res.wex = tick_add_ifset(now_ms, tmout);
2789
2790 s->task->expire = tick_add_ifset(now_ms, tmout);
2791 task_queue(s->task);
2792
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002793 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002794
Thierry Fourniere9636f12018-03-08 09:54:32 +01002795 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002796 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002797}
2798
2799__LJMP static int hlua_socket_new(lua_State *L)
2800{
2801 struct hlua_socket *socket;
2802 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002803 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002804 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002805
2806 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002807 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002808 hlua_pusherror(L, "socket: full stack");
2809 goto out_fail_conf;
2810 }
2811
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002812 /* Create the object: obj[0] = userdata. */
2813 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002814 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002815 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002816 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002817 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002818
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002819 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002820 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002821 hlua_pusherror(L, "socket: uninitialized pools.");
2822 goto out_fail_conf;
2823 }
2824
Willy Tarreau87b09662015-04-03 00:22:06 +02002825 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002826 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2827 lua_setmetatable(L, -2);
2828
Willy Tarreaud420a972015-04-06 00:39:18 +02002829 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002830 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002831 if (!appctx) {
2832 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002833 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002834 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002835
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002836 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002837 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002838 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2839 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002840
Willy Tarreaud420a972015-04-06 00:39:18 +02002841 /* Now create a session, task and stream for this applet */
2842 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2843 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002844 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002845 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002846 }
2847
Christopher Faulet26256f82020-09-14 11:40:13 +02002848 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002849 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002850 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002851 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002852 }
2853
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002854 /* Initialise cross reference between stream and Lua socket object. */
2855 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002856
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002857 /* Configure "right" stream interface. this "si" is used to connect
2858 * and retrieve data from the server. The connection is initialized
2859 * with the "struct server".
2860 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002861 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002862
2863 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002864 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002865 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002866
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002867 return 1;
2868
Willy Tarreaud420a972015-04-06 00:39:18 +02002869 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002870 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002871 out_fail_sess:
2872 appctx_free(appctx);
2873 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002874 WILL_LJMP(lua_error(L));
2875 return 0;
2876}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002877
2878/*
2879 *
2880 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881 * Class Channel
2882 *
2883 *
2884 */
2885
2886/* Returns the struct hlua_channel join to the class channel in the
2887 * stack entry "ud" or throws an argument error.
2888 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002889__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002890{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002891 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892}
2893
Willy Tarreau47860ed2015-03-10 14:07:50 +01002894/* Pushes the channel onto the top of the stack. If the stask does not have a
2895 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002896 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002897static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002898{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002900 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 return 0;
2902
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002903 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002904 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002905 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002906
2907 /* Pop a class sesison metatable and affect it to the userdata. */
2908 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2909 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 return 1;
2911}
2912
2913/* Duplicate all the data present in the input channel and put it
2914 * in a string LUA variables. Returns -1 and push a nil value in
2915 * the stack if the channel is closed and all the data are consumed,
2916 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002917 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002919static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920{
2921 char *blk1;
2922 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002923 size_t len1;
2924 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925 int ret;
2926 luaL_Buffer b;
2927
Willy Tarreau06d80a92017-10-19 14:32:15 +02002928 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 if (unlikely(ret == 0))
2930 return 0;
2931
2932 if (unlikely(ret < 0)) {
2933 lua_pushnil(L);
2934 return -1;
2935 }
2936
2937 luaL_buffinit(L, &b);
2938 luaL_addlstring(&b, blk1, len1);
2939 if (unlikely(ret == 2))
2940 luaL_addlstring(&b, blk2, len2);
2941 luaL_pushresult(&b);
2942
2943 if (unlikely(ret == 2))
2944 return len1 + len2;
2945 return len1;
2946}
2947
2948/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2949 * a yield. This function keep the data in the buffer.
2950 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002951__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002953 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002954
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002955 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2956
Thierry Fournier77016da2020-08-15 14:35:51 +02002957 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2958 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002959 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002960 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002961
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002962 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002963 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 return 1;
2965}
2966
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002967/* Check arguments for the function "hlua_channel_dup_yield". */
2968__LJMP static int hlua_channel_dup(lua_State *L)
2969{
2970 MAY_LJMP(check_args(L, 1, "dup"));
2971 MAY_LJMP(hlua_checkchannel(L, 1));
2972 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2973}
2974
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002975/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2976 * a yield. This function consumes the data in the buffer. It returns
2977 * a string containing the data or a nil pointer if no data are available
2978 * and the channel is closed.
2979 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002980__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002981{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002982 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002983 int ret;
2984
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002985 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002986
Thierry Fournier77016da2020-08-15 14:35:51 +02002987 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2988 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002989 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002990 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002991
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002992 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002994 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002995
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996 if (unlikely(ret == -1))
2997 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002999 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000 return 1;
3001}
3002
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003003/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003004__LJMP static int hlua_channel_get(lua_State *L)
3005{
3006 MAY_LJMP(check_args(L, 1, "get"));
3007 MAY_LJMP(hlua_checkchannel(L, 1));
3008 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3009}
3010
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011/* This functions consumes and returns one line. If the channel is closed,
3012 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003013 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014 * value.
3015 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003016__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003017{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003018 char *blk1;
3019 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003020 size_t len1;
3021 size_t len2;
3022 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003023 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 int ret;
3025 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003026
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003027 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3028
Thierry Fournier77016da2020-08-15 14:35:51 +02003029 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3030 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003031 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003032 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003033
Willy Tarreau06d80a92017-10-19 14:32:15 +02003034 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003035 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02003036 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003037
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003038 if (ret == -1) {
3039 lua_pushnil(L);
3040 return 1;
3041 }
3042
3043 luaL_buffinit(L, &b);
3044 luaL_addlstring(&b, blk1, len1);
3045 len = len1;
3046 if (unlikely(ret == 2)) {
3047 luaL_addlstring(&b, blk2, len2);
3048 len += len2;
3049 }
3050 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003051 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052 return 1;
3053}
3054
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003055/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003056__LJMP static int hlua_channel_getline(lua_State *L)
3057{
3058 MAY_LJMP(check_args(L, 1, "getline"));
3059 MAY_LJMP(hlua_checkchannel(L, 1));
3060 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3061}
3062
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003063/* This function takes a string as input, and append it at the
3064 * input side of channel. If the data is too big, but a space
3065 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003066 * yields. If the data is bigger than the buffer, or if the
3067 * channel is closed, it returns -1. Otherwise, it returns the
3068 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003070__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003072 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073 size_t len;
3074 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3075 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3076 int ret;
3077 int max;
3078
Thierry Fournier77016da2020-08-15 14:35:51 +02003079 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3080 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003081 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003082 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003083
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003084 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003085 * the request buffer if its not required.
3086 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003087 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003088 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003089 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003090 }
3091
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003092 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093 if (max > len - l)
3094 max = len - l;
3095
Willy Tarreau06d80a92017-10-19 14:32:15 +02003096 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003097 if (ret == -2 || ret == -3) {
3098 lua_pushinteger(L, -1);
3099 return 1;
3100 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003101 if (ret == -1) {
3102 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003103 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003104 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003105 l += ret;
3106 lua_pop(L, 1);
3107 lua_pushinteger(L, l);
3108
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003109 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003110 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003111 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003112 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003113 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003114 */
3115 return 1;
3116 }
3117 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003118 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003119 return 1;
3120}
3121
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003122/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3123 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003124 * buffer size is too little for the data.
3125 */
3126__LJMP static int hlua_channel_append(lua_State *L)
3127{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003128 size_t len;
3129
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003131 MAY_LJMP(hlua_checkchannel(L, 1));
3132 MAY_LJMP(luaL_checklstring(L, 2, &len));
3133 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003134 lua_pushinteger(L, 0);
3135
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003136 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003137}
3138
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003139/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003140 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003141 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003142 * or -1 if the channel is closed or if the buffer size is too
3143 * little for the data.
3144 */
3145__LJMP static int hlua_channel_set(lua_State *L)
3146{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003147 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003148
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003149 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003150 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003151 lua_pushinteger(L, 0);
3152
Thierry Fournier77016da2020-08-15 14:35:51 +02003153 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3154 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003155 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003156 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003157
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003158 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003159
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003160 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003161}
3162
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003163/* Append data in the output side of the buffer. This data is immediately
3164 * sent. The function returns the amount of data written. If the buffer
3165 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003166 * if the channel is closed.
3167 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003168__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003169{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003170 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003171 size_t len;
3172 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3173 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3174 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003175 struct hlua *hlua;
3176
3177 /* Get hlua struct, or NULL if we execute from main lua state */
3178 hlua = hlua_gethlua(L);
3179 if (!hlua) {
3180 lua_pushnil(L);
3181 return 1;
3182 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003183
Thierry Fournier77016da2020-08-15 14:35:51 +02003184 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3185 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003186 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003187 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003188
Willy Tarreau47860ed2015-03-10 14:07:50 +01003189 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003190 lua_pushinteger(L, -1);
3191 return 1;
3192 }
3193
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003194 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003195 * the request buffer if its not required.
3196 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003197 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003198 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003199 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003200 }
3201
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003202 /* The written data will be immediately sent, so we can check
3203 * the available space without taking in account the reserve.
3204 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003205 * data, because the buffer will be flushed.
3206 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003207 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003208
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003209 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003210 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003211 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003212 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003213 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003214 return 1;
3215
3216 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003217 if (max > len - l)
3218 max = len - l;
3219
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003220 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003221 * detects a non contiguous buffer and realign it.
3222 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003223 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003224 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003225
3226 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003227 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003228
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003229 /* buffer replace considers that the input part is filled.
3230 * so, I must forward these new data in the output part.
3231 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003232 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003233
3234 l += max;
3235 lua_pop(L, 1);
3236 lua_pushinteger(L, l);
3237
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003238 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003239 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003240 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003241 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003242 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003243 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003244 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003245
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003246 if (l < len) {
3247 /* If we are waiting for space in the response buffer, we
3248 * must set the flag WAKERESWR. This flag required the task
3249 * wake up if any activity is detected on the response buffer.
3250 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003251 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003252 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003253 else
3254 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003255 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003256 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003257
3258 return 1;
3259}
3260
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003261/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003262 * yield the LUA process, and resume it without checking the
3263 * input arguments.
3264 */
3265__LJMP static int hlua_channel_send(lua_State *L)
3266{
3267 MAY_LJMP(check_args(L, 2, "send"));
3268 lua_pushinteger(L, 0);
3269
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003270 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003271}
3272
3273/* This function forward and amount of butes. The data pass from
3274 * the input side of the buffer to the output side, and can be
3275 * forwarded. This function never fails.
3276 *
3277 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003278 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003279 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003280__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003281{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003282 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003283 int len;
3284 int l;
3285 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003286 struct hlua *hlua;
3287
3288 /* Get hlua struct, or NULL if we execute from main lua state */
3289 hlua = hlua_gethlua(L);
3290 if (!hlua)
3291 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003292
3293 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003294
Thierry Fournier77016da2020-08-15 14:35:51 +02003295 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3296 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003297 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003298 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003299
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003300 len = MAY_LJMP(luaL_checkinteger(L, 2));
3301 l = MAY_LJMP(luaL_checkinteger(L, -1));
3302
3303 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003304 if (max > ci_data(chn))
3305 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003306 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003307 l += max;
3308
3309 lua_pop(L, 1);
3310 lua_pushinteger(L, l);
3311
3312 /* Check if it miss bytes to forward. */
3313 if (l < len) {
3314 /* The the input channel or the output channel are closed, we
3315 * must return the amount of data forwarded.
3316 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003317 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003318 return 1;
3319
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003320 /* If we are waiting for space data in the response buffer, we
3321 * must set the flag WAKERESWR. This flag required the task
3322 * wake up if any activity is detected on the response buffer.
3323 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003324 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003325 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003326 else
3327 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003328
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003329 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003330 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003331 }
3332
3333 return 1;
3334}
3335
3336/* Just check the input and prepare the stack for the previous
3337 * function "hlua_channel_forward_yield"
3338 */
3339__LJMP static int hlua_channel_forward(lua_State *L)
3340{
3341 MAY_LJMP(check_args(L, 2, "forward"));
3342 MAY_LJMP(hlua_checkchannel(L, 1));
3343 MAY_LJMP(luaL_checkinteger(L, 2));
3344
3345 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003346 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003347}
3348
3349/* Just returns the number of bytes available in the input
3350 * side of the buffer. This function never fails.
3351 */
3352__LJMP static int hlua_channel_get_in_len(lua_State *L)
3353{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003354 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003355
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003356 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003357 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003358 if (IS_HTX_STRM(chn_strm(chn))) {
3359 struct htx *htx = htxbuf(&chn->buf);
3360 lua_pushinteger(L, htx->data - co_data(chn));
3361 }
3362 else
3363 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003364 return 1;
3365}
3366
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003367/* Returns true if the channel is full. */
3368__LJMP static int hlua_channel_is_full(lua_State *L)
3369{
3370 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003371
3372 MAY_LJMP(check_args(L, 1, "is_full"));
3373 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003374 /* ignore the reserve, we are not on a producer side (ie in an
3375 * applet).
3376 */
3377 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003378 return 1;
3379}
3380
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003381/* Returns true if the channel is the response channel. */
3382__LJMP static int hlua_channel_is_resp(lua_State *L)
3383{
3384 struct channel *chn;
3385
3386 MAY_LJMP(check_args(L, 1, "is_resp"));
3387 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3388
3389 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3390 return 1;
3391}
3392
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003393/* Just returns the number of bytes available in the output
3394 * side of the buffer. This function never fails.
3395 */
3396__LJMP static int hlua_channel_get_out_len(lua_State *L)
3397{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003398 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003399
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003400 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003401 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003402 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003403 return 1;
3404}
3405
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003406/*
3407 *
3408 *
3409 * Class Fetches
3410 *
3411 *
3412 */
3413
3414/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003415 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003416 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003417__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003418{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003419 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003420}
3421
3422/* This function creates and push in the stack a fetch object according
3423 * with a current TXN.
3424 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003425static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003426{
Willy Tarreau7073c472015-04-06 11:15:40 +02003427 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003428
3429 /* Check stack size. */
3430 if (!lua_checkstack(L, 3))
3431 return 0;
3432
3433 /* Create the object: obj[0] = userdata.
3434 * Note that the base of the Fetches object is the
3435 * transaction object.
3436 */
3437 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003438 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003439 lua_rawseti(L, -2, 0);
3440
Willy Tarreau7073c472015-04-06 11:15:40 +02003441 hsmp->s = txn->s;
3442 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003443 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003444 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003445
3446 /* Pop a class sesison metatable and affect it to the userdata. */
3447 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3448 lua_setmetatable(L, -2);
3449
3450 return 1;
3451}
3452
3453/* This function is an LUA binding. It is called with each sample-fetch.
3454 * It uses closure argument to store the associated sample-fetch. It
3455 * returns only one argument or throws an error. An error is thrown
3456 * only if an error is encountered during the argument parsing. If
3457 * the "sample-fetch" function fails, nil is returned.
3458 */
3459__LJMP static int hlua_run_sample_fetch(lua_State *L)
3460{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003461 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003462 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003463 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003464 int i;
3465 struct sample smp;
3466
3467 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003468 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003469
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003470 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003471 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003472
Thierry FOURNIERca988662015-12-20 18:43:03 +01003473 /* Check execution authorization. */
3474 if (f->use & SMP_USE_HTTP_ANY &&
3475 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3476 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3477 "is not available in Lua services", f->kw);
3478 WILL_LJMP(lua_error(L));
3479 }
3480
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003481 /* Get extra arguments. */
3482 for (i = 0; i < lua_gettop(L) - 1; i++) {
3483 if (i >= ARGM_NBARGS)
3484 break;
3485 hlua_lua2arg(L, i + 2, &args[i]);
3486 }
3487 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003488 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003489
3490 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003491 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003492
3493 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003494 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003495 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003496 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003497 }
3498
3499 /* Initialise the sample. */
3500 memset(&smp, 0, sizeof(smp));
3501
3502 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003503 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003504 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003505 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003506 lua_pushstring(L, "");
3507 else
3508 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003509 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003510 }
3511
3512 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003513 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003514 hlua_smp2lua_str(L, &smp);
3515 else
3516 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003517
3518 end:
3519 for (i = 0; args[i].type != ARGT_STOP; i++) {
3520 if (args[i].type == ARGT_STR)
3521 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003522 else if (args[i].type == ARGT_REG)
3523 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003524 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003525 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003526
3527 error:
3528 for (i = 0; args[i].type != ARGT_STOP; i++) {
3529 if (args[i].type == ARGT_STR)
3530 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003531 else if (args[i].type == ARGT_REG)
3532 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003533 }
3534 WILL_LJMP(lua_error(L));
3535 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003536}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003537
3538/*
3539 *
3540 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003541 * Class Converters
3542 *
3543 *
3544 */
3545
3546/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003547 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003548 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003549__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003550{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003551 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003552}
3553
3554/* This function creates and push in the stack a Converters object
3555 * according with a current TXN.
3556 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003557static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003558{
Willy Tarreau7073c472015-04-06 11:15:40 +02003559 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003560
3561 /* Check stack size. */
3562 if (!lua_checkstack(L, 3))
3563 return 0;
3564
3565 /* Create the object: obj[0] = userdata.
3566 * Note that the base of the Converters object is the
3567 * same than the TXN object.
3568 */
3569 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003570 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003571 lua_rawseti(L, -2, 0);
3572
Willy Tarreau7073c472015-04-06 11:15:40 +02003573 hsmp->s = txn->s;
3574 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003575 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003576 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003577
Willy Tarreau87b09662015-04-03 00:22:06 +02003578 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003579 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3580 lua_setmetatable(L, -2);
3581
3582 return 1;
3583}
3584
3585/* This function is an LUA binding. It is called with each converter.
3586 * It uses closure argument to store the associated converter. It
3587 * returns only one argument or throws an error. An error is thrown
3588 * only if an error is encountered during the argument parsing. If
3589 * the converter function function fails, nil is returned.
3590 */
3591__LJMP static int hlua_run_sample_conv(lua_State *L)
3592{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003593 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003594 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003595 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003596 int i;
3597 struct sample smp;
3598
3599 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003600 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003601
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003602 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003603 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003604
3605 /* Get extra arguments. */
3606 for (i = 0; i < lua_gettop(L) - 2; i++) {
3607 if (i >= ARGM_NBARGS)
3608 break;
3609 hlua_lua2arg(L, i + 3, &args[i]);
3610 }
3611 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003612 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003613
3614 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003615 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003616
3617 /* Run the special args checker. */
3618 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3619 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003620 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003621 }
3622
3623 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003624 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003625 if (!hlua_lua2smp(L, 2, &smp)) {
3626 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003627 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003628 }
3629
Willy Tarreau1777ea62016-03-10 16:15:46 +01003630 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3631
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003632 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003633 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003634 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003635 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003636 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003637 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003638 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3639 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003640 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003641 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003642 }
3643
3644 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003645 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003646 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003647 lua_pushstring(L, "");
3648 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003649 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003650 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003651 }
3652
3653 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003654 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003655 hlua_smp2lua_str(L, &smp);
3656 else
3657 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003658 end:
3659 for (i = 0; args[i].type != ARGT_STOP; i++) {
3660 if (args[i].type == ARGT_STR)
3661 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003662 else if (args[i].type == ARGT_REG)
3663 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003664 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003665 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003666
3667 error:
3668 for (i = 0; args[i].type != ARGT_STOP; i++) {
3669 if (args[i].type == ARGT_STR)
3670 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003671 else if (args[i].type == ARGT_REG)
3672 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003673 }
3674 WILL_LJMP(lua_error(L));
3675 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003676}
3677
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003678/*
3679 *
3680 *
3681 * Class AppletTCP
3682 *
3683 *
3684 */
3685
3686/* Returns a struct hlua_txn if the stack entry "ud" is
3687 * a class stream, otherwise it throws an error.
3688 */
3689__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3690{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003691 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003692}
3693
3694/* This function creates and push in the stack an Applet object
3695 * according with a current TXN.
3696 */
3697static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3698{
3699 struct hlua_appctx *appctx;
3700 struct stream_interface *si = ctx->owner;
3701 struct stream *s = si_strm(si);
3702 struct proxy *p = s->be;
3703
3704 /* Check stack size. */
3705 if (!lua_checkstack(L, 3))
3706 return 0;
3707
3708 /* Create the object: obj[0] = userdata.
3709 * Note that the base of the Converters object is the
3710 * same than the TXN object.
3711 */
3712 lua_newtable(L);
3713 appctx = lua_newuserdata(L, sizeof(*appctx));
3714 lua_rawseti(L, -2, 0);
3715 appctx->appctx = ctx;
3716 appctx->htxn.s = s;
3717 appctx->htxn.p = p;
3718
3719 /* Create the "f" field that contains a list of fetches. */
3720 lua_pushstring(L, "f");
3721 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3722 return 0;
3723 lua_settable(L, -3);
3724
3725 /* Create the "sf" field that contains a list of stringsafe fetches. */
3726 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003727 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003728 return 0;
3729 lua_settable(L, -3);
3730
3731 /* Create the "c" field that contains a list of converters. */
3732 lua_pushstring(L, "c");
3733 if (!hlua_converters_new(L, &appctx->htxn, 0))
3734 return 0;
3735 lua_settable(L, -3);
3736
3737 /* Create the "sc" field that contains a list of stringsafe converters. */
3738 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003739 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003740 return 0;
3741 lua_settable(L, -3);
3742
3743 /* Pop a class stream metatable and affect it to the table. */
3744 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3745 lua_setmetatable(L, -2);
3746
3747 return 1;
3748}
3749
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003750__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3751{
3752 struct hlua_appctx *appctx;
3753 struct stream *s;
3754 const char *name;
3755 size_t len;
3756 struct sample smp;
3757
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003758 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3759 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003760
3761 /* It is useles to retrieve the stream, but this function
3762 * runs only in a stream context.
3763 */
3764 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3765 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3766 s = appctx->htxn.s;
3767
3768 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003769 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003770 hlua_lua2smp(L, 3, &smp);
3771
3772 /* Store the sample in a variable. */
3773 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003774
3775 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3776 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3777 else
3778 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3779
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003780 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003781}
3782
3783__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3784{
3785 struct hlua_appctx *appctx;
3786 struct stream *s;
3787 const char *name;
3788 size_t len;
3789 struct sample smp;
3790
3791 MAY_LJMP(check_args(L, 2, "unset_var"));
3792
3793 /* It is useles to retrieve the stream, but this function
3794 * runs only in a stream context.
3795 */
3796 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3797 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3798 s = appctx->htxn.s;
3799
3800 /* Unset the variable. */
3801 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003802 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3803 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003804}
3805
3806__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3807{
3808 struct hlua_appctx *appctx;
3809 struct stream *s;
3810 const char *name;
3811 size_t len;
3812 struct sample smp;
3813
3814 MAY_LJMP(check_args(L, 2, "get_var"));
3815
3816 /* It is useles to retrieve the stream, but this function
3817 * runs only in a stream context.
3818 */
3819 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3820 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3821 s = appctx->htxn.s;
3822
3823 smp_set_owner(&smp, s->be, s->sess, s, 0);
3824 if (!vars_get_by_name(name, len, &smp)) {
3825 lua_pushnil(L);
3826 return 1;
3827 }
3828
3829 return hlua_smp2lua(L, &smp);
3830}
3831
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003832__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3833{
3834 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3835 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003836 struct hlua *hlua;
3837
3838 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003839 if (!s->hlua)
3840 return 0;
3841 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003842
3843 MAY_LJMP(check_args(L, 2, "set_priv"));
3844
3845 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003846 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003847
3848 /* Get and store new value. */
3849 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3850 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3851
3852 return 0;
3853}
3854
3855__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3856{
3857 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3858 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003859 struct hlua *hlua;
3860
3861 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003862 if (!s->hlua) {
3863 lua_pushnil(L);
3864 return 1;
3865 }
3866 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003867
3868 /* Push configuration index in the stack. */
3869 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3870
3871 return 1;
3872}
3873
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003874/* If expected data not yet available, it returns a yield. This function
3875 * consumes the data in the buffer. It returns a string containing the
3876 * data. This string can be empty.
3877 */
3878__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3879{
3880 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3881 struct stream_interface *si = appctx->appctx->owner;
3882 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003883 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003884 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003885 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003886 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003887
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003888 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003889 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003890
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003891 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003892 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003893 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003894 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003895 }
3896
3897 /* End of data: commit the total strings and return. */
3898 if (ret < 0) {
3899 luaL_pushresult(&appctx->b);
3900 return 1;
3901 }
3902
3903 /* Ensure that the block 2 length is usable. */
3904 if (ret == 1)
3905 len2 = 0;
3906
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003907 /* don't check the max length read and don't check. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003908 luaL_addlstring(&appctx->b, blk1, len1);
3909 luaL_addlstring(&appctx->b, blk2, len2);
3910
3911 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003912 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003913 luaL_pushresult(&appctx->b);
3914 return 1;
3915}
3916
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003917/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003918__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3919{
3920 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3921
3922 /* Initialise the string catenation. */
3923 luaL_buffinit(L, &appctx->b);
3924
3925 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3926}
3927
3928/* If expected data not yet available, it returns a yield. This function
3929 * consumes the data in the buffer. It returns a string containing the
3930 * data. This string can be empty.
3931 */
3932__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3933{
3934 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3935 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003936 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003937 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003938 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003939 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003940 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003941 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003942
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003943 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003944 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003945
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003946 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003947 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003948 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003949 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003950 }
3951
3952 /* End of data: commit the total strings and return. */
3953 if (ret < 0) {
3954 luaL_pushresult(&appctx->b);
3955 return 1;
3956 }
3957
3958 /* Ensure that the block 2 length is usable. */
3959 if (ret == 1)
3960 len2 = 0;
3961
3962 if (len == -1) {
3963
3964 /* If len == -1, catenate all the data avalaile and
3965 * yield because we want to get all the data until
3966 * the end of data stream.
3967 */
3968 luaL_addlstring(&appctx->b, blk1, len1);
3969 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003970 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003971 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003972 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003973
3974 } else {
3975
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003976 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003977 if (len1 > len)
3978 len1 = len;
3979 luaL_addlstring(&appctx->b, blk1, len1);
3980 len -= len1;
3981
3982 /* Copy the second block. */
3983 if (len2 > len)
3984 len2 = len;
3985 luaL_addlstring(&appctx->b, blk2, len2);
3986 len -= len2;
3987
3988 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003989 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003990
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003991 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003992 if (len > 0) {
3993 lua_pushinteger(L, len);
3994 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003995 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003996 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003997 }
3998
3999 /* return the result. */
4000 luaL_pushresult(&appctx->b);
4001 return 1;
4002 }
4003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004004 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004005 hlua_pusherror(L, "Lua: internal error");
4006 WILL_LJMP(lua_error(L));
4007 return 0;
4008}
4009
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004010/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004011__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4012{
4013 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4014 int len = -1;
4015
4016 if (lua_gettop(L) > 2)
4017 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4018 if (lua_gettop(L) >= 2) {
4019 len = MAY_LJMP(luaL_checkinteger(L, 2));
4020 lua_pop(L, 1);
4021 }
4022
4023 /* Confirm or set the required length */
4024 lua_pushinteger(L, len);
4025
4026 /* Initialise the string catenation. */
4027 luaL_buffinit(L, &appctx->b);
4028
4029 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4030}
4031
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004032/* Append data in the output side of the buffer. This data is immediately
4033 * sent. The function returns the amount of data written. If the buffer
4034 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004035 * if the channel is closed.
4036 */
4037__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4038{
4039 size_t len;
4040 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4041 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4042 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4043 struct stream_interface *si = appctx->appctx->owner;
4044 struct channel *chn = si_ic(si);
4045 int max;
4046
4047 /* Get the max amount of data which can write as input in the channel. */
4048 max = channel_recv_max(chn);
4049 if (max > (len - l))
4050 max = len - l;
4051
4052 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004053 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004054
4055 /* update counters. */
4056 l += max;
4057 lua_pop(L, 1);
4058 lua_pushinteger(L, l);
4059
4060 /* If some data is not send, declares the situation to the
4061 * applet, and returns a yield.
4062 */
4063 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004064 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004065 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004066 }
4067
4068 return 1;
4069}
4070
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004071/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004072 * yield the LUA process, and resume it without checking the
4073 * input arguments.
4074 */
4075__LJMP static int hlua_applet_tcp_send(lua_State *L)
4076{
4077 MAY_LJMP(check_args(L, 2, "send"));
4078 lua_pushinteger(L, 0);
4079
4080 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4081}
4082
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004083/*
4084 *
4085 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004086 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004087 *
4088 *
4089 */
4090
4091/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004092 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004093 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004094__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004095{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004096 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004097}
4098
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004099/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004100 * according with a current TXN.
4101 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004102static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004103{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004104 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004105 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004106 struct stream_interface *si = ctx->owner;
4107 struct stream *s = si_strm(si);
4108 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004109 struct htx *htx;
4110 struct htx_blk *blk;
4111 struct htx_sl *sl;
4112 struct ist path;
4113 unsigned long long len = 0;
4114 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004115
4116 /* Check stack size. */
4117 if (!lua_checkstack(L, 3))
4118 return 0;
4119
4120 /* Create the object: obj[0] = userdata.
4121 * Note that the base of the Converters object is the
4122 * same than the TXN object.
4123 */
4124 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004125 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004126 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004127 appctx->appctx = ctx;
4128 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004129 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004130 appctx->htxn.s = s;
4131 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004132
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004133 /* Create the "f" field that contains a list of fetches. */
4134 lua_pushstring(L, "f");
4135 if (!hlua_fetches_new(L, &appctx->htxn, 0))
4136 return 0;
4137 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004138
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004139 /* Create the "sf" field that contains a list of stringsafe fetches. */
4140 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004141 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004142 return 0;
4143 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004144
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004145 /* Create the "c" field that contains a list of converters. */
4146 lua_pushstring(L, "c");
4147 if (!hlua_converters_new(L, &appctx->htxn, 0))
4148 return 0;
4149 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004150
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004151 /* Create the "sc" field that contains a list of stringsafe converters. */
4152 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004153 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004154 return 0;
4155 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004156
Christopher Fauleta2097962019-07-15 16:25:33 +02004157 htx = htxbuf(&s->req.buf);
4158 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004159 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004160 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004161
Christopher Fauleta2097962019-07-15 16:25:33 +02004162 /* Stores the request method. */
4163 lua_pushstring(L, "method");
4164 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4165 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004166
Christopher Fauleta2097962019-07-15 16:25:33 +02004167 /* Stores the http version. */
4168 lua_pushstring(L, "version");
4169 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4170 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004171
Christopher Fauleta2097962019-07-15 16:25:33 +02004172 /* creates an array of headers. hlua_http_get_headers() crates and push
4173 * the array on the top of the stack.
4174 */
4175 lua_pushstring(L, "headers");
4176 htxn.s = s;
4177 htxn.p = px;
4178 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004179 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004180 return 0;
4181 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004182
Christopher Fauleta2097962019-07-15 16:25:33 +02004183 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004184 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004185 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004186
Christopher Fauleta2097962019-07-15 16:25:33 +02004187 p = path.ptr;
4188 end = path.ptr + path.len;
4189 q = p;
4190 while (q < end && *q != '?')
4191 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004192
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004193 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004194 lua_pushstring(L, "path");
4195 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004196 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004197
Christopher Fauleta2097962019-07-15 16:25:33 +02004198 /* Stores the query string. */
4199 lua_pushstring(L, "qs");
4200 if (*q == '?')
4201 q++;
4202 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004203 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004204 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004205
Christopher Fauleta2097962019-07-15 16:25:33 +02004206 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4207 struct htx_blk *blk = htx_get_blk(htx, pos);
4208 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004209
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004210 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004211 break;
4212 if (type == HTX_BLK_DATA)
4213 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004214 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004215 if (htx->extra != ULLONG_MAX)
4216 len += htx->extra;
4217
4218 /* Stores the request path. */
4219 lua_pushstring(L, "length");
4220 lua_pushinteger(L, len);
4221 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004222
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004223 /* Create an empty array of HTTP request headers. */
4224 lua_pushstring(L, "response");
4225 lua_newtable(L);
4226 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004227
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004228 /* Pop a class stream metatable and affect it to the table. */
4229 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4230 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004231
4232 return 1;
4233}
4234
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004235__LJMP static int hlua_applet_http_set_var(lua_State *L)
4236{
4237 struct hlua_appctx *appctx;
4238 struct stream *s;
4239 const char *name;
4240 size_t len;
4241 struct sample smp;
4242
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004243 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4244 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004245
4246 /* It is useles to retrieve the stream, but this function
4247 * runs only in a stream context.
4248 */
4249 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4250 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4251 s = appctx->htxn.s;
4252
4253 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004254 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004255 hlua_lua2smp(L, 3, &smp);
4256
4257 /* Store the sample in a variable. */
4258 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004259
4260 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4261 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4262 else
4263 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4264
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004265 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004266}
4267
4268__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4269{
4270 struct hlua_appctx *appctx;
4271 struct stream *s;
4272 const char *name;
4273 size_t len;
4274 struct sample smp;
4275
4276 MAY_LJMP(check_args(L, 2, "unset_var"));
4277
4278 /* It is useles to retrieve the stream, but this function
4279 * runs only in a stream context.
4280 */
4281 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4282 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4283 s = appctx->htxn.s;
4284
4285 /* Unset the variable. */
4286 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004287 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4288 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004289}
4290
4291__LJMP static int hlua_applet_http_get_var(lua_State *L)
4292{
4293 struct hlua_appctx *appctx;
4294 struct stream *s;
4295 const char *name;
4296 size_t len;
4297 struct sample smp;
4298
4299 MAY_LJMP(check_args(L, 2, "get_var"));
4300
4301 /* It is useles to retrieve the stream, but this function
4302 * runs only in a stream context.
4303 */
4304 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4305 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4306 s = appctx->htxn.s;
4307
4308 smp_set_owner(&smp, s->be, s->sess, s, 0);
4309 if (!vars_get_by_name(name, len, &smp)) {
4310 lua_pushnil(L);
4311 return 1;
4312 }
4313
4314 return hlua_smp2lua(L, &smp);
4315}
4316
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004317__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4318{
4319 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4320 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004321 struct hlua *hlua;
4322
4323 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004324 if (!s->hlua)
4325 return 0;
4326 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004327
4328 MAY_LJMP(check_args(L, 2, "set_priv"));
4329
4330 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004331 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004332
4333 /* Get and store new value. */
4334 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4335 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4336
4337 return 0;
4338}
4339
4340__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4341{
4342 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4343 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004344 struct hlua *hlua;
4345
4346 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004347 if (!s->hlua) {
4348 lua_pushnil(L);
4349 return 1;
4350 }
4351 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004352
4353 /* Push configuration index in the stack. */
4354 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4355
4356 return 1;
4357}
4358
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004359/* If expected data not yet available, it returns a yield. This function
4360 * consumes the data in the buffer. It returns a string containing the
4361 * data. This string can be empty.
4362 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004363__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004364{
4365 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4366 struct stream_interface *si = appctx->appctx->owner;
4367 struct channel *req = si_oc(si);
4368 struct htx *htx;
4369 struct htx_blk *blk;
4370 size_t count;
4371 int stop = 0;
4372
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004373 htx = htx_from_buf(&req->buf);
4374 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004375 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004376
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004377 while (count && !stop && blk) {
4378 enum htx_blk_type type = htx_get_blk_type(blk);
4379 uint32_t sz = htx_get_blksz(blk);
4380 struct ist v;
4381 uint32_t vlen;
4382 char *nl;
4383
4384 vlen = sz;
4385 if (vlen > count) {
4386 if (type != HTX_BLK_DATA)
4387 break;
4388 vlen = count;
4389 }
4390
4391 switch (type) {
4392 case HTX_BLK_UNUSED:
4393 break;
4394
4395 case HTX_BLK_DATA:
4396 v = htx_get_blk_value(htx, blk);
4397 v.len = vlen;
4398 nl = istchr(v, '\n');
4399 if (nl != NULL) {
4400 stop = 1;
4401 vlen = nl - v.ptr + 1;
4402 }
4403 luaL_addlstring(&appctx->b, v.ptr, vlen);
4404 break;
4405
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004406 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004407 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004408 stop = 1;
4409 break;
4410
4411 default:
4412 break;
4413 }
4414
4415 co_set_data(req, co_data(req) - vlen);
4416 count -= vlen;
4417 if (sz == vlen)
4418 blk = htx_remove_blk(htx, blk);
4419 else {
4420 htx_cut_data_blk(htx, blk, vlen);
4421 break;
4422 }
4423 }
4424
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004425 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004426 if (!stop) {
4427 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004428 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004429 }
4430
4431 /* return the result. */
4432 luaL_pushresult(&appctx->b);
4433 return 1;
4434}
4435
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004436
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004437/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004438__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004439{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004440 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004441
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004442 /* Initialise the string catenation. */
4443 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004444
Christopher Fauleta2097962019-07-15 16:25:33 +02004445 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004446}
4447
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004448/* If expected data not yet available, it returns a yield. This function
4449 * consumes the data in the buffer. It returns a string containing the
4450 * data. This string can be empty.
4451 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004452__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004453{
4454 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4455 struct stream_interface *si = appctx->appctx->owner;
4456 struct channel *req = si_oc(si);
4457 struct htx *htx;
4458 struct htx_blk *blk;
4459 size_t count;
4460 int len;
4461
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004462 htx = htx_from_buf(&req->buf);
4463 len = MAY_LJMP(luaL_checkinteger(L, 2));
4464 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004465 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004466 while (count && len && blk) {
4467 enum htx_blk_type type = htx_get_blk_type(blk);
4468 uint32_t sz = htx_get_blksz(blk);
4469 struct ist v;
4470 uint32_t vlen;
4471
4472 vlen = sz;
4473 if (len > 0 && vlen > len)
4474 vlen = len;
4475 if (vlen > count) {
4476 if (type != HTX_BLK_DATA)
4477 break;
4478 vlen = count;
4479 }
4480
4481 switch (type) {
4482 case HTX_BLK_UNUSED:
4483 break;
4484
4485 case HTX_BLK_DATA:
4486 v = htx_get_blk_value(htx, blk);
4487 luaL_addlstring(&appctx->b, v.ptr, vlen);
4488 break;
4489
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004490 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004491 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004492 len = 0;
4493 break;
4494
4495 default:
4496 break;
4497 }
4498
4499 co_set_data(req, co_data(req) - vlen);
4500 count -= vlen;
4501 if (len > 0)
4502 len -= vlen;
4503 if (sz == vlen)
4504 blk = htx_remove_blk(htx, blk);
4505 else {
4506 htx_cut_data_blk(htx, blk, vlen);
4507 break;
4508 }
4509 }
4510
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004511 htx_to_buf(htx, &req->buf);
4512
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004513 /* If we are no other data available, yield waiting for new data. */
4514 if (len) {
4515 if (len > 0) {
4516 lua_pushinteger(L, len);
4517 lua_replace(L, 2);
4518 }
4519 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004520 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004521 }
4522
4523 /* return the result. */
4524 luaL_pushresult(&appctx->b);
4525 return 1;
4526}
4527
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004528/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004529__LJMP static int hlua_applet_http_recv(lua_State *L)
4530{
4531 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4532 int len = -1;
4533
4534 /* Check arguments. */
4535 if (lua_gettop(L) > 2)
4536 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4537 if (lua_gettop(L) >= 2) {
4538 len = MAY_LJMP(luaL_checkinteger(L, 2));
4539 lua_pop(L, 1);
4540 }
4541
Christopher Fauleta2097962019-07-15 16:25:33 +02004542 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004543
Christopher Fauleta2097962019-07-15 16:25:33 +02004544 /* Initialise the string catenation. */
4545 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004546
Christopher Fauleta2097962019-07-15 16:25:33 +02004547 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004548}
4549
4550/* Append data in the output side of the buffer. This data is immediately
4551 * sent. The function returns the amount of data written. If the buffer
4552 * cannot contain the data, the function yields. The function returns -1
4553 * if the channel is closed.
4554 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004555__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004556{
4557 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4558 struct stream_interface *si = appctx->appctx->owner;
4559 struct channel *res = si_ic(si);
4560 struct htx *htx = htx_from_buf(&res->buf);
4561 const char *data;
4562 size_t len;
4563 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4564 int max;
4565
Christopher Faulet9060fc02019-07-03 11:39:30 +02004566 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004567 if (!max)
4568 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004569
4570 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4571
4572 /* Get the max amount of data which can write as input in the channel. */
4573 if (max > (len - l))
4574 max = len - l;
4575
4576 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004577 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004578 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004579
4580 /* update counters. */
4581 l += max;
4582 lua_pop(L, 1);
4583 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004584
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004585 /* If some data is not send, declares the situation to the
4586 * applet, and returns a yield.
4587 */
4588 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004589 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004590 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004591 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004592 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004593 }
4594
Christopher Fauleta2097962019-07-15 16:25:33 +02004595 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004596 return 1;
4597}
4598
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004599/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004600 * yield the LUA process, and resume it without checking the
4601 * input arguments.
4602 */
4603__LJMP static int hlua_applet_http_send(lua_State *L)
4604{
4605 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004606
4607 /* We want to send some data. Headers must be sent. */
4608 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4609 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4610 WILL_LJMP(lua_error(L));
4611 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004612
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004613 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004614 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004615
Christopher Fauleta2097962019-07-15 16:25:33 +02004616 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004617}
4618
4619__LJMP static int hlua_applet_http_addheader(lua_State *L)
4620{
4621 const char *name;
4622 int ret;
4623
4624 MAY_LJMP(hlua_checkapplet_http(L, 1));
4625 name = MAY_LJMP(luaL_checkstring(L, 2));
4626 MAY_LJMP(luaL_checkstring(L, 3));
4627
4628 /* Push in the stack the "response" entry. */
4629 ret = lua_getfield(L, 1, "response");
4630 if (ret != LUA_TTABLE) {
4631 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4632 "is expected as an array. %s found", lua_typename(L, ret));
4633 WILL_LJMP(lua_error(L));
4634 }
4635
4636 /* check if the header is already registered if it is not
4637 * the case, register it.
4638 */
4639 ret = lua_getfield(L, -1, name);
4640 if (ret == LUA_TNIL) {
4641
4642 /* Entry not found. */
4643 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4644
4645 /* Insert the new header name in the array in the top of the stack.
4646 * It left the new array in the top of the stack.
4647 */
4648 lua_newtable(L);
4649 lua_pushvalue(L, 2);
4650 lua_pushvalue(L, -2);
4651 lua_settable(L, -4);
4652
4653 } else if (ret != LUA_TTABLE) {
4654
4655 /* corruption error. */
4656 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4657 "is expected as an array. %s found", name, lua_typename(L, ret));
4658 WILL_LJMP(lua_error(L));
4659 }
4660
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004661 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004662 * the header value as new entry.
4663 */
4664 lua_pushvalue(L, 3);
4665 ret = lua_rawlen(L, -2);
4666 lua_rawseti(L, -2, ret + 1);
4667 lua_pushboolean(L, 1);
4668 return 1;
4669}
4670
4671__LJMP static int hlua_applet_http_status(lua_State *L)
4672{
4673 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4674 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004675 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004676
4677 if (status < 100 || status > 599) {
4678 lua_pushboolean(L, 0);
4679 return 1;
4680 }
4681
4682 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004683 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004684 lua_pushboolean(L, 1);
4685 return 1;
4686}
4687
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004688
Christopher Fauleta2097962019-07-15 16:25:33 +02004689__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004690{
4691 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4692 struct stream_interface *si = appctx->appctx->owner;
4693 struct channel *res = si_ic(si);
4694 struct htx *htx;
4695 struct htx_sl *sl;
4696 struct h1m h1m;
4697 const char *status, *reason;
4698 const char *name, *value;
4699 size_t nlen, vlen;
4700 unsigned int flags;
4701
4702 /* Send the message at once. */
4703 htx = htx_from_buf(&res->buf);
4704 h1m_init_res(&h1m);
4705
4706 /* Use the same http version than the request. */
4707 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4708 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4709 if (reason == NULL)
4710 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4711 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4712 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4713 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4714 }
4715 else {
4716 flags = HTX_SL_F_IS_RESP;
4717 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4718 }
4719 if (!sl) {
4720 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004721 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004722 WILL_LJMP(lua_error(L));
4723 }
4724 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4725
4726 /* Get the array associated to the field "response" in the object AppletHTTP. */
4727 lua_pushvalue(L, 0);
4728 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4729 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004730 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004731 WILL_LJMP(lua_error(L));
4732 }
4733
4734 /* Browse the list of headers. */
4735 lua_pushnil(L);
4736 while(lua_next(L, -2) != 0) {
4737 /* We expect a string as -2. */
4738 if (lua_type(L, -2) != LUA_TSTRING) {
4739 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004740 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004741 lua_typename(L, lua_type(L, -2)));
4742 WILL_LJMP(lua_error(L));
4743 }
4744 name = lua_tolstring(L, -2, &nlen);
4745
4746 /* We expect an array as -1. */
4747 if (lua_type(L, -1) != LUA_TTABLE) {
4748 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004749 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004750 name,
4751 lua_typename(L, lua_type(L, -1)));
4752 WILL_LJMP(lua_error(L));
4753 }
4754
4755 /* Browse the table who is on the top of the stack. */
4756 lua_pushnil(L);
4757 while(lua_next(L, -2) != 0) {
4758 int id;
4759
4760 /* We expect a number as -2. */
4761 if (lua_type(L, -2) != LUA_TNUMBER) {
4762 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004763 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004764 name,
4765 lua_typename(L, lua_type(L, -2)));
4766 WILL_LJMP(lua_error(L));
4767 }
4768 id = lua_tointeger(L, -2);
4769
4770 /* We expect a string as -2. */
4771 if (lua_type(L, -1) != LUA_TSTRING) {
4772 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004773 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004774 name, id,
4775 lua_typename(L, lua_type(L, -1)));
4776 WILL_LJMP(lua_error(L));
4777 }
4778 value = lua_tolstring(L, -1, &vlen);
4779
4780 /* Simple Protocol checks. */
4781 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004782 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004783 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4784 struct ist v = ist2(value, vlen);
4785 int ret;
4786
4787 ret = h1_parse_cont_len_header(&h1m, &v);
4788 if (ret < 0) {
4789 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004790 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004791 name);
4792 WILL_LJMP(lua_error(L));
4793 }
4794 else if (ret == 0)
4795 goto next; /* Skip it */
4796 }
4797
4798 /* Add a new header */
4799 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4800 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004801 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004802 name);
4803 WILL_LJMP(lua_error(L));
4804 }
4805 next:
4806 /* Remove the array from the stack, and get next element with a remaining string. */
4807 lua_pop(L, 1);
4808 }
4809
4810 /* Remove the array from the stack, and get next element with a remaining string. */
4811 lua_pop(L, 1);
4812 }
4813
4814 if (h1m.flags & H1_MF_CHNK)
4815 h1m.flags &= ~H1_MF_CLEN;
4816 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4817 h1m.flags |= H1_MF_XFER_LEN;
4818
4819 /* Uset HTX start-line flags */
4820 if (h1m.flags & H1_MF_XFER_ENC)
4821 flags |= HTX_SL_F_XFER_ENC;
4822 if (h1m.flags & H1_MF_XFER_LEN) {
4823 flags |= HTX_SL_F_XFER_LEN;
4824 if (h1m.flags & H1_MF_CHNK)
4825 flags |= HTX_SL_F_CHNK;
4826 else if (h1m.flags & H1_MF_CLEN)
4827 flags |= HTX_SL_F_CLEN;
4828 if (h1m.body_len == 0)
4829 flags |= HTX_SL_F_BODYLESS;
4830 }
4831 sl->flags |= flags;
4832
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004833 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004834 * and the status code implies the presence of a message body, we must
4835 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004836 * for the keepalive compliance. If the applet announces a transfer-encoding
4837 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004838 */
4839 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4840 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4841 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4842 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4843 /* Add a new header */
4844 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4845 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4846 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004847 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004848 WILL_LJMP(lua_error(L));
4849 }
4850 }
4851
4852 /* Finalize headers. */
4853 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4854 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004855 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004856 WILL_LJMP(lua_error(L));
4857 }
4858
4859 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4860 b_reset(&res->buf);
4861 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4862 WILL_LJMP(lua_error(L));
4863 }
4864
4865 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004866 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004867
4868 /* Headers sent, set the flag. */
4869 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4870 return 0;
4871
4872}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004873/* We will build the status line and the headers of the HTTP response.
4874 * We will try send at once if its not possible, we give back the hand
4875 * waiting for more room.
4876 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004877__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004878{
4879 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4880 struct stream_interface *si = appctx->appctx->owner;
4881 struct channel *res = si_ic(si);
4882
4883 if (co_data(res)) {
4884 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004885 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004886 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004887 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004888}
4889
4890
Christopher Fauleta2097962019-07-15 16:25:33 +02004891__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004892{
Christopher Fauleta2097962019-07-15 16:25:33 +02004893 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004894}
4895
Christopher Fauleta2097962019-07-15 16:25:33 +02004896/*
4897 *
4898 *
4899 * Class HTTP
4900 *
4901 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004902 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004903
4904/* Returns a struct hlua_txn if the stack entry "ud" is
4905 * a class stream, otherwise it throws an error.
4906 */
4907__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004908{
Christopher Fauleta2097962019-07-15 16:25:33 +02004909 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4910}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004911
Christopher Fauleta2097962019-07-15 16:25:33 +02004912/* This function creates and push in the stack a HTTP object
4913 * according with a current TXN.
4914 */
4915static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4916{
4917 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004918
Christopher Fauleta2097962019-07-15 16:25:33 +02004919 /* Check stack size. */
4920 if (!lua_checkstack(L, 3))
4921 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004922
Christopher Fauleta2097962019-07-15 16:25:33 +02004923 /* Create the object: obj[0] = userdata.
4924 * Note that the base of the Converters object is the
4925 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004926 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004927 lua_newtable(L);
4928 htxn = lua_newuserdata(L, sizeof(*htxn));
4929 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004930
4931 htxn->s = txn->s;
4932 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004933 htxn->dir = txn->dir;
4934 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004935
4936 /* Pop a class stream metatable and affect it to the table. */
4937 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4938 lua_setmetatable(L, -2);
4939
4940 return 1;
4941}
4942
4943/* This function creates ans returns an array of HTTP headers.
4944 * This function does not fails. It is used as wrapper with the
4945 * 2 following functions.
4946 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004947__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004948{
Christopher Fauleta2097962019-07-15 16:25:33 +02004949 struct htx *htx;
4950 int32_t pos;
4951
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004952 /* Create the table. */
4953 lua_newtable(L);
4954
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004955
Christopher Fauleta2097962019-07-15 16:25:33 +02004956 htx = htxbuf(&msg->chn->buf);
4957 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4958 struct htx_blk *blk = htx_get_blk(htx, pos);
4959 enum htx_blk_type type = htx_get_blk_type(blk);
4960 struct ist n, v;
4961 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004962
Christopher Fauleta2097962019-07-15 16:25:33 +02004963 if (type == HTX_BLK_HDR) {
4964 n = htx_get_blk_name(htx,blk);
4965 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004966 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004967 else if (type == HTX_BLK_EOH)
4968 break;
4969 else
4970 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004971
Christopher Fauleta2097962019-07-15 16:25:33 +02004972 /* Check for existing entry:
4973 * assume that the table is on the top of the stack, and
4974 * push the key in the stack, the function lua_gettable()
4975 * perform the lookup.
4976 */
4977 lua_pushlstring(L, n.ptr, n.len);
4978 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004979
Christopher Fauleta2097962019-07-15 16:25:33 +02004980 switch (lua_type(L, -1)) {
4981 case LUA_TNIL:
4982 /* Table not found, create it. */
4983 lua_pop(L, 1); /* remove the nil value. */
4984 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4985 lua_newtable(L); /* create and push empty table. */
4986 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4987 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4988 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004989 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004990
Christopher Fauleta2097962019-07-15 16:25:33 +02004991 case LUA_TTABLE:
4992 /* Entry found: push the value in the table. */
4993 len = lua_rawlen(L, -1);
4994 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4995 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4996 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4997 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004998
Christopher Fauleta2097962019-07-15 16:25:33 +02004999 default:
5000 /* Other cases are errors. */
5001 hlua_pusherror(L, "internal error during the parsing of headers.");
5002 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005003 }
5004 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005005 return 1;
5006}
5007
5008__LJMP static int hlua_http_req_get_headers(lua_State *L)
5009{
5010 struct hlua_txn *htxn;
5011
5012 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5013 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5014
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005015 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005016 WILL_LJMP(lua_error(L));
5017
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005018 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005019}
5020
5021__LJMP static int hlua_http_res_get_headers(lua_State *L)
5022{
5023 struct hlua_txn *htxn;
5024
5025 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5026 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5027
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005028 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005029 WILL_LJMP(lua_error(L));
5030
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005031 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005032}
5033
5034/* This function replace full header, or just a value in
5035 * the request or in the response. It is a wrapper fir the
5036 * 4 following functions.
5037 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005038__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005039{
5040 size_t name_len;
5041 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5042 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5043 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005044 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005045 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005046
Dragan Dosen26743032019-04-30 15:54:36 +02005047 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005048 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5049
Christopher Fauleta2097962019-07-15 16:25:33 +02005050 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005051 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005052 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005053 return 0;
5054}
5055
5056__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5057{
5058 struct hlua_txn *htxn;
5059
5060 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5061 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5062
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005063 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005064 WILL_LJMP(lua_error(L));
5065
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005066 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005067}
5068
5069__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5070{
5071 struct hlua_txn *htxn;
5072
5073 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5074 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5075
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005076 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005077 WILL_LJMP(lua_error(L));
5078
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005079 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005080}
5081
5082__LJMP static int hlua_http_req_rep_val(lua_State *L)
5083{
5084 struct hlua_txn *htxn;
5085
5086 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5087 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5088
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005089 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005090 WILL_LJMP(lua_error(L));
5091
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005092 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005093}
5094
5095__LJMP static int hlua_http_res_rep_val(lua_State *L)
5096{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005097 struct hlua_txn *htxn;
5098
5099 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5100 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5101
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005102 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005103 WILL_LJMP(lua_error(L));
5104
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005105 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005106}
5107
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005108/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005109 * It is a wrapper for the 2 following functions.
5110 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005111__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005112{
5113 size_t len;
5114 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005115 struct htx *htx = htxbuf(&msg->chn->buf);
5116 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005117
Christopher Fauleta2097962019-07-15 16:25:33 +02005118 ctx.blk = NULL;
5119 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5120 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005121 return 0;
5122}
5123
5124__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5125{
5126 struct hlua_txn *htxn;
5127
5128 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5129 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5130
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005131 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005132 WILL_LJMP(lua_error(L));
5133
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005134 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005135}
5136
5137__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5138{
5139 struct hlua_txn *htxn;
5140
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005141 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005142 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5143
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005144 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005145 WILL_LJMP(lua_error(L));
5146
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005147 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005148}
5149
5150/* This function adds an header. It is a wrapper used by
5151 * the 2 following functions.
5152 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005153__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005154{
5155 size_t name_len;
5156 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5157 size_t value_len;
5158 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005159 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005160
Christopher Fauleta2097962019-07-15 16:25:33 +02005161 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5162 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005163 return 0;
5164}
5165
5166__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5167{
5168 struct hlua_txn *htxn;
5169
5170 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5171 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5172
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005173 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005174 WILL_LJMP(lua_error(L));
5175
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005176 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005177}
5178
5179__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5180{
5181 struct hlua_txn *htxn;
5182
5183 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5184 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5185
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005186 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005187 WILL_LJMP(lua_error(L));
5188
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005189 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005190}
5191
5192static int hlua_http_req_set_hdr(lua_State *L)
5193{
5194 struct hlua_txn *htxn;
5195
5196 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5197 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5198
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005199 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005200 WILL_LJMP(lua_error(L));
5201
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005202 hlua_http_del_hdr(L, &htxn->s->txn->req);
5203 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005204}
5205
5206static int hlua_http_res_set_hdr(lua_State *L)
5207{
5208 struct hlua_txn *htxn;
5209
5210 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5211 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5212
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005213 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005214 WILL_LJMP(lua_error(L));
5215
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005216 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5217 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005218}
5219
5220/* This function set the method. */
5221static int hlua_http_req_set_meth(lua_State *L)
5222{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005223 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005224 size_t name_len;
5225 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005226
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005227 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005228 WILL_LJMP(lua_error(L));
5229
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005230 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005231 return 1;
5232}
5233
5234/* This function set the method. */
5235static int hlua_http_req_set_path(lua_State *L)
5236{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005237 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005238 size_t name_len;
5239 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005240
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005241 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005242 WILL_LJMP(lua_error(L));
5243
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005244 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005245 return 1;
5246}
5247
5248/* This function set the query-string. */
5249static int hlua_http_req_set_query(lua_State *L)
5250{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005251 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005252 size_t name_len;
5253 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005254
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005255 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005256 WILL_LJMP(lua_error(L));
5257
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005258 /* Check length. */
5259 if (name_len > trash.size - 1) {
5260 lua_pushboolean(L, 0);
5261 return 1;
5262 }
5263
5264 /* Add the mark question as prefix. */
5265 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005266 trash.area[trash.data++] = '?';
5267 memcpy(trash.area + trash.data, name, name_len);
5268 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005269
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005270 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005271 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005272 return 1;
5273}
5274
5275/* This function set the uri. */
5276static int hlua_http_req_set_uri(lua_State *L)
5277{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005278 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005279 size_t name_len;
5280 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005281
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005282 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005283 WILL_LJMP(lua_error(L));
5284
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005285 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005286 return 1;
5287}
5288
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005289/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005290static int hlua_http_res_set_status(lua_State *L)
5291{
5292 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5293 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005294 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5295 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005296
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005297 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005298 WILL_LJMP(lua_error(L));
5299
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005300 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005301 return 0;
5302}
5303
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005304/*
5305 *
5306 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005307 * Class TXN
5308 *
5309 *
5310 */
5311
5312/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005313 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005314 */
5315__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5316{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005317 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005318}
5319
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005320__LJMP static int hlua_set_var(lua_State *L)
5321{
5322 struct hlua_txn *htxn;
5323 const char *name;
5324 size_t len;
5325 struct sample smp;
5326
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005327 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5328 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005329
5330 /* It is useles to retrieve the stream, but this function
5331 * runs only in a stream context.
5332 */
5333 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5334 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5335
5336 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005337 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005338 hlua_lua2smp(L, 3, &smp);
5339
5340 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005341 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005342
5343 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5344 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5345 else
5346 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5347
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005348 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005349}
5350
Christopher Faulet85d79c92016-11-09 16:54:56 +01005351__LJMP static int hlua_unset_var(lua_State *L)
5352{
5353 struct hlua_txn *htxn;
5354 const char *name;
5355 size_t len;
5356 struct sample smp;
5357
5358 MAY_LJMP(check_args(L, 2, "unset_var"));
5359
5360 /* It is useles to retrieve the stream, but this function
5361 * runs only in a stream context.
5362 */
5363 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5364 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5365
5366 /* Unset the variable. */
5367 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005368 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5369 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005370}
5371
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005372__LJMP static int hlua_get_var(lua_State *L)
5373{
5374 struct hlua_txn *htxn;
5375 const char *name;
5376 size_t len;
5377 struct sample smp;
5378
5379 MAY_LJMP(check_args(L, 2, "get_var"));
5380
5381 /* It is useles to retrieve the stream, but this function
5382 * runs only in a stream context.
5383 */
5384 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5385 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5386
Willy Tarreau7560dd42016-03-10 16:28:58 +01005387 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005388 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005389 lua_pushnil(L);
5390 return 1;
5391 }
5392
5393 return hlua_smp2lua(L, &smp);
5394}
5395
Willy Tarreau59551662015-03-10 14:23:13 +01005396__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005397{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005398 struct hlua *hlua;
5399
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005400 MAY_LJMP(check_args(L, 2, "set_priv"));
5401
Willy Tarreau87b09662015-04-03 00:22:06 +02005402 /* It is useles to retrieve the stream, but this function
5403 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005404 */
5405 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005406
5407 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005408 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005409 if (!hlua)
5410 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005411
5412 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005413 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005414
5415 /* Get and store new value. */
5416 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5417 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5418
5419 return 0;
5420}
5421
Willy Tarreau59551662015-03-10 14:23:13 +01005422__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005423{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005424 struct hlua *hlua;
5425
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005426 MAY_LJMP(check_args(L, 1, "get_priv"));
5427
Willy Tarreau87b09662015-04-03 00:22:06 +02005428 /* It is useles to retrieve the stream, but this function
5429 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005430 */
5431 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005432
5433 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005434 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005435 if (!hlua) {
5436 lua_pushnil(L);
5437 return 1;
5438 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005439
5440 /* Push configuration index in the stack. */
5441 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5442
5443 return 1;
5444}
5445
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005446/* Create stack entry containing a class TXN. This function
5447 * return 0 if the stack does not contains free slots,
5448 * otherwise it returns 1.
5449 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005450static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005451{
Willy Tarreaude491382015-04-06 11:04:28 +02005452 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005453
5454 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005455 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005456 return 0;
5457
5458 /* NOTE: The allocation never fails. The failure
5459 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005460 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005461 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005462 /* Create the object: obj[0] = userdata. */
5463 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005464 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005465 lua_rawseti(L, -2, 0);
5466
Willy Tarreaude491382015-04-06 11:04:28 +02005467 htxn->s = s;
5468 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005469 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005470 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005471
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005472 /* Create the "f" field that contains a list of fetches. */
5473 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005474 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005475 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005476 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005477
5478 /* Create the "sf" field that contains a list of stringsafe fetches. */
5479 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005480 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005481 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005482 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005483
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005484 /* Create the "c" field that contains a list of converters. */
5485 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005486 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005487 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005488 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005489
5490 /* Create the "sc" field that contains a list of stringsafe converters. */
5491 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005492 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005493 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005494 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005495
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005496 /* Create the "req" field that contains the request channel object. */
5497 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005498 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005499 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005500 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005501
5502 /* Create the "res" field that contains the response channel object. */
5503 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005504 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005505 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005506 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005507
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005508 /* Creates the HTTP object is the current proxy allows http. */
5509 lua_pushstring(L, "http");
5510 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005511 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005512 return 0;
5513 }
5514 else
5515 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005516 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005517
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005518 /* Pop a class sesison metatable and affect it to the userdata. */
5519 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5520 lua_setmetatable(L, -2);
5521
5522 return 1;
5523}
5524
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005525__LJMP static int hlua_txn_deflog(lua_State *L)
5526{
5527 const char *msg;
5528 struct hlua_txn *htxn;
5529
5530 MAY_LJMP(check_args(L, 2, "deflog"));
5531 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5532 msg = MAY_LJMP(luaL_checkstring(L, 2));
5533
5534 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5535 return 0;
5536}
5537
5538__LJMP static int hlua_txn_log(lua_State *L)
5539{
5540 int level;
5541 const char *msg;
5542 struct hlua_txn *htxn;
5543
5544 MAY_LJMP(check_args(L, 3, "log"));
5545 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5546 level = MAY_LJMP(luaL_checkinteger(L, 2));
5547 msg = MAY_LJMP(luaL_checkstring(L, 3));
5548
5549 if (level < 0 || level >= NB_LOG_LEVELS)
5550 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5551
5552 hlua_sendlog(htxn->s->be, level, msg);
5553 return 0;
5554}
5555
5556__LJMP static int hlua_txn_log_debug(lua_State *L)
5557{
5558 const char *msg;
5559 struct hlua_txn *htxn;
5560
5561 MAY_LJMP(check_args(L, 2, "Debug"));
5562 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5563 msg = MAY_LJMP(luaL_checkstring(L, 2));
5564 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5565 return 0;
5566}
5567
5568__LJMP static int hlua_txn_log_info(lua_State *L)
5569{
5570 const char *msg;
5571 struct hlua_txn *htxn;
5572
5573 MAY_LJMP(check_args(L, 2, "Info"));
5574 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5575 msg = MAY_LJMP(luaL_checkstring(L, 2));
5576 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5577 return 0;
5578}
5579
5580__LJMP static int hlua_txn_log_warning(lua_State *L)
5581{
5582 const char *msg;
5583 struct hlua_txn *htxn;
5584
5585 MAY_LJMP(check_args(L, 2, "Warning"));
5586 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5587 msg = MAY_LJMP(luaL_checkstring(L, 2));
5588 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5589 return 0;
5590}
5591
5592__LJMP static int hlua_txn_log_alert(lua_State *L)
5593{
5594 const char *msg;
5595 struct hlua_txn *htxn;
5596
5597 MAY_LJMP(check_args(L, 2, "Alert"));
5598 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5599 msg = MAY_LJMP(luaL_checkstring(L, 2));
5600 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5601 return 0;
5602}
5603
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005604__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5605{
5606 struct hlua_txn *htxn;
5607 int ll;
5608
5609 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5610 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5611 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5612
5613 if (ll < 0 || ll > 7)
5614 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5615
5616 htxn->s->logs.level = ll;
5617 return 0;
5618}
5619
5620__LJMP static int hlua_txn_set_tos(lua_State *L)
5621{
5622 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005623 int tos;
5624
5625 MAY_LJMP(check_args(L, 2, "set_tos"));
5626 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5627 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5628
Willy Tarreau1a18b542018-12-11 16:37:42 +01005629 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005630 return 0;
5631}
5632
5633__LJMP static int hlua_txn_set_mark(lua_State *L)
5634{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005635 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005636 int mark;
5637
5638 MAY_LJMP(check_args(L, 2, "set_mark"));
5639 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5640 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5641
Lukas Tribus579e3e32019-08-11 18:03:45 +02005642 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005643 return 0;
5644}
5645
Patrick Hemmer268a7072018-05-11 12:52:31 -04005646__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5647{
5648 struct hlua_txn *htxn;
5649
5650 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5651 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5652 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5653 return 0;
5654}
5655
5656__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5657{
5658 struct hlua_txn *htxn;
5659
5660 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5661 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5662 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5663 return 0;
5664}
5665
Christopher Faulet700d9e82020-01-31 12:21:52 +01005666/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005667 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005668 * message and terminate the transaction. It returns 1 on success and 0 on
5669 * error. The Reply must be on top of the stack.
5670 */
5671__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5672{
5673 struct htx *htx;
5674 struct htx_sl *sl;
5675 struct h1m h1m;
5676 const char *status, *reason, *body;
5677 size_t status_len, reason_len, body_len;
5678 int ret, code, flags;
5679
5680 code = 200;
5681 status = "200";
5682 status_len = 3;
5683 ret = lua_getfield(L, -1, "status");
5684 if (ret == LUA_TNUMBER) {
5685 code = lua_tointeger(L, -1);
5686 status = lua_tolstring(L, -1, &status_len);
5687 }
5688 lua_pop(L, 1);
5689
5690 reason = http_get_reason(code);
5691 reason_len = strlen(reason);
5692 ret = lua_getfield(L, -1, "reason");
5693 if (ret == LUA_TSTRING)
5694 reason = lua_tolstring(L, -1, &reason_len);
5695 lua_pop(L, 1);
5696
5697 body = NULL;
5698 body_len = 0;
5699 ret = lua_getfield(L, -1, "body");
5700 if (ret == LUA_TSTRING)
5701 body = lua_tolstring(L, -1, &body_len);
5702 lua_pop(L, 1);
5703
5704 /* Prepare the response before inserting the headers */
5705 h1m_init_res(&h1m);
5706 htx = htx_from_buf(&s->res.buf);
5707 channel_htx_truncate(&s->res, htx);
5708 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5709 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5710 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5711 ist2(status, status_len), ist2(reason, reason_len));
5712 }
5713 else {
5714 flags = HTX_SL_F_IS_RESP;
5715 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5716 ist2(status, status_len), ist2(reason, reason_len));
5717 }
5718 if (!sl)
5719 goto fail;
5720 sl->info.res.status = code;
5721
5722 /* Push in the stack the "headers" entry. */
5723 ret = lua_getfield(L, -1, "headers");
5724 if (ret != LUA_TTABLE)
5725 goto skip_headers;
5726
5727 lua_pushnil(L);
5728 while (lua_next(L, -2) != 0) {
5729 struct ist name, value;
5730 const char *n, *v;
5731 size_t nlen, vlen;
5732
5733 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5734 /* Skip element if the key is not a string or if the value is not a table */
5735 goto next_hdr;
5736 }
5737
5738 n = lua_tolstring(L, -2, &nlen);
5739 name = ist2(n, nlen);
5740 if (isteqi(name, ist("content-length"))) {
5741 /* Always skip content-length header. It will be added
5742 * later with the correct len
5743 */
5744 goto next_hdr;
5745 }
5746
5747 /* Loop on header's values */
5748 lua_pushnil(L);
5749 while (lua_next(L, -2)) {
5750 if (!lua_isstring(L, -1)) {
5751 /* Skip the value if it is not a string */
5752 goto next_value;
5753 }
5754
5755 v = lua_tolstring(L, -1, &vlen);
5756 value = ist2(v, vlen);
5757
5758 if (isteqi(name, ist("transfer-encoding")))
5759 h1_parse_xfer_enc_header(&h1m, value);
5760 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5761 goto fail;
5762
5763 next_value:
5764 lua_pop(L, 1);
5765 }
5766
5767 next_hdr:
5768 lua_pop(L, 1);
5769 }
5770 skip_headers:
5771 lua_pop(L, 1);
5772
5773 /* Update h1m flags: CLEN is set if CHNK is not present */
5774 if (!(h1m.flags & H1_MF_CHNK)) {
5775 const char *clen = ultoa(body_len);
5776
5777 h1m.flags |= H1_MF_CLEN;
5778 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5779 goto fail;
5780 }
5781 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5782 h1m.flags |= H1_MF_XFER_LEN;
5783
5784 /* Update HTX start-line flags */
5785 if (h1m.flags & H1_MF_XFER_ENC)
5786 flags |= HTX_SL_F_XFER_ENC;
5787 if (h1m.flags & H1_MF_XFER_LEN) {
5788 flags |= HTX_SL_F_XFER_LEN;
5789 if (h1m.flags & H1_MF_CHNK)
5790 flags |= HTX_SL_F_CHNK;
5791 else if (h1m.flags & H1_MF_CLEN)
5792 flags |= HTX_SL_F_CLEN;
5793 if (h1m.body_len == 0)
5794 flags |= HTX_SL_F_BODYLESS;
5795 }
5796 sl->flags |= flags;
5797
5798
5799 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005800 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005801 goto fail;
5802
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005803 htx->flags |= HTX_FL_EOM;
5804
Christopher Faulet700d9e82020-01-31 12:21:52 +01005805 /* Now, forward the response and terminate the transaction */
5806 s->txn->status = code;
5807 htx_to_buf(htx, &s->res.buf);
5808 if (!http_forward_proxy_resp(s, 1))
5809 goto fail;
5810
5811 return 1;
5812
5813 fail:
5814 channel_htx_truncate(&s->res, htx);
5815 return 0;
5816}
5817
5818/* Terminate a transaction if called from a lua action. For TCP streams,
5819 * processing is just aborted. Nothing is returned to the client and all
5820 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5821 * is forwarded to the client before terminating the transaction. On success,
5822 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5823 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5824 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005825 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005826__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005827{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005828 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005829 struct stream *s;
5830 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005831
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005832 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005833
Christopher Faulet700d9e82020-01-31 12:21:52 +01005834 /* If the flags NOTERM is set, we cannot terminate the session, so we
5835 * just end the execution of the current lua code. */
5836 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005837 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005838
Christopher Faulet700d9e82020-01-31 12:21:52 +01005839 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005840 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005841 struct channel *req = &s->req;
5842 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005843
Christopher Faulet700d9e82020-01-31 12:21:52 +01005844 channel_auto_read(req);
5845 channel_abort(req);
5846 channel_auto_close(req);
5847 channel_erase(req);
5848
5849 res->wex = tick_add_ifset(now_ms, res->wto);
5850 channel_auto_read(res);
5851 channel_auto_close(res);
5852 channel_shutr_now(res);
5853
5854 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5855 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005856 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005857
Christopher Faulet700d9e82020-01-31 12:21:52 +01005858 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5859 /* No reply or invalid reply */
5860 s->txn->status = 0;
5861 http_reply_and_close(s, 0, NULL);
5862 }
5863 else {
5864 /* Remove extra args to have the reply on top of the stack */
5865 if (lua_gettop(L) > 2)
5866 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005867
Christopher Faulet700d9e82020-01-31 12:21:52 +01005868 if (!hlua_txn_forward_reply(L, s)) {
5869 if (!(s->flags & SF_ERR_MASK))
5870 s->flags |= SF_ERR_PRXCOND;
5871 lua_pushinteger(L, ACT_RET_ERR);
5872 WILL_LJMP(hlua_done(L));
5873 return 0; /* Never reached */
5874 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005875 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005876
Christopher Faulet700d9e82020-01-31 12:21:52 +01005877 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5878 if (htxn->dir == SMP_OPT_DIR_REQ) {
5879 /* let's log the request time */
5880 s->logs.tv_request = now;
5881 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5882 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5883 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005884
Christopher Faulet700d9e82020-01-31 12:21:52 +01005885 done:
5886 if (!(s->flags & SF_ERR_MASK))
5887 s->flags |= SF_ERR_LOCAL;
5888 if (!(s->flags & SF_FINST_MASK))
5889 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005890
Christopher Faulet4ad73102020-03-05 11:07:31 +01005891 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005892 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005893 return 0;
5894}
5895
Christopher Faulet700d9e82020-01-31 12:21:52 +01005896/*
5897 *
5898 *
5899 * Class REPLY
5900 *
5901 *
5902 */
5903
5904/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5905 * free slots, the function fails and returns 0;
5906 */
5907static int hlua_txn_reply_new(lua_State *L)
5908{
5909 struct hlua_txn *htxn;
5910 const char *reason, *body = NULL;
5911 int ret, status;
5912
5913 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005914 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005915 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5916 WILL_LJMP(lua_error(L));
5917 }
5918
5919 /* Default value */
5920 status = 200;
5921 reason = http_get_reason(status);
5922
5923 if (lua_istable(L, 2)) {
5924 /* load status and reason from the table argument at index 2 */
5925 ret = lua_getfield(L, 2, "status");
5926 if (ret == LUA_TNIL)
5927 goto reason;
5928 else if (ret != LUA_TNUMBER) {
5929 /* invalid status: ignore the reason */
5930 goto body;
5931 }
5932 status = lua_tointeger(L, -1);
5933
5934 reason:
5935 lua_pop(L, 1); /* restore the stack: remove status */
5936 ret = lua_getfield(L, 2, "reason");
5937 if (ret == LUA_TSTRING)
5938 reason = lua_tostring(L, -1);
5939
5940 body:
5941 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5942 ret = lua_getfield(L, 2, "body");
5943 if (ret == LUA_TSTRING)
5944 body = lua_tostring(L, -1);
5945 lua_pop(L, 1); /* restore the stack: remove body */
5946 }
5947
5948 /* Create the Reply table */
5949 lua_newtable(L);
5950
5951 /* Add status element */
5952 lua_pushstring(L, "status");
5953 lua_pushinteger(L, status);
5954 lua_settable(L, -3);
5955
5956 /* Add reason element */
5957 reason = http_get_reason(status);
5958 lua_pushstring(L, "reason");
5959 lua_pushstring(L, reason);
5960 lua_settable(L, -3);
5961
5962 /* Add body element, nil if undefined */
5963 lua_pushstring(L, "body");
5964 if (body)
5965 lua_pushstring(L, body);
5966 else
5967 lua_pushnil(L);
5968 lua_settable(L, -3);
5969
5970 /* Add headers element */
5971 lua_pushstring(L, "headers");
5972 lua_newtable(L);
5973
5974 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5975 if (lua_istable(L, 2)) {
5976 /* load headers from the table argument at index 2. If it is a table, copy it. */
5977 ret = lua_getfield(L, 2, "headers");
5978 if (ret == LUA_TTABLE) {
5979 /* stack: [ ... <headers:table>, <table> ] */
5980 lua_pushnil(L);
5981 while (lua_next(L, -2) != 0) {
5982 /* stack: [ ... <headers:table>, <table>, k, v] */
5983 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5984 /* invalid value type, skip it */
5985 lua_pop(L, 1);
5986 continue;
5987 }
5988
5989
5990 /* Duplicate the key and swap it with the value. */
5991 lua_pushvalue(L, -2);
5992 lua_insert(L, -2);
5993 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5994
5995 lua_newtable(L);
5996 lua_insert(L, -2);
5997 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5998
5999 if (lua_isstring(L, -1)) {
6000 /* push the value in the inner table */
6001 lua_rawseti(L, -2, 1);
6002 }
6003 else { /* table */
6004 lua_pushnil(L);
6005 while (lua_next(L, -2) != 0) {
6006 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6007 if (!lua_isstring(L, -1)) {
6008 /* invalid value type, skip it*/
6009 lua_pop(L, 1);
6010 continue;
6011 }
6012 /* push the value in the inner table */
6013 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6014 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6015 }
6016 lua_pop(L, 1);
6017 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6018 }
6019
6020 /* push (k,v) on the stack in the headers table:
6021 * stack: [ ... <headers:table>, <table>, k, k, v ]
6022 */
6023 lua_settable(L, -5);
6024 /* stack: [ ... <headers:table>, <table>, k ] */
6025 }
6026 }
6027 lua_pop(L, 1);
6028 }
6029 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6030 lua_settable(L, -3);
6031 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6032
6033 /* Pop a class sesison metatable and affect it to the userdata. */
6034 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6035 lua_setmetatable(L, -2);
6036 return 1;
6037}
6038
6039/* Set the reply status code, and optionally the reason. If no reason is
6040 * provided, the default one corresponding to the status code is used.
6041 */
6042__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6043{
6044 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6045 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6046
6047 /* First argument (self) must be a table */
6048 luaL_checktype(L, 1, LUA_TTABLE);
6049
6050 if (status < 100 || status > 599) {
6051 lua_pushboolean(L, 0);
6052 return 1;
6053 }
6054 if (!reason)
6055 reason = http_get_reason(status);
6056
6057 lua_pushinteger(L, status);
6058 lua_setfield(L, 1, "status");
6059
6060 lua_pushstring(L, reason);
6061 lua_setfield(L, 1, "reason");
6062
6063 lua_pushboolean(L, 1);
6064 return 1;
6065}
6066
6067/* Add a header into the reply object. Each header name is associated to an
6068 * array of values in the "headers" table. If the header name is not found, a
6069 * new entry is created.
6070 */
6071__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6072{
6073 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6074 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6075 int ret;
6076
6077 /* First argument (self) must be a table */
6078 luaL_checktype(L, 1, LUA_TTABLE);
6079
6080 /* Push in the stack the "headers" entry. */
6081 ret = lua_getfield(L, 1, "headers");
6082 if (ret != LUA_TTABLE) {
6083 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6084 WILL_LJMP(lua_error(L));
6085 }
6086
6087 /* check if the header is already registered. If not, register it. */
6088 ret = lua_getfield(L, -1, name);
6089 if (ret == LUA_TNIL) {
6090 /* Entry not found. */
6091 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6092
6093 /* Insert the new header name in the array in the top of the stack.
6094 * It left the new array in the top of the stack.
6095 */
6096 lua_newtable(L);
6097 lua_pushstring(L, name);
6098 lua_pushvalue(L, -2);
6099 lua_settable(L, -4);
6100 }
6101 else if (ret != LUA_TTABLE) {
6102 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6103 WILL_LJMP(lua_error(L));
6104 }
6105
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006106 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006107 * the header value as new entry.
6108 */
6109 lua_pushstring(L, value);
6110 ret = lua_rawlen(L, -2);
6111 lua_rawseti(L, -2, ret + 1);
6112
6113 lua_pushboolean(L, 1);
6114 return 1;
6115}
6116
6117/* Remove all occurrences of a given header name. */
6118__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6119{
6120 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6121 int ret;
6122
6123 /* First argument (self) must be a table */
6124 luaL_checktype(L, 1, LUA_TTABLE);
6125
6126 /* Push in the stack the "headers" entry. */
6127 ret = lua_getfield(L, 1, "headers");
6128 if (ret != LUA_TTABLE) {
6129 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6130 WILL_LJMP(lua_error(L));
6131 }
6132
6133 lua_pushstring(L, name);
6134 lua_pushnil(L);
6135 lua_settable(L, -3);
6136
6137 lua_pushboolean(L, 1);
6138 return 1;
6139}
6140
6141/* Set the reply's body. Overwrite any existing entry. */
6142__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6143{
6144 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6145
6146 /* First argument (self) must be a table */
6147 luaL_checktype(L, 1, LUA_TTABLE);
6148
6149 lua_pushstring(L, payload);
6150 lua_setfield(L, 1, "body");
6151
6152 lua_pushboolean(L, 1);
6153 return 1;
6154}
6155
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006156__LJMP static int hlua_log(lua_State *L)
6157{
6158 int level;
6159 const char *msg;
6160
6161 MAY_LJMP(check_args(L, 2, "log"));
6162 level = MAY_LJMP(luaL_checkinteger(L, 1));
6163 msg = MAY_LJMP(luaL_checkstring(L, 2));
6164
6165 if (level < 0 || level >= NB_LOG_LEVELS)
6166 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6167
6168 hlua_sendlog(NULL, level, msg);
6169 return 0;
6170}
6171
6172__LJMP static int hlua_log_debug(lua_State *L)
6173{
6174 const char *msg;
6175
6176 MAY_LJMP(check_args(L, 1, "debug"));
6177 msg = MAY_LJMP(luaL_checkstring(L, 1));
6178 hlua_sendlog(NULL, LOG_DEBUG, msg);
6179 return 0;
6180}
6181
6182__LJMP static int hlua_log_info(lua_State *L)
6183{
6184 const char *msg;
6185
6186 MAY_LJMP(check_args(L, 1, "info"));
6187 msg = MAY_LJMP(luaL_checkstring(L, 1));
6188 hlua_sendlog(NULL, LOG_INFO, msg);
6189 return 0;
6190}
6191
6192__LJMP static int hlua_log_warning(lua_State *L)
6193{
6194 const char *msg;
6195
6196 MAY_LJMP(check_args(L, 1, "warning"));
6197 msg = MAY_LJMP(luaL_checkstring(L, 1));
6198 hlua_sendlog(NULL, LOG_WARNING, msg);
6199 return 0;
6200}
6201
6202__LJMP static int hlua_log_alert(lua_State *L)
6203{
6204 const char *msg;
6205
6206 MAY_LJMP(check_args(L, 1, "alert"));
6207 msg = MAY_LJMP(luaL_checkstring(L, 1));
6208 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006209 return 0;
6210}
6211
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006212__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006213{
6214 int wakeup_ms = lua_tointeger(L, -1);
6215 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006216 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006217 return 0;
6218}
6219
6220__LJMP static int hlua_sleep(lua_State *L)
6221{
6222 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006223 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006224
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006225 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006226
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006227 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006228 wakeup_ms = tick_add(now_ms, delay);
6229 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006230
Willy Tarreau9635e032018-10-16 17:52:55 +02006231 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006232 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006233}
6234
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006235__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006236{
6237 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006238 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006239
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006240 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006241
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006242 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006243 wakeup_ms = tick_add(now_ms, delay);
6244 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006245
Willy Tarreau9635e032018-10-16 17:52:55 +02006246 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006247 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006248}
6249
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006250/* This functionis an LUA binding. it permits to give back
6251 * the hand at the HAProxy scheduler. It is used when the
6252 * LUA processing consumes a lot of time.
6253 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006254__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006255{
6256 return 0;
6257}
6258
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006259__LJMP static int hlua_yield(lua_State *L)
6260{
Willy Tarreau9635e032018-10-16 17:52:55 +02006261 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006262 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006263}
6264
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006265/* This function change the nice of the currently executed
6266 * task. It is used set low or high priority at the current
6267 * task.
6268 */
Willy Tarreau59551662015-03-10 14:23:13 +01006269__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006270{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006271 struct hlua *hlua;
6272 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006273
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006274 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006275 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006276
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006277 /* Get hlua struct, or NULL if we execute from main lua state */
6278 hlua = hlua_gethlua(L);
6279
6280 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006281 if (!hlua || !hlua->task)
6282 return 0;
6283
6284 if (nice < -1024)
6285 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006286 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006287 nice = 1024;
6288
6289 hlua->task->nice = nice;
6290 return 0;
6291}
6292
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006293/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006294 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6295 * return an E_AGAIN signal, the emmiter of this signal must set a
6296 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006297 *
6298 * Task wrapper are longjmp safe because the only one Lua code
6299 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006300 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006301struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006302{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006303 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006304 enum hlua_exec status;
6305
Christopher Faulet5bc99722018-04-25 10:34:45 +02006306 if (task->thread_mask == MAX_THREADS_MASK)
6307 task_set_affinity(task, tid_bit);
6308
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006309 /* If it is the first call to the task, we must initialize the
6310 * execution timeouts.
6311 */
6312 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006313 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006314
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006315 /* Execute the Lua code. */
6316 status = hlua_ctx_resume(hlua, 1);
6317
6318 switch (status) {
6319 /* finished or yield */
6320 case HLUA_E_OK:
6321 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006322 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006323 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006324 break;
6325
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006326 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006327 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006328 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006329 break;
6330
6331 /* finished with error. */
6332 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006333 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006334 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006335 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006336 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006337 break;
6338
6339 case HLUA_E_ERR:
6340 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006341 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006342 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006343 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006344 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006345 break;
6346 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006347 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006348}
6349
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006350/* This function is an LUA binding that register LUA function to be
6351 * executed after the HAProxy configuration parsing and before the
6352 * HAProxy scheduler starts. This function expect only one LUA
6353 * argument that is a function. This function returns nothing, but
6354 * throws if an error is encountered.
6355 */
6356__LJMP static int hlua_register_init(lua_State *L)
6357{
6358 struct hlua_init_function *init;
6359 int ref;
6360
6361 MAY_LJMP(check_args(L, 1, "register_init"));
6362
6363 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6364
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006365 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006366 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006367 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006368
6369 init->function_ref = ref;
Thierry Fournierc7492592020-11-28 23:57:24 +01006370 LIST_ADDQ(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006371 return 0;
6372}
6373
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006374/* This functio is an LUA binding. It permits to register a task
6375 * executed in parallel of the main HAroxy activity. The task is
6376 * created and it is set in the HAProxy scheduler. It can be called
6377 * from the "init" section, "post init" or during the runtime.
6378 *
6379 * Lua prototype:
6380 *
6381 * <none> core.register_task(<function>)
6382 */
6383static int hlua_register_task(lua_State *L)
6384{
6385 struct hlua *hlua;
6386 struct task *task;
6387 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006388 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006389
6390 MAY_LJMP(check_args(L, 1, "register_task"));
6391
6392 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6393
Thierry Fournier75fc0292020-11-28 13:18:56 +01006394 /* Get the reference state. If the reference is NULL, L is the master
6395 * state, otherwise hlua->T is.
6396 */
6397 hlua = hlua_gethlua(L);
6398 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006399 /* we are in runtime processing */
6400 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006401 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006402 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006403 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006404
Willy Tarreaubafbe012017-11-24 17:34:44 +01006405 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006406 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006407 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006408
Thierry Fournier59f11be2020-11-29 00:37:41 +01006409 /* We are in the common lua state, execute the task anywhere,
6410 * otherwise, inherit the current thread identifier
6411 */
6412 if (state_id == 0)
6413 task = task_new(MAX_THREADS_MASK);
6414 else
6415 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006416 if (!task)
6417 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6418
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006419 task->context = hlua;
6420 task->process = hlua_process_task;
6421
Thierry Fournier021d9862020-11-28 23:42:03 +01006422 if (!hlua_ctx_init(hlua, state_id, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006423 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006424
6425 /* Restore the function in the stack. */
6426 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6427 hlua->nargs = 0;
6428
6429 /* Schedule task. */
6430 task_schedule(task, now_ms);
6431
6432 return 0;
6433}
6434
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006435/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6436 * doesn't allow "yield" functions because the HAProxy engine cannot
6437 * resume converters.
6438 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006439static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006440{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006441 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006442 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006443 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006444
Willy Tarreaube508f12016-03-10 11:47:01 +01006445 if (!stream)
6446 return 0;
6447
Willy Tarreau87b09662015-04-03 00:22:06 +02006448 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006449 * Lua context can be not initialized. This behavior
6450 * permits to save performances because a systematic
6451 * Lua initialization cause 5% performances loss.
6452 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006453 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006454 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006455 if (!stream->hlua) {
6456 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6457 return 0;
6458 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006459 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006460 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6461 return 0;
6462 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006463 }
6464
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006465 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006466 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006467
6468 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006469 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006470 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6471 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006472 else
6473 error = "critical error";
6474 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006475 return 0;
6476 }
6477
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006478 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006479 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006480 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006481 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006482 return 0;
6483 }
6484
6485 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006486 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006487
6488 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006489 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006490 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006491 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006492 return 0;
6493 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006494 hlua_smp2lua(stream->hlua->T, smp);
6495 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006496
6497 /* push keywords in the stack. */
6498 if (arg_p) {
6499 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006500 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006501 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006502 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006503 return 0;
6504 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006505 hlua_arg2lua(stream->hlua->T, arg_p);
6506 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006507 }
6508 }
6509
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006510 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006511 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006512
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006513 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006514 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006515 }
6516
6517 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006518 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006519 /* finished. */
6520 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006521 /* If the stack is empty, the function fails. */
6522 if (lua_gettop(stream->hlua->T) <= 0)
6523 return 0;
6524
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006525 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006526 hlua_lua2smp(stream->hlua->T, -1, smp);
6527 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006528 return 1;
6529
6530 /* yield. */
6531 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006532 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006533 return 0;
6534
6535 /* finished with error. */
6536 case HLUA_E_ERRMSG:
6537 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006538 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006539 fcn->name, lua_tostring(stream->hlua->T, -1));
6540 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006541 return 0;
6542
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006543 case HLUA_E_ETMOUT:
6544 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6545 return 0;
6546
6547 case HLUA_E_NOMEM:
6548 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6549 return 0;
6550
6551 case HLUA_E_YIELD:
6552 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6553 return 0;
6554
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006555 case HLUA_E_ERR:
6556 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006557 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006558 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006559
6560 default:
6561 return 0;
6562 }
6563}
6564
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006565/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6566 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006567 * resume sample-fetches. This function will be called by the sample
6568 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006569 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006570static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6571 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006572{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006573 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006574 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006575 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006576 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006577
Willy Tarreaube508f12016-03-10 11:47:01 +01006578 if (!stream)
6579 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006580
Willy Tarreau87b09662015-04-03 00:22:06 +02006581 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006582 * Lua context can be not initialized. This behavior
6583 * permits to save performances because a systematic
6584 * Lua initialization cause 5% performances loss.
6585 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006586 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006587 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006588 if (!stream->hlua) {
6589 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6590 return 0;
6591 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006592 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006593 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6594 return 0;
6595 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006596 }
6597
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006598 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006599 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006600
6601 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006602 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006603 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6604 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006605 else
6606 error = "critical error";
6607 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006608 return 0;
6609 }
6610
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006611 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006612 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006613 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006614 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006615 return 0;
6616 }
6617
6618 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006619 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006620
6621 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006622 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006623 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006624 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006625 return 0;
6626 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006627 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006628
6629 /* push keywords in the stack. */
6630 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6631 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006632 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006633 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006634 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006635 return 0;
6636 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006637 hlua_arg2lua(stream->hlua->T, arg_p);
6638 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006639 }
6640
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006641 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006642 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006643
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006644 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006645 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006646 }
6647
6648 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006649 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006650 /* finished. */
6651 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006652 /* If the stack is empty, the function fails. */
6653 if (lua_gettop(stream->hlua->T) <= 0)
6654 return 0;
6655
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006656 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006657 hlua_lua2smp(stream->hlua->T, -1, smp);
6658 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006659
6660 /* Set the end of execution flag. */
6661 smp->flags &= ~SMP_F_MAY_CHANGE;
6662 return 1;
6663
6664 /* yield. */
6665 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006666 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006667 return 0;
6668
6669 /* finished with error. */
6670 case HLUA_E_ERRMSG:
6671 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006672 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006673 fcn->name, lua_tostring(stream->hlua->T, -1));
6674 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006675 return 0;
6676
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006677 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006678 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6679 return 0;
6680
6681 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006682 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6683 return 0;
6684
6685 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006686 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6687 return 0;
6688
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006689 case HLUA_E_ERR:
6690 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006691 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006692 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006693
6694 default:
6695 return 0;
6696 }
6697}
6698
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006699/* This function is an LUA binding used for registering
6700 * "sample-conv" functions. It expects a converter name used
6701 * in the haproxy configuration file, and an LUA function.
6702 */
6703__LJMP static int hlua_register_converters(lua_State *L)
6704{
6705 struct sample_conv_kw_list *sck;
6706 const char *name;
6707 int ref;
6708 int len;
6709 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006710 struct sample_conv *sc;
6711 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006712
6713 MAY_LJMP(check_args(L, 2, "register_converters"));
6714
6715 /* First argument : converter name. */
6716 name = MAY_LJMP(luaL_checkstring(L, 1));
6717
6718 /* Second argument : lua function. */
6719 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6720
Thierry Fournierf67442e2020-11-28 20:41:07 +01006721 /* Check if the converter is already registered */
6722 trash = get_trash_chunk();
6723 chunk_printf(trash, "lua.%s", name);
6724 sc = find_sample_conv(trash->area, trash->data);
6725 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006726 fcn = sc->private;
6727 if (fcn->function_ref[hlua_state_id] != -1) {
6728 ha_warning("Trying to register converter 'lua.%s' more than once. "
6729 "This will become a hard error in version 2.5.\n", name);
6730 }
6731 fcn->function_ref[hlua_state_id] = ref;
6732 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006733 }
6734
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006735 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006736 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006737 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006738 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006739 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006740 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006741 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006742
6743 /* Fill fcn. */
6744 fcn->name = strdup(name);
6745 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006746 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01006747 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006748
6749 /* List head */
6750 sck->list.n = sck->list.p = NULL;
6751
6752 /* converter keyword. */
6753 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006754 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006755 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006756 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006757
6758 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6759 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006760 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 +01006761 sck->kw[0].val_args = NULL;
6762 sck->kw[0].in_type = SMP_T_STR;
6763 sck->kw[0].out_type = SMP_T_STR;
6764 sck->kw[0].private = fcn;
6765
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006766 /* Register this new converter */
6767 sample_register_convs(sck);
6768
6769 return 0;
6770}
6771
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006772/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006773 * "sample-fetch" functions. It expects a converter name used
6774 * in the haproxy configuration file, and an LUA function.
6775 */
6776__LJMP static int hlua_register_fetches(lua_State *L)
6777{
6778 const char *name;
6779 int ref;
6780 int len;
6781 struct sample_fetch_kw_list *sfk;
6782 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006783 struct sample_fetch *sf;
6784 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006785
6786 MAY_LJMP(check_args(L, 2, "register_fetches"));
6787
6788 /* First argument : sample-fetch name. */
6789 name = MAY_LJMP(luaL_checkstring(L, 1));
6790
6791 /* Second argument : lua function. */
6792 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6793
Thierry Fournierf67442e2020-11-28 20:41:07 +01006794 /* Check if the sample-fetch is already registered */
6795 trash = get_trash_chunk();
6796 chunk_printf(trash, "lua.%s", name);
6797 sf = find_sample_fetch(trash->area, trash->data);
6798 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006799 fcn = sf->private;
6800 if (fcn->function_ref[hlua_state_id] != -1) {
6801 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6802 "This will become a hard error in version 2.5.\n", name);
6803 }
6804 fcn->function_ref[hlua_state_id] = ref;
6805 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006806 }
6807
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006808 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006809 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006810 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006811 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006812 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006813 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006814 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006815
6816 /* Fill fcn. */
6817 fcn->name = strdup(name);
6818 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006819 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01006820 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006821
6822 /* List head */
6823 sfk->list.n = sfk->list.p = NULL;
6824
6825 /* sample-fetch keyword. */
6826 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006827 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006828 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006829 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006830
6831 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6832 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006833 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 +01006834 sfk->kw[0].val_args = NULL;
6835 sfk->kw[0].out_type = SMP_T_STR;
6836 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6837 sfk->kw[0].val = 0;
6838 sfk->kw[0].private = fcn;
6839
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006840 /* Register this new fetch. */
6841 sample_register_fetches(sfk);
6842
6843 return 0;
6844}
6845
Christopher Faulet501465d2020-02-26 14:54:16 +01006846/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006847 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006848__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006849{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006850 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006851 unsigned int delay;
6852 unsigned int wakeup_ms;
6853
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006854 /* Get hlua struct, or NULL if we execute from main lua state */
6855 hlua = hlua_gethlua(L);
6856 if (!hlua) {
6857 return 0;
6858 }
6859
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006860 MAY_LJMP(check_args(L, 1, "wake_time"));
6861
6862 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6863 wakeup_ms = tick_add(now_ms, delay);
6864 hlua->wake_time = wakeup_ms;
6865 return 0;
6866}
6867
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006868/* This function is a wrapper to execute each LUA function declared as an action
6869 * wrapper during the initialisation period. This function may return any
6870 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6871 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6872 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006873 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006874static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006875 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006876{
6877 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006878 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006879 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006880 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006881
6882 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006883 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6884 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6885 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6886 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006887 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006888 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006889 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006890 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006891
Willy Tarreau87b09662015-04-03 00:22:06 +02006892 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006893 * Lua context can be not initialized. This behavior
6894 * permits to save performances because a systematic
6895 * Lua initialization cause 5% performances loss.
6896 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006897 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006898 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006899 if (!s->hlua) {
6900 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006901 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006902 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006903 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006904 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 +01006905 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006906 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006907 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006908 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006909 }
6910
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006911 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006912 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006913
6914 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006915 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006916 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6917 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006918 else
6919 error = "critical error";
6920 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006921 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006922 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006923 }
6924
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006925 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006926 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006927 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006928 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006929 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006930 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006931 }
6932
6933 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006934 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 +01006935
Willy Tarreau87b09662015-04-03 00:22:06 +02006936 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006937 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006938 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006939 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006940 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006941 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006942 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006943 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006944
6945 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006946 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006947 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006948 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006949 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006950 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006951 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006952 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006953 lua_pushstring(s->hlua->T, *arg);
6954 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006955 }
6956
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006957 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006958 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006959
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006960 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006961 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006962 }
6963
6964 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006965 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006966 /* finished. */
6967 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006968 /* Catch the return value */
6969 if (lua_gettop(s->hlua->T) > 0)
6970 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006971
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006972 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006973 if (act_ret == ACT_RET_YIELD) {
6974 if (flags & ACT_OPT_FINAL)
6975 goto err_yield;
6976
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006977 if (dir == SMP_OPT_DIR_REQ)
6978 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6979 s->hlua->wake_time);
6980 else
6981 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6982 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006983 }
6984 goto end;
6985
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006986 /* yield. */
6987 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006988 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006989 if (dir == SMP_OPT_DIR_REQ)
6990 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6991 s->hlua->wake_time);
6992 else
6993 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6994 s->hlua->wake_time);
6995
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006996 /* Some actions can be wake up when a "write" event
6997 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006998 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006999 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007000 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007001 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007002 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007003 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007004 act_ret = ACT_RET_YIELD;
7005 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007006
7007 /* finished with error. */
7008 case HLUA_E_ERRMSG:
7009 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007010 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007011 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007012 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007013 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007014
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007015 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007016 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007017 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007018
7019 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007020 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007021 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007022
7023 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007024 err_yield:
7025 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007026 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007027 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007028 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007029
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007030 case HLUA_E_ERR:
7031 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007032 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007033 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007034
7035 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007036 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007037 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007038
7039 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007040 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007041 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007042 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007043}
7044
Olivier Houchard9f6af332018-05-25 14:04:04 +02007045struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007046{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007047 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007048
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007049 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007050 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007051 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007052}
7053
7054static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7055{
7056 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007057 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007058 struct task *task;
7059 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007060 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007061
Willy Tarreaubafbe012017-11-24 17:34:44 +01007062 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007063 if (!hlua) {
7064 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007065 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007066 return 0;
7067 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007068 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007069 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007070 ctx->ctx.hlua_apptcp.flags = 0;
7071
7072 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007073 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007074 if (!task) {
7075 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007076 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007077 return 0;
7078 }
7079 task->nice = 0;
7080 task->context = ctx;
7081 task->process = hlua_applet_wakeup;
7082 ctx->ctx.hlua_apptcp.task = task;
7083
7084 /* In the execution wrappers linked with a stream, the
7085 * Lua context can be not initialized. This behavior
7086 * permits to save performances because a systematic
7087 * Lua initialization cause 5% performances loss.
7088 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007089 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 +02007090 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007091 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007092 return 0;
7093 }
7094
7095 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007096 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007097
7098 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007099 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007100 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7101 error = lua_tostring(hlua->T, -1);
7102 else
7103 error = "critical error";
7104 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007105 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007106 return 0;
7107 }
7108
7109 /* Check stack available size. */
7110 if (!lua_checkstack(hlua->T, 1)) {
7111 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007112 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007113 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007114 return 0;
7115 }
7116
7117 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007118 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007119
7120 /* Create and and push object stream in the stack. */
7121 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7122 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007123 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007124 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007125 return 0;
7126 }
7127 hlua->nargs = 1;
7128
7129 /* push keywords in the stack. */
7130 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7131 if (!lua_checkstack(hlua->T, 1)) {
7132 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007133 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007134 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007135 return 0;
7136 }
7137 lua_pushstring(hlua->T, *arg);
7138 hlua->nargs++;
7139 }
7140
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007141 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007142
7143 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007144 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007145 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007146
7147 return 1;
7148}
7149
Willy Tarreau60409db2019-08-21 14:14:50 +02007150void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007151{
7152 struct stream_interface *si = ctx->owner;
7153 struct stream *strm = si_strm(si);
7154 struct channel *res = si_ic(si);
7155 struct act_rule *rule = ctx->rule;
7156 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007157 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007158
7159 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007160 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7161 /* eat the whole request */
7162 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007163 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007164 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007165
7166 /* If the stream is disconnect or closed, ldo nothing. */
7167 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7168 return;
7169
7170 /* Execute the function. */
7171 switch (hlua_ctx_resume(hlua, 1)) {
7172 /* finished. */
7173 case HLUA_E_OK:
7174 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7175
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007176 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007177 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007178 res->flags |= CF_READ_NULL;
7179 si_shutr(si);
7180 return;
7181
7182 /* yield. */
7183 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007184 if (hlua->wake_time != TICK_ETERNITY)
7185 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007186 return;
7187
7188 /* finished with error. */
7189 case HLUA_E_ERRMSG:
7190 /* Display log. */
7191 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007192 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007193 lua_pop(hlua->T, 1);
7194 goto error;
7195
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007196 case HLUA_E_ETMOUT:
7197 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007198 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007199 goto error;
7200
7201 case HLUA_E_NOMEM:
7202 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007203 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007204 goto error;
7205
7206 case HLUA_E_YIELD: /* unexpected */
7207 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007208 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007209 goto error;
7210
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007211 case HLUA_E_ERR:
7212 /* Display log. */
7213 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007214 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007215 goto error;
7216
7217 default:
7218 goto error;
7219 }
7220
7221error:
7222
7223 /* For all other cases, just close the stream. */
7224 si_shutw(si);
7225 si_shutr(si);
7226 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7227}
7228
7229static void hlua_applet_tcp_release(struct appctx *ctx)
7230{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007231 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007232 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007233 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007234 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007235}
7236
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007237/* The function returns 1 if the initialisation is complete, 0 if
7238 * an errors occurs and -1 if more data are required for initializing
7239 * the applet.
7240 */
7241static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7242{
7243 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007244 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007245 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007246 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007247 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007248 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007249
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007250 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007251 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007252 if (!hlua) {
7253 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007254 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007255 return 0;
7256 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007257 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007258 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007259 ctx->ctx.hlua_apphttp.left_bytes = -1;
7260 ctx->ctx.hlua_apphttp.flags = 0;
7261
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007262 if (txn->req.flags & HTTP_MSGF_VER_11)
7263 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7264
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007265 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007266 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007267 if (!task) {
7268 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007269 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007270 return 0;
7271 }
7272 task->nice = 0;
7273 task->context = ctx;
7274 task->process = hlua_applet_wakeup;
7275 ctx->ctx.hlua_apphttp.task = task;
7276
7277 /* In the execution wrappers linked with a stream, the
7278 * Lua context can be not initialized. This behavior
7279 * permits to save performances because a systematic
7280 * Lua initialization cause 5% performances loss.
7281 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007282 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 +02007283 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007284 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007285 return 0;
7286 }
7287
7288 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007289 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007290
7291 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007292 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007293 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7294 error = lua_tostring(hlua->T, -1);
7295 else
7296 error = "critical error";
7297 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007298 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007299 return 0;
7300 }
7301
7302 /* Check stack available size. */
7303 if (!lua_checkstack(hlua->T, 1)) {
7304 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007305 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007306 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007307 return 0;
7308 }
7309
7310 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007311 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007312
7313 /* Create and and push object stream in the stack. */
7314 if (!hlua_applet_http_new(hlua->T, ctx)) {
7315 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007316 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007317 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007318 return 0;
7319 }
7320 hlua->nargs = 1;
7321
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007322 /* push keywords in the stack. */
7323 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7324 if (!lua_checkstack(hlua->T, 1)) {
7325 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007326 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007327 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007328 return 0;
7329 }
7330 lua_pushstring(hlua->T, *arg);
7331 hlua->nargs++;
7332 }
7333
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007334 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007335
7336 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007337 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007338
7339 return 1;
7340}
7341
Willy Tarreau60409db2019-08-21 14:14:50 +02007342void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007343{
7344 struct stream_interface *si = ctx->owner;
7345 struct stream *strm = si_strm(si);
7346 struct channel *req = si_oc(si);
7347 struct channel *res = si_ic(si);
7348 struct act_rule *rule = ctx->rule;
7349 struct proxy *px = strm->be;
7350 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7351 struct htx *req_htx, *res_htx;
7352
7353 res_htx = htx_from_buf(&res->buf);
7354
7355 /* If the stream is disconnect or closed, ldo nothing. */
7356 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7357 goto out;
7358
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007359 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007360 if (!b_size(&res->buf)) {
7361 si_rx_room_blk(si);
7362 goto out;
7363 }
7364 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007365 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007366 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7367
7368 /* Set the currently running flag. */
7369 if (!HLUA_IS_RUNNING(hlua) &&
7370 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7371 struct htx_blk *blk;
7372 size_t count = co_data(req);
7373
7374 if (!count) {
7375 si_cant_get(si);
7376 goto out;
7377 }
7378
7379 /* We need to flush the request header. This left the body for
7380 * the Lua.
7381 */
7382 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007383 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007384 while (count && blk) {
7385 enum htx_blk_type type = htx_get_blk_type(blk);
7386 uint32_t sz = htx_get_blksz(blk);
7387
7388 if (sz > count) {
7389 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007390 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007391 goto out;
7392 }
7393
7394 count -= sz;
7395 co_set_data(req, co_data(req) - sz);
7396 blk = htx_remove_blk(req_htx, blk);
7397
7398 if (type == HTX_BLK_EOH)
7399 break;
7400 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007401 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007402 }
7403
7404 /* Executes The applet if it is not done. */
7405 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7406
7407 /* Execute the function. */
7408 switch (hlua_ctx_resume(hlua, 1)) {
7409 /* finished. */
7410 case HLUA_E_OK:
7411 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7412 break;
7413
7414 /* yield. */
7415 case HLUA_E_AGAIN:
7416 if (hlua->wake_time != TICK_ETERNITY)
7417 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007418 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007419
7420 /* finished with error. */
7421 case HLUA_E_ERRMSG:
7422 /* Display log. */
7423 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007424 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007425 lua_pop(hlua->T, 1);
7426 goto error;
7427
7428 case HLUA_E_ETMOUT:
7429 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007430 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007431 goto error;
7432
7433 case HLUA_E_NOMEM:
7434 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007435 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007436 goto error;
7437
7438 case HLUA_E_YIELD: /* unexpected */
7439 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007440 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007441 goto error;
7442
7443 case HLUA_E_ERR:
7444 /* Display log. */
7445 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007446 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007447 goto error;
7448
7449 default:
7450 goto error;
7451 }
7452 }
7453
7454 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007455 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7456 goto done;
7457
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007458 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7459 goto error;
7460
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007461 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
7462 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007463 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7464 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007465 }
7466
7467 done:
7468 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007469 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007470 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007471 si_shutr(si);
7472 }
7473
7474 /* eat the whole request */
7475 if (co_data(req)) {
7476 req_htx = htx_from_buf(&req->buf);
7477 co_htx_skip(req, req_htx, co_data(req));
7478 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007479 }
7480 }
7481
7482 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007483 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007484 return;
7485
7486 error:
7487
7488 /* If we are in HTTP mode, and we are not send any
7489 * data, return a 500 server error in best effort:
7490 * if there is no room available in the buffer,
7491 * just close the connection.
7492 */
7493 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007494 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007495
7496 channel_erase(res);
7497 res->buf.data = b_data(err);
7498 memcpy(res->buf.area, b_head(err), b_data(err));
7499 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007500 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007501 }
7502 if (!(strm->flags & SF_ERR_MASK))
7503 strm->flags |= SF_ERR_RESOURCE;
7504 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7505 goto done;
7506}
7507
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007508static void hlua_applet_http_release(struct appctx *ctx)
7509{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007510 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007511 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007512 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007513 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007514}
7515
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007516/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007517 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007518 *
7519 * This function can fail with an abort() due to an Lua critical error.
7520 * We are in the configuration parsing process of HAProxy, this abort() is
7521 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007522 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007523static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7524 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007525{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007526 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007527 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007528
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007529 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007530 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007531 if (!rule->arg.hlua_rule) {
7532 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007533 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007534 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007535
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007536 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007537 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7538 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007539 if (!rule->arg.hlua_rule->args) {
7540 memprintf(err, "out of memory error");
7541 return ACT_RET_PRS_ERR;
7542 }
7543
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007544 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007545 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007546
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007547 /* Expect some arguments */
7548 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007549 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007550 memprintf(err, "expect %d arguments", fcn->nargs);
7551 return ACT_RET_PRS_ERR;
7552 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007553 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007554 if (!rule->arg.hlua_rule->args[i]) {
7555 memprintf(err, "out of memory error");
7556 return ACT_RET_PRS_ERR;
7557 }
7558 (*cur_arg)++;
7559 }
7560 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007561
Thierry FOURNIER42148732015-09-02 17:17:33 +02007562 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007563 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007564 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007565}
7566
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007567static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7568 struct act_rule *rule, char **err)
7569{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007570 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007571
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007572 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007573 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007574 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007575 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007576 * the call of this analyzer.
7577 */
7578 if (rule->from != ACT_F_HTTP_REQ) {
7579 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7580 return ACT_RET_PRS_ERR;
7581 }
7582
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007583 /* Memory for the rule. */
7584 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7585 if (!rule->arg.hlua_rule) {
7586 memprintf(err, "out of memory error");
7587 return ACT_RET_PRS_ERR;
7588 }
7589
7590 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007591 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007592
7593 /* TODO: later accept arguments. */
7594 rule->arg.hlua_rule->args = NULL;
7595
7596 /* Add applet pointer in the rule. */
7597 rule->applet.obj_type = OBJ_TYPE_APPLET;
7598 rule->applet.name = fcn->name;
7599 rule->applet.init = hlua_applet_http_init;
7600 rule->applet.fct = hlua_applet_http_fct;
7601 rule->applet.release = hlua_applet_http_release;
7602 rule->applet.timeout = hlua_timeout_applet;
7603
7604 return ACT_RET_PRS_OK;
7605}
7606
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007607/* This function is an LUA binding used for registering
7608 * "sample-conv" functions. It expects a converter name used
7609 * in the haproxy configuration file, and an LUA function.
7610 */
7611__LJMP static int hlua_register_action(lua_State *L)
7612{
7613 struct action_kw_list *akl;
7614 const char *name;
7615 int ref;
7616 int len;
7617 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007618 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007619 struct buffer *trash;
7620 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007621
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007622 /* Initialise the number of expected arguments at 0. */
7623 nargs = 0;
7624
7625 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7626 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007627
7628 /* First argument : converter name. */
7629 name = MAY_LJMP(luaL_checkstring(L, 1));
7630
7631 /* Second argument : environment. */
7632 if (lua_type(L, 2) != LUA_TTABLE)
7633 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7634
7635 /* Third argument : lua function. */
7636 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7637
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007638 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007639 if (lua_gettop(L) >= 4)
7640 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7641
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007642 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007643 lua_pushnil(L);
7644 while (lua_next(L, 2) != 0) {
7645 if (lua_type(L, -1) != LUA_TSTRING)
7646 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7647
Thierry Fournierf67442e2020-11-28 20:41:07 +01007648 /* Check if action exists */
7649 trash = get_trash_chunk();
7650 chunk_printf(trash, "lua.%s", name);
7651 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7652 akw = tcp_req_cont_action(trash->area);
7653 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7654 akw = tcp_res_cont_action(trash->area);
7655 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7656 akw = action_http_req_custom(trash->area);
7657 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7658 akw = action_http_res_custom(trash->area);
7659 } else {
7660 akw = NULL;
7661 }
7662 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007663 fcn = akw->private;
7664 if (fcn->function_ref[hlua_state_id] != -1) {
7665 ha_warning("Trying to register action 'lua.%s' more than once. "
7666 "This will become a hard error in version 2.5.\n", name);
7667 }
7668 fcn->function_ref[hlua_state_id] = ref;
7669
7670 /* pop the environment string. */
7671 lua_pop(L, 1);
7672 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007673 }
7674
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007675 /* Check required environment. Only accepted "http" or "tcp". */
7676 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007677 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007678 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007679 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007680 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007681 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007682 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007683
7684 /* Fill fcn. */
7685 fcn->name = strdup(name);
7686 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007687 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01007688 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007689
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007690 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007691 fcn->nargs = nargs;
7692
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007693 /* List head */
7694 akl->list.n = akl->list.p = NULL;
7695
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007696 /* action keyword. */
7697 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007698 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007699 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007700 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007701
7702 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7703
7704 akl->kw[0].match_pfx = 0;
7705 akl->kw[0].private = fcn;
7706 akl->kw[0].parse = action_register_lua;
7707
7708 /* select the action registering point. */
7709 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7710 tcp_req_cont_keywords_register(akl);
7711 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7712 tcp_res_cont_keywords_register(akl);
7713 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7714 http_req_keywords_register(akl);
7715 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7716 http_res_keywords_register(akl);
7717 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007718 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007719 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7720 "are expected.", lua_tostring(L, -1)));
7721
7722 /* pop the environment string. */
7723 lua_pop(L, 1);
7724 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007725 return ACT_RET_PRS_OK;
7726}
7727
7728static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7729 struct act_rule *rule, char **err)
7730{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007731 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007732
Christopher Faulet280f85b2019-07-15 15:02:04 +02007733 if (px->mode == PR_MODE_HTTP) {
7734 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007735 return ACT_RET_PRS_ERR;
7736 }
7737
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007738 /* Memory for the rule. */
7739 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7740 if (!rule->arg.hlua_rule) {
7741 memprintf(err, "out of memory error");
7742 return ACT_RET_PRS_ERR;
7743 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007744
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007745 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007746 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007747
7748 /* TODO: later accept arguments. */
7749 rule->arg.hlua_rule->args = NULL;
7750
7751 /* Add applet pointer in the rule. */
7752 rule->applet.obj_type = OBJ_TYPE_APPLET;
7753 rule->applet.name = fcn->name;
7754 rule->applet.init = hlua_applet_tcp_init;
7755 rule->applet.fct = hlua_applet_tcp_fct;
7756 rule->applet.release = hlua_applet_tcp_release;
7757 rule->applet.timeout = hlua_timeout_applet;
7758
7759 return 0;
7760}
7761
7762/* This function is an LUA binding used for registering
7763 * "sample-conv" functions. It expects a converter name used
7764 * in the haproxy configuration file, and an LUA function.
7765 */
7766__LJMP static int hlua_register_service(lua_State *L)
7767{
7768 struct action_kw_list *akl;
7769 const char *name;
7770 const char *env;
7771 int ref;
7772 int len;
7773 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007774 struct buffer *trash;
7775 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007776
7777 MAY_LJMP(check_args(L, 3, "register_service"));
7778
7779 /* First argument : converter name. */
7780 name = MAY_LJMP(luaL_checkstring(L, 1));
7781
7782 /* Second argument : environment. */
7783 env = MAY_LJMP(luaL_checkstring(L, 2));
7784
7785 /* Third argument : lua function. */
7786 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7787
Thierry Fournierf67442e2020-11-28 20:41:07 +01007788 /* Check for service already registered */
7789 trash = get_trash_chunk();
7790 chunk_printf(trash, "lua.%s", name);
7791 akw = service_find(trash->area);
7792 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007793 fcn = akw->private;
7794 if (fcn->function_ref[hlua_state_id] != -1) {
7795 ha_warning("Trying to register service 'lua.%s' more than once. "
7796 "This will become a hard error in version 2.5.\n", name);
7797 }
7798 fcn->function_ref[hlua_state_id] = ref;
7799 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007800 }
7801
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007802 /* Allocate and fill the sample fetch keyword struct. */
7803 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7804 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007805 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007806 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007807 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007808 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007809
7810 /* Fill fcn. */
7811 len = strlen("<lua.>") + strlen(name) + 1;
7812 fcn->name = calloc(1, len);
7813 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007814 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007815 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007816 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007817
7818 /* List head */
7819 akl->list.n = akl->list.p = NULL;
7820
7821 /* converter keyword. */
7822 len = strlen("lua.") + strlen(name) + 1;
7823 akl->kw[0].kw = calloc(1, len);
7824 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007825 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007826
7827 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7828
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007829 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007830 if (strcmp(env, "tcp") == 0)
7831 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007832 else if (strcmp(env, "http") == 0)
7833 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007834 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007835 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007836 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007837
7838 akl->kw[0].match_pfx = 0;
7839 akl->kw[0].private = fcn;
7840
7841 /* End of array. */
7842 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7843
7844 /* Register this new converter */
7845 service_keywords_register(akl);
7846
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007847 return 0;
7848}
7849
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007850/* This function initialises Lua cli handler. It copies the
7851 * arguments in the Lua stack and create channel IO objects.
7852 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007853static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007854{
7855 struct hlua *hlua;
7856 struct hlua_function *fcn;
7857 int i;
7858 const char *error;
7859
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007860 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007861 appctx->ctx.hlua_cli.fcn = private;
7862
Willy Tarreaubafbe012017-11-24 17:34:44 +01007863 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007864 if (!hlua) {
7865 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007866 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007867 }
7868 HLUA_INIT(hlua);
7869 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007870
7871 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007872 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007873 * applet_http. It is absolutely compatible.
7874 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007875 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007876 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007877 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007878 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007879 }
7880 appctx->ctx.hlua_cli.task->nice = 0;
7881 appctx->ctx.hlua_cli.task->context = appctx;
7882 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7883
7884 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01007885 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 +01007886 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007887 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007888 }
7889
7890 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007891 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007892 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7893 error = lua_tostring(hlua->T, -1);
7894 else
7895 error = "critical error";
7896 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7897 goto error;
7898 }
7899
7900 /* Check stack available size. */
7901 if (!lua_checkstack(hlua->T, 2)) {
7902 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7903 goto error;
7904 }
7905
7906 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007907 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007908
7909 /* Once the arguments parsed, the CLI is like an AppletTCP,
7910 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007911 */
7912 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7913 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7914 goto error;
7915 }
7916 hlua->nargs = 1;
7917
7918 /* push keywords in the stack. */
7919 for (i = 0; *args[i]; i++) {
7920 /* Check stack available size. */
7921 if (!lua_checkstack(hlua->T, 1)) {
7922 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7923 goto error;
7924 }
7925 lua_pushstring(hlua->T, args[i]);
7926 hlua->nargs++;
7927 }
7928
7929 /* We must initialize the execution timeouts. */
7930 hlua->max_time = hlua_timeout_session;
7931
7932 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007933 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007934
7935 /* It's ok */
7936 return 0;
7937
7938 /* It's not ok. */
7939error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007940 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007941 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007942 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007943 return 1;
7944}
7945
7946static int hlua_cli_io_handler_fct(struct appctx *appctx)
7947{
7948 struct hlua *hlua;
7949 struct stream_interface *si;
7950 struct hlua_function *fcn;
7951
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007952 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007953 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007954 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007955
7956 /* If the stream is disconnect or closed, ldo nothing. */
7957 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7958 return 1;
7959
7960 /* Execute the function. */
7961 switch (hlua_ctx_resume(hlua, 1)) {
7962
7963 /* finished. */
7964 case HLUA_E_OK:
7965 return 1;
7966
7967 /* yield. */
7968 case HLUA_E_AGAIN:
7969 /* We want write. */
7970 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007971 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007972 /* Set the timeout. */
7973 if (hlua->wake_time != TICK_ETERNITY)
7974 task_schedule(hlua->task, hlua->wake_time);
7975 return 0;
7976
7977 /* finished with error. */
7978 case HLUA_E_ERRMSG:
7979 /* Display log. */
7980 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7981 fcn->name, lua_tostring(hlua->T, -1));
7982 lua_pop(hlua->T, 1);
7983 return 1;
7984
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007985 case HLUA_E_ETMOUT:
7986 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7987 fcn->name);
7988 return 1;
7989
7990 case HLUA_E_NOMEM:
7991 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7992 fcn->name);
7993 return 1;
7994
7995 case HLUA_E_YIELD: /* unexpected */
7996 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7997 fcn->name);
7998 return 1;
7999
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008000 case HLUA_E_ERR:
8001 /* Display log. */
8002 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8003 fcn->name);
8004 return 1;
8005
8006 default:
8007 return 1;
8008 }
8009
8010 return 1;
8011}
8012
8013static void hlua_cli_io_release_fct(struct appctx *appctx)
8014{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008015 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008016 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008017}
8018
8019/* This function is an LUA binding used for registering
8020 * new keywords in the cli. It expects a list of keywords
8021 * which are the "path". It is limited to 5 keywords. A
8022 * description of the command, a function to be executed
8023 * for the parsing and a function for io handlers.
8024 */
8025__LJMP static int hlua_register_cli(lua_State *L)
8026{
8027 struct cli_kw_list *cli_kws;
8028 const char *message;
8029 int ref_io;
8030 int len;
8031 struct hlua_function *fcn;
8032 int index;
8033 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008034 struct buffer *trash;
8035 const char *kw[5];
8036 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008037
8038 MAY_LJMP(check_args(L, 3, "register_cli"));
8039
8040 /* First argument : an array of maximum 5 keywords. */
8041 if (!lua_istable(L, 1))
8042 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8043
8044 /* Second argument : string with contextual message. */
8045 message = MAY_LJMP(luaL_checkstring(L, 2));
8046
8047 /* Third and fourth argument : lua function. */
8048 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8049
Thierry Fournierf67442e2020-11-28 20:41:07 +01008050 /* Check for CLI service already registered */
8051 trash = get_trash_chunk();
8052 index = 0;
8053 lua_pushnil(L);
8054 memset(kw, 0, sizeof(kw));
8055 while (lua_next(L, 1) != 0) {
8056 if (index >= CLI_PREFIX_KW_NB)
8057 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8058 if (lua_type(L, -1) != LUA_TSTRING)
8059 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8060 kw[index] = lua_tostring(L, -1);
8061 if (index == 0)
8062 chunk_printf(trash, "%s", kw[index]);
8063 else
8064 chunk_appendf(trash, " %s", kw[index]);
8065 index++;
8066 lua_pop(L, 1);
8067 }
8068 cli_kw = cli_find_kw_exact((char **)kw);
8069 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008070 fcn = cli_kw->private;
8071 if (fcn->function_ref[hlua_state_id] != -1) {
8072 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8073 "This will become a hard error in version 2.5.\n", trash->area);
8074 }
8075 fcn->function_ref[hlua_state_id] = ref_io;
8076 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008077 }
8078
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008079 /* Allocate and fill the sample fetch keyword struct. */
8080 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8081 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008082 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008083 fcn = new_hlua_function();
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008084 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008085 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008086
8087 /* Fill path. */
8088 index = 0;
8089 lua_pushnil(L);
8090 while(lua_next(L, 1) != 0) {
8091 if (index >= 5)
8092 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8093 if (lua_type(L, -1) != LUA_TSTRING)
8094 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8095 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8096 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008097 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008098 index++;
8099 lua_pop(L, 1);
8100 }
8101
8102 /* Copy help message. */
8103 cli_kws->kw[0].usage = strdup(message);
8104 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008105 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008106
8107 /* Fill fcn io handler. */
8108 len = strlen("<lua.cli>") + 1;
8109 for (i = 0; i < index; i++)
8110 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8111 fcn->name = calloc(1, len);
8112 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008113 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008114 strncat((char *)fcn->name, "<lua.cli", len);
8115 for (i = 0; i < index; i++) {
8116 strncat((char *)fcn->name, ".", len);
8117 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8118 }
8119 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008120 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008121
8122 /* Fill last entries. */
8123 cli_kws->kw[0].private = fcn;
8124 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8125 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8126 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8127
8128 /* Register this new converter */
8129 cli_register_kw(cli_kws);
8130
8131 return 0;
8132}
8133
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008134static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8135 struct proxy *defpx, const char *file, int line,
8136 char **err, unsigned int *timeout)
8137{
8138 const char *error;
8139
8140 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008141 if (error == PARSE_TIME_OVER) {
8142 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8143 args[1], args[0]);
8144 return -1;
8145 }
8146 else if (error == PARSE_TIME_UNDER) {
8147 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8148 args[1], args[0]);
8149 return -1;
8150 }
8151 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008152 memprintf(err, "%s: invalid timeout", args[0]);
8153 return -1;
8154 }
8155 return 0;
8156}
8157
8158static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8159 struct proxy *defpx, const char *file, int line,
8160 char **err)
8161{
8162 return hlua_read_timeout(args, section_type, curpx, defpx,
8163 file, line, err, &hlua_timeout_session);
8164}
8165
8166static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8167 struct proxy *defpx, const char *file, int line,
8168 char **err)
8169{
8170 return hlua_read_timeout(args, section_type, curpx, defpx,
8171 file, line, err, &hlua_timeout_task);
8172}
8173
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008174static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8175 struct proxy *defpx, const char *file, int line,
8176 char **err)
8177{
8178 return hlua_read_timeout(args, section_type, curpx, defpx,
8179 file, line, err, &hlua_timeout_applet);
8180}
8181
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008182static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8183 struct proxy *defpx, const char *file, int line,
8184 char **err)
8185{
8186 char *error;
8187
8188 hlua_nb_instruction = strtoll(args[1], &error, 10);
8189 if (*error != '\0') {
8190 memprintf(err, "%s: invalid number", args[0]);
8191 return -1;
8192 }
8193 return 0;
8194}
8195
Willy Tarreau32f61e22015-03-18 17:54:59 +01008196static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8197 struct proxy *defpx, const char *file, int line,
8198 char **err)
8199{
8200 char *error;
8201
8202 if (*(args[1]) == 0) {
8203 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8204 return -1;
8205 }
8206 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8207 if (*error != '\0') {
8208 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8209 return -1;
8210 }
8211 return 0;
8212}
8213
8214
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008215/* This function is called by the main configuration key "lua-load". It loads and
8216 * execute an lua file during the parsing of the HAProxy configuration file. It is
8217 * the main lua entry point.
8218 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008219 * This function runs with the HAProxy keywords API. It returns -1 if an error
8220 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008221 *
8222 * In some error case, LUA set an error message in top of the stack. This function
8223 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008224 *
8225 * This function can fail with an abort() due to an Lua critical error.
8226 * We are in the configuration parsing process of HAProxy, this abort() is
8227 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008228 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008229static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008230{
8231 int error;
8232
8233 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008234 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008235 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008236 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8237 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008238 return -1;
8239 }
8240
8241 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008242 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008243 switch (error) {
8244 case LUA_OK:
8245 break;
8246 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008247 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8248 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008249 return -1;
8250 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008251 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008252 return -1;
8253 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008254 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8255 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008256 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008257#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008258 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008259 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8260 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008261 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008262#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008263 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008264 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8265 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008266 return -1;
8267 }
8268
8269 return 0;
8270}
8271
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008272static int hlua_load(char **args, int section_type, struct proxy *curpx,
8273 struct proxy *defpx, const char *file, int line,
8274 char **err)
8275{
8276 if (*(args[1]) == 0) {
8277 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8278 return -1;
8279 }
8280
Thierry Fournier59f11be2020-11-29 00:37:41 +01008281 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008282 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008283 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008284 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008285}
8286
Thierry Fournier59f11be2020-11-29 00:37:41 +01008287static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
8288 struct proxy *defpx, const char *file, int line,
8289 char **err)
8290{
8291 int len;
8292
8293 if (*(args[1]) == 0) {
8294 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8295 return -1;
8296 }
8297
8298 if (per_thread_load == NULL) {
8299 /* allocate the first entry large enough to store the final NULL */
8300 per_thread_load = calloc(1, sizeof(*per_thread_load));
8301 if (per_thread_load == NULL) {
8302 memprintf(err, "out of memory error");
8303 return -1;
8304 }
8305 }
8306
8307 /* count used entries */
8308 for (len = 0; per_thread_load[len] != NULL; len++)
8309 ;
8310
8311 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8312 if (per_thread_load == NULL) {
8313 memprintf(err, "out of memory error");
8314 return -1;
8315 }
8316
8317 per_thread_load[len] = strdup(args[1]);
8318 per_thread_load[len + 1] = NULL;
8319
8320 if (per_thread_load[len] == NULL) {
8321 memprintf(err, "out of memory error");
8322 return -1;
8323 }
8324
8325 /* loading for thread 1 only */
8326 hlua_state_id = 1;
8327 ha_set_tid(0);
8328 return hlua_load_state(args[1], hlua_states[1], err);
8329}
8330
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008331/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8332 * in the given <ctx>.
8333 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008334static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008335{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008336 lua_getglobal(L, "package"); /* push package variable */
8337 lua_pushstring(L, path); /* push given path */
8338 lua_pushstring(L, ";"); /* push semicolon */
8339 lua_getfield(L, -3, type); /* push old path */
8340 lua_concat(L, 3); /* concatenate to new path */
8341 lua_setfield(L, -2, type); /* store new path */
8342 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008343
8344 return 0;
8345}
8346
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008347static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
8348 struct proxy *defpx, const char *file, int line,
8349 char **err)
8350{
8351 char *path;
8352 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008353 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008354
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008355 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008356 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008357 }
8358
8359 if (!(*args[1])) {
8360 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008361 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008362 }
8363 path = args[1];
8364
8365 if (*args[2]) {
8366 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8367 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008368 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008369 }
8370 type = args[2];
8371 }
8372
Thierry Fournier59f11be2020-11-29 00:37:41 +01008373 p = calloc(1, sizeof(*p));
8374 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008375 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008376 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008377 }
8378 p->path = strdup(path);
8379 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008380 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008381 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008382 }
8383 p->type = strdup(type);
8384 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008385 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008386 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008387 }
8388 LIST_ADDQ(&prepend_path_list, &p->l);
8389
8390 hlua_prepend_path(hlua_states[0], type, path);
8391 hlua_prepend_path(hlua_states[1], type, path);
8392 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008393
8394err2:
8395 free(p->type);
8396 free(p->path);
8397err:
8398 free(p);
8399 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008400}
8401
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008402/* configuration keywords declaration */
8403static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008404 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008405 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008406 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008407 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8408 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008409 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008410 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008411 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008412 { 0, NULL, NULL },
8413}};
8414
Willy Tarreau0108d902018-11-25 19:14:37 +01008415INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8416
Christopher Fauletafd8f102018-11-08 11:34:21 +01008417
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008418/* This function can fail with an abort() due to an Lua critical error.
8419 * We are in the initialisation process of HAProxy, this abort() is
8420 * tolerated.
8421 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008422int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008423{
8424 struct hlua_init_function *init;
8425 const char *msg;
8426 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008427 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008428 const char *kind;
8429 const char *trace;
8430 int return_status = 1;
8431#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8432 int nres;
8433#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008434
Willy Tarreaucdb53462020-12-02 12:12:00 +01008435 /* disable memory limit checks if limit is not set */
8436 if (!hlua_global_allocator.limit)
8437 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8438
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008439 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008440 if (setjmp(safe_ljmp_env) != 0) {
8441 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008442 if (lua_type(L, -1) == LUA_TSTRING)
8443 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008444 else
8445 error = "critical error";
8446 fprintf(stderr, "Lua post-init: %s.\n", error);
8447 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008448 } else {
8449 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008450 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008451
Thierry Fournierb8cef172020-11-28 15:37:17 +01008452 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008453
Thierry Fournierc7492592020-11-28 23:57:24 +01008454 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008455 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008456
8457#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008458 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008459#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008460 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008461#endif
8462 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008463 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008464
8465 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008466 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008467 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008468
8469 case LUA_ERRERR:
8470 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008471 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008472 case LUA_ERRRUN:
8473 if (!kind)
8474 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008475 msg = lua_tostring(L, -1);
8476 lua_settop(L, 0); /* Empty the stack. */
8477 lua_pop(L, 1);
8478 trace = hlua_traceback(L);
Thierry Fournier670db242020-11-28 10:49:59 +01008479 if (msg)
8480 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8481 else
8482 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8483 return_status = 0;
8484 break;
8485
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008486 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008487 /* Unknown error */
8488 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008489 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008490 case LUA_YIELD:
8491 /* yield is not configured at this step, this state doesn't happen */
8492 if (!kind)
8493 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008494 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008495 case LUA_ERRMEM:
8496 if (!kind)
8497 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008498 lua_settop(L, 0);
8499 lua_pop(L, 1);
8500 trace = hlua_traceback(L);
Thierry Fournier670db242020-11-28 10:49:59 +01008501 ha_alert("Lua init: %s: %s\n", kind, trace);
8502 return_status = 0;
8503 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008504 }
Thierry Fournier670db242020-11-28 10:49:59 +01008505 if (!return_status)
8506 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008507 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008508
8509 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008510 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008511}
8512
Thierry Fournierb8cef172020-11-28 15:37:17 +01008513int hlua_post_init()
8514{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008515 int ret;
8516 int i;
8517 int errors;
8518 char *err = NULL;
8519 struct hlua_function *fcn;
8520
Thierry Fournierb8cef172020-11-28 15:37:17 +01008521#if USE_OPENSSL
8522 /* Initialize SSL server. */
8523 if (socket_ssl.xprt->prepare_srv) {
8524 int saved_used_backed = global.ssl_used_backend;
8525 // don't affect maxconn automatic computation
8526 socket_ssl.xprt->prepare_srv(&socket_ssl);
8527 global.ssl_used_backend = saved_used_backed;
8528 }
8529#endif
8530
Thierry Fournierc7492592020-11-28 23:57:24 +01008531 /* Perform post init of common thread */
8532 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008533 ha_set_tid(0);
8534 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8535 if (ret == 0)
8536 return 0;
8537
8538 /* init remaining lua states and load files */
8539 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8540
8541 /* set thread context */
8542 ha_set_tid(hlua_state_id - 1);
8543
8544 /* Init lua state */
8545 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8546
8547 /* Load lua files */
8548 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8549 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8550 if (ret != 0) {
8551 ha_alert("Lua init: %s\n", err);
8552 return 0;
8553 }
8554 }
8555 }
8556
8557 /* Reset thread context */
8558 ha_set_tid(0);
8559
8560 /* Execute post init for all states */
8561 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8562
8563 /* set thread context */
8564 ha_set_tid(hlua_state_id - 1);
8565
8566 /* run post init */
8567 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8568 if (ret == 0)
8569 return 0;
8570 }
8571
8572 /* Reset thread context */
8573 ha_set_tid(0);
8574
8575 /* control functions registering. Each function must have:
8576 * - only the function_ref[0] set positive and all other to -1
8577 * - only the function_ref[0] set to -1 and all other positive
8578 * This ensure a same reference is not used both in shared
8579 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008580 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008581 * complicated to found for the end user.
8582 */
8583 errors = 0;
8584 list_for_each_entry(fcn, &referenced_functions, l) {
8585 ret = 0;
8586 for (i = 1; i < global.nbthread + 1; i++) {
8587 if (fcn->function_ref[i] == -1)
8588 ret--;
8589 else
8590 ret++;
8591 }
8592 if (abs(ret) != global.nbthread) {
8593 ha_alert("Lua function '%s' is not referenced in all thread. "
8594 "Expect function in all thread or in none thread.\n", fcn->name);
8595 errors++;
8596 continue;
8597 }
8598
8599 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008600 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8601 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008602 "exclusive.\n", fcn->name);
8603 errors++;
8604 }
8605 }
8606
8607 if (errors > 0)
8608 return 0;
8609
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008610 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008611 * -1 in order to have probably a segfault if someone use it
8612 */
8613 hlua_state_id = -1;
8614
8615 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008616}
8617
Willy Tarreau32f61e22015-03-18 17:54:59 +01008618/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8619 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8620 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008621 * allocation. <nsize> is the requested new size. A new allocation is
8622 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008623 * zero. This one verifies that the limits are respected but is optimized
8624 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008625 */
8626static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8627{
8628 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008629 size_t limit, old, new;
8630
Tim Duesterhus22586522021-01-08 10:35:33 +01008631 if (unlikely(!ptr && !nsize))
8632 return NULL;
8633
Willy Tarreaucdb53462020-12-02 12:12:00 +01008634 /* a limit of ~0 means unlimited and boot complete, so there's no need
8635 * for accounting anymore.
8636 */
8637 if (likely(~zone->limit == 0))
8638 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008639
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008640 if (!ptr)
8641 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008642
Willy Tarreaucdb53462020-12-02 12:12:00 +01008643 /* enforce strict limits across all threads */
8644 limit = zone->limit;
8645 old = _HA_ATOMIC_LOAD(&zone->allocated);
8646 do {
8647 new = old + nsize - osize;
8648 if (unlikely(nsize && limit && new > limit))
8649 return NULL;
8650 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008651
8652 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008653
8654 if (unlikely(!ptr && nsize)) // failed
8655 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8656
8657 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008658 return ptr;
8659}
8660
Thierry Fournierecb83c22020-11-28 15:49:44 +01008661/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008662 * We are in the initialisation process of HAProxy, this abort() is
8663 * tolerated.
8664 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008665lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008666{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008667 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008668 int idx;
8669 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008670 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008671 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008672 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008673 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008674 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008675 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008676
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008677 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008678 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008679
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008680 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008681 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008682 *context = NULL;
8683
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008684 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008685 * the Lua function can fail with an abort. We are in the initialisation
8686 * process of HAProxy, this abort() is tolerated.
8687 */
8688
Thierry Fournier3c539322020-11-28 16:05:05 +01008689 /* Call post initialisation function in safe environment. */
8690 if (setjmp(safe_ljmp_env) != 0) {
8691 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008692 if (lua_type(L, -1) == LUA_TSTRING)
8693 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008694 else
8695 error_msg = "critical error";
8696 fprintf(stderr, "Lua init: %s.\n", error_msg);
8697 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008698 } else {
8699 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008700 }
8701
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008702 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008703 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008704#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8705#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8706#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008707 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008708#endif
8709#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008710 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008711#endif
8712#undef HLUA_PREPEND_PATH_TOSTRING
8713#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008714
Thierry Fournier59f11be2020-11-29 00:37:41 +01008715 /* Apply configured prepend path */
8716 list_for_each_entry(pp, &prepend_path_list, l)
8717 hlua_prepend_path(L, pp->type, pp->path);
8718
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008719 /*
8720 *
8721 * Create "core" object.
8722 *
8723 */
8724
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008725 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008726 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008727
Thierry Fournierecb83c22020-11-28 15:49:44 +01008728 /* set the thread id */
8729 hlua_class_const_int(L, "thread", thread_num);
8730
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008731 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008732 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008733 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008734
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008735 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008736 hlua_class_function(L, "register_init", hlua_register_init);
8737 hlua_class_function(L, "register_task", hlua_register_task);
8738 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8739 hlua_class_function(L, "register_converters", hlua_register_converters);
8740 hlua_class_function(L, "register_action", hlua_register_action);
8741 hlua_class_function(L, "register_service", hlua_register_service);
8742 hlua_class_function(L, "register_cli", hlua_register_cli);
8743 hlua_class_function(L, "yield", hlua_yield);
8744 hlua_class_function(L, "set_nice", hlua_set_nice);
8745 hlua_class_function(L, "sleep", hlua_sleep);
8746 hlua_class_function(L, "msleep", hlua_msleep);
8747 hlua_class_function(L, "add_acl", hlua_add_acl);
8748 hlua_class_function(L, "del_acl", hlua_del_acl);
8749 hlua_class_function(L, "set_map", hlua_set_map);
8750 hlua_class_function(L, "del_map", hlua_del_map);
8751 hlua_class_function(L, "tcp", hlua_socket_new);
8752 hlua_class_function(L, "log", hlua_log);
8753 hlua_class_function(L, "Debug", hlua_log_debug);
8754 hlua_class_function(L, "Info", hlua_log_info);
8755 hlua_class_function(L, "Warning", hlua_log_warning);
8756 hlua_class_function(L, "Alert", hlua_log_alert);
8757 hlua_class_function(L, "done", hlua_done);
8758 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008759
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008760 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008761
8762 /*
8763 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008764 * Create "act" object.
8765 *
8766 */
8767
8768 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008769 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008770
8771 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008772 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8773 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8774 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8775 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8776 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8777 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8778 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8779 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008780
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008781 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008782
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008783 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008784
8785 /*
8786 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008787 * Register class Map
8788 *
8789 */
8790
8791 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008792 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008793
8794 /* register pattern types. */
8795 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008796 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008797 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008798 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008799 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008800 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008801
8802 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008803 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008804
8805 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008806 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008807
Ilya Shipitsind4259502020-04-08 01:07:56 +05008808 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008809 lua_pushstring(L, "__index");
8810 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008811
8812 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008813 hlua_class_function(L, "lookup", hlua_map_lookup);
8814 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008815
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008816 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008817
Thierry Fournier45e78d72016-02-19 18:34:46 +01008818 /* Register previous table in the registry with reference and named entry.
8819 * The function hlua_register_metatable() pops the stack, so we
8820 * previously create a copy of the table.
8821 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008822 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8823 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008824
8825 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008826 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008827
8828 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008829 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008830
8831 /*
8832 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008833 * Register class Channel
8834 *
8835 */
8836
8837 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008838 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008839
Ilya Shipitsind4259502020-04-08 01:07:56 +05008840 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008841 lua_pushstring(L, "__index");
8842 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008843
8844 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008845 hlua_class_function(L, "get", hlua_channel_get);
8846 hlua_class_function(L, "dup", hlua_channel_dup);
8847 hlua_class_function(L, "getline", hlua_channel_getline);
8848 hlua_class_function(L, "set", hlua_channel_set);
8849 hlua_class_function(L, "append", hlua_channel_append);
8850 hlua_class_function(L, "send", hlua_channel_send);
8851 hlua_class_function(L, "forward", hlua_channel_forward);
8852 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
8853 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
8854 hlua_class_function(L, "is_full", hlua_channel_is_full);
8855 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008856
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008857 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008858
8859 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008860 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008861
8862 /*
8863 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008864 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008865 *
8866 */
8867
8868 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008869 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008870
Ilya Shipitsind4259502020-04-08 01:07:56 +05008871 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008872 lua_pushstring(L, "__index");
8873 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008874
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008875 /* Browse existing fetches and create the associated
8876 * object method.
8877 */
8878 sf = NULL;
8879 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008880 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8881 * by an underscore.
8882 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008883 strncpy(trash.area, sf->kw, trash.size);
8884 trash.area[trash.size - 1] = '\0';
8885 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008886 if (*p == '.' || *p == '-' || *p == '+')
8887 *p = '_';
8888
8889 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008890 lua_pushstring(L, trash.area);
8891 lua_pushlightuserdata(L, sf);
8892 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
8893 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008894 }
8895
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008896 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008897
8898 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008899 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008900
8901 /*
8902 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008903 * Register class Converters
8904 *
8905 */
8906
8907 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008908 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008909
8910 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008911 lua_pushstring(L, "__index");
8912 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008913
8914 /* Browse existing converters and create the associated
8915 * object method.
8916 */
8917 sc = NULL;
8918 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008919 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8920 * by an underscore.
8921 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008922 strncpy(trash.area, sc->kw, trash.size);
8923 trash.area[trash.size - 1] = '\0';
8924 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008925 if (*p == '.' || *p == '-' || *p == '+')
8926 *p = '_';
8927
8928 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008929 lua_pushstring(L, trash.area);
8930 lua_pushlightuserdata(L, sc);
8931 lua_pushcclosure(L, hlua_run_sample_conv, 1);
8932 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008933 }
8934
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008935 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008936
8937 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008938 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008939
8940 /*
8941 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008942 * Register class HTTP
8943 *
8944 */
8945
8946 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008947 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008948
Ilya Shipitsind4259502020-04-08 01:07:56 +05008949 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008950 lua_pushstring(L, "__index");
8951 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008952
8953 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008954 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
8955 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
8956 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
8957 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
8958 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
8959 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
8960 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
8961 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
8962 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
8963 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008964
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008965 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
8966 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
8967 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
8968 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
8969 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
8970 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
8971 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008972
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008973 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008974
8975 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008976 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008977
8978 /*
8979 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008980 * Register class AppletTCP
8981 *
8982 */
8983
8984 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008985 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008986
Ilya Shipitsind4259502020-04-08 01:07:56 +05008987 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008988 lua_pushstring(L, "__index");
8989 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008990
8991 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008992 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
8993 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
8994 hlua_class_function(L, "send", hlua_applet_tcp_send);
8995 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
8996 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
8997 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
8998 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
8999 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009000
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009001 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009002
9003 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009004 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009005
9006 /*
9007 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009008 * Register class AppletHTTP
9009 *
9010 */
9011
9012 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009013 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009014
Ilya Shipitsind4259502020-04-08 01:07:56 +05009015 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009016 lua_pushstring(L, "__index");
9017 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009018
9019 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009020 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9021 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9022 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9023 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9024 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9025 hlua_class_function(L, "getline", hlua_applet_http_getline);
9026 hlua_class_function(L, "receive", hlua_applet_http_recv);
9027 hlua_class_function(L, "send", hlua_applet_http_send);
9028 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9029 hlua_class_function(L, "set_status", hlua_applet_http_status);
9030 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009031
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009032 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009033
9034 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009035 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009036
9037 /*
9038 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009039 * Register class TXN
9040 *
9041 */
9042
9043 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009044 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009045
Ilya Shipitsind4259502020-04-08 01:07:56 +05009046 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009047 lua_pushstring(L, "__index");
9048 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009049
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009050 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009051 hlua_class_function(L, "set_priv", hlua_set_priv);
9052 hlua_class_function(L, "get_priv", hlua_get_priv);
9053 hlua_class_function(L, "set_var", hlua_set_var);
9054 hlua_class_function(L, "unset_var", hlua_unset_var);
9055 hlua_class_function(L, "get_var", hlua_get_var);
9056 hlua_class_function(L, "done", hlua_txn_done);
9057 hlua_class_function(L, "reply", hlua_txn_reply_new);
9058 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9059 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9060 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9061 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9062 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9063 hlua_class_function(L, "deflog", hlua_txn_deflog);
9064 hlua_class_function(L, "log", hlua_txn_log);
9065 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9066 hlua_class_function(L, "Info", hlua_txn_log_info);
9067 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9068 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009069
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009070 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009071
9072 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009073 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009074
9075 /*
9076 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009077 * Register class reply
9078 *
9079 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009080 lua_newtable(L);
9081 lua_pushstring(L, "__index");
9082 lua_newtable(L);
9083 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9084 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9085 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9086 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9087 lua_settable(L, -3); /* Sets the __index entry. */
9088 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009089
9090
9091 /*
9092 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009093 * Register class Socket
9094 *
9095 */
9096
9097 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009098 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009099
Ilya Shipitsind4259502020-04-08 01:07:56 +05009100 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009101 lua_pushstring(L, "__index");
9102 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009103
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009104#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009105 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009106#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009107 hlua_class_function(L, "connect", hlua_socket_connect);
9108 hlua_class_function(L, "send", hlua_socket_send);
9109 hlua_class_function(L, "receive", hlua_socket_receive);
9110 hlua_class_function(L, "close", hlua_socket_close);
9111 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9112 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9113 hlua_class_function(L, "setoption", hlua_socket_setoption);
9114 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009115
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009116 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009117
9118 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009119 lua_pushstring(L, "__gc");
9120 lua_pushcclosure(L, hlua_socket_gc, 0);
9121 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009122
9123 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009124 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009125
Thierry Fournieraafc7772020-12-04 11:47:47 +01009126 lua_atpanic(L, hlua_panic_safe);
9127
9128 return L;
9129}
9130
9131void hlua_init(void) {
9132 int i;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009133#ifdef USE_OPENSSL
9134 struct srv_kw *kw;
9135 int tmp_error;
9136 char *error;
9137 char *args[] = { /* SSL client configuration. */
9138 "ssl",
9139 "verify",
9140 "none",
9141 NULL
9142 };
9143#endif
9144
9145 /* Init post init function list head */
9146 for (i = 0; i < MAX_THREADS + 1; i++)
9147 LIST_INIT(&hlua_init_functions[i]);
9148
9149 /* Init state for common/shared lua parts */
9150 hlua_state_id = 0;
9151 ha_set_tid(0);
9152 hlua_states[0] = hlua_init_state(0);
9153
9154 /* Init state 1 for thread 0. We have at least one thread. */
9155 hlua_state_id = 1;
9156 ha_set_tid(0);
9157 hlua_states[1] = hlua_init_state(1);
9158
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009159 /* Proxy and server configuration initialisation. */
9160 memset(&socket_proxy, 0, sizeof(socket_proxy));
9161 init_new_proxy(&socket_proxy);
9162 socket_proxy.parent = NULL;
9163 socket_proxy.last_change = now.tv_sec;
9164 socket_proxy.id = "LUA-SOCKET";
9165 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
9166 socket_proxy.maxconn = 0;
9167 socket_proxy.accept = NULL;
9168 socket_proxy.options2 |= PR_O2_INDEPSTR;
9169 socket_proxy.srv = NULL;
9170 socket_proxy.conn_retries = 0;
9171 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
9172
9173 /* Init TCP server: unchanged parameters */
9174 memset(&socket_tcp, 0, sizeof(socket_tcp));
9175 socket_tcp.next = NULL;
9176 socket_tcp.proxy = &socket_proxy;
9177 socket_tcp.obj_type = OBJ_TYPE_SERVER;
9178 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04009179 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02009180 socket_tcp.idle_conns = NULL;
9181 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02009182 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009183 socket_tcp.last_change = 0;
9184 socket_tcp.id = "LUA-TCP-CONN";
9185 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9186 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9187 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
9188
9189 /* XXX: Copy default parameter from default server,
9190 * but the default server is not initialized.
9191 */
9192 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
9193 socket_tcp.minconn = socket_proxy.defsrv.minconn;
9194 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
9195 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
9196 socket_tcp.onerror = socket_proxy.defsrv.onerror;
9197 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
9198 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
9199 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
9200 socket_tcp.uweight = socket_proxy.defsrv.iweight;
9201 socket_tcp.iweight = socket_proxy.defsrv.iweight;
9202
9203 socket_tcp.check.status = HCHK_STATUS_INI;
9204 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
9205 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
9206 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
9207 socket_tcp.check.server = &socket_tcp;
9208
9209 socket_tcp.agent.status = HCHK_STATUS_INI;
9210 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
9211 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
9212 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
9213 socket_tcp.agent.server = &socket_tcp;
9214
Willy Tarreaua261e9b2016-12-22 20:44:00 +01009215 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009216
9217#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009218 /* Init TCP server: unchanged parameters */
9219 memset(&socket_ssl, 0, sizeof(socket_ssl));
9220 socket_ssl.next = NULL;
9221 socket_ssl.proxy = &socket_proxy;
9222 socket_ssl.obj_type = OBJ_TYPE_SERVER;
9223 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04009224 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01009225 socket_ssl.idle_conns = NULL;
9226 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02009227 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009228 socket_ssl.last_change = 0;
9229 socket_ssl.id = "LUA-SSL-CONN";
9230 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9231 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9232 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
9233
9234 /* XXX: Copy default parameter from default server,
9235 * but the default server is not initialized.
9236 */
9237 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
9238 socket_ssl.minconn = socket_proxy.defsrv.minconn;
9239 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
9240 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
9241 socket_ssl.onerror = socket_proxy.defsrv.onerror;
9242 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
9243 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
9244 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
9245 socket_ssl.uweight = socket_proxy.defsrv.iweight;
9246 socket_ssl.iweight = socket_proxy.defsrv.iweight;
9247
9248 socket_ssl.check.status = HCHK_STATUS_INI;
9249 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
9250 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
9251 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
9252 socket_ssl.check.server = &socket_ssl;
9253
9254 socket_ssl.agent.status = HCHK_STATUS_INI;
9255 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
9256 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
9257 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
9258 socket_ssl.agent.server = &socket_ssl;
9259
Thierry FOURNIER36d13742015-03-17 16:48:53 +01009260 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01009261 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009262
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009263 for (i = 0; args[i] != NULL; i++) {
9264 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009265 /*
9266 *
9267 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009268 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009269 * features like client certificates and ssl_verify.
9270 *
9271 */
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009272 tmp_error = kw->parse(args, &i, &socket_proxy, &socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009273 if (tmp_error != 0) {
9274 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9275 abort(); /* This must be never arrives because the command line
9276 not editable by the user. */
9277 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009278 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009279 }
9280 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009281#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009282
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009283}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009284
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009285static void hlua_deinit()
9286{
Willy Tarreau186f3762020-12-04 11:48:12 +01009287 int thr;
9288
9289 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9290 if (hlua_states[thr])
9291 lua_close(hlua_states[thr]);
9292 }
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009293}
9294
9295REGISTER_POST_DEINIT(hlua_deinit);
9296
Willy Tarreau80713382018-11-26 10:19:54 +01009297static void hlua_register_build_options(void)
9298{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009299 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009300
Willy Tarreaubb57d942016-12-21 19:04:56 +01009301 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9302 hap_register_build_opts(ptr, 1);
9303}
Willy Tarreau80713382018-11-26 10:19:54 +01009304
9305INITCALL0(STG_REGISTER, hlua_register_build_options);