blob: c162c84390f37c9b8f9520adafea14eebc1fc4b8 [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 Tarreau198e92a2021-03-05 10:23:32 +010051#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020052#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020053#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020054#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020055#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020056#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020057#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020059#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020060#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020061#include <haproxy/xref.h>
62
Thierry FOURNIER380d0932015-01-23 14:27:52 +010063
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010064/* Lua uses longjmp to perform yield or throwing errors. This
65 * macro is used only for identifying the function that can
66 * not return because a longjmp is executed.
67 * __LJMP marks a prototype of hlua file that can use longjmp.
68 * WILL_LJMP() marks an lua function that will use longjmp.
69 * MAY_LJMP() marks an lua function that may use longjmp.
70 */
71#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020072#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010073#define MAY_LJMP(func) func
74
Thierry FOURNIERbabae282015-09-17 11:36:37 +020075/* This couple of function executes securely some Lua calls outside of
76 * the lua runtime environment. Each Lua call can return a longjmp
77 * if it encounter a memory error.
78 *
79 * Lua documentation extract:
80 *
81 * If an error happens outside any protected environment, Lua calls
82 * a panic function (see lua_atpanic) and then calls abort, thus
83 * exiting the host application. Your panic function can avoid this
84 * exit by never returning (e.g., doing a long jump to your own
85 * recovery point outside Lua).
86 *
87 * The panic function runs as if it were a message handler (see
88 * §2.3); in particular, the error message is at the top of the
89 * stack. However, there is no guarantee about stack space. To push
90 * anything on the stack, the panic function must first check the
91 * available space (see §4.2).
92 *
93 * We must check all the Lua entry point. This includes:
94 * - The include/proto/hlua.h exported functions
95 * - the task wrapper function
96 * - The action wrapper function
97 * - The converters wrapper function
98 * - The sample-fetch wrapper functions
99 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500100 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800101 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200102 *
103 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
104 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
105 * because they must be exists in the program stack when the longjmp
106 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200107 *
108 * Note that the Lua processing is not really thread safe. It provides
109 * heavy system which consists to add our own lock function in the Lua
110 * code and recompile the library. This system will probably not accepted
111 * by maintainers of various distribs.
112 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500113 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200114 * quick looking on the Lua sources displays a lua_lock() a the start
115 * of function and a lua_unlock() at the end of the function. So I
116 * conclude that the Lua thread safe mode just perform a mutex around
117 * all execution. So I prefer to do this in the HAProxy code, it will be
118 * easier for distro maintainers.
119 *
120 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
121 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
122 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100124__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200125THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126static int hlua_panic_safe(lua_State *L) { return 0; }
Willy 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. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +0100204static struct proxy *socket_proxy;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100205static struct server *socket_tcp;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100206#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100207static struct server *socket_ssl;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100208#endif
209
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100210/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100211struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100212
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100213/* The following variables contains the reference of the different
214 * Lua classes. These references are useful for identify metadata
215 * associated with an object.
216 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100217static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100218static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100219static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100220static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100221static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100222static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200223static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200224static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200225static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100226static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100227
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100228/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200229 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100230 * short timeout. Lua linked with tasks doesn't have a timeout
231 * because a task may remain alive during all the haproxy execution.
232 */
233static unsigned int hlua_timeout_session = 4000; /* session timeout. */
234static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200235static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100236
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100237/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
238 * it is used for preventing infinite loops.
239 *
240 * I test the scheer with an infinite loop containing one incrementation
241 * and one test. I run this loop between 10 seconds, I raise a ceil of
242 * 710M loops from one interrupt each 9000 instructions, so I fix the value
243 * to one interrupt each 10 000 instructions.
244 *
245 * configured | Number of
246 * instructions | loops executed
247 * between two | in milions
248 * forced yields |
249 * ---------------+---------------
250 * 10 | 160
251 * 500 | 670
252 * 1000 | 680
253 * 5000 | 700
254 * 7000 | 700
255 * 8000 | 700
256 * 9000 | 710 <- ceil
257 * 10000 | 710
258 * 100000 | 710
259 * 1000000 | 710
260 *
261 */
262static unsigned int hlua_nb_instruction = 10000;
263
Willy Tarreaucdb53462020-12-02 12:12:00 +0100264/* Descriptor for the memory allocation state. The limit is pre-initialised to
265 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
266 * is replaced with ~0 during post_init after everything was loaded. This way
267 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
268 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100269 */
270struct hlua_mem_allocator {
271 size_t allocated;
272 size_t limit;
273};
274
Willy Tarreaucdb53462020-12-02 12:12:00 +0100275static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100276
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100277/* These functions converts types between HAProxy internal args or
278 * sample and LUA types. Another function permits to check if the
279 * LUA stack contains arguments according with an required ARG_T
280 * format.
281 */
282static int hlua_arg2lua(lua_State *L, const struct arg *arg);
283static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100284__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100285 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100286static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100287static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100288static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
289
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100290__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200291
Thierry Fournier59f11be2020-11-29 00:37:41 +0100292struct prepend_path {
293 struct list l;
294 char *type;
295 char *path;
296};
297
298static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
299
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200300#define SEND_ERR(__be, __fmt, __args...) \
301 do { \
302 send_log(__be, LOG_ERR, __fmt, ## __args); \
303 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100304 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200305 } while (0)
306
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100307static inline struct hlua_function *new_hlua_function()
308{
309 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100310 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100311
312 fcn = calloc(1, sizeof(*fcn));
313 if (!fcn)
314 return NULL;
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
Christopher Fauletd09cc512021-03-24 14:48:45 +0100353__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200354{
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
359 while (lua_getstack(L, level++, &ar)) {
360
361 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100362 if (b_data(msg))
363 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200364
365 /* Fill fields:
366 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
367 * 'l': fills in the field currentline;
368 * 'n': fills in the field name and namewhat;
369 * 't': fills in the field istailcall;
370 */
371 lua_getinfo(L, "Slnt", &ar);
372
373 /* Append code localisation */
374 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100375 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200376 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100377 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200378
379 /*
380 * Get function name
381 *
382 * if namewhat is no empty, name is defined.
383 * what contains "Lua" for Lua function, "C" for C function,
384 * or "main" for main code.
385 */
386 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100387 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200388
389 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100390 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200391
392 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100393 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200394
395 else /* nothing left... */
396 chunk_appendf(msg, "?");
397
398
399 /* Display tailed call */
400 if (ar.istailcall)
401 chunk_appendf(msg, " ...");
402 }
403
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200404 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200405}
406
407
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100408/* This function check the number of arguments available in the
409 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500410 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100411 */
412__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
413{
414 if (lua_gettop(L) == nb)
415 return;
416 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
417}
418
Mark Lakes22154b42018-01-29 14:38:40 -0800419/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100420 * and the line number where the error is encountered.
421 */
422static int hlua_pusherror(lua_State *L, const char *fmt, ...)
423{
424 va_list argp;
425 va_start(argp, fmt);
426 luaL_where(L, 1);
427 lua_pushvfstring(L, fmt, argp);
428 va_end(argp);
429 lua_concat(L, 2);
430 return 1;
431}
432
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433/* This functions is used with sample fetch and converters. It
434 * converts the HAProxy configuration argument in a lua stack
435 * values.
436 *
437 * It takes an array of "arg", and each entry of the array is
438 * converted and pushed in the LUA stack.
439 */
440static int hlua_arg2lua(lua_State *L, const struct arg *arg)
441{
442 switch (arg->type) {
443 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100444 case ARGT_TIME:
445 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100446 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 break;
448
449 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200450 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100451 break;
452
453 case ARGT_IPV4:
454 case ARGT_IPV6:
455 case ARGT_MSK4:
456 case ARGT_MSK6:
457 case ARGT_FE:
458 case ARGT_BE:
459 case ARGT_TAB:
460 case ARGT_SRV:
461 case ARGT_USR:
462 case ARGT_MAP:
463 default:
464 lua_pushnil(L);
465 break;
466 }
467 return 1;
468}
469
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500470/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100471 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500472 * with sample fetch wrappers. The input arguments are given to the
473 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100474 */
475static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
476{
477 switch (lua_type(L, ud)) {
478
479 case LUA_TNUMBER:
480 case LUA_TBOOLEAN:
481 arg->type = ARGT_SINT;
482 arg->data.sint = lua_tointeger(L, ud);
483 break;
484
485 case LUA_TSTRING:
486 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200487 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200488 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200489 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200490 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100491 break;
492
493 case LUA_TUSERDATA:
494 case LUA_TNIL:
495 case LUA_TTABLE:
496 case LUA_TFUNCTION:
497 case LUA_TTHREAD:
498 case LUA_TLIGHTUSERDATA:
499 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200500 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100501 break;
502 }
503 return 1;
504}
505
506/* the following functions are used to convert a struct sample
507 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500508 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100509 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100510static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100511{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200512 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100513 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100514 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200515 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100516 break;
517
518 case SMP_T_BIN:
519 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200520 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100521 break;
522
523 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200524 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100525 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
526 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
527 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
528 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
529 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
530 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
531 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
532 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
533 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200534 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100535 break;
536 default:
537 lua_pushnil(L);
538 break;
539 }
540 break;
541
542 case SMP_T_IPV4:
543 case SMP_T_IPV6:
544 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200545 if (sample_casts[smp->data.type][SMP_T_STR] &&
546 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200547 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100548 else
549 lua_pushnil(L);
550 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 default:
552 lua_pushnil(L);
553 break;
554 }
555 return 1;
556}
557
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100558/* the following functions are used to convert a struct sample
559 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500560 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100561 */
562static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
563{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100565
566 case SMP_T_BIN:
567 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200568 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100569 break;
570
571 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200572 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100573 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
574 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
575 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
576 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
577 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
578 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
579 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
580 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
581 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100583 break;
584 default:
585 lua_pushstring(L, "");
586 break;
587 }
588 break;
589
590 case SMP_T_SINT:
591 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100592 case SMP_T_IPV4:
593 case SMP_T_IPV6:
594 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200595 if (sample_casts[smp->data.type][SMP_T_STR] &&
596 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200597 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100598 else
599 lua_pushstring(L, "");
600 break;
601 default:
602 lua_pushstring(L, "");
603 break;
604 }
605 return 1;
606}
607
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100608/* the following functions are used to convert an Lua type in a
609 * struct sample. This is useful to provide data from a converter
610 * to the LUA code.
611 */
612static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
613{
614 switch (lua_type(L, ud)) {
615
616 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200617 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200618 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100619 break;
620
621
622 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200623 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200624 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100625 break;
626
627 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200628 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100629 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200630 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200631 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200632 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200633 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100634 break;
635
636 case LUA_TUSERDATA:
637 case LUA_TNIL:
638 case LUA_TTABLE:
639 case LUA_TFUNCTION:
640 case LUA_TTHREAD:
641 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200642 case LUA_TNONE:
643 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200644 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200645 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100646 break;
647 }
648 return 1;
649}
650
Ilya Shipitsind4259502020-04-08 01:07:56 +0500651/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800652 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100654 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500655 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100656 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100657 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100658__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100659 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100660{
661 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200662 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100663 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200664 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200665 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200666 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200667 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100668
669 idx = 0;
670 min_arg = ARGM(mask);
671 mask >>= ARGM_BITS;
672
673 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200674 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100675
676 /* Check oversize. */
677 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200678 msg = "Malformed argument mask";
679 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100680 }
681
682 /* Check for mandatory arguments. */
683 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100684 if (idx < min_arg) {
685
686 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200687 if (idx > 0) {
688 msg = "Mandatory argument expected";
689 goto error;
690 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100691
692 /* If first argument have a certain type, some default values
693 * may be used. See the function smp_resolve_args().
694 */
695 switch (mask & ARGT_MASK) {
696
697 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200698 if (!(p->cap & PR_CAP_FE)) {
699 msg = "Mandatory argument expected";
700 goto error;
701 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100702 argp[idx].data.prx = p;
703 argp[idx].type = ARGT_FE;
704 argp[idx+1].type = ARGT_STOP;
705 break;
706
707 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200708 if (!(p->cap & PR_CAP_BE)) {
709 msg = "Mandatory argument expected";
710 goto error;
711 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100712 argp[idx].data.prx = p;
713 argp[idx].type = ARGT_BE;
714 argp[idx+1].type = ARGT_STOP;
715 break;
716
717 case ARGT_TAB:
718 argp[idx].data.prx = p;
719 argp[idx].type = ARGT_TAB;
720 argp[idx+1].type = ARGT_STOP;
721 break;
722
723 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200724 msg = "Mandatory argument expected";
725 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100726 break;
727 }
728 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200729 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100730 }
731
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500732 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100733 if ((mask & ARGT_MASK) == ARGT_STOP &&
734 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200735 msg = "Last argument expected";
736 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100737 }
738
739 if ((mask & ARGT_MASK) == ARGT_STOP &&
740 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200741 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100742 }
743
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200744 /* Convert some argument types. All string in argp[] are for not
745 * duplicated yet.
746 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100747 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100748 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200749 if (argp[idx].type != ARGT_SINT) {
750 msg = "integer expected";
751 goto error;
752 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100753 argp[idx].type = ARGT_SINT;
754 break;
755
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100756 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200757 if (argp[idx].type != ARGT_SINT) {
758 msg = "integer expected";
759 goto error;
760 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200761 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100762 break;
763
764 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200765 if (argp[idx].type != ARGT_SINT) {
766 msg = "integer expected";
767 goto error;
768 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200769 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100770 break;
771
772 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200773 if (argp[idx].type != ARGT_STR) {
774 msg = "string expected";
775 goto error;
776 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200777 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200778 if (!argp[idx].data.prx) {
779 msg = "frontend doesn't exist";
780 goto error;
781 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100782 argp[idx].type = ARGT_FE;
783 break;
784
785 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200786 if (argp[idx].type != ARGT_STR) {
787 msg = "string expected";
788 goto error;
789 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200790 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200791 if (!argp[idx].data.prx) {
792 msg = "backend doesn't exist";
793 goto error;
794 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100795 argp[idx].type = ARGT_BE;
796 break;
797
798 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200799 if (argp[idx].type != ARGT_STR) {
800 msg = "string expected";
801 goto error;
802 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200803 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200804 if (!argp[idx].data.t) {
805 msg = "table doesn't exist";
806 goto error;
807 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100808 argp[idx].type = ARGT_TAB;
809 break;
810
811 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200812 if (argp[idx].type != ARGT_STR) {
813 msg = "string expected";
814 goto error;
815 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200816 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100817 if (sname) {
818 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200819 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200820 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200821 if (!px) {
822 msg = "backend doesn't exist";
823 goto error;
824 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100825 }
826 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200827 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100828 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100829 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100830 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200831 if (!argp[idx].data.srv) {
832 msg = "server doesn't exist";
833 goto error;
834 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100835 argp[idx].type = ARGT_SRV;
836 break;
837
838 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200839 if (argp[idx].type != ARGT_STR) {
840 msg = "string expected";
841 goto error;
842 }
843 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
844 msg = "invalid IPv4 address";
845 goto error;
846 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100847 argp[idx].type = ARGT_IPV4;
848 break;
849
850 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200851 if (argp[idx].type == ARGT_SINT)
852 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
853 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200854 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
855 msg = "invalid IPv4 mask";
856 goto error;
857 }
858 }
859 else {
860 msg = "integer or string expected";
861 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200862 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100863 argp[idx].type = ARGT_MSK4;
864 break;
865
866 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200867 if (argp[idx].type != ARGT_STR) {
868 msg = "string expected";
869 goto error;
870 }
871 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
872 msg = "invalid IPv6 address";
873 goto error;
874 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100875 argp[idx].type = ARGT_IPV6;
876 break;
877
878 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200879 if (argp[idx].type == ARGT_SINT)
880 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
881 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200882 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
883 msg = "invalid IPv6 mask";
884 goto error;
885 }
886 }
887 else {
888 msg = "integer or string expected";
889 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200890 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100891 argp[idx].type = ARGT_MSK6;
892 break;
893
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200894 case ARGT_REG:
895 if (argp[idx].type != ARGT_STR) {
896 msg = "string expected";
897 goto error;
898 }
899 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
900 if (!reg) {
901 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
902 argp[idx].data.str.area, err);
903 free(err);
904 goto error;
905 }
906 argp[idx].type = ARGT_REG;
907 argp[idx].data.reg = reg;
908 break;
909
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100910 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200911 if (argp[idx].type != ARGT_STR) {
912 msg = "string expected";
913 goto error;
914 }
915 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100916 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200917 ul = p->uri_auth->userlist;
918 else
919 ul = auth_find_userlist(argp[idx].data.str.area);
920
921 if (!ul) {
922 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
923 goto error;
924 }
925 argp[idx].type = ARGT_USR;
926 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200927 break;
928
929 case ARGT_STR:
930 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
931 msg = "unable to duplicate string arg";
932 goto error;
933 }
934 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100935 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200936
Christopher Fauletd25d9262020-08-06 11:04:46 +0200937 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200938 msg = "type not yet supported";
939 goto error;
940 break;
941
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100942 }
943
944 /* Check for type of argument. */
945 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200946 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
947 arg_type_names[(mask & ARGT_MASK)],
948 arg_type_names[argp[idx].type & ARGT_MASK]);
949 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100950 }
951
952 /* Next argument. */
953 mask >>= ARGT_BITS;
954 idx++;
955 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200956 return 0;
957
958 error:
959 for (i = 0; i < idx; i++) {
960 if (argp[i].type == ARGT_STR)
961 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200962 else if (argp[i].type == ARGT_REG)
963 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200964 }
965 WILL_LJMP(luaL_argerror(L, first + idx, msg));
966 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100967}
968
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100969/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500970 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100971 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100972 *
973 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100974 * - hlua_sethlua : create the association between hlua context and lua_state.
975 */
976static inline struct hlua *hlua_gethlua(lua_State *L)
977{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100978 struct hlua **hlua = lua_getextraspace(L);
979 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100980}
981static inline void hlua_sethlua(struct hlua *hlua)
982{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100983 struct hlua **hlua_store = lua_getextraspace(hlua->T);
984 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100985}
986
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100987/* This function is used to send logs. It try to send on screen (stderr)
988 * and on the default syslog server.
989 */
990static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
991{
992 struct tm tm;
993 char *p;
994
995 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200996 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100997 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200998 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200999 /* Break the message if exceed the buffer size. */
1000 *(p-4) = ' ';
1001 *(p-3) = '.';
1002 *(p-2) = '.';
1003 *(p-1) = '.';
1004 break;
1005 }
Willy Tarreau90807112020-02-25 08:16:33 +01001006 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001007 *p = *msg;
1008 else
1009 *p = '.';
1010 }
1011 *p = '\0';
1012
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001013 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001014 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001015 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1016 return;
1017
Willy Tarreaua678b432015-08-28 10:14:59 +02001018 get_localtime(date.tv_sec, &tm);
1019 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001020 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001021 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001022 fflush(stderr);
1023 }
1024}
1025
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001026/* This function just ensure that the yield will be always
1027 * returned with a timeout and permit to set some flags
1028 */
1029__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001030 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001031{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001032 struct hlua *hlua;
1033
1034 /* Get hlua struct, or NULL if we execute from main lua state */
1035 hlua = hlua_gethlua(L);
1036 if (!hlua) {
1037 return;
1038 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001039
1040 /* Set the wake timeout. If timeout is required, we set
1041 * the expiration time.
1042 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001043 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001044
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001045 hlua->flags |= flags;
1046
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001047 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001048 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001049}
1050
Willy Tarreau87b09662015-04-03 00:22:06 +02001051/* This function initialises the Lua environment stored in the stream.
1052 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001053 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001054 *
1055 * This function is particular. it initialises a new Lua thread. If the
1056 * initialisation fails (example: out of memory error), the lua function
1057 * throws an error (longjmp).
1058 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001059 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001060 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001061 * threads appear, the safe environment set a lock to ensure only one
1062 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001063 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001064 *
1065 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001066 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001067 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001068 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001069 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001070 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001071 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001072 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001073int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001074{
1075 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001076 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001077 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001078 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001079 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001080 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001081 if (!already_safe) {
1082 if (!SET_SAFE_LJMP_PARENT(lua)) {
1083 lua->Tref = LUA_REFNIL;
1084 return 0;
1085 }
1086 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001087 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001088 if (!lua->T) {
1089 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001090 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001091 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001092 return 0;
1093 }
1094 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001095 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001096 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001097 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001098 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001099 return 1;
1100}
1101
Willy Tarreau87b09662015-04-03 00:22:06 +02001102/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001103 * is destroyed. The destroy also the memory context. The struct "lua"
1104 * is not freed.
1105 */
1106void hlua_ctx_destroy(struct hlua *lua)
1107{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001108 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001109 return;
1110
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001111 if (!lua->T)
1112 goto end;
1113
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001114 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001115 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001116
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001117 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001118 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001119 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001120 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001121
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001122 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001123 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001124 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001125 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001126 /* Forces a garbage collecting process. If the Lua program is finished
1127 * without error, we run the GC on the thread pointer. Its freed all
1128 * the unused memory.
1129 * If the thread is finnish with an error or is currently yielded,
1130 * it seems that the GC applied on the thread doesn't clean anything,
1131 * so e run the GC on the main thread.
1132 * NOTE: maybe this action locks all the Lua threads untiml the en of
1133 * the garbage collection.
1134 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001135 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001136 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001137 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001138 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001139 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001140 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001141
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001142 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001143
1144end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001145 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001146}
1147
1148/* This function is used to restore the Lua context when a coroutine
1149 * fails. This function copy the common memory between old coroutine
1150 * and the new coroutine. The old coroutine is destroyed, and its
1151 * replaced by the new coroutine.
1152 * If the flag "keep_msg" is set, the last entry of the old is assumed
1153 * as string error message and it is copied in the new stack.
1154 */
1155static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1156{
1157 lua_State *T;
1158 int new_ref;
1159
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001161 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 if (!T)
1163 return 0;
1164
1165 /* Copy last error message. */
1166 if (keep_msg)
1167 lua_xmove(lua->T, T, 1);
1168
1169 /* Copy data between the coroutines. */
1170 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1171 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001172 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173
1174 /* Destroy old data. */
1175 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1176
1177 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001178 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001179
1180 /* Fill the struct with the new coroutine values. */
1181 lua->Mref = new_ref;
1182 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001183 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001184
1185 /* Set context. */
1186 hlua_sethlua(lua);
1187
1188 return 1;
1189}
1190
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001191void hlua_hook(lua_State *L, lua_Debug *ar)
1192{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001193 struct hlua *hlua;
1194
1195 /* Get hlua struct, or NULL if we execute from main lua state */
1196 hlua = hlua_gethlua(L);
1197 if (!hlua)
1198 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001199
1200 /* Lua cannot yield when its returning from a function,
1201 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001202 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001203 */
1204 if (lua_gethookmask(L) & LUA_MASKRET) {
1205 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1206 return;
1207 }
1208
1209 /* restore the interrupt condition. */
1210 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1211
1212 /* If we interrupt the Lua processing in yieldable state, we yield.
1213 * If the state is not yieldable, trying yield causes an error.
1214 */
1215 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001216 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001217
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001218 /* If we cannot yield, update the clock and check the timeout. */
1219 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001220 hlua->run_time += now_ms - hlua->start_time;
1221 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001222 lua_pushfstring(L, "execution timeout");
1223 WILL_LJMP(lua_error(L));
1224 }
1225
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001226 /* Update the start time. */
1227 hlua->start_time = now_ms;
1228
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001229 /* Try to interrupt the process at the end of the current
1230 * unyieldable function.
1231 */
1232 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001233}
1234
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001235/* This function start or resumes the Lua stack execution. If the flag
1236 * "yield_allowed" if no set and the LUA stack execution returns a yield
1237 * The function return an error.
1238 *
1239 * The function can returns 4 values:
1240 * - HLUA_E_OK : The execution is terminated without any errors.
1241 * - HLUA_E_AGAIN : The execution must continue at the next associated
1242 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001243 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001244 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001245 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001246 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001247 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001248 * LUA code.
1249 */
1250static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1251{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001252#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1253 int nres;
1254#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001255 int ret;
1256 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001257 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001258
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001259 /* Initialise run time counter. */
1260 if (!HLUA_IS_RUNNING(lua))
1261 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001262
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001263 /* Lock the whole Lua execution. This lock must be before the
1264 * label "resume_execution".
1265 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001266 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001267 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001268
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001269resume_execution:
1270
1271 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1272 * instructions. it is used for preventing infinite loops.
1273 */
1274 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1275
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001276 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001277 HLUA_SET_RUN(lua);
1278 HLUA_CLR_CTRLYIELD(lua);
1279 HLUA_CLR_WAKERESWR(lua);
1280 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001281
Christopher Fauletbc275a92020-02-26 14:55:16 +01001282 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001283 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001284 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001285
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001286 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001287#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001288 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001289#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001290 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001291#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001292 switch (ret) {
1293
1294 case LUA_OK:
1295 ret = HLUA_E_OK;
1296 break;
1297
1298 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001299 /* Check if the execution timeout is expired. It it is the case, we
1300 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001301 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001302 tv_update_date(0, 1);
1303 lua->run_time += now_ms - lua->start_time;
1304 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001305 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001306 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001307 break;
1308 }
1309 /* Process the forced yield. if the general yield is not allowed or
1310 * if no task were associated this the current Lua execution
1311 * coroutine, we resume the execution. Else we want to return in the
1312 * scheduler and we want to be waked up again, to continue the
1313 * current Lua execution. So we schedule our own task.
1314 */
1315 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001316 if (!yield_allowed || !lua->task)
1317 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001318 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001319 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001320 if (!yield_allowed) {
1321 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001322 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001323 break;
1324 }
1325 ret = HLUA_E_AGAIN;
1326 break;
1327
1328 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001329
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001330 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001331 * because the errors ares the only one mean to return immediately
1332 * from and lua execution.
1333 */
1334 if (lua->flags & HLUA_EXIT) {
1335 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001336 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001337 break;
1338 }
1339
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001340 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001341 if (!lua_checkstack(lua->T, 1)) {
1342 ret = HLUA_E_ERR;
1343 break;
1344 }
1345 msg = lua_tostring(lua->T, -1);
1346 lua_settop(lua->T, 0); /* Empty the stack. */
1347 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001348 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001349 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001350 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001351 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001352 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001353 ret = HLUA_E_ERRMSG;
1354 break;
1355
1356 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001357 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001358 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001359 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001360 break;
1361
1362 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001363 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001364 if (!lua_checkstack(lua->T, 1)) {
1365 ret = HLUA_E_ERR;
1366 break;
1367 }
1368 msg = lua_tostring(lua->T, -1);
1369 lua_settop(lua->T, 0); /* Empty the stack. */
1370 lua_pop(lua->T, 1);
1371 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001372 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001373 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001374 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001375 ret = HLUA_E_ERRMSG;
1376 break;
1377
1378 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001379 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001380 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001381 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001382 break;
1383 }
1384
1385 switch (ret) {
1386 case HLUA_E_AGAIN:
1387 break;
1388
1389 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001390 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001391 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001392 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001393 break;
1394
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001395 case HLUA_E_ETMOUT:
1396 case HLUA_E_NOMEM:
1397 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001398 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001399 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001400 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001401 hlua_ctx_renew(lua, 0);
1402 break;
1403
1404 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001405 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001406 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001407 break;
1408 }
1409
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001410 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001411 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001412 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001413
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001414 return ret;
1415}
1416
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001417/* This function exit the current code. */
1418__LJMP static int hlua_done(lua_State *L)
1419{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001420 struct hlua *hlua;
1421
1422 /* Get hlua struct, or NULL if we execute from main lua state */
1423 hlua = hlua_gethlua(L);
1424 if (!hlua)
1425 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001426
1427 hlua->flags |= HLUA_EXIT;
1428 WILL_LJMP(lua_error(L));
1429
1430 return 0;
1431}
1432
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001433/* This function is an LUA binding. It provides a function
1434 * for deleting ACL from a referenced ACL file.
1435 */
1436__LJMP static int hlua_del_acl(lua_State *L)
1437{
1438 const char *name;
1439 const char *key;
1440 struct pat_ref *ref;
1441
1442 MAY_LJMP(check_args(L, 2, "del_acl"));
1443
1444 name = MAY_LJMP(luaL_checkstring(L, 1));
1445 key = MAY_LJMP(luaL_checkstring(L, 2));
1446
1447 ref = pat_ref_lookup(name);
1448 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001449 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001450
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001451 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001452 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001453 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001454 return 0;
1455}
1456
1457/* This function is an LUA binding. It provides a function
1458 * for deleting map entry from a referenced map file.
1459 */
1460static int hlua_del_map(lua_State *L)
1461{
1462 const char *name;
1463 const char *key;
1464 struct pat_ref *ref;
1465
1466 MAY_LJMP(check_args(L, 2, "del_map"));
1467
1468 name = MAY_LJMP(luaL_checkstring(L, 1));
1469 key = MAY_LJMP(luaL_checkstring(L, 2));
1470
1471 ref = pat_ref_lookup(name);
1472 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001473 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001474
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001475 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001476 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001477 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001478 return 0;
1479}
1480
1481/* This function is an LUA binding. It provides a function
1482 * for adding ACL pattern from a referenced ACL file.
1483 */
1484static int hlua_add_acl(lua_State *L)
1485{
1486 const char *name;
1487 const char *key;
1488 struct pat_ref *ref;
1489
1490 MAY_LJMP(check_args(L, 2, "add_acl"));
1491
1492 name = MAY_LJMP(luaL_checkstring(L, 1));
1493 key = MAY_LJMP(luaL_checkstring(L, 2));
1494
1495 ref = pat_ref_lookup(name);
1496 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001497 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001498
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001499 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001500 if (pat_ref_find_elt(ref, key) == NULL)
1501 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001502 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001503 return 0;
1504}
1505
1506/* This function is an LUA binding. It provides a function
1507 * for setting map pattern and sample from a referenced map
1508 * file.
1509 */
1510static int hlua_set_map(lua_State *L)
1511{
1512 const char *name;
1513 const char *key;
1514 const char *value;
1515 struct pat_ref *ref;
1516
1517 MAY_LJMP(check_args(L, 3, "set_map"));
1518
1519 name = MAY_LJMP(luaL_checkstring(L, 1));
1520 key = MAY_LJMP(luaL_checkstring(L, 2));
1521 value = MAY_LJMP(luaL_checkstring(L, 3));
1522
1523 ref = pat_ref_lookup(name);
1524 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001525 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001526
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001527 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001528 if (pat_ref_find_elt(ref, key) != NULL)
1529 pat_ref_set(ref, key, value, NULL);
1530 else
1531 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001532 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001533 return 0;
1534}
1535
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001536/* A class is a lot of memory that contain data. This data can be a table,
1537 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001538 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001539 * the name of the object (_G[<name>] = <metable> ).
1540 *
1541 * A metable is a table that modify the standard behavior of a standard
1542 * access to the associated data. The entries of this new metatable are
1543 * defined as is:
1544 *
1545 * http://lua-users.org/wiki/MetatableEvents
1546 *
1547 * __index
1548 *
1549 * we access an absent field in a table, the result is nil. This is
1550 * true, but it is not the whole truth. Actually, such access triggers
1551 * the interpreter to look for an __index metamethod: If there is no
1552 * such method, as usually happens, then the access results in nil;
1553 * otherwise, the metamethod will provide the result.
1554 *
1555 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1556 * the key does not appear in the table, but the metatable has an __index
1557 * property:
1558 *
1559 * - if the value is a function, the function is called, passing in the
1560 * table and the key; the return value of that function is returned as
1561 * the result.
1562 *
1563 * - if the value is another table, the value of the key in that table is
1564 * asked for and returned (and if it doesn't exist in that table, but that
1565 * table's metatable has an __index property, then it continues on up)
1566 *
1567 * - Use "rawget(myTable,key)" to skip this metamethod.
1568 *
1569 * http://www.lua.org/pil/13.4.1.html
1570 *
1571 * __newindex
1572 *
1573 * Like __index, but control property assignment.
1574 *
1575 * __mode - Control weak references. A string value with one or both
1576 * of the characters 'k' and 'v' which specifies that the the
1577 * keys and/or values in the table are weak references.
1578 *
1579 * __call - Treat a table like a function. When a table is followed by
1580 * parenthesis such as "myTable( 'foo' )" and the metatable has
1581 * a __call key pointing to a function, that function is invoked
1582 * (passing any specified arguments) and the return value is
1583 * returned.
1584 *
1585 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1586 * called, if the metatable for myTable has a __metatable
1587 * key, the value of that key is returned instead of the
1588 * actual metatable.
1589 *
1590 * __tostring - Control string representation. When the builtin
1591 * "tostring( myTable )" function is called, if the metatable
1592 * for myTable has a __tostring property set to a function,
1593 * that function is invoked (passing myTable to it) and the
1594 * return value is used as the string representation.
1595 *
1596 * __len - Control table length. When the table length is requested using
1597 * the length operator ( '#' ), if the metatable for myTable has
1598 * a __len key pointing to a function, that function is invoked
1599 * (passing myTable to it) and the return value used as the value
1600 * of "#myTable".
1601 *
1602 * __gc - Userdata finalizer code. When userdata is set to be garbage
1603 * collected, if the metatable has a __gc field pointing to a
1604 * function, that function is first invoked, passing the userdata
1605 * to it. The __gc metamethod is not called for tables.
1606 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1607 *
1608 * Special metamethods for redefining standard operators:
1609 * http://www.lua.org/pil/13.1.html
1610 *
1611 * __add "+"
1612 * __sub "-"
1613 * __mul "*"
1614 * __div "/"
1615 * __unm "!"
1616 * __pow "^"
1617 * __concat ".."
1618 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001619 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001620 * http://www.lua.org/pil/13.2.html
1621 *
1622 * __eq "=="
1623 * __lt "<"
1624 * __le "<="
1625 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626
1627/*
1628 *
1629 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001630 * Class Map
1631 *
1632 *
1633 */
1634
1635/* Returns a struct hlua_map if the stack entry "ud" is
1636 * a class session, otherwise it throws an error.
1637 */
1638__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1639{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001640 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001641}
1642
1643/* This function is the map constructor. It don't need
1644 * the class Map object. It creates and return a new Map
1645 * object. It must be called only during "body" or "init"
1646 * context because it process some filesystem accesses.
1647 */
1648__LJMP static int hlua_map_new(struct lua_State *L)
1649{
1650 const char *fn;
1651 int match = PAT_MATCH_STR;
1652 struct sample_conv conv;
1653 const char *file = "";
1654 int line = 0;
1655 lua_Debug ar;
1656 char *err = NULL;
1657 struct arg args[2];
1658
1659 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1660 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1661
1662 fn = MAY_LJMP(luaL_checkstring(L, 1));
1663
1664 if (lua_gettop(L) >= 2) {
1665 match = MAY_LJMP(luaL_checkinteger(L, 2));
1666 if (match < 0 || match >= PAT_MATCH_NUM)
1667 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1668 }
1669
1670 /* Get Lua filename and line number. */
1671 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1672 lua_getinfo(L, "Sl", &ar); /* get info about it */
1673 if (ar.currentline > 0) { /* is there info? */
1674 file = ar.short_src;
1675 line = ar.currentline;
1676 }
1677 }
1678
1679 /* fill fake sample_conv struct. */
1680 conv.kw = ""; /* unused. */
1681 conv.process = NULL; /* unused. */
1682 conv.arg_mask = 0; /* unused. */
1683 conv.val_args = NULL; /* unused. */
1684 conv.out_type = SMP_T_STR;
1685 conv.private = (void *)(long)match;
1686 switch (match) {
1687 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1688 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1689 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1690 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1691 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1692 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1693 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001694 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001695 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1696 default:
1697 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1698 }
1699
1700 /* fill fake args. */
1701 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001702 args[0].data.str.area = strdup(fn);
1703 args[0].data.str.data = strlen(fn);
1704 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001705 args[1].type = ARGT_STOP;
1706
1707 /* load the map. */
1708 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001709 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001710 * free the err variable.
1711 */
1712 luaL_where(L, 1);
1713 lua_pushfstring(L, "'new': %s.", err);
1714 lua_concat(L, 2);
1715 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001716 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001717 WILL_LJMP(lua_error(L));
1718 }
1719
1720 /* create the lua object. */
1721 lua_newtable(L);
1722 lua_pushlightuserdata(L, args[0].data.map);
1723 lua_rawseti(L, -2, 0);
1724
1725 /* Pop a class Map metatable and affect it to the userdata. */
1726 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1727 lua_setmetatable(L, -2);
1728
1729
1730 return 1;
1731}
1732
1733__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1734{
1735 struct map_descriptor *desc;
1736 struct pattern *pat;
1737 struct sample smp;
1738
1739 MAY_LJMP(check_args(L, 2, "lookup"));
1740 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001741 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001742 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001743 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001744 }
1745 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001746 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001747 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001748 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 +01001749 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001750 }
1751
1752 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001753 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001754 if (str)
1755 lua_pushstring(L, "");
1756 else
1757 lua_pushnil(L);
1758 return 1;
1759 }
1760
1761 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001762 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001763 return 1;
1764}
1765
1766__LJMP static int hlua_map_lookup(struct lua_State *L)
1767{
1768 return _hlua_map_lookup(L, 0);
1769}
1770
1771__LJMP static int hlua_map_slookup(struct lua_State *L)
1772{
1773 return _hlua_map_lookup(L, 1);
1774}
1775
1776/*
1777 *
1778 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001779 * Class Socket
1780 *
1781 *
1782 */
1783
1784__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1785{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001786 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787}
1788
1789/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001790 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 * received.
1792 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001793static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001795 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001796
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001797 if (appctx->ctx.hlua_cosocket.die) {
1798 si_shutw(si);
1799 si_shutr(si);
1800 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001801 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1802 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001803 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1804 }
1805
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001806 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001807 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001808 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001809
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001810 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001811 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001812 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001813
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001814 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001815 * to be notified whenever the connection completes.
1816 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001817 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001818 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001819 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001820 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001821 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001822 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823
1824 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001825 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001826
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001827 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001828 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001829 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830
1831 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001832 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001833 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001834
1835 /* Some data were injected in the buffer, notify the stream
1836 * interface.
1837 */
1838 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001839 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001840
1841 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001842 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001843 */
1844 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001845 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846}
1847
Willy Tarreau87b09662015-04-03 00:22:06 +02001848/* This function is called when the "struct stream" is destroyed.
1849 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001850 * Wake all the pending signals.
1851 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001852static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001854 struct xref *peer;
1855
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001856 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001857 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1858 if (peer)
1859 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860
1861 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001862 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1863 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001864}
1865
1866/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001867 * uses this object. If the stream does not exists, just quit.
1868 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 * pending signal can rest in the read and write lists. destroy
1870 * it.
1871 */
1872__LJMP static int hlua_socket_gc(lua_State *L)
1873{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001874 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001875 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001876 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001878 MAY_LJMP(check_args(L, 1, "__gc"));
1879
1880 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001881 peer = xref_get_peer_and_lock(&socket->xref);
1882 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001884 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001886 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001887 appctx->ctx.hlua_cosocket.die = 1;
1888 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001889
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001890 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001891 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 return 0;
1893}
1894
1895/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001896 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001897 */
sada05ed3302018-05-11 11:48:18 -07001898__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001900 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001902 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001903 struct hlua *hlua;
1904
1905 /* Get hlua struct, or NULL if we execute from main lua state */
1906 hlua = hlua_gethlua(L);
1907 if (!hlua)
1908 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001910 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001911
1912 /* Check if we run on the same thread than the xreator thread.
1913 * We cannot access to the socket if the thread is different.
1914 */
1915 if (socket->tid != tid)
1916 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1917
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001918 peer = xref_get_peer_and_lock(&socket->xref);
1919 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001920 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001921
1922 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001923 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001924
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001925 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001926 appctx->ctx.hlua_cosocket.die = 1;
1927 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001929 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001930 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931 return 0;
1932}
1933
sada05ed3302018-05-11 11:48:18 -07001934/* The close function calls close_helper.
1935 */
1936__LJMP static int hlua_socket_close(lua_State *L)
1937{
1938 MAY_LJMP(check_args(L, 1, "close"));
1939 return hlua_socket_close_helper(L);
1940}
1941
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001942/* This Lua function assumes that the stack contain three parameters.
1943 * 1 - USERDATA containing a struct socket
1944 * 2 - INTEGER with values of the macro defined below
1945 * If the integer is -1, we must read at most one line.
1946 * If the integer is -2, we ust read all the data until the
1947 * end of the stream.
1948 * If the integer is positive value, we must read a number of
1949 * bytes corresponding to this value.
1950 */
1951#define HLSR_READ_LINE (-1)
1952#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001953__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001954{
1955 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1956 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001957 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001959 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001961 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001962 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001963 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001964 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001965 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001966 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001967 struct stream_interface *si;
1968 struct stream *s;
1969 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001970 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001972 /* Get hlua struct, or NULL if we execute from main lua state */
1973 hlua = hlua_gethlua(L);
1974
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975 /* Check if this lua stack is schedulable. */
1976 if (!hlua || !hlua->task)
1977 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1978 "'frontend', 'backend' or 'task'"));
1979
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001980 /* Check if we run on the same thread than the xreator thread.
1981 * We cannot access to the socket if the thread is different.
1982 */
1983 if (socket->tid != tid)
1984 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1985
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001986 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001987 peer = xref_get_peer_and_lock(&socket->xref);
1988 if (!peer)
1989 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001990 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1991 si = appctx->owner;
1992 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001993
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001994 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001995 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001996 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001997 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001998 if (nblk < 0) /* Connection close. */
1999 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002000 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002001 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002002
2003 /* remove final \r\n. */
2004 if (nblk == 1) {
2005 if (blk1[len1-1] == '\n') {
2006 len1--;
2007 skip_at_end++;
2008 if (blk1[len1-1] == '\r') {
2009 len1--;
2010 skip_at_end++;
2011 }
2012 }
2013 }
2014 else {
2015 if (blk2[len2-1] == '\n') {
2016 len2--;
2017 skip_at_end++;
2018 if (blk2[len2-1] == '\r') {
2019 len2--;
2020 skip_at_end++;
2021 }
2022 }
2023 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002024 }
2025
2026 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002028 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 if (nblk < 0) /* Connection close. */
2030 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002031 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032 goto connection_empty;
2033 }
2034
2035 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002037 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002038 if (nblk < 0) /* Connection close. */
2039 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002040 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002041 goto connection_empty;
2042
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002043 missing_bytes = wanted - socket->b.n;
2044 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002046 len1 = missing_bytes;
2047 } if (nblk == 2 && len1 + len2 > missing_bytes)
2048 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049 }
2050
2051 len = len1;
2052
2053 luaL_addlstring(&socket->b, blk1, len1);
2054 if (nblk == 2) {
2055 len += len2;
2056 luaL_addlstring(&socket->b, blk2, len2);
2057 }
2058
2059 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002060 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002061
2062 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002063 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002064
2065 /* If the pattern reclaim to read all the data
2066 * in the connection, got out.
2067 */
2068 if (wanted == HLSR_READ_ALL)
2069 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002070 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071 goto connection_empty;
2072
2073 /* Return result. */
2074 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002075 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002076 return 1;
2077
2078connection_closed:
2079
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002080 xref_unlock(&socket->xref, peer);
2081
2082no_peer:
2083
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084 /* If the buffer containds data. */
2085 if (socket->b.n > 0) {
2086 luaL_pushresult(&socket->b);
2087 return 1;
2088 }
2089 lua_pushnil(L);
2090 lua_pushstring(L, "connection closed.");
2091 return 2;
2092
2093connection_empty:
2094
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002095 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2096 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002097 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002098 }
2099 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002100 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 return 0;
2102}
2103
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002104/* This Lua function gets two parameters. The first one can be string
2105 * or a number. If the string is "*l", the user requires one line. If
2106 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002107 * If the value is a number, the user require a number of bytes equal
2108 * to the value. The default value is "*l" (a line).
2109 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002110 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002111 * integer takes this values:
2112 * -1 : read a line
2113 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002114 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002115 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002116 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002117 * concatenated with the read data.
2118 */
2119__LJMP static int hlua_socket_receive(struct lua_State *L)
2120{
2121 int wanted = HLSR_READ_LINE;
2122 const char *pattern;
2123 int type;
2124 char *error;
2125 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002126 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127
2128 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2129 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2130
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002131 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002132
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002133 /* Check if we run on the same thread than the xreator thread.
2134 * We cannot access to the socket if the thread is different.
2135 */
2136 if (socket->tid != tid)
2137 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2138
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002139 /* check for pattern. */
2140 if (lua_gettop(L) >= 2) {
2141 type = lua_type(L, 2);
2142 if (type == LUA_TSTRING) {
2143 pattern = lua_tostring(L, 2);
2144 if (strcmp(pattern, "*a") == 0)
2145 wanted = HLSR_READ_ALL;
2146 else if (strcmp(pattern, "*l") == 0)
2147 wanted = HLSR_READ_LINE;
2148 else {
2149 wanted = strtoll(pattern, &error, 10);
2150 if (*error != '\0')
2151 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2152 }
2153 }
2154 else if (type == LUA_TNUMBER) {
2155 wanted = lua_tointeger(L, 2);
2156 if (wanted < 0)
2157 WILL_LJMP(luaL_error(L, "Unsupported size."));
2158 }
2159 }
2160
2161 /* Set pattern. */
2162 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002163
2164 /* Check if we would replace the top by itself. */
2165 if (lua_gettop(L) != 2)
2166 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002167
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002168 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169 luaL_buffinit(L, &socket->b);
2170
2171 /* Check prefix. */
2172 if (lua_gettop(L) >= 3) {
2173 if (lua_type(L, 3) != LUA_TSTRING)
2174 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2175 pattern = lua_tolstring(L, 3, &len);
2176 luaL_addlstring(&socket->b, pattern, len);
2177 }
2178
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002179 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180}
2181
2182/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002183 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002184 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002185static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002186{
2187 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002188 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 struct appctx *appctx;
2190 size_t buf_len;
2191 const char *buf;
2192 int len;
2193 int send_len;
2194 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002195 struct xref *peer;
2196 struct stream_interface *si;
2197 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002199 /* Get hlua struct, or NULL if we execute from main lua state */
2200 hlua = hlua_gethlua(L);
2201
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202 /* Check if this lua stack is schedulable. */
2203 if (!hlua || !hlua->task)
2204 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2205 "'frontend', 'backend' or 'task'"));
2206
2207 /* Get object */
2208 socket = MAY_LJMP(hlua_checksocket(L, 1));
2209 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002210 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002211
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002212 /* Check if we run on the same thread than the xreator thread.
2213 * We cannot access to the socket if the thread is different.
2214 */
2215 if (socket->tid != tid)
2216 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2217
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002218 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002219 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002220 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002221 lua_pushinteger(L, -1);
2222 return 1;
2223 }
2224 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2225 si = appctx->owner;
2226 s = si_strm(si);
2227
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002229 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002230 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002231 lua_pushinteger(L, -1);
2232 return 1;
2233 }
2234
2235 /* Update the input buffer data. */
2236 buf += sent;
2237 send_len = buf_len - sent;
2238
2239 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002240 if (sent >= buf_len) {
2241 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002242 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002243 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002245 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002246 * the request buffer if its not required.
2247 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002248 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002249 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002250 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002251 }
2252
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002253 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002254 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002255 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002257 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258
2259 /* send data */
2260 if (len < send_len)
2261 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002262 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263
2264 /* "Not enough space" (-1), "Buffer too little to contain
2265 * the data" (-2) are not expected because the available length
2266 * is tested.
2267 * Other unknown error are also not expected.
2268 */
2269 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002270 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002271 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002272
sada05ed3302018-05-11 11:48:18 -07002273 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002274 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002275 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002276 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002277 return 1;
2278 }
2279
2280 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002281 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002282
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002283 s->req.rex = TICK_ETERNITY;
2284 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285
2286 /* Update length sent. */
2287 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002288 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289
2290 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002291 if (sent + len >= buf_len) {
2292 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002293 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002294 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002295
2296hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002297 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2298 xref_unlock(&socket->xref, peer);
2299 WILL_LJMP(luaL_error(L, "out of memory"));
2300 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002301 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002302 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303 return 0;
2304}
2305
2306/* This function initiate the send of data. It just check the input
2307 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002308 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002309 * "hlua_socket_write_yield" that can yield.
2310 *
2311 * The Lua function gets between 3 and 4 parameters. The first one is
2312 * the associated object. The second is a string buffer. The third is
2313 * a facultative integer that represents where is the buffer position
2314 * of the start of the data that can send. The first byte is the
2315 * position "1". The default value is "1". The fourth argument is a
2316 * facultative integer that represents where is the buffer position
2317 * of the end of the data that can send. The default is the last byte.
2318 */
2319static int hlua_socket_send(struct lua_State *L)
2320{
2321 int i;
2322 int j;
2323 const char *buf;
2324 size_t buf_len;
2325
2326 /* Check number of arguments. */
2327 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2328 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2329
2330 /* Get the string. */
2331 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2332
2333 /* Get and check j. */
2334 if (lua_gettop(L) == 4) {
2335 j = MAY_LJMP(luaL_checkinteger(L, 4));
2336 if (j < 0)
2337 j = buf_len + j + 1;
2338 if (j > buf_len)
2339 j = buf_len + 1;
2340 lua_pop(L, 1);
2341 }
2342 else
2343 j = buf_len;
2344
2345 /* Get and check i. */
2346 if (lua_gettop(L) == 3) {
2347 i = MAY_LJMP(luaL_checkinteger(L, 3));
2348 if (i < 0)
2349 i = buf_len + i + 1;
2350 if (i > buf_len)
2351 i = buf_len + 1;
2352 lua_pop(L, 1);
2353 } else
2354 i = 1;
2355
2356 /* Check bth i and j. */
2357 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002358 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359 return 1;
2360 }
2361 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002362 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002363 return 1;
2364 }
2365 if (i == 0)
2366 i = 1;
2367 if (j == 0)
2368 j = 1;
2369
2370 /* Pop the string. */
2371 lua_pop(L, 1);
2372
2373 /* Update the buffer length. */
2374 buf += i - 1;
2375 buf_len = j - i + 1;
2376 lua_pushlstring(L, buf, buf_len);
2377
2378 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002379 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002380
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002381 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382}
2383
Willy Tarreau22b0a682015-06-17 19:43:49 +02002384#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2386{
2387 static char buffer[SOCKET_INFO_MAX_LEN];
2388 int ret;
2389 int len;
2390 char *p;
2391
2392 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2393 if (ret <= 0) {
2394 lua_pushnil(L);
2395 return 1;
2396 }
2397
2398 if (ret == AF_UNIX) {
2399 lua_pushstring(L, buffer+1);
2400 return 1;
2401 }
2402 else if (ret == AF_INET6) {
2403 buffer[0] = '[';
2404 len = strlen(buffer);
2405 buffer[len] = ']';
2406 len++;
2407 buffer[len] = ':';
2408 len++;
2409 p = buffer;
2410 }
2411 else if (ret == AF_INET) {
2412 p = buffer + 1;
2413 len = strlen(p);
2414 p[len] = ':';
2415 len++;
2416 }
2417 else {
2418 lua_pushnil(L);
2419 return 1;
2420 }
2421
2422 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2423 lua_pushnil(L);
2424 return 1;
2425 }
2426
2427 lua_pushstring(L, p);
2428 return 1;
2429}
2430
2431/* Returns information about the peer of the connection. */
2432__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2433{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002434 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002435 struct xref *peer;
2436 struct appctx *appctx;
2437 struct stream_interface *si;
2438 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002439 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002440
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002441 MAY_LJMP(check_args(L, 1, "getpeername"));
2442
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002443 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002444
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002445 /* Check if we run on the same thread than the xreator thread.
2446 * We cannot access to the socket if the thread is different.
2447 */
2448 if (socket->tid != tid)
2449 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2450
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002451 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002452 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002453 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002454 lua_pushnil(L);
2455 return 1;
2456 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002457 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2458 si = appctx->owner;
2459 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002460
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002461 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002462 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002463 lua_pushnil(L);
2464 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002465 }
2466
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002467 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002468 xref_unlock(&socket->xref, peer);
2469 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002470}
2471
2472/* Returns information about my connection side. */
2473static int hlua_socket_getsockname(struct lua_State *L)
2474{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002475 struct hlua_socket *socket;
2476 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002477 struct appctx *appctx;
2478 struct xref *peer;
2479 struct stream_interface *si;
2480 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002481 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002482
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002483 MAY_LJMP(check_args(L, 1, "getsockname"));
2484
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002485 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002486
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002487 /* Check if we run on the same thread than the xreator thread.
2488 * We cannot access to the socket if the thread is different.
2489 */
2490 if (socket->tid != tid)
2491 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2492
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002493 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002494 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002495 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002496 lua_pushnil(L);
2497 return 1;
2498 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002499 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2500 si = appctx->owner;
2501 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002502
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002503 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002504 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002505 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002506 lua_pushnil(L);
2507 return 1;
2508 }
2509
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002510 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002511 xref_unlock(&socket->xref, peer);
2512 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002513}
2514
2515/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002516static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002517 .obj_type = OBJ_TYPE_APPLET,
2518 .name = "<LUA_TCP>",
2519 .fct = hlua_socket_handler,
2520 .release = hlua_socket_release,
2521};
2522
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002523__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002524{
2525 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002526 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002527 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002528 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002529 struct stream_interface *si;
2530 struct stream *s;
2531
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002532 /* Get hlua struct, or NULL if we execute from main lua state */
2533 hlua = hlua_gethlua(L);
2534 if (!hlua)
2535 return 0;
2536
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002537 /* Check if we run on the same thread than the xreator thread.
2538 * We cannot access to the socket if the thread is different.
2539 */
2540 if (socket->tid != tid)
2541 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2542
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002543 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002544 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002545 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002546 lua_pushnil(L);
2547 lua_pushstring(L, "Can't connect");
2548 return 2;
2549 }
2550 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2551 si = appctx->owner;
2552 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002554 /* Check if we run on the same thread than the xreator thread.
2555 * We cannot access to the socket if the thread is different.
2556 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002557 if (socket->tid != tid) {
2558 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002559 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002560 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002561
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002562 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002563 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002564 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002565 lua_pushnil(L);
2566 lua_pushstring(L, "Can't connect");
2567 return 2;
2568 }
2569
Willy Tarreaue09101e2018-10-16 17:37:12 +02002570 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002571
2572 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002573 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002574 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002575 lua_pushinteger(L, 1);
2576 return 1;
2577 }
2578
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002579 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2580 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002581 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002582 }
2583 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002584 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002585 return 0;
2586}
2587
2588/* This function fail or initite the connection. */
2589__LJMP static int hlua_socket_connect(struct lua_State *L)
2590{
2591 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002592 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002594 struct hlua *hlua;
2595 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002596 int low, high;
2597 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002598 struct xref *peer;
2599 struct stream_interface *si;
2600 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002602 if (lua_gettop(L) < 2)
2603 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002604
2605 /* Get args. */
2606 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002607
2608 /* Check if we run on the same thread than the xreator thread.
2609 * We cannot access to the socket if the thread is different.
2610 */
2611 if (socket->tid != tid)
2612 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2613
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002615 if (lua_gettop(L) >= 3) {
2616 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002617 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618
Tim Duesterhus6edab862018-01-06 19:04:45 +01002619 /* Force the ip to end with a colon, to support IPv6 addresses
2620 * that are not enclosed within square brackets.
2621 */
2622 if (port > 0) {
2623 luaL_buffinit(L, &b);
2624 luaL_addstring(&b, ip);
2625 luaL_addchar(&b, ':');
2626 luaL_pushresult(&b);
2627 ip = lua_tolstring(L, lua_gettop(L), NULL);
2628 }
2629 }
2630
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002631 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002632 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002633 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002634 lua_pushnil(L);
2635 return 1;
2636 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637
2638 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002639 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 +02002640 if (!addr) {
2641 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002642 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002643 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002644
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002646 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002647 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002648 if (port == -1) {
2649 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002650 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002651 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002652 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2653 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002654 if (port == -1) {
2655 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002656 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002657 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002658 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002659 }
2660 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002661
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002662 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2663 si = appctx->owner;
2664 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002665
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002666 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002667 xref_unlock(&socket->xref, peer);
2668 WILL_LJMP(luaL_error(L, "connect: internal error"));
2669 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002670 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002671
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002672 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002673 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002674 if (!hlua)
2675 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002676
2677 /* inform the stream that we want to be notified whenever the
2678 * connection completes.
2679 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002680 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002681 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002682 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002683
Willy Tarreauf31af932020-01-14 09:59:38 +01002684 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002685
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002686 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2687 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002688 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002689 }
2690 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002691
2692 task_wakeup(s->task, TASK_WOKEN_INIT);
2693 /* Return yield waiting for connection. */
2694
Willy Tarreau9635e032018-10-16 17:52:55 +02002695 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002696
2697 return 0;
2698}
2699
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002700#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002701__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2702{
2703 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002704 struct xref *peer;
2705 struct appctx *appctx;
2706 struct stream_interface *si;
2707 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002708
2709 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2710 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002711
2712 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002713 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002714 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002715 lua_pushnil(L);
2716 return 1;
2717 }
2718 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2719 si = appctx->owner;
2720 s = si_strm(si);
2721
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002722 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002723 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002724 return MAY_LJMP(hlua_socket_connect(L));
2725}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002726#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002727
2728__LJMP static int hlua_socket_setoption(struct lua_State *L)
2729{
2730 return 0;
2731}
2732
2733__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2734{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002735 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002736 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002737 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002738 struct xref *peer;
2739 struct appctx *appctx;
2740 struct stream_interface *si;
2741 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002742
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002743 MAY_LJMP(check_args(L, 2, "settimeout"));
2744
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002745 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002746
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002747 /* convert the timeout to millis */
2748 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002749
Thierry Fournier17a921b2018-03-08 09:59:02 +01002750 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002751 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002752 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2753
Mark Lakes56cc1252018-03-27 09:48:06 +02002754 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002755 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002756
2757 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002758 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002759 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002760
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002761 /* Check if we run on the same thread than the xreator thread.
2762 * We cannot access to the socket if the thread is different.
2763 */
2764 if (socket->tid != tid)
2765 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2766
Mark Lakes56cc1252018-03-27 09:48:06 +02002767 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002768 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002769 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002770 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2771 WILL_LJMP(lua_error(L));
2772 return 0;
2773 }
2774 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2775 si = appctx->owner;
2776 s = si_strm(si);
2777
Cyril Bonté7bb63452018-08-17 23:51:02 +02002778 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002779 s->req.rto = tmout;
2780 s->req.wto = tmout;
2781 s->res.rto = tmout;
2782 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002783 s->req.rex = tick_add_ifset(now_ms, tmout);
2784 s->req.wex = tick_add_ifset(now_ms, tmout);
2785 s->res.rex = tick_add_ifset(now_ms, tmout);
2786 s->res.wex = tick_add_ifset(now_ms, tmout);
2787
2788 s->task->expire = tick_add_ifset(now_ms, tmout);
2789 task_queue(s->task);
2790
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002791 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002792
Thierry Fourniere9636f12018-03-08 09:54:32 +01002793 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002794 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002795}
2796
2797__LJMP static int hlua_socket_new(lua_State *L)
2798{
2799 struct hlua_socket *socket;
2800 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002801 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002802 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002803
2804 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002805 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002806 hlua_pusherror(L, "socket: full stack");
2807 goto out_fail_conf;
2808 }
2809
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002810 /* Create the object: obj[0] = userdata. */
2811 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002812 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002813 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002814 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002815 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002816
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002817 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002818 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002819 hlua_pusherror(L, "socket: uninitialized pools.");
2820 goto out_fail_conf;
2821 }
2822
Willy Tarreau87b09662015-04-03 00:22:06 +02002823 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002824 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2825 lua_setmetatable(L, -2);
2826
Willy Tarreaud420a972015-04-06 00:39:18 +02002827 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002828 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002829 if (!appctx) {
2830 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002831 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002832 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002833
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002834 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002835 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002836 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2837 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002838
Willy Tarreaud420a972015-04-06 00:39:18 +02002839 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002840 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002841 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002842 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002843 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002844 }
2845
Christopher Faulet26256f82020-09-14 11:40:13 +02002846 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002847 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002848 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002849 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002850 }
2851
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002852 /* Initialise cross reference between stream and Lua socket object. */
2853 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002854
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002855 /* Configure "right" stream interface. this "si" is used to connect
2856 * and retrieve data from the server. The connection is initialized
2857 * with the "struct server".
2858 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002859 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002860
2861 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002862 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002863 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002864
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002865 return 1;
2866
Willy Tarreaud420a972015-04-06 00:39:18 +02002867 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002868 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002869 out_fail_sess:
2870 appctx_free(appctx);
2871 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002872 WILL_LJMP(lua_error(L));
2873 return 0;
2874}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002875
2876/*
2877 *
2878 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879 * Class Channel
2880 *
2881 *
2882 */
2883
2884/* Returns the struct hlua_channel join to the class channel in the
2885 * stack entry "ud" or throws an argument error.
2886 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002887__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002888{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002889 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002890}
2891
Willy Tarreau47860ed2015-03-10 14:07:50 +01002892/* Pushes the channel onto the top of the stack. If the stask does not have a
2893 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002895static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002896{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002898 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 return 0;
2900
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002901 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002902 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002903 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904
2905 /* Pop a class sesison metatable and affect it to the userdata. */
2906 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2907 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908 return 1;
2909}
2910
2911/* Duplicate all the data present in the input channel and put it
2912 * in a string LUA variables. Returns -1 and push a nil value in
2913 * the stack if the channel is closed and all the data are consumed,
2914 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002915 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002917static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918{
2919 char *blk1;
2920 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002921 size_t len1;
2922 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002923 int ret;
2924 luaL_Buffer b;
2925
Willy Tarreau06d80a92017-10-19 14:32:15 +02002926 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 if (unlikely(ret == 0))
2928 return 0;
2929
2930 if (unlikely(ret < 0)) {
2931 lua_pushnil(L);
2932 return -1;
2933 }
2934
2935 luaL_buffinit(L, &b);
2936 luaL_addlstring(&b, blk1, len1);
2937 if (unlikely(ret == 2))
2938 luaL_addlstring(&b, blk2, len2);
2939 luaL_pushresult(&b);
2940
2941 if (unlikely(ret == 2))
2942 return len1 + len2;
2943 return len1;
2944}
2945
2946/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2947 * a yield. This function keep the data in the buffer.
2948 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002949__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002950{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002951 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002952
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002953 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2954
Thierry Fournier77016da2020-08-15 14:35:51 +02002955 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2956 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002957 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002958 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002959
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002960 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002961 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002962 return 1;
2963}
2964
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002965/* Check arguments for the function "hlua_channel_dup_yield". */
2966__LJMP static int hlua_channel_dup(lua_State *L)
2967{
2968 MAY_LJMP(check_args(L, 1, "dup"));
2969 MAY_LJMP(hlua_checkchannel(L, 1));
2970 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2971}
2972
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002973/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2974 * a yield. This function consumes the data in the buffer. It returns
2975 * a string containing the data or a nil pointer if no data are available
2976 * and the channel is closed.
2977 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002978__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002979{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002980 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002981 int ret;
2982
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002983 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002984
Thierry Fournier77016da2020-08-15 14:35:51 +02002985 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2986 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002987 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002988 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002989
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002990 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002991 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002992 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002993
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 if (unlikely(ret == -1))
2995 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002997 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 return 1;
2999}
3000
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003001/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003002__LJMP static int hlua_channel_get(lua_State *L)
3003{
3004 MAY_LJMP(check_args(L, 1, "get"));
3005 MAY_LJMP(hlua_checkchannel(L, 1));
3006 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3007}
3008
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009/* This functions consumes and returns one line. If the channel is closed,
3010 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003011 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003012 * value.
3013 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003014__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016 char *blk1;
3017 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003018 size_t len1;
3019 size_t len2;
3020 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003021 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022 int ret;
3023 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003024
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003025 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3026
Thierry Fournier77016da2020-08-15 14:35:51 +02003027 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3028 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003029 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003030 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003031
Willy Tarreau06d80a92017-10-19 14:32:15 +02003032 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003033 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02003034 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003035
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036 if (ret == -1) {
3037 lua_pushnil(L);
3038 return 1;
3039 }
3040
3041 luaL_buffinit(L, &b);
3042 luaL_addlstring(&b, blk1, len1);
3043 len = len1;
3044 if (unlikely(ret == 2)) {
3045 luaL_addlstring(&b, blk2, len2);
3046 len += len2;
3047 }
3048 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003049 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050 return 1;
3051}
3052
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003053/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003054__LJMP static int hlua_channel_getline(lua_State *L)
3055{
3056 MAY_LJMP(check_args(L, 1, "getline"));
3057 MAY_LJMP(hlua_checkchannel(L, 1));
3058 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3059}
3060
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061/* This function takes a string as input, and append it at the
3062 * input side of channel. If the data is too big, but a space
3063 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003064 * yields. If the data is bigger than the buffer, or if the
3065 * channel is closed, it returns -1. Otherwise, it returns the
3066 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003067 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003068__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003070 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071 size_t len;
3072 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3073 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3074 int ret;
3075 int max;
3076
Thierry Fournier77016da2020-08-15 14:35:51 +02003077 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3078 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003079 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003080 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003081
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003082 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003083 * the request buffer if its not required.
3084 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003085 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003086 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003087 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003088 }
3089
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003090 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003091 if (max > len - l)
3092 max = len - l;
3093
Willy Tarreau06d80a92017-10-19 14:32:15 +02003094 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003095 if (ret == -2 || ret == -3) {
3096 lua_pushinteger(L, -1);
3097 return 1;
3098 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003099 if (ret == -1) {
3100 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003101 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003102 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 l += ret;
3104 lua_pop(L, 1);
3105 lua_pushinteger(L, l);
3106
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003107 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003108 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003109 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003110 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003111 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003112 */
3113 return 1;
3114 }
3115 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003116 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003117 return 1;
3118}
3119
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003120/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3121 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003122 * buffer size is too little for the data.
3123 */
3124__LJMP static int hlua_channel_append(lua_State *L)
3125{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003126 size_t len;
3127
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003128 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003129 MAY_LJMP(hlua_checkchannel(L, 1));
3130 MAY_LJMP(luaL_checklstring(L, 2, &len));
3131 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003132 lua_pushinteger(L, 0);
3133
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003134 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003135}
3136
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003137/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003138 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003139 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003140 * or -1 if the channel is closed or if the buffer size is too
3141 * little for the data.
3142 */
3143__LJMP static int hlua_channel_set(lua_State *L)
3144{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003145 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003146
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003147 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003148 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003149 lua_pushinteger(L, 0);
3150
Thierry Fournier77016da2020-08-15 14:35:51 +02003151 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3152 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003153 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003154 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003155
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003156 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003157
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003158 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003159}
3160
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003161/* Append data in the output side of the buffer. This data is immediately
3162 * sent. The function returns the amount of data written. If the buffer
3163 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164 * if the channel is closed.
3165 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003166__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003167{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003168 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003169 size_t len;
3170 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3171 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3172 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003173 struct hlua *hlua;
3174
3175 /* Get hlua struct, or NULL if we execute from main lua state */
3176 hlua = hlua_gethlua(L);
3177 if (!hlua) {
3178 lua_pushnil(L);
3179 return 1;
3180 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003181
Thierry Fournier77016da2020-08-15 14:35:51 +02003182 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3183 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003184 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003185 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003186
Willy Tarreau47860ed2015-03-10 14:07:50 +01003187 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003188 lua_pushinteger(L, -1);
3189 return 1;
3190 }
3191
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003192 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003193 * the request buffer if its not required.
3194 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003195 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003196 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003197 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003198 }
3199
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003200 /* The written data will be immediately sent, so we can check
3201 * the available space without taking in account the reserve.
3202 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003203 * data, because the buffer will be flushed.
3204 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003205 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003206
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003207 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003208 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003209 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003210 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003211 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003212 return 1;
3213
3214 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003215 if (max > len - l)
3216 max = len - l;
3217
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003218 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003219 * detects a non contiguous buffer and realign it.
3220 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003221 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003222 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003223
3224 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003225 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003226
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003227 /* buffer replace considers that the input part is filled.
3228 * so, I must forward these new data in the output part.
3229 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003230 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003231
3232 l += max;
3233 lua_pop(L, 1);
3234 lua_pushinteger(L, l);
3235
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003236 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003237 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003238 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003239 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003240 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003241 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003242 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003243
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003244 if (l < len) {
3245 /* If we are waiting for space in the response buffer, we
3246 * must set the flag WAKERESWR. This flag required the task
3247 * wake up if any activity is detected on the response buffer.
3248 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003249 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003250 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003251 else
3252 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003253 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003254 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003255
3256 return 1;
3257}
3258
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003259/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003260 * yield the LUA process, and resume it without checking the
3261 * input arguments.
3262 */
3263__LJMP static int hlua_channel_send(lua_State *L)
3264{
3265 MAY_LJMP(check_args(L, 2, "send"));
3266 lua_pushinteger(L, 0);
3267
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003268 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003269}
3270
3271/* This function forward and amount of butes. The data pass from
3272 * the input side of the buffer to the output side, and can be
3273 * forwarded. This function never fails.
3274 *
3275 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003276 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003277 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003278__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003279{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003280 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003281 int len;
3282 int l;
3283 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003284 struct hlua *hlua;
3285
3286 /* Get hlua struct, or NULL if we execute from main lua state */
3287 hlua = hlua_gethlua(L);
3288 if (!hlua)
3289 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003290
3291 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003292
Thierry Fournier77016da2020-08-15 14:35:51 +02003293 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3294 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003295 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003296 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003297
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003298 len = MAY_LJMP(luaL_checkinteger(L, 2));
3299 l = MAY_LJMP(luaL_checkinteger(L, -1));
3300
3301 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003302 if (max > ci_data(chn))
3303 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003304 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003305 l += max;
3306
3307 lua_pop(L, 1);
3308 lua_pushinteger(L, l);
3309
3310 /* Check if it miss bytes to forward. */
3311 if (l < len) {
3312 /* The the input channel or the output channel are closed, we
3313 * must return the amount of data forwarded.
3314 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003315 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003316 return 1;
3317
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003318 /* If we are waiting for space data in the response buffer, we
3319 * must set the flag WAKERESWR. This flag required the task
3320 * wake up if any activity is detected on the response buffer.
3321 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003322 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003323 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003324 else
3325 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003326
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003327 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003328 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003329 }
3330
3331 return 1;
3332}
3333
3334/* Just check the input and prepare the stack for the previous
3335 * function "hlua_channel_forward_yield"
3336 */
3337__LJMP static int hlua_channel_forward(lua_State *L)
3338{
3339 MAY_LJMP(check_args(L, 2, "forward"));
3340 MAY_LJMP(hlua_checkchannel(L, 1));
3341 MAY_LJMP(luaL_checkinteger(L, 2));
3342
3343 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003344 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003345}
3346
3347/* Just returns the number of bytes available in the input
3348 * side of the buffer. This function never fails.
3349 */
3350__LJMP static int hlua_channel_get_in_len(lua_State *L)
3351{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003352 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003353
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003354 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003355 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003356 if (IS_HTX_STRM(chn_strm(chn))) {
3357 struct htx *htx = htxbuf(&chn->buf);
3358 lua_pushinteger(L, htx->data - co_data(chn));
3359 }
3360 else
3361 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003362 return 1;
3363}
3364
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003365/* Returns true if the channel is full. */
3366__LJMP static int hlua_channel_is_full(lua_State *L)
3367{
3368 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003369
3370 MAY_LJMP(check_args(L, 1, "is_full"));
3371 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003372 /* ignore the reserve, we are not on a producer side (ie in an
3373 * applet).
3374 */
3375 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003376 return 1;
3377}
3378
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003379/* Returns true if the channel is the response channel. */
3380__LJMP static int hlua_channel_is_resp(lua_State *L)
3381{
3382 struct channel *chn;
3383
3384 MAY_LJMP(check_args(L, 1, "is_resp"));
3385 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3386
3387 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3388 return 1;
3389}
3390
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003391/* Just returns the number of bytes available in the output
3392 * side of the buffer. This function never fails.
3393 */
3394__LJMP static int hlua_channel_get_out_len(lua_State *L)
3395{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003396 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003397
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003398 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003399 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003400 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003401 return 1;
3402}
3403
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003404/*
3405 *
3406 *
3407 * Class Fetches
3408 *
3409 *
3410 */
3411
3412/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003413 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003414 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003415__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003416{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003417 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003418}
3419
3420/* This function creates and push in the stack a fetch object according
3421 * with a current TXN.
3422 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003423static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003424{
Willy Tarreau7073c472015-04-06 11:15:40 +02003425 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003426
3427 /* Check stack size. */
3428 if (!lua_checkstack(L, 3))
3429 return 0;
3430
3431 /* Create the object: obj[0] = userdata.
3432 * Note that the base of the Fetches object is the
3433 * transaction object.
3434 */
3435 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003436 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003437 lua_rawseti(L, -2, 0);
3438
Willy Tarreau7073c472015-04-06 11:15:40 +02003439 hsmp->s = txn->s;
3440 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003441 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003442 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003443
3444 /* Pop a class sesison metatable and affect it to the userdata. */
3445 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3446 lua_setmetatable(L, -2);
3447
3448 return 1;
3449}
3450
3451/* This function is an LUA binding. It is called with each sample-fetch.
3452 * It uses closure argument to store the associated sample-fetch. It
3453 * returns only one argument or throws an error. An error is thrown
3454 * only if an error is encountered during the argument parsing. If
3455 * the "sample-fetch" function fails, nil is returned.
3456 */
3457__LJMP static int hlua_run_sample_fetch(lua_State *L)
3458{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003459 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003460 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003461 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003462 int i;
3463 struct sample smp;
3464
3465 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003466 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003467
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003468 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003469 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003470
Thierry FOURNIERca988662015-12-20 18:43:03 +01003471 /* Check execution authorization. */
3472 if (f->use & SMP_USE_HTTP_ANY &&
3473 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3474 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3475 "is not available in Lua services", f->kw);
3476 WILL_LJMP(lua_error(L));
3477 }
3478
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003479 /* Get extra arguments. */
3480 for (i = 0; i < lua_gettop(L) - 1; i++) {
3481 if (i >= ARGM_NBARGS)
3482 break;
3483 hlua_lua2arg(L, i + 2, &args[i]);
3484 }
3485 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003486 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003487
3488 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003489 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003490
3491 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003492 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003493 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003494 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003495 }
3496
3497 /* Initialise the sample. */
3498 memset(&smp, 0, sizeof(smp));
3499
3500 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003501 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003502 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003503 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003504 lua_pushstring(L, "");
3505 else
3506 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003507 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003508 }
3509
3510 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003511 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003512 hlua_smp2lua_str(L, &smp);
3513 else
3514 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003515
3516 end:
3517 for (i = 0; args[i].type != ARGT_STOP; i++) {
3518 if (args[i].type == ARGT_STR)
3519 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003520 else if (args[i].type == ARGT_REG)
3521 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003522 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003523 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003524
3525 error:
3526 for (i = 0; args[i].type != ARGT_STOP; i++) {
3527 if (args[i].type == ARGT_STR)
3528 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003529 else if (args[i].type == ARGT_REG)
3530 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003531 }
3532 WILL_LJMP(lua_error(L));
3533 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003534}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003535
3536/*
3537 *
3538 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003539 * Class Converters
3540 *
3541 *
3542 */
3543
3544/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003545 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003546 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003547__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003548{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003549 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003550}
3551
3552/* This function creates and push in the stack a Converters object
3553 * according with a current TXN.
3554 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003555static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003556{
Willy Tarreau7073c472015-04-06 11:15:40 +02003557 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003558
3559 /* Check stack size. */
3560 if (!lua_checkstack(L, 3))
3561 return 0;
3562
3563 /* Create the object: obj[0] = userdata.
3564 * Note that the base of the Converters object is the
3565 * same than the TXN object.
3566 */
3567 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003568 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003569 lua_rawseti(L, -2, 0);
3570
Willy Tarreau7073c472015-04-06 11:15:40 +02003571 hsmp->s = txn->s;
3572 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003573 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003574 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003575
Willy Tarreau87b09662015-04-03 00:22:06 +02003576 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003577 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3578 lua_setmetatable(L, -2);
3579
3580 return 1;
3581}
3582
3583/* This function is an LUA binding. It is called with each converter.
3584 * It uses closure argument to store the associated converter. It
3585 * returns only one argument or throws an error. An error is thrown
3586 * only if an error is encountered during the argument parsing. If
3587 * the converter function function fails, nil is returned.
3588 */
3589__LJMP static int hlua_run_sample_conv(lua_State *L)
3590{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003591 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003592 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003593 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003594 int i;
3595 struct sample smp;
3596
3597 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003598 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003599
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003600 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003601 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003602
3603 /* Get extra arguments. */
3604 for (i = 0; i < lua_gettop(L) - 2; i++) {
3605 if (i >= ARGM_NBARGS)
3606 break;
3607 hlua_lua2arg(L, i + 3, &args[i]);
3608 }
3609 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003610 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003611
3612 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003613 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003614
3615 /* Run the special args checker. */
3616 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3617 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003618 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003619 }
3620
3621 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003622 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003623 if (!hlua_lua2smp(L, 2, &smp)) {
3624 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003625 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003626 }
3627
Willy Tarreau1777ea62016-03-10 16:15:46 +01003628 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3629
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003630 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003631 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003632 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003633 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003634 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003635 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003636 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3637 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003638 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003639 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003640 }
3641
3642 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003643 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003644 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003645 lua_pushstring(L, "");
3646 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003647 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003648 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003649 }
3650
3651 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003652 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003653 hlua_smp2lua_str(L, &smp);
3654 else
3655 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003656 end:
3657 for (i = 0; args[i].type != ARGT_STOP; i++) {
3658 if (args[i].type == ARGT_STR)
3659 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003660 else if (args[i].type == ARGT_REG)
3661 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003662 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003663 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003664
3665 error:
3666 for (i = 0; args[i].type != ARGT_STOP; i++) {
3667 if (args[i].type == ARGT_STR)
3668 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003669 else if (args[i].type == ARGT_REG)
3670 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003671 }
3672 WILL_LJMP(lua_error(L));
3673 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003674}
3675
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003676/*
3677 *
3678 *
3679 * Class AppletTCP
3680 *
3681 *
3682 */
3683
3684/* Returns a struct hlua_txn if the stack entry "ud" is
3685 * a class stream, otherwise it throws an error.
3686 */
3687__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3688{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003689 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003690}
3691
3692/* This function creates and push in the stack an Applet object
3693 * according with a current TXN.
3694 */
3695static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3696{
3697 struct hlua_appctx *appctx;
3698 struct stream_interface *si = ctx->owner;
3699 struct stream *s = si_strm(si);
3700 struct proxy *p = s->be;
3701
3702 /* Check stack size. */
3703 if (!lua_checkstack(L, 3))
3704 return 0;
3705
3706 /* Create the object: obj[0] = userdata.
3707 * Note that the base of the Converters object is the
3708 * same than the TXN object.
3709 */
3710 lua_newtable(L);
3711 appctx = lua_newuserdata(L, sizeof(*appctx));
3712 lua_rawseti(L, -2, 0);
3713 appctx->appctx = ctx;
3714 appctx->htxn.s = s;
3715 appctx->htxn.p = p;
3716
3717 /* Create the "f" field that contains a list of fetches. */
3718 lua_pushstring(L, "f");
3719 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3720 return 0;
3721 lua_settable(L, -3);
3722
3723 /* Create the "sf" field that contains a list of stringsafe fetches. */
3724 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003725 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003726 return 0;
3727 lua_settable(L, -3);
3728
3729 /* Create the "c" field that contains a list of converters. */
3730 lua_pushstring(L, "c");
3731 if (!hlua_converters_new(L, &appctx->htxn, 0))
3732 return 0;
3733 lua_settable(L, -3);
3734
3735 /* Create the "sc" field that contains a list of stringsafe converters. */
3736 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003737 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003738 return 0;
3739 lua_settable(L, -3);
3740
3741 /* Pop a class stream metatable and affect it to the table. */
3742 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3743 lua_setmetatable(L, -2);
3744
3745 return 1;
3746}
3747
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003748__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3749{
3750 struct hlua_appctx *appctx;
3751 struct stream *s;
3752 const char *name;
3753 size_t len;
3754 struct sample smp;
3755
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003756 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3757 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003758
3759 /* It is useles to retrieve the stream, but this function
3760 * runs only in a stream context.
3761 */
3762 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3763 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3764 s = appctx->htxn.s;
3765
3766 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003767 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003768 hlua_lua2smp(L, 3, &smp);
3769
3770 /* Store the sample in a variable. */
3771 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003772
3773 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3774 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3775 else
3776 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3777
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003778 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003779}
3780
3781__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3782{
3783 struct hlua_appctx *appctx;
3784 struct stream *s;
3785 const char *name;
3786 size_t len;
3787 struct sample smp;
3788
3789 MAY_LJMP(check_args(L, 2, "unset_var"));
3790
3791 /* It is useles to retrieve the stream, but this function
3792 * runs only in a stream context.
3793 */
3794 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3795 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3796 s = appctx->htxn.s;
3797
3798 /* Unset the variable. */
3799 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003800 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3801 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003802}
3803
3804__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3805{
3806 struct hlua_appctx *appctx;
3807 struct stream *s;
3808 const char *name;
3809 size_t len;
3810 struct sample smp;
3811
3812 MAY_LJMP(check_args(L, 2, "get_var"));
3813
3814 /* It is useles to retrieve the stream, but this function
3815 * runs only in a stream context.
3816 */
3817 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3818 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3819 s = appctx->htxn.s;
3820
3821 smp_set_owner(&smp, s->be, s->sess, s, 0);
3822 if (!vars_get_by_name(name, len, &smp)) {
3823 lua_pushnil(L);
3824 return 1;
3825 }
3826
3827 return hlua_smp2lua(L, &smp);
3828}
3829
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003830__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3831{
3832 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3833 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003834 struct hlua *hlua;
3835
3836 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003837 if (!s->hlua)
3838 return 0;
3839 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003840
3841 MAY_LJMP(check_args(L, 2, "set_priv"));
3842
3843 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003844 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003845
3846 /* Get and store new value. */
3847 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3848 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3849
3850 return 0;
3851}
3852
3853__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3854{
3855 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3856 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003857 struct hlua *hlua;
3858
3859 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003860 if (!s->hlua) {
3861 lua_pushnil(L);
3862 return 1;
3863 }
3864 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003865
3866 /* Push configuration index in the stack. */
3867 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3868
3869 return 1;
3870}
3871
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003872/* If expected data not yet available, it returns a yield. This function
3873 * consumes the data in the buffer. It returns a string containing the
3874 * data. This string can be empty.
3875 */
3876__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3877{
3878 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3879 struct stream_interface *si = appctx->appctx->owner;
3880 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003881 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003882 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003883 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003884 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003885
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003886 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003887 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003888
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003889 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003890 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003891 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003892 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003893 }
3894
3895 /* End of data: commit the total strings and return. */
3896 if (ret < 0) {
3897 luaL_pushresult(&appctx->b);
3898 return 1;
3899 }
3900
3901 /* Ensure that the block 2 length is usable. */
3902 if (ret == 1)
3903 len2 = 0;
3904
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003905 /* don't check the max length read and don't check. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003906 luaL_addlstring(&appctx->b, blk1, len1);
3907 luaL_addlstring(&appctx->b, blk2, len2);
3908
3909 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003910 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003911 luaL_pushresult(&appctx->b);
3912 return 1;
3913}
3914
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003915/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003916__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3917{
3918 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3919
3920 /* Initialise the string catenation. */
3921 luaL_buffinit(L, &appctx->b);
3922
3923 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3924}
3925
3926/* If expected data not yet available, it returns a yield. This function
3927 * consumes the data in the buffer. It returns a string containing the
3928 * data. This string can be empty.
3929 */
3930__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3931{
3932 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3933 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003934 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003935 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003936 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003937 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003938 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003939 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003940
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003941 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003942 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003943
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003944 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003945 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003946 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003947 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003948 }
3949
3950 /* End of data: commit the total strings and return. */
3951 if (ret < 0) {
3952 luaL_pushresult(&appctx->b);
3953 return 1;
3954 }
3955
3956 /* Ensure that the block 2 length is usable. */
3957 if (ret == 1)
3958 len2 = 0;
3959
3960 if (len == -1) {
3961
3962 /* If len == -1, catenate all the data avalaile and
3963 * yield because we want to get all the data until
3964 * the end of data stream.
3965 */
3966 luaL_addlstring(&appctx->b, blk1, len1);
3967 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003968 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003969 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003970 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003971
3972 } else {
3973
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003974 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003975 if (len1 > len)
3976 len1 = len;
3977 luaL_addlstring(&appctx->b, blk1, len1);
3978 len -= len1;
3979
3980 /* Copy the second block. */
3981 if (len2 > len)
3982 len2 = len;
3983 luaL_addlstring(&appctx->b, blk2, len2);
3984 len -= len2;
3985
3986 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003987 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003988
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003989 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003990 if (len > 0) {
3991 lua_pushinteger(L, len);
3992 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003993 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003994 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003995 }
3996
3997 /* return the result. */
3998 luaL_pushresult(&appctx->b);
3999 return 1;
4000 }
4001
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004002 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004003 hlua_pusherror(L, "Lua: internal error");
4004 WILL_LJMP(lua_error(L));
4005 return 0;
4006}
4007
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004008/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004009__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4010{
4011 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4012 int len = -1;
4013
4014 if (lua_gettop(L) > 2)
4015 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4016 if (lua_gettop(L) >= 2) {
4017 len = MAY_LJMP(luaL_checkinteger(L, 2));
4018 lua_pop(L, 1);
4019 }
4020
4021 /* Confirm or set the required length */
4022 lua_pushinteger(L, len);
4023
4024 /* Initialise the string catenation. */
4025 luaL_buffinit(L, &appctx->b);
4026
4027 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4028}
4029
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004030/* Append data in the output side of the buffer. This data is immediately
4031 * sent. The function returns the amount of data written. If the buffer
4032 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004033 * if the channel is closed.
4034 */
4035__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4036{
4037 size_t len;
4038 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4039 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4040 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4041 struct stream_interface *si = appctx->appctx->owner;
4042 struct channel *chn = si_ic(si);
4043 int max;
4044
4045 /* Get the max amount of data which can write as input in the channel. */
4046 max = channel_recv_max(chn);
4047 if (max > (len - l))
4048 max = len - l;
4049
4050 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004051 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004052
4053 /* update counters. */
4054 l += max;
4055 lua_pop(L, 1);
4056 lua_pushinteger(L, l);
4057
4058 /* If some data is not send, declares the situation to the
4059 * applet, and returns a yield.
4060 */
4061 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004062 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004063 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004064 }
4065
4066 return 1;
4067}
4068
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004069/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004070 * yield the LUA process, and resume it without checking the
4071 * input arguments.
4072 */
4073__LJMP static int hlua_applet_tcp_send(lua_State *L)
4074{
4075 MAY_LJMP(check_args(L, 2, "send"));
4076 lua_pushinteger(L, 0);
4077
4078 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4079}
4080
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004081/*
4082 *
4083 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004084 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004085 *
4086 *
4087 */
4088
4089/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004090 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004091 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004092__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004093{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004094 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004095}
4096
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004097/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004098 * according with a current TXN.
4099 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004100static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004101{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004102 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004103 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004104 struct stream_interface *si = ctx->owner;
4105 struct stream *s = si_strm(si);
4106 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004107 struct htx *htx;
4108 struct htx_blk *blk;
4109 struct htx_sl *sl;
4110 struct ist path;
4111 unsigned long long len = 0;
4112 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004113
4114 /* Check stack size. */
4115 if (!lua_checkstack(L, 3))
4116 return 0;
4117
4118 /* Create the object: obj[0] = userdata.
4119 * Note that the base of the Converters object is the
4120 * same than the TXN object.
4121 */
4122 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004123 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004124 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004125 appctx->appctx = ctx;
4126 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004127 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004128 appctx->htxn.s = s;
4129 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004130
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004131 /* Create the "f" field that contains a list of fetches. */
4132 lua_pushstring(L, "f");
4133 if (!hlua_fetches_new(L, &appctx->htxn, 0))
4134 return 0;
4135 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004136
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004137 /* Create the "sf" field that contains a list of stringsafe fetches. */
4138 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004139 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004140 return 0;
4141 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004142
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004143 /* Create the "c" field that contains a list of converters. */
4144 lua_pushstring(L, "c");
4145 if (!hlua_converters_new(L, &appctx->htxn, 0))
4146 return 0;
4147 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004148
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004149 /* Create the "sc" field that contains a list of stringsafe converters. */
4150 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004151 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004152 return 0;
4153 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004154
Christopher Fauleta2097962019-07-15 16:25:33 +02004155 htx = htxbuf(&s->req.buf);
4156 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004157 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004158 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004159
Christopher Fauleta2097962019-07-15 16:25:33 +02004160 /* Stores the request method. */
4161 lua_pushstring(L, "method");
4162 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4163 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004164
Christopher Fauleta2097962019-07-15 16:25:33 +02004165 /* Stores the http version. */
4166 lua_pushstring(L, "version");
4167 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4168 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004169
Christopher Fauleta2097962019-07-15 16:25:33 +02004170 /* creates an array of headers. hlua_http_get_headers() crates and push
4171 * the array on the top of the stack.
4172 */
4173 lua_pushstring(L, "headers");
4174 htxn.s = s;
4175 htxn.p = px;
4176 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004177 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004178 return 0;
4179 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004180
Christopher Fauleta2097962019-07-15 16:25:33 +02004181 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004182 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004183 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004184
Christopher Fauleta2097962019-07-15 16:25:33 +02004185 p = path.ptr;
4186 end = path.ptr + path.len;
4187 q = p;
4188 while (q < end && *q != '?')
4189 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004190
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004191 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004192 lua_pushstring(L, "path");
4193 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004194 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004195
Christopher Fauleta2097962019-07-15 16:25:33 +02004196 /* Stores the query string. */
4197 lua_pushstring(L, "qs");
4198 if (*q == '?')
4199 q++;
4200 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004201 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004202 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004203
Christopher Fauleta2097962019-07-15 16:25:33 +02004204 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4205 struct htx_blk *blk = htx_get_blk(htx, pos);
4206 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004207
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004208 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004209 break;
4210 if (type == HTX_BLK_DATA)
4211 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004212 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004213 if (htx->extra != ULLONG_MAX)
4214 len += htx->extra;
4215
4216 /* Stores the request path. */
4217 lua_pushstring(L, "length");
4218 lua_pushinteger(L, len);
4219 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004220
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004221 /* Create an empty array of HTTP request headers. */
4222 lua_pushstring(L, "response");
4223 lua_newtable(L);
4224 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004225
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004226 /* Pop a class stream metatable and affect it to the table. */
4227 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4228 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004229
4230 return 1;
4231}
4232
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004233__LJMP static int hlua_applet_http_set_var(lua_State *L)
4234{
4235 struct hlua_appctx *appctx;
4236 struct stream *s;
4237 const char *name;
4238 size_t len;
4239 struct sample smp;
4240
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004241 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4242 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004243
4244 /* It is useles to retrieve the stream, but this function
4245 * runs only in a stream context.
4246 */
4247 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4248 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4249 s = appctx->htxn.s;
4250
4251 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004252 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004253 hlua_lua2smp(L, 3, &smp);
4254
4255 /* Store the sample in a variable. */
4256 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004257
4258 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4259 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4260 else
4261 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4262
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004263 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004264}
4265
4266__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4267{
4268 struct hlua_appctx *appctx;
4269 struct stream *s;
4270 const char *name;
4271 size_t len;
4272 struct sample smp;
4273
4274 MAY_LJMP(check_args(L, 2, "unset_var"));
4275
4276 /* It is useles to retrieve the stream, but this function
4277 * runs only in a stream context.
4278 */
4279 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4280 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4281 s = appctx->htxn.s;
4282
4283 /* Unset the variable. */
4284 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004285 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4286 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004287}
4288
4289__LJMP static int hlua_applet_http_get_var(lua_State *L)
4290{
4291 struct hlua_appctx *appctx;
4292 struct stream *s;
4293 const char *name;
4294 size_t len;
4295 struct sample smp;
4296
4297 MAY_LJMP(check_args(L, 2, "get_var"));
4298
4299 /* It is useles to retrieve the stream, but this function
4300 * runs only in a stream context.
4301 */
4302 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4303 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4304 s = appctx->htxn.s;
4305
4306 smp_set_owner(&smp, s->be, s->sess, s, 0);
4307 if (!vars_get_by_name(name, len, &smp)) {
4308 lua_pushnil(L);
4309 return 1;
4310 }
4311
4312 return hlua_smp2lua(L, &smp);
4313}
4314
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004315__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4316{
4317 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4318 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004319 struct hlua *hlua;
4320
4321 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004322 if (!s->hlua)
4323 return 0;
4324 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004325
4326 MAY_LJMP(check_args(L, 2, "set_priv"));
4327
4328 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004329 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004330
4331 /* Get and store new value. */
4332 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4333 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4334
4335 return 0;
4336}
4337
4338__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4339{
4340 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4341 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004342 struct hlua *hlua;
4343
4344 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004345 if (!s->hlua) {
4346 lua_pushnil(L);
4347 return 1;
4348 }
4349 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004350
4351 /* Push configuration index in the stack. */
4352 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4353
4354 return 1;
4355}
4356
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004357/* If expected data not yet available, it returns a yield. This function
4358 * consumes the data in the buffer. It returns a string containing the
4359 * data. This string can be empty.
4360 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004361__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004362{
4363 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4364 struct stream_interface *si = appctx->appctx->owner;
4365 struct channel *req = si_oc(si);
4366 struct htx *htx;
4367 struct htx_blk *blk;
4368 size_t count;
4369 int stop = 0;
4370
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004371 htx = htx_from_buf(&req->buf);
4372 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004373 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004374
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004375 while (count && !stop && blk) {
4376 enum htx_blk_type type = htx_get_blk_type(blk);
4377 uint32_t sz = htx_get_blksz(blk);
4378 struct ist v;
4379 uint32_t vlen;
4380 char *nl;
4381
4382 vlen = sz;
4383 if (vlen > count) {
4384 if (type != HTX_BLK_DATA)
4385 break;
4386 vlen = count;
4387 }
4388
4389 switch (type) {
4390 case HTX_BLK_UNUSED:
4391 break;
4392
4393 case HTX_BLK_DATA:
4394 v = htx_get_blk_value(htx, blk);
4395 v.len = vlen;
4396 nl = istchr(v, '\n');
4397 if (nl != NULL) {
4398 stop = 1;
4399 vlen = nl - v.ptr + 1;
4400 }
4401 luaL_addlstring(&appctx->b, v.ptr, vlen);
4402 break;
4403
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004404 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004405 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004406 stop = 1;
4407 break;
4408
4409 default:
4410 break;
4411 }
4412
4413 co_set_data(req, co_data(req) - vlen);
4414 count -= vlen;
4415 if (sz == vlen)
4416 blk = htx_remove_blk(htx, blk);
4417 else {
4418 htx_cut_data_blk(htx, blk, vlen);
4419 break;
4420 }
4421 }
4422
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004423 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004424 if (!stop) {
4425 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004426 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004427 }
4428
4429 /* return the result. */
4430 luaL_pushresult(&appctx->b);
4431 return 1;
4432}
4433
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004434
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004435/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004436__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004437{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004438 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004439
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004440 /* Initialise the string catenation. */
4441 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004442
Christopher Fauleta2097962019-07-15 16:25:33 +02004443 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004444}
4445
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004446/* If expected data not yet available, it returns a yield. This function
4447 * consumes the data in the buffer. It returns a string containing the
4448 * data. This string can be empty.
4449 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004450__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004451{
4452 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4453 struct stream_interface *si = appctx->appctx->owner;
4454 struct channel *req = si_oc(si);
4455 struct htx *htx;
4456 struct htx_blk *blk;
4457 size_t count;
4458 int len;
4459
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004460 htx = htx_from_buf(&req->buf);
4461 len = MAY_LJMP(luaL_checkinteger(L, 2));
4462 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004463 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004464 while (count && len && blk) {
4465 enum htx_blk_type type = htx_get_blk_type(blk);
4466 uint32_t sz = htx_get_blksz(blk);
4467 struct ist v;
4468 uint32_t vlen;
4469
4470 vlen = sz;
4471 if (len > 0 && vlen > len)
4472 vlen = len;
4473 if (vlen > count) {
4474 if (type != HTX_BLK_DATA)
4475 break;
4476 vlen = count;
4477 }
4478
4479 switch (type) {
4480 case HTX_BLK_UNUSED:
4481 break;
4482
4483 case HTX_BLK_DATA:
4484 v = htx_get_blk_value(htx, blk);
4485 luaL_addlstring(&appctx->b, v.ptr, vlen);
4486 break;
4487
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004488 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004489 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004490 len = 0;
4491 break;
4492
4493 default:
4494 break;
4495 }
4496
4497 co_set_data(req, co_data(req) - vlen);
4498 count -= vlen;
4499 if (len > 0)
4500 len -= vlen;
4501 if (sz == vlen)
4502 blk = htx_remove_blk(htx, blk);
4503 else {
4504 htx_cut_data_blk(htx, blk, vlen);
4505 break;
4506 }
4507 }
4508
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004509 htx_to_buf(htx, &req->buf);
4510
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004511 /* If we are no other data available, yield waiting for new data. */
4512 if (len) {
4513 if (len > 0) {
4514 lua_pushinteger(L, len);
4515 lua_replace(L, 2);
4516 }
4517 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004518 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004519 }
4520
4521 /* return the result. */
4522 luaL_pushresult(&appctx->b);
4523 return 1;
4524}
4525
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004526/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004527__LJMP static int hlua_applet_http_recv(lua_State *L)
4528{
4529 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4530 int len = -1;
4531
4532 /* Check arguments. */
4533 if (lua_gettop(L) > 2)
4534 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4535 if (lua_gettop(L) >= 2) {
4536 len = MAY_LJMP(luaL_checkinteger(L, 2));
4537 lua_pop(L, 1);
4538 }
4539
Christopher Fauleta2097962019-07-15 16:25:33 +02004540 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004541
Christopher Fauleta2097962019-07-15 16:25:33 +02004542 /* Initialise the string catenation. */
4543 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004544
Christopher Fauleta2097962019-07-15 16:25:33 +02004545 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004546}
4547
4548/* Append data in the output side of the buffer. This data is immediately
4549 * sent. The function returns the amount of data written. If the buffer
4550 * cannot contain the data, the function yields. The function returns -1
4551 * if the channel is closed.
4552 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004553__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004554{
4555 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4556 struct stream_interface *si = appctx->appctx->owner;
4557 struct channel *res = si_ic(si);
4558 struct htx *htx = htx_from_buf(&res->buf);
4559 const char *data;
4560 size_t len;
4561 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4562 int max;
4563
Christopher Faulet9060fc02019-07-03 11:39:30 +02004564 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004565 if (!max)
4566 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004567
4568 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4569
4570 /* Get the max amount of data which can write as input in the channel. */
4571 if (max > (len - l))
4572 max = len - l;
4573
4574 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004575 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004576 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004577
4578 /* update counters. */
4579 l += max;
4580 lua_pop(L, 1);
4581 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004582
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004583 /* If some data is not send, declares the situation to the
4584 * applet, and returns a yield.
4585 */
4586 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004587 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004588 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004589 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004590 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004591 }
4592
Christopher Fauleta2097962019-07-15 16:25:33 +02004593 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004594 return 1;
4595}
4596
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004597/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004598 * yield the LUA process, and resume it without checking the
4599 * input arguments.
4600 */
4601__LJMP static int hlua_applet_http_send(lua_State *L)
4602{
4603 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004604
4605 /* We want to send some data. Headers must be sent. */
4606 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4607 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4608 WILL_LJMP(lua_error(L));
4609 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004610
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004611 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004612 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004613
Christopher Fauleta2097962019-07-15 16:25:33 +02004614 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004615}
4616
4617__LJMP static int hlua_applet_http_addheader(lua_State *L)
4618{
4619 const char *name;
4620 int ret;
4621
4622 MAY_LJMP(hlua_checkapplet_http(L, 1));
4623 name = MAY_LJMP(luaL_checkstring(L, 2));
4624 MAY_LJMP(luaL_checkstring(L, 3));
4625
4626 /* Push in the stack the "response" entry. */
4627 ret = lua_getfield(L, 1, "response");
4628 if (ret != LUA_TTABLE) {
4629 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4630 "is expected as an array. %s found", lua_typename(L, ret));
4631 WILL_LJMP(lua_error(L));
4632 }
4633
4634 /* check if the header is already registered if it is not
4635 * the case, register it.
4636 */
4637 ret = lua_getfield(L, -1, name);
4638 if (ret == LUA_TNIL) {
4639
4640 /* Entry not found. */
4641 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4642
4643 /* Insert the new header name in the array in the top of the stack.
4644 * It left the new array in the top of the stack.
4645 */
4646 lua_newtable(L);
4647 lua_pushvalue(L, 2);
4648 lua_pushvalue(L, -2);
4649 lua_settable(L, -4);
4650
4651 } else if (ret != LUA_TTABLE) {
4652
4653 /* corruption error. */
4654 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4655 "is expected as an array. %s found", name, lua_typename(L, ret));
4656 WILL_LJMP(lua_error(L));
4657 }
4658
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004659 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660 * the header value as new entry.
4661 */
4662 lua_pushvalue(L, 3);
4663 ret = lua_rawlen(L, -2);
4664 lua_rawseti(L, -2, ret + 1);
4665 lua_pushboolean(L, 1);
4666 return 1;
4667}
4668
4669__LJMP static int hlua_applet_http_status(lua_State *L)
4670{
4671 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4672 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004673 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004674
4675 if (status < 100 || status > 599) {
4676 lua_pushboolean(L, 0);
4677 return 1;
4678 }
4679
4680 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004681 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004682 lua_pushboolean(L, 1);
4683 return 1;
4684}
4685
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004686
Christopher Fauleta2097962019-07-15 16:25:33 +02004687__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004688{
4689 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4690 struct stream_interface *si = appctx->appctx->owner;
4691 struct channel *res = si_ic(si);
4692 struct htx *htx;
4693 struct htx_sl *sl;
4694 struct h1m h1m;
4695 const char *status, *reason;
4696 const char *name, *value;
4697 size_t nlen, vlen;
4698 unsigned int flags;
4699
4700 /* Send the message at once. */
4701 htx = htx_from_buf(&res->buf);
4702 h1m_init_res(&h1m);
4703
4704 /* Use the same http version than the request. */
4705 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4706 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4707 if (reason == NULL)
4708 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4709 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4710 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4711 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4712 }
4713 else {
4714 flags = HTX_SL_F_IS_RESP;
4715 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4716 }
4717 if (!sl) {
4718 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004719 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004720 WILL_LJMP(lua_error(L));
4721 }
4722 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4723
4724 /* Get the array associated to the field "response" in the object AppletHTTP. */
4725 lua_pushvalue(L, 0);
4726 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4727 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004728 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004729 WILL_LJMP(lua_error(L));
4730 }
4731
4732 /* Browse the list of headers. */
4733 lua_pushnil(L);
4734 while(lua_next(L, -2) != 0) {
4735 /* We expect a string as -2. */
4736 if (lua_type(L, -2) != LUA_TSTRING) {
4737 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004738 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004739 lua_typename(L, lua_type(L, -2)));
4740 WILL_LJMP(lua_error(L));
4741 }
4742 name = lua_tolstring(L, -2, &nlen);
4743
4744 /* We expect an array as -1. */
4745 if (lua_type(L, -1) != LUA_TTABLE) {
4746 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 +01004747 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004748 name,
4749 lua_typename(L, lua_type(L, -1)));
4750 WILL_LJMP(lua_error(L));
4751 }
4752
4753 /* Browse the table who is on the top of the stack. */
4754 lua_pushnil(L);
4755 while(lua_next(L, -2) != 0) {
4756 int id;
4757
4758 /* We expect a number as -2. */
4759 if (lua_type(L, -2) != LUA_TNUMBER) {
4760 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 +01004761 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004762 name,
4763 lua_typename(L, lua_type(L, -2)));
4764 WILL_LJMP(lua_error(L));
4765 }
4766 id = lua_tointeger(L, -2);
4767
4768 /* We expect a string as -2. */
4769 if (lua_type(L, -1) != LUA_TSTRING) {
4770 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 +01004771 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004772 name, id,
4773 lua_typename(L, lua_type(L, -1)));
4774 WILL_LJMP(lua_error(L));
4775 }
4776 value = lua_tolstring(L, -1, &vlen);
4777
4778 /* Simple Protocol checks. */
4779 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004780 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004781 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4782 struct ist v = ist2(value, vlen);
4783 int ret;
4784
4785 ret = h1_parse_cont_len_header(&h1m, &v);
4786 if (ret < 0) {
4787 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004788 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004789 name);
4790 WILL_LJMP(lua_error(L));
4791 }
4792 else if (ret == 0)
4793 goto next; /* Skip it */
4794 }
4795
4796 /* Add a new header */
4797 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4798 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004799 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004800 name);
4801 WILL_LJMP(lua_error(L));
4802 }
4803 next:
4804 /* Remove the array from the stack, and get next element with a remaining string. */
4805 lua_pop(L, 1);
4806 }
4807
4808 /* Remove the array from the stack, and get next element with a remaining string. */
4809 lua_pop(L, 1);
4810 }
4811
4812 if (h1m.flags & H1_MF_CHNK)
4813 h1m.flags &= ~H1_MF_CLEN;
4814 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4815 h1m.flags |= H1_MF_XFER_LEN;
4816
4817 /* Uset HTX start-line flags */
4818 if (h1m.flags & H1_MF_XFER_ENC)
4819 flags |= HTX_SL_F_XFER_ENC;
4820 if (h1m.flags & H1_MF_XFER_LEN) {
4821 flags |= HTX_SL_F_XFER_LEN;
4822 if (h1m.flags & H1_MF_CHNK)
4823 flags |= HTX_SL_F_CHNK;
4824 else if (h1m.flags & H1_MF_CLEN)
4825 flags |= HTX_SL_F_CLEN;
4826 if (h1m.body_len == 0)
4827 flags |= HTX_SL_F_BODYLESS;
4828 }
4829 sl->flags |= flags;
4830
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004831 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004832 * and the status code implies the presence of a message body, we must
4833 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004834 * for the keepalive compliance. If the applet announces a transfer-encoding
4835 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004836 */
4837 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4838 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4839 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4840 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4841 /* Add a new header */
4842 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4843 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4844 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004845 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004846 WILL_LJMP(lua_error(L));
4847 }
4848 }
4849
4850 /* Finalize headers. */
4851 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4852 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004853 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004854 WILL_LJMP(lua_error(L));
4855 }
4856
4857 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4858 b_reset(&res->buf);
4859 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4860 WILL_LJMP(lua_error(L));
4861 }
4862
4863 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004864 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004865
4866 /* Headers sent, set the flag. */
4867 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4868 return 0;
4869
4870}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004871/* We will build the status line and the headers of the HTTP response.
4872 * We will try send at once if its not possible, we give back the hand
4873 * waiting for more room.
4874 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004875__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004876{
4877 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4878 struct stream_interface *si = appctx->appctx->owner;
4879 struct channel *res = si_ic(si);
4880
4881 if (co_data(res)) {
4882 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004883 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004884 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004885 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004886}
4887
4888
Christopher Fauleta2097962019-07-15 16:25:33 +02004889__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004890{
Christopher Fauleta2097962019-07-15 16:25:33 +02004891 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004892}
4893
Christopher Fauleta2097962019-07-15 16:25:33 +02004894/*
4895 *
4896 *
4897 * Class HTTP
4898 *
4899 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004900 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004901
4902/* Returns a struct hlua_txn if the stack entry "ud" is
4903 * a class stream, otherwise it throws an error.
4904 */
4905__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004906{
Christopher Fauleta2097962019-07-15 16:25:33 +02004907 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4908}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004909
Christopher Fauleta2097962019-07-15 16:25:33 +02004910/* This function creates and push in the stack a HTTP object
4911 * according with a current TXN.
4912 */
4913static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4914{
4915 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004916
Christopher Fauleta2097962019-07-15 16:25:33 +02004917 /* Check stack size. */
4918 if (!lua_checkstack(L, 3))
4919 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004920
Christopher Fauleta2097962019-07-15 16:25:33 +02004921 /* Create the object: obj[0] = userdata.
4922 * Note that the base of the Converters object is the
4923 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004924 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004925 lua_newtable(L);
4926 htxn = lua_newuserdata(L, sizeof(*htxn));
4927 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004928
4929 htxn->s = txn->s;
4930 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004931 htxn->dir = txn->dir;
4932 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004933
4934 /* Pop a class stream metatable and affect it to the table. */
4935 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4936 lua_setmetatable(L, -2);
4937
4938 return 1;
4939}
4940
4941/* This function creates ans returns an array of HTTP headers.
4942 * This function does not fails. It is used as wrapper with the
4943 * 2 following functions.
4944 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004945__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004946{
Christopher Fauleta2097962019-07-15 16:25:33 +02004947 struct htx *htx;
4948 int32_t pos;
4949
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004950 /* Create the table. */
4951 lua_newtable(L);
4952
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004953
Christopher Fauleta2097962019-07-15 16:25:33 +02004954 htx = htxbuf(&msg->chn->buf);
4955 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4956 struct htx_blk *blk = htx_get_blk(htx, pos);
4957 enum htx_blk_type type = htx_get_blk_type(blk);
4958 struct ist n, v;
4959 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004960
Christopher Fauleta2097962019-07-15 16:25:33 +02004961 if (type == HTX_BLK_HDR) {
4962 n = htx_get_blk_name(htx,blk);
4963 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004964 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004965 else if (type == HTX_BLK_EOH)
4966 break;
4967 else
4968 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004969
Christopher Fauleta2097962019-07-15 16:25:33 +02004970 /* Check for existing entry:
4971 * assume that the table is on the top of the stack, and
4972 * push the key in the stack, the function lua_gettable()
4973 * perform the lookup.
4974 */
4975 lua_pushlstring(L, n.ptr, n.len);
4976 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004977
Christopher Fauleta2097962019-07-15 16:25:33 +02004978 switch (lua_type(L, -1)) {
4979 case LUA_TNIL:
4980 /* Table not found, create it. */
4981 lua_pop(L, 1); /* remove the nil value. */
4982 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4983 lua_newtable(L); /* create and push empty table. */
4984 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4985 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4986 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004987 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004988
Christopher Fauleta2097962019-07-15 16:25:33 +02004989 case LUA_TTABLE:
4990 /* Entry found: push the value in the table. */
4991 len = lua_rawlen(L, -1);
4992 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4993 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4994 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4995 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004996
Christopher Fauleta2097962019-07-15 16:25:33 +02004997 default:
4998 /* Other cases are errors. */
4999 hlua_pusherror(L, "internal error during the parsing of headers.");
5000 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005001 }
5002 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005003 return 1;
5004}
5005
5006__LJMP static int hlua_http_req_get_headers(lua_State *L)
5007{
5008 struct hlua_txn *htxn;
5009
5010 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5011 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5012
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005013 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005014 WILL_LJMP(lua_error(L));
5015
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005016 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005017}
5018
5019__LJMP static int hlua_http_res_get_headers(lua_State *L)
5020{
5021 struct hlua_txn *htxn;
5022
5023 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5024 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5025
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005026 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005027 WILL_LJMP(lua_error(L));
5028
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005029 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005030}
5031
5032/* This function replace full header, or just a value in
5033 * the request or in the response. It is a wrapper fir the
5034 * 4 following functions.
5035 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005036__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005037{
5038 size_t name_len;
5039 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5040 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5041 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005042 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005043 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005044
Dragan Dosen26743032019-04-30 15:54:36 +02005045 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005046 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5047
Christopher Fauleta2097962019-07-15 16:25:33 +02005048 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005049 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005050 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005051 return 0;
5052}
5053
5054__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5055{
5056 struct hlua_txn *htxn;
5057
5058 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5059 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5060
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005061 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005062 WILL_LJMP(lua_error(L));
5063
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005064 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005065}
5066
5067__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5068{
5069 struct hlua_txn *htxn;
5070
5071 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5072 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5073
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005074 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005075 WILL_LJMP(lua_error(L));
5076
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005077 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005078}
5079
5080__LJMP static int hlua_http_req_rep_val(lua_State *L)
5081{
5082 struct hlua_txn *htxn;
5083
5084 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5085 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5086
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005087 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005088 WILL_LJMP(lua_error(L));
5089
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005090 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005091}
5092
5093__LJMP static int hlua_http_res_rep_val(lua_State *L)
5094{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005095 struct hlua_txn *htxn;
5096
5097 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5098 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5099
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005100 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005101 WILL_LJMP(lua_error(L));
5102
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005103 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005104}
5105
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005106/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005107 * It is a wrapper for the 2 following functions.
5108 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005109__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005110{
5111 size_t len;
5112 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005113 struct htx *htx = htxbuf(&msg->chn->buf);
5114 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005115
Christopher Fauleta2097962019-07-15 16:25:33 +02005116 ctx.blk = NULL;
5117 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5118 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005119 return 0;
5120}
5121
5122__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5123{
5124 struct hlua_txn *htxn;
5125
5126 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5127 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5128
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005129 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005130 WILL_LJMP(lua_error(L));
5131
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005132 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005133}
5134
5135__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5136{
5137 struct hlua_txn *htxn;
5138
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005139 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005140 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5141
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005142 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005143 WILL_LJMP(lua_error(L));
5144
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005145 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005146}
5147
5148/* This function adds an header. It is a wrapper used by
5149 * the 2 following functions.
5150 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005151__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005152{
5153 size_t name_len;
5154 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5155 size_t value_len;
5156 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005157 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005158
Christopher Fauleta2097962019-07-15 16:25:33 +02005159 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5160 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005161 return 0;
5162}
5163
5164__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5165{
5166 struct hlua_txn *htxn;
5167
5168 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5169 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5170
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005171 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005172 WILL_LJMP(lua_error(L));
5173
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005174 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005175}
5176
5177__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5178{
5179 struct hlua_txn *htxn;
5180
5181 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5182 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5183
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005184 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005185 WILL_LJMP(lua_error(L));
5186
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005187 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005188}
5189
5190static int hlua_http_req_set_hdr(lua_State *L)
5191{
5192 struct hlua_txn *htxn;
5193
5194 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5195 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5196
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005197 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005198 WILL_LJMP(lua_error(L));
5199
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005200 hlua_http_del_hdr(L, &htxn->s->txn->req);
5201 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005202}
5203
5204static int hlua_http_res_set_hdr(lua_State *L)
5205{
5206 struct hlua_txn *htxn;
5207
5208 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5209 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5210
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005211 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005212 WILL_LJMP(lua_error(L));
5213
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005214 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5215 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005216}
5217
5218/* This function set the method. */
5219static int hlua_http_req_set_meth(lua_State *L)
5220{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005221 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005222 size_t name_len;
5223 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005224
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005225 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005226 WILL_LJMP(lua_error(L));
5227
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005228 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005229 return 1;
5230}
5231
5232/* This function set the method. */
5233static int hlua_http_req_set_path(lua_State *L)
5234{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005235 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005236 size_t name_len;
5237 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005238
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005239 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005240 WILL_LJMP(lua_error(L));
5241
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005242 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005243 return 1;
5244}
5245
5246/* This function set the query-string. */
5247static int hlua_http_req_set_query(lua_State *L)
5248{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005249 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005250 size_t name_len;
5251 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005252
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005253 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005254 WILL_LJMP(lua_error(L));
5255
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005256 /* Check length. */
5257 if (name_len > trash.size - 1) {
5258 lua_pushboolean(L, 0);
5259 return 1;
5260 }
5261
5262 /* Add the mark question as prefix. */
5263 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005264 trash.area[trash.data++] = '?';
5265 memcpy(trash.area + trash.data, name, name_len);
5266 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005267
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005268 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005269 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005270 return 1;
5271}
5272
5273/* This function set the uri. */
5274static int hlua_http_req_set_uri(lua_State *L)
5275{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005276 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005277 size_t name_len;
5278 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005279
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005280 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005281 WILL_LJMP(lua_error(L));
5282
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005283 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005284 return 1;
5285}
5286
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005287/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005288static int hlua_http_res_set_status(lua_State *L)
5289{
5290 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5291 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005292 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5293 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005294
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005295 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005296 WILL_LJMP(lua_error(L));
5297
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005298 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005299 return 0;
5300}
5301
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005302/*
5303 *
5304 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005305 * Class TXN
5306 *
5307 *
5308 */
5309
5310/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005311 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005312 */
5313__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5314{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005315 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005316}
5317
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005318__LJMP static int hlua_set_var(lua_State *L)
5319{
5320 struct hlua_txn *htxn;
5321 const char *name;
5322 size_t len;
5323 struct sample smp;
5324
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005325 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5326 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005327
5328 /* It is useles to retrieve the stream, but this function
5329 * runs only in a stream context.
5330 */
5331 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5332 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5333
5334 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005335 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005336 hlua_lua2smp(L, 3, &smp);
5337
5338 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005339 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005340
5341 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5342 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5343 else
5344 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5345
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005346 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005347}
5348
Christopher Faulet85d79c92016-11-09 16:54:56 +01005349__LJMP static int hlua_unset_var(lua_State *L)
5350{
5351 struct hlua_txn *htxn;
5352 const char *name;
5353 size_t len;
5354 struct sample smp;
5355
5356 MAY_LJMP(check_args(L, 2, "unset_var"));
5357
5358 /* It is useles to retrieve the stream, but this function
5359 * runs only in a stream context.
5360 */
5361 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5362 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5363
5364 /* Unset the variable. */
5365 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005366 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5367 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005368}
5369
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005370__LJMP static int hlua_get_var(lua_State *L)
5371{
5372 struct hlua_txn *htxn;
5373 const char *name;
5374 size_t len;
5375 struct sample smp;
5376
5377 MAY_LJMP(check_args(L, 2, "get_var"));
5378
5379 /* It is useles to retrieve the stream, but this function
5380 * runs only in a stream context.
5381 */
5382 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5383 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5384
Willy Tarreau7560dd42016-03-10 16:28:58 +01005385 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005386 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005387 lua_pushnil(L);
5388 return 1;
5389 }
5390
5391 return hlua_smp2lua(L, &smp);
5392}
5393
Willy Tarreau59551662015-03-10 14:23:13 +01005394__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005395{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005396 struct hlua *hlua;
5397
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005398 MAY_LJMP(check_args(L, 2, "set_priv"));
5399
Willy Tarreau87b09662015-04-03 00:22:06 +02005400 /* It is useles to retrieve the stream, but this function
5401 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005402 */
5403 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005404
5405 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005406 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005407 if (!hlua)
5408 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005409
5410 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005411 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005412
5413 /* Get and store new value. */
5414 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5415 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5416
5417 return 0;
5418}
5419
Willy Tarreau59551662015-03-10 14:23:13 +01005420__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005421{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005422 struct hlua *hlua;
5423
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005424 MAY_LJMP(check_args(L, 1, "get_priv"));
5425
Willy Tarreau87b09662015-04-03 00:22:06 +02005426 /* It is useles to retrieve the stream, but this function
5427 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005428 */
5429 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005430
5431 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005432 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005433 if (!hlua) {
5434 lua_pushnil(L);
5435 return 1;
5436 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005437
5438 /* Push configuration index in the stack. */
5439 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5440
5441 return 1;
5442}
5443
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005444/* Create stack entry containing a class TXN. This function
5445 * return 0 if the stack does not contains free slots,
5446 * otherwise it returns 1.
5447 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005448static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005449{
Willy Tarreaude491382015-04-06 11:04:28 +02005450 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005451
5452 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005453 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005454 return 0;
5455
5456 /* NOTE: The allocation never fails. The failure
5457 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005458 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005459 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005460 /* Create the object: obj[0] = userdata. */
5461 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005462 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005463 lua_rawseti(L, -2, 0);
5464
Willy Tarreaude491382015-04-06 11:04:28 +02005465 htxn->s = s;
5466 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005467 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005468 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005469
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005470 /* Create the "f" field that contains a list of fetches. */
5471 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005472 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005473 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005474 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005475
5476 /* Create the "sf" field that contains a list of stringsafe fetches. */
5477 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005478 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005479 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005480 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005481
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005482 /* Create the "c" field that contains a list of converters. */
5483 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005484 if (!hlua_converters_new(L, htxn, 0))
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 "sc" field that contains a list of stringsafe converters. */
5489 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005490 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005491 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005492 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005493
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005494 /* Create the "req" field that contains the request channel object. */
5495 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005496 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005497 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005498 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005499
5500 /* Create the "res" field that contains the response channel object. */
5501 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005502 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005503 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005504 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005505
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005506 /* Creates the HTTP object is the current proxy allows http. */
5507 lua_pushstring(L, "http");
5508 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005509 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005510 return 0;
5511 }
5512 else
5513 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005514 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005515
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005516 /* Pop a class sesison metatable and affect it to the userdata. */
5517 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5518 lua_setmetatable(L, -2);
5519
5520 return 1;
5521}
5522
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005523__LJMP static int hlua_txn_deflog(lua_State *L)
5524{
5525 const char *msg;
5526 struct hlua_txn *htxn;
5527
5528 MAY_LJMP(check_args(L, 2, "deflog"));
5529 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5530 msg = MAY_LJMP(luaL_checkstring(L, 2));
5531
5532 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5533 return 0;
5534}
5535
5536__LJMP static int hlua_txn_log(lua_State *L)
5537{
5538 int level;
5539 const char *msg;
5540 struct hlua_txn *htxn;
5541
5542 MAY_LJMP(check_args(L, 3, "log"));
5543 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5544 level = MAY_LJMP(luaL_checkinteger(L, 2));
5545 msg = MAY_LJMP(luaL_checkstring(L, 3));
5546
5547 if (level < 0 || level >= NB_LOG_LEVELS)
5548 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5549
5550 hlua_sendlog(htxn->s->be, level, msg);
5551 return 0;
5552}
5553
5554__LJMP static int hlua_txn_log_debug(lua_State *L)
5555{
5556 const char *msg;
5557 struct hlua_txn *htxn;
5558
5559 MAY_LJMP(check_args(L, 2, "Debug"));
5560 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5561 msg = MAY_LJMP(luaL_checkstring(L, 2));
5562 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5563 return 0;
5564}
5565
5566__LJMP static int hlua_txn_log_info(lua_State *L)
5567{
5568 const char *msg;
5569 struct hlua_txn *htxn;
5570
5571 MAY_LJMP(check_args(L, 2, "Info"));
5572 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5573 msg = MAY_LJMP(luaL_checkstring(L, 2));
5574 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5575 return 0;
5576}
5577
5578__LJMP static int hlua_txn_log_warning(lua_State *L)
5579{
5580 const char *msg;
5581 struct hlua_txn *htxn;
5582
5583 MAY_LJMP(check_args(L, 2, "Warning"));
5584 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5585 msg = MAY_LJMP(luaL_checkstring(L, 2));
5586 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5587 return 0;
5588}
5589
5590__LJMP static int hlua_txn_log_alert(lua_State *L)
5591{
5592 const char *msg;
5593 struct hlua_txn *htxn;
5594
5595 MAY_LJMP(check_args(L, 2, "Alert"));
5596 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5597 msg = MAY_LJMP(luaL_checkstring(L, 2));
5598 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5599 return 0;
5600}
5601
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005602__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5603{
5604 struct hlua_txn *htxn;
5605 int ll;
5606
5607 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5608 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5609 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5610
5611 if (ll < 0 || ll > 7)
5612 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5613
5614 htxn->s->logs.level = ll;
5615 return 0;
5616}
5617
5618__LJMP static int hlua_txn_set_tos(lua_State *L)
5619{
5620 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005621 int tos;
5622
5623 MAY_LJMP(check_args(L, 2, "set_tos"));
5624 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5625 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5626
Willy Tarreau1a18b542018-12-11 16:37:42 +01005627 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005628 return 0;
5629}
5630
5631__LJMP static int hlua_txn_set_mark(lua_State *L)
5632{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005633 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005634 int mark;
5635
5636 MAY_LJMP(check_args(L, 2, "set_mark"));
5637 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5638 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5639
Lukas Tribus579e3e32019-08-11 18:03:45 +02005640 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005641 return 0;
5642}
5643
Patrick Hemmer268a7072018-05-11 12:52:31 -04005644__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5645{
5646 struct hlua_txn *htxn;
5647
5648 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5649 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5650 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5651 return 0;
5652}
5653
5654__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5655{
5656 struct hlua_txn *htxn;
5657
5658 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5659 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5660 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5661 return 0;
5662}
5663
Christopher Faulet700d9e82020-01-31 12:21:52 +01005664/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005665 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005666 * message and terminate the transaction. It returns 1 on success and 0 on
5667 * error. The Reply must be on top of the stack.
5668 */
5669__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5670{
5671 struct htx *htx;
5672 struct htx_sl *sl;
5673 struct h1m h1m;
5674 const char *status, *reason, *body;
5675 size_t status_len, reason_len, body_len;
5676 int ret, code, flags;
5677
5678 code = 200;
5679 status = "200";
5680 status_len = 3;
5681 ret = lua_getfield(L, -1, "status");
5682 if (ret == LUA_TNUMBER) {
5683 code = lua_tointeger(L, -1);
5684 status = lua_tolstring(L, -1, &status_len);
5685 }
5686 lua_pop(L, 1);
5687
5688 reason = http_get_reason(code);
5689 reason_len = strlen(reason);
5690 ret = lua_getfield(L, -1, "reason");
5691 if (ret == LUA_TSTRING)
5692 reason = lua_tolstring(L, -1, &reason_len);
5693 lua_pop(L, 1);
5694
5695 body = NULL;
5696 body_len = 0;
5697 ret = lua_getfield(L, -1, "body");
5698 if (ret == LUA_TSTRING)
5699 body = lua_tolstring(L, -1, &body_len);
5700 lua_pop(L, 1);
5701
5702 /* Prepare the response before inserting the headers */
5703 h1m_init_res(&h1m);
5704 htx = htx_from_buf(&s->res.buf);
5705 channel_htx_truncate(&s->res, htx);
5706 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5707 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5708 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5709 ist2(status, status_len), ist2(reason, reason_len));
5710 }
5711 else {
5712 flags = HTX_SL_F_IS_RESP;
5713 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5714 ist2(status, status_len), ist2(reason, reason_len));
5715 }
5716 if (!sl)
5717 goto fail;
5718 sl->info.res.status = code;
5719
5720 /* Push in the stack the "headers" entry. */
5721 ret = lua_getfield(L, -1, "headers");
5722 if (ret != LUA_TTABLE)
5723 goto skip_headers;
5724
5725 lua_pushnil(L);
5726 while (lua_next(L, -2) != 0) {
5727 struct ist name, value;
5728 const char *n, *v;
5729 size_t nlen, vlen;
5730
5731 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5732 /* Skip element if the key is not a string or if the value is not a table */
5733 goto next_hdr;
5734 }
5735
5736 n = lua_tolstring(L, -2, &nlen);
5737 name = ist2(n, nlen);
5738 if (isteqi(name, ist("content-length"))) {
5739 /* Always skip content-length header. It will be added
5740 * later with the correct len
5741 */
5742 goto next_hdr;
5743 }
5744
5745 /* Loop on header's values */
5746 lua_pushnil(L);
5747 while (lua_next(L, -2)) {
5748 if (!lua_isstring(L, -1)) {
5749 /* Skip the value if it is not a string */
5750 goto next_value;
5751 }
5752
5753 v = lua_tolstring(L, -1, &vlen);
5754 value = ist2(v, vlen);
5755
5756 if (isteqi(name, ist("transfer-encoding")))
5757 h1_parse_xfer_enc_header(&h1m, value);
5758 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5759 goto fail;
5760
5761 next_value:
5762 lua_pop(L, 1);
5763 }
5764
5765 next_hdr:
5766 lua_pop(L, 1);
5767 }
5768 skip_headers:
5769 lua_pop(L, 1);
5770
5771 /* Update h1m flags: CLEN is set if CHNK is not present */
5772 if (!(h1m.flags & H1_MF_CHNK)) {
5773 const char *clen = ultoa(body_len);
5774
5775 h1m.flags |= H1_MF_CLEN;
5776 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5777 goto fail;
5778 }
5779 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5780 h1m.flags |= H1_MF_XFER_LEN;
5781
5782 /* Update HTX start-line flags */
5783 if (h1m.flags & H1_MF_XFER_ENC)
5784 flags |= HTX_SL_F_XFER_ENC;
5785 if (h1m.flags & H1_MF_XFER_LEN) {
5786 flags |= HTX_SL_F_XFER_LEN;
5787 if (h1m.flags & H1_MF_CHNK)
5788 flags |= HTX_SL_F_CHNK;
5789 else if (h1m.flags & H1_MF_CLEN)
5790 flags |= HTX_SL_F_CLEN;
5791 if (h1m.body_len == 0)
5792 flags |= HTX_SL_F_BODYLESS;
5793 }
5794 sl->flags |= flags;
5795
5796
5797 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005798 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005799 goto fail;
5800
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005801 htx->flags |= HTX_FL_EOM;
5802
Christopher Faulet700d9e82020-01-31 12:21:52 +01005803 /* Now, forward the response and terminate the transaction */
5804 s->txn->status = code;
5805 htx_to_buf(htx, &s->res.buf);
5806 if (!http_forward_proxy_resp(s, 1))
5807 goto fail;
5808
5809 return 1;
5810
5811 fail:
5812 channel_htx_truncate(&s->res, htx);
5813 return 0;
5814}
5815
5816/* Terminate a transaction if called from a lua action. For TCP streams,
5817 * processing is just aborted. Nothing is returned to the client and all
5818 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5819 * is forwarded to the client before terminating the transaction. On success,
5820 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5821 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5822 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005823 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005824__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005825{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005826 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005827 struct stream *s;
5828 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005829
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005830 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005831
Christopher Faulet700d9e82020-01-31 12:21:52 +01005832 /* If the flags NOTERM is set, we cannot terminate the session, so we
5833 * just end the execution of the current lua code. */
5834 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005835 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005836
Christopher Faulet700d9e82020-01-31 12:21:52 +01005837 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005838 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005839 struct channel *req = &s->req;
5840 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005841
Christopher Faulet700d9e82020-01-31 12:21:52 +01005842 channel_auto_read(req);
5843 channel_abort(req);
5844 channel_auto_close(req);
5845 channel_erase(req);
5846
5847 res->wex = tick_add_ifset(now_ms, res->wto);
5848 channel_auto_read(res);
5849 channel_auto_close(res);
5850 channel_shutr_now(res);
5851
5852 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5853 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005854 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005855
Christopher Faulet700d9e82020-01-31 12:21:52 +01005856 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5857 /* No reply or invalid reply */
5858 s->txn->status = 0;
5859 http_reply_and_close(s, 0, NULL);
5860 }
5861 else {
5862 /* Remove extra args to have the reply on top of the stack */
5863 if (lua_gettop(L) > 2)
5864 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005865
Christopher Faulet700d9e82020-01-31 12:21:52 +01005866 if (!hlua_txn_forward_reply(L, s)) {
5867 if (!(s->flags & SF_ERR_MASK))
5868 s->flags |= SF_ERR_PRXCOND;
5869 lua_pushinteger(L, ACT_RET_ERR);
5870 WILL_LJMP(hlua_done(L));
5871 return 0; /* Never reached */
5872 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005873 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005874
Christopher Faulet700d9e82020-01-31 12:21:52 +01005875 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5876 if (htxn->dir == SMP_OPT_DIR_REQ) {
5877 /* let's log the request time */
5878 s->logs.tv_request = now;
5879 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5880 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5881 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005882
Christopher Faulet700d9e82020-01-31 12:21:52 +01005883 done:
5884 if (!(s->flags & SF_ERR_MASK))
5885 s->flags |= SF_ERR_LOCAL;
5886 if (!(s->flags & SF_FINST_MASK))
5887 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005888
Christopher Faulet4ad73102020-03-05 11:07:31 +01005889 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005890 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005891 return 0;
5892}
5893
Christopher Faulet700d9e82020-01-31 12:21:52 +01005894/*
5895 *
5896 *
5897 * Class REPLY
5898 *
5899 *
5900 */
5901
5902/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5903 * free slots, the function fails and returns 0;
5904 */
5905static int hlua_txn_reply_new(lua_State *L)
5906{
5907 struct hlua_txn *htxn;
5908 const char *reason, *body = NULL;
5909 int ret, status;
5910
5911 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005912 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005913 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5914 WILL_LJMP(lua_error(L));
5915 }
5916
5917 /* Default value */
5918 status = 200;
5919 reason = http_get_reason(status);
5920
5921 if (lua_istable(L, 2)) {
5922 /* load status and reason from the table argument at index 2 */
5923 ret = lua_getfield(L, 2, "status");
5924 if (ret == LUA_TNIL)
5925 goto reason;
5926 else if (ret != LUA_TNUMBER) {
5927 /* invalid status: ignore the reason */
5928 goto body;
5929 }
5930 status = lua_tointeger(L, -1);
5931
5932 reason:
5933 lua_pop(L, 1); /* restore the stack: remove status */
5934 ret = lua_getfield(L, 2, "reason");
5935 if (ret == LUA_TSTRING)
5936 reason = lua_tostring(L, -1);
5937
5938 body:
5939 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5940 ret = lua_getfield(L, 2, "body");
5941 if (ret == LUA_TSTRING)
5942 body = lua_tostring(L, -1);
5943 lua_pop(L, 1); /* restore the stack: remove body */
5944 }
5945
5946 /* Create the Reply table */
5947 lua_newtable(L);
5948
5949 /* Add status element */
5950 lua_pushstring(L, "status");
5951 lua_pushinteger(L, status);
5952 lua_settable(L, -3);
5953
5954 /* Add reason element */
5955 reason = http_get_reason(status);
5956 lua_pushstring(L, "reason");
5957 lua_pushstring(L, reason);
5958 lua_settable(L, -3);
5959
5960 /* Add body element, nil if undefined */
5961 lua_pushstring(L, "body");
5962 if (body)
5963 lua_pushstring(L, body);
5964 else
5965 lua_pushnil(L);
5966 lua_settable(L, -3);
5967
5968 /* Add headers element */
5969 lua_pushstring(L, "headers");
5970 lua_newtable(L);
5971
5972 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5973 if (lua_istable(L, 2)) {
5974 /* load headers from the table argument at index 2. If it is a table, copy it. */
5975 ret = lua_getfield(L, 2, "headers");
5976 if (ret == LUA_TTABLE) {
5977 /* stack: [ ... <headers:table>, <table> ] */
5978 lua_pushnil(L);
5979 while (lua_next(L, -2) != 0) {
5980 /* stack: [ ... <headers:table>, <table>, k, v] */
5981 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5982 /* invalid value type, skip it */
5983 lua_pop(L, 1);
5984 continue;
5985 }
5986
5987
5988 /* Duplicate the key and swap it with the value. */
5989 lua_pushvalue(L, -2);
5990 lua_insert(L, -2);
5991 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5992
5993 lua_newtable(L);
5994 lua_insert(L, -2);
5995 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5996
5997 if (lua_isstring(L, -1)) {
5998 /* push the value in the inner table */
5999 lua_rawseti(L, -2, 1);
6000 }
6001 else { /* table */
6002 lua_pushnil(L);
6003 while (lua_next(L, -2) != 0) {
6004 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6005 if (!lua_isstring(L, -1)) {
6006 /* invalid value type, skip it*/
6007 lua_pop(L, 1);
6008 continue;
6009 }
6010 /* push the value in the inner table */
6011 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6012 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6013 }
6014 lua_pop(L, 1);
6015 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6016 }
6017
6018 /* push (k,v) on the stack in the headers table:
6019 * stack: [ ... <headers:table>, <table>, k, k, v ]
6020 */
6021 lua_settable(L, -5);
6022 /* stack: [ ... <headers:table>, <table>, k ] */
6023 }
6024 }
6025 lua_pop(L, 1);
6026 }
6027 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6028 lua_settable(L, -3);
6029 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6030
6031 /* Pop a class sesison metatable and affect it to the userdata. */
6032 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6033 lua_setmetatable(L, -2);
6034 return 1;
6035}
6036
6037/* Set the reply status code, and optionally the reason. If no reason is
6038 * provided, the default one corresponding to the status code is used.
6039 */
6040__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6041{
6042 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6043 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6044
6045 /* First argument (self) must be a table */
6046 luaL_checktype(L, 1, LUA_TTABLE);
6047
6048 if (status < 100 || status > 599) {
6049 lua_pushboolean(L, 0);
6050 return 1;
6051 }
6052 if (!reason)
6053 reason = http_get_reason(status);
6054
6055 lua_pushinteger(L, status);
6056 lua_setfield(L, 1, "status");
6057
6058 lua_pushstring(L, reason);
6059 lua_setfield(L, 1, "reason");
6060
6061 lua_pushboolean(L, 1);
6062 return 1;
6063}
6064
6065/* Add a header into the reply object. Each header name is associated to an
6066 * array of values in the "headers" table. If the header name is not found, a
6067 * new entry is created.
6068 */
6069__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6070{
6071 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6072 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6073 int ret;
6074
6075 /* First argument (self) must be a table */
6076 luaL_checktype(L, 1, LUA_TTABLE);
6077
6078 /* Push in the stack the "headers" entry. */
6079 ret = lua_getfield(L, 1, "headers");
6080 if (ret != LUA_TTABLE) {
6081 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6082 WILL_LJMP(lua_error(L));
6083 }
6084
6085 /* check if the header is already registered. If not, register it. */
6086 ret = lua_getfield(L, -1, name);
6087 if (ret == LUA_TNIL) {
6088 /* Entry not found. */
6089 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6090
6091 /* Insert the new header name in the array in the top of the stack.
6092 * It left the new array in the top of the stack.
6093 */
6094 lua_newtable(L);
6095 lua_pushstring(L, name);
6096 lua_pushvalue(L, -2);
6097 lua_settable(L, -4);
6098 }
6099 else if (ret != LUA_TTABLE) {
6100 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6101 WILL_LJMP(lua_error(L));
6102 }
6103
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006104 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006105 * the header value as new entry.
6106 */
6107 lua_pushstring(L, value);
6108 ret = lua_rawlen(L, -2);
6109 lua_rawseti(L, -2, ret + 1);
6110
6111 lua_pushboolean(L, 1);
6112 return 1;
6113}
6114
6115/* Remove all occurrences of a given header name. */
6116__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6117{
6118 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6119 int ret;
6120
6121 /* First argument (self) must be a table */
6122 luaL_checktype(L, 1, LUA_TTABLE);
6123
6124 /* Push in the stack the "headers" entry. */
6125 ret = lua_getfield(L, 1, "headers");
6126 if (ret != LUA_TTABLE) {
6127 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6128 WILL_LJMP(lua_error(L));
6129 }
6130
6131 lua_pushstring(L, name);
6132 lua_pushnil(L);
6133 lua_settable(L, -3);
6134
6135 lua_pushboolean(L, 1);
6136 return 1;
6137}
6138
6139/* Set the reply's body. Overwrite any existing entry. */
6140__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6141{
6142 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6143
6144 /* First argument (self) must be a table */
6145 luaL_checktype(L, 1, LUA_TTABLE);
6146
6147 lua_pushstring(L, payload);
6148 lua_setfield(L, 1, "body");
6149
6150 lua_pushboolean(L, 1);
6151 return 1;
6152}
6153
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006154__LJMP static int hlua_log(lua_State *L)
6155{
6156 int level;
6157 const char *msg;
6158
6159 MAY_LJMP(check_args(L, 2, "log"));
6160 level = MAY_LJMP(luaL_checkinteger(L, 1));
6161 msg = MAY_LJMP(luaL_checkstring(L, 2));
6162
6163 if (level < 0 || level >= NB_LOG_LEVELS)
6164 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6165
6166 hlua_sendlog(NULL, level, msg);
6167 return 0;
6168}
6169
6170__LJMP static int hlua_log_debug(lua_State *L)
6171{
6172 const char *msg;
6173
6174 MAY_LJMP(check_args(L, 1, "debug"));
6175 msg = MAY_LJMP(luaL_checkstring(L, 1));
6176 hlua_sendlog(NULL, LOG_DEBUG, msg);
6177 return 0;
6178}
6179
6180__LJMP static int hlua_log_info(lua_State *L)
6181{
6182 const char *msg;
6183
6184 MAY_LJMP(check_args(L, 1, "info"));
6185 msg = MAY_LJMP(luaL_checkstring(L, 1));
6186 hlua_sendlog(NULL, LOG_INFO, msg);
6187 return 0;
6188}
6189
6190__LJMP static int hlua_log_warning(lua_State *L)
6191{
6192 const char *msg;
6193
6194 MAY_LJMP(check_args(L, 1, "warning"));
6195 msg = MAY_LJMP(luaL_checkstring(L, 1));
6196 hlua_sendlog(NULL, LOG_WARNING, msg);
6197 return 0;
6198}
6199
6200__LJMP static int hlua_log_alert(lua_State *L)
6201{
6202 const char *msg;
6203
6204 MAY_LJMP(check_args(L, 1, "alert"));
6205 msg = MAY_LJMP(luaL_checkstring(L, 1));
6206 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006207 return 0;
6208}
6209
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006210__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006211{
6212 int wakeup_ms = lua_tointeger(L, -1);
6213 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006214 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006215 return 0;
6216}
6217
6218__LJMP static int hlua_sleep(lua_State *L)
6219{
6220 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006221 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006222
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006223 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006224
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006225 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006226 wakeup_ms = tick_add(now_ms, delay);
6227 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006228
Willy Tarreau9635e032018-10-16 17:52:55 +02006229 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006230 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006231}
6232
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006233__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006234{
6235 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006236 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006237
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006238 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006239
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006240 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006241 wakeup_ms = tick_add(now_ms, delay);
6242 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006243
Willy Tarreau9635e032018-10-16 17:52:55 +02006244 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006245 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006246}
6247
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006248/* This functionis an LUA binding. it permits to give back
6249 * the hand at the HAProxy scheduler. It is used when the
6250 * LUA processing consumes a lot of time.
6251 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006252__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006253{
6254 return 0;
6255}
6256
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006257__LJMP static int hlua_yield(lua_State *L)
6258{
Willy Tarreau9635e032018-10-16 17:52:55 +02006259 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006260 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006261}
6262
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006263/* This function change the nice of the currently executed
6264 * task. It is used set low or high priority at the current
6265 * task.
6266 */
Willy Tarreau59551662015-03-10 14:23:13 +01006267__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006268{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006269 struct hlua *hlua;
6270 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006271
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006272 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006273 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006274
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006275 /* Get hlua struct, or NULL if we execute from main lua state */
6276 hlua = hlua_gethlua(L);
6277
6278 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006279 if (!hlua || !hlua->task)
6280 return 0;
6281
6282 if (nice < -1024)
6283 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006284 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006285 nice = 1024;
6286
6287 hlua->task->nice = nice;
6288 return 0;
6289}
6290
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006291/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006292 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6293 * return an E_AGAIN signal, the emmiter of this signal must set a
6294 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006295 *
6296 * Task wrapper are longjmp safe because the only one Lua code
6297 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006298 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006299struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006300{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006301 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006302 enum hlua_exec status;
6303
Christopher Faulet5bc99722018-04-25 10:34:45 +02006304 if (task->thread_mask == MAX_THREADS_MASK)
6305 task_set_affinity(task, tid_bit);
6306
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006307 /* If it is the first call to the task, we must initialize the
6308 * execution timeouts.
6309 */
6310 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006311 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006312
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006313 /* Execute the Lua code. */
6314 status = hlua_ctx_resume(hlua, 1);
6315
6316 switch (status) {
6317 /* finished or yield */
6318 case HLUA_E_OK:
6319 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006320 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006321 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006322 break;
6323
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006324 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006325 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006326 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006327 break;
6328
6329 /* finished with error. */
6330 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006331 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006332 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006333 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006334 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006335 break;
6336
6337 case HLUA_E_ERR:
6338 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006339 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006340 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006341 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006342 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006343 break;
6344 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006345 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006346}
6347
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006348/* This function is an LUA binding that register LUA function to be
6349 * executed after the HAProxy configuration parsing and before the
6350 * HAProxy scheduler starts. This function expect only one LUA
6351 * argument that is a function. This function returns nothing, but
6352 * throws if an error is encountered.
6353 */
6354__LJMP static int hlua_register_init(lua_State *L)
6355{
6356 struct hlua_init_function *init;
6357 int ref;
6358
6359 MAY_LJMP(check_args(L, 1, "register_init"));
6360
6361 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6362
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006363 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006364 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006365 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006366
6367 init->function_ref = ref;
Thierry Fournierc7492592020-11-28 23:57:24 +01006368 LIST_ADDQ(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006369 return 0;
6370}
6371
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006372/* This functio is an LUA binding. It permits to register a task
6373 * executed in parallel of the main HAroxy activity. The task is
6374 * created and it is set in the HAProxy scheduler. It can be called
6375 * from the "init" section, "post init" or during the runtime.
6376 *
6377 * Lua prototype:
6378 *
6379 * <none> core.register_task(<function>)
6380 */
6381static int hlua_register_task(lua_State *L)
6382{
6383 struct hlua *hlua;
6384 struct task *task;
6385 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006386 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006387
6388 MAY_LJMP(check_args(L, 1, "register_task"));
6389
6390 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6391
Thierry Fournier75fc0292020-11-28 13:18:56 +01006392 /* Get the reference state. If the reference is NULL, L is the master
6393 * state, otherwise hlua->T is.
6394 */
6395 hlua = hlua_gethlua(L);
6396 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006397 /* we are in runtime processing */
6398 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006399 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006400 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006401 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006402
Willy Tarreaubafbe012017-11-24 17:34:44 +01006403 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006404 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006405 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006406 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006407
Thierry Fournier59f11be2020-11-29 00:37:41 +01006408 /* We are in the common lua state, execute the task anywhere,
6409 * otherwise, inherit the current thread identifier
6410 */
6411 if (state_id == 0)
6412 task = task_new(MAX_THREADS_MASK);
6413 else
6414 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006415 if (!task)
6416 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6417
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006418 task->context = hlua;
6419 task->process = hlua_process_task;
6420
Thierry Fournier021d9862020-11-28 23:42:03 +01006421 if (!hlua_ctx_init(hlua, state_id, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006422 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006423
6424 /* Restore the function in the stack. */
6425 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6426 hlua->nargs = 0;
6427
6428 /* Schedule task. */
6429 task_schedule(task, now_ms);
6430
6431 return 0;
6432}
6433
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006434/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6435 * doesn't allow "yield" functions because the HAProxy engine cannot
6436 * resume converters.
6437 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006438static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006439{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006440 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006441 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006442 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006443
Willy Tarreaube508f12016-03-10 11:47:01 +01006444 if (!stream)
6445 return 0;
6446
Willy Tarreau87b09662015-04-03 00:22:06 +02006447 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006448 * Lua context can be not initialized. This behavior
6449 * permits to save performances because a systematic
6450 * Lua initialization cause 5% performances loss.
6451 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006452 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006453 struct hlua *hlua;
6454
6455 hlua = pool_alloc(pool_head_hlua);
6456 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006457 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6458 return 0;
6459 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006460 HLUA_INIT(hlua);
6461 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006462 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006463 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6464 return 0;
6465 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006466 }
6467
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006468 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006469 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006470
6471 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006472 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006473 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6474 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006475 else
6476 error = "critical error";
6477 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006478 return 0;
6479 }
6480
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006481 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006482 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006483 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006484 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006485 return 0;
6486 }
6487
6488 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006489 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006490
6491 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006492 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006493 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006494 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006495 return 0;
6496 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006497 hlua_smp2lua(stream->hlua->T, smp);
6498 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006499
6500 /* push keywords in the stack. */
6501 if (arg_p) {
6502 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006503 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006504 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006505 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006506 return 0;
6507 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006508 hlua_arg2lua(stream->hlua->T, arg_p);
6509 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006510 }
6511 }
6512
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006513 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006514 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006515
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006516 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006517 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006518 }
6519
6520 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006521 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006522 /* finished. */
6523 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006524 /* If the stack is empty, the function fails. */
6525 if (lua_gettop(stream->hlua->T) <= 0)
6526 return 0;
6527
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006528 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006529 hlua_lua2smp(stream->hlua->T, -1, smp);
6530 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006531 return 1;
6532
6533 /* yield. */
6534 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006535 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006536 return 0;
6537
6538 /* finished with error. */
6539 case HLUA_E_ERRMSG:
6540 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006541 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006542 fcn->name, lua_tostring(stream->hlua->T, -1));
6543 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006544 return 0;
6545
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006546 case HLUA_E_ETMOUT:
6547 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6548 return 0;
6549
6550 case HLUA_E_NOMEM:
6551 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6552 return 0;
6553
6554 case HLUA_E_YIELD:
6555 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6556 return 0;
6557
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006558 case HLUA_E_ERR:
6559 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006560 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006561 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006562
6563 default:
6564 return 0;
6565 }
6566}
6567
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006568/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6569 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006570 * resume sample-fetches. This function will be called by the sample
6571 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006572 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006573static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6574 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006575{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006576 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006577 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006578 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006579 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006580
Willy Tarreaube508f12016-03-10 11:47:01 +01006581 if (!stream)
6582 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006583
Willy Tarreau87b09662015-04-03 00:22:06 +02006584 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006585 * Lua context can be not initialized. This behavior
6586 * permits to save performances because a systematic
6587 * Lua initialization cause 5% performances loss.
6588 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006589 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006590 struct hlua *hlua;
6591
6592 hlua = pool_alloc(pool_head_hlua);
6593 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006594 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6595 return 0;
6596 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006597 hlua->T = NULL;
6598 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006599 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006600 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6601 return 0;
6602 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006603 }
6604
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006605 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006606 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006607
6608 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006609 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6611 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006612 else
6613 error = "critical error";
6614 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006615 return 0;
6616 }
6617
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006618 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006619 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006620 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006621 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006622 return 0;
6623 }
6624
6625 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006626 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006627
6628 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006629 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006630 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006631 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006632 return 0;
6633 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006634 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006635
6636 /* push keywords in the stack. */
6637 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6638 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006639 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006640 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006641 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006642 return 0;
6643 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006644 hlua_arg2lua(stream->hlua->T, arg_p);
6645 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006646 }
6647
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006648 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006649 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006650
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006651 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006652 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006653 }
6654
6655 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006656 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006657 /* finished. */
6658 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006659 /* If the stack is empty, the function fails. */
6660 if (lua_gettop(stream->hlua->T) <= 0)
6661 return 0;
6662
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006663 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006664 hlua_lua2smp(stream->hlua->T, -1, smp);
6665 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006666
6667 /* Set the end of execution flag. */
6668 smp->flags &= ~SMP_F_MAY_CHANGE;
6669 return 1;
6670
6671 /* yield. */
6672 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006673 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006674 return 0;
6675
6676 /* finished with error. */
6677 case HLUA_E_ERRMSG:
6678 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006679 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006680 fcn->name, lua_tostring(stream->hlua->T, -1));
6681 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006682 return 0;
6683
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006684 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006685 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6686 return 0;
6687
6688 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006689 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6690 return 0;
6691
6692 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006693 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6694 return 0;
6695
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006696 case HLUA_E_ERR:
6697 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006698 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006699 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006700
6701 default:
6702 return 0;
6703 }
6704}
6705
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006706/* This function is an LUA binding used for registering
6707 * "sample-conv" functions. It expects a converter name used
6708 * in the haproxy configuration file, and an LUA function.
6709 */
6710__LJMP static int hlua_register_converters(lua_State *L)
6711{
6712 struct sample_conv_kw_list *sck;
6713 const char *name;
6714 int ref;
6715 int len;
6716 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006717 struct sample_conv *sc;
6718 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006719
6720 MAY_LJMP(check_args(L, 2, "register_converters"));
6721
6722 /* First argument : converter name. */
6723 name = MAY_LJMP(luaL_checkstring(L, 1));
6724
6725 /* Second argument : lua function. */
6726 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6727
Thierry Fournierf67442e2020-11-28 20:41:07 +01006728 /* Check if the converter is already registered */
6729 trash = get_trash_chunk();
6730 chunk_printf(trash, "lua.%s", name);
6731 sc = find_sample_conv(trash->area, trash->data);
6732 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006733 fcn = sc->private;
6734 if (fcn->function_ref[hlua_state_id] != -1) {
6735 ha_warning("Trying to register converter 'lua.%s' more than once. "
6736 "This will become a hard error in version 2.5.\n", name);
6737 }
6738 fcn->function_ref[hlua_state_id] = ref;
6739 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006740 }
6741
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006742 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006743 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006744 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006745 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006746 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006747 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006748 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006749
6750 /* Fill fcn. */
6751 fcn->name = strdup(name);
6752 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006753 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01006754 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006755
6756 /* List head */
6757 sck->list.n = sck->list.p = NULL;
6758
6759 /* converter keyword. */
6760 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006761 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006762 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006763 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006764
6765 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6766 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006767 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 +01006768 sck->kw[0].val_args = NULL;
6769 sck->kw[0].in_type = SMP_T_STR;
6770 sck->kw[0].out_type = SMP_T_STR;
6771 sck->kw[0].private = fcn;
6772
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006773 /* Register this new converter */
6774 sample_register_convs(sck);
6775
6776 return 0;
6777}
6778
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006779/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006780 * "sample-fetch" functions. It expects a converter name used
6781 * in the haproxy configuration file, and an LUA function.
6782 */
6783__LJMP static int hlua_register_fetches(lua_State *L)
6784{
6785 const char *name;
6786 int ref;
6787 int len;
6788 struct sample_fetch_kw_list *sfk;
6789 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006790 struct sample_fetch *sf;
6791 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006792
6793 MAY_LJMP(check_args(L, 2, "register_fetches"));
6794
6795 /* First argument : sample-fetch name. */
6796 name = MAY_LJMP(luaL_checkstring(L, 1));
6797
6798 /* Second argument : lua function. */
6799 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6800
Thierry Fournierf67442e2020-11-28 20:41:07 +01006801 /* Check if the sample-fetch is already registered */
6802 trash = get_trash_chunk();
6803 chunk_printf(trash, "lua.%s", name);
6804 sf = find_sample_fetch(trash->area, trash->data);
6805 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006806 fcn = sf->private;
6807 if (fcn->function_ref[hlua_state_id] != -1) {
6808 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6809 "This will become a hard error in version 2.5.\n", name);
6810 }
6811 fcn->function_ref[hlua_state_id] = ref;
6812 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006813 }
6814
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006815 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006816 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006817 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006818 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006819 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006820 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006821 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006822
6823 /* Fill fcn. */
6824 fcn->name = strdup(name);
6825 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006826 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01006827 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006828
6829 /* List head */
6830 sfk->list.n = sfk->list.p = NULL;
6831
6832 /* sample-fetch keyword. */
6833 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006834 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006835 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006836 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006837
6838 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6839 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006840 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 +01006841 sfk->kw[0].val_args = NULL;
6842 sfk->kw[0].out_type = SMP_T_STR;
6843 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6844 sfk->kw[0].val = 0;
6845 sfk->kw[0].private = fcn;
6846
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006847 /* Register this new fetch. */
6848 sample_register_fetches(sfk);
6849
6850 return 0;
6851}
6852
Christopher Faulet501465d2020-02-26 14:54:16 +01006853/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006854 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006855__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006856{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006857 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006858 unsigned int delay;
6859 unsigned int wakeup_ms;
6860
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006861 /* Get hlua struct, or NULL if we execute from main lua state */
6862 hlua = hlua_gethlua(L);
6863 if (!hlua) {
6864 return 0;
6865 }
6866
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006867 MAY_LJMP(check_args(L, 1, "wake_time"));
6868
6869 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6870 wakeup_ms = tick_add(now_ms, delay);
6871 hlua->wake_time = wakeup_ms;
6872 return 0;
6873}
6874
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006875/* This function is a wrapper to execute each LUA function declared as an action
6876 * wrapper during the initialisation period. This function may return any
6877 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6878 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6879 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006880 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006881static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006882 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006883{
6884 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006885 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006886 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006887 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006888
6889 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006890 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6891 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6892 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6893 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006894 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006895 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006896 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006897 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006898
Willy Tarreau87b09662015-04-03 00:22:06 +02006899 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006900 * Lua context can be not initialized. This behavior
6901 * permits to save performances because a systematic
6902 * Lua initialization cause 5% performances loss.
6903 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006904 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006905 struct hlua *hlua;
6906
6907 hlua = pool_alloc(pool_head_hlua);
6908 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006909 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 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006913 HLUA_INIT(hlua);
6914 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006915 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 +01006916 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006917 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006918 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006919 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006920 }
6921
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006922 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006923 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006924
6925 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006926 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006927 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6928 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006929 else
6930 error = "critical error";
6931 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006932 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006933 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006934 }
6935
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006936 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006937 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006938 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006939 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006940 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006941 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006942 }
6943
6944 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006945 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 +01006946
Willy Tarreau87b09662015-04-03 00:22:06 +02006947 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006948 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006949 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006950 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006951 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006952 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006953 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006954 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006955
6956 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006957 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006958 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006959 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006960 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006961 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006962 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006963 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006964 lua_pushstring(s->hlua->T, *arg);
6965 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006966 }
6967
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006968 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006969 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006970
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006971 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006972 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006973 }
6974
6975 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006976 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006977 /* finished. */
6978 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006979 /* Catch the return value */
6980 if (lua_gettop(s->hlua->T) > 0)
6981 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006982
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006983 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006984 if (act_ret == ACT_RET_YIELD) {
6985 if (flags & ACT_OPT_FINAL)
6986 goto err_yield;
6987
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006988 if (dir == SMP_OPT_DIR_REQ)
6989 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6990 s->hlua->wake_time);
6991 else
6992 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6993 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006994 }
6995 goto end;
6996
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006997 /* yield. */
6998 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006999 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007000 if (dir == SMP_OPT_DIR_REQ)
7001 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7002 s->hlua->wake_time);
7003 else
7004 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7005 s->hlua->wake_time);
7006
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007007 /* Some actions can be wake up when a "write" event
7008 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007009 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007010 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007011 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007012 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007013 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007014 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007015 act_ret = ACT_RET_YIELD;
7016 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007017
7018 /* finished with error. */
7019 case HLUA_E_ERRMSG:
7020 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007021 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007022 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007023 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007024 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007025
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007026 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007027 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007028 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007029
7030 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007031 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007032 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007033
7034 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007035 err_yield:
7036 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007037 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007038 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007039 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007040
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007041 case HLUA_E_ERR:
7042 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007043 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007044 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007045
7046 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007047 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007048 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007049
7050 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007051 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007052 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007053 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007054}
7055
Willy Tarreau144f84a2021-03-02 16:09:26 +01007056struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007057{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007058 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007059
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007060 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007061 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007062 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007063}
7064
7065static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7066{
7067 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007068 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007069 struct task *task;
7070 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007071 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007072
Willy Tarreaubafbe012017-11-24 17:34:44 +01007073 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007074 if (!hlua) {
7075 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007076 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007077 return 0;
7078 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007079 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007080 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007081 ctx->ctx.hlua_apptcp.flags = 0;
7082
7083 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007084 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007085 if (!task) {
7086 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007087 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007088 return 0;
7089 }
7090 task->nice = 0;
7091 task->context = ctx;
7092 task->process = hlua_applet_wakeup;
7093 ctx->ctx.hlua_apptcp.task = task;
7094
7095 /* In the execution wrappers linked with a stream, the
7096 * Lua context can be not initialized. This behavior
7097 * permits to save performances because a systematic
7098 * Lua initialization cause 5% performances loss.
7099 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007100 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 +02007101 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007102 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007103 return 0;
7104 }
7105
7106 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007107 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007108
7109 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007110 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007111 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7112 error = lua_tostring(hlua->T, -1);
7113 else
7114 error = "critical error";
7115 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007116 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007117 return 0;
7118 }
7119
7120 /* Check stack available size. */
7121 if (!lua_checkstack(hlua->T, 1)) {
7122 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007123 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007124 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007125 return 0;
7126 }
7127
7128 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007129 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007130
7131 /* Create and and push object stream in the stack. */
7132 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7133 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007134 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007135 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007136 return 0;
7137 }
7138 hlua->nargs = 1;
7139
7140 /* push keywords in the stack. */
7141 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7142 if (!lua_checkstack(hlua->T, 1)) {
7143 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007144 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007145 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007146 return 0;
7147 }
7148 lua_pushstring(hlua->T, *arg);
7149 hlua->nargs++;
7150 }
7151
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007152 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007153
7154 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007155 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007156 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007157
7158 return 1;
7159}
7160
Willy Tarreau60409db2019-08-21 14:14:50 +02007161void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007162{
7163 struct stream_interface *si = ctx->owner;
7164 struct stream *strm = si_strm(si);
7165 struct channel *res = si_ic(si);
7166 struct act_rule *rule = ctx->rule;
7167 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007168 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007169
7170 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007171 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7172 /* eat the whole request */
7173 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007174 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007175 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007176
7177 /* If the stream is disconnect or closed, ldo nothing. */
7178 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7179 return;
7180
7181 /* Execute the function. */
7182 switch (hlua_ctx_resume(hlua, 1)) {
7183 /* finished. */
7184 case HLUA_E_OK:
7185 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7186
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007187 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007188 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007189 res->flags |= CF_READ_NULL;
7190 si_shutr(si);
7191 return;
7192
7193 /* yield. */
7194 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007195 if (hlua->wake_time != TICK_ETERNITY)
7196 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007197 return;
7198
7199 /* finished with error. */
7200 case HLUA_E_ERRMSG:
7201 /* Display log. */
7202 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007203 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007204 lua_pop(hlua->T, 1);
7205 goto error;
7206
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007207 case HLUA_E_ETMOUT:
7208 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007209 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007210 goto error;
7211
7212 case HLUA_E_NOMEM:
7213 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007214 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007215 goto error;
7216
7217 case HLUA_E_YIELD: /* unexpected */
7218 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007219 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007220 goto error;
7221
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007222 case HLUA_E_ERR:
7223 /* Display log. */
7224 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007225 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007226 goto error;
7227
7228 default:
7229 goto error;
7230 }
7231
7232error:
7233
7234 /* For all other cases, just close the stream. */
7235 si_shutw(si);
7236 si_shutr(si);
7237 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7238}
7239
7240static void hlua_applet_tcp_release(struct appctx *ctx)
7241{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007242 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007243 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007244 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007245 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007246}
7247
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007248/* The function returns 1 if the initialisation is complete, 0 if
7249 * an errors occurs and -1 if more data are required for initializing
7250 * the applet.
7251 */
7252static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7253{
7254 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007255 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007256 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007257 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007258 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007259 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007260
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007261 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007262 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007263 if (!hlua) {
7264 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007265 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007266 return 0;
7267 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007268 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007269 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007270 ctx->ctx.hlua_apphttp.left_bytes = -1;
7271 ctx->ctx.hlua_apphttp.flags = 0;
7272
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007273 if (txn->req.flags & HTTP_MSGF_VER_11)
7274 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7275
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007276 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007277 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007278 if (!task) {
7279 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007280 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007281 return 0;
7282 }
7283 task->nice = 0;
7284 task->context = ctx;
7285 task->process = hlua_applet_wakeup;
7286 ctx->ctx.hlua_apphttp.task = task;
7287
7288 /* In the execution wrappers linked with a stream, the
7289 * Lua context can be not initialized. This behavior
7290 * permits to save performances because a systematic
7291 * Lua initialization cause 5% performances loss.
7292 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007293 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 +02007294 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007295 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007296 return 0;
7297 }
7298
7299 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007300 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007301
7302 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007303 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007304 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7305 error = lua_tostring(hlua->T, -1);
7306 else
7307 error = "critical error";
7308 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007309 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007310 return 0;
7311 }
7312
7313 /* Check stack available size. */
7314 if (!lua_checkstack(hlua->T, 1)) {
7315 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007316 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007317 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007318 return 0;
7319 }
7320
7321 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007322 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007323
7324 /* Create and and push object stream in the stack. */
7325 if (!hlua_applet_http_new(hlua->T, ctx)) {
7326 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007327 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007328 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007329 return 0;
7330 }
7331 hlua->nargs = 1;
7332
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007333 /* push keywords in the stack. */
7334 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7335 if (!lua_checkstack(hlua->T, 1)) {
7336 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007337 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007338 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007339 return 0;
7340 }
7341 lua_pushstring(hlua->T, *arg);
7342 hlua->nargs++;
7343 }
7344
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007345 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007346
7347 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007348 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007349
7350 return 1;
7351}
7352
Willy Tarreau60409db2019-08-21 14:14:50 +02007353void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007354{
7355 struct stream_interface *si = ctx->owner;
7356 struct stream *strm = si_strm(si);
7357 struct channel *req = si_oc(si);
7358 struct channel *res = si_ic(si);
7359 struct act_rule *rule = ctx->rule;
7360 struct proxy *px = strm->be;
7361 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7362 struct htx *req_htx, *res_htx;
7363
7364 res_htx = htx_from_buf(&res->buf);
7365
7366 /* If the stream is disconnect or closed, ldo nothing. */
7367 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7368 goto out;
7369
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007370 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007371 if (!b_size(&res->buf)) {
7372 si_rx_room_blk(si);
7373 goto out;
7374 }
7375 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007376 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007377 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7378
7379 /* Set the currently running flag. */
7380 if (!HLUA_IS_RUNNING(hlua) &&
7381 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7382 struct htx_blk *blk;
7383 size_t count = co_data(req);
7384
7385 if (!count) {
7386 si_cant_get(si);
7387 goto out;
7388 }
7389
7390 /* We need to flush the request header. This left the body for
7391 * the Lua.
7392 */
7393 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007394 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007395 while (count && blk) {
7396 enum htx_blk_type type = htx_get_blk_type(blk);
7397 uint32_t sz = htx_get_blksz(blk);
7398
7399 if (sz > count) {
7400 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007401 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007402 goto out;
7403 }
7404
7405 count -= sz;
7406 co_set_data(req, co_data(req) - sz);
7407 blk = htx_remove_blk(req_htx, blk);
7408
7409 if (type == HTX_BLK_EOH)
7410 break;
7411 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007412 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007413 }
7414
7415 /* Executes The applet if it is not done. */
7416 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7417
7418 /* Execute the function. */
7419 switch (hlua_ctx_resume(hlua, 1)) {
7420 /* finished. */
7421 case HLUA_E_OK:
7422 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7423 break;
7424
7425 /* yield. */
7426 case HLUA_E_AGAIN:
7427 if (hlua->wake_time != TICK_ETERNITY)
7428 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007429 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007430
7431 /* finished with error. */
7432 case HLUA_E_ERRMSG:
7433 /* Display log. */
7434 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007435 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007436 lua_pop(hlua->T, 1);
7437 goto error;
7438
7439 case HLUA_E_ETMOUT:
7440 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007441 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007442 goto error;
7443
7444 case HLUA_E_NOMEM:
7445 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007446 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007447 goto error;
7448
7449 case HLUA_E_YIELD: /* unexpected */
7450 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007451 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007452 goto error;
7453
7454 case HLUA_E_ERR:
7455 /* Display log. */
7456 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007457 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007458 goto error;
7459
7460 default:
7461 goto error;
7462 }
7463 }
7464
7465 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007466 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7467 goto done;
7468
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007469 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7470 goto error;
7471
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007472 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
7473 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007474 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7475 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007476 }
7477
7478 done:
7479 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007480 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007481 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007482 si_shutr(si);
7483 }
7484
7485 /* eat the whole request */
7486 if (co_data(req)) {
7487 req_htx = htx_from_buf(&req->buf);
7488 co_htx_skip(req, req_htx, co_data(req));
7489 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007490 }
7491 }
7492
7493 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007494 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007495 return;
7496
7497 error:
7498
7499 /* If we are in HTTP mode, and we are not send any
7500 * data, return a 500 server error in best effort:
7501 * if there is no room available in the buffer,
7502 * just close the connection.
7503 */
7504 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007505 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007506
7507 channel_erase(res);
7508 res->buf.data = b_data(err);
7509 memcpy(res->buf.area, b_head(err), b_data(err));
7510 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007511 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007512 }
7513 if (!(strm->flags & SF_ERR_MASK))
7514 strm->flags |= SF_ERR_RESOURCE;
7515 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7516 goto done;
7517}
7518
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007519static void hlua_applet_http_release(struct appctx *ctx)
7520{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007521 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007522 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007523 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007524 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007525}
7526
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007527/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007528 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007529 *
7530 * This function can fail with an abort() due to an Lua critical error.
7531 * We are in the configuration parsing process of HAProxy, this abort() is
7532 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007533 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007534static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7535 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007536{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007537 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007538 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007539
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007540 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007541 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007542 if (!rule->arg.hlua_rule) {
7543 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007544 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007545 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007546
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007547 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007548 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7549 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007550 if (!rule->arg.hlua_rule->args) {
7551 memprintf(err, "out of memory error");
7552 return ACT_RET_PRS_ERR;
7553 }
7554
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007555 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007556 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007557
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007558 /* Expect some arguments */
7559 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007560 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007561 memprintf(err, "expect %d arguments", fcn->nargs);
7562 return ACT_RET_PRS_ERR;
7563 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007564 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007565 if (!rule->arg.hlua_rule->args[i]) {
7566 memprintf(err, "out of memory error");
7567 return ACT_RET_PRS_ERR;
7568 }
7569 (*cur_arg)++;
7570 }
7571 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007572
Thierry FOURNIER42148732015-09-02 17:17:33 +02007573 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007574 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007575 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007576}
7577
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007578static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7579 struct act_rule *rule, char **err)
7580{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007581 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007582
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007583 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007584 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007585 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007586 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007587 * the call of this analyzer.
7588 */
7589 if (rule->from != ACT_F_HTTP_REQ) {
7590 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7591 return ACT_RET_PRS_ERR;
7592 }
7593
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007594 /* Memory for the rule. */
7595 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7596 if (!rule->arg.hlua_rule) {
7597 memprintf(err, "out of memory error");
7598 return ACT_RET_PRS_ERR;
7599 }
7600
7601 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007602 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007603
7604 /* TODO: later accept arguments. */
7605 rule->arg.hlua_rule->args = NULL;
7606
7607 /* Add applet pointer in the rule. */
7608 rule->applet.obj_type = OBJ_TYPE_APPLET;
7609 rule->applet.name = fcn->name;
7610 rule->applet.init = hlua_applet_http_init;
7611 rule->applet.fct = hlua_applet_http_fct;
7612 rule->applet.release = hlua_applet_http_release;
7613 rule->applet.timeout = hlua_timeout_applet;
7614
7615 return ACT_RET_PRS_OK;
7616}
7617
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007618/* This function is an LUA binding used for registering
7619 * "sample-conv" functions. It expects a converter name used
7620 * in the haproxy configuration file, and an LUA function.
7621 */
7622__LJMP static int hlua_register_action(lua_State *L)
7623{
7624 struct action_kw_list *akl;
7625 const char *name;
7626 int ref;
7627 int len;
7628 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007629 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007630 struct buffer *trash;
7631 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007632
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007633 /* Initialise the number of expected arguments at 0. */
7634 nargs = 0;
7635
7636 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7637 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007638
7639 /* First argument : converter name. */
7640 name = MAY_LJMP(luaL_checkstring(L, 1));
7641
7642 /* Second argument : environment. */
7643 if (lua_type(L, 2) != LUA_TTABLE)
7644 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7645
7646 /* Third argument : lua function. */
7647 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7648
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007649 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007650 if (lua_gettop(L) >= 4)
7651 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7652
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007653 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007654 lua_pushnil(L);
7655 while (lua_next(L, 2) != 0) {
7656 if (lua_type(L, -1) != LUA_TSTRING)
7657 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7658
Thierry Fournierf67442e2020-11-28 20:41:07 +01007659 /* Check if action exists */
7660 trash = get_trash_chunk();
7661 chunk_printf(trash, "lua.%s", name);
7662 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7663 akw = tcp_req_cont_action(trash->area);
7664 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7665 akw = tcp_res_cont_action(trash->area);
7666 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7667 akw = action_http_req_custom(trash->area);
7668 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7669 akw = action_http_res_custom(trash->area);
7670 } else {
7671 akw = NULL;
7672 }
7673 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007674 fcn = akw->private;
7675 if (fcn->function_ref[hlua_state_id] != -1) {
7676 ha_warning("Trying to register action 'lua.%s' more than once. "
7677 "This will become a hard error in version 2.5.\n", name);
7678 }
7679 fcn->function_ref[hlua_state_id] = ref;
7680
7681 /* pop the environment string. */
7682 lua_pop(L, 1);
7683 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007684 }
7685
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007686 /* Check required environment. Only accepted "http" or "tcp". */
7687 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007688 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007689 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007690 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007691 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007692 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007693 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007694
7695 /* Fill fcn. */
7696 fcn->name = strdup(name);
7697 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007698 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01007699 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007700
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007701 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007702 fcn->nargs = nargs;
7703
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007704 /* List head */
7705 akl->list.n = akl->list.p = NULL;
7706
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007707 /* action keyword. */
7708 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007709 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007710 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007711 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007712
7713 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7714
7715 akl->kw[0].match_pfx = 0;
7716 akl->kw[0].private = fcn;
7717 akl->kw[0].parse = action_register_lua;
7718
7719 /* select the action registering point. */
7720 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7721 tcp_req_cont_keywords_register(akl);
7722 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7723 tcp_res_cont_keywords_register(akl);
7724 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7725 http_req_keywords_register(akl);
7726 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7727 http_res_keywords_register(akl);
7728 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007729 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007730 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7731 "are expected.", lua_tostring(L, -1)));
7732
7733 /* pop the environment string. */
7734 lua_pop(L, 1);
7735 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007736 return ACT_RET_PRS_OK;
7737}
7738
7739static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7740 struct act_rule *rule, char **err)
7741{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007742 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007743
Christopher Faulet280f85b2019-07-15 15:02:04 +02007744 if (px->mode == PR_MODE_HTTP) {
7745 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007746 return ACT_RET_PRS_ERR;
7747 }
7748
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007749 /* Memory for the rule. */
7750 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7751 if (!rule->arg.hlua_rule) {
7752 memprintf(err, "out of memory error");
7753 return ACT_RET_PRS_ERR;
7754 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007755
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007756 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007757 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007758
7759 /* TODO: later accept arguments. */
7760 rule->arg.hlua_rule->args = NULL;
7761
7762 /* Add applet pointer in the rule. */
7763 rule->applet.obj_type = OBJ_TYPE_APPLET;
7764 rule->applet.name = fcn->name;
7765 rule->applet.init = hlua_applet_tcp_init;
7766 rule->applet.fct = hlua_applet_tcp_fct;
7767 rule->applet.release = hlua_applet_tcp_release;
7768 rule->applet.timeout = hlua_timeout_applet;
7769
7770 return 0;
7771}
7772
7773/* This function is an LUA binding used for registering
7774 * "sample-conv" functions. It expects a converter name used
7775 * in the haproxy configuration file, and an LUA function.
7776 */
7777__LJMP static int hlua_register_service(lua_State *L)
7778{
7779 struct action_kw_list *akl;
7780 const char *name;
7781 const char *env;
7782 int ref;
7783 int len;
7784 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007785 struct buffer *trash;
7786 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007787
7788 MAY_LJMP(check_args(L, 3, "register_service"));
7789
7790 /* First argument : converter name. */
7791 name = MAY_LJMP(luaL_checkstring(L, 1));
7792
7793 /* Second argument : environment. */
7794 env = MAY_LJMP(luaL_checkstring(L, 2));
7795
7796 /* Third argument : lua function. */
7797 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7798
Thierry Fournierf67442e2020-11-28 20:41:07 +01007799 /* Check for service already registered */
7800 trash = get_trash_chunk();
7801 chunk_printf(trash, "lua.%s", name);
7802 akw = service_find(trash->area);
7803 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007804 fcn = akw->private;
7805 if (fcn->function_ref[hlua_state_id] != -1) {
7806 ha_warning("Trying to register service 'lua.%s' more than once. "
7807 "This will become a hard error in version 2.5.\n", name);
7808 }
7809 fcn->function_ref[hlua_state_id] = ref;
7810 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007811 }
7812
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007813 /* Allocate and fill the sample fetch keyword struct. */
7814 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7815 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007816 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007817 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007818 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007819 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007820
7821 /* Fill fcn. */
7822 len = strlen("<lua.>") + strlen(name) + 1;
7823 fcn->name = calloc(1, len);
7824 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007825 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007826 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007827 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007828
7829 /* List head */
7830 akl->list.n = akl->list.p = NULL;
7831
7832 /* converter keyword. */
7833 len = strlen("lua.") + strlen(name) + 1;
7834 akl->kw[0].kw = calloc(1, len);
7835 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007836 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007837
7838 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7839
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007840 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007841 if (strcmp(env, "tcp") == 0)
7842 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007843 else if (strcmp(env, "http") == 0)
7844 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007845 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007846 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007847 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007848
7849 akl->kw[0].match_pfx = 0;
7850 akl->kw[0].private = fcn;
7851
7852 /* End of array. */
7853 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7854
7855 /* Register this new converter */
7856 service_keywords_register(akl);
7857
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007858 return 0;
7859}
7860
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007861/* This function initialises Lua cli handler. It copies the
7862 * arguments in the Lua stack and create channel IO objects.
7863 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007864static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007865{
7866 struct hlua *hlua;
7867 struct hlua_function *fcn;
7868 int i;
7869 const char *error;
7870
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007871 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007872 appctx->ctx.hlua_cli.fcn = private;
7873
Willy Tarreaubafbe012017-11-24 17:34:44 +01007874 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007875 if (!hlua) {
7876 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007877 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007878 }
7879 HLUA_INIT(hlua);
7880 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007881
7882 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007883 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007884 * applet_http. It is absolutely compatible.
7885 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007886 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007887 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007888 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007889 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007890 }
7891 appctx->ctx.hlua_cli.task->nice = 0;
7892 appctx->ctx.hlua_cli.task->context = appctx;
7893 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7894
7895 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01007896 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 +01007897 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007898 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007899 }
7900
7901 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007902 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007903 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7904 error = lua_tostring(hlua->T, -1);
7905 else
7906 error = "critical error";
7907 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7908 goto error;
7909 }
7910
7911 /* Check stack available size. */
7912 if (!lua_checkstack(hlua->T, 2)) {
7913 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7914 goto error;
7915 }
7916
7917 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007918 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007919
7920 /* Once the arguments parsed, the CLI is like an AppletTCP,
7921 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007922 */
7923 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7924 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7925 goto error;
7926 }
7927 hlua->nargs = 1;
7928
7929 /* push keywords in the stack. */
7930 for (i = 0; *args[i]; i++) {
7931 /* Check stack available size. */
7932 if (!lua_checkstack(hlua->T, 1)) {
7933 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7934 goto error;
7935 }
7936 lua_pushstring(hlua->T, args[i]);
7937 hlua->nargs++;
7938 }
7939
7940 /* We must initialize the execution timeouts. */
7941 hlua->max_time = hlua_timeout_session;
7942
7943 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007944 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007945
7946 /* It's ok */
7947 return 0;
7948
7949 /* It's not ok. */
7950error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007951 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007952 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007953 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007954 return 1;
7955}
7956
7957static int hlua_cli_io_handler_fct(struct appctx *appctx)
7958{
7959 struct hlua *hlua;
7960 struct stream_interface *si;
7961 struct hlua_function *fcn;
7962
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007963 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007964 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007965 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007966
7967 /* If the stream is disconnect or closed, ldo nothing. */
7968 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7969 return 1;
7970
7971 /* Execute the function. */
7972 switch (hlua_ctx_resume(hlua, 1)) {
7973
7974 /* finished. */
7975 case HLUA_E_OK:
7976 return 1;
7977
7978 /* yield. */
7979 case HLUA_E_AGAIN:
7980 /* We want write. */
7981 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007982 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007983 /* Set the timeout. */
7984 if (hlua->wake_time != TICK_ETERNITY)
7985 task_schedule(hlua->task, hlua->wake_time);
7986 return 0;
7987
7988 /* finished with error. */
7989 case HLUA_E_ERRMSG:
7990 /* Display log. */
7991 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7992 fcn->name, lua_tostring(hlua->T, -1));
7993 lua_pop(hlua->T, 1);
7994 return 1;
7995
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007996 case HLUA_E_ETMOUT:
7997 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7998 fcn->name);
7999 return 1;
8000
8001 case HLUA_E_NOMEM:
8002 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8003 fcn->name);
8004 return 1;
8005
8006 case HLUA_E_YIELD: /* unexpected */
8007 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8008 fcn->name);
8009 return 1;
8010
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008011 case HLUA_E_ERR:
8012 /* Display log. */
8013 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8014 fcn->name);
8015 return 1;
8016
8017 default:
8018 return 1;
8019 }
8020
8021 return 1;
8022}
8023
8024static void hlua_cli_io_release_fct(struct appctx *appctx)
8025{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008026 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008027 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008028}
8029
8030/* This function is an LUA binding used for registering
8031 * new keywords in the cli. It expects a list of keywords
8032 * which are the "path". It is limited to 5 keywords. A
8033 * description of the command, a function to be executed
8034 * for the parsing and a function for io handlers.
8035 */
8036__LJMP static int hlua_register_cli(lua_State *L)
8037{
8038 struct cli_kw_list *cli_kws;
8039 const char *message;
8040 int ref_io;
8041 int len;
8042 struct hlua_function *fcn;
8043 int index;
8044 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008045 struct buffer *trash;
8046 const char *kw[5];
8047 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008048
8049 MAY_LJMP(check_args(L, 3, "register_cli"));
8050
8051 /* First argument : an array of maximum 5 keywords. */
8052 if (!lua_istable(L, 1))
8053 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8054
8055 /* Second argument : string with contextual message. */
8056 message = MAY_LJMP(luaL_checkstring(L, 2));
8057
8058 /* Third and fourth argument : lua function. */
8059 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8060
Thierry Fournierf67442e2020-11-28 20:41:07 +01008061 /* Check for CLI service already registered */
8062 trash = get_trash_chunk();
8063 index = 0;
8064 lua_pushnil(L);
8065 memset(kw, 0, sizeof(kw));
8066 while (lua_next(L, 1) != 0) {
8067 if (index >= CLI_PREFIX_KW_NB)
8068 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8069 if (lua_type(L, -1) != LUA_TSTRING)
8070 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8071 kw[index] = lua_tostring(L, -1);
8072 if (index == 0)
8073 chunk_printf(trash, "%s", kw[index]);
8074 else
8075 chunk_appendf(trash, " %s", kw[index]);
8076 index++;
8077 lua_pop(L, 1);
8078 }
8079 cli_kw = cli_find_kw_exact((char **)kw);
8080 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008081 fcn = cli_kw->private;
8082 if (fcn->function_ref[hlua_state_id] != -1) {
8083 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8084 "This will become a hard error in version 2.5.\n", trash->area);
8085 }
8086 fcn->function_ref[hlua_state_id] = ref_io;
8087 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008088 }
8089
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008090 /* Allocate and fill the sample fetch keyword struct. */
8091 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8092 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008093 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008094 fcn = new_hlua_function();
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008095 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008096 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008097
8098 /* Fill path. */
8099 index = 0;
8100 lua_pushnil(L);
8101 while(lua_next(L, 1) != 0) {
8102 if (index >= 5)
8103 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8104 if (lua_type(L, -1) != LUA_TSTRING)
8105 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8106 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8107 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008108 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008109 index++;
8110 lua_pop(L, 1);
8111 }
8112
8113 /* Copy help message. */
8114 cli_kws->kw[0].usage = strdup(message);
8115 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008116 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008117
8118 /* Fill fcn io handler. */
8119 len = strlen("<lua.cli>") + 1;
8120 for (i = 0; i < index; i++)
8121 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8122 fcn->name = calloc(1, len);
8123 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008124 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008125 strncat((char *)fcn->name, "<lua.cli", len);
8126 for (i = 0; i < index; i++) {
8127 strncat((char *)fcn->name, ".", len);
8128 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8129 }
8130 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008131 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008132
8133 /* Fill last entries. */
8134 cli_kws->kw[0].private = fcn;
8135 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8136 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8137 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8138
8139 /* Register this new converter */
8140 cli_register_kw(cli_kws);
8141
8142 return 0;
8143}
8144
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008145static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008146 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008147 char **err, unsigned int *timeout)
8148{
8149 const char *error;
8150
8151 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008152 if (error == PARSE_TIME_OVER) {
8153 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8154 args[1], args[0]);
8155 return -1;
8156 }
8157 else if (error == PARSE_TIME_UNDER) {
8158 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8159 args[1], args[0]);
8160 return -1;
8161 }
8162 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008163 memprintf(err, "%s: invalid timeout", args[0]);
8164 return -1;
8165 }
8166 return 0;
8167}
8168
8169static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008170 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008171 char **err)
8172{
8173 return hlua_read_timeout(args, section_type, curpx, defpx,
8174 file, line, err, &hlua_timeout_session);
8175}
8176
8177static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008178 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008179 char **err)
8180{
8181 return hlua_read_timeout(args, section_type, curpx, defpx,
8182 file, line, err, &hlua_timeout_task);
8183}
8184
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008185static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008186 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008187 char **err)
8188{
8189 return hlua_read_timeout(args, section_type, curpx, defpx,
8190 file, line, err, &hlua_timeout_applet);
8191}
8192
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008193static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008194 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008195 char **err)
8196{
8197 char *error;
8198
8199 hlua_nb_instruction = strtoll(args[1], &error, 10);
8200 if (*error != '\0') {
8201 memprintf(err, "%s: invalid number", args[0]);
8202 return -1;
8203 }
8204 return 0;
8205}
8206
Willy Tarreau32f61e22015-03-18 17:54:59 +01008207static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008208 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008209 char **err)
8210{
8211 char *error;
8212
8213 if (*(args[1]) == 0) {
8214 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8215 return -1;
8216 }
8217 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8218 if (*error != '\0') {
8219 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8220 return -1;
8221 }
8222 return 0;
8223}
8224
8225
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008226/* This function is called by the main configuration key "lua-load". It loads and
8227 * execute an lua file during the parsing of the HAProxy configuration file. It is
8228 * the main lua entry point.
8229 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008230 * This function runs with the HAProxy keywords API. It returns -1 if an error
8231 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008232 *
8233 * In some error case, LUA set an error message in top of the stack. This function
8234 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008235 *
8236 * This function can fail with an abort() due to an Lua critical error.
8237 * We are in the configuration parsing process of HAProxy, this abort() is
8238 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008239 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008240static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008241{
8242 int error;
8243
8244 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008245 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008246 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008247 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8248 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008249 return -1;
8250 }
8251
8252 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008253 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008254 switch (error) {
8255 case LUA_OK:
8256 break;
8257 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008258 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8259 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008260 return -1;
8261 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008262 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008263 return -1;
8264 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008265 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8266 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008267 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008268#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008269 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008270 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8271 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008272 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008273#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008274 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008275 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8276 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008277 return -1;
8278 }
8279
8280 return 0;
8281}
8282
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008283static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008284 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008285 char **err)
8286{
8287 if (*(args[1]) == 0) {
8288 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8289 return -1;
8290 }
8291
Thierry Fournier59f11be2020-11-29 00:37:41 +01008292 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008293 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008294 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008295 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008296}
8297
Thierry Fournier59f11be2020-11-29 00:37:41 +01008298static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008299 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008300 char **err)
8301{
8302 int len;
8303
8304 if (*(args[1]) == 0) {
8305 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8306 return -1;
8307 }
8308
8309 if (per_thread_load == NULL) {
8310 /* allocate the first entry large enough to store the final NULL */
8311 per_thread_load = calloc(1, sizeof(*per_thread_load));
8312 if (per_thread_load == NULL) {
8313 memprintf(err, "out of memory error");
8314 return -1;
8315 }
8316 }
8317
8318 /* count used entries */
8319 for (len = 0; per_thread_load[len] != NULL; len++)
8320 ;
8321
8322 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8323 if (per_thread_load == NULL) {
8324 memprintf(err, "out of memory error");
8325 return -1;
8326 }
8327
8328 per_thread_load[len] = strdup(args[1]);
8329 per_thread_load[len + 1] = NULL;
8330
8331 if (per_thread_load[len] == NULL) {
8332 memprintf(err, "out of memory error");
8333 return -1;
8334 }
8335
8336 /* loading for thread 1 only */
8337 hlua_state_id = 1;
8338 ha_set_tid(0);
8339 return hlua_load_state(args[1], hlua_states[1], err);
8340}
8341
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008342/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8343 * in the given <ctx>.
8344 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008345static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008346{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008347 lua_getglobal(L, "package"); /* push package variable */
8348 lua_pushstring(L, path); /* push given path */
8349 lua_pushstring(L, ";"); /* push semicolon */
8350 lua_getfield(L, -3, type); /* push old path */
8351 lua_concat(L, 3); /* concatenate to new path */
8352 lua_setfield(L, -2, type); /* store new path */
8353 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008354
8355 return 0;
8356}
8357
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008358static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008359 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008360 char **err)
8361{
8362 char *path;
8363 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008364 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008365
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008366 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008367 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008368 }
8369
8370 if (!(*args[1])) {
8371 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008372 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008373 }
8374 path = args[1];
8375
8376 if (*args[2]) {
8377 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8378 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008379 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008380 }
8381 type = args[2];
8382 }
8383
Thierry Fournier59f11be2020-11-29 00:37:41 +01008384 p = calloc(1, sizeof(*p));
8385 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008386 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008387 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008388 }
8389 p->path = strdup(path);
8390 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008391 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008392 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008393 }
8394 p->type = strdup(type);
8395 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008396 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008397 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008398 }
8399 LIST_ADDQ(&prepend_path_list, &p->l);
8400
8401 hlua_prepend_path(hlua_states[0], type, path);
8402 hlua_prepend_path(hlua_states[1], type, path);
8403 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008404
8405err2:
8406 free(p->type);
8407 free(p->path);
8408err:
8409 free(p);
8410 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008411}
8412
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008413/* configuration keywords declaration */
8414static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008415 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008416 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008417 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008418 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8419 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008420 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008421 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008422 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008423 { 0, NULL, NULL },
8424}};
8425
Willy Tarreau0108d902018-11-25 19:14:37 +01008426INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8427
Christopher Fauletafd8f102018-11-08 11:34:21 +01008428
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008429/* This function can fail with an abort() due to an Lua critical error.
8430 * We are in the initialisation process of HAProxy, this abort() is
8431 * tolerated.
8432 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008433int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008434{
8435 struct hlua_init_function *init;
8436 const char *msg;
8437 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008438 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008439 const char *kind;
8440 const char *trace;
8441 int return_status = 1;
8442#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8443 int nres;
8444#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008445
Willy Tarreaucdb53462020-12-02 12:12:00 +01008446 /* disable memory limit checks if limit is not set */
8447 if (!hlua_global_allocator.limit)
8448 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8449
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008450 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008451 if (setjmp(safe_ljmp_env) != 0) {
8452 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008453 if (lua_type(L, -1) == LUA_TSTRING)
8454 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008455 else
8456 error = "critical error";
8457 fprintf(stderr, "Lua post-init: %s.\n", error);
8458 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008459 } else {
8460 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008461 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008462
Thierry Fournierb8cef172020-11-28 15:37:17 +01008463 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008464
Thierry Fournierc7492592020-11-28 23:57:24 +01008465 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008466 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008467
8468#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008469 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008470#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008471 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008472#endif
8473 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008474 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008475
8476 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008477 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008478 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008479
8480 case LUA_ERRERR:
8481 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008482 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008483 case LUA_ERRRUN:
8484 if (!kind)
8485 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008486 msg = lua_tostring(L, -1);
8487 lua_settop(L, 0); /* Empty the stack. */
8488 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008489 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008490 if (msg)
8491 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8492 else
8493 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8494 return_status = 0;
8495 break;
8496
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008497 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008498 /* Unknown error */
8499 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008500 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008501 case LUA_YIELD:
8502 /* yield is not configured at this step, this state doesn't happen */
8503 if (!kind)
8504 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008505 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008506 case LUA_ERRMEM:
8507 if (!kind)
8508 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008509 lua_settop(L, 0);
8510 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008511 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008512 ha_alert("Lua init: %s: %s\n", kind, trace);
8513 return_status = 0;
8514 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008515 }
Thierry Fournier670db242020-11-28 10:49:59 +01008516 if (!return_status)
8517 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008518 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008519
8520 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008521 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008522}
8523
Thierry Fournierb8cef172020-11-28 15:37:17 +01008524int hlua_post_init()
8525{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008526 int ret;
8527 int i;
8528 int errors;
8529 char *err = NULL;
8530 struct hlua_function *fcn;
8531
Thierry Fournierb8cef172020-11-28 15:37:17 +01008532#if USE_OPENSSL
8533 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008534 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008535 int saved_used_backed = global.ssl_used_backend;
8536 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008537 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008538 global.ssl_used_backend = saved_used_backed;
8539 }
8540#endif
8541
Thierry Fournierc7492592020-11-28 23:57:24 +01008542 /* Perform post init of common thread */
8543 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008544 ha_set_tid(0);
8545 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8546 if (ret == 0)
8547 return 0;
8548
8549 /* init remaining lua states and load files */
8550 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8551
8552 /* set thread context */
8553 ha_set_tid(hlua_state_id - 1);
8554
8555 /* Init lua state */
8556 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8557
8558 /* Load lua files */
8559 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8560 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8561 if (ret != 0) {
8562 ha_alert("Lua init: %s\n", err);
8563 return 0;
8564 }
8565 }
8566 }
8567
8568 /* Reset thread context */
8569 ha_set_tid(0);
8570
8571 /* Execute post init for all states */
8572 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8573
8574 /* set thread context */
8575 ha_set_tid(hlua_state_id - 1);
8576
8577 /* run post init */
8578 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8579 if (ret == 0)
8580 return 0;
8581 }
8582
8583 /* Reset thread context */
8584 ha_set_tid(0);
8585
8586 /* control functions registering. Each function must have:
8587 * - only the function_ref[0] set positive and all other to -1
8588 * - only the function_ref[0] set to -1 and all other positive
8589 * This ensure a same reference is not used both in shared
8590 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008591 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008592 * complicated to found for the end user.
8593 */
8594 errors = 0;
8595 list_for_each_entry(fcn, &referenced_functions, l) {
8596 ret = 0;
8597 for (i = 1; i < global.nbthread + 1; i++) {
8598 if (fcn->function_ref[i] == -1)
8599 ret--;
8600 else
8601 ret++;
8602 }
8603 if (abs(ret) != global.nbthread) {
8604 ha_alert("Lua function '%s' is not referenced in all thread. "
8605 "Expect function in all thread or in none thread.\n", fcn->name);
8606 errors++;
8607 continue;
8608 }
8609
8610 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008611 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8612 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008613 "exclusive.\n", fcn->name);
8614 errors++;
8615 }
8616 }
8617
8618 if (errors > 0)
8619 return 0;
8620
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008621 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008622 * -1 in order to have probably a segfault if someone use it
8623 */
8624 hlua_state_id = -1;
8625
8626 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008627}
8628
Willy Tarreau32f61e22015-03-18 17:54:59 +01008629/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8630 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8631 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008632 * allocation. <nsize> is the requested new size. A new allocation is
8633 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008634 * zero. This one verifies that the limits are respected but is optimized
8635 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008636 */
8637static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8638{
8639 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008640 size_t limit, old, new;
8641
Tim Duesterhus22586522021-01-08 10:35:33 +01008642 if (unlikely(!ptr && !nsize))
8643 return NULL;
8644
Willy Tarreaucdb53462020-12-02 12:12:00 +01008645 /* a limit of ~0 means unlimited and boot complete, so there's no need
8646 * for accounting anymore.
8647 */
Christopher Fauletcc2c4f82021-03-24 14:52:24 +01008648 if (likely(~zone->limit == 0))
8649 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008650
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008651 if (!ptr)
8652 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008653
Willy Tarreaucdb53462020-12-02 12:12:00 +01008654 /* enforce strict limits across all threads */
8655 limit = zone->limit;
8656 old = _HA_ATOMIC_LOAD(&zone->allocated);
8657 do {
8658 new = old + nsize - osize;
8659 if (unlikely(nsize && limit && new > limit))
8660 return NULL;
8661 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008662
8663 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008664
8665 if (unlikely(!ptr && nsize)) // failed
8666 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8667
8668 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008669 return ptr;
8670}
8671
Thierry Fournierecb83c22020-11-28 15:49:44 +01008672/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008673 * We are in the initialisation process of HAProxy, this abort() is
8674 * tolerated.
8675 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008676lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008677{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008678 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008679 int idx;
8680 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008681 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008682 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008683 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008684 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008685 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008686 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008687
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008688 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008689 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008690
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008691 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008692 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008693 *context = NULL;
8694
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008695 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008696 * the Lua function can fail with an abort. We are in the initialisation
8697 * process of HAProxy, this abort() is tolerated.
8698 */
8699
Thierry Fournier3c539322020-11-28 16:05:05 +01008700 /* Call post initialisation function in safe environment. */
8701 if (setjmp(safe_ljmp_env) != 0) {
8702 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008703 if (lua_type(L, -1) == LUA_TSTRING)
8704 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008705 else
8706 error_msg = "critical error";
8707 fprintf(stderr, "Lua init: %s.\n", error_msg);
8708 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008709 } else {
8710 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008711 }
8712
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008713 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008714 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008715#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8716#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8717#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008718 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008719#endif
8720#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008721 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008722#endif
8723#undef HLUA_PREPEND_PATH_TOSTRING
8724#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008725
Thierry Fournier59f11be2020-11-29 00:37:41 +01008726 /* Apply configured prepend path */
8727 list_for_each_entry(pp, &prepend_path_list, l)
8728 hlua_prepend_path(L, pp->type, pp->path);
8729
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008730 /*
8731 *
8732 * Create "core" object.
8733 *
8734 */
8735
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008736 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008737 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008738
Thierry Fournierecb83c22020-11-28 15:49:44 +01008739 /* set the thread id */
8740 hlua_class_const_int(L, "thread", thread_num);
8741
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008742 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008743 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008744 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008745
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008746 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008747 hlua_class_function(L, "register_init", hlua_register_init);
8748 hlua_class_function(L, "register_task", hlua_register_task);
8749 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8750 hlua_class_function(L, "register_converters", hlua_register_converters);
8751 hlua_class_function(L, "register_action", hlua_register_action);
8752 hlua_class_function(L, "register_service", hlua_register_service);
8753 hlua_class_function(L, "register_cli", hlua_register_cli);
8754 hlua_class_function(L, "yield", hlua_yield);
8755 hlua_class_function(L, "set_nice", hlua_set_nice);
8756 hlua_class_function(L, "sleep", hlua_sleep);
8757 hlua_class_function(L, "msleep", hlua_msleep);
8758 hlua_class_function(L, "add_acl", hlua_add_acl);
8759 hlua_class_function(L, "del_acl", hlua_del_acl);
8760 hlua_class_function(L, "set_map", hlua_set_map);
8761 hlua_class_function(L, "del_map", hlua_del_map);
8762 hlua_class_function(L, "tcp", hlua_socket_new);
8763 hlua_class_function(L, "log", hlua_log);
8764 hlua_class_function(L, "Debug", hlua_log_debug);
8765 hlua_class_function(L, "Info", hlua_log_info);
8766 hlua_class_function(L, "Warning", hlua_log_warning);
8767 hlua_class_function(L, "Alert", hlua_log_alert);
8768 hlua_class_function(L, "done", hlua_done);
8769 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008770
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008771 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008772
8773 /*
8774 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008775 * Create "act" object.
8776 *
8777 */
8778
8779 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008780 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008781
8782 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008783 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8784 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8785 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8786 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8787 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8788 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8789 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8790 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008791
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008792 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008793
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008794 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008795
8796 /*
8797 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008798 * Register class Map
8799 *
8800 */
8801
8802 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008803 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008804
8805 /* register pattern types. */
8806 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008807 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008808 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008809 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008810 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008811 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008812
8813 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008814 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008815
8816 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008817 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008818
Ilya Shipitsind4259502020-04-08 01:07:56 +05008819 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008820 lua_pushstring(L, "__index");
8821 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008822
8823 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008824 hlua_class_function(L, "lookup", hlua_map_lookup);
8825 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008826
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008827 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008828
Thierry Fournier45e78d72016-02-19 18:34:46 +01008829 /* Register previous table in the registry with reference and named entry.
8830 * The function hlua_register_metatable() pops the stack, so we
8831 * previously create a copy of the table.
8832 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008833 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8834 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008835
8836 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008837 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008838
8839 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008840 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008841
8842 /*
8843 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008844 * Register class Channel
8845 *
8846 */
8847
8848 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008849 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008850
Ilya Shipitsind4259502020-04-08 01:07:56 +05008851 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008852 lua_pushstring(L, "__index");
8853 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008854
8855 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008856 hlua_class_function(L, "get", hlua_channel_get);
8857 hlua_class_function(L, "dup", hlua_channel_dup);
8858 hlua_class_function(L, "getline", hlua_channel_getline);
8859 hlua_class_function(L, "set", hlua_channel_set);
8860 hlua_class_function(L, "append", hlua_channel_append);
8861 hlua_class_function(L, "send", hlua_channel_send);
8862 hlua_class_function(L, "forward", hlua_channel_forward);
8863 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
8864 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
8865 hlua_class_function(L, "is_full", hlua_channel_is_full);
8866 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008867
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008868 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008869
8870 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008871 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008872
8873 /*
8874 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008875 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008876 *
8877 */
8878
8879 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008880 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008881
Ilya Shipitsind4259502020-04-08 01:07:56 +05008882 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008883 lua_pushstring(L, "__index");
8884 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008885
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008886 /* Browse existing fetches and create the associated
8887 * object method.
8888 */
8889 sf = NULL;
8890 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008891 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8892 * by an underscore.
8893 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008894 strncpy(trash.area, sf->kw, trash.size);
8895 trash.area[trash.size - 1] = '\0';
8896 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008897 if (*p == '.' || *p == '-' || *p == '+')
8898 *p = '_';
8899
8900 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008901 lua_pushstring(L, trash.area);
8902 lua_pushlightuserdata(L, sf);
8903 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
8904 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008905 }
8906
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008907 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008908
8909 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008910 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008911
8912 /*
8913 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008914 * Register class Converters
8915 *
8916 */
8917
8918 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008919 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008920
8921 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008922 lua_pushstring(L, "__index");
8923 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008924
8925 /* Browse existing converters and create the associated
8926 * object method.
8927 */
8928 sc = NULL;
8929 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008930 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8931 * by an underscore.
8932 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008933 strncpy(trash.area, sc->kw, trash.size);
8934 trash.area[trash.size - 1] = '\0';
8935 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008936 if (*p == '.' || *p == '-' || *p == '+')
8937 *p = '_';
8938
8939 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008940 lua_pushstring(L, trash.area);
8941 lua_pushlightuserdata(L, sc);
8942 lua_pushcclosure(L, hlua_run_sample_conv, 1);
8943 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008944 }
8945
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008946 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008947
8948 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008949 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008950
8951 /*
8952 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008953 * Register class HTTP
8954 *
8955 */
8956
8957 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008958 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008959
Ilya Shipitsind4259502020-04-08 01:07:56 +05008960 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008961 lua_pushstring(L, "__index");
8962 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008963
8964 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008965 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
8966 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
8967 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
8968 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
8969 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
8970 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
8971 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
8972 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
8973 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
8974 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008975
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008976 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
8977 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
8978 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
8979 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
8980 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
8981 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
8982 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008983
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008984 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008985
8986 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008987 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008988
8989 /*
8990 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008991 * Register class AppletTCP
8992 *
8993 */
8994
8995 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008996 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008997
Ilya Shipitsind4259502020-04-08 01:07:56 +05008998 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008999 lua_pushstring(L, "__index");
9000 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009001
9002 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009003 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9004 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9005 hlua_class_function(L, "send", hlua_applet_tcp_send);
9006 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9007 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9008 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9009 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9010 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009011
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009012 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009013
9014 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009015 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009016
9017 /*
9018 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009019 * Register class AppletHTTP
9020 *
9021 */
9022
9023 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009024 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009025
Ilya Shipitsind4259502020-04-08 01:07:56 +05009026 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009027 lua_pushstring(L, "__index");
9028 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009029
9030 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009031 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9032 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9033 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9034 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9035 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9036 hlua_class_function(L, "getline", hlua_applet_http_getline);
9037 hlua_class_function(L, "receive", hlua_applet_http_recv);
9038 hlua_class_function(L, "send", hlua_applet_http_send);
9039 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9040 hlua_class_function(L, "set_status", hlua_applet_http_status);
9041 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009042
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009043 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009044
9045 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009046 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009047
9048 /*
9049 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009050 * Register class TXN
9051 *
9052 */
9053
9054 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009055 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009056
Ilya Shipitsind4259502020-04-08 01:07:56 +05009057 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009058 lua_pushstring(L, "__index");
9059 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009060
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009061 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009062 hlua_class_function(L, "set_priv", hlua_set_priv);
9063 hlua_class_function(L, "get_priv", hlua_get_priv);
9064 hlua_class_function(L, "set_var", hlua_set_var);
9065 hlua_class_function(L, "unset_var", hlua_unset_var);
9066 hlua_class_function(L, "get_var", hlua_get_var);
9067 hlua_class_function(L, "done", hlua_txn_done);
9068 hlua_class_function(L, "reply", hlua_txn_reply_new);
9069 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9070 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9071 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9072 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9073 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9074 hlua_class_function(L, "deflog", hlua_txn_deflog);
9075 hlua_class_function(L, "log", hlua_txn_log);
9076 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9077 hlua_class_function(L, "Info", hlua_txn_log_info);
9078 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9079 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009080
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009081 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009082
9083 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009084 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009085
9086 /*
9087 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009088 * Register class reply
9089 *
9090 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009091 lua_newtable(L);
9092 lua_pushstring(L, "__index");
9093 lua_newtable(L);
9094 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9095 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9096 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9097 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9098 lua_settable(L, -3); /* Sets the __index entry. */
9099 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009100
9101
9102 /*
9103 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009104 * Register class Socket
9105 *
9106 */
9107
9108 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009109 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009110
Ilya Shipitsind4259502020-04-08 01:07:56 +05009111 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009112 lua_pushstring(L, "__index");
9113 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009114
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009115#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009116 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009117#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009118 hlua_class_function(L, "connect", hlua_socket_connect);
9119 hlua_class_function(L, "send", hlua_socket_send);
9120 hlua_class_function(L, "receive", hlua_socket_receive);
9121 hlua_class_function(L, "close", hlua_socket_close);
9122 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9123 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9124 hlua_class_function(L, "setoption", hlua_socket_setoption);
9125 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009126
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009127 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009128
9129 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009130 lua_pushstring(L, "__gc");
9131 lua_pushcclosure(L, hlua_socket_gc, 0);
9132 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009133
9134 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009135 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009136
Thierry Fournieraafc7772020-12-04 11:47:47 +01009137 lua_atpanic(L, hlua_panic_safe);
9138
9139 return L;
9140}
9141
9142void hlua_init(void) {
9143 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009144 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009145#ifdef USE_OPENSSL
9146 struct srv_kw *kw;
9147 int tmp_error;
9148 char *error;
9149 char *args[] = { /* SSL client configuration. */
9150 "ssl",
9151 "verify",
9152 "none",
9153 NULL
9154 };
9155#endif
9156
9157 /* Init post init function list head */
9158 for (i = 0; i < MAX_THREADS + 1; i++)
9159 LIST_INIT(&hlua_init_functions[i]);
9160
9161 /* Init state for common/shared lua parts */
9162 hlua_state_id = 0;
9163 ha_set_tid(0);
9164 hlua_states[0] = hlua_init_state(0);
9165
9166 /* Init state 1 for thread 0. We have at least one thread. */
9167 hlua_state_id = 1;
9168 ha_set_tid(0);
9169 hlua_states[1] = hlua_init_state(1);
9170
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009171 /* Proxy and server configuration initialisation. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009172 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg);
9173 if (!socket_proxy) {
9174 fprintf(stderr, "Lua init: %s\n", errmsg);
9175 exit(1);
9176 }
9177 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009178
9179 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009180 socket_tcp = new_server(socket_proxy);
9181 if (!socket_tcp) {
9182 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9183 exit(1);
9184 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009185
9186#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009187 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009188 socket_ssl = new_server(socket_proxy);
9189 if (!socket_ssl) {
9190 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9191 exit(1);
9192 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009193
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009194 socket_ssl->use_ssl = 1;
9195 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009196
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009197 for (i = 0; args[i] != NULL; i++) {
9198 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009199 /*
9200 *
9201 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009202 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009203 * features like client certificates and ssl_verify.
9204 *
9205 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009206 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009207 if (tmp_error != 0) {
9208 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9209 abort(); /* This must be never arrives because the command line
9210 not editable by the user. */
9211 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009212 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009213 }
9214 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009215#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009216
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009217}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009218
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009219static void hlua_deinit()
9220{
Willy Tarreau186f3762020-12-04 11:48:12 +01009221 int thr;
9222
9223 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9224 if (hlua_states[thr])
9225 lua_close(hlua_states[thr]);
9226 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009227
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009228 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009229
Willy Tarreau0f143af2021-03-05 10:41:48 +01009230#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009231 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009232#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009233
9234 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009235}
9236
9237REGISTER_POST_DEINIT(hlua_deinit);
9238
Willy Tarreau80713382018-11-26 10:19:54 +01009239static void hlua_register_build_options(void)
9240{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009241 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009242
Willy Tarreaubb57d942016-12-21 19:04:56 +01009243 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9244 hap_register_build_opts(ptr, 1);
9245}
Willy Tarreau80713382018-11-26 10:19:54 +01009246
9247INITCALL0(STG_REGISTER, hlua_register_build_options);