blob: 0de1937db1459ce5cca800558ee0c8a16dc8a971 [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 Fauleta2097962019-07-15 16:25:33 +02004210 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
4211 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
Christopher Faulete461e342018-12-18 16:43:35 +01004384 if (type == HTX_BLK_EOM) {
4385 stop = 1;
4386 break;
4387 }
4388
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004389 vlen = sz;
4390 if (vlen > count) {
4391 if (type != HTX_BLK_DATA)
4392 break;
4393 vlen = count;
4394 }
4395
4396 switch (type) {
4397 case HTX_BLK_UNUSED:
4398 break;
4399
4400 case HTX_BLK_DATA:
4401 v = htx_get_blk_value(htx, blk);
4402 v.len = vlen;
4403 nl = istchr(v, '\n');
4404 if (nl != NULL) {
4405 stop = 1;
4406 vlen = nl - v.ptr + 1;
4407 }
4408 luaL_addlstring(&appctx->b, v.ptr, vlen);
4409 break;
4410
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004411 case HTX_BLK_TLR:
4412 case HTX_BLK_EOM:
4413 stop = 1;
4414 break;
4415
4416 default:
4417 break;
4418 }
4419
4420 co_set_data(req, co_data(req) - vlen);
4421 count -= vlen;
4422 if (sz == vlen)
4423 blk = htx_remove_blk(htx, blk);
4424 else {
4425 htx_cut_data_blk(htx, blk, vlen);
4426 break;
4427 }
4428 }
4429
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004430 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004431 if (!stop) {
4432 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004433 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004434 }
4435
4436 /* return the result. */
4437 luaL_pushresult(&appctx->b);
4438 return 1;
4439}
4440
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004441
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004442/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004443__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004444{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004445 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004446
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004447 /* Initialise the string catenation. */
4448 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004449
Christopher Fauleta2097962019-07-15 16:25:33 +02004450 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004451}
4452
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004453/* If expected data not yet available, it returns a yield. This function
4454 * consumes the data in the buffer. It returns a string containing the
4455 * data. This string can be empty.
4456 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004457__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004458{
4459 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4460 struct stream_interface *si = appctx->appctx->owner;
4461 struct channel *req = si_oc(si);
4462 struct htx *htx;
4463 struct htx_blk *blk;
4464 size_t count;
4465 int len;
4466
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004467 htx = htx_from_buf(&req->buf);
4468 len = MAY_LJMP(luaL_checkinteger(L, 2));
4469 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004470 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004471 while (count && len && blk) {
4472 enum htx_blk_type type = htx_get_blk_type(blk);
4473 uint32_t sz = htx_get_blksz(blk);
4474 struct ist v;
4475 uint32_t vlen;
4476
Christopher Faulete461e342018-12-18 16:43:35 +01004477 if (type == HTX_BLK_EOM) {
4478 len = 0;
4479 break;
4480 }
4481
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004482 vlen = sz;
4483 if (len > 0 && vlen > len)
4484 vlen = len;
4485 if (vlen > count) {
4486 if (type != HTX_BLK_DATA)
4487 break;
4488 vlen = count;
4489 }
4490
4491 switch (type) {
4492 case HTX_BLK_UNUSED:
4493 break;
4494
4495 case HTX_BLK_DATA:
4496 v = htx_get_blk_value(htx, blk);
4497 luaL_addlstring(&appctx->b, v.ptr, vlen);
4498 break;
4499
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004500 case HTX_BLK_TLR:
4501 case HTX_BLK_EOM:
4502 len = 0;
4503 break;
4504
4505 default:
4506 break;
4507 }
4508
4509 co_set_data(req, co_data(req) - vlen);
4510 count -= vlen;
4511 if (len > 0)
4512 len -= vlen;
4513 if (sz == vlen)
4514 blk = htx_remove_blk(htx, blk);
4515 else {
4516 htx_cut_data_blk(htx, blk, vlen);
4517 break;
4518 }
4519 }
4520
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004521 htx_to_buf(htx, &req->buf);
4522
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004523 /* If we are no other data available, yield waiting for new data. */
4524 if (len) {
4525 if (len > 0) {
4526 lua_pushinteger(L, len);
4527 lua_replace(L, 2);
4528 }
4529 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004530 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004531 }
4532
4533 /* return the result. */
4534 luaL_pushresult(&appctx->b);
4535 return 1;
4536}
4537
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004538/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004539__LJMP static int hlua_applet_http_recv(lua_State *L)
4540{
4541 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4542 int len = -1;
4543
4544 /* Check arguments. */
4545 if (lua_gettop(L) > 2)
4546 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4547 if (lua_gettop(L) >= 2) {
4548 len = MAY_LJMP(luaL_checkinteger(L, 2));
4549 lua_pop(L, 1);
4550 }
4551
Christopher Fauleta2097962019-07-15 16:25:33 +02004552 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004553
Christopher Fauleta2097962019-07-15 16:25:33 +02004554 /* Initialise the string catenation. */
4555 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004556
Christopher Fauleta2097962019-07-15 16:25:33 +02004557 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004558}
4559
4560/* Append data in the output side of the buffer. This data is immediately
4561 * sent. The function returns the amount of data written. If the buffer
4562 * cannot contain the data, the function yields. The function returns -1
4563 * if the channel is closed.
4564 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004565__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004566{
4567 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4568 struct stream_interface *si = appctx->appctx->owner;
4569 struct channel *res = si_ic(si);
4570 struct htx *htx = htx_from_buf(&res->buf);
4571 const char *data;
4572 size_t len;
4573 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4574 int max;
4575
Christopher Faulet9060fc02019-07-03 11:39:30 +02004576 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004577 if (!max)
4578 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004579
4580 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4581
4582 /* Get the max amount of data which can write as input in the channel. */
4583 if (max > (len - l))
4584 max = len - l;
4585
4586 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004587 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004588 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004589
4590 /* update counters. */
4591 l += max;
4592 lua_pop(L, 1);
4593 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004594
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004595 /* If some data is not send, declares the situation to the
4596 * applet, and returns a yield.
4597 */
4598 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004599 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004600 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004601 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004602 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004603 }
4604
Christopher Fauleta2097962019-07-15 16:25:33 +02004605 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004606 return 1;
4607}
4608
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004609/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004610 * yield the LUA process, and resume it without checking the
4611 * input arguments.
4612 */
4613__LJMP static int hlua_applet_http_send(lua_State *L)
4614{
4615 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004616
4617 /* We want to send some data. Headers must be sent. */
4618 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4619 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4620 WILL_LJMP(lua_error(L));
4621 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004622
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004623 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004624 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004625
Christopher Fauleta2097962019-07-15 16:25:33 +02004626 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004627}
4628
4629__LJMP static int hlua_applet_http_addheader(lua_State *L)
4630{
4631 const char *name;
4632 int ret;
4633
4634 MAY_LJMP(hlua_checkapplet_http(L, 1));
4635 name = MAY_LJMP(luaL_checkstring(L, 2));
4636 MAY_LJMP(luaL_checkstring(L, 3));
4637
4638 /* Push in the stack the "response" entry. */
4639 ret = lua_getfield(L, 1, "response");
4640 if (ret != LUA_TTABLE) {
4641 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4642 "is expected as an array. %s found", lua_typename(L, ret));
4643 WILL_LJMP(lua_error(L));
4644 }
4645
4646 /* check if the header is already registered if it is not
4647 * the case, register it.
4648 */
4649 ret = lua_getfield(L, -1, name);
4650 if (ret == LUA_TNIL) {
4651
4652 /* Entry not found. */
4653 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4654
4655 /* Insert the new header name in the array in the top of the stack.
4656 * It left the new array in the top of the stack.
4657 */
4658 lua_newtable(L);
4659 lua_pushvalue(L, 2);
4660 lua_pushvalue(L, -2);
4661 lua_settable(L, -4);
4662
4663 } else if (ret != LUA_TTABLE) {
4664
4665 /* corruption error. */
4666 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4667 "is expected as an array. %s found", name, lua_typename(L, ret));
4668 WILL_LJMP(lua_error(L));
4669 }
4670
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004671 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004672 * the header value as new entry.
4673 */
4674 lua_pushvalue(L, 3);
4675 ret = lua_rawlen(L, -2);
4676 lua_rawseti(L, -2, ret + 1);
4677 lua_pushboolean(L, 1);
4678 return 1;
4679}
4680
4681__LJMP static int hlua_applet_http_status(lua_State *L)
4682{
4683 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4684 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004685 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004686
4687 if (status < 100 || status > 599) {
4688 lua_pushboolean(L, 0);
4689 return 1;
4690 }
4691
4692 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004693 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004694 lua_pushboolean(L, 1);
4695 return 1;
4696}
4697
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004698
Christopher Fauleta2097962019-07-15 16:25:33 +02004699__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004700{
4701 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4702 struct stream_interface *si = appctx->appctx->owner;
4703 struct channel *res = si_ic(si);
4704 struct htx *htx;
4705 struct htx_sl *sl;
4706 struct h1m h1m;
4707 const char *status, *reason;
4708 const char *name, *value;
4709 size_t nlen, vlen;
4710 unsigned int flags;
4711
4712 /* Send the message at once. */
4713 htx = htx_from_buf(&res->buf);
4714 h1m_init_res(&h1m);
4715
4716 /* Use the same http version than the request. */
4717 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4718 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4719 if (reason == NULL)
4720 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4721 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4722 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4723 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4724 }
4725 else {
4726 flags = HTX_SL_F_IS_RESP;
4727 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4728 }
4729 if (!sl) {
4730 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004731 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004732 WILL_LJMP(lua_error(L));
4733 }
4734 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4735
4736 /* Get the array associated to the field "response" in the object AppletHTTP. */
4737 lua_pushvalue(L, 0);
4738 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4739 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\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 WILL_LJMP(lua_error(L));
4742 }
4743
4744 /* Browse the list of headers. */
4745 lua_pushnil(L);
4746 while(lua_next(L, -2) != 0) {
4747 /* We expect a string as -2. */
4748 if (lua_type(L, -2) != LUA_TSTRING) {
4749 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004750 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004751 lua_typename(L, lua_type(L, -2)));
4752 WILL_LJMP(lua_error(L));
4753 }
4754 name = lua_tolstring(L, -2, &nlen);
4755
4756 /* We expect an array as -1. */
4757 if (lua_type(L, -1) != LUA_TTABLE) {
4758 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 +01004759 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004760 name,
4761 lua_typename(L, lua_type(L, -1)));
4762 WILL_LJMP(lua_error(L));
4763 }
4764
4765 /* Browse the table who is on the top of the stack. */
4766 lua_pushnil(L);
4767 while(lua_next(L, -2) != 0) {
4768 int id;
4769
4770 /* We expect a number as -2. */
4771 if (lua_type(L, -2) != LUA_TNUMBER) {
4772 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 +01004773 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004774 name,
4775 lua_typename(L, lua_type(L, -2)));
4776 WILL_LJMP(lua_error(L));
4777 }
4778 id = lua_tointeger(L, -2);
4779
4780 /* We expect a string as -2. */
4781 if (lua_type(L, -1) != LUA_TSTRING) {
4782 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 +01004783 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004784 name, id,
4785 lua_typename(L, lua_type(L, -1)));
4786 WILL_LJMP(lua_error(L));
4787 }
4788 value = lua_tolstring(L, -1, &vlen);
4789
4790 /* Simple Protocol checks. */
4791 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004792 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004793 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4794 struct ist v = ist2(value, vlen);
4795 int ret;
4796
4797 ret = h1_parse_cont_len_header(&h1m, &v);
4798 if (ret < 0) {
4799 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004800 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004801 name);
4802 WILL_LJMP(lua_error(L));
4803 }
4804 else if (ret == 0)
4805 goto next; /* Skip it */
4806 }
4807
4808 /* Add a new header */
4809 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4810 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004811 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004812 name);
4813 WILL_LJMP(lua_error(L));
4814 }
4815 next:
4816 /* Remove the array from the stack, and get next element with a remaining string. */
4817 lua_pop(L, 1);
4818 }
4819
4820 /* Remove the array from the stack, and get next element with a remaining string. */
4821 lua_pop(L, 1);
4822 }
4823
4824 if (h1m.flags & H1_MF_CHNK)
4825 h1m.flags &= ~H1_MF_CLEN;
4826 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4827 h1m.flags |= H1_MF_XFER_LEN;
4828
4829 /* Uset HTX start-line flags */
4830 if (h1m.flags & H1_MF_XFER_ENC)
4831 flags |= HTX_SL_F_XFER_ENC;
4832 if (h1m.flags & H1_MF_XFER_LEN) {
4833 flags |= HTX_SL_F_XFER_LEN;
4834 if (h1m.flags & H1_MF_CHNK)
4835 flags |= HTX_SL_F_CHNK;
4836 else if (h1m.flags & H1_MF_CLEN)
4837 flags |= HTX_SL_F_CLEN;
4838 if (h1m.body_len == 0)
4839 flags |= HTX_SL_F_BODYLESS;
4840 }
4841 sl->flags |= flags;
4842
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004843 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004844 * and the status code implies the presence of a message body, we must
4845 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004846 * for the keepalive compliance. If the applet announces a transfer-encoding
4847 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004848 */
4849 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4850 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4851 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4852 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4853 /* Add a new header */
4854 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4855 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4856 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004857 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004858 WILL_LJMP(lua_error(L));
4859 }
4860 }
4861
4862 /* Finalize headers. */
4863 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4864 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004865 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004866 WILL_LJMP(lua_error(L));
4867 }
4868
4869 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4870 b_reset(&res->buf);
4871 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4872 WILL_LJMP(lua_error(L));
4873 }
4874
4875 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004876 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004877
4878 /* Headers sent, set the flag. */
4879 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4880 return 0;
4881
4882}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004883/* We will build the status line and the headers of the HTTP response.
4884 * We will try send at once if its not possible, we give back the hand
4885 * waiting for more room.
4886 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004887__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004888{
4889 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4890 struct stream_interface *si = appctx->appctx->owner;
4891 struct channel *res = si_ic(si);
4892
4893 if (co_data(res)) {
4894 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004895 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004896 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004897 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004898}
4899
4900
Christopher Fauleta2097962019-07-15 16:25:33 +02004901__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004902{
Christopher Fauleta2097962019-07-15 16:25:33 +02004903 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004904}
4905
Christopher Fauleta2097962019-07-15 16:25:33 +02004906/*
4907 *
4908 *
4909 * Class HTTP
4910 *
4911 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004912 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004913
4914/* Returns a struct hlua_txn if the stack entry "ud" is
4915 * a class stream, otherwise it throws an error.
4916 */
4917__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004918{
Christopher Fauleta2097962019-07-15 16:25:33 +02004919 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4920}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004921
Christopher Fauleta2097962019-07-15 16:25:33 +02004922/* This function creates and push in the stack a HTTP object
4923 * according with a current TXN.
4924 */
4925static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4926{
4927 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004928
Christopher Fauleta2097962019-07-15 16:25:33 +02004929 /* Check stack size. */
4930 if (!lua_checkstack(L, 3))
4931 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004932
Christopher Fauleta2097962019-07-15 16:25:33 +02004933 /* Create the object: obj[0] = userdata.
4934 * Note that the base of the Converters object is the
4935 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004936 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004937 lua_newtable(L);
4938 htxn = lua_newuserdata(L, sizeof(*htxn));
4939 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004940
4941 htxn->s = txn->s;
4942 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004943 htxn->dir = txn->dir;
4944 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004945
4946 /* Pop a class stream metatable and affect it to the table. */
4947 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4948 lua_setmetatable(L, -2);
4949
4950 return 1;
4951}
4952
4953/* This function creates ans returns an array of HTTP headers.
4954 * This function does not fails. It is used as wrapper with the
4955 * 2 following functions.
4956 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004957__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004958{
Christopher Fauleta2097962019-07-15 16:25:33 +02004959 struct htx *htx;
4960 int32_t pos;
4961
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004962 /* Create the table. */
4963 lua_newtable(L);
4964
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004965
Christopher Fauleta2097962019-07-15 16:25:33 +02004966 htx = htxbuf(&msg->chn->buf);
4967 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4968 struct htx_blk *blk = htx_get_blk(htx, pos);
4969 enum htx_blk_type type = htx_get_blk_type(blk);
4970 struct ist n, v;
4971 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004972
Christopher Fauleta2097962019-07-15 16:25:33 +02004973 if (type == HTX_BLK_HDR) {
4974 n = htx_get_blk_name(htx,blk);
4975 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004976 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004977 else if (type == HTX_BLK_EOH)
4978 break;
4979 else
4980 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004981
Christopher Fauleta2097962019-07-15 16:25:33 +02004982 /* Check for existing entry:
4983 * assume that the table is on the top of the stack, and
4984 * push the key in the stack, the function lua_gettable()
4985 * perform the lookup.
4986 */
4987 lua_pushlstring(L, n.ptr, n.len);
4988 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004989
Christopher Fauleta2097962019-07-15 16:25:33 +02004990 switch (lua_type(L, -1)) {
4991 case LUA_TNIL:
4992 /* Table not found, create it. */
4993 lua_pop(L, 1); /* remove the nil value. */
4994 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4995 lua_newtable(L); /* create and push empty table. */
4996 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4997 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4998 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004999 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005000
Christopher Fauleta2097962019-07-15 16:25:33 +02005001 case LUA_TTABLE:
5002 /* Entry found: push the value in the table. */
5003 len = lua_rawlen(L, -1);
5004 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5005 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5006 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5007 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005008
Christopher Fauleta2097962019-07-15 16:25:33 +02005009 default:
5010 /* Other cases are errors. */
5011 hlua_pusherror(L, "internal error during the parsing of headers.");
5012 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005013 }
5014 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005015 return 1;
5016}
5017
5018__LJMP static int hlua_http_req_get_headers(lua_State *L)
5019{
5020 struct hlua_txn *htxn;
5021
5022 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5023 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5024
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005025 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005026 WILL_LJMP(lua_error(L));
5027
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005028 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005029}
5030
5031__LJMP static int hlua_http_res_get_headers(lua_State *L)
5032{
5033 struct hlua_txn *htxn;
5034
5035 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5036 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5037
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005038 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005039 WILL_LJMP(lua_error(L));
5040
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005041 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005042}
5043
5044/* This function replace full header, or just a value in
5045 * the request or in the response. It is a wrapper fir the
5046 * 4 following functions.
5047 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005048__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005049{
5050 size_t name_len;
5051 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5052 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5053 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005054 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005055 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005056
Dragan Dosen26743032019-04-30 15:54:36 +02005057 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005058 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5059
Christopher Fauleta2097962019-07-15 16:25:33 +02005060 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005061 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005062 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005063 return 0;
5064}
5065
5066__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5067{
5068 struct hlua_txn *htxn;
5069
5070 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5071 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5072
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005073 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005074 WILL_LJMP(lua_error(L));
5075
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005076 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005077}
5078
5079__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5080{
5081 struct hlua_txn *htxn;
5082
5083 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5084 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5085
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005086 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005087 WILL_LJMP(lua_error(L));
5088
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005089 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005090}
5091
5092__LJMP static int hlua_http_req_rep_val(lua_State *L)
5093{
5094 struct hlua_txn *htxn;
5095
5096 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5097 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5098
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005099 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005100 WILL_LJMP(lua_error(L));
5101
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005102 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005103}
5104
5105__LJMP static int hlua_http_res_rep_val(lua_State *L)
5106{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005107 struct hlua_txn *htxn;
5108
5109 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5110 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5111
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005112 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005113 WILL_LJMP(lua_error(L));
5114
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005115 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005116}
5117
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005118/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005119 * It is a wrapper for the 2 following functions.
5120 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005121__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005122{
5123 size_t len;
5124 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005125 struct htx *htx = htxbuf(&msg->chn->buf);
5126 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005127
Christopher Fauleta2097962019-07-15 16:25:33 +02005128 ctx.blk = NULL;
5129 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5130 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005131 return 0;
5132}
5133
5134__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5135{
5136 struct hlua_txn *htxn;
5137
5138 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5139 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5140
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005141 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005142 WILL_LJMP(lua_error(L));
5143
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005144 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005145}
5146
5147__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5148{
5149 struct hlua_txn *htxn;
5150
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005151 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005152 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5153
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005154 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005155 WILL_LJMP(lua_error(L));
5156
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005157 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005158}
5159
5160/* This function adds an header. It is a wrapper used by
5161 * the 2 following functions.
5162 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005163__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005164{
5165 size_t name_len;
5166 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5167 size_t value_len;
5168 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005169 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005170
Christopher Fauleta2097962019-07-15 16:25:33 +02005171 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5172 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005173 return 0;
5174}
5175
5176__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5177{
5178 struct hlua_txn *htxn;
5179
5180 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5181 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5182
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005183 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005184 WILL_LJMP(lua_error(L));
5185
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005186 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005187}
5188
5189__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5190{
5191 struct hlua_txn *htxn;
5192
5193 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5194 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5195
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005196 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005197 WILL_LJMP(lua_error(L));
5198
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005199 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005200}
5201
5202static int hlua_http_req_set_hdr(lua_State *L)
5203{
5204 struct hlua_txn *htxn;
5205
5206 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5207 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5208
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005209 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005210 WILL_LJMP(lua_error(L));
5211
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005212 hlua_http_del_hdr(L, &htxn->s->txn->req);
5213 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005214}
5215
5216static int hlua_http_res_set_hdr(lua_State *L)
5217{
5218 struct hlua_txn *htxn;
5219
5220 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5221 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5222
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005223 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005224 WILL_LJMP(lua_error(L));
5225
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005226 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5227 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005228}
5229
5230/* This function set the method. */
5231static int hlua_http_req_set_meth(lua_State *L)
5232{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005233 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005234 size_t name_len;
5235 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005236
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005237 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005238 WILL_LJMP(lua_error(L));
5239
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005240 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005241 return 1;
5242}
5243
5244/* This function set the method. */
5245static int hlua_http_req_set_path(lua_State *L)
5246{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005247 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005248 size_t name_len;
5249 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005250
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005251 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005252 WILL_LJMP(lua_error(L));
5253
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005254 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005255 return 1;
5256}
5257
5258/* This function set the query-string. */
5259static int hlua_http_req_set_query(lua_State *L)
5260{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005261 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005262 size_t name_len;
5263 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005264
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005265 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005266 WILL_LJMP(lua_error(L));
5267
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005268 /* Check length. */
5269 if (name_len > trash.size - 1) {
5270 lua_pushboolean(L, 0);
5271 return 1;
5272 }
5273
5274 /* Add the mark question as prefix. */
5275 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005276 trash.area[trash.data++] = '?';
5277 memcpy(trash.area + trash.data, name, name_len);
5278 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005279
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005280 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005281 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005282 return 1;
5283}
5284
5285/* This function set the uri. */
5286static int hlua_http_req_set_uri(lua_State *L)
5287{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005288 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005289 size_t name_len;
5290 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005291
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005292 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005293 WILL_LJMP(lua_error(L));
5294
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005295 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005296 return 1;
5297}
5298
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005299/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005300static int hlua_http_res_set_status(lua_State *L)
5301{
5302 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5303 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005304 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5305 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005306
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005307 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005308 WILL_LJMP(lua_error(L));
5309
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005310 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005311 return 0;
5312}
5313
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005314/*
5315 *
5316 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005317 * Class TXN
5318 *
5319 *
5320 */
5321
5322/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005323 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005324 */
5325__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5326{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005327 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005328}
5329
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005330__LJMP static int hlua_set_var(lua_State *L)
5331{
5332 struct hlua_txn *htxn;
5333 const char *name;
5334 size_t len;
5335 struct sample smp;
5336
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005337 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5338 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005339
5340 /* It is useles to retrieve the stream, but this function
5341 * runs only in a stream context.
5342 */
5343 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5344 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5345
5346 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005347 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005348 hlua_lua2smp(L, 3, &smp);
5349
5350 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005351 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005352
5353 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5354 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5355 else
5356 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5357
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005358 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005359}
5360
Christopher Faulet85d79c92016-11-09 16:54:56 +01005361__LJMP static int hlua_unset_var(lua_State *L)
5362{
5363 struct hlua_txn *htxn;
5364 const char *name;
5365 size_t len;
5366 struct sample smp;
5367
5368 MAY_LJMP(check_args(L, 2, "unset_var"));
5369
5370 /* It is useles to retrieve the stream, but this function
5371 * runs only in a stream context.
5372 */
5373 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5374 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5375
5376 /* Unset the variable. */
5377 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005378 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5379 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005380}
5381
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005382__LJMP static int hlua_get_var(lua_State *L)
5383{
5384 struct hlua_txn *htxn;
5385 const char *name;
5386 size_t len;
5387 struct sample smp;
5388
5389 MAY_LJMP(check_args(L, 2, "get_var"));
5390
5391 /* It is useles to retrieve the stream, but this function
5392 * runs only in a stream context.
5393 */
5394 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5395 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5396
Willy Tarreau7560dd42016-03-10 16:28:58 +01005397 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005398 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005399 lua_pushnil(L);
5400 return 1;
5401 }
5402
5403 return hlua_smp2lua(L, &smp);
5404}
5405
Willy Tarreau59551662015-03-10 14:23:13 +01005406__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005407{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005408 struct hlua *hlua;
5409
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005410 MAY_LJMP(check_args(L, 2, "set_priv"));
5411
Willy Tarreau87b09662015-04-03 00:22:06 +02005412 /* It is useles to retrieve the stream, but this function
5413 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005414 */
5415 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005416
5417 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005418 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005419 if (!hlua)
5420 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005421
5422 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005423 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005424
5425 /* Get and store new value. */
5426 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5427 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5428
5429 return 0;
5430}
5431
Willy Tarreau59551662015-03-10 14:23:13 +01005432__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005433{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005434 struct hlua *hlua;
5435
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005436 MAY_LJMP(check_args(L, 1, "get_priv"));
5437
Willy Tarreau87b09662015-04-03 00:22:06 +02005438 /* It is useles to retrieve the stream, but this function
5439 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005440 */
5441 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005442
5443 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005444 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005445 if (!hlua) {
5446 lua_pushnil(L);
5447 return 1;
5448 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005449
5450 /* Push configuration index in the stack. */
5451 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5452
5453 return 1;
5454}
5455
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005456/* Create stack entry containing a class TXN. This function
5457 * return 0 if the stack does not contains free slots,
5458 * otherwise it returns 1.
5459 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005460static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005461{
Willy Tarreaude491382015-04-06 11:04:28 +02005462 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005463
5464 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005465 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005466 return 0;
5467
5468 /* NOTE: The allocation never fails. The failure
5469 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005470 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005471 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005472 /* Create the object: obj[0] = userdata. */
5473 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005474 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005475 lua_rawseti(L, -2, 0);
5476
Willy Tarreaude491382015-04-06 11:04:28 +02005477 htxn->s = s;
5478 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005479 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005480 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005481
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005482 /* Create the "f" field that contains a list of fetches. */
5483 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005484 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005485 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005486 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005487
5488 /* Create the "sf" field that contains a list of stringsafe fetches. */
5489 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005490 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005491 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005492 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005493
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005494 /* Create the "c" field that contains a list of converters. */
5495 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005496 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005497 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005498 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005499
5500 /* Create the "sc" field that contains a list of stringsafe converters. */
5501 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005502 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005503 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005504 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005505
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005506 /* Create the "req" field that contains the request channel object. */
5507 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005508 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005509 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005510 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005511
5512 /* Create the "res" field that contains the response channel object. */
5513 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005514 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005515 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005516 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005517
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005518 /* Creates the HTTP object is the current proxy allows http. */
5519 lua_pushstring(L, "http");
5520 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005521 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005522 return 0;
5523 }
5524 else
5525 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005526 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005527
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005528 /* Pop a class sesison metatable and affect it to the userdata. */
5529 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5530 lua_setmetatable(L, -2);
5531
5532 return 1;
5533}
5534
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005535__LJMP static int hlua_txn_deflog(lua_State *L)
5536{
5537 const char *msg;
5538 struct hlua_txn *htxn;
5539
5540 MAY_LJMP(check_args(L, 2, "deflog"));
5541 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5542 msg = MAY_LJMP(luaL_checkstring(L, 2));
5543
5544 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5545 return 0;
5546}
5547
5548__LJMP static int hlua_txn_log(lua_State *L)
5549{
5550 int level;
5551 const char *msg;
5552 struct hlua_txn *htxn;
5553
5554 MAY_LJMP(check_args(L, 3, "log"));
5555 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5556 level = MAY_LJMP(luaL_checkinteger(L, 2));
5557 msg = MAY_LJMP(luaL_checkstring(L, 3));
5558
5559 if (level < 0 || level >= NB_LOG_LEVELS)
5560 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5561
5562 hlua_sendlog(htxn->s->be, level, msg);
5563 return 0;
5564}
5565
5566__LJMP static int hlua_txn_log_debug(lua_State *L)
5567{
5568 const char *msg;
5569 struct hlua_txn *htxn;
5570
5571 MAY_LJMP(check_args(L, 2, "Debug"));
5572 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5573 msg = MAY_LJMP(luaL_checkstring(L, 2));
5574 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5575 return 0;
5576}
5577
5578__LJMP static int hlua_txn_log_info(lua_State *L)
5579{
5580 const char *msg;
5581 struct hlua_txn *htxn;
5582
5583 MAY_LJMP(check_args(L, 2, "Info"));
5584 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5585 msg = MAY_LJMP(luaL_checkstring(L, 2));
5586 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5587 return 0;
5588}
5589
5590__LJMP static int hlua_txn_log_warning(lua_State *L)
5591{
5592 const char *msg;
5593 struct hlua_txn *htxn;
5594
5595 MAY_LJMP(check_args(L, 2, "Warning"));
5596 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5597 msg = MAY_LJMP(luaL_checkstring(L, 2));
5598 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5599 return 0;
5600}
5601
5602__LJMP static int hlua_txn_log_alert(lua_State *L)
5603{
5604 const char *msg;
5605 struct hlua_txn *htxn;
5606
5607 MAY_LJMP(check_args(L, 2, "Alert"));
5608 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5609 msg = MAY_LJMP(luaL_checkstring(L, 2));
5610 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5611 return 0;
5612}
5613
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005614__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5615{
5616 struct hlua_txn *htxn;
5617 int ll;
5618
5619 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5620 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5621 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5622
5623 if (ll < 0 || ll > 7)
5624 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5625
5626 htxn->s->logs.level = ll;
5627 return 0;
5628}
5629
5630__LJMP static int hlua_txn_set_tos(lua_State *L)
5631{
5632 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005633 int tos;
5634
5635 MAY_LJMP(check_args(L, 2, "set_tos"));
5636 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5637 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5638
Willy Tarreau1a18b542018-12-11 16:37:42 +01005639 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005640 return 0;
5641}
5642
5643__LJMP static int hlua_txn_set_mark(lua_State *L)
5644{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005645 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005646 int mark;
5647
5648 MAY_LJMP(check_args(L, 2, "set_mark"));
5649 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5650 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5651
Lukas Tribus579e3e32019-08-11 18:03:45 +02005652 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005653 return 0;
5654}
5655
Patrick Hemmer268a7072018-05-11 12:52:31 -04005656__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5657{
5658 struct hlua_txn *htxn;
5659
5660 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5661 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5662 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5663 return 0;
5664}
5665
5666__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5667{
5668 struct hlua_txn *htxn;
5669
5670 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5671 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5672 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5673 return 0;
5674}
5675
Christopher Faulet700d9e82020-01-31 12:21:52 +01005676/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005677 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005678 * message and terminate the transaction. It returns 1 on success and 0 on
5679 * error. The Reply must be on top of the stack.
5680 */
5681__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5682{
5683 struct htx *htx;
5684 struct htx_sl *sl;
5685 struct h1m h1m;
5686 const char *status, *reason, *body;
5687 size_t status_len, reason_len, body_len;
5688 int ret, code, flags;
5689
5690 code = 200;
5691 status = "200";
5692 status_len = 3;
5693 ret = lua_getfield(L, -1, "status");
5694 if (ret == LUA_TNUMBER) {
5695 code = lua_tointeger(L, -1);
5696 status = lua_tolstring(L, -1, &status_len);
5697 }
5698 lua_pop(L, 1);
5699
5700 reason = http_get_reason(code);
5701 reason_len = strlen(reason);
5702 ret = lua_getfield(L, -1, "reason");
5703 if (ret == LUA_TSTRING)
5704 reason = lua_tolstring(L, -1, &reason_len);
5705 lua_pop(L, 1);
5706
5707 body = NULL;
5708 body_len = 0;
5709 ret = lua_getfield(L, -1, "body");
5710 if (ret == LUA_TSTRING)
5711 body = lua_tolstring(L, -1, &body_len);
5712 lua_pop(L, 1);
5713
5714 /* Prepare the response before inserting the headers */
5715 h1m_init_res(&h1m);
5716 htx = htx_from_buf(&s->res.buf);
5717 channel_htx_truncate(&s->res, htx);
5718 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5719 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5720 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5721 ist2(status, status_len), ist2(reason, reason_len));
5722 }
5723 else {
5724 flags = HTX_SL_F_IS_RESP;
5725 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5726 ist2(status, status_len), ist2(reason, reason_len));
5727 }
5728 if (!sl)
5729 goto fail;
5730 sl->info.res.status = code;
5731
5732 /* Push in the stack the "headers" entry. */
5733 ret = lua_getfield(L, -1, "headers");
5734 if (ret != LUA_TTABLE)
5735 goto skip_headers;
5736
5737 lua_pushnil(L);
5738 while (lua_next(L, -2) != 0) {
5739 struct ist name, value;
5740 const char *n, *v;
5741 size_t nlen, vlen;
5742
5743 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5744 /* Skip element if the key is not a string or if the value is not a table */
5745 goto next_hdr;
5746 }
5747
5748 n = lua_tolstring(L, -2, &nlen);
5749 name = ist2(n, nlen);
5750 if (isteqi(name, ist("content-length"))) {
5751 /* Always skip content-length header. It will be added
5752 * later with the correct len
5753 */
5754 goto next_hdr;
5755 }
5756
5757 /* Loop on header's values */
5758 lua_pushnil(L);
5759 while (lua_next(L, -2)) {
5760 if (!lua_isstring(L, -1)) {
5761 /* Skip the value if it is not a string */
5762 goto next_value;
5763 }
5764
5765 v = lua_tolstring(L, -1, &vlen);
5766 value = ist2(v, vlen);
5767
5768 if (isteqi(name, ist("transfer-encoding")))
5769 h1_parse_xfer_enc_header(&h1m, value);
5770 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5771 goto fail;
5772
5773 next_value:
5774 lua_pop(L, 1);
5775 }
5776
5777 next_hdr:
5778 lua_pop(L, 1);
5779 }
5780 skip_headers:
5781 lua_pop(L, 1);
5782
5783 /* Update h1m flags: CLEN is set if CHNK is not present */
5784 if (!(h1m.flags & H1_MF_CHNK)) {
5785 const char *clen = ultoa(body_len);
5786
5787 h1m.flags |= H1_MF_CLEN;
5788 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5789 goto fail;
5790 }
5791 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5792 h1m.flags |= H1_MF_XFER_LEN;
5793
5794 /* Update HTX start-line flags */
5795 if (h1m.flags & H1_MF_XFER_ENC)
5796 flags |= HTX_SL_F_XFER_ENC;
5797 if (h1m.flags & H1_MF_XFER_LEN) {
5798 flags |= HTX_SL_F_XFER_LEN;
5799 if (h1m.flags & H1_MF_CHNK)
5800 flags |= HTX_SL_F_CHNK;
5801 else if (h1m.flags & H1_MF_CLEN)
5802 flags |= HTX_SL_F_CLEN;
5803 if (h1m.body_len == 0)
5804 flags |= HTX_SL_F_BODYLESS;
5805 }
5806 sl->flags |= flags;
5807
5808
5809 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5810 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5811 !htx_add_endof(htx, HTX_BLK_EOM))
5812 goto fail;
5813
5814 /* Now, forward the response and terminate the transaction */
5815 s->txn->status = code;
5816 htx_to_buf(htx, &s->res.buf);
5817 if (!http_forward_proxy_resp(s, 1))
5818 goto fail;
5819
5820 return 1;
5821
5822 fail:
5823 channel_htx_truncate(&s->res, htx);
5824 return 0;
5825}
5826
5827/* Terminate a transaction if called from a lua action. For TCP streams,
5828 * processing is just aborted. Nothing is returned to the client and all
5829 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5830 * is forwarded to the client before terminating the transaction. On success,
5831 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5832 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5833 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005834 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005835__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005836{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005837 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005838 struct stream *s;
5839 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005840
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005841 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005842
Christopher Faulet700d9e82020-01-31 12:21:52 +01005843 /* If the flags NOTERM is set, we cannot terminate the session, so we
5844 * just end the execution of the current lua code. */
5845 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005846 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005847
Christopher Faulet700d9e82020-01-31 12:21:52 +01005848 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005849 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005850 struct channel *req = &s->req;
5851 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005852
Christopher Faulet700d9e82020-01-31 12:21:52 +01005853 channel_auto_read(req);
5854 channel_abort(req);
5855 channel_auto_close(req);
5856 channel_erase(req);
5857
5858 res->wex = tick_add_ifset(now_ms, res->wto);
5859 channel_auto_read(res);
5860 channel_auto_close(res);
5861 channel_shutr_now(res);
5862
5863 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5864 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005865 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005866
Christopher Faulet700d9e82020-01-31 12:21:52 +01005867 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5868 /* No reply or invalid reply */
5869 s->txn->status = 0;
5870 http_reply_and_close(s, 0, NULL);
5871 }
5872 else {
5873 /* Remove extra args to have the reply on top of the stack */
5874 if (lua_gettop(L) > 2)
5875 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005876
Christopher Faulet700d9e82020-01-31 12:21:52 +01005877 if (!hlua_txn_forward_reply(L, s)) {
5878 if (!(s->flags & SF_ERR_MASK))
5879 s->flags |= SF_ERR_PRXCOND;
5880 lua_pushinteger(L, ACT_RET_ERR);
5881 WILL_LJMP(hlua_done(L));
5882 return 0; /* Never reached */
5883 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005884 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005885
Christopher Faulet700d9e82020-01-31 12:21:52 +01005886 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5887 if (htxn->dir == SMP_OPT_DIR_REQ) {
5888 /* let's log the request time */
5889 s->logs.tv_request = now;
5890 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5891 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5892 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005893
Christopher Faulet700d9e82020-01-31 12:21:52 +01005894 done:
5895 if (!(s->flags & SF_ERR_MASK))
5896 s->flags |= SF_ERR_LOCAL;
5897 if (!(s->flags & SF_FINST_MASK))
5898 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005899
Christopher Faulet4ad73102020-03-05 11:07:31 +01005900 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005901 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005902 return 0;
5903}
5904
Christopher Faulet700d9e82020-01-31 12:21:52 +01005905/*
5906 *
5907 *
5908 * Class REPLY
5909 *
5910 *
5911 */
5912
5913/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5914 * free slots, the function fails and returns 0;
5915 */
5916static int hlua_txn_reply_new(lua_State *L)
5917{
5918 struct hlua_txn *htxn;
5919 const char *reason, *body = NULL;
5920 int ret, status;
5921
5922 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005923 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005924 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5925 WILL_LJMP(lua_error(L));
5926 }
5927
5928 /* Default value */
5929 status = 200;
5930 reason = http_get_reason(status);
5931
5932 if (lua_istable(L, 2)) {
5933 /* load status and reason from the table argument at index 2 */
5934 ret = lua_getfield(L, 2, "status");
5935 if (ret == LUA_TNIL)
5936 goto reason;
5937 else if (ret != LUA_TNUMBER) {
5938 /* invalid status: ignore the reason */
5939 goto body;
5940 }
5941 status = lua_tointeger(L, -1);
5942
5943 reason:
5944 lua_pop(L, 1); /* restore the stack: remove status */
5945 ret = lua_getfield(L, 2, "reason");
5946 if (ret == LUA_TSTRING)
5947 reason = lua_tostring(L, -1);
5948
5949 body:
5950 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5951 ret = lua_getfield(L, 2, "body");
5952 if (ret == LUA_TSTRING)
5953 body = lua_tostring(L, -1);
5954 lua_pop(L, 1); /* restore the stack: remove body */
5955 }
5956
5957 /* Create the Reply table */
5958 lua_newtable(L);
5959
5960 /* Add status element */
5961 lua_pushstring(L, "status");
5962 lua_pushinteger(L, status);
5963 lua_settable(L, -3);
5964
5965 /* Add reason element */
5966 reason = http_get_reason(status);
5967 lua_pushstring(L, "reason");
5968 lua_pushstring(L, reason);
5969 lua_settable(L, -3);
5970
5971 /* Add body element, nil if undefined */
5972 lua_pushstring(L, "body");
5973 if (body)
5974 lua_pushstring(L, body);
5975 else
5976 lua_pushnil(L);
5977 lua_settable(L, -3);
5978
5979 /* Add headers element */
5980 lua_pushstring(L, "headers");
5981 lua_newtable(L);
5982
5983 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5984 if (lua_istable(L, 2)) {
5985 /* load headers from the table argument at index 2. If it is a table, copy it. */
5986 ret = lua_getfield(L, 2, "headers");
5987 if (ret == LUA_TTABLE) {
5988 /* stack: [ ... <headers:table>, <table> ] */
5989 lua_pushnil(L);
5990 while (lua_next(L, -2) != 0) {
5991 /* stack: [ ... <headers:table>, <table>, k, v] */
5992 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5993 /* invalid value type, skip it */
5994 lua_pop(L, 1);
5995 continue;
5996 }
5997
5998
5999 /* Duplicate the key and swap it with the value. */
6000 lua_pushvalue(L, -2);
6001 lua_insert(L, -2);
6002 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6003
6004 lua_newtable(L);
6005 lua_insert(L, -2);
6006 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6007
6008 if (lua_isstring(L, -1)) {
6009 /* push the value in the inner table */
6010 lua_rawseti(L, -2, 1);
6011 }
6012 else { /* table */
6013 lua_pushnil(L);
6014 while (lua_next(L, -2) != 0) {
6015 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6016 if (!lua_isstring(L, -1)) {
6017 /* invalid value type, skip it*/
6018 lua_pop(L, 1);
6019 continue;
6020 }
6021 /* push the value in the inner table */
6022 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6023 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6024 }
6025 lua_pop(L, 1);
6026 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6027 }
6028
6029 /* push (k,v) on the stack in the headers table:
6030 * stack: [ ... <headers:table>, <table>, k, k, v ]
6031 */
6032 lua_settable(L, -5);
6033 /* stack: [ ... <headers:table>, <table>, k ] */
6034 }
6035 }
6036 lua_pop(L, 1);
6037 }
6038 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6039 lua_settable(L, -3);
6040 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6041
6042 /* Pop a class sesison metatable and affect it to the userdata. */
6043 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6044 lua_setmetatable(L, -2);
6045 return 1;
6046}
6047
6048/* Set the reply status code, and optionally the reason. If no reason is
6049 * provided, the default one corresponding to the status code is used.
6050 */
6051__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6052{
6053 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6054 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6055
6056 /* First argument (self) must be a table */
6057 luaL_checktype(L, 1, LUA_TTABLE);
6058
6059 if (status < 100 || status > 599) {
6060 lua_pushboolean(L, 0);
6061 return 1;
6062 }
6063 if (!reason)
6064 reason = http_get_reason(status);
6065
6066 lua_pushinteger(L, status);
6067 lua_setfield(L, 1, "status");
6068
6069 lua_pushstring(L, reason);
6070 lua_setfield(L, 1, "reason");
6071
6072 lua_pushboolean(L, 1);
6073 return 1;
6074}
6075
6076/* Add a header into the reply object. Each header name is associated to an
6077 * array of values in the "headers" table. If the header name is not found, a
6078 * new entry is created.
6079 */
6080__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6081{
6082 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6083 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6084 int ret;
6085
6086 /* First argument (self) must be a table */
6087 luaL_checktype(L, 1, LUA_TTABLE);
6088
6089 /* Push in the stack the "headers" entry. */
6090 ret = lua_getfield(L, 1, "headers");
6091 if (ret != LUA_TTABLE) {
6092 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6093 WILL_LJMP(lua_error(L));
6094 }
6095
6096 /* check if the header is already registered. If not, register it. */
6097 ret = lua_getfield(L, -1, name);
6098 if (ret == LUA_TNIL) {
6099 /* Entry not found. */
6100 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6101
6102 /* Insert the new header name in the array in the top of the stack.
6103 * It left the new array in the top of the stack.
6104 */
6105 lua_newtable(L);
6106 lua_pushstring(L, name);
6107 lua_pushvalue(L, -2);
6108 lua_settable(L, -4);
6109 }
6110 else if (ret != LUA_TTABLE) {
6111 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6112 WILL_LJMP(lua_error(L));
6113 }
6114
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006115 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006116 * the header value as new entry.
6117 */
6118 lua_pushstring(L, value);
6119 ret = lua_rawlen(L, -2);
6120 lua_rawseti(L, -2, ret + 1);
6121
6122 lua_pushboolean(L, 1);
6123 return 1;
6124}
6125
6126/* Remove all occurrences of a given header name. */
6127__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6128{
6129 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6130 int ret;
6131
6132 /* First argument (self) must be a table */
6133 luaL_checktype(L, 1, LUA_TTABLE);
6134
6135 /* Push in the stack the "headers" entry. */
6136 ret = lua_getfield(L, 1, "headers");
6137 if (ret != LUA_TTABLE) {
6138 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6139 WILL_LJMP(lua_error(L));
6140 }
6141
6142 lua_pushstring(L, name);
6143 lua_pushnil(L);
6144 lua_settable(L, -3);
6145
6146 lua_pushboolean(L, 1);
6147 return 1;
6148}
6149
6150/* Set the reply's body. Overwrite any existing entry. */
6151__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6152{
6153 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6154
6155 /* First argument (self) must be a table */
6156 luaL_checktype(L, 1, LUA_TTABLE);
6157
6158 lua_pushstring(L, payload);
6159 lua_setfield(L, 1, "body");
6160
6161 lua_pushboolean(L, 1);
6162 return 1;
6163}
6164
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006165__LJMP static int hlua_log(lua_State *L)
6166{
6167 int level;
6168 const char *msg;
6169
6170 MAY_LJMP(check_args(L, 2, "log"));
6171 level = MAY_LJMP(luaL_checkinteger(L, 1));
6172 msg = MAY_LJMP(luaL_checkstring(L, 2));
6173
6174 if (level < 0 || level >= NB_LOG_LEVELS)
6175 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6176
6177 hlua_sendlog(NULL, level, msg);
6178 return 0;
6179}
6180
6181__LJMP static int hlua_log_debug(lua_State *L)
6182{
6183 const char *msg;
6184
6185 MAY_LJMP(check_args(L, 1, "debug"));
6186 msg = MAY_LJMP(luaL_checkstring(L, 1));
6187 hlua_sendlog(NULL, LOG_DEBUG, msg);
6188 return 0;
6189}
6190
6191__LJMP static int hlua_log_info(lua_State *L)
6192{
6193 const char *msg;
6194
6195 MAY_LJMP(check_args(L, 1, "info"));
6196 msg = MAY_LJMP(luaL_checkstring(L, 1));
6197 hlua_sendlog(NULL, LOG_INFO, msg);
6198 return 0;
6199}
6200
6201__LJMP static int hlua_log_warning(lua_State *L)
6202{
6203 const char *msg;
6204
6205 MAY_LJMP(check_args(L, 1, "warning"));
6206 msg = MAY_LJMP(luaL_checkstring(L, 1));
6207 hlua_sendlog(NULL, LOG_WARNING, msg);
6208 return 0;
6209}
6210
6211__LJMP static int hlua_log_alert(lua_State *L)
6212{
6213 const char *msg;
6214
6215 MAY_LJMP(check_args(L, 1, "alert"));
6216 msg = MAY_LJMP(luaL_checkstring(L, 1));
6217 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006218 return 0;
6219}
6220
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006221__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006222{
6223 int wakeup_ms = lua_tointeger(L, -1);
6224 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006225 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006226 return 0;
6227}
6228
6229__LJMP static int hlua_sleep(lua_State *L)
6230{
6231 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006232 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006233
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006234 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006235
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006236 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006237 wakeup_ms = tick_add(now_ms, delay);
6238 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006239
Willy Tarreau9635e032018-10-16 17:52:55 +02006240 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006241 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006242}
6243
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006244__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006245{
6246 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006247 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006248
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006249 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006250
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006251 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006252 wakeup_ms = tick_add(now_ms, delay);
6253 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006254
Willy Tarreau9635e032018-10-16 17:52:55 +02006255 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006256 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006257}
6258
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006259/* This functionis an LUA binding. it permits to give back
6260 * the hand at the HAProxy scheduler. It is used when the
6261 * LUA processing consumes a lot of time.
6262 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006263__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006264{
6265 return 0;
6266}
6267
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006268__LJMP static int hlua_yield(lua_State *L)
6269{
Willy Tarreau9635e032018-10-16 17:52:55 +02006270 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006271 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006272}
6273
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006274/* This function change the nice of the currently executed
6275 * task. It is used set low or high priority at the current
6276 * task.
6277 */
Willy Tarreau59551662015-03-10 14:23:13 +01006278__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006279{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006280 struct hlua *hlua;
6281 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006282
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006283 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006284 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006285
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006286 /* Get hlua struct, or NULL if we execute from main lua state */
6287 hlua = hlua_gethlua(L);
6288
6289 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006290 if (!hlua || !hlua->task)
6291 return 0;
6292
6293 if (nice < -1024)
6294 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006295 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006296 nice = 1024;
6297
6298 hlua->task->nice = nice;
6299 return 0;
6300}
6301
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006302/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006303 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6304 * return an E_AGAIN signal, the emmiter of this signal must set a
6305 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006306 *
6307 * Task wrapper are longjmp safe because the only one Lua code
6308 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006309 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006310struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006311{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006312 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006313 enum hlua_exec status;
6314
Christopher Faulet5bc99722018-04-25 10:34:45 +02006315 if (task->thread_mask == MAX_THREADS_MASK)
6316 task_set_affinity(task, tid_bit);
6317
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006318 /* If it is the first call to the task, we must initialize the
6319 * execution timeouts.
6320 */
6321 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006322 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006323
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006324 /* Execute the Lua code. */
6325 status = hlua_ctx_resume(hlua, 1);
6326
6327 switch (status) {
6328 /* finished or yield */
6329 case HLUA_E_OK:
6330 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006331 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006332 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006333 break;
6334
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006335 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006336 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006337 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006338 break;
6339
6340 /* finished with error. */
6341 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006342 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006343 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006344 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006345 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006346 break;
6347
6348 case HLUA_E_ERR:
6349 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006350 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006351 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006352 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006353 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006354 break;
6355 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006356 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006357}
6358
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006359/* This function is an LUA binding that register LUA function to be
6360 * executed after the HAProxy configuration parsing and before the
6361 * HAProxy scheduler starts. This function expect only one LUA
6362 * argument that is a function. This function returns nothing, but
6363 * throws if an error is encountered.
6364 */
6365__LJMP static int hlua_register_init(lua_State *L)
6366{
6367 struct hlua_init_function *init;
6368 int ref;
6369
6370 MAY_LJMP(check_args(L, 1, "register_init"));
6371
6372 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6373
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006374 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006375 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006376 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006377
6378 init->function_ref = ref;
Thierry Fournierc7492592020-11-28 23:57:24 +01006379 LIST_ADDQ(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006380 return 0;
6381}
6382
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006383/* This functio is an LUA binding. It permits to register a task
6384 * executed in parallel of the main HAroxy activity. The task is
6385 * created and it is set in the HAProxy scheduler. It can be called
6386 * from the "init" section, "post init" or during the runtime.
6387 *
6388 * Lua prototype:
6389 *
6390 * <none> core.register_task(<function>)
6391 */
6392static int hlua_register_task(lua_State *L)
6393{
6394 struct hlua *hlua;
6395 struct task *task;
6396 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006397 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006398
6399 MAY_LJMP(check_args(L, 1, "register_task"));
6400
6401 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6402
Thierry Fournier75fc0292020-11-28 13:18:56 +01006403 /* Get the reference state. If the reference is NULL, L is the master
6404 * state, otherwise hlua->T is.
6405 */
6406 hlua = hlua_gethlua(L);
6407 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006408 /* we are in runtime processing */
6409 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006410 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006411 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006412 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006413
Willy Tarreaubafbe012017-11-24 17:34:44 +01006414 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006415 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006416 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006417
Thierry Fournier59f11be2020-11-29 00:37:41 +01006418 /* We are in the common lua state, execute the task anywhere,
6419 * otherwise, inherit the current thread identifier
6420 */
6421 if (state_id == 0)
6422 task = task_new(MAX_THREADS_MASK);
6423 else
6424 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006425 if (!task)
6426 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6427
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006428 task->context = hlua;
6429 task->process = hlua_process_task;
6430
Thierry Fournier021d9862020-11-28 23:42:03 +01006431 if (!hlua_ctx_init(hlua, state_id, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006432 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006433
6434 /* Restore the function in the stack. */
6435 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6436 hlua->nargs = 0;
6437
6438 /* Schedule task. */
6439 task_schedule(task, now_ms);
6440
6441 return 0;
6442}
6443
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006444/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6445 * doesn't allow "yield" functions because the HAProxy engine cannot
6446 * resume converters.
6447 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006448static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006449{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006450 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006451 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006452 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006453
Willy Tarreaube508f12016-03-10 11:47:01 +01006454 if (!stream)
6455 return 0;
6456
Willy Tarreau87b09662015-04-03 00:22:06 +02006457 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006458 * Lua context can be not initialized. This behavior
6459 * permits to save performances because a systematic
6460 * Lua initialization cause 5% performances loss.
6461 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006462 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006463 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006464 if (!stream->hlua) {
6465 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6466 return 0;
6467 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006468 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006469 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6470 return 0;
6471 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006472 }
6473
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006474 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006475 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006476
6477 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006478 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006479 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6480 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006481 else
6482 error = "critical error";
6483 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006484 return 0;
6485 }
6486
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006487 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006488 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006489 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006490 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006491 return 0;
6492 }
6493
6494 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006495 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006496
6497 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006498 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006499 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006500 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006501 return 0;
6502 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006503 hlua_smp2lua(stream->hlua->T, smp);
6504 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006505
6506 /* push keywords in the stack. */
6507 if (arg_p) {
6508 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006509 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006510 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006511 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006512 return 0;
6513 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006514 hlua_arg2lua(stream->hlua->T, arg_p);
6515 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006516 }
6517 }
6518
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006519 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006520 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006521
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006522 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006523 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006524 }
6525
6526 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006527 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006528 /* finished. */
6529 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006530 /* If the stack is empty, the function fails. */
6531 if (lua_gettop(stream->hlua->T) <= 0)
6532 return 0;
6533
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006534 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006535 hlua_lua2smp(stream->hlua->T, -1, smp);
6536 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006537 return 1;
6538
6539 /* yield. */
6540 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006541 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006542 return 0;
6543
6544 /* finished with error. */
6545 case HLUA_E_ERRMSG:
6546 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006547 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006548 fcn->name, lua_tostring(stream->hlua->T, -1));
6549 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006550 return 0;
6551
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006552 case HLUA_E_ETMOUT:
6553 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6554 return 0;
6555
6556 case HLUA_E_NOMEM:
6557 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6558 return 0;
6559
6560 case HLUA_E_YIELD:
6561 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6562 return 0;
6563
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006564 case HLUA_E_ERR:
6565 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006566 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006567 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006568
6569 default:
6570 return 0;
6571 }
6572}
6573
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006574/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6575 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006576 * resume sample-fetches. This function will be called by the sample
6577 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006578 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006579static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6580 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006581{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006582 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006583 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006584 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006585 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006586
Willy Tarreaube508f12016-03-10 11:47:01 +01006587 if (!stream)
6588 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006589
Willy Tarreau87b09662015-04-03 00:22:06 +02006590 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006591 * Lua context can be not initialized. This behavior
6592 * permits to save performances because a systematic
6593 * Lua initialization cause 5% performances loss.
6594 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006595 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006596 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006597 if (!stream->hlua) {
6598 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6599 return 0;
6600 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006601 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006602 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6603 return 0;
6604 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006605 }
6606
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006607 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006608 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006609
6610 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006611 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006612 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6613 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006614 else
6615 error = "critical error";
6616 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006617 return 0;
6618 }
6619
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006620 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006621 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006622 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006623 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006624 return 0;
6625 }
6626
6627 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006628 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006629
6630 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006631 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006632 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006633 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006634 return 0;
6635 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006636 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006637
6638 /* push keywords in the stack. */
6639 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6640 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006641 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006642 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006643 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006644 return 0;
6645 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006646 hlua_arg2lua(stream->hlua->T, arg_p);
6647 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006648 }
6649
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006650 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006651 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006652
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006653 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006654 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006655 }
6656
6657 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006658 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006659 /* finished. */
6660 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006661 /* If the stack is empty, the function fails. */
6662 if (lua_gettop(stream->hlua->T) <= 0)
6663 return 0;
6664
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006665 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006666 hlua_lua2smp(stream->hlua->T, -1, smp);
6667 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006668
6669 /* Set the end of execution flag. */
6670 smp->flags &= ~SMP_F_MAY_CHANGE;
6671 return 1;
6672
6673 /* yield. */
6674 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006675 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006676 return 0;
6677
6678 /* finished with error. */
6679 case HLUA_E_ERRMSG:
6680 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006681 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006682 fcn->name, lua_tostring(stream->hlua->T, -1));
6683 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006684 return 0;
6685
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006686 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006687 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6688 return 0;
6689
6690 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006691 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6692 return 0;
6693
6694 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006695 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6696 return 0;
6697
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006698 case HLUA_E_ERR:
6699 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006700 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006701 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006702
6703 default:
6704 return 0;
6705 }
6706}
6707
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006708/* This function is an LUA binding used for registering
6709 * "sample-conv" functions. It expects a converter name used
6710 * in the haproxy configuration file, and an LUA function.
6711 */
6712__LJMP static int hlua_register_converters(lua_State *L)
6713{
6714 struct sample_conv_kw_list *sck;
6715 const char *name;
6716 int ref;
6717 int len;
6718 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006719 struct sample_conv *sc;
6720 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006721
6722 MAY_LJMP(check_args(L, 2, "register_converters"));
6723
6724 /* First argument : converter name. */
6725 name = MAY_LJMP(luaL_checkstring(L, 1));
6726
6727 /* Second argument : lua function. */
6728 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6729
Thierry Fournierf67442e2020-11-28 20:41:07 +01006730 /* Check if the converter is already registered */
6731 trash = get_trash_chunk();
6732 chunk_printf(trash, "lua.%s", name);
6733 sc = find_sample_conv(trash->area, trash->data);
6734 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006735 fcn = sc->private;
6736 if (fcn->function_ref[hlua_state_id] != -1) {
6737 ha_warning("Trying to register converter 'lua.%s' more than once. "
6738 "This will become a hard error in version 2.5.\n", name);
6739 }
6740 fcn->function_ref[hlua_state_id] = ref;
6741 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006742 }
6743
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006744 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006745 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006746 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006747 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006748 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006749 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006750 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006751
6752 /* Fill fcn. */
6753 fcn->name = strdup(name);
6754 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006755 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01006756 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006757
6758 /* List head */
6759 sck->list.n = sck->list.p = NULL;
6760
6761 /* converter keyword. */
6762 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006763 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006764 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006765 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006766
6767 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6768 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006769 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 +01006770 sck->kw[0].val_args = NULL;
6771 sck->kw[0].in_type = SMP_T_STR;
6772 sck->kw[0].out_type = SMP_T_STR;
6773 sck->kw[0].private = fcn;
6774
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006775 /* Register this new converter */
6776 sample_register_convs(sck);
6777
6778 return 0;
6779}
6780
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006781/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006782 * "sample-fetch" functions. It expects a converter name used
6783 * in the haproxy configuration file, and an LUA function.
6784 */
6785__LJMP static int hlua_register_fetches(lua_State *L)
6786{
6787 const char *name;
6788 int ref;
6789 int len;
6790 struct sample_fetch_kw_list *sfk;
6791 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006792 struct sample_fetch *sf;
6793 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006794
6795 MAY_LJMP(check_args(L, 2, "register_fetches"));
6796
6797 /* First argument : sample-fetch name. */
6798 name = MAY_LJMP(luaL_checkstring(L, 1));
6799
6800 /* Second argument : lua function. */
6801 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6802
Thierry Fournierf67442e2020-11-28 20:41:07 +01006803 /* Check if the sample-fetch is already registered */
6804 trash = get_trash_chunk();
6805 chunk_printf(trash, "lua.%s", name);
6806 sf = find_sample_fetch(trash->area, trash->data);
6807 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006808 fcn = sf->private;
6809 if (fcn->function_ref[hlua_state_id] != -1) {
6810 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6811 "This will become a hard error in version 2.5.\n", name);
6812 }
6813 fcn->function_ref[hlua_state_id] = ref;
6814 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006815 }
6816
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006817 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006818 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006819 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006820 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006821 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006822 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006823 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006824
6825 /* Fill fcn. */
6826 fcn->name = strdup(name);
6827 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006828 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01006829 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006830
6831 /* List head */
6832 sfk->list.n = sfk->list.p = NULL;
6833
6834 /* sample-fetch keyword. */
6835 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006836 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006837 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006838 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006839
6840 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6841 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006842 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 +01006843 sfk->kw[0].val_args = NULL;
6844 sfk->kw[0].out_type = SMP_T_STR;
6845 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6846 sfk->kw[0].val = 0;
6847 sfk->kw[0].private = fcn;
6848
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006849 /* Register this new fetch. */
6850 sample_register_fetches(sfk);
6851
6852 return 0;
6853}
6854
Christopher Faulet501465d2020-02-26 14:54:16 +01006855/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006856 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006857__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006858{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006859 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006860 unsigned int delay;
6861 unsigned int wakeup_ms;
6862
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006863 /* Get hlua struct, or NULL if we execute from main lua state */
6864 hlua = hlua_gethlua(L);
6865 if (!hlua) {
6866 return 0;
6867 }
6868
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006869 MAY_LJMP(check_args(L, 1, "wake_time"));
6870
6871 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6872 wakeup_ms = tick_add(now_ms, delay);
6873 hlua->wake_time = wakeup_ms;
6874 return 0;
6875}
6876
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006877/* This function is a wrapper to execute each LUA function declared as an action
6878 * wrapper during the initialisation period. This function may return any
6879 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6880 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6881 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006882 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006883static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006884 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006885{
6886 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006887 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006888 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006889 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006890
6891 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006892 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6893 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6894 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6895 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006896 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006897 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006898 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006899 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006900
Willy Tarreau87b09662015-04-03 00:22:06 +02006901 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006902 * Lua context can be not initialized. This behavior
6903 * permits to save performances because a systematic
6904 * Lua initialization cause 5% performances loss.
6905 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006906 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006907 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006908 if (!s->hlua) {
6909 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006910 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006911 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006912 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006913 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 +01006914 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006915 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006916 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006917 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006918 }
6919
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006920 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006921 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006922
6923 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006924 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006925 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6926 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006927 else
6928 error = "critical error";
6929 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006930 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006931 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006932 }
6933
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006934 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006935 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006936 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006937 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006938 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006939 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006940 }
6941
6942 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006943 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 +01006944
Willy Tarreau87b09662015-04-03 00:22:06 +02006945 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006946 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006947 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006948 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006949 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006950 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006951 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006952 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006953
6954 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006955 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006956 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006957 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006958 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006959 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006960 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006961 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006962 lua_pushstring(s->hlua->T, *arg);
6963 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006964 }
6965
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006966 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006967 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006968
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006969 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006970 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006971 }
6972
6973 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006974 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006975 /* finished. */
6976 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006977 /* Catch the return value */
6978 if (lua_gettop(s->hlua->T) > 0)
6979 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006980
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006981 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006982 if (act_ret == ACT_RET_YIELD) {
6983 if (flags & ACT_OPT_FINAL)
6984 goto err_yield;
6985
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006986 if (dir == SMP_OPT_DIR_REQ)
6987 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6988 s->hlua->wake_time);
6989 else
6990 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6991 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006992 }
6993 goto end;
6994
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006995 /* yield. */
6996 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006997 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006998 if (dir == SMP_OPT_DIR_REQ)
6999 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7000 s->hlua->wake_time);
7001 else
7002 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7003 s->hlua->wake_time);
7004
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007005 /* Some actions can be wake up when a "write" event
7006 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007007 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007008 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007009 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007010 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007011 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007012 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007013 act_ret = ACT_RET_YIELD;
7014 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007015
7016 /* finished with error. */
7017 case HLUA_E_ERRMSG:
7018 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007019 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007020 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007021 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007022 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007023
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007024 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007025 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007026 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007027
7028 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007029 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007030 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007031
7032 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007033 err_yield:
7034 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007035 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007036 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007037 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007038
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007039 case HLUA_E_ERR:
7040 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007041 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007042 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007043
7044 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007045 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007046 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007047
7048 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007049 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007050 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007051 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007052}
7053
Olivier Houchard9f6af332018-05-25 14:04:04 +02007054struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007055{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007056 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007057
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007058 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007059 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007060 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007061}
7062
7063static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7064{
7065 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007066 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007067 struct task *task;
7068 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007069 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007070
Willy Tarreaubafbe012017-11-24 17:34:44 +01007071 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007072 if (!hlua) {
7073 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007074 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007075 return 0;
7076 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007077 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007078 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007079 ctx->ctx.hlua_apptcp.flags = 0;
7080
7081 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007082 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007083 if (!task) {
7084 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007085 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007086 return 0;
7087 }
7088 task->nice = 0;
7089 task->context = ctx;
7090 task->process = hlua_applet_wakeup;
7091 ctx->ctx.hlua_apptcp.task = task;
7092
7093 /* In the execution wrappers linked with a stream, the
7094 * Lua context can be not initialized. This behavior
7095 * permits to save performances because a systematic
7096 * Lua initialization cause 5% performances loss.
7097 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007098 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 +02007099 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007100 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007101 return 0;
7102 }
7103
7104 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007105 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007106
7107 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007108 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007109 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7110 error = lua_tostring(hlua->T, -1);
7111 else
7112 error = "critical error";
7113 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007114 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007115 return 0;
7116 }
7117
7118 /* Check stack available size. */
7119 if (!lua_checkstack(hlua->T, 1)) {
7120 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007121 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007122 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007123 return 0;
7124 }
7125
7126 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007127 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007128
7129 /* Create and and push object stream in the stack. */
7130 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7131 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007132 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007133 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007134 return 0;
7135 }
7136 hlua->nargs = 1;
7137
7138 /* push keywords in the stack. */
7139 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7140 if (!lua_checkstack(hlua->T, 1)) {
7141 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007142 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007143 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007144 return 0;
7145 }
7146 lua_pushstring(hlua->T, *arg);
7147 hlua->nargs++;
7148 }
7149
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007150 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007151
7152 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007153 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007154 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007155
7156 return 1;
7157}
7158
Willy Tarreau60409db2019-08-21 14:14:50 +02007159void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007160{
7161 struct stream_interface *si = ctx->owner;
7162 struct stream *strm = si_strm(si);
7163 struct channel *res = si_ic(si);
7164 struct act_rule *rule = ctx->rule;
7165 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007166 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007167
7168 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007169 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7170 /* eat the whole request */
7171 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007172 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007173 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007174
7175 /* If the stream is disconnect or closed, ldo nothing. */
7176 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7177 return;
7178
7179 /* Execute the function. */
7180 switch (hlua_ctx_resume(hlua, 1)) {
7181 /* finished. */
7182 case HLUA_E_OK:
7183 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7184
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007185 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007186 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007187 res->flags |= CF_READ_NULL;
7188 si_shutr(si);
7189 return;
7190
7191 /* yield. */
7192 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007193 if (hlua->wake_time != TICK_ETERNITY)
7194 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007195 return;
7196
7197 /* finished with error. */
7198 case HLUA_E_ERRMSG:
7199 /* Display log. */
7200 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007201 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007202 lua_pop(hlua->T, 1);
7203 goto error;
7204
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007205 case HLUA_E_ETMOUT:
7206 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007207 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007208 goto error;
7209
7210 case HLUA_E_NOMEM:
7211 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007212 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007213 goto error;
7214
7215 case HLUA_E_YIELD: /* unexpected */
7216 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007217 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007218 goto error;
7219
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007220 case HLUA_E_ERR:
7221 /* Display log. */
7222 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007223 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007224 goto error;
7225
7226 default:
7227 goto error;
7228 }
7229
7230error:
7231
7232 /* For all other cases, just close the stream. */
7233 si_shutw(si);
7234 si_shutr(si);
7235 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7236}
7237
7238static void hlua_applet_tcp_release(struct appctx *ctx)
7239{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007240 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007241 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007242 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007243 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007244}
7245
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007246/* The function returns 1 if the initialisation is complete, 0 if
7247 * an errors occurs and -1 if more data are required for initializing
7248 * the applet.
7249 */
7250static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7251{
7252 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007253 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007254 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007255 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007256 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007257 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007258
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007259 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007260 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007261 if (!hlua) {
7262 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007263 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007264 return 0;
7265 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007266 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007267 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007268 ctx->ctx.hlua_apphttp.left_bytes = -1;
7269 ctx->ctx.hlua_apphttp.flags = 0;
7270
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007271 if (txn->req.flags & HTTP_MSGF_VER_11)
7272 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7273
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007274 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007275 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007276 if (!task) {
7277 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007278 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007279 return 0;
7280 }
7281 task->nice = 0;
7282 task->context = ctx;
7283 task->process = hlua_applet_wakeup;
7284 ctx->ctx.hlua_apphttp.task = task;
7285
7286 /* In the execution wrappers linked with a stream, the
7287 * Lua context can be not initialized. This behavior
7288 * permits to save performances because a systematic
7289 * Lua initialization cause 5% performances loss.
7290 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007291 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 +02007292 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007293 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007294 return 0;
7295 }
7296
7297 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007298 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007299
7300 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007301 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007302 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7303 error = lua_tostring(hlua->T, -1);
7304 else
7305 error = "critical error";
7306 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007307 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007308 return 0;
7309 }
7310
7311 /* Check stack available size. */
7312 if (!lua_checkstack(hlua->T, 1)) {
7313 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007314 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007315 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007316 return 0;
7317 }
7318
7319 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007320 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007321
7322 /* Create and and push object stream in the stack. */
7323 if (!hlua_applet_http_new(hlua->T, ctx)) {
7324 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007325 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007326 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007327 return 0;
7328 }
7329 hlua->nargs = 1;
7330
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007331 /* push keywords in the stack. */
7332 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7333 if (!lua_checkstack(hlua->T, 1)) {
7334 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007335 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007336 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007337 return 0;
7338 }
7339 lua_pushstring(hlua->T, *arg);
7340 hlua->nargs++;
7341 }
7342
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007343 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007344
7345 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007346 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007347
7348 return 1;
7349}
7350
Willy Tarreau60409db2019-08-21 14:14:50 +02007351void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007352{
7353 struct stream_interface *si = ctx->owner;
7354 struct stream *strm = si_strm(si);
7355 struct channel *req = si_oc(si);
7356 struct channel *res = si_ic(si);
7357 struct act_rule *rule = ctx->rule;
7358 struct proxy *px = strm->be;
7359 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7360 struct htx *req_htx, *res_htx;
7361
7362 res_htx = htx_from_buf(&res->buf);
7363
7364 /* If the stream is disconnect or closed, ldo nothing. */
7365 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7366 goto out;
7367
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007368 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007369 if (!b_size(&res->buf)) {
7370 si_rx_room_blk(si);
7371 goto out;
7372 }
7373 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007374 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007375 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7376
7377 /* Set the currently running flag. */
7378 if (!HLUA_IS_RUNNING(hlua) &&
7379 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7380 struct htx_blk *blk;
7381 size_t count = co_data(req);
7382
7383 if (!count) {
7384 si_cant_get(si);
7385 goto out;
7386 }
7387
7388 /* We need to flush the request header. This left the body for
7389 * the Lua.
7390 */
7391 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007392 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007393 while (count && blk) {
7394 enum htx_blk_type type = htx_get_blk_type(blk);
7395 uint32_t sz = htx_get_blksz(blk);
7396
7397 if (sz > count) {
7398 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007399 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007400 goto out;
7401 }
7402
7403 count -= sz;
7404 co_set_data(req, co_data(req) - sz);
7405 blk = htx_remove_blk(req_htx, blk);
7406
7407 if (type == HTX_BLK_EOH)
7408 break;
7409 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007410 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007411 }
7412
7413 /* Executes The applet if it is not done. */
7414 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7415
7416 /* Execute the function. */
7417 switch (hlua_ctx_resume(hlua, 1)) {
7418 /* finished. */
7419 case HLUA_E_OK:
7420 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7421 break;
7422
7423 /* yield. */
7424 case HLUA_E_AGAIN:
7425 if (hlua->wake_time != TICK_ETERNITY)
7426 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007427 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007428
7429 /* finished with error. */
7430 case HLUA_E_ERRMSG:
7431 /* Display log. */
7432 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007433 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007434 lua_pop(hlua->T, 1);
7435 goto error;
7436
7437 case HLUA_E_ETMOUT:
7438 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007439 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007440 goto error;
7441
7442 case HLUA_E_NOMEM:
7443 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007444 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007445 goto error;
7446
7447 case HLUA_E_YIELD: /* unexpected */
7448 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007449 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007450 goto error;
7451
7452 case HLUA_E_ERR:
7453 /* Display log. */
7454 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007455 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007456 goto error;
7457
7458 default:
7459 goto error;
7460 }
7461 }
7462
7463 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007464 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7465 goto done;
7466
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007467 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7468 goto error;
7469
Christopher Faulet54b5e212019-06-04 10:08:28 +02007470 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007471 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007472 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7473 si_rx_room_blk(si);
7474 goto out;
7475 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007476 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007477 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7478 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007479 }
7480
7481 done:
7482 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007483 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007484 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007485 si_shutr(si);
7486 }
7487
7488 /* eat the whole request */
7489 if (co_data(req)) {
7490 req_htx = htx_from_buf(&req->buf);
7491 co_htx_skip(req, req_htx, co_data(req));
7492 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007493 }
7494 }
7495
7496 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007497 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007498 return;
7499
7500 error:
7501
7502 /* If we are in HTTP mode, and we are not send any
7503 * data, return a 500 server error in best effort:
7504 * if there is no room available in the buffer,
7505 * just close the connection.
7506 */
7507 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007508 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007509
7510 channel_erase(res);
7511 res->buf.data = b_data(err);
7512 memcpy(res->buf.area, b_head(err), b_data(err));
7513 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007514 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007515 }
7516 if (!(strm->flags & SF_ERR_MASK))
7517 strm->flags |= SF_ERR_RESOURCE;
7518 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7519 goto done;
7520}
7521
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007522static void hlua_applet_http_release(struct appctx *ctx)
7523{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007524 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007525 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007526 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007527 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007528}
7529
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007530/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007531 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007532 *
7533 * This function can fail with an abort() due to an Lua critical error.
7534 * We are in the configuration parsing process of HAProxy, this abort() is
7535 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007536 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007537static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7538 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007539{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007540 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007541 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007542
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007543 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007544 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007545 if (!rule->arg.hlua_rule) {
7546 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007547 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007548 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007549
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007550 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007551 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7552 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007553 if (!rule->arg.hlua_rule->args) {
7554 memprintf(err, "out of memory error");
7555 return ACT_RET_PRS_ERR;
7556 }
7557
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007558 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007559 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007560
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007561 /* Expect some arguments */
7562 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007563 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007564 memprintf(err, "expect %d arguments", fcn->nargs);
7565 return ACT_RET_PRS_ERR;
7566 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007567 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007568 if (!rule->arg.hlua_rule->args[i]) {
7569 memprintf(err, "out of memory error");
7570 return ACT_RET_PRS_ERR;
7571 }
7572 (*cur_arg)++;
7573 }
7574 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007575
Thierry FOURNIER42148732015-09-02 17:17:33 +02007576 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007577 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007578 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007579}
7580
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007581static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7582 struct act_rule *rule, char **err)
7583{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007584 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007585
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007586 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007587 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007588 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007589 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007590 * the call of this analyzer.
7591 */
7592 if (rule->from != ACT_F_HTTP_REQ) {
7593 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7594 return ACT_RET_PRS_ERR;
7595 }
7596
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007597 /* Memory for the rule. */
7598 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7599 if (!rule->arg.hlua_rule) {
7600 memprintf(err, "out of memory error");
7601 return ACT_RET_PRS_ERR;
7602 }
7603
7604 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007605 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007606
7607 /* TODO: later accept arguments. */
7608 rule->arg.hlua_rule->args = NULL;
7609
7610 /* Add applet pointer in the rule. */
7611 rule->applet.obj_type = OBJ_TYPE_APPLET;
7612 rule->applet.name = fcn->name;
7613 rule->applet.init = hlua_applet_http_init;
7614 rule->applet.fct = hlua_applet_http_fct;
7615 rule->applet.release = hlua_applet_http_release;
7616 rule->applet.timeout = hlua_timeout_applet;
7617
7618 return ACT_RET_PRS_OK;
7619}
7620
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007621/* This function is an LUA binding used for registering
7622 * "sample-conv" functions. It expects a converter name used
7623 * in the haproxy configuration file, and an LUA function.
7624 */
7625__LJMP static int hlua_register_action(lua_State *L)
7626{
7627 struct action_kw_list *akl;
7628 const char *name;
7629 int ref;
7630 int len;
7631 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007632 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007633 struct buffer *trash;
7634 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007635
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007636 /* Initialise the number of expected arguments at 0. */
7637 nargs = 0;
7638
7639 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7640 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007641
7642 /* First argument : converter name. */
7643 name = MAY_LJMP(luaL_checkstring(L, 1));
7644
7645 /* Second argument : environment. */
7646 if (lua_type(L, 2) != LUA_TTABLE)
7647 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7648
7649 /* Third argument : lua function. */
7650 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7651
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007652 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007653 if (lua_gettop(L) >= 4)
7654 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7655
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007656 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007657 lua_pushnil(L);
7658 while (lua_next(L, 2) != 0) {
7659 if (lua_type(L, -1) != LUA_TSTRING)
7660 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7661
Thierry Fournierf67442e2020-11-28 20:41:07 +01007662 /* Check if action exists */
7663 trash = get_trash_chunk();
7664 chunk_printf(trash, "lua.%s", name);
7665 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7666 akw = tcp_req_cont_action(trash->area);
7667 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7668 akw = tcp_res_cont_action(trash->area);
7669 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7670 akw = action_http_req_custom(trash->area);
7671 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7672 akw = action_http_res_custom(trash->area);
7673 } else {
7674 akw = NULL;
7675 }
7676 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007677 fcn = akw->private;
7678 if (fcn->function_ref[hlua_state_id] != -1) {
7679 ha_warning("Trying to register action 'lua.%s' more than once. "
7680 "This will become a hard error in version 2.5.\n", name);
7681 }
7682 fcn->function_ref[hlua_state_id] = ref;
7683
7684 /* pop the environment string. */
7685 lua_pop(L, 1);
7686 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007687 }
7688
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007689 /* Check required environment. Only accepted "http" or "tcp". */
7690 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007691 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007692 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007693 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007694 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007695 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007696 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007697
7698 /* Fill fcn. */
7699 fcn->name = strdup(name);
7700 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007701 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01007702 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007703
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007704 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007705 fcn->nargs = nargs;
7706
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007707 /* List head */
7708 akl->list.n = akl->list.p = NULL;
7709
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007710 /* action keyword. */
7711 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007712 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007713 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007714 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007715
7716 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7717
7718 akl->kw[0].match_pfx = 0;
7719 akl->kw[0].private = fcn;
7720 akl->kw[0].parse = action_register_lua;
7721
7722 /* select the action registering point. */
7723 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7724 tcp_req_cont_keywords_register(akl);
7725 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7726 tcp_res_cont_keywords_register(akl);
7727 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7728 http_req_keywords_register(akl);
7729 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7730 http_res_keywords_register(akl);
7731 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007732 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007733 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7734 "are expected.", lua_tostring(L, -1)));
7735
7736 /* pop the environment string. */
7737 lua_pop(L, 1);
7738 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007739 return ACT_RET_PRS_OK;
7740}
7741
7742static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7743 struct act_rule *rule, char **err)
7744{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007745 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007746
Christopher Faulet280f85b2019-07-15 15:02:04 +02007747 if (px->mode == PR_MODE_HTTP) {
7748 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007749 return ACT_RET_PRS_ERR;
7750 }
7751
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007752 /* Memory for the rule. */
7753 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7754 if (!rule->arg.hlua_rule) {
7755 memprintf(err, "out of memory error");
7756 return ACT_RET_PRS_ERR;
7757 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007758
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007759 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007760 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007761
7762 /* TODO: later accept arguments. */
7763 rule->arg.hlua_rule->args = NULL;
7764
7765 /* Add applet pointer in the rule. */
7766 rule->applet.obj_type = OBJ_TYPE_APPLET;
7767 rule->applet.name = fcn->name;
7768 rule->applet.init = hlua_applet_tcp_init;
7769 rule->applet.fct = hlua_applet_tcp_fct;
7770 rule->applet.release = hlua_applet_tcp_release;
7771 rule->applet.timeout = hlua_timeout_applet;
7772
7773 return 0;
7774}
7775
7776/* This function is an LUA binding used for registering
7777 * "sample-conv" functions. It expects a converter name used
7778 * in the haproxy configuration file, and an LUA function.
7779 */
7780__LJMP static int hlua_register_service(lua_State *L)
7781{
7782 struct action_kw_list *akl;
7783 const char *name;
7784 const char *env;
7785 int ref;
7786 int len;
7787 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007788 struct buffer *trash;
7789 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007790
7791 MAY_LJMP(check_args(L, 3, "register_service"));
7792
7793 /* First argument : converter name. */
7794 name = MAY_LJMP(luaL_checkstring(L, 1));
7795
7796 /* Second argument : environment. */
7797 env = MAY_LJMP(luaL_checkstring(L, 2));
7798
7799 /* Third argument : lua function. */
7800 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7801
Thierry Fournierf67442e2020-11-28 20:41:07 +01007802 /* Check for service already registered */
7803 trash = get_trash_chunk();
7804 chunk_printf(trash, "lua.%s", name);
7805 akw = service_find(trash->area);
7806 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007807 fcn = akw->private;
7808 if (fcn->function_ref[hlua_state_id] != -1) {
7809 ha_warning("Trying to register service 'lua.%s' more than once. "
7810 "This will become a hard error in version 2.5.\n", name);
7811 }
7812 fcn->function_ref[hlua_state_id] = ref;
7813 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007814 }
7815
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007816 /* Allocate and fill the sample fetch keyword struct. */
7817 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7818 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007819 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007820 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007821 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007822 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007823
7824 /* Fill fcn. */
7825 len = strlen("<lua.>") + strlen(name) + 1;
7826 fcn->name = calloc(1, len);
7827 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007828 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007829 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007830 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007831
7832 /* List head */
7833 akl->list.n = akl->list.p = NULL;
7834
7835 /* converter keyword. */
7836 len = strlen("lua.") + strlen(name) + 1;
7837 akl->kw[0].kw = calloc(1, len);
7838 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007839 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007840
7841 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7842
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007843 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007844 if (strcmp(env, "tcp") == 0)
7845 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007846 else if (strcmp(env, "http") == 0)
7847 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007848 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007849 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007850 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007851
7852 akl->kw[0].match_pfx = 0;
7853 akl->kw[0].private = fcn;
7854
7855 /* End of array. */
7856 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7857
7858 /* Register this new converter */
7859 service_keywords_register(akl);
7860
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007861 return 0;
7862}
7863
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007864/* This function initialises Lua cli handler. It copies the
7865 * arguments in the Lua stack and create channel IO objects.
7866 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007867static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007868{
7869 struct hlua *hlua;
7870 struct hlua_function *fcn;
7871 int i;
7872 const char *error;
7873
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007874 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007875 appctx->ctx.hlua_cli.fcn = private;
7876
Willy Tarreaubafbe012017-11-24 17:34:44 +01007877 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007878 if (!hlua) {
7879 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007880 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007881 }
7882 HLUA_INIT(hlua);
7883 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007884
7885 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007886 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007887 * applet_http. It is absolutely compatible.
7888 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007889 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007890 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007891 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007892 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007893 }
7894 appctx->ctx.hlua_cli.task->nice = 0;
7895 appctx->ctx.hlua_cli.task->context = appctx;
7896 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7897
7898 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01007899 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 +01007900 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007901 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007902 }
7903
7904 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007905 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007906 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7907 error = lua_tostring(hlua->T, -1);
7908 else
7909 error = "critical error";
7910 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7911 goto error;
7912 }
7913
7914 /* Check stack available size. */
7915 if (!lua_checkstack(hlua->T, 2)) {
7916 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7917 goto error;
7918 }
7919
7920 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007921 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007922
7923 /* Once the arguments parsed, the CLI is like an AppletTCP,
7924 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007925 */
7926 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7927 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7928 goto error;
7929 }
7930 hlua->nargs = 1;
7931
7932 /* push keywords in the stack. */
7933 for (i = 0; *args[i]; i++) {
7934 /* Check stack available size. */
7935 if (!lua_checkstack(hlua->T, 1)) {
7936 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7937 goto error;
7938 }
7939 lua_pushstring(hlua->T, args[i]);
7940 hlua->nargs++;
7941 }
7942
7943 /* We must initialize the execution timeouts. */
7944 hlua->max_time = hlua_timeout_session;
7945
7946 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007947 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007948
7949 /* It's ok */
7950 return 0;
7951
7952 /* It's not ok. */
7953error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007954 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007955 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007956 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007957 return 1;
7958}
7959
7960static int hlua_cli_io_handler_fct(struct appctx *appctx)
7961{
7962 struct hlua *hlua;
7963 struct stream_interface *si;
7964 struct hlua_function *fcn;
7965
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007966 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007967 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007968 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007969
7970 /* If the stream is disconnect or closed, ldo nothing. */
7971 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7972 return 1;
7973
7974 /* Execute the function. */
7975 switch (hlua_ctx_resume(hlua, 1)) {
7976
7977 /* finished. */
7978 case HLUA_E_OK:
7979 return 1;
7980
7981 /* yield. */
7982 case HLUA_E_AGAIN:
7983 /* We want write. */
7984 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007985 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007986 /* Set the timeout. */
7987 if (hlua->wake_time != TICK_ETERNITY)
7988 task_schedule(hlua->task, hlua->wake_time);
7989 return 0;
7990
7991 /* finished with error. */
7992 case HLUA_E_ERRMSG:
7993 /* Display log. */
7994 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7995 fcn->name, lua_tostring(hlua->T, -1));
7996 lua_pop(hlua->T, 1);
7997 return 1;
7998
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007999 case HLUA_E_ETMOUT:
8000 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8001 fcn->name);
8002 return 1;
8003
8004 case HLUA_E_NOMEM:
8005 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8006 fcn->name);
8007 return 1;
8008
8009 case HLUA_E_YIELD: /* unexpected */
8010 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8011 fcn->name);
8012 return 1;
8013
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008014 case HLUA_E_ERR:
8015 /* Display log. */
8016 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8017 fcn->name);
8018 return 1;
8019
8020 default:
8021 return 1;
8022 }
8023
8024 return 1;
8025}
8026
8027static void hlua_cli_io_release_fct(struct appctx *appctx)
8028{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008029 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008030 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008031}
8032
8033/* This function is an LUA binding used for registering
8034 * new keywords in the cli. It expects a list of keywords
8035 * which are the "path". It is limited to 5 keywords. A
8036 * description of the command, a function to be executed
8037 * for the parsing and a function for io handlers.
8038 */
8039__LJMP static int hlua_register_cli(lua_State *L)
8040{
8041 struct cli_kw_list *cli_kws;
8042 const char *message;
8043 int ref_io;
8044 int len;
8045 struct hlua_function *fcn;
8046 int index;
8047 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008048 struct buffer *trash;
8049 const char *kw[5];
8050 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008051
8052 MAY_LJMP(check_args(L, 3, "register_cli"));
8053
8054 /* First argument : an array of maximum 5 keywords. */
8055 if (!lua_istable(L, 1))
8056 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8057
8058 /* Second argument : string with contextual message. */
8059 message = MAY_LJMP(luaL_checkstring(L, 2));
8060
8061 /* Third and fourth argument : lua function. */
8062 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8063
Thierry Fournierf67442e2020-11-28 20:41:07 +01008064 /* Check for CLI service already registered */
8065 trash = get_trash_chunk();
8066 index = 0;
8067 lua_pushnil(L);
8068 memset(kw, 0, sizeof(kw));
8069 while (lua_next(L, 1) != 0) {
8070 if (index >= CLI_PREFIX_KW_NB)
8071 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8072 if (lua_type(L, -1) != LUA_TSTRING)
8073 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8074 kw[index] = lua_tostring(L, -1);
8075 if (index == 0)
8076 chunk_printf(trash, "%s", kw[index]);
8077 else
8078 chunk_appendf(trash, " %s", kw[index]);
8079 index++;
8080 lua_pop(L, 1);
8081 }
8082 cli_kw = cli_find_kw_exact((char **)kw);
8083 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008084 fcn = cli_kw->private;
8085 if (fcn->function_ref[hlua_state_id] != -1) {
8086 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8087 "This will become a hard error in version 2.5.\n", trash->area);
8088 }
8089 fcn->function_ref[hlua_state_id] = ref_io;
8090 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008091 }
8092
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008093 /* Allocate and fill the sample fetch keyword struct. */
8094 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8095 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008096 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008097 fcn = new_hlua_function();
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008098 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008099 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008100
8101 /* Fill path. */
8102 index = 0;
8103 lua_pushnil(L);
8104 while(lua_next(L, 1) != 0) {
8105 if (index >= 5)
8106 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8107 if (lua_type(L, -1) != LUA_TSTRING)
8108 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8109 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8110 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008111 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008112 index++;
8113 lua_pop(L, 1);
8114 }
8115
8116 /* Copy help message. */
8117 cli_kws->kw[0].usage = strdup(message);
8118 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008119 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008120
8121 /* Fill fcn io handler. */
8122 len = strlen("<lua.cli>") + 1;
8123 for (i = 0; i < index; i++)
8124 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8125 fcn->name = calloc(1, len);
8126 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008127 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008128 strncat((char *)fcn->name, "<lua.cli", len);
8129 for (i = 0; i < index; i++) {
8130 strncat((char *)fcn->name, ".", len);
8131 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8132 }
8133 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008134 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008135
8136 /* Fill last entries. */
8137 cli_kws->kw[0].private = fcn;
8138 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8139 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8140 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8141
8142 /* Register this new converter */
8143 cli_register_kw(cli_kws);
8144
8145 return 0;
8146}
8147
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008148static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8149 struct proxy *defpx, const char *file, int line,
8150 char **err, unsigned int *timeout)
8151{
8152 const char *error;
8153
8154 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008155 if (error == PARSE_TIME_OVER) {
8156 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8157 args[1], args[0]);
8158 return -1;
8159 }
8160 else if (error == PARSE_TIME_UNDER) {
8161 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8162 args[1], args[0]);
8163 return -1;
8164 }
8165 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008166 memprintf(err, "%s: invalid timeout", args[0]);
8167 return -1;
8168 }
8169 return 0;
8170}
8171
8172static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8173 struct proxy *defpx, const char *file, int line,
8174 char **err)
8175{
8176 return hlua_read_timeout(args, section_type, curpx, defpx,
8177 file, line, err, &hlua_timeout_session);
8178}
8179
8180static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8181 struct proxy *defpx, const char *file, int line,
8182 char **err)
8183{
8184 return hlua_read_timeout(args, section_type, curpx, defpx,
8185 file, line, err, &hlua_timeout_task);
8186}
8187
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008188static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8189 struct proxy *defpx, const char *file, int line,
8190 char **err)
8191{
8192 return hlua_read_timeout(args, section_type, curpx, defpx,
8193 file, line, err, &hlua_timeout_applet);
8194}
8195
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008196static int hlua_forced_yield(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 hlua_nb_instruction = strtoll(args[1], &error, 10);
8203 if (*error != '\0') {
8204 memprintf(err, "%s: invalid number", args[0]);
8205 return -1;
8206 }
8207 return 0;
8208}
8209
Willy Tarreau32f61e22015-03-18 17:54:59 +01008210static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8211 struct proxy *defpx, const char *file, int line,
8212 char **err)
8213{
8214 char *error;
8215
8216 if (*(args[1]) == 0) {
8217 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8218 return -1;
8219 }
8220 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8221 if (*error != '\0') {
8222 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8223 return -1;
8224 }
8225 return 0;
8226}
8227
8228
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008229/* This function is called by the main configuration key "lua-load". It loads and
8230 * execute an lua file during the parsing of the HAProxy configuration file. It is
8231 * the main lua entry point.
8232 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008233 * This function runs with the HAProxy keywords API. It returns -1 if an error
8234 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008235 *
8236 * In some error case, LUA set an error message in top of the stack. This function
8237 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008238 *
8239 * This function can fail with an abort() due to an Lua critical error.
8240 * We are in the configuration parsing process of HAProxy, this abort() is
8241 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008242 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008243static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008244{
8245 int error;
8246
8247 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008248 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008249 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008250 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8251 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008252 return -1;
8253 }
8254
8255 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008256 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008257 switch (error) {
8258 case LUA_OK:
8259 break;
8260 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008261 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8262 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008263 return -1;
8264 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008265 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008266 return -1;
8267 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008268 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8269 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008270 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008271#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008272 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008273 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8274 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008275 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008276#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008277 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008278 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8279 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008280 return -1;
8281 }
8282
8283 return 0;
8284}
8285
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008286static int hlua_load(char **args, int section_type, struct proxy *curpx,
8287 struct proxy *defpx, const char *file, int line,
8288 char **err)
8289{
8290 if (*(args[1]) == 0) {
8291 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8292 return -1;
8293 }
8294
Thierry Fournier59f11be2020-11-29 00:37:41 +01008295 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008296 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008297 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008298 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008299}
8300
Thierry Fournier59f11be2020-11-29 00:37:41 +01008301static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
8302 struct proxy *defpx, const char *file, int line,
8303 char **err)
8304{
8305 int len;
8306
8307 if (*(args[1]) == 0) {
8308 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8309 return -1;
8310 }
8311
8312 if (per_thread_load == NULL) {
8313 /* allocate the first entry large enough to store the final NULL */
8314 per_thread_load = calloc(1, sizeof(*per_thread_load));
8315 if (per_thread_load == NULL) {
8316 memprintf(err, "out of memory error");
8317 return -1;
8318 }
8319 }
8320
8321 /* count used entries */
8322 for (len = 0; per_thread_load[len] != NULL; len++)
8323 ;
8324
8325 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8326 if (per_thread_load == NULL) {
8327 memprintf(err, "out of memory error");
8328 return -1;
8329 }
8330
8331 per_thread_load[len] = strdup(args[1]);
8332 per_thread_load[len + 1] = NULL;
8333
8334 if (per_thread_load[len] == NULL) {
8335 memprintf(err, "out of memory error");
8336 return -1;
8337 }
8338
8339 /* loading for thread 1 only */
8340 hlua_state_id = 1;
8341 ha_set_tid(0);
8342 return hlua_load_state(args[1], hlua_states[1], err);
8343}
8344
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008345/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8346 * in the given <ctx>.
8347 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008348static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008349{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008350 lua_getglobal(L, "package"); /* push package variable */
8351 lua_pushstring(L, path); /* push given path */
8352 lua_pushstring(L, ";"); /* push semicolon */
8353 lua_getfield(L, -3, type); /* push old path */
8354 lua_concat(L, 3); /* concatenate to new path */
8355 lua_setfield(L, -2, type); /* store new path */
8356 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008357
8358 return 0;
8359}
8360
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008361static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
8362 struct proxy *defpx, const char *file, int line,
8363 char **err)
8364{
8365 char *path;
8366 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008367 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008368
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008369 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008370 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008371 }
8372
8373 if (!(*args[1])) {
8374 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008375 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008376 }
8377 path = args[1];
8378
8379 if (*args[2]) {
8380 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8381 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008382 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008383 }
8384 type = args[2];
8385 }
8386
Thierry Fournier59f11be2020-11-29 00:37:41 +01008387 p = calloc(1, sizeof(*p));
8388 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008389 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008390 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008391 }
8392 p->path = strdup(path);
8393 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008394 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008395 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008396 }
8397 p->type = strdup(type);
8398 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008399 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008400 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008401 }
8402 LIST_ADDQ(&prepend_path_list, &p->l);
8403
8404 hlua_prepend_path(hlua_states[0], type, path);
8405 hlua_prepend_path(hlua_states[1], type, path);
8406 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008407
8408err2:
8409 free(p->type);
8410 free(p->path);
8411err:
8412 free(p);
8413 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008414}
8415
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008416/* configuration keywords declaration */
8417static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008418 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008419 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008420 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008421 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8422 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008423 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008424 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008425 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008426 { 0, NULL, NULL },
8427}};
8428
Willy Tarreau0108d902018-11-25 19:14:37 +01008429INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8430
Christopher Fauletafd8f102018-11-08 11:34:21 +01008431
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008432/* This function can fail with an abort() due to an Lua critical error.
8433 * We are in the initialisation process of HAProxy, this abort() is
8434 * tolerated.
8435 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008436int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008437{
8438 struct hlua_init_function *init;
8439 const char *msg;
8440 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008441 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008442 const char *kind;
8443 const char *trace;
8444 int return_status = 1;
8445#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8446 int nres;
8447#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008448
Willy Tarreaucdb53462020-12-02 12:12:00 +01008449 /* disable memory limit checks if limit is not set */
8450 if (!hlua_global_allocator.limit)
8451 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8452
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008453 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008454 if (setjmp(safe_ljmp_env) != 0) {
8455 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008456 if (lua_type(L, -1) == LUA_TSTRING)
8457 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008458 else
8459 error = "critical error";
8460 fprintf(stderr, "Lua post-init: %s.\n", error);
8461 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008462 } else {
8463 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008464 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008465
Thierry Fournierb8cef172020-11-28 15:37:17 +01008466 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008467
Thierry Fournierc7492592020-11-28 23:57:24 +01008468 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008469 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008470
8471#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008472 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008473#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008474 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008475#endif
8476 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008477 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008478
8479 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008480 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008481 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008482
8483 case LUA_ERRERR:
8484 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008485 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008486 case LUA_ERRRUN:
8487 if (!kind)
8488 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008489 msg = lua_tostring(L, -1);
8490 lua_settop(L, 0); /* Empty the stack. */
8491 lua_pop(L, 1);
8492 trace = hlua_traceback(L);
Thierry Fournier670db242020-11-28 10:49:59 +01008493 if (msg)
8494 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8495 else
8496 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8497 return_status = 0;
8498 break;
8499
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008500 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008501 /* Unknown error */
8502 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008503 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008504 case LUA_YIELD:
8505 /* yield is not configured at this step, this state doesn't happen */
8506 if (!kind)
8507 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008508 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008509 case LUA_ERRMEM:
8510 if (!kind)
8511 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008512 lua_settop(L, 0);
8513 lua_pop(L, 1);
8514 trace = hlua_traceback(L);
Thierry Fournier670db242020-11-28 10:49:59 +01008515 ha_alert("Lua init: %s: %s\n", kind, trace);
8516 return_status = 0;
8517 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008518 }
Thierry Fournier670db242020-11-28 10:49:59 +01008519 if (!return_status)
8520 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008521 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008522
8523 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008524 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008525}
8526
Thierry Fournierb8cef172020-11-28 15:37:17 +01008527int hlua_post_init()
8528{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008529 int ret;
8530 int i;
8531 int errors;
8532 char *err = NULL;
8533 struct hlua_function *fcn;
8534
Thierry Fournierb8cef172020-11-28 15:37:17 +01008535#if USE_OPENSSL
8536 /* Initialize SSL server. */
8537 if (socket_ssl.xprt->prepare_srv) {
8538 int saved_used_backed = global.ssl_used_backend;
8539 // don't affect maxconn automatic computation
8540 socket_ssl.xprt->prepare_srv(&socket_ssl);
8541 global.ssl_used_backend = saved_used_backed;
8542 }
8543#endif
8544
Thierry Fournierc7492592020-11-28 23:57:24 +01008545 /* Perform post init of common thread */
8546 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008547 ha_set_tid(0);
8548 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8549 if (ret == 0)
8550 return 0;
8551
8552 /* init remaining lua states and load files */
8553 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8554
8555 /* set thread context */
8556 ha_set_tid(hlua_state_id - 1);
8557
8558 /* Init lua state */
8559 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8560
8561 /* Load lua files */
8562 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8563 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8564 if (ret != 0) {
8565 ha_alert("Lua init: %s\n", err);
8566 return 0;
8567 }
8568 }
8569 }
8570
8571 /* Reset thread context */
8572 ha_set_tid(0);
8573
8574 /* Execute post init for all states */
8575 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8576
8577 /* set thread context */
8578 ha_set_tid(hlua_state_id - 1);
8579
8580 /* run post init */
8581 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8582 if (ret == 0)
8583 return 0;
8584 }
8585
8586 /* Reset thread context */
8587 ha_set_tid(0);
8588
8589 /* control functions registering. Each function must have:
8590 * - only the function_ref[0] set positive and all other to -1
8591 * - only the function_ref[0] set to -1 and all other positive
8592 * This ensure a same reference is not used both in shared
8593 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008594 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008595 * complicated to found for the end user.
8596 */
8597 errors = 0;
8598 list_for_each_entry(fcn, &referenced_functions, l) {
8599 ret = 0;
8600 for (i = 1; i < global.nbthread + 1; i++) {
8601 if (fcn->function_ref[i] == -1)
8602 ret--;
8603 else
8604 ret++;
8605 }
8606 if (abs(ret) != global.nbthread) {
8607 ha_alert("Lua function '%s' is not referenced in all thread. "
8608 "Expect function in all thread or in none thread.\n", fcn->name);
8609 errors++;
8610 continue;
8611 }
8612
8613 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008614 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8615 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008616 "exclusive.\n", fcn->name);
8617 errors++;
8618 }
8619 }
8620
8621 if (errors > 0)
8622 return 0;
8623
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008624 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008625 * -1 in order to have probably a segfault if someone use it
8626 */
8627 hlua_state_id = -1;
8628
8629 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008630}
8631
Willy Tarreau32f61e22015-03-18 17:54:59 +01008632/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8633 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8634 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008635 * allocation. <nsize> is the requested new size. A new allocation is
8636 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008637 * zero. This one verifies that the limits are respected but is optimized
8638 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008639 */
8640static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8641{
8642 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008643 size_t limit, old, new;
8644
Tim Duesterhus22586522021-01-08 10:35:33 +01008645 if (unlikely(!ptr && !nsize))
8646 return NULL;
8647
Willy Tarreaucdb53462020-12-02 12:12:00 +01008648 /* a limit of ~0 means unlimited and boot complete, so there's no need
8649 * for accounting anymore.
8650 */
8651 if (likely(~zone->limit == 0))
8652 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008653
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008654 if (!ptr)
8655 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008656
Willy Tarreaucdb53462020-12-02 12:12:00 +01008657 /* enforce strict limits across all threads */
8658 limit = zone->limit;
8659 old = _HA_ATOMIC_LOAD(&zone->allocated);
8660 do {
8661 new = old + nsize - osize;
8662 if (unlikely(nsize && limit && new > limit))
8663 return NULL;
8664 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008665
8666 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008667
8668 if (unlikely(!ptr && nsize)) // failed
8669 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8670
8671 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008672 return ptr;
8673}
8674
Thierry Fournierecb83c22020-11-28 15:49:44 +01008675/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008676 * We are in the initialisation process of HAProxy, this abort() is
8677 * tolerated.
8678 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008679lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008680{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008681 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008682 int idx;
8683 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008684 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008685 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008686 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008687 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008688 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008689 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008690
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008691 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008692 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008693
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008694 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008695 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008696 *context = NULL;
8697
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008698 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008699 * the Lua function can fail with an abort. We are in the initialisation
8700 * process of HAProxy, this abort() is tolerated.
8701 */
8702
Thierry Fournier3c539322020-11-28 16:05:05 +01008703 /* Call post initialisation function in safe environment. */
8704 if (setjmp(safe_ljmp_env) != 0) {
8705 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008706 if (lua_type(L, -1) == LUA_TSTRING)
8707 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008708 else
8709 error_msg = "critical error";
8710 fprintf(stderr, "Lua init: %s.\n", error_msg);
8711 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008712 } else {
8713 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008714 }
8715
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008716 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008717 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008718#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8719#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8720#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008721 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008722#endif
8723#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008724 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008725#endif
8726#undef HLUA_PREPEND_PATH_TOSTRING
8727#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008728
Thierry Fournier59f11be2020-11-29 00:37:41 +01008729 /* Apply configured prepend path */
8730 list_for_each_entry(pp, &prepend_path_list, l)
8731 hlua_prepend_path(L, pp->type, pp->path);
8732
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008733 /*
8734 *
8735 * Create "core" object.
8736 *
8737 */
8738
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008739 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008740 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008741
Thierry Fournierecb83c22020-11-28 15:49:44 +01008742 /* set the thread id */
8743 hlua_class_const_int(L, "thread", thread_num);
8744
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008745 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008746 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008747 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008748
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008749 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008750 hlua_class_function(L, "register_init", hlua_register_init);
8751 hlua_class_function(L, "register_task", hlua_register_task);
8752 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8753 hlua_class_function(L, "register_converters", hlua_register_converters);
8754 hlua_class_function(L, "register_action", hlua_register_action);
8755 hlua_class_function(L, "register_service", hlua_register_service);
8756 hlua_class_function(L, "register_cli", hlua_register_cli);
8757 hlua_class_function(L, "yield", hlua_yield);
8758 hlua_class_function(L, "set_nice", hlua_set_nice);
8759 hlua_class_function(L, "sleep", hlua_sleep);
8760 hlua_class_function(L, "msleep", hlua_msleep);
8761 hlua_class_function(L, "add_acl", hlua_add_acl);
8762 hlua_class_function(L, "del_acl", hlua_del_acl);
8763 hlua_class_function(L, "set_map", hlua_set_map);
8764 hlua_class_function(L, "del_map", hlua_del_map);
8765 hlua_class_function(L, "tcp", hlua_socket_new);
8766 hlua_class_function(L, "log", hlua_log);
8767 hlua_class_function(L, "Debug", hlua_log_debug);
8768 hlua_class_function(L, "Info", hlua_log_info);
8769 hlua_class_function(L, "Warning", hlua_log_warning);
8770 hlua_class_function(L, "Alert", hlua_log_alert);
8771 hlua_class_function(L, "done", hlua_done);
8772 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008773
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008774 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008775
8776 /*
8777 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008778 * Create "act" object.
8779 *
8780 */
8781
8782 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008783 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008784
8785 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008786 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8787 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8788 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8789 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8790 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8791 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8792 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8793 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008794
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008795 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008796
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008797 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008798
8799 /*
8800 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008801 * Register class Map
8802 *
8803 */
8804
8805 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008806 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008807
8808 /* register pattern types. */
8809 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008810 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008811 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008812 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008813 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008814 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008815
8816 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008817 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008818
8819 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008820 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008821
Ilya Shipitsind4259502020-04-08 01:07:56 +05008822 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008823 lua_pushstring(L, "__index");
8824 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008825
8826 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008827 hlua_class_function(L, "lookup", hlua_map_lookup);
8828 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008829
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008830 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008831
Thierry Fournier45e78d72016-02-19 18:34:46 +01008832 /* Register previous table in the registry with reference and named entry.
8833 * The function hlua_register_metatable() pops the stack, so we
8834 * previously create a copy of the table.
8835 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008836 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8837 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008838
8839 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008840 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008841
8842 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008843 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008844
8845 /*
8846 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008847 * Register class Channel
8848 *
8849 */
8850
8851 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008852 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008853
Ilya Shipitsind4259502020-04-08 01:07:56 +05008854 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008855 lua_pushstring(L, "__index");
8856 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008857
8858 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008859 hlua_class_function(L, "get", hlua_channel_get);
8860 hlua_class_function(L, "dup", hlua_channel_dup);
8861 hlua_class_function(L, "getline", hlua_channel_getline);
8862 hlua_class_function(L, "set", hlua_channel_set);
8863 hlua_class_function(L, "append", hlua_channel_append);
8864 hlua_class_function(L, "send", hlua_channel_send);
8865 hlua_class_function(L, "forward", hlua_channel_forward);
8866 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
8867 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
8868 hlua_class_function(L, "is_full", hlua_channel_is_full);
8869 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008870
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008871 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008872
8873 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008874 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008875
8876 /*
8877 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008878 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008879 *
8880 */
8881
8882 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008883 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008884
Ilya Shipitsind4259502020-04-08 01:07:56 +05008885 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008886 lua_pushstring(L, "__index");
8887 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008888
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008889 /* Browse existing fetches and create the associated
8890 * object method.
8891 */
8892 sf = NULL;
8893 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008894 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8895 * by an underscore.
8896 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008897 strncpy(trash.area, sf->kw, trash.size);
8898 trash.area[trash.size - 1] = '\0';
8899 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008900 if (*p == '.' || *p == '-' || *p == '+')
8901 *p = '_';
8902
8903 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008904 lua_pushstring(L, trash.area);
8905 lua_pushlightuserdata(L, sf);
8906 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
8907 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008908 }
8909
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008910 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008911
8912 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008913 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008914
8915 /*
8916 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008917 * Register class Converters
8918 *
8919 */
8920
8921 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008922 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008923
8924 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008925 lua_pushstring(L, "__index");
8926 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008927
8928 /* Browse existing converters and create the associated
8929 * object method.
8930 */
8931 sc = NULL;
8932 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008933 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8934 * by an underscore.
8935 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008936 strncpy(trash.area, sc->kw, trash.size);
8937 trash.area[trash.size - 1] = '\0';
8938 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008939 if (*p == '.' || *p == '-' || *p == '+')
8940 *p = '_';
8941
8942 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008943 lua_pushstring(L, trash.area);
8944 lua_pushlightuserdata(L, sc);
8945 lua_pushcclosure(L, hlua_run_sample_conv, 1);
8946 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008947 }
8948
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008949 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008950
8951 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008952 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008953
8954 /*
8955 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008956 * Register class HTTP
8957 *
8958 */
8959
8960 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008961 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008962
Ilya Shipitsind4259502020-04-08 01:07:56 +05008963 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008964 lua_pushstring(L, "__index");
8965 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008966
8967 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008968 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
8969 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
8970 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
8971 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
8972 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
8973 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
8974 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
8975 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
8976 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
8977 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008978
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008979 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
8980 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
8981 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
8982 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
8983 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
8984 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
8985 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008986
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008987 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008988
8989 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008990 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008991
8992 /*
8993 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008994 * Register class AppletTCP
8995 *
8996 */
8997
8998 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008999 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009000
Ilya Shipitsind4259502020-04-08 01:07:56 +05009001 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009002 lua_pushstring(L, "__index");
9003 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009004
9005 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009006 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9007 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9008 hlua_class_function(L, "send", hlua_applet_tcp_send);
9009 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9010 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9011 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9012 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9013 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009014
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009015 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009016
9017 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009018 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009019
9020 /*
9021 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009022 * Register class AppletHTTP
9023 *
9024 */
9025
9026 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009027 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009028
Ilya Shipitsind4259502020-04-08 01:07:56 +05009029 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009030 lua_pushstring(L, "__index");
9031 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009032
9033 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009034 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9035 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9036 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9037 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9038 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9039 hlua_class_function(L, "getline", hlua_applet_http_getline);
9040 hlua_class_function(L, "receive", hlua_applet_http_recv);
9041 hlua_class_function(L, "send", hlua_applet_http_send);
9042 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9043 hlua_class_function(L, "set_status", hlua_applet_http_status);
9044 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009045
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009046 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009047
9048 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009049 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009050
9051 /*
9052 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009053 * Register class TXN
9054 *
9055 */
9056
9057 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009058 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009059
Ilya Shipitsind4259502020-04-08 01:07:56 +05009060 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009061 lua_pushstring(L, "__index");
9062 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009063
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009064 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009065 hlua_class_function(L, "set_priv", hlua_set_priv);
9066 hlua_class_function(L, "get_priv", hlua_get_priv);
9067 hlua_class_function(L, "set_var", hlua_set_var);
9068 hlua_class_function(L, "unset_var", hlua_unset_var);
9069 hlua_class_function(L, "get_var", hlua_get_var);
9070 hlua_class_function(L, "done", hlua_txn_done);
9071 hlua_class_function(L, "reply", hlua_txn_reply_new);
9072 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9073 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9074 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9075 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9076 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9077 hlua_class_function(L, "deflog", hlua_txn_deflog);
9078 hlua_class_function(L, "log", hlua_txn_log);
9079 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9080 hlua_class_function(L, "Info", hlua_txn_log_info);
9081 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9082 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009083
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009084 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009085
9086 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009087 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009088
9089 /*
9090 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009091 * Register class reply
9092 *
9093 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009094 lua_newtable(L);
9095 lua_pushstring(L, "__index");
9096 lua_newtable(L);
9097 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9098 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9099 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9100 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9101 lua_settable(L, -3); /* Sets the __index entry. */
9102 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009103
9104
9105 /*
9106 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009107 * Register class Socket
9108 *
9109 */
9110
9111 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009112 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009113
Ilya Shipitsind4259502020-04-08 01:07:56 +05009114 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009115 lua_pushstring(L, "__index");
9116 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009117
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009118#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009119 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009120#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009121 hlua_class_function(L, "connect", hlua_socket_connect);
9122 hlua_class_function(L, "send", hlua_socket_send);
9123 hlua_class_function(L, "receive", hlua_socket_receive);
9124 hlua_class_function(L, "close", hlua_socket_close);
9125 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9126 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9127 hlua_class_function(L, "setoption", hlua_socket_setoption);
9128 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009129
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009130 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009131
9132 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009133 lua_pushstring(L, "__gc");
9134 lua_pushcclosure(L, hlua_socket_gc, 0);
9135 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009136
9137 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009138 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009139
Thierry Fournieraafc7772020-12-04 11:47:47 +01009140 lua_atpanic(L, hlua_panic_safe);
9141
9142 return L;
9143}
9144
9145void hlua_init(void) {
9146 int i;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009147#ifdef USE_OPENSSL
9148 struct srv_kw *kw;
9149 int tmp_error;
9150 char *error;
9151 char *args[] = { /* SSL client configuration. */
9152 "ssl",
9153 "verify",
9154 "none",
9155 NULL
9156 };
9157#endif
9158
9159 /* Init post init function list head */
9160 for (i = 0; i < MAX_THREADS + 1; i++)
9161 LIST_INIT(&hlua_init_functions[i]);
9162
9163 /* Init state for common/shared lua parts */
9164 hlua_state_id = 0;
9165 ha_set_tid(0);
9166 hlua_states[0] = hlua_init_state(0);
9167
9168 /* Init state 1 for thread 0. We have at least one thread. */
9169 hlua_state_id = 1;
9170 ha_set_tid(0);
9171 hlua_states[1] = hlua_init_state(1);
9172
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009173 /* Proxy and server configuration initialisation. */
9174 memset(&socket_proxy, 0, sizeof(socket_proxy));
9175 init_new_proxy(&socket_proxy);
9176 socket_proxy.parent = NULL;
9177 socket_proxy.last_change = now.tv_sec;
9178 socket_proxy.id = "LUA-SOCKET";
9179 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
9180 socket_proxy.maxconn = 0;
9181 socket_proxy.accept = NULL;
9182 socket_proxy.options2 |= PR_O2_INDEPSTR;
9183 socket_proxy.srv = NULL;
9184 socket_proxy.conn_retries = 0;
9185 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
9186
9187 /* Init TCP server: unchanged parameters */
9188 memset(&socket_tcp, 0, sizeof(socket_tcp));
9189 socket_tcp.next = NULL;
9190 socket_tcp.proxy = &socket_proxy;
9191 socket_tcp.obj_type = OBJ_TYPE_SERVER;
9192 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04009193 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02009194 socket_tcp.idle_conns = NULL;
9195 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02009196 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009197 socket_tcp.last_change = 0;
9198 socket_tcp.id = "LUA-TCP-CONN";
9199 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9200 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9201 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
9202
9203 /* XXX: Copy default parameter from default server,
9204 * but the default server is not initialized.
9205 */
9206 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
9207 socket_tcp.minconn = socket_proxy.defsrv.minconn;
9208 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
9209 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
9210 socket_tcp.onerror = socket_proxy.defsrv.onerror;
9211 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
9212 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
9213 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
9214 socket_tcp.uweight = socket_proxy.defsrv.iweight;
9215 socket_tcp.iweight = socket_proxy.defsrv.iweight;
9216
9217 socket_tcp.check.status = HCHK_STATUS_INI;
9218 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
9219 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
9220 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
9221 socket_tcp.check.server = &socket_tcp;
9222
9223 socket_tcp.agent.status = HCHK_STATUS_INI;
9224 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
9225 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
9226 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
9227 socket_tcp.agent.server = &socket_tcp;
9228
Willy Tarreaua261e9b2016-12-22 20:44:00 +01009229 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009230
9231#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009232 /* Init TCP server: unchanged parameters */
9233 memset(&socket_ssl, 0, sizeof(socket_ssl));
9234 socket_ssl.next = NULL;
9235 socket_ssl.proxy = &socket_proxy;
9236 socket_ssl.obj_type = OBJ_TYPE_SERVER;
9237 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04009238 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01009239 socket_ssl.idle_conns = NULL;
9240 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02009241 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009242 socket_ssl.last_change = 0;
9243 socket_ssl.id = "LUA-SSL-CONN";
9244 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9245 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9246 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
9247
9248 /* XXX: Copy default parameter from default server,
9249 * but the default server is not initialized.
9250 */
9251 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
9252 socket_ssl.minconn = socket_proxy.defsrv.minconn;
9253 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
9254 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
9255 socket_ssl.onerror = socket_proxy.defsrv.onerror;
9256 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
9257 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
9258 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
9259 socket_ssl.uweight = socket_proxy.defsrv.iweight;
9260 socket_ssl.iweight = socket_proxy.defsrv.iweight;
9261
9262 socket_ssl.check.status = HCHK_STATUS_INI;
9263 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
9264 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
9265 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
9266 socket_ssl.check.server = &socket_ssl;
9267
9268 socket_ssl.agent.status = HCHK_STATUS_INI;
9269 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
9270 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
9271 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
9272 socket_ssl.agent.server = &socket_ssl;
9273
Thierry FOURNIER36d13742015-03-17 16:48:53 +01009274 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01009275 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009276
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009277 for (i = 0; args[i] != NULL; i++) {
9278 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009279 /*
9280 *
9281 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009282 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009283 * features like client certificates and ssl_verify.
9284 *
9285 */
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009286 tmp_error = kw->parse(args, &i, &socket_proxy, &socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009287 if (tmp_error != 0) {
9288 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9289 abort(); /* This must be never arrives because the command line
9290 not editable by the user. */
9291 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009292 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009293 }
9294 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009295#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009296
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009297}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009298
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009299static void hlua_deinit()
9300{
Willy Tarreau186f3762020-12-04 11:48:12 +01009301 int thr;
9302
9303 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9304 if (hlua_states[thr])
9305 lua_close(hlua_states[thr]);
9306 }
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009307}
9308
9309REGISTER_POST_DEINIT(hlua_deinit);
9310
Willy Tarreau80713382018-11-26 10:19:54 +01009311static void hlua_register_build_options(void)
9312{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009313 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009314
Willy Tarreaubb57d942016-12-21 19:04:56 +01009315 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9316 hap_register_build_opts(ptr, 1);
9317}
Willy Tarreau80713382018-11-26 10:19:54 +01009318
9319INITCALL0(STG_REGISTER, hlua_register_build_options);