blob: 434e8b3bc2d4c2ab6b79ab60b6f5b1e59b85049f [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 Tarreau3d6ee402021-05-08 20:28:07 +020048#include <haproxy/proxy.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 Tarreau69c66e32021-07-14 19:41:25 +0200127static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; }
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
Willy Tarreau3eb2e472021-08-20 15:47:25 +0200146/* This function takes the Lua global lock. Keep this function's visibility
147 * global so that it can appear in stack dumps and performance profiles!
148 */
149void lua_take_global_lock()
150{
151 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
152}
153
154static inline void lua_drop_global_lock()
155{
156 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
157}
158
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100159#define SET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200160 ({ \
161 int ret; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100162 if ((__HLUA)->state_id == 0) \
Willy Tarreau3eb2e472021-08-20 15:47:25 +0200163 lua_take_global_lock(); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200164 if (setjmp(safe_ljmp_env) != 0) { \
165 lua_atpanic(__L, hlua_panic_safe); \
166 ret = 0; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100167 if ((__HLUA)->state_id == 0) \
Willy Tarreau3eb2e472021-08-20 15:47:25 +0200168 lua_drop_global_lock(); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200169 } else { \
170 lua_atpanic(__L, hlua_panic_ljmp); \
171 ret = 1; \
172 } \
173 ret; \
174 })
175
176/* If we are the last function catching Lua errors, we
177 * must reset the panic function.
178 */
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100179#define RESET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200180 do { \
181 lua_atpanic(__L, hlua_panic_safe); \
Thierry Fournier021d9862020-11-28 23:42:03 +0100182 if ((__HLUA)->state_id == 0) \
Willy Tarreau3eb2e472021-08-20 15:47:25 +0200183 lua_drop_global_lock(); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200184 } while(0)
185
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100186#define SET_SAFE_LJMP(__HLUA) \
187 SET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
188
189#define RESET_SAFE_LJMP(__HLUA) \
190 RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
191
192#define SET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100193 SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100194
195#define RESET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100196 RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100197
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200198/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200199#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100200/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200201#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200202/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100203#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100204#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200205
Thierry Fournierafc63e22020-11-28 17:06:51 +0100206/* The main Lua execution context. The 0 index is the
207 * common state shared by all threads.
208 */
Willy Tarreau186f3762020-12-04 11:48:12 +0100209static lua_State *hlua_states[MAX_THREADS + 1];
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100210
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100211/* This is the memory pool containing struct lua for applets
212 * (including cli).
213 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100214DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100215
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100216/* Used for Socket connection. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +0100217static struct proxy *socket_proxy;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100218static struct server *socket_tcp;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100219#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100220static struct server *socket_ssl;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100221#endif
222
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100223/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100224struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100225
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100226/* The following variables contains the reference of the different
227 * Lua classes. These references are useful for identify metadata
228 * associated with an object.
229 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100230static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100231static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100232static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100233static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100234static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100235static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200236static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200237static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200238static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100239static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100240
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100241/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200242 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100243 * short timeout. Lua linked with tasks doesn't have a timeout
244 * because a task may remain alive during all the haproxy execution.
245 */
246static unsigned int hlua_timeout_session = 4000; /* session timeout. */
247static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200248static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100249
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100250/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
251 * it is used for preventing infinite loops.
252 *
253 * I test the scheer with an infinite loop containing one incrementation
254 * and one test. I run this loop between 10 seconds, I raise a ceil of
255 * 710M loops from one interrupt each 9000 instructions, so I fix the value
256 * to one interrupt each 10 000 instructions.
257 *
258 * configured | Number of
259 * instructions | loops executed
260 * between two | in milions
261 * forced yields |
262 * ---------------+---------------
263 * 10 | 160
264 * 500 | 670
265 * 1000 | 680
266 * 5000 | 700
267 * 7000 | 700
268 * 8000 | 700
269 * 9000 | 710 <- ceil
270 * 10000 | 710
271 * 100000 | 710
272 * 1000000 | 710
273 *
274 */
275static unsigned int hlua_nb_instruction = 10000;
276
Willy Tarreaucdb53462020-12-02 12:12:00 +0100277/* Descriptor for the memory allocation state. The limit is pre-initialised to
278 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
279 * is replaced with ~0 during post_init after everything was loaded. This way
280 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
281 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100282 */
283struct hlua_mem_allocator {
284 size_t allocated;
285 size_t limit;
286};
287
Willy Tarreaucdb53462020-12-02 12:12:00 +0100288static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100289
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100290/* These functions converts types between HAProxy internal args or
291 * sample and LUA types. Another function permits to check if the
292 * LUA stack contains arguments according with an required ARG_T
293 * format.
294 */
295static int hlua_arg2lua(lua_State *L, const struct arg *arg);
296static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100297__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100298 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100299static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100300static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100301static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
302
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100303__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200304
Thierry Fournier59f11be2020-11-29 00:37:41 +0100305struct prepend_path {
306 struct list l;
307 char *type;
308 char *path;
309};
310
311static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
312
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200313#define SEND_ERR(__be, __fmt, __args...) \
314 do { \
315 send_log(__be, LOG_ERR, __fmt, ## __args); \
316 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100317 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200318 } while (0)
319
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100320static inline struct hlua_function *new_hlua_function()
321{
322 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100323 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100324
325 fcn = calloc(1, sizeof(*fcn));
326 if (!fcn)
327 return NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200328 LIST_APPEND(&referenced_functions, &fcn->l);
Thierry Fournierc7492592020-11-28 23:57:24 +0100329 for (i = 0; i < MAX_THREADS + 1; i++)
330 fcn->function_ref[i] = -1;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100331 return fcn;
332}
333
Christopher Fauletdda44442021-04-12 14:05:43 +0200334static inline void release_hlua_function(struct hlua_function *fcn)
335{
336 if (!fcn)
337 return;
338 if (fcn->name)
339 ha_free(&fcn->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200340 LIST_DELETE(&fcn->l);
Christopher Fauletdda44442021-04-12 14:05:43 +0200341 ha_free(&fcn);
342}
343
Thierry Fournierc7492592020-11-28 23:57:24 +0100344/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
345static inline int fcn_ref_to_stack_id(struct hlua_function *fcn)
346{
347 if (fcn->function_ref[0] == -1)
348 return tid + 1;
349 return 0;
350}
351
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100352/* Used to check an Lua function type in the stack. It creates and
353 * returns a reference of the function. This function throws an
Aurelien DARRAGON5ce11522023-03-02 17:50:49 +0100354 * error if the argument is not a "function".
355 * When no longer used, the ref must be released with hlua_unref()
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100356 */
357__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
358{
359 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100360 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100361 WILL_LJMP(luaL_argerror(L, argno, msg));
362 }
363 lua_pushvalue(L, argno);
364 return luaL_ref(L, LUA_REGISTRYINDEX);
365}
366
Christopher Fauletddc90322020-02-25 10:20:04 +0100367/* Used to check an Lua table type in the stack. It creates and
368 * returns a reference of the table. This function throws an
Aurelien DARRAGON5ce11522023-03-02 17:50:49 +0100369 * error if the argument is not a "table".
370 * When no longer used, the ref must be released with hlua_unref()
Christopher Fauletddc90322020-02-25 10:20:04 +0100371 */
372__LJMP unsigned int hlua_checktable(lua_State *L, int argno)
373{
374 if (!lua_istable(L, argno)) {
375 const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno));
376 WILL_LJMP(luaL_argerror(L, argno, msg));
377 }
378 lua_pushvalue(L, argno);
379 return luaL_ref(L, LUA_REGISTRYINDEX);
380}
381
Aurelien DARRAGON5ce11522023-03-02 17:50:49 +0100382/* Get a reference to the object that is at the top of the stack
383 * The referenced object will be popped from the stack
384 *
385 * The function returns the reference to the object which must
386 * be cleared using hlua_unref() when no longer used
387 */
388__LJMP int hlua_ref(lua_State *L)
389{
390 return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX));
391}
392
393/* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX)
394 * on <L> stack
395 * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref())
396 *
397 * When the reference is no longer used, it should be released by calling
398 * hlua_unref()
399 *
400 * <L> can be from any co-routine as long as it belongs to the same lua
401 * parent state that the one used to get the reference.
402 */
403void hlua_pushref(lua_State *L, int ref)
404{
405 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
406}
407
408/* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX)
409 * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref())
410 *
411 * This will allow the reference to be reused and the referred object
412 * to be garbage collected.
413 *
414 * <L> can be from any co-routine as long as it belongs to the same lua
415 * parent state that the one used to get the reference.
416 */
417void hlua_unref(lua_State *L, int ref)
418{
419 luaL_unref(L, LUA_REGISTRYINDEX, ref);
420}
421
Christopher Fauletd09cc512021-03-24 14:48:45 +0100422__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200423{
424 lua_Debug ar;
425 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200426 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200427
428 while (lua_getstack(L, level++, &ar)) {
429
430 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100431 if (b_data(msg))
432 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200433
434 /* Fill fields:
435 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
436 * 'l': fills in the field currentline;
437 * 'n': fills in the field name and namewhat;
438 * 't': fills in the field istailcall;
439 */
440 lua_getinfo(L, "Slnt", &ar);
441
442 /* Append code localisation */
443 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100444 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200445 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100446 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200447
448 /*
449 * Get function name
450 *
451 * if namewhat is no empty, name is defined.
452 * what contains "Lua" for Lua function, "C" for C function,
453 * or "main" for main code.
454 */
455 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100456 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200457
458 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100459 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200460
461 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100462 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200463
464 else /* nothing left... */
465 chunk_appendf(msg, "?");
466
467
468 /* Display tailed call */
469 if (ar.istailcall)
470 chunk_appendf(msg, " ...");
471 }
472
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200474}
475
476
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100477/* This function check the number of arguments available in the
478 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500479 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100480 */
481__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
482{
483 if (lua_gettop(L) == nb)
484 return;
485 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
486}
487
Mark Lakes22154b42018-01-29 14:38:40 -0800488/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100489 * and the line number where the error is encountered.
490 */
491static int hlua_pusherror(lua_State *L, const char *fmt, ...)
492{
493 va_list argp;
494 va_start(argp, fmt);
495 luaL_where(L, 1);
496 lua_pushvfstring(L, fmt, argp);
497 va_end(argp);
498 lua_concat(L, 2);
499 return 1;
500}
501
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100502/* This functions is used with sample fetch and converters. It
503 * converts the HAProxy configuration argument in a lua stack
504 * values.
505 *
506 * It takes an array of "arg", and each entry of the array is
507 * converted and pushed in the LUA stack.
508 */
509static int hlua_arg2lua(lua_State *L, const struct arg *arg)
510{
511 switch (arg->type) {
512 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100513 case ARGT_TIME:
514 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100515 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100516 break;
517
518 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100520 break;
521
522 case ARGT_IPV4:
523 case ARGT_IPV6:
524 case ARGT_MSK4:
525 case ARGT_MSK6:
526 case ARGT_FE:
527 case ARGT_BE:
528 case ARGT_TAB:
529 case ARGT_SRV:
530 case ARGT_USR:
531 case ARGT_MAP:
532 default:
533 lua_pushnil(L);
534 break;
535 }
536 return 1;
537}
538
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500539/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500541 * with sample fetch wrappers. The input arguments are given to the
542 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100543 */
544static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
545{
546 switch (lua_type(L, ud)) {
547
548 case LUA_TNUMBER:
549 case LUA_TBOOLEAN:
550 arg->type = ARGT_SINT;
551 arg->data.sint = lua_tointeger(L, ud);
552 break;
553
554 case LUA_TSTRING:
555 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200556 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200557 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200558 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200559 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100560 break;
561
562 case LUA_TUSERDATA:
563 case LUA_TNIL:
564 case LUA_TTABLE:
565 case LUA_TFUNCTION:
566 case LUA_TTHREAD:
567 case LUA_TLIGHTUSERDATA:
568 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200569 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571 }
572 return 1;
573}
574
575/* the following functions are used to convert a struct sample
576 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500577 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100578 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100579static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100580{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200581 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100583 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200584 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100585 break;
586
587 case SMP_T_BIN:
588 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200589 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590 break;
591
592 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200593 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100594 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
595 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
596 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
597 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
598 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
599 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
600 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
601 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
602 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200603 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100604 break;
605 default:
606 lua_pushnil(L);
607 break;
608 }
609 break;
610
611 case SMP_T_IPV4:
612 case SMP_T_IPV6:
613 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200614 if (sample_casts[smp->data.type][SMP_T_STR] &&
615 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100617 else
618 lua_pushnil(L);
619 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100620 default:
621 lua_pushnil(L);
622 break;
623 }
624 return 1;
625}
626
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100627/* the following functions are used to convert a struct sample
628 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500629 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100630 */
631static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
632{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200633 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100634
635 case SMP_T_BIN:
636 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200637 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100638 break;
639
640 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200641 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100642 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
643 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
644 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
645 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
646 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
647 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
648 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
649 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
650 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200651 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100652 break;
653 default:
654 lua_pushstring(L, "");
655 break;
656 }
657 break;
658
659 case SMP_T_SINT:
660 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100661 case SMP_T_IPV4:
662 case SMP_T_IPV6:
663 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200664 if (sample_casts[smp->data.type][SMP_T_STR] &&
665 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200666 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100667 else
668 lua_pushstring(L, "");
669 break;
670 default:
671 lua_pushstring(L, "");
672 break;
673 }
674 return 1;
675}
676
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100677/* the following functions are used to convert an Lua type in a
678 * struct sample. This is useful to provide data from a converter
679 * to the LUA code.
680 */
681static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
682{
683 switch (lua_type(L, ud)) {
684
685 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200686 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200687 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100688 break;
689
690
691 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200692 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200693 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100694 break;
695
696 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200697 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100698 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200699 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200700 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200701 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200702 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100703 break;
704
705 case LUA_TUSERDATA:
706 case LUA_TNIL:
707 case LUA_TTABLE:
708 case LUA_TFUNCTION:
709 case LUA_TTHREAD:
710 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200711 case LUA_TNONE:
712 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200713 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200714 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100715 break;
716 }
717 return 1;
718}
719
Ilya Shipitsind4259502020-04-08 01:07:56 +0500720/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800721 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100722 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100723 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500724 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100725 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100726 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100727__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100728 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100729{
730 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200731 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100732 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200733 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200734 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200735 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200736 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100737
738 idx = 0;
739 min_arg = ARGM(mask);
740 mask >>= ARGM_BITS;
741
742 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200743 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100744
745 /* Check oversize. */
746 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200747 msg = "Malformed argument mask";
748 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100749 }
750
751 /* Check for mandatory arguments. */
752 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100753 if (idx < min_arg) {
754
755 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200756 if (idx > 0) {
757 msg = "Mandatory argument expected";
758 goto error;
759 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100760
761 /* If first argument have a certain type, some default values
762 * may be used. See the function smp_resolve_args().
763 */
764 switch (mask & ARGT_MASK) {
765
766 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200767 if (!(p->cap & PR_CAP_FE)) {
768 msg = "Mandatory argument expected";
769 goto error;
770 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100771 argp[idx].data.prx = p;
772 argp[idx].type = ARGT_FE;
773 argp[idx+1].type = ARGT_STOP;
774 break;
775
776 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200777 if (!(p->cap & PR_CAP_BE)) {
778 msg = "Mandatory argument expected";
779 goto error;
780 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100781 argp[idx].data.prx = p;
782 argp[idx].type = ARGT_BE;
783 argp[idx+1].type = ARGT_STOP;
784 break;
785
786 case ARGT_TAB:
Olivier Houcharda2658272022-09-13 00:35:53 +0200787 if (!p->table) {
788 msg = "Mandatory argument expected";
789 goto error;
790 }
791 argp[idx].data.t = p->table;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100792 argp[idx].type = ARGT_TAB;
793 argp[idx+1].type = ARGT_STOP;
794 break;
795
796 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200797 msg = "Mandatory argument expected";
798 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100799 break;
800 }
801 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200802 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100803 }
804
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500805 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100806 if ((mask & ARGT_MASK) == ARGT_STOP &&
807 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200808 msg = "Last argument expected";
809 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100810 }
811
812 if ((mask & ARGT_MASK) == ARGT_STOP &&
813 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200814 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100815 }
816
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200817 /* Convert some argument types. All string in argp[] are for not
818 * duplicated yet.
819 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100820 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100821 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200822 if (argp[idx].type != ARGT_SINT) {
823 msg = "integer expected";
824 goto error;
825 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100826 argp[idx].type = ARGT_SINT;
827 break;
828
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100829 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200830 if (argp[idx].type != ARGT_SINT) {
831 msg = "integer expected";
832 goto error;
833 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200834 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100835 break;
836
837 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200838 if (argp[idx].type != ARGT_SINT) {
839 msg = "integer expected";
840 goto error;
841 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200842 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100843 break;
844
845 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200846 if (argp[idx].type != ARGT_STR) {
847 msg = "string expected";
848 goto error;
849 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200850 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200851 if (!argp[idx].data.prx) {
852 msg = "frontend doesn't exist";
853 goto error;
854 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100855 argp[idx].type = ARGT_FE;
856 break;
857
858 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200859 if (argp[idx].type != ARGT_STR) {
860 msg = "string expected";
861 goto error;
862 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200863 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200864 if (!argp[idx].data.prx) {
865 msg = "backend doesn't exist";
866 goto error;
867 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100868 argp[idx].type = ARGT_BE;
869 break;
870
871 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200872 if (argp[idx].type != ARGT_STR) {
873 msg = "string expected";
874 goto error;
875 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200876 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200877 if (!argp[idx].data.t) {
878 msg = "table doesn't exist";
879 goto error;
880 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100881 argp[idx].type = ARGT_TAB;
882 break;
883
884 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200885 if (argp[idx].type != ARGT_STR) {
886 msg = "string expected";
887 goto error;
888 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200889 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100890 if (sname) {
891 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200892 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200893 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200894 if (!px) {
895 msg = "backend doesn't exist";
896 goto error;
897 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100898 }
899 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200900 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100901 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100902 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100903 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200904 if (!argp[idx].data.srv) {
905 msg = "server doesn't exist";
906 goto error;
907 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100908 argp[idx].type = ARGT_SRV;
909 break;
910
911 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200912 if (argp[idx].type != ARGT_STR) {
913 msg = "string expected";
914 goto error;
915 }
916 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
917 msg = "invalid IPv4 address";
918 goto error;
919 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100920 argp[idx].type = ARGT_IPV4;
921 break;
922
923 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200924 if (argp[idx].type == ARGT_SINT)
925 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
926 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200927 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
928 msg = "invalid IPv4 mask";
929 goto error;
930 }
931 }
932 else {
933 msg = "integer or string expected";
934 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200935 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100936 argp[idx].type = ARGT_MSK4;
937 break;
938
939 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200940 if (argp[idx].type != ARGT_STR) {
941 msg = "string expected";
942 goto error;
943 }
944 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
945 msg = "invalid IPv6 address";
946 goto error;
947 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100948 argp[idx].type = ARGT_IPV6;
949 break;
950
951 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200952 if (argp[idx].type == ARGT_SINT)
953 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
954 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200955 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
956 msg = "invalid IPv6 mask";
957 goto error;
958 }
959 }
960 else {
961 msg = "integer or string expected";
962 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200963 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100964 argp[idx].type = ARGT_MSK6;
965 break;
966
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200967 case ARGT_REG:
968 if (argp[idx].type != ARGT_STR) {
969 msg = "string expected";
970 goto error;
971 }
972 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
973 if (!reg) {
974 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
975 argp[idx].data.str.area, err);
976 free(err);
977 goto error;
978 }
979 argp[idx].type = ARGT_REG;
980 argp[idx].data.reg = reg;
981 break;
982
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100983 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200984 if (argp[idx].type != ARGT_STR) {
985 msg = "string expected";
986 goto error;
987 }
988 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100989 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200990 ul = p->uri_auth->userlist;
991 else
992 ul = auth_find_userlist(argp[idx].data.str.area);
993
994 if (!ul) {
995 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
996 goto error;
997 }
998 argp[idx].type = ARGT_USR;
999 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001000 break;
1001
1002 case ARGT_STR:
1003 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
1004 msg = "unable to duplicate string arg";
1005 goto error;
1006 }
1007 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001008 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001009
Christopher Fauletd25d9262020-08-06 11:04:46 +02001010 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +02001011 msg = "type not yet supported";
1012 goto error;
1013 break;
1014
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001015 }
1016
1017 /* Check for type of argument. */
1018 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001019 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
1020 arg_type_names[(mask & ARGT_MASK)],
1021 arg_type_names[argp[idx].type & ARGT_MASK]);
1022 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001023 }
1024
1025 /* Next argument. */
1026 mask >>= ARGT_BITS;
1027 idx++;
1028 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001029 return 0;
1030
1031 error:
Olivier Houchardda25dac2022-09-13 00:31:17 +02001032 argp[idx].type = ARGT_STOP;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001033 for (i = 0; i < idx; i++) {
1034 if (argp[i].type == ARGT_STR)
1035 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02001036 else if (argp[i].type == ARGT_REG)
1037 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001038 }
1039 WILL_LJMP(luaL_argerror(L, first + idx, msg));
1040 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001041}
1042
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001043/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001044 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001045 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001046 *
1047 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001048 * - hlua_sethlua : create the association between hlua context and lua_state.
1049 */
1050static inline struct hlua *hlua_gethlua(lua_State *L)
1051{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001052 struct hlua **hlua = lua_getextraspace(L);
1053 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001054}
1055static inline void hlua_sethlua(struct hlua *hlua)
1056{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001057 struct hlua **hlua_store = lua_getextraspace(hlua->T);
1058 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001059}
1060
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001061/* This function is used to send logs. It try to send on screen (stderr)
1062 * and on the default syslog server.
1063 */
1064static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
1065{
1066 struct tm tm;
1067 char *p;
1068
1069 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001070 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001071 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001072 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001073 /* Break the message if exceed the buffer size. */
1074 *(p-4) = ' ';
1075 *(p-3) = '.';
1076 *(p-2) = '.';
1077 *(p-1) = '.';
1078 break;
1079 }
Willy Tarreau90807112020-02-25 08:16:33 +01001080 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001081 *p = *msg;
1082 else
1083 *p = '.';
1084 }
1085 *p = '\0';
1086
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001087 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001088 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001089 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1090 return;
1091
Willy Tarreaua678b432015-08-28 10:14:59 +02001092 get_localtime(date.tv_sec, &tm);
1093 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001094 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001095 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001096 fflush(stderr);
1097 }
1098}
1099
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001100/* This function just ensure that the yield will be always
1101 * returned with a timeout and permit to set some flags
1102 */
1103__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001104 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001105{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001106 struct hlua *hlua;
1107
1108 /* Get hlua struct, or NULL if we execute from main lua state */
1109 hlua = hlua_gethlua(L);
1110 if (!hlua) {
1111 return;
1112 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001113
1114 /* Set the wake timeout. If timeout is required, we set
1115 * the expiration time.
1116 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001117 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001118
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001119 hlua->flags |= flags;
1120
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001121 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001122 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001123}
1124
Willy Tarreau87b09662015-04-03 00:22:06 +02001125/* This function initialises the Lua environment stored in the stream.
1126 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001127 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001128 *
1129 * This function is particular. it initialises a new Lua thread. If the
1130 * initialisation fails (example: out of memory error), the lua function
1131 * throws an error (longjmp).
1132 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001133 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001134 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001135 * threads appear, the safe environment set a lock to ensure only one
1136 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001137 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001138 *
1139 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001140 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001141 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001142 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001143 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001144 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001145 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001146 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001147int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001148{
1149 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001150 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001151 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001152 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001153 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001154 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001155 if (!already_safe) {
1156 if (!SET_SAFE_LJMP_PARENT(lua)) {
1157 lua->Tref = LUA_REFNIL;
1158 return 0;
1159 }
1160 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001161 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 if (!lua->T) {
1163 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001164 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001165 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 return 0;
1167 }
1168 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001169 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001171 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001172 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 return 1;
1174}
1175
Willy Tarreau87b09662015-04-03 00:22:06 +02001176/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001177 * is destroyed. The destroy also the memory context. The struct "lua"
Aurelien DARRAGONd5c8a102023-03-01 16:45:50 +01001178 * will be freed.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001179 */
1180void hlua_ctx_destroy(struct hlua *lua)
1181{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001182 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001183 return;
1184
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001185 if (!lua->T)
1186 goto end;
1187
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001188 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001189 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001190
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001191 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001192 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001194 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001195
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001196 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001197 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001198 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001199 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001200 /* Forces a garbage collecting process. If the Lua program is finished
1201 * without error, we run the GC on the thread pointer. Its freed all
1202 * the unused memory.
1203 * If the thread is finnish with an error or is currently yielded,
1204 * it seems that the GC applied on the thread doesn't clean anything,
1205 * so e run the GC on the main thread.
1206 * NOTE: maybe this action locks all the Lua threads untiml the en of
1207 * the garbage collection.
1208 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001209 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001210 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001211 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001212 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001213 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001214 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001215
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001216 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001217
1218end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001219 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001220}
1221
1222/* This function is used to restore the Lua context when a coroutine
1223 * fails. This function copy the common memory between old coroutine
1224 * and the new coroutine. The old coroutine is destroyed, and its
1225 * replaced by the new coroutine.
1226 * If the flag "keep_msg" is set, the last entry of the old is assumed
1227 * as string error message and it is copied in the new stack.
1228 */
1229static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1230{
1231 lua_State *T;
1232 int new_ref;
1233
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001234 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001235 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001236 if (!T)
1237 return 0;
1238
1239 /* Copy last error message. */
1240 if (keep_msg)
1241 lua_xmove(lua->T, T, 1);
1242
1243 /* Copy data between the coroutines. */
1244 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1245 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001246 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001247
1248 /* Destroy old data. */
1249 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1250
1251 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001252 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001253
1254 /* Fill the struct with the new coroutine values. */
1255 lua->Mref = new_ref;
1256 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001257 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001258
1259 /* Set context. */
1260 hlua_sethlua(lua);
1261
1262 return 1;
1263}
1264
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001265void hlua_hook(lua_State *L, lua_Debug *ar)
1266{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001267 struct hlua *hlua;
1268
1269 /* Get hlua struct, or NULL if we execute from main lua state */
1270 hlua = hlua_gethlua(L);
1271 if (!hlua)
1272 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001273
1274 /* Lua cannot yield when its returning from a function,
1275 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001276 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001277 */
1278 if (lua_gethookmask(L) & LUA_MASKRET) {
1279 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1280 return;
1281 }
1282
1283 /* restore the interrupt condition. */
1284 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1285
1286 /* If we interrupt the Lua processing in yieldable state, we yield.
1287 * If the state is not yieldable, trying yield causes an error.
1288 */
1289 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001290 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001291
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001292 /* If we cannot yield, update the clock and check the timeout. */
1293 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001294 hlua->run_time += now_ms - hlua->start_time;
1295 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001296 lua_pushfstring(L, "execution timeout");
1297 WILL_LJMP(lua_error(L));
1298 }
1299
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001300 /* Update the start time. */
1301 hlua->start_time = now_ms;
1302
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001303 /* Try to interrupt the process at the end of the current
1304 * unyieldable function.
1305 */
1306 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001307}
1308
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001309/* This function start or resumes the Lua stack execution. If the flag
1310 * "yield_allowed" if no set and the LUA stack execution returns a yield
1311 * The function return an error.
1312 *
1313 * The function can returns 4 values:
1314 * - HLUA_E_OK : The execution is terminated without any errors.
1315 * - HLUA_E_AGAIN : The execution must continue at the next associated
1316 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001317 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001318 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001319 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001320 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001321 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001322 * LUA code.
1323 */
1324static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1325{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001326#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1327 int nres;
1328#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001329 int ret;
1330 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001331 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001332
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001333 /* Initialise run time counter. */
1334 if (!HLUA_IS_RUNNING(lua))
1335 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001336
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001337 /* Lock the whole Lua execution. This lock must be before the
1338 * label "resume_execution".
1339 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001340 if (lua->state_id == 0)
Willy Tarreau3eb2e472021-08-20 15:47:25 +02001341 lua_take_global_lock();
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001342
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001343resume_execution:
1344
1345 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1346 * instructions. it is used for preventing infinite loops.
1347 */
1348 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1349
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001350 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001351 HLUA_SET_RUN(lua);
1352 HLUA_CLR_CTRLYIELD(lua);
1353 HLUA_CLR_WAKERESWR(lua);
1354 HLUA_CLR_WAKEREQWR(lua);
Christopher Fauleta6e792f2021-08-04 17:58:21 +02001355 HLUA_CLR_NOYIELD(lua);
1356 if (!yield_allowed)
1357 HLUA_SET_NOYIELD(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001358
Christopher Fauletbc275a92020-02-26 14:55:16 +01001359 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001360 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001361 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001362
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001363 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001364#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001365 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001366#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001367 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001368#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001369 switch (ret) {
1370
1371 case LUA_OK:
1372 ret = HLUA_E_OK;
1373 break;
1374
1375 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001376 /* Check if the execution timeout is expired. It it is the case, we
1377 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001378 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001379 tv_update_date(0, 1);
1380 lua->run_time += now_ms - lua->start_time;
1381 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001382 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001383 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001384 break;
1385 }
1386 /* Process the forced yield. if the general yield is not allowed or
1387 * if no task were associated this the current Lua execution
1388 * coroutine, we resume the execution. Else we want to return in the
1389 * scheduler and we want to be waked up again, to continue the
1390 * current Lua execution. So we schedule our own task.
1391 */
1392 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001393 if (!yield_allowed || !lua->task)
1394 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001395 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001396 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001397 if (!yield_allowed) {
1398 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001399 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001400 break;
1401 }
1402 ret = HLUA_E_AGAIN;
1403 break;
1404
1405 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001406
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001407 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001408 * because the errors ares the only one mean to return immediately
1409 * from and lua execution.
1410 */
1411 if (lua->flags & HLUA_EXIT) {
1412 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001413 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001414 break;
1415 }
1416
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001417 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001418 if (!lua_checkstack(lua->T, 1)) {
1419 ret = HLUA_E_ERR;
1420 break;
1421 }
1422 msg = lua_tostring(lua->T, -1);
1423 lua_settop(lua->T, 0); /* Empty the stack. */
1424 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001425 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001426 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001427 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001428 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001429 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001430 ret = HLUA_E_ERRMSG;
1431 break;
1432
1433 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001434 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001435 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001436 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001437 break;
1438
1439 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001440 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001441 if (!lua_checkstack(lua->T, 1)) {
1442 ret = HLUA_E_ERR;
1443 break;
1444 }
1445 msg = lua_tostring(lua->T, -1);
1446 lua_settop(lua->T, 0); /* Empty the stack. */
1447 lua_pop(lua->T, 1);
1448 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001449 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001450 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001451 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001452 ret = HLUA_E_ERRMSG;
1453 break;
1454
1455 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001456 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001457 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001458 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001459 break;
1460 }
1461
1462 switch (ret) {
1463 case HLUA_E_AGAIN:
1464 break;
1465
1466 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001467 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001468 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001469 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001470 break;
1471
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001472 case HLUA_E_ETMOUT:
1473 case HLUA_E_NOMEM:
1474 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001475 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001476 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001477 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001478 hlua_ctx_renew(lua, 0);
1479 break;
1480
1481 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001482 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001483 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001484 break;
1485 }
1486
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001487 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001488 if (lua->state_id == 0)
Willy Tarreau3eb2e472021-08-20 15:47:25 +02001489 lua_drop_global_lock();
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001490
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001491 return ret;
1492}
1493
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001494/* This function exit the current code. */
1495__LJMP static int hlua_done(lua_State *L)
1496{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001497 struct hlua *hlua;
1498
1499 /* Get hlua struct, or NULL if we execute from main lua state */
1500 hlua = hlua_gethlua(L);
1501 if (!hlua)
1502 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001503
1504 hlua->flags |= HLUA_EXIT;
1505 WILL_LJMP(lua_error(L));
1506
1507 return 0;
1508}
1509
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001510/* This function is an LUA binding. It provides a function
1511 * for deleting ACL from a referenced ACL file.
1512 */
1513__LJMP static int hlua_del_acl(lua_State *L)
1514{
1515 const char *name;
1516 const char *key;
1517 struct pat_ref *ref;
1518
1519 MAY_LJMP(check_args(L, 2, "del_acl"));
1520
1521 name = MAY_LJMP(luaL_checkstring(L, 1));
1522 key = MAY_LJMP(luaL_checkstring(L, 2));
1523
1524 ref = pat_ref_lookup(name);
1525 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001526 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001527
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001528 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001529 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001530 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001531 return 0;
1532}
1533
1534/* This function is an LUA binding. It provides a function
1535 * for deleting map entry from a referenced map file.
1536 */
1537static int hlua_del_map(lua_State *L)
1538{
1539 const char *name;
1540 const char *key;
1541 struct pat_ref *ref;
1542
1543 MAY_LJMP(check_args(L, 2, "del_map"));
1544
1545 name = MAY_LJMP(luaL_checkstring(L, 1));
1546 key = MAY_LJMP(luaL_checkstring(L, 2));
1547
1548 ref = pat_ref_lookup(name);
1549 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001550 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001551
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001552 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001553 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001554 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001555 return 0;
1556}
1557
1558/* This function is an LUA binding. It provides a function
1559 * for adding ACL pattern from a referenced ACL file.
1560 */
1561static int hlua_add_acl(lua_State *L)
1562{
1563 const char *name;
1564 const char *key;
1565 struct pat_ref *ref;
1566
1567 MAY_LJMP(check_args(L, 2, "add_acl"));
1568
1569 name = MAY_LJMP(luaL_checkstring(L, 1));
1570 key = MAY_LJMP(luaL_checkstring(L, 2));
1571
1572 ref = pat_ref_lookup(name);
1573 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001574 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001575
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001576 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001577 if (pat_ref_find_elt(ref, key) == NULL)
1578 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001579 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001580 return 0;
1581}
1582
1583/* This function is an LUA binding. It provides a function
1584 * for setting map pattern and sample from a referenced map
1585 * file.
1586 */
1587static int hlua_set_map(lua_State *L)
1588{
1589 const char *name;
1590 const char *key;
1591 const char *value;
1592 struct pat_ref *ref;
1593
1594 MAY_LJMP(check_args(L, 3, "set_map"));
1595
1596 name = MAY_LJMP(luaL_checkstring(L, 1));
1597 key = MAY_LJMP(luaL_checkstring(L, 2));
1598 value = MAY_LJMP(luaL_checkstring(L, 3));
1599
1600 ref = pat_ref_lookup(name);
1601 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001602 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001603
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001604 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001605 if (pat_ref_find_elt(ref, key) != NULL)
1606 pat_ref_set(ref, key, value, NULL);
1607 else
1608 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001609 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001610 return 0;
1611}
1612
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001613/* A class is a lot of memory that contain data. This data can be a table,
1614 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001615 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001616 * the name of the object (_G[<name>] = <metable> ).
1617 *
1618 * A metable is a table that modify the standard behavior of a standard
1619 * access to the associated data. The entries of this new metatable are
1620 * defined as is:
1621 *
1622 * http://lua-users.org/wiki/MetatableEvents
1623 *
1624 * __index
1625 *
1626 * we access an absent field in a table, the result is nil. This is
1627 * true, but it is not the whole truth. Actually, such access triggers
1628 * the interpreter to look for an __index metamethod: If there is no
1629 * such method, as usually happens, then the access results in nil;
1630 * otherwise, the metamethod will provide the result.
1631 *
1632 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1633 * the key does not appear in the table, but the metatable has an __index
1634 * property:
1635 *
1636 * - if the value is a function, the function is called, passing in the
1637 * table and the key; the return value of that function is returned as
1638 * the result.
1639 *
1640 * - if the value is another table, the value of the key in that table is
1641 * asked for and returned (and if it doesn't exist in that table, but that
1642 * table's metatable has an __index property, then it continues on up)
1643 *
1644 * - Use "rawget(myTable,key)" to skip this metamethod.
1645 *
1646 * http://www.lua.org/pil/13.4.1.html
1647 *
1648 * __newindex
1649 *
1650 * Like __index, but control property assignment.
1651 *
1652 * __mode - Control weak references. A string value with one or both
1653 * of the characters 'k' and 'v' which specifies that the the
1654 * keys and/or values in the table are weak references.
1655 *
1656 * __call - Treat a table like a function. When a table is followed by
1657 * parenthesis such as "myTable( 'foo' )" and the metatable has
1658 * a __call key pointing to a function, that function is invoked
1659 * (passing any specified arguments) and the return value is
1660 * returned.
1661 *
1662 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1663 * called, if the metatable for myTable has a __metatable
1664 * key, the value of that key is returned instead of the
1665 * actual metatable.
1666 *
1667 * __tostring - Control string representation. When the builtin
1668 * "tostring( myTable )" function is called, if the metatable
1669 * for myTable has a __tostring property set to a function,
1670 * that function is invoked (passing myTable to it) and the
1671 * return value is used as the string representation.
1672 *
1673 * __len - Control table length. When the table length is requested using
1674 * the length operator ( '#' ), if the metatable for myTable has
1675 * a __len key pointing to a function, that function is invoked
1676 * (passing myTable to it) and the return value used as the value
1677 * of "#myTable".
1678 *
1679 * __gc - Userdata finalizer code. When userdata is set to be garbage
1680 * collected, if the metatable has a __gc field pointing to a
1681 * function, that function is first invoked, passing the userdata
1682 * to it. The __gc metamethod is not called for tables.
1683 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1684 *
1685 * Special metamethods for redefining standard operators:
1686 * http://www.lua.org/pil/13.1.html
1687 *
1688 * __add "+"
1689 * __sub "-"
1690 * __mul "*"
1691 * __div "/"
1692 * __unm "!"
1693 * __pow "^"
1694 * __concat ".."
1695 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001696 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001697 * http://www.lua.org/pil/13.2.html
1698 *
1699 * __eq "=="
1700 * __lt "<"
1701 * __le "<="
1702 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703
1704/*
1705 *
1706 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001707 * Class Map
1708 *
1709 *
1710 */
1711
1712/* Returns a struct hlua_map if the stack entry "ud" is
1713 * a class session, otherwise it throws an error.
1714 */
1715__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1716{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001717 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001718}
1719
1720/* This function is the map constructor. It don't need
1721 * the class Map object. It creates and return a new Map
1722 * object. It must be called only during "body" or "init"
1723 * context because it process some filesystem accesses.
1724 */
1725__LJMP static int hlua_map_new(struct lua_State *L)
1726{
1727 const char *fn;
1728 int match = PAT_MATCH_STR;
1729 struct sample_conv conv;
1730 const char *file = "";
1731 int line = 0;
1732 lua_Debug ar;
1733 char *err = NULL;
1734 struct arg args[2];
1735
1736 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1737 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1738
1739 fn = MAY_LJMP(luaL_checkstring(L, 1));
1740
1741 if (lua_gettop(L) >= 2) {
1742 match = MAY_LJMP(luaL_checkinteger(L, 2));
1743 if (match < 0 || match >= PAT_MATCH_NUM)
1744 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1745 }
1746
1747 /* Get Lua filename and line number. */
1748 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1749 lua_getinfo(L, "Sl", &ar); /* get info about it */
1750 if (ar.currentline > 0) { /* is there info? */
1751 file = ar.short_src;
1752 line = ar.currentline;
1753 }
1754 }
1755
1756 /* fill fake sample_conv struct. */
1757 conv.kw = ""; /* unused. */
1758 conv.process = NULL; /* unused. */
1759 conv.arg_mask = 0; /* unused. */
1760 conv.val_args = NULL; /* unused. */
1761 conv.out_type = SMP_T_STR;
1762 conv.private = (void *)(long)match;
1763 switch (match) {
1764 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1765 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1766 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1767 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1768 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1769 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1770 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001771 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001772 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1773 default:
1774 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1775 }
1776
1777 /* fill fake args. */
1778 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001779 args[0].data.str.area = strdup(fn);
1780 args[0].data.str.data = strlen(fn);
1781 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001782 args[1].type = ARGT_STOP;
1783
1784 /* load the map. */
1785 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001786 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001787 * free the err variable.
1788 */
1789 luaL_where(L, 1);
1790 lua_pushfstring(L, "'new': %s.", err);
1791 lua_concat(L, 2);
1792 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001793 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001794 WILL_LJMP(lua_error(L));
1795 }
1796
1797 /* create the lua object. */
1798 lua_newtable(L);
1799 lua_pushlightuserdata(L, args[0].data.map);
1800 lua_rawseti(L, -2, 0);
1801
1802 /* Pop a class Map metatable and affect it to the userdata. */
1803 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1804 lua_setmetatable(L, -2);
1805
1806
1807 return 1;
1808}
1809
1810__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1811{
1812 struct map_descriptor *desc;
1813 struct pattern *pat;
1814 struct sample smp;
1815
1816 MAY_LJMP(check_args(L, 2, "lookup"));
1817 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001818 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001819 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001820 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001821 }
1822 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001823 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001824 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001825 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 +01001826 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001827 }
1828
1829 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001830 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001831 if (str)
1832 lua_pushstring(L, "");
1833 else
1834 lua_pushnil(L);
1835 return 1;
1836 }
1837
1838 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001839 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001840 return 1;
1841}
1842
1843__LJMP static int hlua_map_lookup(struct lua_State *L)
1844{
1845 return _hlua_map_lookup(L, 0);
1846}
1847
1848__LJMP static int hlua_map_slookup(struct lua_State *L)
1849{
1850 return _hlua_map_lookup(L, 1);
1851}
1852
1853/*
1854 *
1855 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001856 * Class Socket
1857 *
1858 *
1859 */
1860
1861__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1862{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001863 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001864}
1865
1866/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001867 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868 * received.
1869 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001870static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001871{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001872 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001873
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001874 if (appctx->ctx.hlua_cosocket.die) {
1875 si_shutw(si);
1876 si_shutr(si);
1877 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001878 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1879 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001880 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1881 }
1882
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001883 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001884 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001885 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001886
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001887 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001888 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001889 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001890
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001891 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001892 * to be notified whenever the connection completes.
1893 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001894 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001895 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001896 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001897 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001898 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001899 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900
1901 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001902 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001904 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001905 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001906 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907
1908 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001909 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001910 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001911
1912 /* Some data were injected in the buffer, notify the stream
1913 * interface.
1914 */
1915 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001916 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001917
1918 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001919 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001920 */
1921 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001922 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923}
1924
Willy Tarreau87b09662015-04-03 00:22:06 +02001925/* This function is called when the "struct stream" is destroyed.
1926 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927 * Wake all the pending signals.
1928 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001929static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001931 struct xref *peer;
1932
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001934 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1935 if (peer)
1936 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001937
1938 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001939 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1940 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001941}
1942
1943/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001944 * uses this object. If the stream does not exists, just quit.
1945 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946 * pending signal can rest in the read and write lists. destroy
1947 * it.
1948 */
1949__LJMP static int hlua_socket_gc(lua_State *L)
1950{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001951 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001952 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001953 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001954
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001955 MAY_LJMP(check_args(L, 1, "__gc"));
1956
1957 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001958 peer = xref_get_peer_and_lock(&socket->xref);
1959 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001961 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001963 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001964 appctx->ctx.hlua_cosocket.die = 1;
1965 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001966
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001967 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001968 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969 return 0;
1970}
1971
1972/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001973 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974 */
sada05ed3302018-05-11 11:48:18 -07001975__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001977 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001978 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001979 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001980 struct hlua *hlua;
1981
1982 /* Get hlua struct, or NULL if we execute from main lua state */
1983 hlua = hlua_gethlua(L);
1984 if (!hlua)
1985 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001987 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001988
1989 /* Check if we run on the same thread than the xreator thread.
1990 * We cannot access to the socket if the thread is different.
1991 */
1992 if (socket->tid != tid)
1993 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1994
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001995 peer = xref_get_peer_and_lock(&socket->xref);
1996 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001998
1999 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002000 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002001
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002002 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002003 appctx->ctx.hlua_cosocket.die = 1;
2004 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002006 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002007 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002008 return 0;
2009}
2010
sada05ed3302018-05-11 11:48:18 -07002011/* The close function calls close_helper.
2012 */
2013__LJMP static int hlua_socket_close(lua_State *L)
2014{
2015 MAY_LJMP(check_args(L, 1, "close"));
2016 return hlua_socket_close_helper(L);
2017}
2018
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002019/* This Lua function assumes that the stack contain three parameters.
2020 * 1 - USERDATA containing a struct socket
2021 * 2 - INTEGER with values of the macro defined below
2022 * If the integer is -1, we must read at most one line.
2023 * If the integer is -2, we ust read all the data until the
2024 * end of the stream.
2025 * If the integer is positive value, we must read a number of
2026 * bytes corresponding to this value.
2027 */
2028#define HLSR_READ_LINE (-1)
2029#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002030__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002031{
2032 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2033 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002034 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002035 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002036 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02002038 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002039 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02002040 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002041 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002042 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01002043 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002044 struct stream_interface *si;
2045 struct stream *s;
2046 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002047 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002049 /* Get hlua struct, or NULL if we execute from main lua state */
2050 hlua = hlua_gethlua(L);
2051
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002052 /* Check if this lua stack is schedulable. */
2053 if (!hlua || !hlua->task)
2054 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
2055 "'frontend', 'backend' or 'task'"));
2056
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002057 /* Check if we run on the same thread than the xreator thread.
2058 * We cannot access to the socket if the thread is different.
2059 */
2060 if (socket->tid != tid)
2061 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2062
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002063 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002064 peer = xref_get_peer_and_lock(&socket->xref);
2065 if (!peer)
2066 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002067 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2068 si = appctx->owner;
2069 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002071 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002072 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002074 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075 if (nblk < 0) /* Connection close. */
2076 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002077 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002079
2080 /* remove final \r\n. */
2081 if (nblk == 1) {
2082 if (blk1[len1-1] == '\n') {
2083 len1--;
2084 skip_at_end++;
2085 if (blk1[len1-1] == '\r') {
2086 len1--;
2087 skip_at_end++;
2088 }
2089 }
2090 }
2091 else {
2092 if (blk2[len2-1] == '\n') {
2093 len2--;
2094 skip_at_end++;
2095 if (blk2[len2-1] == '\r') {
2096 len2--;
2097 skip_at_end++;
2098 }
2099 }
2100 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 }
2102
2103 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002104 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002105 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002106 if (nblk < 0) /* Connection close. */
2107 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002108 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002109 goto connection_empty;
2110 }
2111
2112 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002113 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002114 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002115 if (nblk < 0) /* Connection close. */
2116 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002117 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002118 goto connection_empty;
2119
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002120 missing_bytes = wanted - socket->b.n;
2121 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002122 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002123 len1 = missing_bytes;
2124 } if (nblk == 2 && len1 + len2 > missing_bytes)
2125 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002126 }
2127
2128 len = len1;
2129
2130 luaL_addlstring(&socket->b, blk1, len1);
2131 if (nblk == 2) {
2132 len += len2;
2133 luaL_addlstring(&socket->b, blk2, len2);
2134 }
2135
2136 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002137 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002138
2139 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002140 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002141
2142 /* If the pattern reclaim to read all the data
2143 * in the connection, got out.
2144 */
2145 if (wanted == HLSR_READ_ALL)
2146 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002147 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 goto connection_empty;
2149
2150 /* Return result. */
2151 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002152 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002153 return 1;
2154
2155connection_closed:
2156
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002157 xref_unlock(&socket->xref, peer);
2158
2159no_peer:
2160
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002161 /* If the buffer containds data. */
2162 if (socket->b.n > 0) {
2163 luaL_pushresult(&socket->b);
2164 return 1;
2165 }
2166 lua_pushnil(L);
2167 lua_pushstring(L, "connection closed.");
2168 return 2;
2169
2170connection_empty:
2171
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002172 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2173 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002174 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002175 }
2176 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002177 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178 return 0;
2179}
2180
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002181/* This Lua function gets two parameters. The first one can be string
2182 * or a number. If the string is "*l", the user requires one line. If
2183 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002184 * If the value is a number, the user require a number of bytes equal
2185 * to the value. The default value is "*l" (a line).
2186 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002187 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002188 * integer takes this values:
2189 * -1 : read a line
2190 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002191 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002192 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002193 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002194 * concatenated with the read data.
2195 */
2196__LJMP static int hlua_socket_receive(struct lua_State *L)
2197{
2198 int wanted = HLSR_READ_LINE;
2199 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002200 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201 char *error;
2202 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002203 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204
2205 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2206 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2207
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002208 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002209
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002210 /* Check if we run on the same thread than the xreator thread.
2211 * We cannot access to the socket if the thread is different.
2212 */
2213 if (socket->tid != tid)
2214 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2215
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002216 /* check for pattern. */
2217 if (lua_gettop(L) >= 2) {
2218 type = lua_type(L, 2);
2219 if (type == LUA_TSTRING) {
2220 pattern = lua_tostring(L, 2);
2221 if (strcmp(pattern, "*a") == 0)
2222 wanted = HLSR_READ_ALL;
2223 else if (strcmp(pattern, "*l") == 0)
2224 wanted = HLSR_READ_LINE;
2225 else {
2226 wanted = strtoll(pattern, &error, 10);
2227 if (*error != '\0')
2228 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2229 }
2230 }
2231 else if (type == LUA_TNUMBER) {
2232 wanted = lua_tointeger(L, 2);
2233 if (wanted < 0)
2234 WILL_LJMP(luaL_error(L, "Unsupported size."));
2235 }
2236 }
2237
2238 /* Set pattern. */
2239 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002240
2241 /* Check if we would replace the top by itself. */
2242 if (lua_gettop(L) != 2)
2243 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244
Christopher Fauletc31b2002021-05-03 10:11:13 +02002245 /* Save index of the top of the stack because since buffers are used, it
2246 * may change
2247 */
2248 lastarg = lua_gettop(L);
2249
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002250 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002251 luaL_buffinit(L, &socket->b);
2252
2253 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002254 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002255 if (lua_type(L, 3) != LUA_TSTRING)
2256 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2257 pattern = lua_tolstring(L, 3, &len);
2258 luaL_addlstring(&socket->b, pattern, len);
2259 }
2260
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002261 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262}
2263
2264/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002265 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002266 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002267static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002268{
2269 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002270 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271 struct appctx *appctx;
2272 size_t buf_len;
2273 const char *buf;
2274 int len;
2275 int send_len;
2276 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002277 struct xref *peer;
2278 struct stream_interface *si;
2279 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002281 /* Get hlua struct, or NULL if we execute from main lua state */
2282 hlua = hlua_gethlua(L);
2283
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284 /* Check if this lua stack is schedulable. */
2285 if (!hlua || !hlua->task)
2286 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2287 "'frontend', 'backend' or 'task'"));
2288
2289 /* Get object */
2290 socket = MAY_LJMP(hlua_checksocket(L, 1));
2291 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002292 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002293
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002294 /* Check if we run on the same thread than the xreator thread.
2295 * We cannot access to the socket if the thread is different.
2296 */
2297 if (socket->tid != tid)
2298 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2299
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002300 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002301 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002302 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002303 lua_pushinteger(L, -1);
2304 return 1;
2305 }
2306 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2307 si = appctx->owner;
2308 s = si_strm(si);
2309
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002311 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002312 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002313 lua_pushinteger(L, -1);
2314 return 1;
2315 }
2316
2317 /* Update the input buffer data. */
2318 buf += sent;
2319 send_len = buf_len - sent;
2320
2321 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002322 if (sent >= buf_len) {
2323 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002324 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002325 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002327 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002328 * the request buffer if its not required.
2329 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002330 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002331 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002332 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002333 }
2334
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002335 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002336 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002337 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002338 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002339 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340
2341 /* send data */
2342 if (len < send_len)
2343 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002344 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002345
2346 /* "Not enough space" (-1), "Buffer too little to contain
2347 * the data" (-2) are not expected because the available length
2348 * is tested.
2349 * Other unknown error are also not expected.
2350 */
2351 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002352 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002353 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002354
sada05ed3302018-05-11 11:48:18 -07002355 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002357 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002358 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359 return 1;
2360 }
2361
2362 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002363 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002364
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002365 s->req.rex = TICK_ETERNITY;
2366 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367
2368 /* Update length sent. */
2369 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002370 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371
2372 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002373 if (sent + len >= buf_len) {
2374 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002375 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002376 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002377
2378hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002379 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2380 xref_unlock(&socket->xref, peer);
2381 WILL_LJMP(luaL_error(L, "out of memory"));
2382 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002383 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002384 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385 return 0;
2386}
2387
2388/* This function initiate the send of data. It just check the input
2389 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002390 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002391 * "hlua_socket_write_yield" that can yield.
2392 *
2393 * The Lua function gets between 3 and 4 parameters. The first one is
2394 * the associated object. The second is a string buffer. The third is
2395 * a facultative integer that represents where is the buffer position
2396 * of the start of the data that can send. The first byte is the
2397 * position "1". The default value is "1". The fourth argument is a
2398 * facultative integer that represents where is the buffer position
2399 * of the end of the data that can send. The default is the last byte.
2400 */
2401static int hlua_socket_send(struct lua_State *L)
2402{
2403 int i;
2404 int j;
2405 const char *buf;
2406 size_t buf_len;
2407
2408 /* Check number of arguments. */
2409 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2410 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2411
2412 /* Get the string. */
2413 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2414
2415 /* Get and check j. */
2416 if (lua_gettop(L) == 4) {
2417 j = MAY_LJMP(luaL_checkinteger(L, 4));
2418 if (j < 0)
2419 j = buf_len + j + 1;
2420 if (j > buf_len)
2421 j = buf_len + 1;
2422 lua_pop(L, 1);
2423 }
2424 else
2425 j = buf_len;
2426
2427 /* Get and check i. */
2428 if (lua_gettop(L) == 3) {
2429 i = MAY_LJMP(luaL_checkinteger(L, 3));
2430 if (i < 0)
2431 i = buf_len + i + 1;
2432 if (i > buf_len)
2433 i = buf_len + 1;
2434 lua_pop(L, 1);
2435 } else
2436 i = 1;
2437
2438 /* Check bth i and j. */
2439 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002440 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002441 return 1;
2442 }
2443 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002444 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445 return 1;
2446 }
2447 if (i == 0)
2448 i = 1;
2449 if (j == 0)
2450 j = 1;
2451
2452 /* Pop the string. */
2453 lua_pop(L, 1);
2454
2455 /* Update the buffer length. */
2456 buf += i - 1;
2457 buf_len = j - i + 1;
2458 lua_pushlstring(L, buf, buf_len);
2459
2460 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002461 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002462
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002463 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002464}
2465
Willy Tarreau22b0a682015-06-17 19:43:49 +02002466#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002467__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2468{
2469 static char buffer[SOCKET_INFO_MAX_LEN];
2470 int ret;
2471 int len;
2472 char *p;
2473
2474 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2475 if (ret <= 0) {
2476 lua_pushnil(L);
2477 return 1;
2478 }
2479
2480 if (ret == AF_UNIX) {
2481 lua_pushstring(L, buffer+1);
2482 return 1;
2483 }
2484 else if (ret == AF_INET6) {
2485 buffer[0] = '[';
2486 len = strlen(buffer);
2487 buffer[len] = ']';
2488 len++;
2489 buffer[len] = ':';
2490 len++;
2491 p = buffer;
2492 }
2493 else if (ret == AF_INET) {
2494 p = buffer + 1;
2495 len = strlen(p);
2496 p[len] = ':';
2497 len++;
2498 }
2499 else {
2500 lua_pushnil(L);
2501 return 1;
2502 }
2503
2504 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2505 lua_pushnil(L);
2506 return 1;
2507 }
2508
2509 lua_pushstring(L, p);
2510 return 1;
2511}
2512
2513/* Returns information about the peer of the connection. */
2514__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2515{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002516 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002517 struct xref *peer;
2518 struct appctx *appctx;
2519 struct stream_interface *si;
2520 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002521 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002522
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002523 MAY_LJMP(check_args(L, 1, "getpeername"));
2524
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002525 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002526
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002527 /* Check if we run on the same thread than the xreator thread.
2528 * We cannot access to the socket if the thread is different.
2529 */
2530 if (socket->tid != tid)
2531 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2532
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002533 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002534 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002535 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002536 lua_pushnil(L);
2537 return 1;
2538 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002539 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2540 si = appctx->owner;
2541 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002542
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002543 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002544 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002545 lua_pushnil(L);
2546 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002547 }
2548
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002549 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002550 xref_unlock(&socket->xref, peer);
2551 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002552}
2553
2554/* Returns information about my connection side. */
2555static int hlua_socket_getsockname(struct lua_State *L)
2556{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002557 struct hlua_socket *socket;
2558 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002559 struct appctx *appctx;
2560 struct xref *peer;
2561 struct stream_interface *si;
2562 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002563 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002564
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002565 MAY_LJMP(check_args(L, 1, "getsockname"));
2566
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002567 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002568
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002569 /* Check if we run on the same thread than the xreator thread.
2570 * We cannot access to the socket if the thread is different.
2571 */
2572 if (socket->tid != tid)
2573 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2574
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002575 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002576 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002577 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002578 lua_pushnil(L);
2579 return 1;
2580 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002581 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2582 si = appctx->owner;
2583 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002585 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002586 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002587 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002588 lua_pushnil(L);
2589 return 1;
2590 }
2591
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002592 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002593 xref_unlock(&socket->xref, peer);
2594 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002595}
2596
2597/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002598static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002599 .obj_type = OBJ_TYPE_APPLET,
2600 .name = "<LUA_TCP>",
2601 .fct = hlua_socket_handler,
2602 .release = hlua_socket_release,
2603};
2604
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002605__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606{
2607 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002608 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002609 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002610 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002611 struct stream_interface *si;
2612 struct stream *s;
2613
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002614 /* Get hlua struct, or NULL if we execute from main lua state */
2615 hlua = hlua_gethlua(L);
2616 if (!hlua)
2617 return 0;
2618
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002619 /* Check if we run on the same thread than the xreator thread.
2620 * We cannot access to the socket if the thread is different.
2621 */
2622 if (socket->tid != tid)
2623 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2624
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002625 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002626 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002627 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002628 lua_pushnil(L);
2629 lua_pushstring(L, "Can't connect");
2630 return 2;
2631 }
2632 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2633 si = appctx->owner;
2634 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002636 /* Check if we run on the same thread than the xreator thread.
2637 * We cannot access to the socket if the thread is different.
2638 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002639 if (socket->tid != tid) {
2640 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002641 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002642 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002643
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002644 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002645 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002646 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647 lua_pushnil(L);
2648 lua_pushstring(L, "Can't connect");
2649 return 2;
2650 }
2651
Willy Tarreaue09101e2018-10-16 17:37:12 +02002652 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653
2654 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002655 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002656 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002657 lua_pushinteger(L, 1);
2658 return 1;
2659 }
2660
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002661 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2662 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002663 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002664 }
2665 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002666 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002667 return 0;
2668}
2669
2670/* This function fail or initite the connection. */
2671__LJMP static int hlua_socket_connect(struct lua_State *L)
2672{
2673 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002674 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002675 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002676 struct hlua *hlua;
2677 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002678 int low, high;
2679 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002680 struct xref *peer;
2681 struct stream_interface *si;
2682 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002683
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002684 if (lua_gettop(L) < 2)
2685 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002686
2687 /* Get args. */
2688 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002689
2690 /* Check if we run on the same thread than the xreator thread.
2691 * We cannot access to the socket if the thread is different.
2692 */
2693 if (socket->tid != tid)
2694 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2695
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002696 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002697 if (lua_gettop(L) >= 3) {
2698 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002699 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002700
Tim Duesterhus6edab862018-01-06 19:04:45 +01002701 /* Force the ip to end with a colon, to support IPv6 addresses
2702 * that are not enclosed within square brackets.
2703 */
2704 if (port > 0) {
2705 luaL_buffinit(L, &b);
2706 luaL_addstring(&b, ip);
2707 luaL_addchar(&b, ':');
2708 luaL_pushresult(&b);
2709 ip = lua_tolstring(L, lua_gettop(L), NULL);
2710 }
2711 }
2712
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002713 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002714 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002715 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002716 lua_pushnil(L);
2717 return 1;
2718 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002719
2720 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002721 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 +02002722 if (!addr) {
2723 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002724 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002725 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002726
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002727 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002728 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002729 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002730 if (port == -1) {
2731 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002732 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002733 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002734 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2735 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002736 if (port == -1) {
2737 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002738 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002739 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002740 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002741 }
2742 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002743
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002744 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2745 si = appctx->owner;
2746 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002747
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002748 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002749 xref_unlock(&socket->xref, peer);
2750 WILL_LJMP(luaL_error(L, "connect: internal error"));
2751 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002752 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002753
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002754 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002755 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002756 if (!hlua)
2757 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002758
2759 /* inform the stream that we want to be notified whenever the
2760 * connection completes.
2761 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002762 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002763 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002764 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002765
Willy Tarreauf31af932020-01-14 09:59:38 +01002766 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002767
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002768 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2769 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002770 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002771 }
2772 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002773
2774 task_wakeup(s->task, TASK_WOKEN_INIT);
2775 /* Return yield waiting for connection. */
2776
Willy Tarreau9635e032018-10-16 17:52:55 +02002777 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002778
2779 return 0;
2780}
2781
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002782#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002783__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2784{
2785 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002786 struct xref *peer;
2787 struct appctx *appctx;
2788 struct stream_interface *si;
2789 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002790
2791 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2792 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002793
2794 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002795 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002796 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002797 lua_pushnil(L);
2798 return 1;
2799 }
2800 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2801 si = appctx->owner;
2802 s = si_strm(si);
2803
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002804 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002805 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002806 return MAY_LJMP(hlua_socket_connect(L));
2807}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002808#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002809
2810__LJMP static int hlua_socket_setoption(struct lua_State *L)
2811{
2812 return 0;
2813}
2814
2815__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2816{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002817 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002818 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002819 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002820 struct xref *peer;
2821 struct appctx *appctx;
2822 struct stream_interface *si;
2823 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002824
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002825 MAY_LJMP(check_args(L, 2, "settimeout"));
2826
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002827 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002828
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002829 /* convert the timeout to millis */
2830 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002831
Thierry Fournier17a921b2018-03-08 09:59:02 +01002832 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002833 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002834 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2835
Mark Lakes56cc1252018-03-27 09:48:06 +02002836 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002837 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002838
2839 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002840 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002841 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002842
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002843 /* Check if we run on the same thread than the xreator thread.
2844 * We cannot access to the socket if the thread is different.
2845 */
2846 if (socket->tid != tid)
2847 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2848
Mark Lakes56cc1252018-03-27 09:48:06 +02002849 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002850 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002851 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002852 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2853 WILL_LJMP(lua_error(L));
2854 return 0;
2855 }
2856 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2857 si = appctx->owner;
2858 s = si_strm(si);
2859
Cyril Bonté7bb63452018-08-17 23:51:02 +02002860 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002861 s->req.rto = tmout;
2862 s->req.wto = tmout;
2863 s->res.rto = tmout;
2864 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002865 s->req.rex = tick_add_ifset(now_ms, tmout);
2866 s->req.wex = tick_add_ifset(now_ms, tmout);
2867 s->res.rex = tick_add_ifset(now_ms, tmout);
2868 s->res.wex = tick_add_ifset(now_ms, tmout);
2869
2870 s->task->expire = tick_add_ifset(now_ms, tmout);
2871 task_queue(s->task);
2872
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002873 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002874
Thierry Fourniere9636f12018-03-08 09:54:32 +01002875 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002876 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002877}
2878
2879__LJMP static int hlua_socket_new(lua_State *L)
2880{
2881 struct hlua_socket *socket;
2882 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002883 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002884 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002885
2886 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002887 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002888 hlua_pusherror(L, "socket: full stack");
2889 goto out_fail_conf;
2890 }
2891
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002892 /* Create the object: obj[0] = userdata. */
2893 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002894 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002895 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002896 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002897 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002898
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002899 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002900 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002901 hlua_pusherror(L, "socket: uninitialized pools.");
2902 goto out_fail_conf;
2903 }
2904
Willy Tarreau87b09662015-04-03 00:22:06 +02002905 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002906 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2907 lua_setmetatable(L, -2);
2908
Willy Tarreaud420a972015-04-06 00:39:18 +02002909 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002910 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002911 if (!appctx) {
2912 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002913 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002914 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002915
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002916 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002917 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002918 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2919 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002920
Willy Tarreaud420a972015-04-06 00:39:18 +02002921 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002922 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002923 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002924 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002925 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002926 }
2927
Christopher Faulet26256f82020-09-14 11:40:13 +02002928 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002929 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002930 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002931 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002932 }
2933
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002934 /* Initialise cross reference between stream and Lua socket object. */
2935 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002936
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002937 /* Configure "right" stream interface. this "si" is used to connect
2938 * and retrieve data from the server. The connection is initialized
2939 * with the "struct server".
2940 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002941 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002942
2943 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002944 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002945 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002946
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002947 return 1;
2948
Willy Tarreaud420a972015-04-06 00:39:18 +02002949 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002950 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002951 out_fail_sess:
2952 appctx_free(appctx);
2953 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002954 WILL_LJMP(lua_error(L));
2955 return 0;
2956}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002957
2958/*
2959 *
2960 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002961 * Class Channel
2962 *
2963 *
2964 */
2965
2966/* Returns the struct hlua_channel join to the class channel in the
2967 * stack entry "ud" or throws an argument error.
2968 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002969__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002970{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002971 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002972}
2973
Willy Tarreau47860ed2015-03-10 14:07:50 +01002974/* Pushes the channel onto the top of the stack. If the stask does not have a
2975 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002976 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002977static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002978{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002979 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002980 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002981 return 0;
2982
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002983 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002984 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002985 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986
2987 /* Pop a class sesison metatable and affect it to the userdata. */
2988 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2989 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 return 1;
2991}
2992
2993/* Duplicate all the data present in the input channel and put it
2994 * in a string LUA variables. Returns -1 and push a nil value in
2995 * the stack if the channel is closed and all the data are consumed,
2996 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002997 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002999static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000{
3001 char *blk1;
3002 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003003 size_t len1;
3004 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003005 int ret;
3006 luaL_Buffer b;
3007
Willy Tarreau06d80a92017-10-19 14:32:15 +02003008 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02003009 if (unlikely(ret <= 0)) {
3010 if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) {
3011 lua_pushnil(L);
3012 return -1;
3013 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014 return 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015 }
3016
3017 luaL_buffinit(L, &b);
3018 luaL_addlstring(&b, blk1, len1);
3019 if (unlikely(ret == 2))
3020 luaL_addlstring(&b, blk2, len2);
3021 luaL_pushresult(&b);
3022
3023 if (unlikely(ret == 2))
3024 return len1 + len2;
3025 return len1;
3026}
3027
3028/* "_hlua_channel_dup" wrapper. If no data are available, it returns
3029 * a yield. This function keep the data in the buffer.
3030 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003031__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003032{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003033 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003034
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003035 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3036
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003037 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003038 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003039 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003040 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003041
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003042 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02003043 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003044 return 1;
3045}
3046
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003047/* Check arguments for the function "hlua_channel_dup_yield". */
3048__LJMP static int hlua_channel_dup(lua_State *L)
3049{
3050 MAY_LJMP(check_args(L, 1, "dup"));
3051 MAY_LJMP(hlua_checkchannel(L, 1));
3052 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
3053}
3054
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003055/* "_hlua_channel_dup" wrapper. If no data are available, it returns
3056 * a yield. This function consumes the data in the buffer. It returns
3057 * a string containing the data or a nil pointer if no data are available
3058 * and the channel is closed.
3059 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003060__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003062 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003063 int ret;
3064
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003065 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003066
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003067 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003068 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003069 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003070 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003071
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003072 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02003074 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003075
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076 if (unlikely(ret == -1))
3077 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003078
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003079 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003080 return 1;
3081}
3082
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003083/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003084__LJMP static int hlua_channel_get(lua_State *L)
3085{
3086 MAY_LJMP(check_args(L, 1, "get"));
3087 MAY_LJMP(hlua_checkchannel(L, 1));
3088 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3089}
3090
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003091/* This functions consumes and returns one line. If the channel is closed,
3092 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003093 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003094 * value.
3095 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003096__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003097{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003098 char *blk1;
3099 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003100 size_t len1;
3101 size_t len2;
3102 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003103 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003104 int ret;
3105 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003106
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003107 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3108
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003109 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003110 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003111 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003112 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003113
Willy Tarreau06d80a92017-10-19 14:32:15 +02003114 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02003115 if (ret == 0) {
3116 if (HLUA_CANT_YIELD(hlua_gethlua(L))) {
3117 _hlua_channel_dup(chn, L);
3118 return 1;
3119 }
Willy Tarreau9635e032018-10-16 17:52:55 +02003120 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet055310f2021-08-05 11:58:37 +02003121 }
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003122
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123 if (ret == -1) {
3124 lua_pushnil(L);
3125 return 1;
3126 }
3127
3128 luaL_buffinit(L, &b);
3129 luaL_addlstring(&b, blk1, len1);
3130 len = len1;
3131 if (unlikely(ret == 2)) {
3132 luaL_addlstring(&b, blk2, len2);
3133 len += len2;
3134 }
3135 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003136 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003137 return 1;
3138}
3139
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003140/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003141__LJMP static int hlua_channel_getline(lua_State *L)
3142{
3143 MAY_LJMP(check_args(L, 1, "getline"));
3144 MAY_LJMP(hlua_checkchannel(L, 1));
3145 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3146}
3147
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003148/* This function takes a string as argument, and append it at the input side of
3149 * channel. If data cannot be copied, because there is not enough space to do so
3150 * or because the channel is closed, it returns -1. Otherwise, it returns the
3151 * amount of data written (the string length). This function does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003152 */
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003153__LJMP static int hlua_channel_append(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003154{
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003155 struct channel *chn;
3156 const char *str;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003157 size_t len;
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003158
3159 MAY_LJMP(check_args(L, 2, "append"));
3160 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3161 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003163 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003164 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003165 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003166 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003167
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003168 if (len > c_room(chn) || ci_putblk(chn, str, len) < 0)
3169 lua_pushinteger(L, -1);
3170 else
3171 lua_pushinteger(L, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172 return 1;
3173}
3174
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003175/* The function replaces input data from the channel by the string passed as
3176 * argument. Before clearing the buffer, we take care the copy will be
3177 * possible. It returns the amount of data written (the string length) on
3178 * success or -1 if the copy is not performed. This function does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179 */
3180__LJMP static int hlua_channel_set(lua_State *L)
3181{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003182 struct channel *chn;
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003183 const char *str;
3184 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003185
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003186 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003187 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003188 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003189
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003190 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003191 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003192 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003193 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003194
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003195 /* Be sure we can copied the string once input data will be removed. */
3196 if (len > ci_data(chn) || channel_input_closed(chn))
3197 lua_pushinteger(L, -1);
3198 else {
3199 b_set_data(&chn->buf, co_data(chn));
3200 if (ci_putblk(chn, str, len) < 0)
3201 lua_pushinteger(L, -1);
3202 else
3203 lua_pushinteger(L, len);
3204 }
3205 return -1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003206}
3207
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003208/* Append data in the output side of the buffer. This data is immediately
3209 * sent. The function returns the amount of data written. If the buffer
3210 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003211 * if the channel is closed.
3212 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003213__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003214{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003215 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003216 size_t len;
3217 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3218 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3219 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003220 struct hlua *hlua;
3221
3222 /* Get hlua struct, or NULL if we execute from main lua state */
3223 hlua = hlua_gethlua(L);
3224 if (!hlua) {
3225 lua_pushnil(L);
3226 return 1;
3227 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003228
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003229 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003230 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003231 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003232 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003233
Willy Tarreau47860ed2015-03-10 14:07:50 +01003234 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003235 lua_pushinteger(L, -1);
3236 return 1;
3237 }
3238
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003239 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003240 * the request buffer if its not required.
3241 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003242 if (chn->buf.size == 0) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003243 if (HLUA_CANT_YIELD(hlua_gethlua(L)))
3244 return 1;
Willy Tarreau4b962a42018-11-15 11:03:21 +01003245 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003246 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003247 }
3248
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003249 /* The written data will be immediately sent, so we can check
3250 * the available space without taking in account the reserve.
3251 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003252 * data, because the buffer will be flushed.
3253 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003254 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003255
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003256 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003257 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003258 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003259 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003260 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003261 return 1;
3262
3263 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003264 if (max > len - l)
3265 max = len - l;
3266
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003267 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003268 * detects a non contiguous buffer and realign it.
3269 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003270 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003271 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003272
3273 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003274 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003275
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003276 /* buffer replace considers that the input part is filled.
3277 * so, I must forward these new data in the output part.
3278 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003279 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003280
3281 l += max;
3282 lua_pop(L, 1);
3283 lua_pushinteger(L, l);
3284
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003285 if (l < len) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003286 /* If there is no space available, and the output buffer is empty.
3287 * in this case, we cannot add more data, so we cannot yield,
3288 * we return the amount of copied data.
3289 */
3290 max = b_room(&chn->buf);
3291 if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L)))
3292 return 1;
3293
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003294 /* If we are waiting for space in the response buffer, we
3295 * must set the flag WAKERESWR. This flag required the task
3296 * wake up if any activity is detected on the response buffer.
3297 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003298 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003299 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003300 else
3301 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003302 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003303 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003304
3305 return 1;
3306}
3307
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003308/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003309 * yield the LUA process, and resume it without checking the
3310 * input arguments.
3311 */
3312__LJMP static int hlua_channel_send(lua_State *L)
3313{
3314 MAY_LJMP(check_args(L, 2, "send"));
3315 lua_pushinteger(L, 0);
3316
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003317 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003318}
3319
3320/* This function forward and amount of butes. The data pass from
3321 * the input side of the buffer to the output side, and can be
3322 * forwarded. This function never fails.
3323 *
3324 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003325 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003326 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003327__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003328{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003329 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003330 int len;
3331 int l;
3332 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003333 struct hlua *hlua;
3334
3335 /* Get hlua struct, or NULL if we execute from main lua state */
3336 hlua = hlua_gethlua(L);
3337 if (!hlua)
3338 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003339
3340 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003341
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003342 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003343 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003344 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003345 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003346
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003347 len = MAY_LJMP(luaL_checkinteger(L, 2));
3348 l = MAY_LJMP(luaL_checkinteger(L, -1));
3349
3350 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003351 if (max > ci_data(chn))
3352 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003353 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003354 l += max;
3355
3356 lua_pop(L, 1);
3357 lua_pushinteger(L, l);
3358
3359 /* Check if it miss bytes to forward. */
3360 if (l < len) {
3361 /* The the input channel or the output channel are closed, we
3362 * must return the amount of data forwarded.
3363 */
Christopher Faulet055310f2021-08-05 11:58:37 +02003364 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003365 return 1;
3366
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003367 /* If we are waiting for space data in the response buffer, we
3368 * must set the flag WAKERESWR. This flag required the task
3369 * wake up if any activity is detected on the response buffer.
3370 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003371 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003372 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003373 else
3374 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003375
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003376 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003377 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003378 }
3379
3380 return 1;
3381}
3382
3383/* Just check the input and prepare the stack for the previous
3384 * function "hlua_channel_forward_yield"
3385 */
3386__LJMP static int hlua_channel_forward(lua_State *L)
3387{
3388 MAY_LJMP(check_args(L, 2, "forward"));
3389 MAY_LJMP(hlua_checkchannel(L, 1));
3390 MAY_LJMP(luaL_checkinteger(L, 2));
3391
3392 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003393 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003394}
3395
3396/* Just returns the number of bytes available in the input
3397 * side of the buffer. This function never fails.
3398 */
3399__LJMP static int hlua_channel_get_in_len(lua_State *L)
3400{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003401 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003402
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003403 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003404 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003405 if (IS_HTX_STRM(chn_strm(chn))) {
3406 struct htx *htx = htxbuf(&chn->buf);
3407 lua_pushinteger(L, htx->data - co_data(chn));
3408 }
3409 else
3410 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003411 return 1;
3412}
3413
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003414/* Returns true if the channel is full. */
3415__LJMP static int hlua_channel_is_full(lua_State *L)
3416{
3417 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003418
3419 MAY_LJMP(check_args(L, 1, "is_full"));
3420 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003421 /* ignore the reserve, we are not on a producer side (ie in an
3422 * applet).
3423 */
3424 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003425 return 1;
3426}
3427
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003428/* Returns true if the channel is the response channel. */
3429__LJMP static int hlua_channel_is_resp(lua_State *L)
3430{
3431 struct channel *chn;
3432
3433 MAY_LJMP(check_args(L, 1, "is_resp"));
3434 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3435
3436 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3437 return 1;
3438}
3439
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003440/* Just returns the number of bytes available in the output
3441 * side of the buffer. This function never fails.
3442 */
3443__LJMP static int hlua_channel_get_out_len(lua_State *L)
3444{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003445 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003446
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003447 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003448 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003449 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003450 return 1;
3451}
3452
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003453/*
3454 *
3455 *
3456 * Class Fetches
3457 *
3458 *
3459 */
3460
3461/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003462 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003463 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003464__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003465{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003466 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003467}
3468
3469/* This function creates and push in the stack a fetch object according
3470 * with a current TXN.
3471 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003472static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003473{
Willy Tarreau7073c472015-04-06 11:15:40 +02003474 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003475
3476 /* Check stack size. */
3477 if (!lua_checkstack(L, 3))
3478 return 0;
3479
3480 /* Create the object: obj[0] = userdata.
3481 * Note that the base of the Fetches object is the
3482 * transaction object.
3483 */
3484 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003485 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003486 lua_rawseti(L, -2, 0);
3487
Willy Tarreau7073c472015-04-06 11:15:40 +02003488 hsmp->s = txn->s;
3489 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003490 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003491 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003492
3493 /* Pop a class sesison metatable and affect it to the userdata. */
3494 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3495 lua_setmetatable(L, -2);
3496
3497 return 1;
3498}
3499
3500/* This function is an LUA binding. It is called with each sample-fetch.
3501 * It uses closure argument to store the associated sample-fetch. It
3502 * returns only one argument or throws an error. An error is thrown
3503 * only if an error is encountered during the argument parsing. If
3504 * the "sample-fetch" function fails, nil is returned.
3505 */
3506__LJMP static int hlua_run_sample_fetch(lua_State *L)
3507{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003508 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003509 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003510 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003511 int i;
3512 struct sample smp;
3513
3514 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003515 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003516
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003517 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003518 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003519
Thierry FOURNIERca988662015-12-20 18:43:03 +01003520 /* Check execution authorization. */
3521 if (f->use & SMP_USE_HTTP_ANY &&
3522 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3523 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3524 "is not available in Lua services", f->kw);
3525 WILL_LJMP(lua_error(L));
3526 }
3527
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003528 /* Get extra arguments. */
3529 for (i = 0; i < lua_gettop(L) - 1; i++) {
3530 if (i >= ARGM_NBARGS)
3531 break;
3532 hlua_lua2arg(L, i + 2, &args[i]);
3533 }
3534 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003535 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003536
3537 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003538 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003539
3540 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003541 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003542 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003543 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003544 }
3545
3546 /* Initialise the sample. */
3547 memset(&smp, 0, sizeof(smp));
3548
3549 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003550 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003551 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003552 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003553 lua_pushstring(L, "");
3554 else
3555 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003556 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003557 }
3558
3559 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003560 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003561 hlua_smp2lua_str(L, &smp);
3562 else
3563 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003564
3565 end:
3566 for (i = 0; args[i].type != ARGT_STOP; i++) {
3567 if (args[i].type == ARGT_STR)
3568 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003569 else if (args[i].type == ARGT_REG)
3570 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003571 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003572 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003573
3574 error:
3575 for (i = 0; args[i].type != ARGT_STOP; i++) {
3576 if (args[i].type == ARGT_STR)
3577 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003578 else if (args[i].type == ARGT_REG)
3579 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003580 }
3581 WILL_LJMP(lua_error(L));
3582 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003583}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003584
3585/*
3586 *
3587 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003588 * Class Converters
3589 *
3590 *
3591 */
3592
3593/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003594 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003595 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003596__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003597{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003598 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003599}
3600
3601/* This function creates and push in the stack a Converters object
3602 * according with a current TXN.
3603 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003604static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003605{
Willy Tarreau7073c472015-04-06 11:15:40 +02003606 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003607
3608 /* Check stack size. */
3609 if (!lua_checkstack(L, 3))
3610 return 0;
3611
3612 /* Create the object: obj[0] = userdata.
3613 * Note that the base of the Converters object is the
3614 * same than the TXN object.
3615 */
3616 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003617 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003618 lua_rawseti(L, -2, 0);
3619
Willy Tarreau7073c472015-04-06 11:15:40 +02003620 hsmp->s = txn->s;
3621 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003622 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003623 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003624
Willy Tarreau87b09662015-04-03 00:22:06 +02003625 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003626 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3627 lua_setmetatable(L, -2);
3628
3629 return 1;
3630}
3631
3632/* This function is an LUA binding. It is called with each converter.
3633 * It uses closure argument to store the associated converter. It
3634 * returns only one argument or throws an error. An error is thrown
3635 * only if an error is encountered during the argument parsing. If
3636 * the converter function function fails, nil is returned.
3637 */
3638__LJMP static int hlua_run_sample_conv(lua_State *L)
3639{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003640 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003641 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003642 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003643 int i;
3644 struct sample smp;
3645
3646 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003647 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003648
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003649 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003650 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003651
3652 /* Get extra arguments. */
3653 for (i = 0; i < lua_gettop(L) - 2; i++) {
3654 if (i >= ARGM_NBARGS)
3655 break;
3656 hlua_lua2arg(L, i + 3, &args[i]);
3657 }
3658 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003659 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003660
3661 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003662 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003663
3664 /* Run the special args checker. */
3665 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3666 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003667 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003668 }
3669
3670 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003671 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003672 if (!hlua_lua2smp(L, 2, &smp)) {
3673 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003674 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003675 }
3676
Willy Tarreau1777ea62016-03-10 16:15:46 +01003677 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3678
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003679 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003680 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003681 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003682 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003683 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003684 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003685 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3686 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003687 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003688 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003689 }
3690
3691 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003692 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003693 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003694 lua_pushstring(L, "");
3695 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003696 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003697 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003698 }
3699
3700 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003701 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003702 hlua_smp2lua_str(L, &smp);
3703 else
3704 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003705 end:
3706 for (i = 0; args[i].type != ARGT_STOP; i++) {
3707 if (args[i].type == ARGT_STR)
3708 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003709 else if (args[i].type == ARGT_REG)
3710 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003711 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003712 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003713
3714 error:
3715 for (i = 0; args[i].type != ARGT_STOP; i++) {
3716 if (args[i].type == ARGT_STR)
3717 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003718 else if (args[i].type == ARGT_REG)
3719 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003720 }
3721 WILL_LJMP(lua_error(L));
3722 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003723}
3724
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003725/*
3726 *
3727 *
3728 * Class AppletTCP
3729 *
3730 *
3731 */
3732
3733/* Returns a struct hlua_txn if the stack entry "ud" is
3734 * a class stream, otherwise it throws an error.
3735 */
3736__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3737{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003738 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003739}
3740
3741/* This function creates and push in the stack an Applet object
3742 * according with a current TXN.
3743 */
3744static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3745{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003746 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003747 struct stream_interface *si = ctx->owner;
3748 struct stream *s = si_strm(si);
3749 struct proxy *p = s->be;
3750
3751 /* Check stack size. */
3752 if (!lua_checkstack(L, 3))
3753 return 0;
3754
3755 /* Create the object: obj[0] = userdata.
3756 * Note that the base of the Converters object is the
3757 * same than the TXN object.
3758 */
3759 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003760 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003761 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003762 luactx->appctx = ctx;
3763 luactx->htxn.s = s;
3764 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003765
3766 /* Create the "f" field that contains a list of fetches. */
3767 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003768 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003769 return 0;
3770 lua_settable(L, -3);
3771
3772 /* Create the "sf" field that contains a list of stringsafe fetches. */
3773 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003774 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003775 return 0;
3776 lua_settable(L, -3);
3777
3778 /* Create the "c" field that contains a list of converters. */
3779 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003780 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003781 return 0;
3782 lua_settable(L, -3);
3783
3784 /* Create the "sc" field that contains a list of stringsafe converters. */
3785 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003786 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003787 return 0;
3788 lua_settable(L, -3);
3789
3790 /* Pop a class stream metatable and affect it to the table. */
3791 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3792 lua_setmetatable(L, -2);
3793
3794 return 1;
3795}
3796
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003797__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3798{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003799 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003800 struct stream *s;
3801 const char *name;
3802 size_t len;
3803 struct sample smp;
3804
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003805 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3806 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003807
3808 /* It is useles to retrieve the stream, but this function
3809 * runs only in a stream context.
3810 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003811 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003812 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003813 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003814
3815 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003816 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003817 hlua_lua2smp(L, 3, &smp);
3818
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02003819 /* Store the sample in a variable. We don't need to dup the smp, vars API
3820 * already takes care of duplicating dynamic var data.
3821 */
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003822 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003823
3824 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3825 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3826 else
3827 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3828
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003829 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003830}
3831
3832__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3833{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003834 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003835 struct stream *s;
3836 const char *name;
3837 size_t len;
3838 struct sample smp;
3839
3840 MAY_LJMP(check_args(L, 2, "unset_var"));
3841
3842 /* It is useles to retrieve the stream, but this function
3843 * runs only in a stream context.
3844 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003845 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003846 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003847 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003848
3849 /* Unset the variable. */
3850 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003851 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3852 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003853}
3854
3855__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3856{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003857 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003858 struct stream *s;
3859 const char *name;
3860 size_t len;
3861 struct sample smp;
3862
3863 MAY_LJMP(check_args(L, 2, "get_var"));
3864
3865 /* It is useles to retrieve the stream, but this function
3866 * runs only in a stream context.
3867 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003868 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003869 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003870 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003871
3872 smp_set_owner(&smp, s->be, s->sess, s, 0);
3873 if (!vars_get_by_name(name, len, &smp)) {
3874 lua_pushnil(L);
3875 return 1;
3876 }
3877
3878 return hlua_smp2lua(L, &smp);
3879}
3880
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003881__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3882{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003883 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3884 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003885 struct hlua *hlua;
3886
3887 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003888 if (!s->hlua)
3889 return 0;
3890 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003891
3892 MAY_LJMP(check_args(L, 2, "set_priv"));
3893
3894 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003895 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003896
3897 /* Get and store new value. */
3898 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3899 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3900
3901 return 0;
3902}
3903
3904__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3905{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003906 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3907 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003908 struct hlua *hlua;
3909
3910 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003911 if (!s->hlua) {
3912 lua_pushnil(L);
3913 return 1;
3914 }
3915 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003916
3917 /* Push configuration index in the stack. */
3918 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3919
3920 return 1;
3921}
3922
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003923/* If expected data not yet available, it returns a yield. This function
3924 * consumes the data in the buffer. It returns a string containing the
3925 * data. This string can be empty.
3926 */
3927__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3928{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003929 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3930 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003931 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003932 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003933 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003934 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003935 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003936
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003937 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003938 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003939
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003940 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003941 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003942 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003943 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003944 }
3945
3946 /* End of data: commit the total strings and return. */
3947 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003948 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003949 return 1;
3950 }
3951
3952 /* Ensure that the block 2 length is usable. */
3953 if (ret == 1)
3954 len2 = 0;
3955
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003956 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003957 luaL_addlstring(&luactx->b, blk1, len1);
3958 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003959
3960 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003961 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003962 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003963 return 1;
3964}
3965
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003966/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003967__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3968{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003969 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003970
3971 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003972 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003973
3974 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3975}
3976
3977/* If expected data not yet available, it returns a yield. This function
3978 * consumes the data in the buffer. It returns a string containing the
3979 * data. This string can be empty.
3980 */
3981__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3982{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003983 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3984 struct stream_interface *si = luactx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003985 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003986 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003987 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003988 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003989 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003990 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003991
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003992 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003993 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003994
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003995 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003996 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003997 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003998 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003999 }
4000
4001 /* End of data: commit the total strings and return. */
4002 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004003 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004004 return 1;
4005 }
4006
4007 /* Ensure that the block 2 length is usable. */
4008 if (ret == 1)
4009 len2 = 0;
4010
4011 if (len == -1) {
4012
4013 /* If len == -1, catenate all the data avalaile and
4014 * yield because we want to get all the data until
4015 * the end of data stream.
4016 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004017 luaL_addlstring(&luactx->b, blk1, len1);
4018 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004019 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004020 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004021 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004022
4023 } else {
4024
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004025 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004026 if (len1 > len)
4027 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004028 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004029 len -= len1;
4030
4031 /* Copy the second block. */
4032 if (len2 > len)
4033 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004034 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004035 len -= len2;
4036
4037 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004038 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004039
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004040 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004041 if (len > 0) {
4042 lua_pushinteger(L, len);
4043 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004044 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004045 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004046 }
4047
4048 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004049 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004050 return 1;
4051 }
4052
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004053 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004054 hlua_pusherror(L, "Lua: internal error");
4055 WILL_LJMP(lua_error(L));
4056 return 0;
4057}
4058
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004059/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004060__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4061{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004062 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004063 int len = -1;
4064
4065 if (lua_gettop(L) > 2)
4066 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4067 if (lua_gettop(L) >= 2) {
4068 len = MAY_LJMP(luaL_checkinteger(L, 2));
4069 lua_pop(L, 1);
4070 }
4071
4072 /* Confirm or set the required length */
4073 lua_pushinteger(L, len);
4074
4075 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004076 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004077
4078 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4079}
4080
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004081/* Append data in the output side of the buffer. This data is immediately
4082 * sent. The function returns the amount of data written. If the buffer
4083 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004084 * if the channel is closed.
4085 */
4086__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4087{
4088 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004089 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004090 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4091 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004092 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004093 struct channel *chn = si_ic(si);
4094 int max;
4095
4096 /* Get the max amount of data which can write as input in the channel. */
4097 max = channel_recv_max(chn);
4098 if (max > (len - l))
4099 max = len - l;
4100
4101 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004102 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004103
4104 /* update counters. */
4105 l += max;
4106 lua_pop(L, 1);
4107 lua_pushinteger(L, l);
4108
4109 /* If some data is not send, declares the situation to the
4110 * applet, and returns a yield.
4111 */
4112 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004113 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004114 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004115 }
4116
4117 return 1;
4118}
4119
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004120/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004121 * yield the LUA process, and resume it without checking the
4122 * input arguments.
4123 */
4124__LJMP static int hlua_applet_tcp_send(lua_State *L)
4125{
4126 MAY_LJMP(check_args(L, 2, "send"));
4127 lua_pushinteger(L, 0);
4128
4129 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4130}
4131
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004132/*
4133 *
4134 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004135 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004136 *
4137 *
4138 */
4139
4140/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004141 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004142 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004143__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004144{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004145 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004146}
4147
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004148/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004149 * according with a current TXN.
4150 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004151static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004152{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004153 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004154 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004155 struct stream_interface *si = ctx->owner;
4156 struct stream *s = si_strm(si);
4157 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004158 struct htx *htx;
4159 struct htx_blk *blk;
4160 struct htx_sl *sl;
4161 struct ist path;
4162 unsigned long long len = 0;
4163 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004164
4165 /* Check stack size. */
4166 if (!lua_checkstack(L, 3))
4167 return 0;
4168
4169 /* Create the object: obj[0] = userdata.
4170 * Note that the base of the Converters object is the
4171 * same than the TXN object.
4172 */
4173 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004174 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004175 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004176 luactx->appctx = ctx;
4177 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4178 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4179 luactx->htxn.s = s;
4180 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004181
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004182 /* Create the "f" field that contains a list of fetches. */
4183 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004184 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004185 return 0;
4186 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004187
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004188 /* Create the "sf" field that contains a list of stringsafe fetches. */
4189 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004190 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004191 return 0;
4192 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004193
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004194 /* Create the "c" field that contains a list of converters. */
4195 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004196 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004197 return 0;
4198 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004199
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004200 /* Create the "sc" field that contains a list of stringsafe converters. */
4201 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004202 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004203 return 0;
4204 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004205
Christopher Fauleta2097962019-07-15 16:25:33 +02004206 htx = htxbuf(&s->req.buf);
4207 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004208 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004209 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004210
Christopher Fauleta2097962019-07-15 16:25:33 +02004211 /* Stores the request method. */
4212 lua_pushstring(L, "method");
4213 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4214 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004215
Christopher Fauleta2097962019-07-15 16:25:33 +02004216 /* Stores the http version. */
4217 lua_pushstring(L, "version");
4218 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4219 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004220
Christopher Fauleta2097962019-07-15 16:25:33 +02004221 /* creates an array of headers. hlua_http_get_headers() crates and push
4222 * the array on the top of the stack.
4223 */
4224 lua_pushstring(L, "headers");
4225 htxn.s = s;
4226 htxn.p = px;
4227 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004228 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004229 return 0;
4230 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004231
Christopher Fauleta2097962019-07-15 16:25:33 +02004232 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004233 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004234 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004235
Christopher Fauleta2097962019-07-15 16:25:33 +02004236 p = path.ptr;
4237 end = path.ptr + path.len;
4238 q = p;
4239 while (q < end && *q != '?')
4240 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004241
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004242 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004243 lua_pushstring(L, "path");
4244 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004245 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004246
Christopher Fauleta2097962019-07-15 16:25:33 +02004247 /* Stores the query string. */
4248 lua_pushstring(L, "qs");
4249 if (*q == '?')
4250 q++;
4251 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004252 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004253 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004254
Christopher Fauleta2097962019-07-15 16:25:33 +02004255 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4256 struct htx_blk *blk = htx_get_blk(htx, pos);
4257 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004258
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004259 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004260 break;
4261 if (type == HTX_BLK_DATA)
4262 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004263 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004264 if (htx->extra != ULLONG_MAX)
4265 len += htx->extra;
4266
4267 /* Stores the request path. */
4268 lua_pushstring(L, "length");
4269 lua_pushinteger(L, len);
4270 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004271
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004272 /* Create an empty array of HTTP request headers. */
4273 lua_pushstring(L, "response");
4274 lua_newtable(L);
4275 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004276
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004277 /* Pop a class stream metatable and affect it to the table. */
4278 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4279 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004280
4281 return 1;
4282}
4283
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004284__LJMP static int hlua_applet_http_set_var(lua_State *L)
4285{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004286 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004287 struct stream *s;
4288 const char *name;
4289 size_t len;
4290 struct sample smp;
4291
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004292 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4293 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004294
4295 /* It is useles to retrieve the stream, but this function
4296 * runs only in a stream context.
4297 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004298 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004299 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004300 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004301
4302 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004303 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004304 hlua_lua2smp(L, 3, &smp);
4305
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02004306 /* Store the sample in a variable. We don't need to dup the smp, vars API
4307 * already takes care of duplicating dynamic var data.
4308 */
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004309 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004310
4311 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4312 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4313 else
4314 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4315
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004316 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004317}
4318
4319__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4320{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004321 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004322 struct stream *s;
4323 const char *name;
4324 size_t len;
4325 struct sample smp;
4326
4327 MAY_LJMP(check_args(L, 2, "unset_var"));
4328
4329 /* It is useles to retrieve the stream, but this function
4330 * runs only in a stream context.
4331 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004332 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004333 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004334 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004335
4336 /* Unset the variable. */
4337 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004338 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4339 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004340}
4341
4342__LJMP static int hlua_applet_http_get_var(lua_State *L)
4343{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004344 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004345 struct stream *s;
4346 const char *name;
4347 size_t len;
4348 struct sample smp;
4349
4350 MAY_LJMP(check_args(L, 2, "get_var"));
4351
4352 /* It is useles to retrieve the stream, but this function
4353 * runs only in a stream context.
4354 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004355 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004356 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004357 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004358
4359 smp_set_owner(&smp, s->be, s->sess, s, 0);
4360 if (!vars_get_by_name(name, len, &smp)) {
4361 lua_pushnil(L);
4362 return 1;
4363 }
4364
4365 return hlua_smp2lua(L, &smp);
4366}
4367
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004368__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4369{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004370 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4371 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004372 struct hlua *hlua;
4373
4374 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004375 if (!s->hlua)
4376 return 0;
4377 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004378
4379 MAY_LJMP(check_args(L, 2, "set_priv"));
4380
4381 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004382 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004383
4384 /* Get and store new value. */
4385 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4386 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4387
4388 return 0;
4389}
4390
4391__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4392{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004393 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4394 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004395 struct hlua *hlua;
4396
4397 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004398 if (!s->hlua) {
4399 lua_pushnil(L);
4400 return 1;
4401 }
4402 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004403
4404 /* Push configuration index in the stack. */
4405 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4406
4407 return 1;
4408}
4409
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004410/* If expected data not yet available, it returns a yield. This function
4411 * consumes the data in the buffer. It returns a string containing the
4412 * data. This string can be empty.
4413 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004414__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004415{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004416 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4417 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004418 struct channel *req = si_oc(si);
4419 struct htx *htx;
4420 struct htx_blk *blk;
4421 size_t count;
4422 int stop = 0;
4423
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004424 htx = htx_from_buf(&req->buf);
4425 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004426 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004427
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004428 while (count && !stop && blk) {
4429 enum htx_blk_type type = htx_get_blk_type(blk);
4430 uint32_t sz = htx_get_blksz(blk);
4431 struct ist v;
4432 uint32_t vlen;
4433 char *nl;
4434
4435 vlen = sz;
4436 if (vlen > count) {
4437 if (type != HTX_BLK_DATA)
4438 break;
4439 vlen = count;
4440 }
4441
4442 switch (type) {
4443 case HTX_BLK_UNUSED:
4444 break;
4445
4446 case HTX_BLK_DATA:
4447 v = htx_get_blk_value(htx, blk);
4448 v.len = vlen;
4449 nl = istchr(v, '\n');
4450 if (nl != NULL) {
4451 stop = 1;
4452 vlen = nl - v.ptr + 1;
4453 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004454 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004455 break;
4456
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004457 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004458 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004459 stop = 1;
4460 break;
4461
4462 default:
4463 break;
4464 }
4465
4466 co_set_data(req, co_data(req) - vlen);
4467 count -= vlen;
4468 if (sz == vlen)
4469 blk = htx_remove_blk(htx, blk);
4470 else {
4471 htx_cut_data_blk(htx, blk, vlen);
4472 break;
4473 }
4474 }
4475
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004476 /* The message was fully consumed and no more data are expected
4477 * (EOM flag set).
4478 */
Christopher Fauleta5a25b12022-08-29 15:37:16 +02004479 if (htx_is_empty(htx) && (req->flags & CF_EOI))
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004480 stop = 1;
4481
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004482 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004483 if (!stop) {
4484 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004485 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004486 }
4487
4488 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004489 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004490 return 1;
4491}
4492
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004493
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004494/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004495__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004496{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004497 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004498
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004499 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004500 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004501
Christopher Fauleta2097962019-07-15 16:25:33 +02004502 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004503}
4504
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004505/* If expected data not yet available, it returns a yield. This function
4506 * consumes the data in the buffer. It returns a string containing the
4507 * data. This string can be empty.
4508 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004509__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004510{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004511 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4512 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004513 struct channel *req = si_oc(si);
4514 struct htx *htx;
4515 struct htx_blk *blk;
4516 size_t count;
4517 int len;
4518
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004519 htx = htx_from_buf(&req->buf);
4520 len = MAY_LJMP(luaL_checkinteger(L, 2));
4521 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004522 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004523 while (count && len && blk) {
4524 enum htx_blk_type type = htx_get_blk_type(blk);
4525 uint32_t sz = htx_get_blksz(blk);
4526 struct ist v;
4527 uint32_t vlen;
4528
4529 vlen = sz;
4530 if (len > 0 && vlen > len)
4531 vlen = len;
4532 if (vlen > count) {
4533 if (type != HTX_BLK_DATA)
4534 break;
4535 vlen = count;
4536 }
4537
4538 switch (type) {
4539 case HTX_BLK_UNUSED:
4540 break;
4541
4542 case HTX_BLK_DATA:
4543 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004544 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004545 break;
4546
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004547 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004548 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004549 len = 0;
4550 break;
4551
4552 default:
4553 break;
4554 }
4555
4556 co_set_data(req, co_data(req) - vlen);
4557 count -= vlen;
4558 if (len > 0)
4559 len -= vlen;
4560 if (sz == vlen)
4561 blk = htx_remove_blk(htx, blk);
4562 else {
4563 htx_cut_data_blk(htx, blk, vlen);
4564 break;
4565 }
4566 }
4567
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004568 /* The message was fully consumed and no more data are expected
4569 * (EOM flag set).
4570 */
Christopher Fauleta5a25b12022-08-29 15:37:16 +02004571 if (htx_is_empty(htx) && (req->flags & CF_EOI))
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004572 len = 0;
4573
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004574 htx_to_buf(htx, &req->buf);
4575
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004576 /* If we are no other data available, yield waiting for new data. */
4577 if (len) {
4578 if (len > 0) {
4579 lua_pushinteger(L, len);
4580 lua_replace(L, 2);
4581 }
4582 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004583 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004584 }
4585
4586 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004587 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004588 return 1;
4589}
4590
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004591/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004592__LJMP static int hlua_applet_http_recv(lua_State *L)
4593{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004594 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004595 int len = -1;
4596
4597 /* Check arguments. */
4598 if (lua_gettop(L) > 2)
4599 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4600 if (lua_gettop(L) >= 2) {
4601 len = MAY_LJMP(luaL_checkinteger(L, 2));
4602 lua_pop(L, 1);
4603 }
4604
Christopher Fauleta2097962019-07-15 16:25:33 +02004605 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004606
Christopher Fauleta2097962019-07-15 16:25:33 +02004607 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004608 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004609
Christopher Fauleta2097962019-07-15 16:25:33 +02004610 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004611}
4612
4613/* Append data in the output side of the buffer. This data is immediately
4614 * sent. The function returns the amount of data written. If the buffer
4615 * cannot contain the data, the function yields. The function returns -1
4616 * if the channel is closed.
4617 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004618__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004619{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004620 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4621 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004622 struct channel *res = si_ic(si);
4623 struct htx *htx = htx_from_buf(&res->buf);
4624 const char *data;
4625 size_t len;
4626 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4627 int max;
4628
Christopher Faulet9060fc02019-07-03 11:39:30 +02004629 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004630 if (!max)
4631 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004632
4633 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4634
4635 /* Get the max amount of data which can write as input in the channel. */
4636 if (max > (len - l))
4637 max = len - l;
4638
4639 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004640 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004641 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004642
4643 /* update counters. */
4644 l += max;
4645 lua_pop(L, 1);
4646 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004647
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004648 /* If some data is not send, declares the situation to the
4649 * applet, and returns a yield.
4650 */
4651 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004652 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004653 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004654 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004655 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004656 }
4657
Christopher Fauleta2097962019-07-15 16:25:33 +02004658 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004659 return 1;
4660}
4661
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004662/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004663 * yield the LUA process, and resume it without checking the
4664 * input arguments.
4665 */
4666__LJMP static int hlua_applet_http_send(lua_State *L)
4667{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004668 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004669
4670 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004671 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004672 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4673 WILL_LJMP(lua_error(L));
4674 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004675
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004676 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004677 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004678
Christopher Fauleta2097962019-07-15 16:25:33 +02004679 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004680}
4681
4682__LJMP static int hlua_applet_http_addheader(lua_State *L)
4683{
4684 const char *name;
4685 int ret;
4686
4687 MAY_LJMP(hlua_checkapplet_http(L, 1));
4688 name = MAY_LJMP(luaL_checkstring(L, 2));
4689 MAY_LJMP(luaL_checkstring(L, 3));
4690
4691 /* Push in the stack the "response" entry. */
4692 ret = lua_getfield(L, 1, "response");
4693 if (ret != LUA_TTABLE) {
4694 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4695 "is expected as an array. %s found", lua_typename(L, ret));
4696 WILL_LJMP(lua_error(L));
4697 }
4698
4699 /* check if the header is already registered if it is not
4700 * the case, register it.
4701 */
4702 ret = lua_getfield(L, -1, name);
4703 if (ret == LUA_TNIL) {
4704
4705 /* Entry not found. */
4706 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4707
4708 /* Insert the new header name in the array in the top of the stack.
4709 * It left the new array in the top of the stack.
4710 */
4711 lua_newtable(L);
4712 lua_pushvalue(L, 2);
4713 lua_pushvalue(L, -2);
4714 lua_settable(L, -4);
4715
4716 } else if (ret != LUA_TTABLE) {
4717
4718 /* corruption error. */
4719 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4720 "is expected as an array. %s found", name, lua_typename(L, ret));
4721 WILL_LJMP(lua_error(L));
4722 }
4723
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004724 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004725 * the header value as new entry.
4726 */
4727 lua_pushvalue(L, 3);
4728 ret = lua_rawlen(L, -2);
4729 lua_rawseti(L, -2, ret + 1);
4730 lua_pushboolean(L, 1);
4731 return 1;
4732}
4733
4734__LJMP static int hlua_applet_http_status(lua_State *L)
4735{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004736 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004737 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004738 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004739
4740 if (status < 100 || status > 599) {
4741 lua_pushboolean(L, 0);
4742 return 1;
4743 }
4744
Willy Tarreau7e702d12021-04-28 17:59:21 +02004745 luactx->appctx->ctx.hlua_apphttp.status = status;
4746 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004747 lua_pushboolean(L, 1);
4748 return 1;
4749}
4750
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004751
Christopher Fauleta2097962019-07-15 16:25:33 +02004752__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004753{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004754 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4755 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004756 struct channel *res = si_ic(si);
4757 struct htx *htx;
4758 struct htx_sl *sl;
4759 struct h1m h1m;
4760 const char *status, *reason;
4761 const char *name, *value;
4762 size_t nlen, vlen;
4763 unsigned int flags;
4764
4765 /* Send the message at once. */
4766 htx = htx_from_buf(&res->buf);
4767 h1m_init_res(&h1m);
4768
4769 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004770 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4771 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004772 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02004773 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
4774 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004775 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4776 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4777 }
4778 else {
4779 flags = HTX_SL_F_IS_RESP;
4780 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4781 }
4782 if (!sl) {
4783 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004784 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004785 WILL_LJMP(lua_error(L));
4786 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004787 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004788
4789 /* Get the array associated to the field "response" in the object AppletHTTP. */
4790 lua_pushvalue(L, 0);
4791 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4792 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004793 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004794 WILL_LJMP(lua_error(L));
4795 }
4796
4797 /* Browse the list of headers. */
4798 lua_pushnil(L);
4799 while(lua_next(L, -2) != 0) {
4800 /* We expect a string as -2. */
4801 if (lua_type(L, -2) != LUA_TSTRING) {
4802 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004803 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004804 lua_typename(L, lua_type(L, -2)));
4805 WILL_LJMP(lua_error(L));
4806 }
4807 name = lua_tolstring(L, -2, &nlen);
4808
4809 /* We expect an array as -1. */
4810 if (lua_type(L, -1) != LUA_TTABLE) {
4811 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004812 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004813 name,
4814 lua_typename(L, lua_type(L, -1)));
4815 WILL_LJMP(lua_error(L));
4816 }
4817
4818 /* Browse the table who is on the top of the stack. */
4819 lua_pushnil(L);
4820 while(lua_next(L, -2) != 0) {
4821 int id;
4822
4823 /* We expect a number as -2. */
4824 if (lua_type(L, -2) != LUA_TNUMBER) {
4825 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004826 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004827 name,
4828 lua_typename(L, lua_type(L, -2)));
4829 WILL_LJMP(lua_error(L));
4830 }
4831 id = lua_tointeger(L, -2);
4832
4833 /* We expect a string as -2. */
4834 if (lua_type(L, -1) != LUA_TSTRING) {
4835 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004836 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004837 name, id,
4838 lua_typename(L, lua_type(L, -1)));
4839 WILL_LJMP(lua_error(L));
4840 }
4841 value = lua_tolstring(L, -1, &vlen);
4842
4843 /* Simple Protocol checks. */
4844 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004845 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004846 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4847 struct ist v = ist2(value, vlen);
4848 int ret;
4849
4850 ret = h1_parse_cont_len_header(&h1m, &v);
4851 if (ret < 0) {
4852 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004853 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004854 name);
4855 WILL_LJMP(lua_error(L));
4856 }
4857 else if (ret == 0)
4858 goto next; /* Skip it */
4859 }
4860
4861 /* Add a new header */
4862 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4863 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004864 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004865 name);
4866 WILL_LJMP(lua_error(L));
4867 }
4868 next:
4869 /* Remove the array from the stack, and get next element with a remaining string. */
4870 lua_pop(L, 1);
4871 }
4872
4873 /* Remove the array from the stack, and get next element with a remaining string. */
4874 lua_pop(L, 1);
4875 }
4876
4877 if (h1m.flags & H1_MF_CHNK)
4878 h1m.flags &= ~H1_MF_CLEN;
4879 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4880 h1m.flags |= H1_MF_XFER_LEN;
4881
4882 /* Uset HTX start-line flags */
4883 if (h1m.flags & H1_MF_XFER_ENC)
4884 flags |= HTX_SL_F_XFER_ENC;
4885 if (h1m.flags & H1_MF_XFER_LEN) {
4886 flags |= HTX_SL_F_XFER_LEN;
4887 if (h1m.flags & H1_MF_CHNK)
4888 flags |= HTX_SL_F_CHNK;
4889 else if (h1m.flags & H1_MF_CLEN)
4890 flags |= HTX_SL_F_CLEN;
4891 if (h1m.body_len == 0)
4892 flags |= HTX_SL_F_BODYLESS;
4893 }
4894 sl->flags |= flags;
4895
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004896 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004897 * and the status code implies the presence of a message body, we must
4898 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004899 * for the keepalive compliance. If the applet announces a transfer-encoding
4900 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004901 */
4902 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02004903 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
4904 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
4905 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004906 /* Add a new header */
4907 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4908 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4909 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004910 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004911 WILL_LJMP(lua_error(L));
4912 }
4913 }
4914
4915 /* Finalize headers. */
4916 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4917 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004918 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004919 WILL_LJMP(lua_error(L));
4920 }
4921
4922 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4923 b_reset(&res->buf);
4924 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4925 WILL_LJMP(lua_error(L));
4926 }
4927
4928 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004929 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004930
4931 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004932 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004933 return 0;
4934
4935}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004936/* We will build the status line and the headers of the HTTP response.
4937 * We will try send at once if its not possible, we give back the hand
4938 * waiting for more room.
4939 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004940__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004941{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004942 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4943 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004944 struct channel *res = si_ic(si);
4945
4946 if (co_data(res)) {
4947 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004948 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004949 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004950 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004951}
4952
4953
Christopher Fauleta2097962019-07-15 16:25:33 +02004954__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004955{
Christopher Fauleta2097962019-07-15 16:25:33 +02004956 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004957}
4958
Christopher Fauleta2097962019-07-15 16:25:33 +02004959/*
4960 *
4961 *
4962 * Class HTTP
4963 *
4964 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004965 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004966
4967/* Returns a struct hlua_txn if the stack entry "ud" is
4968 * a class stream, otherwise it throws an error.
4969 */
4970__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004971{
Christopher Fauleta2097962019-07-15 16:25:33 +02004972 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4973}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004974
Christopher Fauleta2097962019-07-15 16:25:33 +02004975/* This function creates and push in the stack a HTTP object
4976 * according with a current TXN.
4977 */
4978static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4979{
4980 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004981
Christopher Fauleta2097962019-07-15 16:25:33 +02004982 /* Check stack size. */
4983 if (!lua_checkstack(L, 3))
4984 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004985
Christopher Fauleta2097962019-07-15 16:25:33 +02004986 /* Create the object: obj[0] = userdata.
4987 * Note that the base of the Converters object is the
4988 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004989 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004990 lua_newtable(L);
4991 htxn = lua_newuserdata(L, sizeof(*htxn));
4992 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004993
4994 htxn->s = txn->s;
4995 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004996 htxn->dir = txn->dir;
4997 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004998
4999 /* Pop a class stream metatable and affect it to the table. */
5000 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5001 lua_setmetatable(L, -2);
5002
5003 return 1;
5004}
5005
5006/* This function creates ans returns an array of HTTP headers.
5007 * This function does not fails. It is used as wrapper with the
5008 * 2 following functions.
5009 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005010__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005011{
Christopher Fauleta2097962019-07-15 16:25:33 +02005012 struct htx *htx;
5013 int32_t pos;
5014
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005015 /* Create the table. */
5016 lua_newtable(L);
5017
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005018
Christopher Fauleta2097962019-07-15 16:25:33 +02005019 htx = htxbuf(&msg->chn->buf);
5020 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5021 struct htx_blk *blk = htx_get_blk(htx, pos);
5022 enum htx_blk_type type = htx_get_blk_type(blk);
5023 struct ist n, v;
5024 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005025
Christopher Fauleta2097962019-07-15 16:25:33 +02005026 if (type == HTX_BLK_HDR) {
5027 n = htx_get_blk_name(htx,blk);
5028 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005029 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005030 else if (type == HTX_BLK_EOH)
5031 break;
5032 else
5033 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005034
Christopher Fauleta2097962019-07-15 16:25:33 +02005035 /* Check for existing entry:
5036 * assume that the table is on the top of the stack, and
5037 * push the key in the stack, the function lua_gettable()
5038 * perform the lookup.
5039 */
5040 lua_pushlstring(L, n.ptr, n.len);
5041 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005042
Christopher Fauleta2097962019-07-15 16:25:33 +02005043 switch (lua_type(L, -1)) {
5044 case LUA_TNIL:
5045 /* Table not found, create it. */
5046 lua_pop(L, 1); /* remove the nil value. */
5047 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5048 lua_newtable(L); /* create and push empty table. */
5049 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5050 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5051 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005052 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005053
Christopher Fauleta2097962019-07-15 16:25:33 +02005054 case LUA_TTABLE:
5055 /* Entry found: push the value in the table. */
5056 len = lua_rawlen(L, -1);
5057 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5058 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5059 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5060 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005061
Christopher Fauleta2097962019-07-15 16:25:33 +02005062 default:
5063 /* Other cases are errors. */
5064 hlua_pusherror(L, "internal error during the parsing of headers.");
5065 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005066 }
5067 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005068 return 1;
5069}
5070
5071__LJMP static int hlua_http_req_get_headers(lua_State *L)
5072{
5073 struct hlua_txn *htxn;
5074
5075 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5076 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5077
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005078 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005079 WILL_LJMP(lua_error(L));
5080
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005081 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005082}
5083
5084__LJMP static int hlua_http_res_get_headers(lua_State *L)
5085{
5086 struct hlua_txn *htxn;
5087
5088 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5089 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5090
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005091 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005092 WILL_LJMP(lua_error(L));
5093
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005094 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005095}
5096
5097/* This function replace full header, or just a value in
5098 * the request or in the response. It is a wrapper fir the
5099 * 4 following functions.
5100 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005101__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005102{
5103 size_t name_len;
5104 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5105 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5106 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005107 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005108 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005109
Dragan Dosen26743032019-04-30 15:54:36 +02005110 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005111 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5112
Christopher Fauleta2097962019-07-15 16:25:33 +02005113 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005114 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005115 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005116 return 0;
5117}
5118
5119__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5120{
5121 struct hlua_txn *htxn;
5122
5123 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5124 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5125
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005126 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005127 WILL_LJMP(lua_error(L));
5128
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005129 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005130}
5131
5132__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5133{
5134 struct hlua_txn *htxn;
5135
5136 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5137 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5138
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005139 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005140 WILL_LJMP(lua_error(L));
5141
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005142 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005143}
5144
5145__LJMP static int hlua_http_req_rep_val(lua_State *L)
5146{
5147 struct hlua_txn *htxn;
5148
5149 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5150 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5151
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005152 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005153 WILL_LJMP(lua_error(L));
5154
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005155 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005156}
5157
5158__LJMP static int hlua_http_res_rep_val(lua_State *L)
5159{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005160 struct hlua_txn *htxn;
5161
5162 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5163 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5164
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005165 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005166 WILL_LJMP(lua_error(L));
5167
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005168 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005169}
5170
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005171/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005172 * It is a wrapper for the 2 following functions.
5173 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005174__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005175{
5176 size_t len;
5177 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005178 struct htx *htx = htxbuf(&msg->chn->buf);
5179 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005180
Christopher Fauleta2097962019-07-15 16:25:33 +02005181 ctx.blk = NULL;
5182 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5183 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005184 return 0;
5185}
5186
5187__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5188{
5189 struct hlua_txn *htxn;
5190
5191 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5192 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5193
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005194 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005195 WILL_LJMP(lua_error(L));
5196
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005197 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005198}
5199
5200__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5201{
5202 struct hlua_txn *htxn;
5203
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005204 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005205 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5206
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005207 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005208 WILL_LJMP(lua_error(L));
5209
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005210 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005211}
5212
5213/* This function adds an header. It is a wrapper used by
5214 * the 2 following functions.
5215 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005216__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005217{
5218 size_t name_len;
5219 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5220 size_t value_len;
5221 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005222 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005223
Christopher Fauleta2097962019-07-15 16:25:33 +02005224 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5225 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005226 return 0;
5227}
5228
5229__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5230{
5231 struct hlua_txn *htxn;
5232
5233 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5234 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5235
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005236 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005237 WILL_LJMP(lua_error(L));
5238
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005239 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005240}
5241
5242__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5243{
5244 struct hlua_txn *htxn;
5245
5246 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5247 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5248
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005249 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005250 WILL_LJMP(lua_error(L));
5251
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005252 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005253}
5254
5255static int hlua_http_req_set_hdr(lua_State *L)
5256{
5257 struct hlua_txn *htxn;
5258
5259 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5260 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5261
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005262 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005263 WILL_LJMP(lua_error(L));
5264
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005265 hlua_http_del_hdr(L, &htxn->s->txn->req);
5266 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005267}
5268
5269static int hlua_http_res_set_hdr(lua_State *L)
5270{
5271 struct hlua_txn *htxn;
5272
5273 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5274 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5275
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005276 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005277 WILL_LJMP(lua_error(L));
5278
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005279 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5280 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005281}
5282
5283/* This function set the method. */
5284static int hlua_http_req_set_meth(lua_State *L)
5285{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005286 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005287 size_t name_len;
5288 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005289
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005290 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005291 WILL_LJMP(lua_error(L));
5292
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005293 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005294 return 1;
5295}
5296
5297/* This function set the method. */
5298static int hlua_http_req_set_path(lua_State *L)
5299{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005300 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005301 size_t name_len;
5302 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005303
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005304 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005305 WILL_LJMP(lua_error(L));
5306
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005307 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005308 return 1;
5309}
5310
5311/* This function set the query-string. */
5312static int hlua_http_req_set_query(lua_State *L)
5313{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005314 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005315 size_t name_len;
5316 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005317
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005318 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005319 WILL_LJMP(lua_error(L));
5320
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005321 /* Check length. */
5322 if (name_len > trash.size - 1) {
5323 lua_pushboolean(L, 0);
5324 return 1;
5325 }
5326
5327 /* Add the mark question as prefix. */
5328 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005329 trash.area[trash.data++] = '?';
5330 memcpy(trash.area + trash.data, name, name_len);
5331 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005332
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005333 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005334 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005335 return 1;
5336}
5337
5338/* This function set the uri. */
5339static int hlua_http_req_set_uri(lua_State *L)
5340{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005341 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005342 size_t name_len;
5343 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005344
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005345 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005346 WILL_LJMP(lua_error(L));
5347
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005348 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005349 return 1;
5350}
5351
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005352/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005353static int hlua_http_res_set_status(lua_State *L)
5354{
5355 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5356 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005357 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5358 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005359
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005360 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005361 WILL_LJMP(lua_error(L));
5362
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005363 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005364 return 0;
5365}
5366
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005367/*
5368 *
5369 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005370 * Class TXN
5371 *
5372 *
5373 */
5374
5375/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005376 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005377 */
5378__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5379{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005380 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005381}
5382
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005383__LJMP static int hlua_set_var(lua_State *L)
5384{
5385 struct hlua_txn *htxn;
5386 const char *name;
5387 size_t len;
5388 struct sample smp;
5389
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005390 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5391 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005392
5393 /* It is useles to retrieve the stream, but this function
5394 * runs only in a stream context.
5395 */
5396 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5397 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5398
5399 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005400 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005401 hlua_lua2smp(L, 3, &smp);
5402
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02005403 /* Store the sample in a variable. We don't need to dup the smp, vars API
5404 * already takes care of duplicating dynamic var data.
5405 */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005406 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005407
5408 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5409 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5410 else
5411 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5412
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005413 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005414}
5415
Christopher Faulet85d79c92016-11-09 16:54:56 +01005416__LJMP static int hlua_unset_var(lua_State *L)
5417{
5418 struct hlua_txn *htxn;
5419 const char *name;
5420 size_t len;
5421 struct sample smp;
5422
5423 MAY_LJMP(check_args(L, 2, "unset_var"));
5424
5425 /* It is useles to retrieve the stream, but this function
5426 * runs only in a stream context.
5427 */
5428 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5429 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5430
5431 /* Unset the variable. */
5432 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005433 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5434 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005435}
5436
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005437__LJMP static int hlua_get_var(lua_State *L)
5438{
5439 struct hlua_txn *htxn;
5440 const char *name;
5441 size_t len;
5442 struct sample smp;
5443
5444 MAY_LJMP(check_args(L, 2, "get_var"));
5445
5446 /* It is useles to retrieve the stream, but this function
5447 * runs only in a stream context.
5448 */
5449 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5450 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5451
Willy Tarreau7560dd42016-03-10 16:28:58 +01005452 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005453 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005454 lua_pushnil(L);
5455 return 1;
5456 }
5457
5458 return hlua_smp2lua(L, &smp);
5459}
5460
Willy Tarreau59551662015-03-10 14:23:13 +01005461__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005462{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005463 struct hlua *hlua;
5464
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005465 MAY_LJMP(check_args(L, 2, "set_priv"));
5466
Willy Tarreau87b09662015-04-03 00:22:06 +02005467 /* It is useles to retrieve the stream, but this function
5468 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005469 */
5470 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005471
5472 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005473 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005474 if (!hlua)
5475 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005476
5477 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005478 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005479
5480 /* Get and store new value. */
5481 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5482 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5483
5484 return 0;
5485}
5486
Willy Tarreau59551662015-03-10 14:23:13 +01005487__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005488{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005489 struct hlua *hlua;
5490
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005491 MAY_LJMP(check_args(L, 1, "get_priv"));
5492
Willy Tarreau87b09662015-04-03 00:22:06 +02005493 /* It is useles to retrieve the stream, but this function
5494 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005495 */
5496 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005497
5498 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005499 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005500 if (!hlua) {
5501 lua_pushnil(L);
5502 return 1;
5503 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005504
5505 /* Push configuration index in the stack. */
5506 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5507
5508 return 1;
5509}
5510
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005511/* Create stack entry containing a class TXN. This function
5512 * return 0 if the stack does not contains free slots,
5513 * otherwise it returns 1.
5514 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005515static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005516{
Willy Tarreaude491382015-04-06 11:04:28 +02005517 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005518
5519 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005520 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005521 return 0;
5522
5523 /* NOTE: The allocation never fails. The failure
5524 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005525 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005526 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005527 /* Create the object: obj[0] = userdata. */
5528 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005529 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005530 lua_rawseti(L, -2, 0);
5531
Willy Tarreaude491382015-04-06 11:04:28 +02005532 htxn->s = s;
5533 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005534 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005535 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005536
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005537 /* Create the "f" field that contains a list of fetches. */
5538 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005539 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005540 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005541 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005542
5543 /* Create the "sf" field that contains a list of stringsafe fetches. */
5544 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005545 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005546 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005547 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005548
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005549 /* Create the "c" field that contains a list of converters. */
5550 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005551 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005552 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005553 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005554
5555 /* Create the "sc" field that contains a list of stringsafe converters. */
5556 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005557 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005558 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005559 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005560
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005561 /* Create the "req" field that contains the request channel object. */
5562 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005563 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005564 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005565 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005566
5567 /* Create the "res" field that contains the response channel object. */
5568 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005569 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005570 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005571 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005572
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005573 /* Creates the HTTP object is the current proxy allows http. */
5574 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005575 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005576 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005577 return 0;
5578 }
5579 else
5580 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005581 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005582
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005583 /* Pop a class sesison metatable and affect it to the userdata. */
5584 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5585 lua_setmetatable(L, -2);
5586
5587 return 1;
5588}
5589
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005590__LJMP static int hlua_txn_deflog(lua_State *L)
5591{
5592 const char *msg;
5593 struct hlua_txn *htxn;
5594
5595 MAY_LJMP(check_args(L, 2, "deflog"));
5596 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5597 msg = MAY_LJMP(luaL_checkstring(L, 2));
5598
5599 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5600 return 0;
5601}
5602
5603__LJMP static int hlua_txn_log(lua_State *L)
5604{
5605 int level;
5606 const char *msg;
5607 struct hlua_txn *htxn;
5608
5609 MAY_LJMP(check_args(L, 3, "log"));
5610 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5611 level = MAY_LJMP(luaL_checkinteger(L, 2));
5612 msg = MAY_LJMP(luaL_checkstring(L, 3));
5613
5614 if (level < 0 || level >= NB_LOG_LEVELS)
5615 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5616
5617 hlua_sendlog(htxn->s->be, level, msg);
5618 return 0;
5619}
5620
5621__LJMP static int hlua_txn_log_debug(lua_State *L)
5622{
5623 const char *msg;
5624 struct hlua_txn *htxn;
5625
5626 MAY_LJMP(check_args(L, 2, "Debug"));
5627 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5628 msg = MAY_LJMP(luaL_checkstring(L, 2));
5629 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5630 return 0;
5631}
5632
5633__LJMP static int hlua_txn_log_info(lua_State *L)
5634{
5635 const char *msg;
5636 struct hlua_txn *htxn;
5637
5638 MAY_LJMP(check_args(L, 2, "Info"));
5639 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5640 msg = MAY_LJMP(luaL_checkstring(L, 2));
5641 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5642 return 0;
5643}
5644
5645__LJMP static int hlua_txn_log_warning(lua_State *L)
5646{
5647 const char *msg;
5648 struct hlua_txn *htxn;
5649
5650 MAY_LJMP(check_args(L, 2, "Warning"));
5651 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5652 msg = MAY_LJMP(luaL_checkstring(L, 2));
5653 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5654 return 0;
5655}
5656
5657__LJMP static int hlua_txn_log_alert(lua_State *L)
5658{
5659 const char *msg;
5660 struct hlua_txn *htxn;
5661
5662 MAY_LJMP(check_args(L, 2, "Alert"));
5663 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5664 msg = MAY_LJMP(luaL_checkstring(L, 2));
5665 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5666 return 0;
5667}
5668
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005669__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5670{
5671 struct hlua_txn *htxn;
5672 int ll;
5673
5674 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5675 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5676 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5677
5678 if (ll < 0 || ll > 7)
5679 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5680
5681 htxn->s->logs.level = ll;
5682 return 0;
5683}
5684
5685__LJMP static int hlua_txn_set_tos(lua_State *L)
5686{
5687 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005688 int tos;
5689
5690 MAY_LJMP(check_args(L, 2, "set_tos"));
5691 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5692 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5693
Willy Tarreau1a18b542018-12-11 16:37:42 +01005694 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005695 return 0;
5696}
5697
5698__LJMP static int hlua_txn_set_mark(lua_State *L)
5699{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005700 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005701 int mark;
5702
5703 MAY_LJMP(check_args(L, 2, "set_mark"));
5704 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5705 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5706
Lukas Tribus579e3e32019-08-11 18:03:45 +02005707 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005708 return 0;
5709}
5710
Patrick Hemmer268a7072018-05-11 12:52:31 -04005711__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5712{
5713 struct hlua_txn *htxn;
5714
5715 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5716 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5717 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5718 return 0;
5719}
5720
5721__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5722{
5723 struct hlua_txn *htxn;
5724
5725 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5726 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5727 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5728 return 0;
5729}
5730
Christopher Faulet700d9e82020-01-31 12:21:52 +01005731/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005732 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005733 * message and terminate the transaction. It returns 1 on success and 0 on
5734 * error. The Reply must be on top of the stack.
5735 */
5736__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5737{
5738 struct htx *htx;
5739 struct htx_sl *sl;
5740 struct h1m h1m;
5741 const char *status, *reason, *body;
5742 size_t status_len, reason_len, body_len;
5743 int ret, code, flags;
5744
5745 code = 200;
5746 status = "200";
5747 status_len = 3;
5748 ret = lua_getfield(L, -1, "status");
5749 if (ret == LUA_TNUMBER) {
5750 code = lua_tointeger(L, -1);
5751 status = lua_tolstring(L, -1, &status_len);
5752 }
5753 lua_pop(L, 1);
5754
5755 reason = http_get_reason(code);
5756 reason_len = strlen(reason);
5757 ret = lua_getfield(L, -1, "reason");
5758 if (ret == LUA_TSTRING)
5759 reason = lua_tolstring(L, -1, &reason_len);
5760 lua_pop(L, 1);
5761
5762 body = NULL;
5763 body_len = 0;
5764 ret = lua_getfield(L, -1, "body");
5765 if (ret == LUA_TSTRING)
5766 body = lua_tolstring(L, -1, &body_len);
5767 lua_pop(L, 1);
5768
5769 /* Prepare the response before inserting the headers */
5770 h1m_init_res(&h1m);
5771 htx = htx_from_buf(&s->res.buf);
5772 channel_htx_truncate(&s->res, htx);
5773 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5774 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5775 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5776 ist2(status, status_len), ist2(reason, reason_len));
5777 }
5778 else {
5779 flags = HTX_SL_F_IS_RESP;
5780 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5781 ist2(status, status_len), ist2(reason, reason_len));
5782 }
5783 if (!sl)
5784 goto fail;
5785 sl->info.res.status = code;
5786
5787 /* Push in the stack the "headers" entry. */
5788 ret = lua_getfield(L, -1, "headers");
5789 if (ret != LUA_TTABLE)
5790 goto skip_headers;
5791
5792 lua_pushnil(L);
5793 while (lua_next(L, -2) != 0) {
5794 struct ist name, value;
5795 const char *n, *v;
5796 size_t nlen, vlen;
5797
5798 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5799 /* Skip element if the key is not a string or if the value is not a table */
5800 goto next_hdr;
5801 }
5802
5803 n = lua_tolstring(L, -2, &nlen);
5804 name = ist2(n, nlen);
5805 if (isteqi(name, ist("content-length"))) {
5806 /* Always skip content-length header. It will be added
5807 * later with the correct len
5808 */
5809 goto next_hdr;
5810 }
5811
5812 /* Loop on header's values */
5813 lua_pushnil(L);
5814 while (lua_next(L, -2)) {
5815 if (!lua_isstring(L, -1)) {
5816 /* Skip the value if it is not a string */
5817 goto next_value;
5818 }
5819
5820 v = lua_tolstring(L, -1, &vlen);
5821 value = ist2(v, vlen);
5822
5823 if (isteqi(name, ist("transfer-encoding")))
5824 h1_parse_xfer_enc_header(&h1m, value);
5825 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5826 goto fail;
5827
5828 next_value:
5829 lua_pop(L, 1);
5830 }
5831
5832 next_hdr:
5833 lua_pop(L, 1);
5834 }
5835 skip_headers:
5836 lua_pop(L, 1);
5837
5838 /* Update h1m flags: CLEN is set if CHNK is not present */
5839 if (!(h1m.flags & H1_MF_CHNK)) {
5840 const char *clen = ultoa(body_len);
5841
5842 h1m.flags |= H1_MF_CLEN;
5843 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5844 goto fail;
5845 }
5846 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5847 h1m.flags |= H1_MF_XFER_LEN;
5848
5849 /* Update HTX start-line flags */
5850 if (h1m.flags & H1_MF_XFER_ENC)
5851 flags |= HTX_SL_F_XFER_ENC;
5852 if (h1m.flags & H1_MF_XFER_LEN) {
5853 flags |= HTX_SL_F_XFER_LEN;
5854 if (h1m.flags & H1_MF_CHNK)
5855 flags |= HTX_SL_F_CHNK;
5856 else if (h1m.flags & H1_MF_CLEN)
5857 flags |= HTX_SL_F_CLEN;
5858 if (h1m.body_len == 0)
5859 flags |= HTX_SL_F_BODYLESS;
5860 }
5861 sl->flags |= flags;
5862
5863
5864 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005865 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005866 goto fail;
5867
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005868 htx->flags |= HTX_FL_EOM;
5869
Christopher Faulet700d9e82020-01-31 12:21:52 +01005870 /* Now, forward the response and terminate the transaction */
5871 s->txn->status = code;
5872 htx_to_buf(htx, &s->res.buf);
5873 if (!http_forward_proxy_resp(s, 1))
5874 goto fail;
5875
5876 return 1;
5877
5878 fail:
5879 channel_htx_truncate(&s->res, htx);
5880 return 0;
5881}
5882
5883/* Terminate a transaction if called from a lua action. For TCP streams,
5884 * processing is just aborted. Nothing is returned to the client and all
5885 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5886 * is forwarded to the client before terminating the transaction. On success,
5887 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5888 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5889 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005890 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005891__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005892{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005893 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005894 struct stream *s;
5895 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005896
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005897 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005898
Christopher Faulet700d9e82020-01-31 12:21:52 +01005899 /* If the flags NOTERM is set, we cannot terminate the session, so we
5900 * just end the execution of the current lua code. */
5901 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005902 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005903
Christopher Faulet700d9e82020-01-31 12:21:52 +01005904 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005905 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005906 struct channel *req = &s->req;
5907 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005908
Christopher Faulet700d9e82020-01-31 12:21:52 +01005909 channel_auto_read(req);
5910 channel_abort(req);
5911 channel_auto_close(req);
5912 channel_erase(req);
5913
5914 res->wex = tick_add_ifset(now_ms, res->wto);
5915 channel_auto_read(res);
5916 channel_auto_close(res);
5917 channel_shutr_now(res);
5918
5919 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5920 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005921 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005922
Christopher Faulet700d9e82020-01-31 12:21:52 +01005923 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5924 /* No reply or invalid reply */
5925 s->txn->status = 0;
5926 http_reply_and_close(s, 0, NULL);
5927 }
5928 else {
5929 /* Remove extra args to have the reply on top of the stack */
5930 if (lua_gettop(L) > 2)
5931 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005932
Christopher Faulet700d9e82020-01-31 12:21:52 +01005933 if (!hlua_txn_forward_reply(L, s)) {
5934 if (!(s->flags & SF_ERR_MASK))
5935 s->flags |= SF_ERR_PRXCOND;
5936 lua_pushinteger(L, ACT_RET_ERR);
5937 WILL_LJMP(hlua_done(L));
5938 return 0; /* Never reached */
5939 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005940 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005941
Christopher Faulet700d9e82020-01-31 12:21:52 +01005942 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5943 if (htxn->dir == SMP_OPT_DIR_REQ) {
5944 /* let's log the request time */
5945 s->logs.tv_request = now;
5946 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02005947 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01005948 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005949
Christopher Faulet700d9e82020-01-31 12:21:52 +01005950 done:
5951 if (!(s->flags & SF_ERR_MASK))
5952 s->flags |= SF_ERR_LOCAL;
5953 if (!(s->flags & SF_FINST_MASK))
5954 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005955
Christopher Faulet4ad73102020-03-05 11:07:31 +01005956 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005957 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005958 return 0;
5959}
5960
Christopher Faulet700d9e82020-01-31 12:21:52 +01005961/*
5962 *
5963 *
5964 * Class REPLY
5965 *
5966 *
5967 */
5968
5969/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5970 * free slots, the function fails and returns 0;
5971 */
5972static int hlua_txn_reply_new(lua_State *L)
5973{
5974 struct hlua_txn *htxn;
5975 const char *reason, *body = NULL;
5976 int ret, status;
5977
5978 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005979 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005980 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5981 WILL_LJMP(lua_error(L));
5982 }
5983
5984 /* Default value */
5985 status = 200;
5986 reason = http_get_reason(status);
5987
5988 if (lua_istable(L, 2)) {
5989 /* load status and reason from the table argument at index 2 */
5990 ret = lua_getfield(L, 2, "status");
5991 if (ret == LUA_TNIL)
5992 goto reason;
5993 else if (ret != LUA_TNUMBER) {
5994 /* invalid status: ignore the reason */
5995 goto body;
5996 }
5997 status = lua_tointeger(L, -1);
5998
5999 reason:
6000 lua_pop(L, 1); /* restore the stack: remove status */
6001 ret = lua_getfield(L, 2, "reason");
6002 if (ret == LUA_TSTRING)
6003 reason = lua_tostring(L, -1);
6004
6005 body:
6006 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
6007 ret = lua_getfield(L, 2, "body");
6008 if (ret == LUA_TSTRING)
6009 body = lua_tostring(L, -1);
6010 lua_pop(L, 1); /* restore the stack: remove body */
6011 }
6012
6013 /* Create the Reply table */
6014 lua_newtable(L);
6015
6016 /* Add status element */
6017 lua_pushstring(L, "status");
6018 lua_pushinteger(L, status);
6019 lua_settable(L, -3);
6020
6021 /* Add reason element */
6022 reason = http_get_reason(status);
6023 lua_pushstring(L, "reason");
6024 lua_pushstring(L, reason);
6025 lua_settable(L, -3);
6026
6027 /* Add body element, nil if undefined */
6028 lua_pushstring(L, "body");
6029 if (body)
6030 lua_pushstring(L, body);
6031 else
6032 lua_pushnil(L);
6033 lua_settable(L, -3);
6034
6035 /* Add headers element */
6036 lua_pushstring(L, "headers");
6037 lua_newtable(L);
6038
6039 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6040 if (lua_istable(L, 2)) {
6041 /* load headers from the table argument at index 2. If it is a table, copy it. */
6042 ret = lua_getfield(L, 2, "headers");
6043 if (ret == LUA_TTABLE) {
6044 /* stack: [ ... <headers:table>, <table> ] */
6045 lua_pushnil(L);
6046 while (lua_next(L, -2) != 0) {
6047 /* stack: [ ... <headers:table>, <table>, k, v] */
6048 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
6049 /* invalid value type, skip it */
6050 lua_pop(L, 1);
6051 continue;
6052 }
6053
6054
6055 /* Duplicate the key and swap it with the value. */
6056 lua_pushvalue(L, -2);
6057 lua_insert(L, -2);
6058 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6059
6060 lua_newtable(L);
6061 lua_insert(L, -2);
6062 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6063
6064 if (lua_isstring(L, -1)) {
6065 /* push the value in the inner table */
6066 lua_rawseti(L, -2, 1);
6067 }
6068 else { /* table */
6069 lua_pushnil(L);
6070 while (lua_next(L, -2) != 0) {
6071 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6072 if (!lua_isstring(L, -1)) {
6073 /* invalid value type, skip it*/
6074 lua_pop(L, 1);
6075 continue;
6076 }
6077 /* push the value in the inner table */
6078 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6079 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6080 }
6081 lua_pop(L, 1);
6082 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6083 }
6084
6085 /* push (k,v) on the stack in the headers table:
6086 * stack: [ ... <headers:table>, <table>, k, k, v ]
6087 */
6088 lua_settable(L, -5);
6089 /* stack: [ ... <headers:table>, <table>, k ] */
6090 }
6091 }
6092 lua_pop(L, 1);
6093 }
6094 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6095 lua_settable(L, -3);
6096 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6097
6098 /* Pop a class sesison metatable and affect it to the userdata. */
6099 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6100 lua_setmetatable(L, -2);
6101 return 1;
6102}
6103
6104/* Set the reply status code, and optionally the reason. If no reason is
6105 * provided, the default one corresponding to the status code is used.
6106 */
6107__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6108{
6109 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6110 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6111
6112 /* First argument (self) must be a table */
6113 luaL_checktype(L, 1, LUA_TTABLE);
6114
6115 if (status < 100 || status > 599) {
6116 lua_pushboolean(L, 0);
6117 return 1;
6118 }
6119 if (!reason)
6120 reason = http_get_reason(status);
6121
6122 lua_pushinteger(L, status);
6123 lua_setfield(L, 1, "status");
6124
6125 lua_pushstring(L, reason);
6126 lua_setfield(L, 1, "reason");
6127
6128 lua_pushboolean(L, 1);
6129 return 1;
6130}
6131
6132/* Add a header into the reply object. Each header name is associated to an
6133 * array of values in the "headers" table. If the header name is not found, a
6134 * new entry is created.
6135 */
6136__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6137{
6138 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6139 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6140 int ret;
6141
6142 /* First argument (self) must be a table */
6143 luaL_checktype(L, 1, LUA_TTABLE);
6144
6145 /* Push in the stack the "headers" entry. */
6146 ret = lua_getfield(L, 1, "headers");
6147 if (ret != LUA_TTABLE) {
6148 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6149 WILL_LJMP(lua_error(L));
6150 }
6151
6152 /* check if the header is already registered. If not, register it. */
6153 ret = lua_getfield(L, -1, name);
6154 if (ret == LUA_TNIL) {
6155 /* Entry not found. */
6156 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6157
6158 /* Insert the new header name in the array in the top of the stack.
6159 * It left the new array in the top of the stack.
6160 */
6161 lua_newtable(L);
6162 lua_pushstring(L, name);
6163 lua_pushvalue(L, -2);
6164 lua_settable(L, -4);
6165 }
6166 else if (ret != LUA_TTABLE) {
6167 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6168 WILL_LJMP(lua_error(L));
6169 }
6170
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006171 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006172 * the header value as new entry.
6173 */
6174 lua_pushstring(L, value);
6175 ret = lua_rawlen(L, -2);
6176 lua_rawseti(L, -2, ret + 1);
6177
6178 lua_pushboolean(L, 1);
6179 return 1;
6180}
6181
6182/* Remove all occurrences of a given header name. */
6183__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6184{
6185 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6186 int ret;
6187
6188 /* First argument (self) must be a table */
6189 luaL_checktype(L, 1, LUA_TTABLE);
6190
6191 /* Push in the stack the "headers" entry. */
6192 ret = lua_getfield(L, 1, "headers");
6193 if (ret != LUA_TTABLE) {
6194 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6195 WILL_LJMP(lua_error(L));
6196 }
6197
6198 lua_pushstring(L, name);
6199 lua_pushnil(L);
6200 lua_settable(L, -3);
6201
6202 lua_pushboolean(L, 1);
6203 return 1;
6204}
6205
6206/* Set the reply's body. Overwrite any existing entry. */
6207__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6208{
6209 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6210
6211 /* First argument (self) must be a table */
6212 luaL_checktype(L, 1, LUA_TTABLE);
6213
6214 lua_pushstring(L, payload);
6215 lua_setfield(L, 1, "body");
6216
6217 lua_pushboolean(L, 1);
6218 return 1;
6219}
6220
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006221__LJMP static int hlua_log(lua_State *L)
6222{
6223 int level;
6224 const char *msg;
6225
6226 MAY_LJMP(check_args(L, 2, "log"));
6227 level = MAY_LJMP(luaL_checkinteger(L, 1));
6228 msg = MAY_LJMP(luaL_checkstring(L, 2));
6229
6230 if (level < 0 || level >= NB_LOG_LEVELS)
6231 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6232
6233 hlua_sendlog(NULL, level, msg);
6234 return 0;
6235}
6236
6237__LJMP static int hlua_log_debug(lua_State *L)
6238{
6239 const char *msg;
6240
6241 MAY_LJMP(check_args(L, 1, "debug"));
6242 msg = MAY_LJMP(luaL_checkstring(L, 1));
6243 hlua_sendlog(NULL, LOG_DEBUG, msg);
6244 return 0;
6245}
6246
6247__LJMP static int hlua_log_info(lua_State *L)
6248{
6249 const char *msg;
6250
6251 MAY_LJMP(check_args(L, 1, "info"));
6252 msg = MAY_LJMP(luaL_checkstring(L, 1));
6253 hlua_sendlog(NULL, LOG_INFO, msg);
6254 return 0;
6255}
6256
6257__LJMP static int hlua_log_warning(lua_State *L)
6258{
6259 const char *msg;
6260
6261 MAY_LJMP(check_args(L, 1, "warning"));
6262 msg = MAY_LJMP(luaL_checkstring(L, 1));
6263 hlua_sendlog(NULL, LOG_WARNING, msg);
6264 return 0;
6265}
6266
6267__LJMP static int hlua_log_alert(lua_State *L)
6268{
6269 const char *msg;
6270
6271 MAY_LJMP(check_args(L, 1, "alert"));
6272 msg = MAY_LJMP(luaL_checkstring(L, 1));
6273 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006274 return 0;
6275}
6276
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006277__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006278{
6279 int wakeup_ms = lua_tointeger(L, -1);
Willy Tarreau9cc2e4c2021-09-30 16:12:31 +02006280 if (!tick_is_expired(wakeup_ms, now_ms))
Willy Tarreau9635e032018-10-16 17:52:55 +02006281 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006282 return 0;
6283}
6284
6285__LJMP static int hlua_sleep(lua_State *L)
6286{
6287 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006288 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006289
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006290 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006291
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006292 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006293 wakeup_ms = tick_add(now_ms, delay);
6294 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006295
Willy Tarreau9635e032018-10-16 17:52:55 +02006296 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006297 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006298}
6299
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006300__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006301{
6302 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006303 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006304
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006305 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006306
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006307 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006308 wakeup_ms = tick_add(now_ms, delay);
6309 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006310
Willy Tarreau9635e032018-10-16 17:52:55 +02006311 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006312 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006313}
6314
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006315/* This functionis an LUA binding. it permits to give back
6316 * the hand at the HAProxy scheduler. It is used when the
6317 * LUA processing consumes a lot of time.
6318 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006319__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006320{
6321 return 0;
6322}
6323
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006324__LJMP static int hlua_yield(lua_State *L)
6325{
Willy Tarreau9635e032018-10-16 17:52:55 +02006326 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006327 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006328}
6329
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006330/* This function change the nice of the currently executed
6331 * task. It is used set low or high priority at the current
6332 * task.
6333 */
Willy Tarreau59551662015-03-10 14:23:13 +01006334__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006335{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006336 struct hlua *hlua;
6337 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006338
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006339 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006340 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006341
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006342 /* Get hlua struct, or NULL if we execute from main lua state */
6343 hlua = hlua_gethlua(L);
6344
6345 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006346 if (!hlua || !hlua->task)
6347 return 0;
6348
6349 if (nice < -1024)
6350 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006351 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006352 nice = 1024;
6353
6354 hlua->task->nice = nice;
6355 return 0;
6356}
6357
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006358/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006359 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6360 * return an E_AGAIN signal, the emmiter of this signal must set a
6361 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006362 *
6363 * Task wrapper are longjmp safe because the only one Lua code
6364 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006365 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006366struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006367{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006368 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006369 enum hlua_exec status;
6370
Christopher Faulet5bc99722018-04-25 10:34:45 +02006371 if (task->thread_mask == MAX_THREADS_MASK)
6372 task_set_affinity(task, tid_bit);
6373
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006374 /* If it is the first call to the task, we must initialize the
6375 * execution timeouts.
6376 */
6377 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006378 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006379
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006380 /* Execute the Lua code. */
6381 status = hlua_ctx_resume(hlua, 1);
6382
6383 switch (status) {
6384 /* finished or yield */
6385 case HLUA_E_OK:
6386 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006387 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006388 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006389 break;
6390
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006391 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006392 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006393 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006394 break;
6395
6396 /* finished with error. */
6397 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006398 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006399 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006400 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006401 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006402 break;
6403
6404 case HLUA_E_ERR:
6405 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006406 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006407 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006408 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006409 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006410 break;
6411 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006412 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006413}
6414
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006415/* This function is an LUA binding that register LUA function to be
6416 * executed after the HAProxy configuration parsing and before the
6417 * HAProxy scheduler starts. This function expect only one LUA
6418 * argument that is a function. This function returns nothing, but
6419 * throws if an error is encountered.
6420 */
6421__LJMP static int hlua_register_init(lua_State *L)
6422{
6423 struct hlua_init_function *init;
6424 int ref;
6425
6426 MAY_LJMP(check_args(L, 1, "register_init"));
6427
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006428 if (hlua_gethlua(L)) {
6429 /* runtime processing */
6430 WILL_LJMP(luaL_error(L, "register_init: not available outside of body context"));
6431 }
6432
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006433 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6434
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006435 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006436 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006437 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006438
6439 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006440 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006441 return 0;
6442}
6443
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006444/* This functio is an LUA binding. It permits to register a task
6445 * executed in parallel of the main HAroxy activity. The task is
6446 * created and it is set in the HAProxy scheduler. It can be called
6447 * from the "init" section, "post init" or during the runtime.
6448 *
6449 * Lua prototype:
6450 *
6451 * <none> core.register_task(<function>)
6452 */
6453static int hlua_register_task(lua_State *L)
6454{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006455 struct hlua *hlua = NULL;
6456 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006457 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006458 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006459
6460 MAY_LJMP(check_args(L, 1, "register_task"));
6461
6462 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6463
Thierry Fournier75fc0292020-11-28 13:18:56 +01006464 /* Get the reference state. If the reference is NULL, L is the master
6465 * state, otherwise hlua->T is.
6466 */
6467 hlua = hlua_gethlua(L);
6468 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006469 /* we are in runtime processing */
6470 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006471 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006472 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006473 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006474
Willy Tarreaubafbe012017-11-24 17:34:44 +01006475 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006476 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006477 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006478 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006479
Thierry Fournier59f11be2020-11-29 00:37:41 +01006480 /* We are in the common lua state, execute the task anywhere,
6481 * otherwise, inherit the current thread identifier
6482 */
6483 if (state_id == 0)
6484 task = task_new(MAX_THREADS_MASK);
6485 else
6486 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006487 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006488 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006489
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006490 task->context = hlua;
6491 task->process = hlua_process_task;
6492
Thierry Fournier021d9862020-11-28 23:42:03 +01006493 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006494 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006495
6496 /* Restore the function in the stack. */
6497 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
Aurelien DARRAGONb6aade32023-03-13 14:09:21 +01006498 /* function ref not needed anymore since it was pushed to the substack */
6499 hlua_unref(L, ref);
6500
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006501 hlua->nargs = 0;
6502
6503 /* Schedule task. */
Willy Tarreau790810f2021-09-30 16:17:37 +02006504 task_wakeup(task, TASK_WOKEN_INIT);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006505
6506 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006507
6508 alloc_error:
6509 task_destroy(task);
6510 hlua_ctx_destroy(hlua);
6511 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6512 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006513}
6514
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006515/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6516 * doesn't allow "yield" functions because the HAProxy engine cannot
6517 * resume converters.
6518 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006519static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006520{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006521 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006522 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006523 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006524
Willy Tarreaube508f12016-03-10 11:47:01 +01006525 if (!stream)
6526 return 0;
6527
Willy Tarreau87b09662015-04-03 00:22:06 +02006528 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006529 * Lua context can be not initialized. This behavior
6530 * permits to save performances because a systematic
6531 * Lua initialization cause 5% performances loss.
6532 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006533 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006534 struct hlua *hlua;
6535
6536 hlua = pool_alloc(pool_head_hlua);
6537 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006538 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6539 return 0;
6540 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006541 HLUA_INIT(hlua);
6542 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006543 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006544 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6545 return 0;
6546 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006547 }
6548
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006549 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006550 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006551
6552 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006553 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006554 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6555 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006556 else
6557 error = "critical error";
6558 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006559 return 0;
6560 }
6561
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006562 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006563 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006564 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006565 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006566 return 0;
6567 }
6568
6569 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006570 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006571
6572 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006574 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006575 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006576 return 0;
6577 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006578 hlua_smp2lua(stream->hlua->T, smp);
6579 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006580
6581 /* push keywords in the stack. */
6582 if (arg_p) {
6583 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006584 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006585 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006586 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006587 return 0;
6588 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006589 hlua_arg2lua(stream->hlua->T, arg_p);
6590 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006591 }
6592 }
6593
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006594 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006595 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006596
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006597 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006598 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006599 }
6600
6601 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006602 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006603 /* finished. */
6604 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006605 /* If the stack is empty, the function fails. */
6606 if (lua_gettop(stream->hlua->T) <= 0)
6607 return 0;
6608
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006609 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 hlua_lua2smp(stream->hlua->T, -1, smp);
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02006611 /* dup the smp before popping the related lua value and
6612 * returning it to haproxy
6613 */
6614 smp_dup(smp);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006615 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006616 return 1;
6617
6618 /* yield. */
6619 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006620 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006621 return 0;
6622
6623 /* finished with error. */
6624 case HLUA_E_ERRMSG:
6625 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006626 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006627 fcn->name, lua_tostring(stream->hlua->T, -1));
6628 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006629 return 0;
6630
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006631 case HLUA_E_ETMOUT:
6632 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6633 return 0;
6634
6635 case HLUA_E_NOMEM:
6636 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6637 return 0;
6638
6639 case HLUA_E_YIELD:
6640 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6641 return 0;
6642
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006643 case HLUA_E_ERR:
6644 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006645 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006646 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006647
6648 default:
6649 return 0;
6650 }
6651}
6652
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006653/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6654 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006655 * resume sample-fetches. This function will be called by the sample
6656 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006657 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006658static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6659 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006660{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006661 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006662 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006663 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006664 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006665
Willy Tarreaube508f12016-03-10 11:47:01 +01006666 if (!stream)
6667 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006668
Willy Tarreau87b09662015-04-03 00:22:06 +02006669 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006670 * Lua context can be not initialized. This behavior
6671 * permits to save performances because a systematic
6672 * Lua initialization cause 5% performances loss.
6673 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006674 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006675 struct hlua *hlua;
6676
6677 hlua = pool_alloc(pool_head_hlua);
6678 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006679 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6680 return 0;
6681 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006682 hlua->T = NULL;
6683 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006684 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006685 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6686 return 0;
6687 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006688 }
6689
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006690 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006691 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006692
6693 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006694 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006695 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6696 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006697 else
6698 error = "critical error";
6699 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006700 return 0;
6701 }
6702
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006703 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006704 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006705 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006706 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006707 return 0;
6708 }
6709
6710 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006711 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006712
6713 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006714 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006715 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006716 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006717 return 0;
6718 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006719 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006720
6721 /* push keywords in the stack. */
6722 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6723 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006724 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006725 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006726 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006727 return 0;
6728 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006729 hlua_arg2lua(stream->hlua->T, arg_p);
6730 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006731 }
6732
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006733 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006734 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006735
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006736 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006737 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006738 }
6739
6740 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006741 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006742 /* finished. */
6743 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006744 /* If the stack is empty, the function fails. */
6745 if (lua_gettop(stream->hlua->T) <= 0)
6746 return 0;
6747
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006748 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006749 hlua_lua2smp(stream->hlua->T, -1, smp);
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02006750 /* dup the smp before popping the related lua value and
6751 * returning it to haproxy
6752 */
6753 smp_dup(smp);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006754 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006755
6756 /* Set the end of execution flag. */
6757 smp->flags &= ~SMP_F_MAY_CHANGE;
6758 return 1;
6759
6760 /* yield. */
6761 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006762 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006763 return 0;
6764
6765 /* finished with error. */
6766 case HLUA_E_ERRMSG:
6767 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006768 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006769 fcn->name, lua_tostring(stream->hlua->T, -1));
6770 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006771 return 0;
6772
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006773 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006774 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6775 return 0;
6776
6777 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006778 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6779 return 0;
6780
6781 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006782 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6783 return 0;
6784
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006785 case HLUA_E_ERR:
6786 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006787 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006788 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006789
6790 default:
6791 return 0;
6792 }
6793}
6794
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006795/* This function is an LUA binding used for registering
6796 * "sample-conv" functions. It expects a converter name used
6797 * in the haproxy configuration file, and an LUA function.
6798 */
6799__LJMP static int hlua_register_converters(lua_State *L)
6800{
6801 struct sample_conv_kw_list *sck;
6802 const char *name;
6803 int ref;
6804 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02006805 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006806 struct sample_conv *sc;
6807 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006808
6809 MAY_LJMP(check_args(L, 2, "register_converters"));
6810
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006811 if (hlua_gethlua(L)) {
6812 /* runtime processing */
6813 WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context"));
6814 }
6815
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006816 /* First argument : converter name. */
6817 name = MAY_LJMP(luaL_checkstring(L, 1));
6818
6819 /* Second argument : lua function. */
6820 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6821
Thierry Fournierf67442e2020-11-28 20:41:07 +01006822 /* Check if the converter is already registered */
6823 trash = get_trash_chunk();
6824 chunk_printf(trash, "lua.%s", name);
6825 sc = find_sample_conv(trash->area, trash->data);
6826 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006827 fcn = sc->private;
6828 if (fcn->function_ref[hlua_state_id] != -1) {
6829 ha_warning("Trying to register converter 'lua.%s' more than once. "
6830 "This will become a hard error in version 2.5.\n", name);
6831 }
6832 fcn->function_ref[hlua_state_id] = ref;
6833 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006834 }
6835
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006836 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006837 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006838 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02006839 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006840 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006841 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02006842 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006843
6844 /* Fill fcn. */
6845 fcn->name = strdup(name);
6846 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02006847 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006848 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006849
6850 /* List head */
6851 sck->list.n = sck->list.p = NULL;
6852
6853 /* converter keyword. */
6854 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006855 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006856 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02006857 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006858
6859 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6860 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006861 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 +01006862 sck->kw[0].val_args = NULL;
6863 sck->kw[0].in_type = SMP_T_STR;
6864 sck->kw[0].out_type = SMP_T_STR;
6865 sck->kw[0].private = fcn;
6866
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006867 /* Register this new converter */
6868 sample_register_convs(sck);
6869
6870 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02006871
6872 alloc_error:
6873 release_hlua_function(fcn);
6874 ha_free(&sck);
6875 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6876 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006877}
6878
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006879/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006880 * "sample-fetch" functions. It expects a converter name used
6881 * in the haproxy configuration file, and an LUA function.
6882 */
6883__LJMP static int hlua_register_fetches(lua_State *L)
6884{
6885 const char *name;
6886 int ref;
6887 int len;
6888 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02006889 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006890 struct sample_fetch *sf;
6891 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006892
6893 MAY_LJMP(check_args(L, 2, "register_fetches"));
6894
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006895 if (hlua_gethlua(L)) {
6896 /* runtime processing */
6897 WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context"));
6898 }
6899
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006900 /* First argument : sample-fetch name. */
6901 name = MAY_LJMP(luaL_checkstring(L, 1));
6902
6903 /* Second argument : lua function. */
6904 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6905
Thierry Fournierf67442e2020-11-28 20:41:07 +01006906 /* Check if the sample-fetch is already registered */
6907 trash = get_trash_chunk();
6908 chunk_printf(trash, "lua.%s", name);
6909 sf = find_sample_fetch(trash->area, trash->data);
6910 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006911 fcn = sf->private;
6912 if (fcn->function_ref[hlua_state_id] != -1) {
6913 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6914 "This will become a hard error in version 2.5.\n", name);
6915 }
6916 fcn->function_ref[hlua_state_id] = ref;
6917 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006918 }
6919
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006920 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006921 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006922 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02006923 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006924 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006925 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02006926 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006927
6928 /* Fill fcn. */
6929 fcn->name = strdup(name);
6930 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02006931 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006932 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006933
6934 /* List head */
6935 sfk->list.n = sfk->list.p = NULL;
6936
6937 /* sample-fetch keyword. */
6938 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006939 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006940 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02006941 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006942
6943 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6944 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006945 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 +01006946 sfk->kw[0].val_args = NULL;
6947 sfk->kw[0].out_type = SMP_T_STR;
6948 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6949 sfk->kw[0].val = 0;
6950 sfk->kw[0].private = fcn;
6951
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006952 /* Register this new fetch. */
6953 sample_register_fetches(sfk);
6954
6955 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02006956
6957 alloc_error:
6958 release_hlua_function(fcn);
6959 ha_free(&sfk);
6960 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6961 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006962}
6963
Christopher Faulet501465d2020-02-26 14:54:16 +01006964/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006965 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006966__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006967{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006968 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006969 unsigned int delay;
6970 unsigned int wakeup_ms;
6971
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006972 /* Get hlua struct, or NULL if we execute from main lua state */
6973 hlua = hlua_gethlua(L);
6974 if (!hlua) {
6975 return 0;
6976 }
6977
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006978 MAY_LJMP(check_args(L, 1, "wake_time"));
6979
6980 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6981 wakeup_ms = tick_add(now_ms, delay);
6982 hlua->wake_time = wakeup_ms;
6983 return 0;
6984}
6985
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006986/* This function is a wrapper to execute each LUA function declared as an action
6987 * wrapper during the initialisation period. This function may return any
6988 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6989 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6990 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006991 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006992static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006993 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006994{
6995 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006996 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006997 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006998 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006999
7000 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01007001 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
7002 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
7003 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
7004 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007005 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007006 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007007 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007008 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007009
Willy Tarreau87b09662015-04-03 00:22:06 +02007010 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007011 * Lua context can be not initialized. This behavior
7012 * permits to save performances because a systematic
7013 * Lua initialization cause 5% performances loss.
7014 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007015 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007016 struct hlua *hlua;
7017
7018 hlua = pool_alloc(pool_head_hlua);
7019 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007020 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007021 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007022 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007023 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007024 HLUA_INIT(hlua);
7025 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01007026 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 +01007027 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007028 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007029 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007030 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007031 }
7032
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007033 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007034 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007035
7036 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007037 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007038 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
7039 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01007040 else
7041 error = "critical error";
7042 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007043 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007044 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007045 }
7046
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007047 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007048 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007049 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007050 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007051 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007052 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007053 }
7054
7055 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007056 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 +01007057
Willy Tarreau87b09662015-04-03 00:22:06 +02007058 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007059 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007060 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007061 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007062 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007063 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007064 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007065 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007066
7067 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007068 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007069 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007070 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007071 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007072 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007073 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007074 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007075 lua_pushstring(s->hlua->T, *arg);
7076 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007077 }
7078
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007079 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007080 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007081
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007082 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007083 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007084 }
7085
7086 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01007087 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007088 /* finished. */
7089 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007090 /* Catch the return value */
7091 if (lua_gettop(s->hlua->T) > 0)
7092 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007093
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007094 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007095 if (act_ret == ACT_RET_YIELD) {
7096 if (flags & ACT_OPT_FINAL)
7097 goto err_yield;
7098
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007099 if (dir == SMP_OPT_DIR_REQ)
7100 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7101 s->hlua->wake_time);
7102 else
7103 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7104 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007105 }
7106 goto end;
7107
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007108 /* yield. */
7109 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007110 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007111 if (dir == SMP_OPT_DIR_REQ)
7112 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7113 s->hlua->wake_time);
7114 else
7115 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7116 s->hlua->wake_time);
7117
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007118 /* Some actions can be wake up when a "write" event
7119 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007120 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007121 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007122 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007123 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007124 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007125 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007126 act_ret = ACT_RET_YIELD;
7127 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007128
7129 /* finished with error. */
7130 case HLUA_E_ERRMSG:
7131 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007132 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007133 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007134 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007135 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007136
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007137 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007138 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007139 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007140
7141 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007142 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007143 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007144
7145 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007146 err_yield:
7147 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007148 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007149 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007150 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007151
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007152 case HLUA_E_ERR:
7153 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007154 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007155 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007156
7157 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007158 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007159 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007160
7161 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007162 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007163 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007164 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007165}
7166
Willy Tarreau144f84a2021-03-02 16:09:26 +01007167struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007168{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007169 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007170
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007171 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007172 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007173 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007174}
7175
7176static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7177{
7178 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007179 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007180 struct task *task;
7181 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007182 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007183
Willy Tarreaubafbe012017-11-24 17:34:44 +01007184 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007185 if (!hlua) {
7186 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007187 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007188 return 0;
7189 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007190 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007191 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007192 ctx->ctx.hlua_apptcp.flags = 0;
7193
7194 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007195 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007196 if (!task) {
7197 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007198 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007199 return 0;
7200 }
7201 task->nice = 0;
7202 task->context = ctx;
7203 task->process = hlua_applet_wakeup;
7204 ctx->ctx.hlua_apptcp.task = task;
7205
7206 /* In the execution wrappers linked with a stream, the
7207 * Lua context can be not initialized. This behavior
7208 * permits to save performances because a systematic
7209 * Lua initialization cause 5% performances loss.
7210 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007211 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 +02007212 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007213 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007214 return 0;
7215 }
7216
7217 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007218 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007219
7220 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007221 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007222 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7223 error = lua_tostring(hlua->T, -1);
7224 else
7225 error = "critical error";
7226 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007227 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007228 return 0;
7229 }
7230
7231 /* Check stack available size. */
7232 if (!lua_checkstack(hlua->T, 1)) {
7233 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007234 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007235 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007236 return 0;
7237 }
7238
7239 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007240 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007241
7242 /* Create and and push object stream in the stack. */
7243 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7244 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007245 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007246 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007247 return 0;
7248 }
7249 hlua->nargs = 1;
7250
7251 /* push keywords in the stack. */
7252 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7253 if (!lua_checkstack(hlua->T, 1)) {
7254 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007255 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007256 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007257 return 0;
7258 }
7259 lua_pushstring(hlua->T, *arg);
7260 hlua->nargs++;
7261 }
7262
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007263 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007264
7265 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007266 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007267 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007268
7269 return 1;
7270}
7271
Willy Tarreau60409db2019-08-21 14:14:50 +02007272void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007273{
7274 struct stream_interface *si = ctx->owner;
7275 struct stream *strm = si_strm(si);
7276 struct channel *res = si_ic(si);
7277 struct act_rule *rule = ctx->rule;
7278 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007279 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007280
7281 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007282 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7283 /* eat the whole request */
7284 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007285 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007286 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007287
7288 /* If the stream is disconnect or closed, ldo nothing. */
7289 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7290 return;
7291
7292 /* Execute the function. */
7293 switch (hlua_ctx_resume(hlua, 1)) {
7294 /* finished. */
7295 case HLUA_E_OK:
7296 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7297
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007298 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007299 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007300 res->flags |= CF_READ_NULL;
7301 si_shutr(si);
7302 return;
7303
7304 /* yield. */
7305 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007306 if (hlua->wake_time != TICK_ETERNITY)
7307 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007308 return;
7309
7310 /* finished with error. */
7311 case HLUA_E_ERRMSG:
7312 /* Display log. */
7313 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007314 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007315 lua_pop(hlua->T, 1);
7316 goto error;
7317
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007318 case HLUA_E_ETMOUT:
7319 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007320 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007321 goto error;
7322
7323 case HLUA_E_NOMEM:
7324 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007325 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007326 goto error;
7327
7328 case HLUA_E_YIELD: /* unexpected */
7329 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007330 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007331 goto error;
7332
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007333 case HLUA_E_ERR:
7334 /* Display log. */
7335 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007336 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007337 goto error;
7338
7339 default:
7340 goto error;
7341 }
7342
7343error:
7344
7345 /* For all other cases, just close the stream. */
7346 si_shutw(si);
7347 si_shutr(si);
7348 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7349}
7350
7351static void hlua_applet_tcp_release(struct appctx *ctx)
7352{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007353 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007354 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007355 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007356 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007357}
7358
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007359/* The function returns 1 if the initialisation is complete, 0 if
7360 * an errors occurs and -1 if more data are required for initializing
7361 * the applet.
7362 */
7363static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7364{
7365 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007366 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007367 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007368 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007369 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007370 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007371
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007372 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007373 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007374 if (!hlua) {
7375 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007376 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007377 return 0;
7378 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007379 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007380 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007381 ctx->ctx.hlua_apphttp.left_bytes = -1;
7382 ctx->ctx.hlua_apphttp.flags = 0;
7383
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007384 if (txn->req.flags & HTTP_MSGF_VER_11)
7385 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7386
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007387 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007388 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007389 if (!task) {
7390 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007391 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007392 return 0;
7393 }
7394 task->nice = 0;
7395 task->context = ctx;
7396 task->process = hlua_applet_wakeup;
7397 ctx->ctx.hlua_apphttp.task = task;
7398
7399 /* In the execution wrappers linked with a stream, the
7400 * Lua context can be not initialized. This behavior
7401 * permits to save performances because a systematic
7402 * Lua initialization cause 5% performances loss.
7403 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007404 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 +02007405 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007406 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007407 return 0;
7408 }
7409
7410 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007411 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007412
7413 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007414 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007415 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7416 error = lua_tostring(hlua->T, -1);
7417 else
7418 error = "critical error";
7419 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007420 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007421 return 0;
7422 }
7423
7424 /* Check stack available size. */
7425 if (!lua_checkstack(hlua->T, 1)) {
7426 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007427 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007428 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007429 return 0;
7430 }
7431
7432 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007433 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007434
7435 /* Create and and push object stream in the stack. */
7436 if (!hlua_applet_http_new(hlua->T, ctx)) {
7437 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007438 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007439 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007440 return 0;
7441 }
7442 hlua->nargs = 1;
7443
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007444 /* push keywords in the stack. */
7445 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7446 if (!lua_checkstack(hlua->T, 1)) {
7447 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007448 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007449 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007450 return 0;
7451 }
7452 lua_pushstring(hlua->T, *arg);
7453 hlua->nargs++;
7454 }
7455
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007456 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007457
7458 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007459 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007460
7461 return 1;
7462}
7463
Willy Tarreau60409db2019-08-21 14:14:50 +02007464void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007465{
7466 struct stream_interface *si = ctx->owner;
7467 struct stream *strm = si_strm(si);
7468 struct channel *req = si_oc(si);
7469 struct channel *res = si_ic(si);
7470 struct act_rule *rule = ctx->rule;
7471 struct proxy *px = strm->be;
7472 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7473 struct htx *req_htx, *res_htx;
7474
7475 res_htx = htx_from_buf(&res->buf);
7476
7477 /* If the stream is disconnect or closed, ldo nothing. */
7478 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7479 goto out;
7480
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007481 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007482 if (!b_size(&res->buf)) {
7483 si_rx_room_blk(si);
7484 goto out;
7485 }
7486 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007487 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007488 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7489
7490 /* Set the currently running flag. */
7491 if (!HLUA_IS_RUNNING(hlua) &&
7492 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007493 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007494 si_cant_get(si);
7495 goto out;
7496 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007497 }
7498
7499 /* Executes The applet if it is not done. */
7500 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7501
7502 /* Execute the function. */
7503 switch (hlua_ctx_resume(hlua, 1)) {
7504 /* finished. */
7505 case HLUA_E_OK:
7506 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7507 break;
7508
7509 /* yield. */
7510 case HLUA_E_AGAIN:
7511 if (hlua->wake_time != TICK_ETERNITY)
7512 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007513 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007514
7515 /* finished with error. */
7516 case HLUA_E_ERRMSG:
7517 /* Display log. */
7518 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007519 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007520 lua_pop(hlua->T, 1);
7521 goto error;
7522
7523 case HLUA_E_ETMOUT:
7524 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007525 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007526 goto error;
7527
7528 case HLUA_E_NOMEM:
7529 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007530 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007531 goto error;
7532
7533 case HLUA_E_YIELD: /* unexpected */
7534 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007535 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007536 goto error;
7537
7538 case HLUA_E_ERR:
7539 /* Display log. */
7540 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007541 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007542 goto error;
7543
7544 default:
7545 goto error;
7546 }
7547 }
7548
7549 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007550 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7551 goto done;
7552
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007553 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7554 goto error;
7555
Christopher Faulet868b3b12022-04-07 10:07:18 +02007556 /* no more data are expected. If the response buffer is empty
7557 * for a chunked message, be sure to add something (EOT block in
7558 * this case) to have something to send. It is important to be
7559 * sure the EOM flags will be handled by the endpoint.
7560 */
7561 if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) {
7562 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
7563 si_rx_room_blk(si);
7564 goto out;
7565 }
7566 channel_add_input(res, 1);
7567 }
7568
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007569 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet79c0fc72022-03-07 15:50:54 +01007570 res->flags |= CF_EOI;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007571 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7572 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007573 }
7574
7575 done:
7576 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007577 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007578 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007579 si_shutr(si);
7580 }
7581
7582 /* eat the whole request */
7583 if (co_data(req)) {
7584 req_htx = htx_from_buf(&req->buf);
7585 co_htx_skip(req, req_htx, co_data(req));
7586 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007587 }
7588 }
7589
7590 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007591 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007592 return;
7593
7594 error:
7595
7596 /* If we are in HTTP mode, and we are not send any
7597 * data, return a 500 server error in best effort:
7598 * if there is no room available in the buffer,
7599 * just close the connection.
7600 */
7601 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007602 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007603
7604 channel_erase(res);
7605 res->buf.data = b_data(err);
7606 memcpy(res->buf.area, b_head(err), b_data(err));
7607 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007608 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007609 }
7610 if (!(strm->flags & SF_ERR_MASK))
7611 strm->flags |= SF_ERR_RESOURCE;
7612 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7613 goto done;
7614}
7615
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007616static void hlua_applet_http_release(struct appctx *ctx)
7617{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007618 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007619 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007620 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007621 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007622}
7623
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007624/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007625 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007626 *
7627 * This function can fail with an abort() due to an Lua critical error.
7628 * We are in the configuration parsing process of HAProxy, this abort() is
7629 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007630 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007631static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7632 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007633{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007634 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007635 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007636
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007637 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007638 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007639 if (!rule->arg.hlua_rule) {
7640 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007641 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007642 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007643
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007644 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007645 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7646 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007647 if (!rule->arg.hlua_rule->args) {
7648 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007649 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007650 }
7651
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007652 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007653 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007654
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007655 /* Expect some arguments */
7656 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007657 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007658 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007659 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007660 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007661 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007662 if (!rule->arg.hlua_rule->args[i]) {
7663 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007664 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007665 }
7666 (*cur_arg)++;
7667 }
7668 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007669
Thierry FOURNIER42148732015-09-02 17:17:33 +02007670 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007671 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007672 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007673
7674 error:
7675 if (rule->arg.hlua_rule) {
7676 if (rule->arg.hlua_rule->args) {
7677 for (i = 0; i < fcn->nargs; i++)
7678 ha_free(&rule->arg.hlua_rule->args[i]);
7679 ha_free(&rule->arg.hlua_rule->args);
7680 }
7681 ha_free(&rule->arg.hlua_rule);
7682 }
7683 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007684}
7685
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007686static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7687 struct act_rule *rule, char **err)
7688{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007689 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007690
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007691 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007692 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007693 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007694 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007695 * the call of this analyzer.
7696 */
7697 if (rule->from != ACT_F_HTTP_REQ) {
7698 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7699 return ACT_RET_PRS_ERR;
7700 }
7701
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007702 /* Memory for the rule. */
7703 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7704 if (!rule->arg.hlua_rule) {
7705 memprintf(err, "out of memory error");
7706 return ACT_RET_PRS_ERR;
7707 }
7708
7709 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007710 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007711
7712 /* TODO: later accept arguments. */
7713 rule->arg.hlua_rule->args = NULL;
7714
7715 /* Add applet pointer in the rule. */
7716 rule->applet.obj_type = OBJ_TYPE_APPLET;
7717 rule->applet.name = fcn->name;
7718 rule->applet.init = hlua_applet_http_init;
7719 rule->applet.fct = hlua_applet_http_fct;
7720 rule->applet.release = hlua_applet_http_release;
7721 rule->applet.timeout = hlua_timeout_applet;
7722
7723 return ACT_RET_PRS_OK;
7724}
7725
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007726/* This function is an LUA binding used for registering
7727 * "sample-conv" functions. It expects a converter name used
7728 * in the haproxy configuration file, and an LUA function.
7729 */
7730__LJMP static int hlua_register_action(lua_State *L)
7731{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007732 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007733 const char *name;
7734 int ref;
7735 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007736 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007737 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007738 struct buffer *trash;
7739 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007740
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007741 /* Initialise the number of expected arguments at 0. */
7742 nargs = 0;
7743
7744 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7745 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007746
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01007747 if (hlua_gethlua(L)) {
7748 /* runtime processing */
7749 WILL_LJMP(luaL_error(L, "register_action: not available outside of body context"));
7750 }
7751
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007752 /* First argument : converter name. */
7753 name = MAY_LJMP(luaL_checkstring(L, 1));
7754
7755 /* Second argument : environment. */
7756 if (lua_type(L, 2) != LUA_TTABLE)
7757 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7758
7759 /* Third argument : lua function. */
7760 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7761
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007762 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007763 if (lua_gettop(L) >= 4)
7764 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7765
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007766 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007767 lua_pushnil(L);
7768 while (lua_next(L, 2) != 0) {
7769 if (lua_type(L, -1) != LUA_TSTRING)
7770 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7771
Thierry Fournierf67442e2020-11-28 20:41:07 +01007772 /* Check if action exists */
7773 trash = get_trash_chunk();
7774 chunk_printf(trash, "lua.%s", name);
7775 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7776 akw = tcp_req_cont_action(trash->area);
7777 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7778 akw = tcp_res_cont_action(trash->area);
7779 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7780 akw = action_http_req_custom(trash->area);
7781 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7782 akw = action_http_res_custom(trash->area);
7783 } else {
7784 akw = NULL;
7785 }
7786 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007787 fcn = akw->private;
7788 if (fcn->function_ref[hlua_state_id] != -1) {
7789 ha_warning("Trying to register action 'lua.%s' more than once. "
7790 "This will become a hard error in version 2.5.\n", name);
7791 }
7792 fcn->function_ref[hlua_state_id] = ref;
7793
7794 /* pop the environment string. */
7795 lua_pop(L, 1);
7796 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007797 }
7798
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007799 /* Check required environment. Only accepted "http" or "tcp". */
7800 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007801 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007802 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007803 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007804 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007805 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007806 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007807
7808 /* Fill fcn. */
7809 fcn->name = strdup(name);
7810 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007811 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007812 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007813
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007814 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007815 fcn->nargs = nargs;
7816
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007817 /* List head */
7818 akl->list.n = akl->list.p = NULL;
7819
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007820 /* action keyword. */
7821 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007822 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007823 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007824 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007825
7826 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7827
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007828 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007829 akl->kw[0].private = fcn;
7830 akl->kw[0].parse = action_register_lua;
7831
7832 /* select the action registering point. */
7833 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7834 tcp_req_cont_keywords_register(akl);
7835 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7836 tcp_res_cont_keywords_register(akl);
7837 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7838 http_req_keywords_register(akl);
7839 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7840 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007841 else {
7842 release_hlua_function(fcn);
7843 if (akl)
7844 ha_free((char **)&(akl->kw[0].kw));
7845 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007846 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007847 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7848 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007849 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007850
7851 /* pop the environment string. */
7852 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007853
7854 /* reset for next loop */
7855 akl = NULL;
7856 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007857 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007858 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007859
7860 alloc_error:
7861 release_hlua_function(fcn);
7862 ha_free(&akl);
7863 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7864 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007865}
7866
7867static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7868 struct act_rule *rule, char **err)
7869{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007870 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007871
Christopher Faulet280f85b2019-07-15 15:02:04 +02007872 if (px->mode == PR_MODE_HTTP) {
7873 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007874 return ACT_RET_PRS_ERR;
7875 }
7876
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007877 /* Memory for the rule. */
7878 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7879 if (!rule->arg.hlua_rule) {
7880 memprintf(err, "out of memory error");
7881 return ACT_RET_PRS_ERR;
7882 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007883
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007884 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007885 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007886
7887 /* TODO: later accept arguments. */
7888 rule->arg.hlua_rule->args = NULL;
7889
7890 /* Add applet pointer in the rule. */
7891 rule->applet.obj_type = OBJ_TYPE_APPLET;
7892 rule->applet.name = fcn->name;
7893 rule->applet.init = hlua_applet_tcp_init;
7894 rule->applet.fct = hlua_applet_tcp_fct;
7895 rule->applet.release = hlua_applet_tcp_release;
7896 rule->applet.timeout = hlua_timeout_applet;
7897
7898 return 0;
7899}
7900
7901/* This function is an LUA binding used for registering
7902 * "sample-conv" functions. It expects a converter name used
7903 * in the haproxy configuration file, and an LUA function.
7904 */
7905__LJMP static int hlua_register_service(lua_State *L)
7906{
7907 struct action_kw_list *akl;
7908 const char *name;
7909 const char *env;
7910 int ref;
7911 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007912 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007913 struct buffer *trash;
7914 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007915
7916 MAY_LJMP(check_args(L, 3, "register_service"));
7917
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01007918 if (hlua_gethlua(L)) {
7919 /* runtime processing */
7920 WILL_LJMP(luaL_error(L, "register_service: not available outside of body context"));
7921 }
7922
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007923 /* First argument : converter name. */
7924 name = MAY_LJMP(luaL_checkstring(L, 1));
7925
7926 /* Second argument : environment. */
7927 env = MAY_LJMP(luaL_checkstring(L, 2));
7928
7929 /* Third argument : lua function. */
7930 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7931
Thierry Fournierf67442e2020-11-28 20:41:07 +01007932 /* Check for service already registered */
7933 trash = get_trash_chunk();
7934 chunk_printf(trash, "lua.%s", name);
7935 akw = service_find(trash->area);
7936 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007937 fcn = akw->private;
7938 if (fcn->function_ref[hlua_state_id] != -1) {
7939 ha_warning("Trying to register service 'lua.%s' more than once. "
7940 "This will become a hard error in version 2.5.\n", name);
7941 }
7942 fcn->function_ref[hlua_state_id] = ref;
7943 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007944 }
7945
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007946 /* Allocate and fill the sample fetch keyword struct. */
7947 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7948 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007949 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007950 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007951 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007952 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007953
7954 /* Fill fcn. */
7955 len = strlen("<lua.>") + strlen(name) + 1;
7956 fcn->name = calloc(1, len);
7957 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007958 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007959 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007960 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007961
7962 /* List head */
7963 akl->list.n = akl->list.p = NULL;
7964
7965 /* converter keyword. */
7966 len = strlen("lua.") + strlen(name) + 1;
7967 akl->kw[0].kw = calloc(1, len);
7968 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007969 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007970
7971 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7972
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007973 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007974 if (strcmp(env, "tcp") == 0)
7975 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007976 else if (strcmp(env, "http") == 0)
7977 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007978 else {
7979 release_hlua_function(fcn);
7980 if (akl)
7981 ha_free((char **)&(akl->kw[0].kw));
7982 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007983 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007984 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02007985 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007986
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007987 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007988 akl->kw[0].private = fcn;
7989
7990 /* End of array. */
7991 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7992
7993 /* Register this new converter */
7994 service_keywords_register(akl);
7995
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007996 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007997
7998 alloc_error:
7999 release_hlua_function(fcn);
8000 ha_free(&akl);
8001 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8002 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008003}
8004
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008005/* This function initialises Lua cli handler. It copies the
8006 * arguments in the Lua stack and create channel IO objects.
8007 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02008008static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008009{
8010 struct hlua *hlua;
8011 struct hlua_function *fcn;
8012 int i;
8013 const char *error;
8014
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008015 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008016 appctx->ctx.hlua_cli.fcn = private;
8017
Willy Tarreaubafbe012017-11-24 17:34:44 +01008018 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008019 if (!hlua) {
8020 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008021 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008022 }
8023 HLUA_INIT(hlua);
8024 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008025
8026 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05008027 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008028 * applet_http. It is absolutely compatible.
8029 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01008030 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008031 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01008032 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008033 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008034 }
8035 appctx->ctx.hlua_cli.task->nice = 0;
8036 appctx->ctx.hlua_cli.task->context = appctx;
8037 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
8038
8039 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01008040 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 +01008041 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008042 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008043 }
8044
8045 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008046 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008047 if (lua_type(hlua->T, -1) == LUA_TSTRING)
8048 error = lua_tostring(hlua->T, -1);
8049 else
8050 error = "critical error";
8051 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
8052 goto error;
8053 }
8054
8055 /* Check stack available size. */
8056 if (!lua_checkstack(hlua->T, 2)) {
8057 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8058 goto error;
8059 }
8060
8061 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008062 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008063
8064 /* Once the arguments parsed, the CLI is like an AppletTCP,
8065 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008066 */
8067 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
8068 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8069 goto error;
8070 }
8071 hlua->nargs = 1;
8072
8073 /* push keywords in the stack. */
8074 for (i = 0; *args[i]; i++) {
8075 /* Check stack available size. */
8076 if (!lua_checkstack(hlua->T, 1)) {
8077 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8078 goto error;
8079 }
8080 lua_pushstring(hlua->T, args[i]);
8081 hlua->nargs++;
8082 }
8083
8084 /* We must initialize the execution timeouts. */
8085 hlua->max_time = hlua_timeout_session;
8086
8087 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008088 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008089
8090 /* It's ok */
8091 return 0;
8092
8093 /* It's not ok. */
8094error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008095 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008096 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008097 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008098 return 1;
8099}
8100
8101static int hlua_cli_io_handler_fct(struct appctx *appctx)
8102{
8103 struct hlua *hlua;
8104 struct stream_interface *si;
8105 struct hlua_function *fcn;
8106
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008107 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008108 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008109 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008110
8111 /* If the stream is disconnect or closed, ldo nothing. */
8112 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8113 return 1;
8114
8115 /* Execute the function. */
8116 switch (hlua_ctx_resume(hlua, 1)) {
8117
8118 /* finished. */
8119 case HLUA_E_OK:
8120 return 1;
8121
8122 /* yield. */
8123 case HLUA_E_AGAIN:
8124 /* We want write. */
8125 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008126 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008127 /* Set the timeout. */
8128 if (hlua->wake_time != TICK_ETERNITY)
8129 task_schedule(hlua->task, hlua->wake_time);
8130 return 0;
8131
8132 /* finished with error. */
8133 case HLUA_E_ERRMSG:
8134 /* Display log. */
8135 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8136 fcn->name, lua_tostring(hlua->T, -1));
8137 lua_pop(hlua->T, 1);
8138 return 1;
8139
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008140 case HLUA_E_ETMOUT:
8141 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8142 fcn->name);
8143 return 1;
8144
8145 case HLUA_E_NOMEM:
8146 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8147 fcn->name);
8148 return 1;
8149
8150 case HLUA_E_YIELD: /* unexpected */
8151 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8152 fcn->name);
8153 return 1;
8154
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008155 case HLUA_E_ERR:
8156 /* Display log. */
8157 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8158 fcn->name);
8159 return 1;
8160
8161 default:
8162 return 1;
8163 }
8164
8165 return 1;
8166}
8167
8168static void hlua_cli_io_release_fct(struct appctx *appctx)
8169{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008170 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008171 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008172}
8173
8174/* This function is an LUA binding used for registering
8175 * new keywords in the cli. It expects a list of keywords
8176 * which are the "path". It is limited to 5 keywords. A
8177 * description of the command, a function to be executed
8178 * for the parsing and a function for io handlers.
8179 */
8180__LJMP static int hlua_register_cli(lua_State *L)
8181{
8182 struct cli_kw_list *cli_kws;
8183 const char *message;
8184 int ref_io;
8185 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008186 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008187 int index;
8188 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008189 struct buffer *trash;
8190 const char *kw[5];
8191 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008192 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008193
8194 MAY_LJMP(check_args(L, 3, "register_cli"));
8195
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01008196 if (hlua_gethlua(L)) {
8197 /* runtime processing */
8198 WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context"));
8199 }
8200
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008201 /* First argument : an array of maximum 5 keywords. */
8202 if (!lua_istable(L, 1))
8203 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8204
8205 /* Second argument : string with contextual message. */
8206 message = MAY_LJMP(luaL_checkstring(L, 2));
8207
8208 /* Third and fourth argument : lua function. */
8209 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8210
Thierry Fournierf67442e2020-11-28 20:41:07 +01008211 /* Check for CLI service already registered */
8212 trash = get_trash_chunk();
8213 index = 0;
8214 lua_pushnil(L);
8215 memset(kw, 0, sizeof(kw));
8216 while (lua_next(L, 1) != 0) {
8217 if (index >= CLI_PREFIX_KW_NB)
8218 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8219 if (lua_type(L, -1) != LUA_TSTRING)
8220 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8221 kw[index] = lua_tostring(L, -1);
8222 if (index == 0)
8223 chunk_printf(trash, "%s", kw[index]);
8224 else
8225 chunk_appendf(trash, " %s", kw[index]);
8226 index++;
8227 lua_pop(L, 1);
8228 }
8229 cli_kw = cli_find_kw_exact((char **)kw);
8230 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008231 fcn = cli_kw->private;
8232 if (fcn->function_ref[hlua_state_id] != -1) {
8233 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8234 "This will become a hard error in version 2.5.\n", trash->area);
8235 }
8236 fcn->function_ref[hlua_state_id] = ref_io;
8237 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008238 }
8239
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008240 /* Allocate and fill the sample fetch keyword struct. */
8241 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008242 if (!cli_kws) {
8243 errmsg = "Lua out of memory error.";
8244 goto error;
8245 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008246 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008247 if (!fcn) {
8248 errmsg = "Lua out of memory error.";
8249 goto error;
8250 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008251
8252 /* Fill path. */
8253 index = 0;
8254 lua_pushnil(L);
8255 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008256 if (index >= 5) {
8257 errmsg = "1st argument must be a table with a maximum of 5 entries";
8258 goto error;
8259 }
8260 if (lua_type(L, -1) != LUA_TSTRING) {
8261 errmsg = "1st argument must be a table filled with strings";
8262 goto error;
8263 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008264 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008265 if (!cli_kws->kw[0].str_kw[index]) {
8266 errmsg = "Lua out of memory error.";
8267 goto error;
8268 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008269 index++;
8270 lua_pop(L, 1);
8271 }
8272
8273 /* Copy help message. */
8274 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008275 if (!cli_kws->kw[0].usage) {
8276 errmsg = "Lua out of memory error.";
8277 goto error;
8278 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008279
8280 /* Fill fcn io handler. */
8281 len = strlen("<lua.cli>") + 1;
8282 for (i = 0; i < index; i++)
8283 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8284 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008285 if (!fcn->name) {
8286 errmsg = "Lua out of memory error.";
8287 goto error;
8288 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008289 strncat((char *)fcn->name, "<lua.cli", len);
8290 for (i = 0; i < index; i++) {
8291 strncat((char *)fcn->name, ".", len);
8292 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8293 }
8294 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008295 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008296
8297 /* Fill last entries. */
8298 cli_kws->kw[0].private = fcn;
8299 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8300 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8301 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8302
8303 /* Register this new converter */
8304 cli_register_kw(cli_kws);
8305
8306 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008307
8308 error:
8309 release_hlua_function(fcn);
8310 if (cli_kws) {
8311 for (i = 0; i < index; i++)
8312 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8313 ha_free((char **)&(cli_kws->kw[0].usage));
8314 }
8315 ha_free(&cli_kws);
8316 WILL_LJMP(luaL_error(L, errmsg));
8317 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008318}
8319
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008320static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008321 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008322 char **err, unsigned int *timeout)
8323{
8324 const char *error;
8325
8326 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008327 if (error == PARSE_TIME_OVER) {
8328 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8329 args[1], args[0]);
8330 return -1;
8331 }
8332 else if (error == PARSE_TIME_UNDER) {
8333 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8334 args[1], args[0]);
8335 return -1;
8336 }
8337 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008338 memprintf(err, "%s: invalid timeout", args[0]);
8339 return -1;
8340 }
8341 return 0;
8342}
8343
8344static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008345 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008346 char **err)
8347{
8348 return hlua_read_timeout(args, section_type, curpx, defpx,
8349 file, line, err, &hlua_timeout_session);
8350}
8351
8352static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008353 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008354 char **err)
8355{
8356 return hlua_read_timeout(args, section_type, curpx, defpx,
8357 file, line, err, &hlua_timeout_task);
8358}
8359
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008360static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008361 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008362 char **err)
8363{
8364 return hlua_read_timeout(args, section_type, curpx, defpx,
8365 file, line, err, &hlua_timeout_applet);
8366}
8367
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008368static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008369 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008370 char **err)
8371{
8372 char *error;
8373
8374 hlua_nb_instruction = strtoll(args[1], &error, 10);
8375 if (*error != '\0') {
8376 memprintf(err, "%s: invalid number", args[0]);
8377 return -1;
8378 }
8379 return 0;
8380}
8381
Willy Tarreau32f61e22015-03-18 17:54:59 +01008382static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008383 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008384 char **err)
8385{
8386 char *error;
8387
8388 if (*(args[1]) == 0) {
8389 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8390 return -1;
8391 }
8392 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8393 if (*error != '\0') {
8394 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8395 return -1;
8396 }
8397 return 0;
8398}
8399
8400
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008401/* This function is called by the main configuration key "lua-load". It loads and
8402 * execute an lua file during the parsing of the HAProxy configuration file. It is
8403 * the main lua entry point.
8404 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008405 * This function runs with the HAProxy keywords API. It returns -1 if an error
8406 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008407 *
8408 * In some error case, LUA set an error message in top of the stack. This function
8409 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008410 *
8411 * This function can fail with an abort() due to an Lua critical error.
8412 * We are in the configuration parsing process of HAProxy, this abort() is
8413 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008414 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008415static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008416{
8417 int error;
8418
8419 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008420 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008421 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008422 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8423 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008424 return -1;
8425 }
8426
8427 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008428 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008429 switch (error) {
8430 case LUA_OK:
8431 break;
8432 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008433 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8434 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008435 return -1;
8436 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008437 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008438 return -1;
8439 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008440 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8441 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008442 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008443#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008444 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008445 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8446 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008447 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008448#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008449 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008450 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8451 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008452 return -1;
8453 }
8454
8455 return 0;
8456}
8457
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008458static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008459 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008460 char **err)
8461{
8462 if (*(args[1]) == 0) {
8463 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8464 return -1;
8465 }
8466
Thierry Fournier59f11be2020-11-29 00:37:41 +01008467 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008468 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008469 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008470 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008471}
8472
Thierry Fournier59f11be2020-11-29 00:37:41 +01008473static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008474 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008475 char **err)
8476{
8477 int len;
8478
8479 if (*(args[1]) == 0) {
8480 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8481 return -1;
8482 }
8483
8484 if (per_thread_load == NULL) {
8485 /* allocate the first entry large enough to store the final NULL */
8486 per_thread_load = calloc(1, sizeof(*per_thread_load));
8487 if (per_thread_load == NULL) {
8488 memprintf(err, "out of memory error");
8489 return -1;
8490 }
8491 }
8492
8493 /* count used entries */
8494 for (len = 0; per_thread_load[len] != NULL; len++)
8495 ;
8496
8497 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8498 if (per_thread_load == NULL) {
8499 memprintf(err, "out of memory error");
8500 return -1;
8501 }
8502
8503 per_thread_load[len] = strdup(args[1]);
8504 per_thread_load[len + 1] = NULL;
8505
8506 if (per_thread_load[len] == NULL) {
8507 memprintf(err, "out of memory error");
8508 return -1;
8509 }
8510
8511 /* loading for thread 1 only */
8512 hlua_state_id = 1;
8513 ha_set_tid(0);
8514 return hlua_load_state(args[1], hlua_states[1], err);
8515}
8516
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008517/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8518 * in the given <ctx>.
8519 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008520static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008521{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008522 lua_getglobal(L, "package"); /* push package variable */
8523 lua_pushstring(L, path); /* push given path */
8524 lua_pushstring(L, ";"); /* push semicolon */
8525 lua_getfield(L, -3, type); /* push old path */
8526 lua_concat(L, 3); /* concatenate to new path */
8527 lua_setfield(L, -2, type); /* store new path */
8528 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008529
8530 return 0;
8531}
8532
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008533static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008534 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008535 char **err)
8536{
8537 char *path;
8538 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008539 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008540
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008541 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008542 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008543 }
8544
8545 if (!(*args[1])) {
8546 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008547 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008548 }
8549 path = args[1];
8550
8551 if (*args[2]) {
8552 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8553 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008554 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008555 }
8556 type = args[2];
8557 }
8558
Thierry Fournier59f11be2020-11-29 00:37:41 +01008559 p = calloc(1, sizeof(*p));
8560 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008561 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008562 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008563 }
8564 p->path = strdup(path);
8565 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008566 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008567 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008568 }
8569 p->type = strdup(type);
8570 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008571 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008572 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008573 }
Willy Tarreau2b718102021-04-21 07:32:39 +02008574 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01008575
8576 hlua_prepend_path(hlua_states[0], type, path);
8577 hlua_prepend_path(hlua_states[1], type, path);
8578 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008579
8580err2:
8581 free(p->type);
8582 free(p->path);
8583err:
8584 free(p);
8585 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008586}
8587
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008588/* configuration keywords declaration */
8589static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008590 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008591 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008592 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008593 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8594 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008595 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008596 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008597 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008598 { 0, NULL, NULL },
8599}};
8600
Willy Tarreau0108d902018-11-25 19:14:37 +01008601INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8602
Christopher Fauletafd8f102018-11-08 11:34:21 +01008603
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008604/* This function can fail with an abort() due to an Lua critical error.
8605 * We are in the initialisation process of HAProxy, this abort() is
8606 * tolerated.
8607 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008608int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008609{
8610 struct hlua_init_function *init;
8611 const char *msg;
8612 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008613 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008614 const char *kind;
8615 const char *trace;
8616 int return_status = 1;
8617#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8618 int nres;
8619#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008620
Willy Tarreaucdb53462020-12-02 12:12:00 +01008621 /* disable memory limit checks if limit is not set */
8622 if (!hlua_global_allocator.limit)
8623 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8624
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008625 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008626 if (setjmp(safe_ljmp_env) != 0) {
8627 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008628 if (lua_type(L, -1) == LUA_TSTRING)
8629 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008630 else
8631 error = "critical error";
8632 fprintf(stderr, "Lua post-init: %s.\n", error);
8633 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008634 } else {
8635 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008636 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008637
Thierry Fournierb8cef172020-11-28 15:37:17 +01008638 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008639
Thierry Fournierc7492592020-11-28 23:57:24 +01008640 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008641 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Aurelien DARRAGON93591b42023-03-20 16:29:55 +01008642 /* function ref should be released right away since it was pushed
8643 * on the stack and will not be used anymore
8644 */
8645 hlua_unref(L, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008646
8647#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008648 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008649#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008650 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008651#endif
8652 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008653 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008654
8655 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008656 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008657 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008658
8659 case LUA_ERRERR:
8660 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008661 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008662 case LUA_ERRRUN:
8663 if (!kind)
8664 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008665 msg = lua_tostring(L, -1);
8666 lua_settop(L, 0); /* Empty the stack. */
8667 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008668 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008669 if (msg)
8670 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8671 else
8672 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8673 return_status = 0;
8674 break;
8675
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008676 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008677 /* Unknown error */
8678 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008679 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008680 case LUA_YIELD:
8681 /* yield is not configured at this step, this state doesn't happen */
8682 if (!kind)
8683 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008684 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008685 case LUA_ERRMEM:
8686 if (!kind)
8687 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008688 lua_settop(L, 0);
8689 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008690 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008691 ha_alert("Lua init: %s: %s\n", kind, trace);
8692 return_status = 0;
8693 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008694 }
Thierry Fournier670db242020-11-28 10:49:59 +01008695 if (!return_status)
8696 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008697 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008698
8699 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008700 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008701}
8702
Thierry Fournierb8cef172020-11-28 15:37:17 +01008703int hlua_post_init()
8704{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008705 int ret;
8706 int i;
8707 int errors;
8708 char *err = NULL;
8709 struct hlua_function *fcn;
8710
Willy Tarreau42afc592021-08-30 09:35:18 +02008711#if defined(USE_OPENSSL)
Thierry Fournierb8cef172020-11-28 15:37:17 +01008712 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008713 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008714 int saved_used_backed = global.ssl_used_backend;
8715 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008716 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008717 global.ssl_used_backend = saved_used_backed;
8718 }
8719#endif
8720
Thierry Fournierc7492592020-11-28 23:57:24 +01008721 /* Perform post init of common thread */
8722 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008723 ha_set_tid(0);
8724 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8725 if (ret == 0)
8726 return 0;
8727
8728 /* init remaining lua states and load files */
8729 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8730
8731 /* set thread context */
8732 ha_set_tid(hlua_state_id - 1);
8733
8734 /* Init lua state */
8735 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8736
8737 /* Load lua files */
8738 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8739 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8740 if (ret != 0) {
8741 ha_alert("Lua init: %s\n", err);
8742 return 0;
8743 }
8744 }
8745 }
8746
8747 /* Reset thread context */
8748 ha_set_tid(0);
8749
8750 /* Execute post init for all states */
8751 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8752
8753 /* set thread context */
8754 ha_set_tid(hlua_state_id - 1);
8755
8756 /* run post init */
8757 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8758 if (ret == 0)
8759 return 0;
8760 }
8761
8762 /* Reset thread context */
8763 ha_set_tid(0);
8764
8765 /* control functions registering. Each function must have:
8766 * - only the function_ref[0] set positive and all other to -1
8767 * - only the function_ref[0] set to -1 and all other positive
8768 * This ensure a same reference is not used both in shared
8769 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008770 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008771 * complicated to found for the end user.
8772 */
8773 errors = 0;
8774 list_for_each_entry(fcn, &referenced_functions, l) {
8775 ret = 0;
8776 for (i = 1; i < global.nbthread + 1; i++) {
8777 if (fcn->function_ref[i] == -1)
8778 ret--;
8779 else
8780 ret++;
8781 }
8782 if (abs(ret) != global.nbthread) {
8783 ha_alert("Lua function '%s' is not referenced in all thread. "
8784 "Expect function in all thread or in none thread.\n", fcn->name);
8785 errors++;
8786 continue;
8787 }
8788
8789 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008790 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8791 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008792 "exclusive.\n", fcn->name);
8793 errors++;
8794 }
8795 }
8796
8797 if (errors > 0)
8798 return 0;
8799
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008800 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008801 * -1 in order to have probably a segfault if someone use it
8802 */
8803 hlua_state_id = -1;
8804
8805 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008806}
8807
Willy Tarreau32f61e22015-03-18 17:54:59 +01008808/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8809 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8810 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008811 * allocation. <nsize> is the requested new size. A new allocation is
8812 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008813 * zero. This one verifies that the limits are respected but is optimized
8814 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreaue00ad052021-10-22 16:00:02 +02008815 *
8816 * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses
8817 * POSIX by making realloc(ptr,0) an effective free(), but others do not do
8818 * that and will simply allocate zero as if it were the result of malloc(0),
8819 * so mapping this onto realloc() will lead to memory leaks on non-glibc
8820 * systems.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008821 */
8822static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8823{
8824 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008825 size_t limit, old, new;
8826
Tim Duesterhus22586522021-01-08 10:35:33 +01008827 if (unlikely(!ptr && !nsize))
8828 return NULL;
8829
Willy Tarreaucdb53462020-12-02 12:12:00 +01008830 /* a limit of ~0 means unlimited and boot complete, so there's no need
8831 * for accounting anymore.
8832 */
Willy Tarreaue00ad052021-10-22 16:00:02 +02008833 if (likely(~zone->limit == 0)) {
8834 if (!nsize)
8835 ha_free(&ptr);
8836 else
8837 ptr = realloc(ptr, nsize);
8838 return ptr;
8839 }
Willy Tarreau32f61e22015-03-18 17:54:59 +01008840
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008841 if (!ptr)
8842 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008843
Willy Tarreaucdb53462020-12-02 12:12:00 +01008844 /* enforce strict limits across all threads */
8845 limit = zone->limit;
8846 old = _HA_ATOMIC_LOAD(&zone->allocated);
8847 do {
8848 new = old + nsize - osize;
8849 if (unlikely(nsize && limit && new > limit))
8850 return NULL;
8851 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008852
Willy Tarreaue00ad052021-10-22 16:00:02 +02008853 if (!nsize)
8854 ha_free(&ptr);
8855 else
8856 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008857
8858 if (unlikely(!ptr && nsize)) // failed
8859 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8860
8861 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008862 return ptr;
8863}
8864
Thierry Fournierecb83c22020-11-28 15:49:44 +01008865/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008866 * We are in the initialisation process of HAProxy, this abort() is
8867 * tolerated.
8868 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008869lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008870{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008871 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008872 int idx;
8873 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008874 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008875 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008876 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008877 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008878 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008879 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008880
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008881 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008882 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008883
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008884 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008885 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008886 *context = NULL;
8887
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008888 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008889 * the Lua function can fail with an abort. We are in the initialisation
8890 * process of HAProxy, this abort() is tolerated.
8891 */
8892
Thierry Fournier3c539322020-11-28 16:05:05 +01008893 /* Call post initialisation function in safe environment. */
8894 if (setjmp(safe_ljmp_env) != 0) {
8895 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008896 if (lua_type(L, -1) == LUA_TSTRING)
8897 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008898 else
8899 error_msg = "critical error";
8900 fprintf(stderr, "Lua init: %s.\n", error_msg);
8901 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008902 } else {
8903 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008904 }
8905
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008906 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008907 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008908#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8909#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8910#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008911 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008912#endif
8913#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008914 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008915#endif
8916#undef HLUA_PREPEND_PATH_TOSTRING
8917#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008918
Thierry Fournier59f11be2020-11-29 00:37:41 +01008919 /* Apply configured prepend path */
8920 list_for_each_entry(pp, &prepend_path_list, l)
8921 hlua_prepend_path(L, pp->type, pp->path);
8922
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008923 /*
8924 *
8925 * Create "core" object.
8926 *
8927 */
8928
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008929 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008930 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008931
Thierry Fournierecb83c22020-11-28 15:49:44 +01008932 /* set the thread id */
8933 hlua_class_const_int(L, "thread", thread_num);
8934
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008935 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008936 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008937 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008938
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008939 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008940 hlua_class_function(L, "register_init", hlua_register_init);
8941 hlua_class_function(L, "register_task", hlua_register_task);
8942 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8943 hlua_class_function(L, "register_converters", hlua_register_converters);
8944 hlua_class_function(L, "register_action", hlua_register_action);
8945 hlua_class_function(L, "register_service", hlua_register_service);
8946 hlua_class_function(L, "register_cli", hlua_register_cli);
8947 hlua_class_function(L, "yield", hlua_yield);
8948 hlua_class_function(L, "set_nice", hlua_set_nice);
8949 hlua_class_function(L, "sleep", hlua_sleep);
8950 hlua_class_function(L, "msleep", hlua_msleep);
8951 hlua_class_function(L, "add_acl", hlua_add_acl);
8952 hlua_class_function(L, "del_acl", hlua_del_acl);
8953 hlua_class_function(L, "set_map", hlua_set_map);
8954 hlua_class_function(L, "del_map", hlua_del_map);
8955 hlua_class_function(L, "tcp", hlua_socket_new);
8956 hlua_class_function(L, "log", hlua_log);
8957 hlua_class_function(L, "Debug", hlua_log_debug);
8958 hlua_class_function(L, "Info", hlua_log_info);
8959 hlua_class_function(L, "Warning", hlua_log_warning);
8960 hlua_class_function(L, "Alert", hlua_log_alert);
8961 hlua_class_function(L, "done", hlua_done);
8962 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008963
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008964 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008965
8966 /*
8967 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008968 * Create "act" object.
8969 *
8970 */
8971
8972 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008973 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008974
8975 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008976 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8977 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8978 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8979 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8980 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8981 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8982 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8983 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008984
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008985 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008986
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008987 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008988
8989 /*
8990 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008991 * Register class Map
8992 *
8993 */
8994
8995 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008996 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008997
8998 /* register pattern types. */
8999 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009000 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009001 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009002 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009003 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009004 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009005
9006 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009007 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009008
9009 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009010 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009011
Ilya Shipitsind4259502020-04-08 01:07:56 +05009012 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009013 lua_pushstring(L, "__index");
9014 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009015
9016 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009017 hlua_class_function(L, "lookup", hlua_map_lookup);
9018 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009019
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009020 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009021
Thierry Fournier45e78d72016-02-19 18:34:46 +01009022 /* Register previous table in the registry with reference and named entry.
9023 * The function hlua_register_metatable() pops the stack, so we
9024 * previously create a copy of the table.
9025 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009026 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
9027 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009028
9029 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009030 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009031
9032 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009033 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009034
9035 /*
9036 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009037 * Register class Channel
9038 *
9039 */
9040
9041 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009042 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009043
Ilya Shipitsind4259502020-04-08 01:07:56 +05009044 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009045 lua_pushstring(L, "__index");
9046 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009047
9048 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009049 hlua_class_function(L, "get", hlua_channel_get);
9050 hlua_class_function(L, "dup", hlua_channel_dup);
9051 hlua_class_function(L, "getline", hlua_channel_getline);
9052 hlua_class_function(L, "set", hlua_channel_set);
9053 hlua_class_function(L, "append", hlua_channel_append);
9054 hlua_class_function(L, "send", hlua_channel_send);
9055 hlua_class_function(L, "forward", hlua_channel_forward);
9056 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
9057 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
9058 hlua_class_function(L, "is_full", hlua_channel_is_full);
9059 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009060
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009061 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009062
9063 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009064 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009065
9066 /*
9067 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009068 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009069 *
9070 */
9071
9072 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009073 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009074
Ilya Shipitsind4259502020-04-08 01:07:56 +05009075 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009076 lua_pushstring(L, "__index");
9077 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009078
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009079 /* Browse existing fetches and create the associated
9080 * object method.
9081 */
9082 sf = NULL;
9083 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009084 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9085 * by an underscore.
9086 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009087 strlcpy2(trash.area, sf->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009088 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009089 if (*p == '.' || *p == '-' || *p == '+')
9090 *p = '_';
9091
9092 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009093 lua_pushstring(L, trash.area);
9094 lua_pushlightuserdata(L, sf);
9095 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
9096 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009097 }
9098
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009099 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009100
9101 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009102 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009103
9104 /*
9105 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009106 * Register class Converters
9107 *
9108 */
9109
9110 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009111 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009112
9113 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009114 lua_pushstring(L, "__index");
9115 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009116
9117 /* Browse existing converters and create the associated
9118 * object method.
9119 */
9120 sc = NULL;
9121 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009122 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9123 * by an underscore.
9124 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009125 strlcpy2(trash.area, sc->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009126 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009127 if (*p == '.' || *p == '-' || *p == '+')
9128 *p = '_';
9129
9130 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009131 lua_pushstring(L, trash.area);
9132 lua_pushlightuserdata(L, sc);
9133 lua_pushcclosure(L, hlua_run_sample_conv, 1);
9134 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009135 }
9136
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009137 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009138
9139 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009140 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009141
9142 /*
9143 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009144 * Register class HTTP
9145 *
9146 */
9147
9148 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009149 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009150
Ilya Shipitsind4259502020-04-08 01:07:56 +05009151 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009152 lua_pushstring(L, "__index");
9153 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009154
9155 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009156 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9157 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9158 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9159 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9160 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9161 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9162 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9163 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9164 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9165 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009166
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009167 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9168 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9169 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9170 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9171 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9172 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9173 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009174
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009175 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009176
9177 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009178 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009179
9180 /*
9181 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009182 * Register class AppletTCP
9183 *
9184 */
9185
9186 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009187 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009188
Ilya Shipitsind4259502020-04-08 01:07:56 +05009189 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009190 lua_pushstring(L, "__index");
9191 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009192
9193 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009194 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9195 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9196 hlua_class_function(L, "send", hlua_applet_tcp_send);
9197 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9198 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9199 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9200 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9201 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009202
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009203 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009204
9205 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009206 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009207
9208 /*
9209 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009210 * Register class AppletHTTP
9211 *
9212 */
9213
9214 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009215 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009216
Ilya Shipitsind4259502020-04-08 01:07:56 +05009217 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009218 lua_pushstring(L, "__index");
9219 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009220
9221 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009222 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9223 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9224 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9225 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9226 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9227 hlua_class_function(L, "getline", hlua_applet_http_getline);
9228 hlua_class_function(L, "receive", hlua_applet_http_recv);
9229 hlua_class_function(L, "send", hlua_applet_http_send);
9230 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9231 hlua_class_function(L, "set_status", hlua_applet_http_status);
9232 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009233
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009234 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009235
9236 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009237 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009238
9239 /*
9240 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009241 * Register class TXN
9242 *
9243 */
9244
9245 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009246 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009247
Ilya Shipitsind4259502020-04-08 01:07:56 +05009248 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009249 lua_pushstring(L, "__index");
9250 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009251
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009252 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009253 hlua_class_function(L, "set_priv", hlua_set_priv);
9254 hlua_class_function(L, "get_priv", hlua_get_priv);
9255 hlua_class_function(L, "set_var", hlua_set_var);
9256 hlua_class_function(L, "unset_var", hlua_unset_var);
9257 hlua_class_function(L, "get_var", hlua_get_var);
9258 hlua_class_function(L, "done", hlua_txn_done);
9259 hlua_class_function(L, "reply", hlua_txn_reply_new);
9260 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9261 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9262 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9263 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9264 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9265 hlua_class_function(L, "deflog", hlua_txn_deflog);
9266 hlua_class_function(L, "log", hlua_txn_log);
9267 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9268 hlua_class_function(L, "Info", hlua_txn_log_info);
9269 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9270 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009271
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009272 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009273
9274 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009275 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009276
9277 /*
9278 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009279 * Register class reply
9280 *
9281 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009282 lua_newtable(L);
9283 lua_pushstring(L, "__index");
9284 lua_newtable(L);
9285 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9286 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9287 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9288 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9289 lua_settable(L, -3); /* Sets the __index entry. */
9290 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009291
9292
9293 /*
9294 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009295 * Register class Socket
9296 *
9297 */
9298
9299 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009300 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009301
Ilya Shipitsind4259502020-04-08 01:07:56 +05009302 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009303 lua_pushstring(L, "__index");
9304 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009305
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009306#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009307 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009308#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009309 hlua_class_function(L, "connect", hlua_socket_connect);
9310 hlua_class_function(L, "send", hlua_socket_send);
9311 hlua_class_function(L, "receive", hlua_socket_receive);
9312 hlua_class_function(L, "close", hlua_socket_close);
9313 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9314 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9315 hlua_class_function(L, "setoption", hlua_socket_setoption);
9316 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009317
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009318 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009319
9320 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009321 lua_pushstring(L, "__gc");
9322 lua_pushcclosure(L, hlua_socket_gc, 0);
9323 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009324
9325 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009326 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009327
Thierry Fournieraafc7772020-12-04 11:47:47 +01009328 lua_atpanic(L, hlua_panic_safe);
9329
9330 return L;
9331}
9332
9333void hlua_init(void) {
9334 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009335 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009336#ifdef USE_OPENSSL
9337 struct srv_kw *kw;
9338 int tmp_error;
9339 char *error;
9340 char *args[] = { /* SSL client configuration. */
9341 "ssl",
9342 "verify",
9343 "none",
9344 NULL
9345 };
9346#endif
9347
9348 /* Init post init function list head */
9349 for (i = 0; i < MAX_THREADS + 1; i++)
9350 LIST_INIT(&hlua_init_functions[i]);
9351
9352 /* Init state for common/shared lua parts */
9353 hlua_state_id = 0;
9354 ha_set_tid(0);
9355 hlua_states[0] = hlua_init_state(0);
9356
9357 /* Init state 1 for thread 0. We have at least one thread. */
9358 hlua_state_id = 1;
9359 ha_set_tid(0);
9360 hlua_states[1] = hlua_init_state(1);
9361
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009362 /* Proxy and server configuration initialisation. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009363 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg);
9364 if (!socket_proxy) {
9365 fprintf(stderr, "Lua init: %s\n", errmsg);
9366 exit(1);
9367 }
9368 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009369
9370 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009371 socket_tcp = new_server(socket_proxy);
9372 if (!socket_tcp) {
9373 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9374 exit(1);
9375 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009376
9377#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009378 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009379 socket_ssl = new_server(socket_proxy);
9380 if (!socket_ssl) {
9381 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9382 exit(1);
9383 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009384
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009385 socket_ssl->use_ssl = 1;
9386 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009387
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009388 for (i = 0; args[i] != NULL; i++) {
9389 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009390 /*
9391 *
9392 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009393 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009394 * features like client certificates and ssl_verify.
9395 *
9396 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009397 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009398 if (tmp_error != 0) {
9399 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9400 abort(); /* This must be never arrives because the command line
9401 not editable by the user. */
9402 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009403 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009404 }
9405 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009406#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009407
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009408}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009409
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009410static void hlua_deinit()
9411{
Willy Tarreau186f3762020-12-04 11:48:12 +01009412 int thr;
9413
9414 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9415 if (hlua_states[thr])
9416 lua_close(hlua_states[thr]);
9417 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009418
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009419 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009420
Willy Tarreau0f143af2021-03-05 10:41:48 +01009421#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009422 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009423#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009424
9425 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009426}
9427
9428REGISTER_POST_DEINIT(hlua_deinit);
9429
Willy Tarreau80713382018-11-26 10:19:54 +01009430static void hlua_register_build_options(void)
9431{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009432 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009433
Willy Tarreaubb57d942016-12-21 19:04:56 +01009434 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9435 hap_register_build_opts(ptr, 1);
9436}
Willy Tarreau80713382018-11-26 10:19:54 +01009437
9438INITCALL0(STG_REGISTER, hlua_register_build_options);