blob: af64a02a09a70efd7d5c3699a6d46b0fd26c7e48 [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
354 * error if the rgument is not a "function".
355 */
356__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
357{
358 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100359 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100360 WILL_LJMP(luaL_argerror(L, argno, msg));
361 }
362 lua_pushvalue(L, argno);
363 return luaL_ref(L, LUA_REGISTRYINDEX);
364}
365
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200366/* Return the string that is of the top of the stack. */
367const char *hlua_get_top_error_string(lua_State *L)
368{
369 if (lua_gettop(L) < 1)
370 return "unknown error";
371 if (lua_type(L, -1) != LUA_TSTRING)
372 return "unknown error";
373 return lua_tostring(L, -1);
374}
375
Christopher Fauletd09cc512021-03-24 14:48:45 +0100376__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200377{
378 lua_Debug ar;
379 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200380 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200381
382 while (lua_getstack(L, level++, &ar)) {
383
384 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100385 if (b_data(msg))
386 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200387
388 /* Fill fields:
389 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
390 * 'l': fills in the field currentline;
391 * 'n': fills in the field name and namewhat;
392 * 't': fills in the field istailcall;
393 */
394 lua_getinfo(L, "Slnt", &ar);
395
396 /* Append code localisation */
397 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100398 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200399 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100400 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200401
402 /*
403 * Get function name
404 *
405 * if namewhat is no empty, name is defined.
406 * what contains "Lua" for Lua function, "C" for C function,
407 * or "main" for main code.
408 */
409 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100410 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200411
412 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100413 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200414
415 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100416 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200417
418 else /* nothing left... */
419 chunk_appendf(msg, "?");
420
421
422 /* Display tailed call */
423 if (ar.istailcall)
424 chunk_appendf(msg, " ...");
425 }
426
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200427 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200428}
429
430
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100431/* This function check the number of arguments available in the
432 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500433 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100434 */
435__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
436{
437 if (lua_gettop(L) == nb)
438 return;
439 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
440}
441
Mark Lakes22154b42018-01-29 14:38:40 -0800442/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100443 * and the line number where the error is encountered.
444 */
445static int hlua_pusherror(lua_State *L, const char *fmt, ...)
446{
447 va_list argp;
448 va_start(argp, fmt);
449 luaL_where(L, 1);
450 lua_pushvfstring(L, fmt, argp);
451 va_end(argp);
452 lua_concat(L, 2);
453 return 1;
454}
455
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456/* This functions is used with sample fetch and converters. It
457 * converts the HAProxy configuration argument in a lua stack
458 * values.
459 *
460 * It takes an array of "arg", and each entry of the array is
461 * converted and pushed in the LUA stack.
462 */
463static int hlua_arg2lua(lua_State *L, const struct arg *arg)
464{
465 switch (arg->type) {
466 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100467 case ARGT_TIME:
468 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100469 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100470 break;
471
472 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100474 break;
475
476 case ARGT_IPV4:
477 case ARGT_IPV6:
478 case ARGT_MSK4:
479 case ARGT_MSK6:
480 case ARGT_FE:
481 case ARGT_BE:
482 case ARGT_TAB:
483 case ARGT_SRV:
484 case ARGT_USR:
485 case ARGT_MAP:
486 default:
487 lua_pushnil(L);
488 break;
489 }
490 return 1;
491}
492
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500493/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100494 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500495 * with sample fetch wrappers. The input arguments are given to the
496 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100497 */
498static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
499{
500 switch (lua_type(L, ud)) {
501
502 case LUA_TNUMBER:
503 case LUA_TBOOLEAN:
504 arg->type = ARGT_SINT;
505 arg->data.sint = lua_tointeger(L, ud);
506 break;
507
508 case LUA_TSTRING:
509 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200510 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200511 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200512 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200513 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100514 break;
515
516 case LUA_TUSERDATA:
517 case LUA_TNIL:
518 case LUA_TTABLE:
519 case LUA_TFUNCTION:
520 case LUA_TTHREAD:
521 case LUA_TLIGHTUSERDATA:
522 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200523 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100524 break;
525 }
526 return 1;
527}
528
529/* the following functions are used to convert a struct sample
530 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500531 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100532 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100533static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100534{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200535 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100536 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100537 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200538 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100539 break;
540
541 case SMP_T_BIN:
542 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200543 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100544 break;
545
546 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200547 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100548 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
549 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
550 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
551 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
552 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
553 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
554 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
555 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
556 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200557 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100558 break;
559 default:
560 lua_pushnil(L);
561 break;
562 }
563 break;
564
565 case SMP_T_IPV4:
566 case SMP_T_IPV6:
567 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200568 if (sample_casts[smp->data.type][SMP_T_STR] &&
569 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200570 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100571 else
572 lua_pushnil(L);
573 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100574 default:
575 lua_pushnil(L);
576 break;
577 }
578 return 1;
579}
580
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100581/* the following functions are used to convert a struct sample
582 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500583 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100584 */
585static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
586{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200587 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100588
589 case SMP_T_BIN:
590 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200591 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100592 break;
593
594 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200595 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100596 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
597 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
598 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
599 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
600 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
601 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
602 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
603 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
604 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200605 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100606 break;
607 default:
608 lua_pushstring(L, "");
609 break;
610 }
611 break;
612
613 case SMP_T_SINT:
614 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100615 case SMP_T_IPV4:
616 case SMP_T_IPV6:
617 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200618 if (sample_casts[smp->data.type][SMP_T_STR] &&
619 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200620 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100621 else
622 lua_pushstring(L, "");
623 break;
624 default:
625 lua_pushstring(L, "");
626 break;
627 }
628 return 1;
629}
630
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100631/* the following functions are used to convert an Lua type in a
632 * struct sample. This is useful to provide data from a converter
633 * to the LUA code.
634 */
635static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
636{
637 switch (lua_type(L, ud)) {
638
639 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200640 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200641 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100642 break;
643
644
645 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200646 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200647 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100648 break;
649
650 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200651 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100652 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200653 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200654 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200655 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200656 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100657 break;
658
659 case LUA_TUSERDATA:
660 case LUA_TNIL:
661 case LUA_TTABLE:
662 case LUA_TFUNCTION:
663 case LUA_TTHREAD:
664 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200665 case LUA_TNONE:
666 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200667 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200668 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100669 break;
670 }
671 return 1;
672}
673
Ilya Shipitsind4259502020-04-08 01:07:56 +0500674/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800675 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100676 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100677 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500678 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100679 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100680 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100682 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100683{
684 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200685 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100686 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200687 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200688 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200689 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200690 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100691
692 idx = 0;
693 min_arg = ARGM(mask);
694 mask >>= ARGM_BITS;
695
696 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200697 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100698
699 /* Check oversize. */
700 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200701 msg = "Malformed argument mask";
702 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100703 }
704
705 /* Check for mandatory arguments. */
706 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100707 if (idx < min_arg) {
708
709 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200710 if (idx > 0) {
711 msg = "Mandatory argument expected";
712 goto error;
713 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100714
715 /* If first argument have a certain type, some default values
716 * may be used. See the function smp_resolve_args().
717 */
718 switch (mask & ARGT_MASK) {
719
720 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200721 if (!(p->cap & PR_CAP_FE)) {
722 msg = "Mandatory argument expected";
723 goto error;
724 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100725 argp[idx].data.prx = p;
726 argp[idx].type = ARGT_FE;
727 argp[idx+1].type = ARGT_STOP;
728 break;
729
730 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200731 if (!(p->cap & PR_CAP_BE)) {
732 msg = "Mandatory argument expected";
733 goto error;
734 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100735 argp[idx].data.prx = p;
736 argp[idx].type = ARGT_BE;
737 argp[idx+1].type = ARGT_STOP;
738 break;
739
740 case ARGT_TAB:
Olivier Houcharda2658272022-09-13 00:35:53 +0200741 if (!p->table) {
742 msg = "Mandatory argument expected";
743 goto error;
744 }
745 argp[idx].data.t = p->table;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100746 argp[idx].type = ARGT_TAB;
747 argp[idx+1].type = ARGT_STOP;
748 break;
749
750 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200751 msg = "Mandatory argument expected";
752 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100753 break;
754 }
755 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200756 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100757 }
758
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500759 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100760 if ((mask & ARGT_MASK) == ARGT_STOP &&
761 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200762 msg = "Last argument expected";
763 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100764 }
765
766 if ((mask & ARGT_MASK) == ARGT_STOP &&
767 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200768 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100769 }
770
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200771 /* Convert some argument types. All string in argp[] are for not
772 * duplicated yet.
773 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100774 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100775 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200776 if (argp[idx].type != ARGT_SINT) {
777 msg = "integer expected";
778 goto error;
779 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100780 argp[idx].type = ARGT_SINT;
781 break;
782
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200784 if (argp[idx].type != ARGT_SINT) {
785 msg = "integer expected";
786 goto error;
787 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200788 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100789 break;
790
791 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200792 if (argp[idx].type != ARGT_SINT) {
793 msg = "integer expected";
794 goto error;
795 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200796 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100797 break;
798
799 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200800 if (argp[idx].type != ARGT_STR) {
801 msg = "string expected";
802 goto error;
803 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200804 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200805 if (!argp[idx].data.prx) {
806 msg = "frontend doesn't exist";
807 goto error;
808 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100809 argp[idx].type = ARGT_FE;
810 break;
811
812 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200813 if (argp[idx].type != ARGT_STR) {
814 msg = "string expected";
815 goto error;
816 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200817 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200818 if (!argp[idx].data.prx) {
819 msg = "backend doesn't exist";
820 goto error;
821 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100822 argp[idx].type = ARGT_BE;
823 break;
824
825 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200826 if (argp[idx].type != ARGT_STR) {
827 msg = "string expected";
828 goto error;
829 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200830 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200831 if (!argp[idx].data.t) {
832 msg = "table doesn't exist";
833 goto error;
834 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100835 argp[idx].type = ARGT_TAB;
836 break;
837
838 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200839 if (argp[idx].type != ARGT_STR) {
840 msg = "string expected";
841 goto error;
842 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200843 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100844 if (sname) {
845 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200846 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200847 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200848 if (!px) {
849 msg = "backend doesn't exist";
850 goto error;
851 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100852 }
853 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200854 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100855 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100856 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100857 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200858 if (!argp[idx].data.srv) {
859 msg = "server doesn't exist";
860 goto error;
861 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100862 argp[idx].type = ARGT_SRV;
863 break;
864
865 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200866 if (argp[idx].type != ARGT_STR) {
867 msg = "string expected";
868 goto error;
869 }
870 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
871 msg = "invalid IPv4 address";
872 goto error;
873 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100874 argp[idx].type = ARGT_IPV4;
875 break;
876
877 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200878 if (argp[idx].type == ARGT_SINT)
879 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
880 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200881 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
882 msg = "invalid IPv4 mask";
883 goto error;
884 }
885 }
886 else {
887 msg = "integer or string expected";
888 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200889 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100890 argp[idx].type = ARGT_MSK4;
891 break;
892
893 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200894 if (argp[idx].type != ARGT_STR) {
895 msg = "string expected";
896 goto error;
897 }
898 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
899 msg = "invalid IPv6 address";
900 goto error;
901 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100902 argp[idx].type = ARGT_IPV6;
903 break;
904
905 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200906 if (argp[idx].type == ARGT_SINT)
907 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
908 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200909 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
910 msg = "invalid IPv6 mask";
911 goto error;
912 }
913 }
914 else {
915 msg = "integer or string expected";
916 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200917 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100918 argp[idx].type = ARGT_MSK6;
919 break;
920
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200921 case ARGT_REG:
922 if (argp[idx].type != ARGT_STR) {
923 msg = "string expected";
924 goto error;
925 }
926 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
927 if (!reg) {
928 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
929 argp[idx].data.str.area, err);
930 free(err);
931 goto error;
932 }
933 argp[idx].type = ARGT_REG;
934 argp[idx].data.reg = reg;
935 break;
936
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100937 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200938 if (argp[idx].type != ARGT_STR) {
939 msg = "string expected";
940 goto error;
941 }
942 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100943 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200944 ul = p->uri_auth->userlist;
945 else
946 ul = auth_find_userlist(argp[idx].data.str.area);
947
948 if (!ul) {
949 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
950 goto error;
951 }
952 argp[idx].type = ARGT_USR;
953 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200954 break;
955
956 case ARGT_STR:
957 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
958 msg = "unable to duplicate string arg";
959 goto error;
960 }
961 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100962 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200963
Christopher Fauletd25d9262020-08-06 11:04:46 +0200964 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200965 msg = "type not yet supported";
966 goto error;
967 break;
968
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100969 }
970
971 /* Check for type of argument. */
972 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200973 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
974 arg_type_names[(mask & ARGT_MASK)],
975 arg_type_names[argp[idx].type & ARGT_MASK]);
976 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100977 }
978
979 /* Next argument. */
980 mask >>= ARGT_BITS;
981 idx++;
982 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200983 return 0;
984
985 error:
Olivier Houchardda25dac2022-09-13 00:31:17 +0200986 argp[idx].type = ARGT_STOP;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200987 for (i = 0; i < idx; i++) {
988 if (argp[i].type == ARGT_STR)
989 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200990 else if (argp[i].type == ARGT_REG)
991 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200992 }
993 WILL_LJMP(luaL_argerror(L, first + idx, msg));
994 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100995}
996
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100997/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500998 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100999 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001000 *
1001 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001002 * - hlua_sethlua : create the association between hlua context and lua_state.
1003 */
1004static inline struct hlua *hlua_gethlua(lua_State *L)
1005{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001006 struct hlua **hlua = lua_getextraspace(L);
1007 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001008}
1009static inline void hlua_sethlua(struct hlua *hlua)
1010{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001011 struct hlua **hlua_store = lua_getextraspace(hlua->T);
1012 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001013}
1014
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001015/* This function is used to send logs. It try to send on screen (stderr)
1016 * and on the default syslog server.
1017 */
1018static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
1019{
1020 struct tm tm;
1021 char *p;
1022
1023 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001024 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001025 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001026 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001027 /* Break the message if exceed the buffer size. */
1028 *(p-4) = ' ';
1029 *(p-3) = '.';
1030 *(p-2) = '.';
1031 *(p-1) = '.';
1032 break;
1033 }
Willy Tarreau90807112020-02-25 08:16:33 +01001034 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001035 *p = *msg;
1036 else
1037 *p = '.';
1038 }
1039 *p = '\0';
1040
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001041 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001042 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001043 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1044 return;
1045
Willy Tarreaua678b432015-08-28 10:14:59 +02001046 get_localtime(date.tv_sec, &tm);
1047 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001048 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001049 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001050 fflush(stderr);
1051 }
1052}
1053
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001054/* This function just ensure that the yield will be always
1055 * returned with a timeout and permit to set some flags
1056 */
1057__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001058 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001059{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001060 struct hlua *hlua;
1061
1062 /* Get hlua struct, or NULL if we execute from main lua state */
1063 hlua = hlua_gethlua(L);
1064 if (!hlua) {
1065 return;
1066 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001067
1068 /* Set the wake timeout. If timeout is required, we set
1069 * the expiration time.
1070 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001071 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001072
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001073 hlua->flags |= flags;
1074
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001075 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001076 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001077}
1078
Willy Tarreau87b09662015-04-03 00:22:06 +02001079/* This function initialises the Lua environment stored in the stream.
1080 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001081 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001082 *
1083 * This function is particular. it initialises a new Lua thread. If the
1084 * initialisation fails (example: out of memory error), the lua function
1085 * throws an error (longjmp).
1086 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001087 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001088 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001089 * threads appear, the safe environment set a lock to ensure only one
1090 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001091 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001092 *
1093 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001094 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001095 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001096 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001097 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001098 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001099 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001100 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001101int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001102{
1103 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001104 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001105 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001106 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001107 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001108 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001109 if (!already_safe) {
1110 if (!SET_SAFE_LJMP_PARENT(lua)) {
1111 lua->Tref = LUA_REFNIL;
1112 return 0;
1113 }
1114 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001115 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001116 if (!lua->T) {
1117 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001118 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001119 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001120 return 0;
1121 }
1122 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001123 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001124 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001125 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001126 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001127 return 1;
1128}
1129
Willy Tarreau87b09662015-04-03 00:22:06 +02001130/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001131 * is destroyed. The destroy also the memory context. The struct "lua"
Aurelien DARRAGONd5c8a102023-03-01 16:45:50 +01001132 * will be freed.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001133 */
1134void hlua_ctx_destroy(struct hlua *lua)
1135{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001136 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001137 return;
1138
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001139 if (!lua->T)
1140 goto end;
1141
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001142 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001143 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001144
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001145 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001146 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001147 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001148 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001149
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001150 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001151 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001152 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001153 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001154 /* Forces a garbage collecting process. If the Lua program is finished
1155 * without error, we run the GC on the thread pointer. Its freed all
1156 * the unused memory.
1157 * If the thread is finnish with an error or is currently yielded,
1158 * it seems that the GC applied on the thread doesn't clean anything,
1159 * so e run the GC on the main thread.
1160 * NOTE: maybe this action locks all the Lua threads untiml the en of
1161 * the garbage collection.
1162 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001163 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001164 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001165 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001166 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001167 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001168 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001169
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001170 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001171
1172end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001173 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001174}
1175
1176/* This function is used to restore the Lua context when a coroutine
1177 * fails. This function copy the common memory between old coroutine
1178 * and the new coroutine. The old coroutine is destroyed, and its
1179 * replaced by the new coroutine.
1180 * If the flag "keep_msg" is set, the last entry of the old is assumed
1181 * as string error message and it is copied in the new stack.
1182 */
1183static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1184{
1185 lua_State *T;
1186 int new_ref;
1187
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001188 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001189 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001190 if (!T)
1191 return 0;
1192
1193 /* Copy last error message. */
1194 if (keep_msg)
1195 lua_xmove(lua->T, T, 1);
1196
1197 /* Copy data between the coroutines. */
1198 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1199 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001200 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001201
1202 /* Destroy old data. */
1203 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1204
1205 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001206 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207
1208 /* Fill the struct with the new coroutine values. */
1209 lua->Mref = new_ref;
1210 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001211 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212
1213 /* Set context. */
1214 hlua_sethlua(lua);
1215
1216 return 1;
1217}
1218
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001219void hlua_hook(lua_State *L, lua_Debug *ar)
1220{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001221 struct hlua *hlua;
1222
1223 /* Get hlua struct, or NULL if we execute from main lua state */
1224 hlua = hlua_gethlua(L);
1225 if (!hlua)
1226 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001227
1228 /* Lua cannot yield when its returning from a function,
1229 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001230 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001231 */
1232 if (lua_gethookmask(L) & LUA_MASKRET) {
1233 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1234 return;
1235 }
1236
1237 /* restore the interrupt condition. */
1238 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1239
1240 /* If we interrupt the Lua processing in yieldable state, we yield.
1241 * If the state is not yieldable, trying yield causes an error.
1242 */
1243 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001244 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001245
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001246 /* If we cannot yield, update the clock and check the timeout. */
1247 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001248 hlua->run_time += now_ms - hlua->start_time;
1249 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001250 lua_pushfstring(L, "execution timeout");
1251 WILL_LJMP(lua_error(L));
1252 }
1253
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001254 /* Update the start time. */
1255 hlua->start_time = now_ms;
1256
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001257 /* Try to interrupt the process at the end of the current
1258 * unyieldable function.
1259 */
1260 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001261}
1262
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001263/* This function start or resumes the Lua stack execution. If the flag
1264 * "yield_allowed" if no set and the LUA stack execution returns a yield
1265 * The function return an error.
1266 *
1267 * The function can returns 4 values:
1268 * - HLUA_E_OK : The execution is terminated without any errors.
1269 * - HLUA_E_AGAIN : The execution must continue at the next associated
1270 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001271 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001272 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001273 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001274 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001275 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001276 * LUA code.
1277 */
1278static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1279{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001280#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1281 int nres;
1282#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001283 int ret;
1284 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001285 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001286
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001287 /* Initialise run time counter. */
1288 if (!HLUA_IS_RUNNING(lua))
1289 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001290
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001291 /* Lock the whole Lua execution. This lock must be before the
1292 * label "resume_execution".
1293 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001294 if (lua->state_id == 0)
Willy Tarreau3eb2e472021-08-20 15:47:25 +02001295 lua_take_global_lock();
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001296
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001297resume_execution:
1298
1299 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1300 * instructions. it is used for preventing infinite loops.
1301 */
1302 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1303
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001304 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001305 HLUA_SET_RUN(lua);
1306 HLUA_CLR_CTRLYIELD(lua);
1307 HLUA_CLR_WAKERESWR(lua);
1308 HLUA_CLR_WAKEREQWR(lua);
Christopher Fauleta6e792f2021-08-04 17:58:21 +02001309 HLUA_CLR_NOYIELD(lua);
1310 if (!yield_allowed)
1311 HLUA_SET_NOYIELD(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001312
Christopher Fauletbc275a92020-02-26 14:55:16 +01001313 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001314 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001315 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001316
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001317 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001318#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001319 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001320#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001321 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001322#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001323 switch (ret) {
1324
1325 case LUA_OK:
1326 ret = HLUA_E_OK;
1327 break;
1328
1329 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001330 /* Check if the execution timeout is expired. It it is the case, we
1331 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001332 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001333 tv_update_date(0, 1);
1334 lua->run_time += now_ms - lua->start_time;
1335 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001336 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001337 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001338 break;
1339 }
1340 /* Process the forced yield. if the general yield is not allowed or
1341 * if no task were associated this the current Lua execution
1342 * coroutine, we resume the execution. Else we want to return in the
1343 * scheduler and we want to be waked up again, to continue the
1344 * current Lua execution. So we schedule our own task.
1345 */
1346 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001347 if (!yield_allowed || !lua->task)
1348 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001349 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001350 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001351 if (!yield_allowed) {
1352 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001353 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001354 break;
1355 }
1356 ret = HLUA_E_AGAIN;
1357 break;
1358
1359 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001360
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001361 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001362 * because the errors ares the only one mean to return immediately
1363 * from and lua execution.
1364 */
1365 if (lua->flags & HLUA_EXIT) {
1366 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001367 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001368 break;
1369 }
1370
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001371 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001372 if (!lua_checkstack(lua->T, 1)) {
1373 ret = HLUA_E_ERR;
1374 break;
1375 }
1376 msg = lua_tostring(lua->T, -1);
1377 lua_settop(lua->T, 0); /* Empty the stack. */
1378 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001379 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001380 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001381 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001382 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001383 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001384 ret = HLUA_E_ERRMSG;
1385 break;
1386
1387 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001388 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001389 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001390 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001391 break;
1392
1393 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001394 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001395 if (!lua_checkstack(lua->T, 1)) {
1396 ret = HLUA_E_ERR;
1397 break;
1398 }
1399 msg = lua_tostring(lua->T, -1);
1400 lua_settop(lua->T, 0); /* Empty the stack. */
1401 lua_pop(lua->T, 1);
1402 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001403 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001404 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001405 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001406 ret = HLUA_E_ERRMSG;
1407 break;
1408
1409 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001410 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001411 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001412 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001413 break;
1414 }
1415
1416 switch (ret) {
1417 case HLUA_E_AGAIN:
1418 break;
1419
1420 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001421 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001422 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001423 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001424 break;
1425
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001426 case HLUA_E_ETMOUT:
1427 case HLUA_E_NOMEM:
1428 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001429 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001430 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001431 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001432 hlua_ctx_renew(lua, 0);
1433 break;
1434
1435 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001436 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001437 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001438 break;
1439 }
1440
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001441 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001442 if (lua->state_id == 0)
Willy Tarreau3eb2e472021-08-20 15:47:25 +02001443 lua_drop_global_lock();
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001444
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001445 return ret;
1446}
1447
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001448/* This function exit the current code. */
1449__LJMP static int hlua_done(lua_State *L)
1450{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001451 struct hlua *hlua;
1452
1453 /* Get hlua struct, or NULL if we execute from main lua state */
1454 hlua = hlua_gethlua(L);
1455 if (!hlua)
1456 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001457
1458 hlua->flags |= HLUA_EXIT;
1459 WILL_LJMP(lua_error(L));
1460
1461 return 0;
1462}
1463
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001464/* This function is an LUA binding. It provides a function
1465 * for deleting ACL from a referenced ACL file.
1466 */
1467__LJMP static int hlua_del_acl(lua_State *L)
1468{
1469 const char *name;
1470 const char *key;
1471 struct pat_ref *ref;
1472
1473 MAY_LJMP(check_args(L, 2, "del_acl"));
1474
1475 name = MAY_LJMP(luaL_checkstring(L, 1));
1476 key = MAY_LJMP(luaL_checkstring(L, 2));
1477
1478 ref = pat_ref_lookup(name);
1479 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001480 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001481
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001482 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001483 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001484 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001485 return 0;
1486}
1487
1488/* This function is an LUA binding. It provides a function
1489 * for deleting map entry from a referenced map file.
1490 */
1491static int hlua_del_map(lua_State *L)
1492{
1493 const char *name;
1494 const char *key;
1495 struct pat_ref *ref;
1496
1497 MAY_LJMP(check_args(L, 2, "del_map"));
1498
1499 name = MAY_LJMP(luaL_checkstring(L, 1));
1500 key = MAY_LJMP(luaL_checkstring(L, 2));
1501
1502 ref = pat_ref_lookup(name);
1503 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001504 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001505
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001506 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001507 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001508 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001509 return 0;
1510}
1511
1512/* This function is an LUA binding. It provides a function
1513 * for adding ACL pattern from a referenced ACL file.
1514 */
1515static int hlua_add_acl(lua_State *L)
1516{
1517 const char *name;
1518 const char *key;
1519 struct pat_ref *ref;
1520
1521 MAY_LJMP(check_args(L, 2, "add_acl"));
1522
1523 name = MAY_LJMP(luaL_checkstring(L, 1));
1524 key = MAY_LJMP(luaL_checkstring(L, 2));
1525
1526 ref = pat_ref_lookup(name);
1527 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001528 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001529
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001530 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001531 if (pat_ref_find_elt(ref, key) == NULL)
1532 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001533 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001534 return 0;
1535}
1536
1537/* This function is an LUA binding. It provides a function
1538 * for setting map pattern and sample from a referenced map
1539 * file.
1540 */
1541static int hlua_set_map(lua_State *L)
1542{
1543 const char *name;
1544 const char *key;
1545 const char *value;
1546 struct pat_ref *ref;
1547
1548 MAY_LJMP(check_args(L, 3, "set_map"));
1549
1550 name = MAY_LJMP(luaL_checkstring(L, 1));
1551 key = MAY_LJMP(luaL_checkstring(L, 2));
1552 value = MAY_LJMP(luaL_checkstring(L, 3));
1553
1554 ref = pat_ref_lookup(name);
1555 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001556 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001557
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001558 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001559 if (pat_ref_find_elt(ref, key) != NULL)
1560 pat_ref_set(ref, key, value, NULL);
1561 else
1562 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001563 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001564 return 0;
1565}
1566
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001567/* A class is a lot of memory that contain data. This data can be a table,
1568 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001569 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001570 * the name of the object (_G[<name>] = <metable> ).
1571 *
1572 * A metable is a table that modify the standard behavior of a standard
1573 * access to the associated data. The entries of this new metatable are
1574 * defined as is:
1575 *
1576 * http://lua-users.org/wiki/MetatableEvents
1577 *
1578 * __index
1579 *
1580 * we access an absent field in a table, the result is nil. This is
1581 * true, but it is not the whole truth. Actually, such access triggers
1582 * the interpreter to look for an __index metamethod: If there is no
1583 * such method, as usually happens, then the access results in nil;
1584 * otherwise, the metamethod will provide the result.
1585 *
1586 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1587 * the key does not appear in the table, but the metatable has an __index
1588 * property:
1589 *
1590 * - if the value is a function, the function is called, passing in the
1591 * table and the key; the return value of that function is returned as
1592 * the result.
1593 *
1594 * - if the value is another table, the value of the key in that table is
1595 * asked for and returned (and if it doesn't exist in that table, but that
1596 * table's metatable has an __index property, then it continues on up)
1597 *
1598 * - Use "rawget(myTable,key)" to skip this metamethod.
1599 *
1600 * http://www.lua.org/pil/13.4.1.html
1601 *
1602 * __newindex
1603 *
1604 * Like __index, but control property assignment.
1605 *
1606 * __mode - Control weak references. A string value with one or both
1607 * of the characters 'k' and 'v' which specifies that the the
1608 * keys and/or values in the table are weak references.
1609 *
1610 * __call - Treat a table like a function. When a table is followed by
1611 * parenthesis such as "myTable( 'foo' )" and the metatable has
1612 * a __call key pointing to a function, that function is invoked
1613 * (passing any specified arguments) and the return value is
1614 * returned.
1615 *
1616 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1617 * called, if the metatable for myTable has a __metatable
1618 * key, the value of that key is returned instead of the
1619 * actual metatable.
1620 *
1621 * __tostring - Control string representation. When the builtin
1622 * "tostring( myTable )" function is called, if the metatable
1623 * for myTable has a __tostring property set to a function,
1624 * that function is invoked (passing myTable to it) and the
1625 * return value is used as the string representation.
1626 *
1627 * __len - Control table length. When the table length is requested using
1628 * the length operator ( '#' ), if the metatable for myTable has
1629 * a __len key pointing to a function, that function is invoked
1630 * (passing myTable to it) and the return value used as the value
1631 * of "#myTable".
1632 *
1633 * __gc - Userdata finalizer code. When userdata is set to be garbage
1634 * collected, if the metatable has a __gc field pointing to a
1635 * function, that function is first invoked, passing the userdata
1636 * to it. The __gc metamethod is not called for tables.
1637 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1638 *
1639 * Special metamethods for redefining standard operators:
1640 * http://www.lua.org/pil/13.1.html
1641 *
1642 * __add "+"
1643 * __sub "-"
1644 * __mul "*"
1645 * __div "/"
1646 * __unm "!"
1647 * __pow "^"
1648 * __concat ".."
1649 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001650 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001651 * http://www.lua.org/pil/13.2.html
1652 *
1653 * __eq "=="
1654 * __lt "<"
1655 * __le "<="
1656 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657
1658/*
1659 *
1660 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001661 * Class Map
1662 *
1663 *
1664 */
1665
1666/* Returns a struct hlua_map if the stack entry "ud" is
1667 * a class session, otherwise it throws an error.
1668 */
1669__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1670{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001671 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001672}
1673
1674/* This function is the map constructor. It don't need
1675 * the class Map object. It creates and return a new Map
1676 * object. It must be called only during "body" or "init"
1677 * context because it process some filesystem accesses.
1678 */
1679__LJMP static int hlua_map_new(struct lua_State *L)
1680{
1681 const char *fn;
1682 int match = PAT_MATCH_STR;
1683 struct sample_conv conv;
1684 const char *file = "";
1685 int line = 0;
1686 lua_Debug ar;
1687 char *err = NULL;
1688 struct arg args[2];
1689
1690 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1691 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1692
1693 fn = MAY_LJMP(luaL_checkstring(L, 1));
1694
1695 if (lua_gettop(L) >= 2) {
1696 match = MAY_LJMP(luaL_checkinteger(L, 2));
1697 if (match < 0 || match >= PAT_MATCH_NUM)
1698 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1699 }
1700
1701 /* Get Lua filename and line number. */
1702 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1703 lua_getinfo(L, "Sl", &ar); /* get info about it */
1704 if (ar.currentline > 0) { /* is there info? */
1705 file = ar.short_src;
1706 line = ar.currentline;
1707 }
1708 }
1709
1710 /* fill fake sample_conv struct. */
1711 conv.kw = ""; /* unused. */
1712 conv.process = NULL; /* unused. */
1713 conv.arg_mask = 0; /* unused. */
1714 conv.val_args = NULL; /* unused. */
1715 conv.out_type = SMP_T_STR;
1716 conv.private = (void *)(long)match;
1717 switch (match) {
1718 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1719 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1720 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1721 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1722 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1723 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1724 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001725 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001726 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1727 default:
1728 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1729 }
1730
1731 /* fill fake args. */
1732 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001733 args[0].data.str.area = strdup(fn);
1734 args[0].data.str.data = strlen(fn);
1735 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001736 args[1].type = ARGT_STOP;
1737
1738 /* load the map. */
1739 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001740 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001741 * free the err variable.
1742 */
1743 luaL_where(L, 1);
1744 lua_pushfstring(L, "'new': %s.", err);
1745 lua_concat(L, 2);
1746 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001747 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001748 WILL_LJMP(lua_error(L));
1749 }
1750
1751 /* create the lua object. */
1752 lua_newtable(L);
1753 lua_pushlightuserdata(L, args[0].data.map);
1754 lua_rawseti(L, -2, 0);
1755
1756 /* Pop a class Map metatable and affect it to the userdata. */
1757 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1758 lua_setmetatable(L, -2);
1759
1760
1761 return 1;
1762}
1763
1764__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1765{
1766 struct map_descriptor *desc;
1767 struct pattern *pat;
1768 struct sample smp;
1769
1770 MAY_LJMP(check_args(L, 2, "lookup"));
1771 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001772 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001773 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001774 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001775 }
1776 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001777 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001778 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001779 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 +01001780 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001781 }
1782
1783 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001784 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001785 if (str)
1786 lua_pushstring(L, "");
1787 else
1788 lua_pushnil(L);
1789 return 1;
1790 }
1791
1792 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001793 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001794 return 1;
1795}
1796
1797__LJMP static int hlua_map_lookup(struct lua_State *L)
1798{
1799 return _hlua_map_lookup(L, 0);
1800}
1801
1802__LJMP static int hlua_map_slookup(struct lua_State *L)
1803{
1804 return _hlua_map_lookup(L, 1);
1805}
1806
1807/*
1808 *
1809 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001810 * Class Socket
1811 *
1812 *
1813 */
1814
1815__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1816{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001817 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818}
1819
1820/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001821 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822 * received.
1823 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001824static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001826 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001828 if (appctx->ctx.hlua_cosocket.die) {
1829 si_shutw(si);
1830 si_shutr(si);
1831 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001832 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1833 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001834 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1835 }
1836
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001837 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001838 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001839 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001840
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001841 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001842 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001843 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001844
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001845 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001846 * to be notified whenever the connection completes.
1847 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001848 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001849 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001850 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001851 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001852 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001853 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001854
1855 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001856 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001858 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001859 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001860 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861
1862 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001863 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001864 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001865
1866 /* Some data were injected in the buffer, notify the stream
1867 * interface.
1868 */
1869 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001870 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001871
1872 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001873 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001874 */
1875 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001876 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877}
1878
Willy Tarreau87b09662015-04-03 00:22:06 +02001879/* This function is called when the "struct stream" is destroyed.
1880 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001881 * Wake all the pending signals.
1882 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001883static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001885 struct xref *peer;
1886
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001887 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001888 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1889 if (peer)
1890 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891
1892 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001893 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1894 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895}
1896
1897/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001898 * uses this object. If the stream does not exists, just quit.
1899 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900 * pending signal can rest in the read and write lists. destroy
1901 * it.
1902 */
1903__LJMP static int hlua_socket_gc(lua_State *L)
1904{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001905 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001907 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001909 MAY_LJMP(check_args(L, 1, "__gc"));
1910
1911 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001912 peer = xref_get_peer_and_lock(&socket->xref);
1913 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001915 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001917 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001918 appctx->ctx.hlua_cosocket.die = 1;
1919 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001920
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001921 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001922 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923 return 0;
1924}
1925
1926/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001927 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928 */
sada05ed3302018-05-11 11:48:18 -07001929__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001931 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001932 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001933 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001934 struct hlua *hlua;
1935
1936 /* Get hlua struct, or NULL if we execute from main lua state */
1937 hlua = hlua_gethlua(L);
1938 if (!hlua)
1939 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001940
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001941 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001942
1943 /* Check if we run on the same thread than the xreator thread.
1944 * We cannot access to the socket if the thread is different.
1945 */
1946 if (socket->tid != tid)
1947 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1948
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001949 peer = xref_get_peer_and_lock(&socket->xref);
1950 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001951 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001952
1953 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001954 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001955
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001956 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001957 appctx->ctx.hlua_cosocket.die = 1;
1958 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001960 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001961 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 return 0;
1963}
1964
sada05ed3302018-05-11 11:48:18 -07001965/* The close function calls close_helper.
1966 */
1967__LJMP static int hlua_socket_close(lua_State *L)
1968{
1969 MAY_LJMP(check_args(L, 1, "close"));
1970 return hlua_socket_close_helper(L);
1971}
1972
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973/* This Lua function assumes that the stack contain three parameters.
1974 * 1 - USERDATA containing a struct socket
1975 * 2 - INTEGER with values of the macro defined below
1976 * If the integer is -1, we must read at most one line.
1977 * If the integer is -2, we ust read all the data until the
1978 * end of the stream.
1979 * If the integer is positive value, we must read a number of
1980 * bytes corresponding to this value.
1981 */
1982#define HLSR_READ_LINE (-1)
1983#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001984__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985{
1986 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1987 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001988 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001989 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001990 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001991 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001992 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001993 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001994 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001995 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001996 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001997 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001998 struct stream_interface *si;
1999 struct stream *s;
2000 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002001 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002002
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002003 /* Get hlua struct, or NULL if we execute from main lua state */
2004 hlua = hlua_gethlua(L);
2005
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002006 /* Check if this lua stack is schedulable. */
2007 if (!hlua || !hlua->task)
2008 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
2009 "'frontend', 'backend' or 'task'"));
2010
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002011 /* Check if we run on the same thread than the xreator thread.
2012 * We cannot access to the socket if the thread is different.
2013 */
2014 if (socket->tid != tid)
2015 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2016
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002017 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002018 peer = xref_get_peer_and_lock(&socket->xref);
2019 if (!peer)
2020 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002021 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2022 si = appctx->owner;
2023 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002024
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002025 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002028 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 if (nblk < 0) /* Connection close. */
2030 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002031 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002033
2034 /* remove final \r\n. */
2035 if (nblk == 1) {
2036 if (blk1[len1-1] == '\n') {
2037 len1--;
2038 skip_at_end++;
2039 if (blk1[len1-1] == '\r') {
2040 len1--;
2041 skip_at_end++;
2042 }
2043 }
2044 }
2045 else {
2046 if (blk2[len2-1] == '\n') {
2047 len2--;
2048 skip_at_end++;
2049 if (blk2[len2-1] == '\r') {
2050 len2--;
2051 skip_at_end++;
2052 }
2053 }
2054 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002055 }
2056
2057 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002058 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002059 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060 if (nblk < 0) /* Connection close. */
2061 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002062 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063 goto connection_empty;
2064 }
2065
2066 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002068 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069 if (nblk < 0) /* Connection close. */
2070 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002071 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002072 goto connection_empty;
2073
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002074 missing_bytes = wanted - socket->b.n;
2075 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002076 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002077 len1 = missing_bytes;
2078 } if (nblk == 2 && len1 + len2 > missing_bytes)
2079 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002080 }
2081
2082 len = len1;
2083
2084 luaL_addlstring(&socket->b, blk1, len1);
2085 if (nblk == 2) {
2086 len += len2;
2087 luaL_addlstring(&socket->b, blk2, len2);
2088 }
2089
2090 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002091 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002092
2093 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002094 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095
2096 /* If the pattern reclaim to read all the data
2097 * in the connection, got out.
2098 */
2099 if (wanted == HLSR_READ_ALL)
2100 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002101 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 goto connection_empty;
2103
2104 /* Return result. */
2105 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002106 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002107 return 1;
2108
2109connection_closed:
2110
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002111 xref_unlock(&socket->xref, peer);
2112
2113no_peer:
2114
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002115 /* If the buffer containds data. */
2116 if (socket->b.n > 0) {
2117 luaL_pushresult(&socket->b);
2118 return 1;
2119 }
2120 lua_pushnil(L);
2121 lua_pushstring(L, "connection closed.");
2122 return 2;
2123
2124connection_empty:
2125
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002126 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2127 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002128 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002129 }
2130 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002131 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002132 return 0;
2133}
2134
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002135/* This Lua function gets two parameters. The first one can be string
2136 * or a number. If the string is "*l", the user requires one line. If
2137 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002138 * If the value is a number, the user require a number of bytes equal
2139 * to the value. The default value is "*l" (a line).
2140 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002141 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002142 * integer takes this values:
2143 * -1 : read a line
2144 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002145 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002146 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002147 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 * concatenated with the read data.
2149 */
2150__LJMP static int hlua_socket_receive(struct lua_State *L)
2151{
2152 int wanted = HLSR_READ_LINE;
2153 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002154 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002155 char *error;
2156 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002157 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002158
2159 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2160 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2161
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002162 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002163
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002164 /* Check if we run on the same thread than the xreator thread.
2165 * We cannot access to the socket if the thread is different.
2166 */
2167 if (socket->tid != tid)
2168 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2169
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002170 /* check for pattern. */
2171 if (lua_gettop(L) >= 2) {
2172 type = lua_type(L, 2);
2173 if (type == LUA_TSTRING) {
2174 pattern = lua_tostring(L, 2);
2175 if (strcmp(pattern, "*a") == 0)
2176 wanted = HLSR_READ_ALL;
2177 else if (strcmp(pattern, "*l") == 0)
2178 wanted = HLSR_READ_LINE;
2179 else {
2180 wanted = strtoll(pattern, &error, 10);
2181 if (*error != '\0')
2182 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2183 }
2184 }
2185 else if (type == LUA_TNUMBER) {
2186 wanted = lua_tointeger(L, 2);
2187 if (wanted < 0)
2188 WILL_LJMP(luaL_error(L, "Unsupported size."));
2189 }
2190 }
2191
2192 /* Set pattern. */
2193 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002194
2195 /* Check if we would replace the top by itself. */
2196 if (lua_gettop(L) != 2)
2197 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198
Christopher Fauletc31b2002021-05-03 10:11:13 +02002199 /* Save index of the top of the stack because since buffers are used, it
2200 * may change
2201 */
2202 lastarg = lua_gettop(L);
2203
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002204 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002205 luaL_buffinit(L, &socket->b);
2206
2207 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002208 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002209 if (lua_type(L, 3) != LUA_TSTRING)
2210 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2211 pattern = lua_tolstring(L, 3, &len);
2212 luaL_addlstring(&socket->b, pattern, len);
2213 }
2214
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002215 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002216}
2217
2218/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002219 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002220 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002221static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002222{
2223 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002224 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002225 struct appctx *appctx;
2226 size_t buf_len;
2227 const char *buf;
2228 int len;
2229 int send_len;
2230 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002231 struct xref *peer;
2232 struct stream_interface *si;
2233 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002234
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002235 /* Get hlua struct, or NULL if we execute from main lua state */
2236 hlua = hlua_gethlua(L);
2237
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002238 /* Check if this lua stack is schedulable. */
2239 if (!hlua || !hlua->task)
2240 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2241 "'frontend', 'backend' or 'task'"));
2242
2243 /* Get object */
2244 socket = MAY_LJMP(hlua_checksocket(L, 1));
2245 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002246 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002248 /* Check if we run on the same thread than the xreator thread.
2249 * We cannot access to the socket if the thread is different.
2250 */
2251 if (socket->tid != tid)
2252 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2253
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002254 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002255 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002256 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002257 lua_pushinteger(L, -1);
2258 return 1;
2259 }
2260 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2261 si = appctx->owner;
2262 s = si_strm(si);
2263
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002264 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002265 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002266 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267 lua_pushinteger(L, -1);
2268 return 1;
2269 }
2270
2271 /* Update the input buffer data. */
2272 buf += sent;
2273 send_len = buf_len - sent;
2274
2275 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002276 if (sent >= buf_len) {
2277 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002279 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002281 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002282 * the request buffer if its not required.
2283 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002284 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002285 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002286 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002287 }
2288
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002289 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002290 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002291 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002293 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294
2295 /* send data */
2296 if (len < send_len)
2297 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002298 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299
2300 /* "Not enough space" (-1), "Buffer too little to contain
2301 * the data" (-2) are not expected because the available length
2302 * is tested.
2303 * Other unknown error are also not expected.
2304 */
2305 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002306 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002307 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002308
sada05ed3302018-05-11 11:48:18 -07002309 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002311 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002312 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002313 return 1;
2314 }
2315
2316 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002317 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002318
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002319 s->req.rex = TICK_ETERNITY;
2320 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321
2322 /* Update length sent. */
2323 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002324 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002325
2326 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002327 if (sent + len >= buf_len) {
2328 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002329 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002330 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002331
2332hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002333 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2334 xref_unlock(&socket->xref, peer);
2335 WILL_LJMP(luaL_error(L, "out of memory"));
2336 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002337 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002338 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002339 return 0;
2340}
2341
2342/* This function initiate the send of data. It just check the input
2343 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002344 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002345 * "hlua_socket_write_yield" that can yield.
2346 *
2347 * The Lua function gets between 3 and 4 parameters. The first one is
2348 * the associated object. The second is a string buffer. The third is
2349 * a facultative integer that represents where is the buffer position
2350 * of the start of the data that can send. The first byte is the
2351 * position "1". The default value is "1". The fourth argument is a
2352 * facultative integer that represents where is the buffer position
2353 * of the end of the data that can send. The default is the last byte.
2354 */
2355static int hlua_socket_send(struct lua_State *L)
2356{
2357 int i;
2358 int j;
2359 const char *buf;
2360 size_t buf_len;
2361
2362 /* Check number of arguments. */
2363 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2364 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2365
2366 /* Get the string. */
2367 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2368
2369 /* Get and check j. */
2370 if (lua_gettop(L) == 4) {
2371 j = MAY_LJMP(luaL_checkinteger(L, 4));
2372 if (j < 0)
2373 j = buf_len + j + 1;
2374 if (j > buf_len)
2375 j = buf_len + 1;
2376 lua_pop(L, 1);
2377 }
2378 else
2379 j = buf_len;
2380
2381 /* Get and check i. */
2382 if (lua_gettop(L) == 3) {
2383 i = MAY_LJMP(luaL_checkinteger(L, 3));
2384 if (i < 0)
2385 i = buf_len + i + 1;
2386 if (i > buf_len)
2387 i = buf_len + 1;
2388 lua_pop(L, 1);
2389 } else
2390 i = 1;
2391
2392 /* Check bth i and j. */
2393 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002394 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002395 return 1;
2396 }
2397 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002398 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399 return 1;
2400 }
2401 if (i == 0)
2402 i = 1;
2403 if (j == 0)
2404 j = 1;
2405
2406 /* Pop the string. */
2407 lua_pop(L, 1);
2408
2409 /* Update the buffer length. */
2410 buf += i - 1;
2411 buf_len = j - i + 1;
2412 lua_pushlstring(L, buf, buf_len);
2413
2414 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002415 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002417 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418}
2419
Willy Tarreau22b0a682015-06-17 19:43:49 +02002420#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002421__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2422{
2423 static char buffer[SOCKET_INFO_MAX_LEN];
2424 int ret;
2425 int len;
2426 char *p;
2427
2428 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2429 if (ret <= 0) {
2430 lua_pushnil(L);
2431 return 1;
2432 }
2433
2434 if (ret == AF_UNIX) {
2435 lua_pushstring(L, buffer+1);
2436 return 1;
2437 }
2438 else if (ret == AF_INET6) {
2439 buffer[0] = '[';
2440 len = strlen(buffer);
2441 buffer[len] = ']';
2442 len++;
2443 buffer[len] = ':';
2444 len++;
2445 p = buffer;
2446 }
2447 else if (ret == AF_INET) {
2448 p = buffer + 1;
2449 len = strlen(p);
2450 p[len] = ':';
2451 len++;
2452 }
2453 else {
2454 lua_pushnil(L);
2455 return 1;
2456 }
2457
2458 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2459 lua_pushnil(L);
2460 return 1;
2461 }
2462
2463 lua_pushstring(L, p);
2464 return 1;
2465}
2466
2467/* Returns information about the peer of the connection. */
2468__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2469{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002470 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002471 struct xref *peer;
2472 struct appctx *appctx;
2473 struct stream_interface *si;
2474 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002475 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002476
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002477 MAY_LJMP(check_args(L, 1, "getpeername"));
2478
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002479 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002480
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002481 /* Check if we run on the same thread than the xreator thread.
2482 * We cannot access to the socket if the thread is different.
2483 */
2484 if (socket->tid != tid)
2485 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2486
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002487 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002488 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002489 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002490 lua_pushnil(L);
2491 return 1;
2492 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002493 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2494 si = appctx->owner;
2495 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002496
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002497 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002498 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002499 lua_pushnil(L);
2500 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002501 }
2502
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002503 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002504 xref_unlock(&socket->xref, peer);
2505 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002506}
2507
2508/* Returns information about my connection side. */
2509static int hlua_socket_getsockname(struct lua_State *L)
2510{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002511 struct hlua_socket *socket;
2512 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002513 struct appctx *appctx;
2514 struct xref *peer;
2515 struct stream_interface *si;
2516 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002517 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002518
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519 MAY_LJMP(check_args(L, 1, "getsockname"));
2520
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002521 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002522
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002523 /* Check if we run on the same thread than the xreator thread.
2524 * We cannot access to the socket if the thread is different.
2525 */
2526 if (socket->tid != tid)
2527 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2528
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002529 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002530 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002531 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002532 lua_pushnil(L);
2533 return 1;
2534 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002535 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2536 si = appctx->owner;
2537 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002539 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002540 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002541 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002542 lua_pushnil(L);
2543 return 1;
2544 }
2545
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002546 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002547 xref_unlock(&socket->xref, peer);
2548 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002549}
2550
2551/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002552static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553 .obj_type = OBJ_TYPE_APPLET,
2554 .name = "<LUA_TCP>",
2555 .fct = hlua_socket_handler,
2556 .release = hlua_socket_release,
2557};
2558
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002559__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002560{
2561 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002562 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002563 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002564 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002565 struct stream_interface *si;
2566 struct stream *s;
2567
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002568 /* Get hlua struct, or NULL if we execute from main lua state */
2569 hlua = hlua_gethlua(L);
2570 if (!hlua)
2571 return 0;
2572
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002573 /* Check if we run on the same thread than the xreator thread.
2574 * We cannot access to the socket if the thread is different.
2575 */
2576 if (socket->tid != tid)
2577 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2578
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002579 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002580 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002581 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002582 lua_pushnil(L);
2583 lua_pushstring(L, "Can't connect");
2584 return 2;
2585 }
2586 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2587 si = appctx->owner;
2588 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002589
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002590 /* Check if we run on the same thread than the xreator thread.
2591 * We cannot access to the socket if the thread is different.
2592 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002593 if (socket->tid != tid) {
2594 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002595 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002596 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002597
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002599 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002600 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601 lua_pushnil(L);
2602 lua_pushstring(L, "Can't connect");
2603 return 2;
2604 }
2605
Willy Tarreaue09101e2018-10-16 17:37:12 +02002606 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002607
2608 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002609 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002610 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002611 lua_pushinteger(L, 1);
2612 return 1;
2613 }
2614
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002615 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2616 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002617 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002618 }
2619 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002620 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002621 return 0;
2622}
2623
2624/* This function fail or initite the connection. */
2625__LJMP static int hlua_socket_connect(struct lua_State *L)
2626{
2627 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002628 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002629 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002630 struct hlua *hlua;
2631 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002632 int low, high;
2633 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002634 struct xref *peer;
2635 struct stream_interface *si;
2636 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002638 if (lua_gettop(L) < 2)
2639 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640
2641 /* Get args. */
2642 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002643
2644 /* Check if we run on the same thread than the xreator thread.
2645 * We cannot access to the socket if the thread is different.
2646 */
2647 if (socket->tid != tid)
2648 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2649
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002651 if (lua_gettop(L) >= 3) {
2652 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002653 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654
Tim Duesterhus6edab862018-01-06 19:04:45 +01002655 /* Force the ip to end with a colon, to support IPv6 addresses
2656 * that are not enclosed within square brackets.
2657 */
2658 if (port > 0) {
2659 luaL_buffinit(L, &b);
2660 luaL_addstring(&b, ip);
2661 luaL_addchar(&b, ':');
2662 luaL_pushresult(&b);
2663 ip = lua_tolstring(L, lua_gettop(L), NULL);
2664 }
2665 }
2666
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002667 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002668 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002669 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002670 lua_pushnil(L);
2671 return 1;
2672 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002673
2674 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002675 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 +02002676 if (!addr) {
2677 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002678 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002679 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002680
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002681 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002682 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002683 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002684 if (port == -1) {
2685 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002686 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002687 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002688 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2689 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002690 if (port == -1) {
2691 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002692 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002693 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002694 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002695 }
2696 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002697
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002698 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2699 si = appctx->owner;
2700 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002701
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002702 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002703 xref_unlock(&socket->xref, peer);
2704 WILL_LJMP(luaL_error(L, "connect: internal error"));
2705 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002706 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002707
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002708 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002709 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002710 if (!hlua)
2711 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002712
2713 /* inform the stream that we want to be notified whenever the
2714 * connection completes.
2715 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002716 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002717 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002718 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002719
Willy Tarreauf31af932020-01-14 09:59:38 +01002720 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002721
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002722 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2723 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002724 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002725 }
2726 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002727
2728 task_wakeup(s->task, TASK_WOKEN_INIT);
2729 /* Return yield waiting for connection. */
2730
Willy Tarreau9635e032018-10-16 17:52:55 +02002731 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002732
2733 return 0;
2734}
2735
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002736#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002737__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2738{
2739 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002740 struct xref *peer;
2741 struct appctx *appctx;
2742 struct stream_interface *si;
2743 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002744
2745 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2746 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002747
2748 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002749 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002750 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002751 lua_pushnil(L);
2752 return 1;
2753 }
2754 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2755 si = appctx->owner;
2756 s = si_strm(si);
2757
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002758 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002759 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002760 return MAY_LJMP(hlua_socket_connect(L));
2761}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002762#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002763
2764__LJMP static int hlua_socket_setoption(struct lua_State *L)
2765{
2766 return 0;
2767}
2768
2769__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2770{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002771 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002772 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002773 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002774 struct xref *peer;
2775 struct appctx *appctx;
2776 struct stream_interface *si;
2777 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002778
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002779 MAY_LJMP(check_args(L, 2, "settimeout"));
2780
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002781 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002782
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002783 /* convert the timeout to millis */
2784 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002785
Thierry Fournier17a921b2018-03-08 09:59:02 +01002786 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002787 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002788 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2789
Mark Lakes56cc1252018-03-27 09:48:06 +02002790 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002791 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002792
2793 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002794 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002795 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002796
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002797 /* Check if we run on the same thread than the xreator thread.
2798 * We cannot access to the socket if the thread is different.
2799 */
2800 if (socket->tid != tid)
2801 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2802
Mark Lakes56cc1252018-03-27 09:48:06 +02002803 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002804 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002805 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002806 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2807 WILL_LJMP(lua_error(L));
2808 return 0;
2809 }
2810 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2811 si = appctx->owner;
2812 s = si_strm(si);
2813
Cyril Bonté7bb63452018-08-17 23:51:02 +02002814 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002815 s->req.rto = tmout;
2816 s->req.wto = tmout;
2817 s->res.rto = tmout;
2818 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002819 s->req.rex = tick_add_ifset(now_ms, tmout);
2820 s->req.wex = tick_add_ifset(now_ms, tmout);
2821 s->res.rex = tick_add_ifset(now_ms, tmout);
2822 s->res.wex = tick_add_ifset(now_ms, tmout);
2823
2824 s->task->expire = tick_add_ifset(now_ms, tmout);
2825 task_queue(s->task);
2826
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002827 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002828
Thierry Fourniere9636f12018-03-08 09:54:32 +01002829 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002830 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002831}
2832
2833__LJMP static int hlua_socket_new(lua_State *L)
2834{
2835 struct hlua_socket *socket;
2836 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002837 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002838 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002839
2840 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002841 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002842 hlua_pusherror(L, "socket: full stack");
2843 goto out_fail_conf;
2844 }
2845
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002846 /* Create the object: obj[0] = userdata. */
2847 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002848 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002849 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002850 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002851 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002852
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002853 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002854 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002855 hlua_pusherror(L, "socket: uninitialized pools.");
2856 goto out_fail_conf;
2857 }
2858
Willy Tarreau87b09662015-04-03 00:22:06 +02002859 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002860 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2861 lua_setmetatable(L, -2);
2862
Willy Tarreaud420a972015-04-06 00:39:18 +02002863 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002864 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002865 if (!appctx) {
2866 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002867 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002868 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002869
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002870 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002871 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002872 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2873 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002874
Willy Tarreaud420a972015-04-06 00:39:18 +02002875 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002876 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002877 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002878 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002879 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002880 }
2881
Christopher Faulet26256f82020-09-14 11:40:13 +02002882 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002883 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002884 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002885 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002886 }
2887
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002888 /* Initialise cross reference between stream and Lua socket object. */
2889 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002890
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002891 /* Configure "right" stream interface. this "si" is used to connect
2892 * and retrieve data from the server. The connection is initialized
2893 * with the "struct server".
2894 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002895 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002896
2897 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002898 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002899 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002900
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002901 return 1;
2902
Willy Tarreaud420a972015-04-06 00:39:18 +02002903 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002904 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002905 out_fail_sess:
2906 appctx_free(appctx);
2907 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002908 WILL_LJMP(lua_error(L));
2909 return 0;
2910}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002911
2912/*
2913 *
2914 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915 * Class Channel
2916 *
2917 *
2918 */
2919
2920/* Returns the struct hlua_channel join to the class channel in the
2921 * stack entry "ud" or throws an argument error.
2922 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002923__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002924{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002925 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002926}
2927
Willy Tarreau47860ed2015-03-10 14:07:50 +01002928/* Pushes the channel onto the top of the stack. If the stask does not have a
2929 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002930 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002931static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002932{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002934 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002935 return 0;
2936
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002937 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002938 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002939 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940
2941 /* Pop a class sesison metatable and affect it to the userdata. */
2942 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2943 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944 return 1;
2945}
2946
2947/* Duplicate all the data present in the input channel and put it
2948 * in a string LUA variables. Returns -1 and push a nil value in
2949 * the stack if the channel is closed and all the data are consumed,
2950 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002951 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002953static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002954{
2955 char *blk1;
2956 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002957 size_t len1;
2958 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002959 int ret;
2960 luaL_Buffer b;
2961
Willy Tarreau06d80a92017-10-19 14:32:15 +02002962 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02002963 if (unlikely(ret <= 0)) {
2964 if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) {
2965 lua_pushnil(L);
2966 return -1;
2967 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968 return 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002969 }
2970
2971 luaL_buffinit(L, &b);
2972 luaL_addlstring(&b, blk1, len1);
2973 if (unlikely(ret == 2))
2974 luaL_addlstring(&b, blk2, len2);
2975 luaL_pushresult(&b);
2976
2977 if (unlikely(ret == 2))
2978 return len1 + len2;
2979 return len1;
2980}
2981
2982/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2983 * a yield. This function keep the data in the buffer.
2984 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002985__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002987 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002988
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002989 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2990
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01002991 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02002992 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002993 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002994 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002995
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002997 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 return 1;
2999}
3000
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003001/* Check arguments for the function "hlua_channel_dup_yield". */
3002__LJMP static int hlua_channel_dup(lua_State *L)
3003{
3004 MAY_LJMP(check_args(L, 1, "dup"));
3005 MAY_LJMP(hlua_checkchannel(L, 1));
3006 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
3007}
3008
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009/* "_hlua_channel_dup" wrapper. If no data are available, it returns
3010 * a yield. This function consumes the data in the buffer. It returns
3011 * a string containing the data or a nil pointer if no data are available
3012 * and the channel is closed.
3013 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003014__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003016 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003017 int ret;
3018
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003019 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003020
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003021 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003022 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003023 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003024 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003025
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003026 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02003028 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003029
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030 if (unlikely(ret == -1))
3031 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003032
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003033 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003034 return 1;
3035}
3036
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003037/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003038__LJMP static int hlua_channel_get(lua_State *L)
3039{
3040 MAY_LJMP(check_args(L, 1, "get"));
3041 MAY_LJMP(hlua_checkchannel(L, 1));
3042 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3043}
3044
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003045/* This functions consumes and returns one line. If the channel is closed,
3046 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003047 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048 * value.
3049 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003050__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052 char *blk1;
3053 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003054 size_t len1;
3055 size_t len2;
3056 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003057 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003058 int ret;
3059 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003060
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003061 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3062
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003063 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003064 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003065 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003066 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003067
Willy Tarreau06d80a92017-10-19 14:32:15 +02003068 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02003069 if (ret == 0) {
3070 if (HLUA_CANT_YIELD(hlua_gethlua(L))) {
3071 _hlua_channel_dup(chn, L);
3072 return 1;
3073 }
Willy Tarreau9635e032018-10-16 17:52:55 +02003074 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet055310f2021-08-05 11:58:37 +02003075 }
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003076
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003077 if (ret == -1) {
3078 lua_pushnil(L);
3079 return 1;
3080 }
3081
3082 luaL_buffinit(L, &b);
3083 luaL_addlstring(&b, blk1, len1);
3084 len = len1;
3085 if (unlikely(ret == 2)) {
3086 luaL_addlstring(&b, blk2, len2);
3087 len += len2;
3088 }
3089 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003090 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003091 return 1;
3092}
3093
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003094/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003095__LJMP static int hlua_channel_getline(lua_State *L)
3096{
3097 MAY_LJMP(check_args(L, 1, "getline"));
3098 MAY_LJMP(hlua_checkchannel(L, 1));
3099 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3100}
3101
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003102/* This function takes a string as argument, and append it at the input side of
3103 * channel. If data cannot be copied, because there is not enough space to do so
3104 * or because the channel is closed, it returns -1. Otherwise, it returns the
3105 * amount of data written (the string length). This function does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003106 */
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003107__LJMP static int hlua_channel_append(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003108{
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003109 struct channel *chn;
3110 const char *str;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003111 size_t len;
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003112
3113 MAY_LJMP(check_args(L, 2, "append"));
3114 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3115 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003116
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003117 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003118 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003119 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003120 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003121
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003122 if (len > c_room(chn) || ci_putblk(chn, str, len) < 0)
3123 lua_pushinteger(L, -1);
3124 else
3125 lua_pushinteger(L, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003126 return 1;
3127}
3128
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003129/* The function replaces input data from the channel by the string passed as
3130 * argument. Before clearing the buffer, we take care the copy will be
3131 * possible. It returns the amount of data written (the string length) on
3132 * success or -1 if the copy is not performed. This function does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003133 */
3134__LJMP static int hlua_channel_set(lua_State *L)
3135{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003136 struct channel *chn;
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003137 const char *str;
3138 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003139
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003140 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003141 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003142 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003143
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003144 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003145 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003146 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003147 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003148
Christopher Faulet3b7bc3e2021-08-06 09:59:49 +02003149 /* Be sure we can copied the string once input data will be removed. */
3150 if (len > ci_data(chn) || channel_input_closed(chn))
3151 lua_pushinteger(L, -1);
3152 else {
3153 b_set_data(&chn->buf, co_data(chn));
3154 if (ci_putblk(chn, str, len) < 0)
3155 lua_pushinteger(L, -1);
3156 else
3157 lua_pushinteger(L, len);
3158 }
3159 return -1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160}
3161
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003162/* Append data in the output side of the buffer. This data is immediately
3163 * sent. The function returns the amount of data written. If the buffer
3164 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003165 * if the channel is closed.
3166 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003167__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003168{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003169 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003170 size_t len;
3171 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3172 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3173 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003174 struct hlua *hlua;
3175
3176 /* Get hlua struct, or NULL if we execute from main lua state */
3177 hlua = hlua_gethlua(L);
3178 if (!hlua) {
3179 lua_pushnil(L);
3180 return 1;
3181 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003182
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003183 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003184 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003185 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003186 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003187
Willy Tarreau47860ed2015-03-10 14:07:50 +01003188 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003189 lua_pushinteger(L, -1);
3190 return 1;
3191 }
3192
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003193 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003194 * the request buffer if its not required.
3195 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003196 if (chn->buf.size == 0) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003197 if (HLUA_CANT_YIELD(hlua_gethlua(L)))
3198 return 1;
Willy Tarreau4b962a42018-11-15 11:03:21 +01003199 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003200 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003201 }
3202
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003203 /* The written data will be immediately sent, so we can check
3204 * the available space without taking in account the reserve.
3205 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003206 * data, because the buffer will be flushed.
3207 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003208 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003209
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003210 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003211 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003212 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003213 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003214 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003215 return 1;
3216
3217 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003218 if (max > len - l)
3219 max = len - l;
3220
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003221 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003222 * detects a non contiguous buffer and realign it.
3223 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003224 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003225 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003226
3227 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003228 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003229
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003230 /* buffer replace considers that the input part is filled.
3231 * so, I must forward these new data in the output part.
3232 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003233 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003234
3235 l += max;
3236 lua_pop(L, 1);
3237 lua_pushinteger(L, l);
3238
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003239 if (l < len) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003240 /* If there is no space available, and the output buffer is empty.
3241 * in this case, we cannot add more data, so we cannot yield,
3242 * we return the amount of copied data.
3243 */
3244 max = b_room(&chn->buf);
3245 if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L)))
3246 return 1;
3247
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003248 /* If we are waiting for space in the response buffer, we
3249 * must set the flag WAKERESWR. This flag required the task
3250 * wake up if any activity is detected on the response buffer.
3251 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003252 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003253 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003254 else
3255 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003256 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003257 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003258
3259 return 1;
3260}
3261
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003262/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003263 * yield the LUA process, and resume it without checking the
3264 * input arguments.
3265 */
3266__LJMP static int hlua_channel_send(lua_State *L)
3267{
3268 MAY_LJMP(check_args(L, 2, "send"));
3269 lua_pushinteger(L, 0);
3270
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003271 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003272}
3273
3274/* This function forward and amount of butes. The data pass from
3275 * the input side of the buffer to the output side, and can be
3276 * forwarded. This function never fails.
3277 *
3278 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003279 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003280 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003281__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003282{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003283 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003284 int len;
3285 int l;
3286 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003287 struct hlua *hlua;
3288
3289 /* Get hlua struct, or NULL if we execute from main lua state */
3290 hlua = hlua_gethlua(L);
3291 if (!hlua)
3292 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003293
3294 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003295
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003296 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003297 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003298 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003299 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003300
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003301 len = MAY_LJMP(luaL_checkinteger(L, 2));
3302 l = MAY_LJMP(luaL_checkinteger(L, -1));
3303
3304 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003305 if (max > ci_data(chn))
3306 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003307 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003308 l += max;
3309
3310 lua_pop(L, 1);
3311 lua_pushinteger(L, l);
3312
3313 /* Check if it miss bytes to forward. */
3314 if (l < len) {
3315 /* The the input channel or the output channel are closed, we
3316 * must return the amount of data forwarded.
3317 */
Christopher Faulet055310f2021-08-05 11:58:37 +02003318 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003319 return 1;
3320
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003321 /* If we are waiting for space data in the response buffer, we
3322 * must set the flag WAKERESWR. This flag required the task
3323 * wake up if any activity is detected on the response buffer.
3324 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003325 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003326 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003327 else
3328 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003329
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003330 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003331 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003332 }
3333
3334 return 1;
3335}
3336
3337/* Just check the input and prepare the stack for the previous
3338 * function "hlua_channel_forward_yield"
3339 */
3340__LJMP static int hlua_channel_forward(lua_State *L)
3341{
3342 MAY_LJMP(check_args(L, 2, "forward"));
3343 MAY_LJMP(hlua_checkchannel(L, 1));
3344 MAY_LJMP(luaL_checkinteger(L, 2));
3345
3346 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003347 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003348}
3349
3350/* Just returns the number of bytes available in the input
3351 * side of the buffer. This function never fails.
3352 */
3353__LJMP static int hlua_channel_get_in_len(lua_State *L)
3354{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003355 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003356
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003357 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003358 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003359 if (IS_HTX_STRM(chn_strm(chn))) {
3360 struct htx *htx = htxbuf(&chn->buf);
3361 lua_pushinteger(L, htx->data - co_data(chn));
3362 }
3363 else
3364 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003365 return 1;
3366}
3367
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003368/* Returns true if the channel is full. */
3369__LJMP static int hlua_channel_is_full(lua_State *L)
3370{
3371 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003372
3373 MAY_LJMP(check_args(L, 1, "is_full"));
3374 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003375 /* ignore the reserve, we are not on a producer side (ie in an
3376 * applet).
3377 */
3378 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003379 return 1;
3380}
3381
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003382/* Returns true if the channel is the response channel. */
3383__LJMP static int hlua_channel_is_resp(lua_State *L)
3384{
3385 struct channel *chn;
3386
3387 MAY_LJMP(check_args(L, 1, "is_resp"));
3388 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3389
3390 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3391 return 1;
3392}
3393
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003394/* Just returns the number of bytes available in the output
3395 * side of the buffer. This function never fails.
3396 */
3397__LJMP static int hlua_channel_get_out_len(lua_State *L)
3398{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003399 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003400
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003401 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003402 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003403 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003404 return 1;
3405}
3406
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003407/*
3408 *
3409 *
3410 * Class Fetches
3411 *
3412 *
3413 */
3414
3415/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003416 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003417 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003418__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003419{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003420 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003421}
3422
3423/* This function creates and push in the stack a fetch object according
3424 * with a current TXN.
3425 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003426static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003427{
Willy Tarreau7073c472015-04-06 11:15:40 +02003428 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003429
3430 /* Check stack size. */
3431 if (!lua_checkstack(L, 3))
3432 return 0;
3433
3434 /* Create the object: obj[0] = userdata.
3435 * Note that the base of the Fetches object is the
3436 * transaction object.
3437 */
3438 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003439 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003440 lua_rawseti(L, -2, 0);
3441
Willy Tarreau7073c472015-04-06 11:15:40 +02003442 hsmp->s = txn->s;
3443 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003444 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003445 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003446
3447 /* Pop a class sesison metatable and affect it to the userdata. */
3448 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3449 lua_setmetatable(L, -2);
3450
3451 return 1;
3452}
3453
3454/* This function is an LUA binding. It is called with each sample-fetch.
3455 * It uses closure argument to store the associated sample-fetch. It
3456 * returns only one argument or throws an error. An error is thrown
3457 * only if an error is encountered during the argument parsing. If
3458 * the "sample-fetch" function fails, nil is returned.
3459 */
3460__LJMP static int hlua_run_sample_fetch(lua_State *L)
3461{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003462 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003463 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003464 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003465 int i;
3466 struct sample smp;
3467
3468 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003469 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003470
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003471 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003472 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003473
Thierry FOURNIERca988662015-12-20 18:43:03 +01003474 /* Check execution authorization. */
3475 if (f->use & SMP_USE_HTTP_ANY &&
3476 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3477 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3478 "is not available in Lua services", f->kw);
3479 WILL_LJMP(lua_error(L));
3480 }
3481
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003482 /* Get extra arguments. */
3483 for (i = 0; i < lua_gettop(L) - 1; i++) {
3484 if (i >= ARGM_NBARGS)
3485 break;
3486 hlua_lua2arg(L, i + 2, &args[i]);
3487 }
3488 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003489 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003490
3491 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003492 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003493
3494 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003495 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003496 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003497 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003498 }
3499
3500 /* Initialise the sample. */
3501 memset(&smp, 0, sizeof(smp));
3502
3503 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003504 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003505 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003506 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003507 lua_pushstring(L, "");
3508 else
3509 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003510 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003511 }
3512
3513 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003514 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003515 hlua_smp2lua_str(L, &smp);
3516 else
3517 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003518
3519 end:
3520 for (i = 0; args[i].type != ARGT_STOP; i++) {
3521 if (args[i].type == ARGT_STR)
3522 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003523 else if (args[i].type == ARGT_REG)
3524 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003525 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003526 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003527
3528 error:
3529 for (i = 0; args[i].type != ARGT_STOP; i++) {
3530 if (args[i].type == ARGT_STR)
3531 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003532 else if (args[i].type == ARGT_REG)
3533 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003534 }
3535 WILL_LJMP(lua_error(L));
3536 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003537}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003538
3539/*
3540 *
3541 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003542 * Class Converters
3543 *
3544 *
3545 */
3546
3547/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003548 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003549 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003550__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003551{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003552 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003553}
3554
3555/* This function creates and push in the stack a Converters object
3556 * according with a current TXN.
3557 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003558static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003559{
Willy Tarreau7073c472015-04-06 11:15:40 +02003560 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003561
3562 /* Check stack size. */
3563 if (!lua_checkstack(L, 3))
3564 return 0;
3565
3566 /* Create the object: obj[0] = userdata.
3567 * Note that the base of the Converters object is the
3568 * same than the TXN object.
3569 */
3570 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003571 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003572 lua_rawseti(L, -2, 0);
3573
Willy Tarreau7073c472015-04-06 11:15:40 +02003574 hsmp->s = txn->s;
3575 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003576 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003577 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003578
Willy Tarreau87b09662015-04-03 00:22:06 +02003579 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003580 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3581 lua_setmetatable(L, -2);
3582
3583 return 1;
3584}
3585
3586/* This function is an LUA binding. It is called with each converter.
3587 * It uses closure argument to store the associated converter. It
3588 * returns only one argument or throws an error. An error is thrown
3589 * only if an error is encountered during the argument parsing. If
3590 * the converter function function fails, nil is returned.
3591 */
3592__LJMP static int hlua_run_sample_conv(lua_State *L)
3593{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003594 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003595 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003596 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003597 int i;
3598 struct sample smp;
3599
3600 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003601 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003602
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003603 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003604 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003605
3606 /* Get extra arguments. */
3607 for (i = 0; i < lua_gettop(L) - 2; i++) {
3608 if (i >= ARGM_NBARGS)
3609 break;
3610 hlua_lua2arg(L, i + 3, &args[i]);
3611 }
3612 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003613 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003614
3615 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003616 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003617
3618 /* Run the special args checker. */
3619 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3620 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003621 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003622 }
3623
3624 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003625 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003626 if (!hlua_lua2smp(L, 2, &smp)) {
3627 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003628 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003629 }
3630
Willy Tarreau1777ea62016-03-10 16:15:46 +01003631 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3632
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003633 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003634 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003635 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003636 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003637 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003638 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003639 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3640 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003641 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003642 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003643 }
3644
3645 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003646 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003647 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003648 lua_pushstring(L, "");
3649 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003650 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003651 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003652 }
3653
3654 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003655 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003656 hlua_smp2lua_str(L, &smp);
3657 else
3658 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003659 end:
3660 for (i = 0; args[i].type != ARGT_STOP; i++) {
3661 if (args[i].type == ARGT_STR)
3662 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003663 else if (args[i].type == ARGT_REG)
3664 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003665 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003666 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003667
3668 error:
3669 for (i = 0; args[i].type != ARGT_STOP; i++) {
3670 if (args[i].type == ARGT_STR)
3671 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003672 else if (args[i].type == ARGT_REG)
3673 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003674 }
3675 WILL_LJMP(lua_error(L));
3676 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003677}
3678
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003679/*
3680 *
3681 *
3682 * Class AppletTCP
3683 *
3684 *
3685 */
3686
3687/* Returns a struct hlua_txn if the stack entry "ud" is
3688 * a class stream, otherwise it throws an error.
3689 */
3690__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3691{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003692 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003693}
3694
3695/* This function creates and push in the stack an Applet object
3696 * according with a current TXN.
3697 */
3698static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3699{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003700 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003701 struct stream_interface *si = ctx->owner;
3702 struct stream *s = si_strm(si);
3703 struct proxy *p = s->be;
3704
3705 /* Check stack size. */
3706 if (!lua_checkstack(L, 3))
3707 return 0;
3708
3709 /* Create the object: obj[0] = userdata.
3710 * Note that the base of the Converters object is the
3711 * same than the TXN object.
3712 */
3713 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003714 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003715 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003716 luactx->appctx = ctx;
3717 luactx->htxn.s = s;
3718 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003719
3720 /* Create the "f" field that contains a list of fetches. */
3721 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003722 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003723 return 0;
3724 lua_settable(L, -3);
3725
3726 /* Create the "sf" field that contains a list of stringsafe fetches. */
3727 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003728 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003729 return 0;
3730 lua_settable(L, -3);
3731
3732 /* Create the "c" field that contains a list of converters. */
3733 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003734 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003735 return 0;
3736 lua_settable(L, -3);
3737
3738 /* Create the "sc" field that contains a list of stringsafe converters. */
3739 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003740 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003741 return 0;
3742 lua_settable(L, -3);
3743
3744 /* Pop a class stream metatable and affect it to the table. */
3745 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3746 lua_setmetatable(L, -2);
3747
3748 return 1;
3749}
3750
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003751__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3752{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003753 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003754 struct stream *s;
3755 const char *name;
3756 size_t len;
3757 struct sample smp;
3758
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003759 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3760 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003761
3762 /* It is useles to retrieve the stream, but this function
3763 * runs only in a stream context.
3764 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003765 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003766 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003767 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003768
3769 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003770 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003771 hlua_lua2smp(L, 3, &smp);
3772
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02003773 /* Store the sample in a variable. We don't need to dup the smp, vars API
3774 * already takes care of duplicating dynamic var data.
3775 */
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003776 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003777
3778 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3779 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3780 else
3781 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3782
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003783 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003784}
3785
3786__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3787{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003788 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003789 struct stream *s;
3790 const char *name;
3791 size_t len;
3792 struct sample smp;
3793
3794 MAY_LJMP(check_args(L, 2, "unset_var"));
3795
3796 /* It is useles to retrieve the stream, but this function
3797 * runs only in a stream context.
3798 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003799 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003800 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003801 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003802
3803 /* Unset the variable. */
3804 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003805 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3806 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003807}
3808
3809__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3810{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003811 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003812 struct stream *s;
3813 const char *name;
3814 size_t len;
3815 struct sample smp;
3816
3817 MAY_LJMP(check_args(L, 2, "get_var"));
3818
3819 /* It is useles to retrieve the stream, but this function
3820 * runs only in a stream context.
3821 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003822 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003823 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003824 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003825
3826 smp_set_owner(&smp, s->be, s->sess, s, 0);
3827 if (!vars_get_by_name(name, len, &smp)) {
3828 lua_pushnil(L);
3829 return 1;
3830 }
3831
3832 return hlua_smp2lua(L, &smp);
3833}
3834
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003835__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3836{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003837 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3838 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003839 struct hlua *hlua;
3840
3841 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003842 if (!s->hlua)
3843 return 0;
3844 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003845
3846 MAY_LJMP(check_args(L, 2, "set_priv"));
3847
3848 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003849 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003850
3851 /* Get and store new value. */
3852 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3853 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3854
3855 return 0;
3856}
3857
3858__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3859{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003860 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3861 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003862 struct hlua *hlua;
3863
3864 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003865 if (!s->hlua) {
3866 lua_pushnil(L);
3867 return 1;
3868 }
3869 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003870
3871 /* Push configuration index in the stack. */
3872 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3873
3874 return 1;
3875}
3876
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003877/* If expected data not yet available, it returns a yield. This function
3878 * consumes the data in the buffer. It returns a string containing the
3879 * data. This string can be empty.
3880 */
3881__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3882{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003883 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3884 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003885 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003886 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003887 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003888 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003889 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003890
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003891 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003892 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003893
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003894 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003895 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003896 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003897 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003898 }
3899
3900 /* End of data: commit the total strings and return. */
3901 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003902 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003903 return 1;
3904 }
3905
3906 /* Ensure that the block 2 length is usable. */
3907 if (ret == 1)
3908 len2 = 0;
3909
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003910 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003911 luaL_addlstring(&luactx->b, blk1, len1);
3912 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003913
3914 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003915 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003916 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003917 return 1;
3918}
3919
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003920/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003921__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3922{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003923 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003924
3925 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003926 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003927
3928 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3929}
3930
3931/* If expected data not yet available, it returns a yield. This function
3932 * consumes the data in the buffer. It returns a string containing the
3933 * data. This string can be empty.
3934 */
3935__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3936{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003937 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3938 struct stream_interface *si = luactx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003939 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003940 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003941 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003942 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003943 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003944 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003945
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003946 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003947 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003948
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003949 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003950 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003951 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003952 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003953 }
3954
3955 /* End of data: commit the total strings and return. */
3956 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003957 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003958 return 1;
3959 }
3960
3961 /* Ensure that the block 2 length is usable. */
3962 if (ret == 1)
3963 len2 = 0;
3964
3965 if (len == -1) {
3966
3967 /* If len == -1, catenate all the data avalaile and
3968 * yield because we want to get all the data until
3969 * the end of data stream.
3970 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003971 luaL_addlstring(&luactx->b, blk1, len1);
3972 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003973 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003974 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003975 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003976
3977 } else {
3978
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003979 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003980 if (len1 > len)
3981 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02003982 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003983 len -= len1;
3984
3985 /* Copy the second block. */
3986 if (len2 > len)
3987 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02003988 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003989 len -= len2;
3990
3991 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003992 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003993
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003994 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003995 if (len > 0) {
3996 lua_pushinteger(L, len);
3997 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003998 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003999 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004000 }
4001
4002 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004003 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004004 return 1;
4005 }
4006
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004007 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004008 hlua_pusherror(L, "Lua: internal error");
4009 WILL_LJMP(lua_error(L));
4010 return 0;
4011}
4012
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004013/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004014__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4015{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004016 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004017 int len = -1;
4018
4019 if (lua_gettop(L) > 2)
4020 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4021 if (lua_gettop(L) >= 2) {
4022 len = MAY_LJMP(luaL_checkinteger(L, 2));
4023 lua_pop(L, 1);
4024 }
4025
4026 /* Confirm or set the required length */
4027 lua_pushinteger(L, len);
4028
4029 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004030 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004031
4032 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4033}
4034
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004035/* Append data in the output side of the buffer. This data is immediately
4036 * sent. The function returns the amount of data written. If the buffer
4037 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004038 * if the channel is closed.
4039 */
4040__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4041{
4042 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004043 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004044 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4045 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004046 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004047 struct channel *chn = si_ic(si);
4048 int max;
4049
4050 /* Get the max amount of data which can write as input in the channel. */
4051 max = channel_recv_max(chn);
4052 if (max > (len - l))
4053 max = len - l;
4054
4055 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004056 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004057
4058 /* update counters. */
4059 l += max;
4060 lua_pop(L, 1);
4061 lua_pushinteger(L, l);
4062
4063 /* If some data is not send, declares the situation to the
4064 * applet, and returns a yield.
4065 */
4066 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004067 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004068 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004069 }
4070
4071 return 1;
4072}
4073
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004074/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004075 * yield the LUA process, and resume it without checking the
4076 * input arguments.
4077 */
4078__LJMP static int hlua_applet_tcp_send(lua_State *L)
4079{
4080 MAY_LJMP(check_args(L, 2, "send"));
4081 lua_pushinteger(L, 0);
4082
4083 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4084}
4085
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004086/*
4087 *
4088 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004089 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004090 *
4091 *
4092 */
4093
4094/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004095 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004096 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004097__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004098{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004099 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004100}
4101
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004102/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004103 * according with a current TXN.
4104 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004105static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004106{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004107 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004108 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004109 struct stream_interface *si = ctx->owner;
4110 struct stream *s = si_strm(si);
4111 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004112 struct htx *htx;
4113 struct htx_blk *blk;
4114 struct htx_sl *sl;
4115 struct ist path;
4116 unsigned long long len = 0;
4117 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004118
4119 /* Check stack size. */
4120 if (!lua_checkstack(L, 3))
4121 return 0;
4122
4123 /* Create the object: obj[0] = userdata.
4124 * Note that the base of the Converters object is the
4125 * same than the TXN object.
4126 */
4127 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004128 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004129 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004130 luactx->appctx = ctx;
4131 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4132 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4133 luactx->htxn.s = s;
4134 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004135
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004136 /* Create the "f" field that contains a list of fetches. */
4137 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004138 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004139 return 0;
4140 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004141
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004142 /* Create the "sf" field that contains a list of stringsafe fetches. */
4143 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004144 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004145 return 0;
4146 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004147
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004148 /* Create the "c" field that contains a list of converters. */
4149 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004150 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004151 return 0;
4152 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004153
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004154 /* Create the "sc" field that contains a list of stringsafe converters. */
4155 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004156 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004157 return 0;
4158 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004159
Christopher Fauleta2097962019-07-15 16:25:33 +02004160 htx = htxbuf(&s->req.buf);
4161 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004162 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004163 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004164
Christopher Fauleta2097962019-07-15 16:25:33 +02004165 /* Stores the request method. */
4166 lua_pushstring(L, "method");
4167 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4168 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004169
Christopher Fauleta2097962019-07-15 16:25:33 +02004170 /* Stores the http version. */
4171 lua_pushstring(L, "version");
4172 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4173 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004174
Christopher Fauleta2097962019-07-15 16:25:33 +02004175 /* creates an array of headers. hlua_http_get_headers() crates and push
4176 * the array on the top of the stack.
4177 */
4178 lua_pushstring(L, "headers");
4179 htxn.s = s;
4180 htxn.p = px;
4181 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004182 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004183 return 0;
4184 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004185
Christopher Fauleta2097962019-07-15 16:25:33 +02004186 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004187 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004188 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004189
Christopher Fauleta2097962019-07-15 16:25:33 +02004190 p = path.ptr;
4191 end = path.ptr + path.len;
4192 q = p;
4193 while (q < end && *q != '?')
4194 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004195
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004196 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004197 lua_pushstring(L, "path");
4198 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004199 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004200
Christopher Fauleta2097962019-07-15 16:25:33 +02004201 /* Stores the query string. */
4202 lua_pushstring(L, "qs");
4203 if (*q == '?')
4204 q++;
4205 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004206 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004207 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004208
Christopher Fauleta2097962019-07-15 16:25:33 +02004209 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4210 struct htx_blk *blk = htx_get_blk(htx, pos);
4211 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004212
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004213 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004214 break;
4215 if (type == HTX_BLK_DATA)
4216 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004217 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004218 if (htx->extra != ULLONG_MAX)
4219 len += htx->extra;
4220
4221 /* Stores the request path. */
4222 lua_pushstring(L, "length");
4223 lua_pushinteger(L, len);
4224 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004225
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004226 /* Create an empty array of HTTP request headers. */
4227 lua_pushstring(L, "response");
4228 lua_newtable(L);
4229 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004230
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004231 /* Pop a class stream metatable and affect it to the table. */
4232 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4233 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004234
4235 return 1;
4236}
4237
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004238__LJMP static int hlua_applet_http_set_var(lua_State *L)
4239{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004240 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004241 struct stream *s;
4242 const char *name;
4243 size_t len;
4244 struct sample smp;
4245
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004246 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4247 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004248
4249 /* It is useles to retrieve the stream, but this function
4250 * runs only in a stream context.
4251 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004252 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004253 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004254 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004255
4256 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004257 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004258 hlua_lua2smp(L, 3, &smp);
4259
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02004260 /* Store the sample in a variable. We don't need to dup the smp, vars API
4261 * already takes care of duplicating dynamic var data.
4262 */
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004263 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004264
4265 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4266 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4267 else
4268 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4269
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004270 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004271}
4272
4273__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4274{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004275 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004276 struct stream *s;
4277 const char *name;
4278 size_t len;
4279 struct sample smp;
4280
4281 MAY_LJMP(check_args(L, 2, "unset_var"));
4282
4283 /* It is useles to retrieve the stream, but this function
4284 * runs only in a stream context.
4285 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004286 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004287 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004288 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004289
4290 /* Unset the variable. */
4291 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004292 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4293 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004294}
4295
4296__LJMP static int hlua_applet_http_get_var(lua_State *L)
4297{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004298 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004299 struct stream *s;
4300 const char *name;
4301 size_t len;
4302 struct sample smp;
4303
4304 MAY_LJMP(check_args(L, 2, "get_var"));
4305
4306 /* It is useles to retrieve the stream, but this function
4307 * runs only in a stream context.
4308 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004309 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004310 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004311 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004312
4313 smp_set_owner(&smp, s->be, s->sess, s, 0);
4314 if (!vars_get_by_name(name, len, &smp)) {
4315 lua_pushnil(L);
4316 return 1;
4317 }
4318
4319 return hlua_smp2lua(L, &smp);
4320}
4321
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004322__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4323{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004324 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4325 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004326 struct hlua *hlua;
4327
4328 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004329 if (!s->hlua)
4330 return 0;
4331 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004332
4333 MAY_LJMP(check_args(L, 2, "set_priv"));
4334
4335 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004336 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004337
4338 /* Get and store new value. */
4339 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4340 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4341
4342 return 0;
4343}
4344
4345__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4346{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004347 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4348 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004349 struct hlua *hlua;
4350
4351 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004352 if (!s->hlua) {
4353 lua_pushnil(L);
4354 return 1;
4355 }
4356 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004357
4358 /* Push configuration index in the stack. */
4359 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4360
4361 return 1;
4362}
4363
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004364/* If expected data not yet available, it returns a yield. This function
4365 * consumes the data in the buffer. It returns a string containing the
4366 * data. This string can be empty.
4367 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004368__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004369{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004370 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4371 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004372 struct channel *req = si_oc(si);
4373 struct htx *htx;
4374 struct htx_blk *blk;
4375 size_t count;
4376 int stop = 0;
4377
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004378 htx = htx_from_buf(&req->buf);
4379 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004380 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004381
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004382 while (count && !stop && blk) {
4383 enum htx_blk_type type = htx_get_blk_type(blk);
4384 uint32_t sz = htx_get_blksz(blk);
4385 struct ist v;
4386 uint32_t vlen;
4387 char *nl;
4388
4389 vlen = sz;
4390 if (vlen > count) {
4391 if (type != HTX_BLK_DATA)
4392 break;
4393 vlen = count;
4394 }
4395
4396 switch (type) {
4397 case HTX_BLK_UNUSED:
4398 break;
4399
4400 case HTX_BLK_DATA:
4401 v = htx_get_blk_value(htx, blk);
4402 v.len = vlen;
4403 nl = istchr(v, '\n');
4404 if (nl != NULL) {
4405 stop = 1;
4406 vlen = nl - v.ptr + 1;
4407 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004408 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004409 break;
4410
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004411 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004412 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004413 stop = 1;
4414 break;
4415
4416 default:
4417 break;
4418 }
4419
4420 co_set_data(req, co_data(req) - vlen);
4421 count -= vlen;
4422 if (sz == vlen)
4423 blk = htx_remove_blk(htx, blk);
4424 else {
4425 htx_cut_data_blk(htx, blk, vlen);
4426 break;
4427 }
4428 }
4429
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004430 /* The message was fully consumed and no more data are expected
4431 * (EOM flag set).
4432 */
Christopher Fauleta5a25b12022-08-29 15:37:16 +02004433 if (htx_is_empty(htx) && (req->flags & CF_EOI))
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004434 stop = 1;
4435
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004436 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004437 if (!stop) {
4438 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004439 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004440 }
4441
4442 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004443 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004444 return 1;
4445}
4446
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004447
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004448/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004449__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004450{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004451 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004452
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004453 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004454 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004455
Christopher Fauleta2097962019-07-15 16:25:33 +02004456 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004457}
4458
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004459/* If expected data not yet available, it returns a yield. This function
4460 * consumes the data in the buffer. It returns a string containing the
4461 * data. This string can be empty.
4462 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004463__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004464{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004465 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4466 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004467 struct channel *req = si_oc(si);
4468 struct htx *htx;
4469 struct htx_blk *blk;
4470 size_t count;
4471 int len;
4472
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004473 htx = htx_from_buf(&req->buf);
4474 len = MAY_LJMP(luaL_checkinteger(L, 2));
4475 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004476 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004477 while (count && len && blk) {
4478 enum htx_blk_type type = htx_get_blk_type(blk);
4479 uint32_t sz = htx_get_blksz(blk);
4480 struct ist v;
4481 uint32_t vlen;
4482
4483 vlen = sz;
4484 if (len > 0 && vlen > len)
4485 vlen = len;
4486 if (vlen > count) {
4487 if (type != HTX_BLK_DATA)
4488 break;
4489 vlen = count;
4490 }
4491
4492 switch (type) {
4493 case HTX_BLK_UNUSED:
4494 break;
4495
4496 case HTX_BLK_DATA:
4497 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004498 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004499 break;
4500
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004501 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004502 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004503 len = 0;
4504 break;
4505
4506 default:
4507 break;
4508 }
4509
4510 co_set_data(req, co_data(req) - vlen);
4511 count -= vlen;
4512 if (len > 0)
4513 len -= vlen;
4514 if (sz == vlen)
4515 blk = htx_remove_blk(htx, blk);
4516 else {
4517 htx_cut_data_blk(htx, blk, vlen);
4518 break;
4519 }
4520 }
4521
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004522 /* The message was fully consumed and no more data are expected
4523 * (EOM flag set).
4524 */
Christopher Fauleta5a25b12022-08-29 15:37:16 +02004525 if (htx_is_empty(htx) && (req->flags & CF_EOI))
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004526 len = 0;
4527
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004528 htx_to_buf(htx, &req->buf);
4529
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004530 /* If we are no other data available, yield waiting for new data. */
4531 if (len) {
4532 if (len > 0) {
4533 lua_pushinteger(L, len);
4534 lua_replace(L, 2);
4535 }
4536 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004537 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004538 }
4539
4540 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004541 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004542 return 1;
4543}
4544
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004545/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004546__LJMP static int hlua_applet_http_recv(lua_State *L)
4547{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004548 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004549 int len = -1;
4550
4551 /* Check arguments. */
4552 if (lua_gettop(L) > 2)
4553 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4554 if (lua_gettop(L) >= 2) {
4555 len = MAY_LJMP(luaL_checkinteger(L, 2));
4556 lua_pop(L, 1);
4557 }
4558
Christopher Fauleta2097962019-07-15 16:25:33 +02004559 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004560
Christopher Fauleta2097962019-07-15 16:25:33 +02004561 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004562 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004563
Christopher Fauleta2097962019-07-15 16:25:33 +02004564 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004565}
4566
4567/* Append data in the output side of the buffer. This data is immediately
4568 * sent. The function returns the amount of data written. If the buffer
4569 * cannot contain the data, the function yields. The function returns -1
4570 * if the channel is closed.
4571 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004572__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004573{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004574 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4575 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004576 struct channel *res = si_ic(si);
4577 struct htx *htx = htx_from_buf(&res->buf);
4578 const char *data;
4579 size_t len;
4580 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4581 int max;
4582
Christopher Faulet9060fc02019-07-03 11:39:30 +02004583 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004584 if (!max)
4585 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004586
4587 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4588
4589 /* Get the max amount of data which can write as input in the channel. */
4590 if (max > (len - l))
4591 max = len - l;
4592
4593 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004594 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004595 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004596
4597 /* update counters. */
4598 l += max;
4599 lua_pop(L, 1);
4600 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004601
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004602 /* If some data is not send, declares the situation to the
4603 * applet, and returns a yield.
4604 */
4605 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004606 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004607 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004608 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004609 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004610 }
4611
Christopher Fauleta2097962019-07-15 16:25:33 +02004612 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004613 return 1;
4614}
4615
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004616/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004617 * yield the LUA process, and resume it without checking the
4618 * input arguments.
4619 */
4620__LJMP static int hlua_applet_http_send(lua_State *L)
4621{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004622 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004623
4624 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004625 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004626 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4627 WILL_LJMP(lua_error(L));
4628 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004629
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004630 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004631 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004632
Christopher Fauleta2097962019-07-15 16:25:33 +02004633 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004634}
4635
4636__LJMP static int hlua_applet_http_addheader(lua_State *L)
4637{
4638 const char *name;
4639 int ret;
4640
4641 MAY_LJMP(hlua_checkapplet_http(L, 1));
4642 name = MAY_LJMP(luaL_checkstring(L, 2));
4643 MAY_LJMP(luaL_checkstring(L, 3));
4644
4645 /* Push in the stack the "response" entry. */
4646 ret = lua_getfield(L, 1, "response");
4647 if (ret != LUA_TTABLE) {
4648 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4649 "is expected as an array. %s found", lua_typename(L, ret));
4650 WILL_LJMP(lua_error(L));
4651 }
4652
4653 /* check if the header is already registered if it is not
4654 * the case, register it.
4655 */
4656 ret = lua_getfield(L, -1, name);
4657 if (ret == LUA_TNIL) {
4658
4659 /* Entry not found. */
4660 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4661
4662 /* Insert the new header name in the array in the top of the stack.
4663 * It left the new array in the top of the stack.
4664 */
4665 lua_newtable(L);
4666 lua_pushvalue(L, 2);
4667 lua_pushvalue(L, -2);
4668 lua_settable(L, -4);
4669
4670 } else if (ret != LUA_TTABLE) {
4671
4672 /* corruption error. */
4673 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4674 "is expected as an array. %s found", name, lua_typename(L, ret));
4675 WILL_LJMP(lua_error(L));
4676 }
4677
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004678 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004679 * the header value as new entry.
4680 */
4681 lua_pushvalue(L, 3);
4682 ret = lua_rawlen(L, -2);
4683 lua_rawseti(L, -2, ret + 1);
4684 lua_pushboolean(L, 1);
4685 return 1;
4686}
4687
4688__LJMP static int hlua_applet_http_status(lua_State *L)
4689{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004690 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004691 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004692 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004693
4694 if (status < 100 || status > 599) {
4695 lua_pushboolean(L, 0);
4696 return 1;
4697 }
4698
Willy Tarreau7e702d12021-04-28 17:59:21 +02004699 luactx->appctx->ctx.hlua_apphttp.status = status;
4700 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004701 lua_pushboolean(L, 1);
4702 return 1;
4703}
4704
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004705
Christopher Fauleta2097962019-07-15 16:25:33 +02004706__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004707{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004708 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4709 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004710 struct channel *res = si_ic(si);
4711 struct htx *htx;
4712 struct htx_sl *sl;
4713 struct h1m h1m;
4714 const char *status, *reason;
4715 const char *name, *value;
4716 size_t nlen, vlen;
4717 unsigned int flags;
4718
4719 /* Send the message at once. */
4720 htx = htx_from_buf(&res->buf);
4721 h1m_init_res(&h1m);
4722
4723 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004724 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4725 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004726 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02004727 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
4728 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004729 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4730 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4731 }
4732 else {
4733 flags = HTX_SL_F_IS_RESP;
4734 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4735 }
4736 if (!sl) {
4737 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004738 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004739 WILL_LJMP(lua_error(L));
4740 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004741 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004742
4743 /* Get the array associated to the field "response" in the object AppletHTTP. */
4744 lua_pushvalue(L, 0);
4745 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4746 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004747 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004748 WILL_LJMP(lua_error(L));
4749 }
4750
4751 /* Browse the list of headers. */
4752 lua_pushnil(L);
4753 while(lua_next(L, -2) != 0) {
4754 /* We expect a string as -2. */
4755 if (lua_type(L, -2) != LUA_TSTRING) {
4756 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004757 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004758 lua_typename(L, lua_type(L, -2)));
4759 WILL_LJMP(lua_error(L));
4760 }
4761 name = lua_tolstring(L, -2, &nlen);
4762
4763 /* We expect an array as -1. */
4764 if (lua_type(L, -1) != LUA_TTABLE) {
4765 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 +02004766 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004767 name,
4768 lua_typename(L, lua_type(L, -1)));
4769 WILL_LJMP(lua_error(L));
4770 }
4771
4772 /* Browse the table who is on the top of the stack. */
4773 lua_pushnil(L);
4774 while(lua_next(L, -2) != 0) {
4775 int id;
4776
4777 /* We expect a number as -2. */
4778 if (lua_type(L, -2) != LUA_TNUMBER) {
4779 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 +02004780 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004781 name,
4782 lua_typename(L, lua_type(L, -2)));
4783 WILL_LJMP(lua_error(L));
4784 }
4785 id = lua_tointeger(L, -2);
4786
4787 /* We expect a string as -2. */
4788 if (lua_type(L, -1) != LUA_TSTRING) {
4789 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 +02004790 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004791 name, id,
4792 lua_typename(L, lua_type(L, -1)));
4793 WILL_LJMP(lua_error(L));
4794 }
4795 value = lua_tolstring(L, -1, &vlen);
4796
4797 /* Simple Protocol checks. */
4798 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004799 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004800 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4801 struct ist v = ist2(value, vlen);
4802 int ret;
4803
4804 ret = h1_parse_cont_len_header(&h1m, &v);
4805 if (ret < 0) {
4806 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004807 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004808 name);
4809 WILL_LJMP(lua_error(L));
4810 }
4811 else if (ret == 0)
4812 goto next; /* Skip it */
4813 }
4814
4815 /* Add a new header */
4816 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4817 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004818 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004819 name);
4820 WILL_LJMP(lua_error(L));
4821 }
4822 next:
4823 /* Remove the array from the stack, and get next element with a remaining string. */
4824 lua_pop(L, 1);
4825 }
4826
4827 /* Remove the array from the stack, and get next element with a remaining string. */
4828 lua_pop(L, 1);
4829 }
4830
4831 if (h1m.flags & H1_MF_CHNK)
4832 h1m.flags &= ~H1_MF_CLEN;
4833 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4834 h1m.flags |= H1_MF_XFER_LEN;
4835
4836 /* Uset HTX start-line flags */
4837 if (h1m.flags & H1_MF_XFER_ENC)
4838 flags |= HTX_SL_F_XFER_ENC;
4839 if (h1m.flags & H1_MF_XFER_LEN) {
4840 flags |= HTX_SL_F_XFER_LEN;
4841 if (h1m.flags & H1_MF_CHNK)
4842 flags |= HTX_SL_F_CHNK;
4843 else if (h1m.flags & H1_MF_CLEN)
4844 flags |= HTX_SL_F_CLEN;
4845 if (h1m.body_len == 0)
4846 flags |= HTX_SL_F_BODYLESS;
4847 }
4848 sl->flags |= flags;
4849
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004850 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004851 * and the status code implies the presence of a message body, we must
4852 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004853 * for the keepalive compliance. If the applet announces a transfer-encoding
4854 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004855 */
4856 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02004857 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
4858 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
4859 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004860 /* Add a new header */
4861 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4862 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4863 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' 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 WILL_LJMP(lua_error(L));
4866 }
4867 }
4868
4869 /* Finalize headers. */
4870 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4871 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004872 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004873 WILL_LJMP(lua_error(L));
4874 }
4875
4876 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4877 b_reset(&res->buf);
4878 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4879 WILL_LJMP(lua_error(L));
4880 }
4881
4882 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004883 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004884
4885 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004886 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004887 return 0;
4888
4889}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004890/* We will build the status line and the headers of the HTTP response.
4891 * We will try send at once if its not possible, we give back the hand
4892 * waiting for more room.
4893 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004894__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004895{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004896 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4897 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004898 struct channel *res = si_ic(si);
4899
4900 if (co_data(res)) {
4901 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004902 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004903 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004904 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004905}
4906
4907
Christopher Fauleta2097962019-07-15 16:25:33 +02004908__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004909{
Christopher Fauleta2097962019-07-15 16:25:33 +02004910 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004911}
4912
Christopher Fauleta2097962019-07-15 16:25:33 +02004913/*
4914 *
4915 *
4916 * Class HTTP
4917 *
4918 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004919 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004920
4921/* Returns a struct hlua_txn if the stack entry "ud" is
4922 * a class stream, otherwise it throws an error.
4923 */
4924__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004925{
Christopher Fauleta2097962019-07-15 16:25:33 +02004926 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4927}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004928
Christopher Fauleta2097962019-07-15 16:25:33 +02004929/* This function creates and push in the stack a HTTP object
4930 * according with a current TXN.
4931 */
4932static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4933{
4934 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004935
Christopher Fauleta2097962019-07-15 16:25:33 +02004936 /* Check stack size. */
4937 if (!lua_checkstack(L, 3))
4938 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004939
Christopher Fauleta2097962019-07-15 16:25:33 +02004940 /* Create the object: obj[0] = userdata.
4941 * Note that the base of the Converters object is the
4942 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004943 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004944 lua_newtable(L);
4945 htxn = lua_newuserdata(L, sizeof(*htxn));
4946 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004947
4948 htxn->s = txn->s;
4949 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004950 htxn->dir = txn->dir;
4951 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004952
4953 /* Pop a class stream metatable and affect it to the table. */
4954 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4955 lua_setmetatable(L, -2);
4956
4957 return 1;
4958}
4959
4960/* This function creates ans returns an array of HTTP headers.
4961 * This function does not fails. It is used as wrapper with the
4962 * 2 following functions.
4963 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004964__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004965{
Christopher Fauleta2097962019-07-15 16:25:33 +02004966 struct htx *htx;
4967 int32_t pos;
4968
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004969 /* Create the table. */
4970 lua_newtable(L);
4971
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004972
Christopher Fauleta2097962019-07-15 16:25:33 +02004973 htx = htxbuf(&msg->chn->buf);
4974 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4975 struct htx_blk *blk = htx_get_blk(htx, pos);
4976 enum htx_blk_type type = htx_get_blk_type(blk);
4977 struct ist n, v;
4978 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004979
Christopher Fauleta2097962019-07-15 16:25:33 +02004980 if (type == HTX_BLK_HDR) {
4981 n = htx_get_blk_name(htx,blk);
4982 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004983 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004984 else if (type == HTX_BLK_EOH)
4985 break;
4986 else
4987 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004988
Christopher Fauleta2097962019-07-15 16:25:33 +02004989 /* Check for existing entry:
4990 * assume that the table is on the top of the stack, and
4991 * push the key in the stack, the function lua_gettable()
4992 * perform the lookup.
4993 */
4994 lua_pushlstring(L, n.ptr, n.len);
4995 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004996
Christopher Fauleta2097962019-07-15 16:25:33 +02004997 switch (lua_type(L, -1)) {
4998 case LUA_TNIL:
4999 /* Table not found, create it. */
5000 lua_pop(L, 1); /* remove the nil value. */
5001 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5002 lua_newtable(L); /* create and push empty table. */
5003 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5004 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5005 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005006 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005007
Christopher Fauleta2097962019-07-15 16:25:33 +02005008 case LUA_TTABLE:
5009 /* Entry found: push the value in the table. */
5010 len = lua_rawlen(L, -1);
5011 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5012 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5013 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5014 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005015
Christopher Fauleta2097962019-07-15 16:25:33 +02005016 default:
5017 /* Other cases are errors. */
5018 hlua_pusherror(L, "internal error during the parsing of headers.");
5019 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005020 }
5021 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005022 return 1;
5023}
5024
5025__LJMP static int hlua_http_req_get_headers(lua_State *L)
5026{
5027 struct hlua_txn *htxn;
5028
5029 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5030 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5031
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005032 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005033 WILL_LJMP(lua_error(L));
5034
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005035 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005036}
5037
5038__LJMP static int hlua_http_res_get_headers(lua_State *L)
5039{
5040 struct hlua_txn *htxn;
5041
5042 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5043 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5044
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005045 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005046 WILL_LJMP(lua_error(L));
5047
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005048 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005049}
5050
5051/* This function replace full header, or just a value in
5052 * the request or in the response. It is a wrapper fir the
5053 * 4 following functions.
5054 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005055__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005056{
5057 size_t name_len;
5058 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5059 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5060 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005061 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005062 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005063
Dragan Dosen26743032019-04-30 15:54:36 +02005064 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005065 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5066
Christopher Fauleta2097962019-07-15 16:25:33 +02005067 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005068 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005069 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005070 return 0;
5071}
5072
5073__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5074{
5075 struct hlua_txn *htxn;
5076
5077 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5078 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5079
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005080 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005081 WILL_LJMP(lua_error(L));
5082
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005083 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005084}
5085
5086__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5087{
5088 struct hlua_txn *htxn;
5089
5090 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5091 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5092
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005093 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005094 WILL_LJMP(lua_error(L));
5095
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005096 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005097}
5098
5099__LJMP static int hlua_http_req_rep_val(lua_State *L)
5100{
5101 struct hlua_txn *htxn;
5102
5103 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5104 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5105
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005106 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005107 WILL_LJMP(lua_error(L));
5108
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005109 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005110}
5111
5112__LJMP static int hlua_http_res_rep_val(lua_State *L)
5113{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005114 struct hlua_txn *htxn;
5115
5116 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5117 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5118
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005119 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005120 WILL_LJMP(lua_error(L));
5121
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005122 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005123}
5124
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005125/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005126 * It is a wrapper for the 2 following functions.
5127 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005128__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005129{
5130 size_t len;
5131 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005132 struct htx *htx = htxbuf(&msg->chn->buf);
5133 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005134
Christopher Fauleta2097962019-07-15 16:25:33 +02005135 ctx.blk = NULL;
5136 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5137 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005138 return 0;
5139}
5140
5141__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5142{
5143 struct hlua_txn *htxn;
5144
5145 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5146 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5147
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005148 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005149 WILL_LJMP(lua_error(L));
5150
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005151 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005152}
5153
5154__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5155{
5156 struct hlua_txn *htxn;
5157
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005158 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005159 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5160
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005161 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005162 WILL_LJMP(lua_error(L));
5163
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005164 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005165}
5166
5167/* This function adds an header. It is a wrapper used by
5168 * the 2 following functions.
5169 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005170__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005171{
5172 size_t name_len;
5173 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5174 size_t value_len;
5175 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005176 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005177
Christopher Fauleta2097962019-07-15 16:25:33 +02005178 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5179 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005180 return 0;
5181}
5182
5183__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5184{
5185 struct hlua_txn *htxn;
5186
5187 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5188 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5189
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005190 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005191 WILL_LJMP(lua_error(L));
5192
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005193 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005194}
5195
5196__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5197{
5198 struct hlua_txn *htxn;
5199
5200 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5201 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5202
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005203 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005204 WILL_LJMP(lua_error(L));
5205
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005206 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005207}
5208
5209static int hlua_http_req_set_hdr(lua_State *L)
5210{
5211 struct hlua_txn *htxn;
5212
5213 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5214 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5215
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005216 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005217 WILL_LJMP(lua_error(L));
5218
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005219 hlua_http_del_hdr(L, &htxn->s->txn->req);
5220 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005221}
5222
5223static int hlua_http_res_set_hdr(lua_State *L)
5224{
5225 struct hlua_txn *htxn;
5226
5227 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5228 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5229
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005230 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005231 WILL_LJMP(lua_error(L));
5232
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005233 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5234 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005235}
5236
5237/* This function set the method. */
5238static int hlua_http_req_set_meth(lua_State *L)
5239{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005240 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005241 size_t name_len;
5242 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005243
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005244 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005245 WILL_LJMP(lua_error(L));
5246
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005247 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005248 return 1;
5249}
5250
5251/* This function set the method. */
5252static int hlua_http_req_set_path(lua_State *L)
5253{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005254 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005255 size_t name_len;
5256 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005257
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005258 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005259 WILL_LJMP(lua_error(L));
5260
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005261 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005262 return 1;
5263}
5264
5265/* This function set the query-string. */
5266static int hlua_http_req_set_query(lua_State *L)
5267{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005268 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005269 size_t name_len;
5270 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005271
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005272 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005273 WILL_LJMP(lua_error(L));
5274
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005275 /* Check length. */
5276 if (name_len > trash.size - 1) {
5277 lua_pushboolean(L, 0);
5278 return 1;
5279 }
5280
5281 /* Add the mark question as prefix. */
5282 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005283 trash.area[trash.data++] = '?';
5284 memcpy(trash.area + trash.data, name, name_len);
5285 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005286
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005287 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005288 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005289 return 1;
5290}
5291
5292/* This function set the uri. */
5293static int hlua_http_req_set_uri(lua_State *L)
5294{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005295 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005296 size_t name_len;
5297 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005298
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005299 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005300 WILL_LJMP(lua_error(L));
5301
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005302 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005303 return 1;
5304}
5305
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005306/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005307static int hlua_http_res_set_status(lua_State *L)
5308{
5309 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5310 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005311 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5312 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005313
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005314 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005315 WILL_LJMP(lua_error(L));
5316
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005317 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005318 return 0;
5319}
5320
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005321/*
5322 *
5323 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005324 * Class TXN
5325 *
5326 *
5327 */
5328
5329/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005330 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005331 */
5332__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5333{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005334 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005335}
5336
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005337__LJMP static int hlua_set_var(lua_State *L)
5338{
5339 struct hlua_txn *htxn;
5340 const char *name;
5341 size_t len;
5342 struct sample smp;
5343
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005344 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5345 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005346
5347 /* It is useles to retrieve the stream, but this function
5348 * runs only in a stream context.
5349 */
5350 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5351 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5352
5353 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005354 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005355 hlua_lua2smp(L, 3, &smp);
5356
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02005357 /* Store the sample in a variable. We don't need to dup the smp, vars API
5358 * already takes care of duplicating dynamic var data.
5359 */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005360 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005361
5362 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5363 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5364 else
5365 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5366
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005367 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005368}
5369
Christopher Faulet85d79c92016-11-09 16:54:56 +01005370__LJMP static int hlua_unset_var(lua_State *L)
5371{
5372 struct hlua_txn *htxn;
5373 const char *name;
5374 size_t len;
5375 struct sample smp;
5376
5377 MAY_LJMP(check_args(L, 2, "unset_var"));
5378
5379 /* It is useles to retrieve the stream, but this function
5380 * runs only in a stream context.
5381 */
5382 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5383 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5384
5385 /* Unset the variable. */
5386 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005387 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5388 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005389}
5390
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005391__LJMP static int hlua_get_var(lua_State *L)
5392{
5393 struct hlua_txn *htxn;
5394 const char *name;
5395 size_t len;
5396 struct sample smp;
5397
5398 MAY_LJMP(check_args(L, 2, "get_var"));
5399
5400 /* It is useles to retrieve the stream, but this function
5401 * runs only in a stream context.
5402 */
5403 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5404 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
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);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005407 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005408 lua_pushnil(L);
5409 return 1;
5410 }
5411
5412 return hlua_smp2lua(L, &smp);
5413}
5414
Willy Tarreau59551662015-03-10 14:23:13 +01005415__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005416{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005417 struct hlua *hlua;
5418
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005419 MAY_LJMP(check_args(L, 2, "set_priv"));
5420
Willy Tarreau87b09662015-04-03 00:22:06 +02005421 /* It is useles to retrieve the stream, but this function
5422 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005423 */
5424 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005425
5426 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005427 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005428 if (!hlua)
5429 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005430
5431 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005432 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005433
5434 /* Get and store new value. */
5435 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5436 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5437
5438 return 0;
5439}
5440
Willy Tarreau59551662015-03-10 14:23:13 +01005441__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005442{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005443 struct hlua *hlua;
5444
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005445 MAY_LJMP(check_args(L, 1, "get_priv"));
5446
Willy Tarreau87b09662015-04-03 00:22:06 +02005447 /* It is useles to retrieve the stream, but this function
5448 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005449 */
5450 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005451
5452 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005453 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005454 if (!hlua) {
5455 lua_pushnil(L);
5456 return 1;
5457 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005458
5459 /* Push configuration index in the stack. */
5460 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5461
5462 return 1;
5463}
5464
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005465/* Create stack entry containing a class TXN. This function
5466 * return 0 if the stack does not contains free slots,
5467 * otherwise it returns 1.
5468 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005469static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005470{
Willy Tarreaude491382015-04-06 11:04:28 +02005471 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005472
5473 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005474 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005475 return 0;
5476
5477 /* NOTE: The allocation never fails. The failure
5478 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005479 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005480 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005481 /* Create the object: obj[0] = userdata. */
5482 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005483 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005484 lua_rawseti(L, -2, 0);
5485
Willy Tarreaude491382015-04-06 11:04:28 +02005486 htxn->s = s;
5487 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005488 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005489 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005490
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005491 /* Create the "f" field that contains a list of fetches. */
5492 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005493 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005494 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005495 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005496
5497 /* Create the "sf" field that contains a list of stringsafe fetches. */
5498 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005499 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005500 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005501 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005502
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005503 /* Create the "c" field that contains a list of converters. */
5504 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005505 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005506 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005507 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005508
5509 /* Create the "sc" field that contains a list of stringsafe converters. */
5510 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005511 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005512 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005513 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005514
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005515 /* Create the "req" field that contains the request channel object. */
5516 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005517 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005518 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005519 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005520
5521 /* Create the "res" field that contains the response channel object. */
5522 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005523 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005524 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005525 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005526
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005527 /* Creates the HTTP object is the current proxy allows http. */
5528 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005529 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005530 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005531 return 0;
5532 }
5533 else
5534 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005535 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005536
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005537 /* Pop a class sesison metatable and affect it to the userdata. */
5538 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5539 lua_setmetatable(L, -2);
5540
5541 return 1;
5542}
5543
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005544__LJMP static int hlua_txn_deflog(lua_State *L)
5545{
5546 const char *msg;
5547 struct hlua_txn *htxn;
5548
5549 MAY_LJMP(check_args(L, 2, "deflog"));
5550 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5551 msg = MAY_LJMP(luaL_checkstring(L, 2));
5552
5553 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5554 return 0;
5555}
5556
5557__LJMP static int hlua_txn_log(lua_State *L)
5558{
5559 int level;
5560 const char *msg;
5561 struct hlua_txn *htxn;
5562
5563 MAY_LJMP(check_args(L, 3, "log"));
5564 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5565 level = MAY_LJMP(luaL_checkinteger(L, 2));
5566 msg = MAY_LJMP(luaL_checkstring(L, 3));
5567
5568 if (level < 0 || level >= NB_LOG_LEVELS)
5569 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5570
5571 hlua_sendlog(htxn->s->be, level, msg);
5572 return 0;
5573}
5574
5575__LJMP static int hlua_txn_log_debug(lua_State *L)
5576{
5577 const char *msg;
5578 struct hlua_txn *htxn;
5579
5580 MAY_LJMP(check_args(L, 2, "Debug"));
5581 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5582 msg = MAY_LJMP(luaL_checkstring(L, 2));
5583 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5584 return 0;
5585}
5586
5587__LJMP static int hlua_txn_log_info(lua_State *L)
5588{
5589 const char *msg;
5590 struct hlua_txn *htxn;
5591
5592 MAY_LJMP(check_args(L, 2, "Info"));
5593 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5594 msg = MAY_LJMP(luaL_checkstring(L, 2));
5595 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5596 return 0;
5597}
5598
5599__LJMP static int hlua_txn_log_warning(lua_State *L)
5600{
5601 const char *msg;
5602 struct hlua_txn *htxn;
5603
5604 MAY_LJMP(check_args(L, 2, "Warning"));
5605 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5606 msg = MAY_LJMP(luaL_checkstring(L, 2));
5607 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5608 return 0;
5609}
5610
5611__LJMP static int hlua_txn_log_alert(lua_State *L)
5612{
5613 const char *msg;
5614 struct hlua_txn *htxn;
5615
5616 MAY_LJMP(check_args(L, 2, "Alert"));
5617 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5618 msg = MAY_LJMP(luaL_checkstring(L, 2));
5619 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5620 return 0;
5621}
5622
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005623__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5624{
5625 struct hlua_txn *htxn;
5626 int ll;
5627
5628 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5629 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5630 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5631
5632 if (ll < 0 || ll > 7)
5633 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5634
5635 htxn->s->logs.level = ll;
5636 return 0;
5637}
5638
5639__LJMP static int hlua_txn_set_tos(lua_State *L)
5640{
5641 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005642 int tos;
5643
5644 MAY_LJMP(check_args(L, 2, "set_tos"));
5645 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5646 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5647
Willy Tarreau1a18b542018-12-11 16:37:42 +01005648 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005649 return 0;
5650}
5651
5652__LJMP static int hlua_txn_set_mark(lua_State *L)
5653{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005654 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005655 int mark;
5656
5657 MAY_LJMP(check_args(L, 2, "set_mark"));
5658 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5659 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5660
Lukas Tribus579e3e32019-08-11 18:03:45 +02005661 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005662 return 0;
5663}
5664
Patrick Hemmer268a7072018-05-11 12:52:31 -04005665__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5666{
5667 struct hlua_txn *htxn;
5668
5669 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5670 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5671 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5672 return 0;
5673}
5674
5675__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5676{
5677 struct hlua_txn *htxn;
5678
5679 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5680 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5681 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5682 return 0;
5683}
5684
Christopher Faulet700d9e82020-01-31 12:21:52 +01005685/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005686 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005687 * message and terminate the transaction. It returns 1 on success and 0 on
5688 * error. The Reply must be on top of the stack.
5689 */
5690__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5691{
5692 struct htx *htx;
5693 struct htx_sl *sl;
5694 struct h1m h1m;
5695 const char *status, *reason, *body;
5696 size_t status_len, reason_len, body_len;
5697 int ret, code, flags;
5698
5699 code = 200;
5700 status = "200";
5701 status_len = 3;
5702 ret = lua_getfield(L, -1, "status");
5703 if (ret == LUA_TNUMBER) {
5704 code = lua_tointeger(L, -1);
5705 status = lua_tolstring(L, -1, &status_len);
5706 }
5707 lua_pop(L, 1);
5708
5709 reason = http_get_reason(code);
5710 reason_len = strlen(reason);
5711 ret = lua_getfield(L, -1, "reason");
5712 if (ret == LUA_TSTRING)
5713 reason = lua_tolstring(L, -1, &reason_len);
5714 lua_pop(L, 1);
5715
5716 body = NULL;
5717 body_len = 0;
5718 ret = lua_getfield(L, -1, "body");
5719 if (ret == LUA_TSTRING)
5720 body = lua_tolstring(L, -1, &body_len);
5721 lua_pop(L, 1);
5722
5723 /* Prepare the response before inserting the headers */
5724 h1m_init_res(&h1m);
5725 htx = htx_from_buf(&s->res.buf);
5726 channel_htx_truncate(&s->res, htx);
5727 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5728 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5729 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5730 ist2(status, status_len), ist2(reason, reason_len));
5731 }
5732 else {
5733 flags = HTX_SL_F_IS_RESP;
5734 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5735 ist2(status, status_len), ist2(reason, reason_len));
5736 }
5737 if (!sl)
5738 goto fail;
5739 sl->info.res.status = code;
5740
5741 /* Push in the stack the "headers" entry. */
5742 ret = lua_getfield(L, -1, "headers");
5743 if (ret != LUA_TTABLE)
5744 goto skip_headers;
5745
5746 lua_pushnil(L);
5747 while (lua_next(L, -2) != 0) {
5748 struct ist name, value;
5749 const char *n, *v;
5750 size_t nlen, vlen;
5751
5752 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5753 /* Skip element if the key is not a string or if the value is not a table */
5754 goto next_hdr;
5755 }
5756
5757 n = lua_tolstring(L, -2, &nlen);
5758 name = ist2(n, nlen);
5759 if (isteqi(name, ist("content-length"))) {
5760 /* Always skip content-length header. It will be added
5761 * later with the correct len
5762 */
5763 goto next_hdr;
5764 }
5765
5766 /* Loop on header's values */
5767 lua_pushnil(L);
5768 while (lua_next(L, -2)) {
5769 if (!lua_isstring(L, -1)) {
5770 /* Skip the value if it is not a string */
5771 goto next_value;
5772 }
5773
5774 v = lua_tolstring(L, -1, &vlen);
5775 value = ist2(v, vlen);
5776
5777 if (isteqi(name, ist("transfer-encoding")))
5778 h1_parse_xfer_enc_header(&h1m, value);
5779 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5780 goto fail;
5781
5782 next_value:
5783 lua_pop(L, 1);
5784 }
5785
5786 next_hdr:
5787 lua_pop(L, 1);
5788 }
5789 skip_headers:
5790 lua_pop(L, 1);
5791
5792 /* Update h1m flags: CLEN is set if CHNK is not present */
5793 if (!(h1m.flags & H1_MF_CHNK)) {
5794 const char *clen = ultoa(body_len);
5795
5796 h1m.flags |= H1_MF_CLEN;
5797 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5798 goto fail;
5799 }
5800 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5801 h1m.flags |= H1_MF_XFER_LEN;
5802
5803 /* Update HTX start-line flags */
5804 if (h1m.flags & H1_MF_XFER_ENC)
5805 flags |= HTX_SL_F_XFER_ENC;
5806 if (h1m.flags & H1_MF_XFER_LEN) {
5807 flags |= HTX_SL_F_XFER_LEN;
5808 if (h1m.flags & H1_MF_CHNK)
5809 flags |= HTX_SL_F_CHNK;
5810 else if (h1m.flags & H1_MF_CLEN)
5811 flags |= HTX_SL_F_CLEN;
5812 if (h1m.body_len == 0)
5813 flags |= HTX_SL_F_BODYLESS;
5814 }
5815 sl->flags |= flags;
5816
5817
5818 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005819 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005820 goto fail;
5821
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005822 htx->flags |= HTX_FL_EOM;
5823
Christopher Faulet700d9e82020-01-31 12:21:52 +01005824 /* Now, forward the response and terminate the transaction */
5825 s->txn->status = code;
5826 htx_to_buf(htx, &s->res.buf);
5827 if (!http_forward_proxy_resp(s, 1))
5828 goto fail;
5829
5830 return 1;
5831
5832 fail:
5833 channel_htx_truncate(&s->res, htx);
5834 return 0;
5835}
5836
5837/* Terminate a transaction if called from a lua action. For TCP streams,
5838 * processing is just aborted. Nothing is returned to the client and all
5839 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5840 * is forwarded to the client before terminating the transaction. On success,
5841 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5842 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5843 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005844 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005845__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005846{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005847 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005848 struct stream *s;
5849 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005850
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005851 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005852
Christopher Faulet700d9e82020-01-31 12:21:52 +01005853 /* If the flags NOTERM is set, we cannot terminate the session, so we
5854 * just end the execution of the current lua code. */
5855 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005856 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005857
Christopher Faulet700d9e82020-01-31 12:21:52 +01005858 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005859 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005860 struct channel *req = &s->req;
5861 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005862
Christopher Faulet700d9e82020-01-31 12:21:52 +01005863 channel_auto_read(req);
5864 channel_abort(req);
5865 channel_auto_close(req);
5866 channel_erase(req);
5867
5868 res->wex = tick_add_ifset(now_ms, res->wto);
5869 channel_auto_read(res);
5870 channel_auto_close(res);
5871 channel_shutr_now(res);
5872
5873 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5874 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005875 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005876
Christopher Faulet700d9e82020-01-31 12:21:52 +01005877 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5878 /* No reply or invalid reply */
5879 s->txn->status = 0;
5880 http_reply_and_close(s, 0, NULL);
5881 }
5882 else {
5883 /* Remove extra args to have the reply on top of the stack */
5884 if (lua_gettop(L) > 2)
5885 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005886
Christopher Faulet700d9e82020-01-31 12:21:52 +01005887 if (!hlua_txn_forward_reply(L, s)) {
5888 if (!(s->flags & SF_ERR_MASK))
5889 s->flags |= SF_ERR_PRXCOND;
5890 lua_pushinteger(L, ACT_RET_ERR);
5891 WILL_LJMP(hlua_done(L));
5892 return 0; /* Never reached */
5893 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005894 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005895
Christopher Faulet700d9e82020-01-31 12:21:52 +01005896 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5897 if (htxn->dir == SMP_OPT_DIR_REQ) {
5898 /* let's log the request time */
5899 s->logs.tv_request = now;
5900 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02005901 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01005902 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005903
Christopher Faulet700d9e82020-01-31 12:21:52 +01005904 done:
5905 if (!(s->flags & SF_ERR_MASK))
5906 s->flags |= SF_ERR_LOCAL;
5907 if (!(s->flags & SF_FINST_MASK))
5908 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005909
Christopher Faulet4ad73102020-03-05 11:07:31 +01005910 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005911 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005912 return 0;
5913}
5914
Christopher Faulet700d9e82020-01-31 12:21:52 +01005915/*
5916 *
5917 *
5918 * Class REPLY
5919 *
5920 *
5921 */
5922
5923/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5924 * free slots, the function fails and returns 0;
5925 */
5926static int hlua_txn_reply_new(lua_State *L)
5927{
5928 struct hlua_txn *htxn;
5929 const char *reason, *body = NULL;
5930 int ret, status;
5931
5932 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005933 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005934 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5935 WILL_LJMP(lua_error(L));
5936 }
5937
5938 /* Default value */
5939 status = 200;
5940 reason = http_get_reason(status);
5941
5942 if (lua_istable(L, 2)) {
5943 /* load status and reason from the table argument at index 2 */
5944 ret = lua_getfield(L, 2, "status");
5945 if (ret == LUA_TNIL)
5946 goto reason;
5947 else if (ret != LUA_TNUMBER) {
5948 /* invalid status: ignore the reason */
5949 goto body;
5950 }
5951 status = lua_tointeger(L, -1);
5952
5953 reason:
5954 lua_pop(L, 1); /* restore the stack: remove status */
5955 ret = lua_getfield(L, 2, "reason");
5956 if (ret == LUA_TSTRING)
5957 reason = lua_tostring(L, -1);
5958
5959 body:
5960 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5961 ret = lua_getfield(L, 2, "body");
5962 if (ret == LUA_TSTRING)
5963 body = lua_tostring(L, -1);
5964 lua_pop(L, 1); /* restore the stack: remove body */
5965 }
5966
5967 /* Create the Reply table */
5968 lua_newtable(L);
5969
5970 /* Add status element */
5971 lua_pushstring(L, "status");
5972 lua_pushinteger(L, status);
5973 lua_settable(L, -3);
5974
5975 /* Add reason element */
5976 reason = http_get_reason(status);
5977 lua_pushstring(L, "reason");
5978 lua_pushstring(L, reason);
5979 lua_settable(L, -3);
5980
5981 /* Add body element, nil if undefined */
5982 lua_pushstring(L, "body");
5983 if (body)
5984 lua_pushstring(L, body);
5985 else
5986 lua_pushnil(L);
5987 lua_settable(L, -3);
5988
5989 /* Add headers element */
5990 lua_pushstring(L, "headers");
5991 lua_newtable(L);
5992
5993 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5994 if (lua_istable(L, 2)) {
5995 /* load headers from the table argument at index 2. If it is a table, copy it. */
5996 ret = lua_getfield(L, 2, "headers");
5997 if (ret == LUA_TTABLE) {
5998 /* stack: [ ... <headers:table>, <table> ] */
5999 lua_pushnil(L);
6000 while (lua_next(L, -2) != 0) {
6001 /* stack: [ ... <headers:table>, <table>, k, v] */
6002 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
6003 /* invalid value type, skip it */
6004 lua_pop(L, 1);
6005 continue;
6006 }
6007
6008
6009 /* Duplicate the key and swap it with the value. */
6010 lua_pushvalue(L, -2);
6011 lua_insert(L, -2);
6012 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6013
6014 lua_newtable(L);
6015 lua_insert(L, -2);
6016 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6017
6018 if (lua_isstring(L, -1)) {
6019 /* push the value in the inner table */
6020 lua_rawseti(L, -2, 1);
6021 }
6022 else { /* table */
6023 lua_pushnil(L);
6024 while (lua_next(L, -2) != 0) {
6025 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6026 if (!lua_isstring(L, -1)) {
6027 /* invalid value type, skip it*/
6028 lua_pop(L, 1);
6029 continue;
6030 }
6031 /* push the value in the inner table */
6032 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6033 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6034 }
6035 lua_pop(L, 1);
6036 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6037 }
6038
6039 /* push (k,v) on the stack in the headers table:
6040 * stack: [ ... <headers:table>, <table>, k, k, v ]
6041 */
6042 lua_settable(L, -5);
6043 /* stack: [ ... <headers:table>, <table>, k ] */
6044 }
6045 }
6046 lua_pop(L, 1);
6047 }
6048 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6049 lua_settable(L, -3);
6050 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6051
6052 /* Pop a class sesison metatable and affect it to the userdata. */
6053 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6054 lua_setmetatable(L, -2);
6055 return 1;
6056}
6057
6058/* Set the reply status code, and optionally the reason. If no reason is
6059 * provided, the default one corresponding to the status code is used.
6060 */
6061__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6062{
6063 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6064 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6065
6066 /* First argument (self) must be a table */
6067 luaL_checktype(L, 1, LUA_TTABLE);
6068
6069 if (status < 100 || status > 599) {
6070 lua_pushboolean(L, 0);
6071 return 1;
6072 }
6073 if (!reason)
6074 reason = http_get_reason(status);
6075
6076 lua_pushinteger(L, status);
6077 lua_setfield(L, 1, "status");
6078
6079 lua_pushstring(L, reason);
6080 lua_setfield(L, 1, "reason");
6081
6082 lua_pushboolean(L, 1);
6083 return 1;
6084}
6085
6086/* Add a header into the reply object. Each header name is associated to an
6087 * array of values in the "headers" table. If the header name is not found, a
6088 * new entry is created.
6089 */
6090__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6091{
6092 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6093 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6094 int ret;
6095
6096 /* First argument (self) must be a table */
6097 luaL_checktype(L, 1, LUA_TTABLE);
6098
6099 /* Push in the stack the "headers" entry. */
6100 ret = lua_getfield(L, 1, "headers");
6101 if (ret != LUA_TTABLE) {
6102 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6103 WILL_LJMP(lua_error(L));
6104 }
6105
6106 /* check if the header is already registered. If not, register it. */
6107 ret = lua_getfield(L, -1, name);
6108 if (ret == LUA_TNIL) {
6109 /* Entry not found. */
6110 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6111
6112 /* Insert the new header name in the array in the top of the stack.
6113 * It left the new array in the top of the stack.
6114 */
6115 lua_newtable(L);
6116 lua_pushstring(L, name);
6117 lua_pushvalue(L, -2);
6118 lua_settable(L, -4);
6119 }
6120 else if (ret != LUA_TTABLE) {
6121 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6122 WILL_LJMP(lua_error(L));
6123 }
6124
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006125 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006126 * the header value as new entry.
6127 */
6128 lua_pushstring(L, value);
6129 ret = lua_rawlen(L, -2);
6130 lua_rawseti(L, -2, ret + 1);
6131
6132 lua_pushboolean(L, 1);
6133 return 1;
6134}
6135
6136/* Remove all occurrences of a given header name. */
6137__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6138{
6139 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
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 be an array. %s found", lua_typename(L, ret));
6149 WILL_LJMP(lua_error(L));
6150 }
6151
6152 lua_pushstring(L, name);
6153 lua_pushnil(L);
6154 lua_settable(L, -3);
6155
6156 lua_pushboolean(L, 1);
6157 return 1;
6158}
6159
6160/* Set the reply's body. Overwrite any existing entry. */
6161__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6162{
6163 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6164
6165 /* First argument (self) must be a table */
6166 luaL_checktype(L, 1, LUA_TTABLE);
6167
6168 lua_pushstring(L, payload);
6169 lua_setfield(L, 1, "body");
6170
6171 lua_pushboolean(L, 1);
6172 return 1;
6173}
6174
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006175__LJMP static int hlua_log(lua_State *L)
6176{
6177 int level;
6178 const char *msg;
6179
6180 MAY_LJMP(check_args(L, 2, "log"));
6181 level = MAY_LJMP(luaL_checkinteger(L, 1));
6182 msg = MAY_LJMP(luaL_checkstring(L, 2));
6183
6184 if (level < 0 || level >= NB_LOG_LEVELS)
6185 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6186
6187 hlua_sendlog(NULL, level, msg);
6188 return 0;
6189}
6190
6191__LJMP static int hlua_log_debug(lua_State *L)
6192{
6193 const char *msg;
6194
6195 MAY_LJMP(check_args(L, 1, "debug"));
6196 msg = MAY_LJMP(luaL_checkstring(L, 1));
6197 hlua_sendlog(NULL, LOG_DEBUG, msg);
6198 return 0;
6199}
6200
6201__LJMP static int hlua_log_info(lua_State *L)
6202{
6203 const char *msg;
6204
6205 MAY_LJMP(check_args(L, 1, "info"));
6206 msg = MAY_LJMP(luaL_checkstring(L, 1));
6207 hlua_sendlog(NULL, LOG_INFO, msg);
6208 return 0;
6209}
6210
6211__LJMP static int hlua_log_warning(lua_State *L)
6212{
6213 const char *msg;
6214
6215 MAY_LJMP(check_args(L, 1, "warning"));
6216 msg = MAY_LJMP(luaL_checkstring(L, 1));
6217 hlua_sendlog(NULL, LOG_WARNING, msg);
6218 return 0;
6219}
6220
6221__LJMP static int hlua_log_alert(lua_State *L)
6222{
6223 const char *msg;
6224
6225 MAY_LJMP(check_args(L, 1, "alert"));
6226 msg = MAY_LJMP(luaL_checkstring(L, 1));
6227 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006228 return 0;
6229}
6230
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006231__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006232{
6233 int wakeup_ms = lua_tointeger(L, -1);
Willy Tarreau9cc2e4c2021-09-30 16:12:31 +02006234 if (!tick_is_expired(wakeup_ms, now_ms))
Willy Tarreau9635e032018-10-16 17:52:55 +02006235 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006236 return 0;
6237}
6238
6239__LJMP static int hlua_sleep(lua_State *L)
6240{
6241 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006242 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006243
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006244 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006245
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006246 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006247 wakeup_ms = tick_add(now_ms, delay);
6248 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006249
Willy Tarreau9635e032018-10-16 17:52:55 +02006250 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006251 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006252}
6253
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006254__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006255{
6256 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006257 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006258
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006259 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006260
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006261 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006262 wakeup_ms = tick_add(now_ms, delay);
6263 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006264
Willy Tarreau9635e032018-10-16 17:52:55 +02006265 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006266 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006267}
6268
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006269/* This functionis an LUA binding. it permits to give back
6270 * the hand at the HAProxy scheduler. It is used when the
6271 * LUA processing consumes a lot of time.
6272 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006273__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006274{
6275 return 0;
6276}
6277
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006278__LJMP static int hlua_yield(lua_State *L)
6279{
Willy Tarreau9635e032018-10-16 17:52:55 +02006280 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006281 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006282}
6283
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006284/* This function change the nice of the currently executed
6285 * task. It is used set low or high priority at the current
6286 * task.
6287 */
Willy Tarreau59551662015-03-10 14:23:13 +01006288__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006289{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006290 struct hlua *hlua;
6291 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006292
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006293 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006294 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006295
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006296 /* Get hlua struct, or NULL if we execute from main lua state */
6297 hlua = hlua_gethlua(L);
6298
6299 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006300 if (!hlua || !hlua->task)
6301 return 0;
6302
6303 if (nice < -1024)
6304 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006305 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006306 nice = 1024;
6307
6308 hlua->task->nice = nice;
6309 return 0;
6310}
6311
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006312/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006313 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6314 * return an E_AGAIN signal, the emmiter of this signal must set a
6315 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006316 *
6317 * Task wrapper are longjmp safe because the only one Lua code
6318 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006319 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006320struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006321{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006322 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006323 enum hlua_exec status;
6324
Christopher Faulet5bc99722018-04-25 10:34:45 +02006325 if (task->thread_mask == MAX_THREADS_MASK)
6326 task_set_affinity(task, tid_bit);
6327
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006328 /* If it is the first call to the task, we must initialize the
6329 * execution timeouts.
6330 */
6331 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006332 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006333
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006334 /* Execute the Lua code. */
6335 status = hlua_ctx_resume(hlua, 1);
6336
6337 switch (status) {
6338 /* finished or yield */
6339 case HLUA_E_OK:
6340 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006341 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006342 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006343 break;
6344
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006345 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006346 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006347 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006348 break;
6349
6350 /* finished with error. */
6351 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006352 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006353 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006354 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006355 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006356 break;
6357
6358 case HLUA_E_ERR:
6359 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006360 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006361 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006362 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006363 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006364 break;
6365 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006366 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006367}
6368
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006369/* This function is an LUA binding that register LUA function to be
6370 * executed after the HAProxy configuration parsing and before the
6371 * HAProxy scheduler starts. This function expect only one LUA
6372 * argument that is a function. This function returns nothing, but
6373 * throws if an error is encountered.
6374 */
6375__LJMP static int hlua_register_init(lua_State *L)
6376{
6377 struct hlua_init_function *init;
6378 int ref;
6379
6380 MAY_LJMP(check_args(L, 1, "register_init"));
6381
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006382 if (hlua_gethlua(L)) {
6383 /* runtime processing */
6384 WILL_LJMP(luaL_error(L, "register_init: not available outside of body context"));
6385 }
6386
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006387 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6388
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006389 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006390 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006391 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006392
6393 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006394 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006395 return 0;
6396}
6397
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006398/* This functio is an LUA binding. It permits to register a task
6399 * executed in parallel of the main HAroxy activity. The task is
6400 * created and it is set in the HAProxy scheduler. It can be called
6401 * from the "init" section, "post init" or during the runtime.
6402 *
6403 * Lua prototype:
6404 *
6405 * <none> core.register_task(<function>)
6406 */
6407static int hlua_register_task(lua_State *L)
6408{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006409 struct hlua *hlua = NULL;
6410 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006411 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006412 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006413
6414 MAY_LJMP(check_args(L, 1, "register_task"));
6415
6416 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6417
Thierry Fournier75fc0292020-11-28 13:18:56 +01006418 /* Get the reference state. If the reference is NULL, L is the master
6419 * state, otherwise hlua->T is.
6420 */
6421 hlua = hlua_gethlua(L);
6422 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006423 /* we are in runtime processing */
6424 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006425 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006426 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006427 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006428
Willy Tarreaubafbe012017-11-24 17:34:44 +01006429 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006430 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006431 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006432 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006433
Thierry Fournier59f11be2020-11-29 00:37:41 +01006434 /* We are in the common lua state, execute the task anywhere,
6435 * otherwise, inherit the current thread identifier
6436 */
6437 if (state_id == 0)
6438 task = task_new(MAX_THREADS_MASK);
6439 else
6440 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006441 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006442 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006443
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006444 task->context = hlua;
6445 task->process = hlua_process_task;
6446
Thierry Fournier021d9862020-11-28 23:42:03 +01006447 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006448 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006449
6450 /* Restore the function in the stack. */
6451 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6452 hlua->nargs = 0;
6453
6454 /* Schedule task. */
Willy Tarreau790810f2021-09-30 16:17:37 +02006455 task_wakeup(task, TASK_WOKEN_INIT);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006456
6457 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006458
6459 alloc_error:
6460 task_destroy(task);
6461 hlua_ctx_destroy(hlua);
6462 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6463 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006464}
6465
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006466/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6467 * doesn't allow "yield" functions because the HAProxy engine cannot
6468 * resume converters.
6469 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006470static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006471{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006472 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006473 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006474 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006475
Willy Tarreaube508f12016-03-10 11:47:01 +01006476 if (!stream)
6477 return 0;
6478
Willy Tarreau87b09662015-04-03 00:22:06 +02006479 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006480 * Lua context can be not initialized. This behavior
6481 * permits to save performances because a systematic
6482 * Lua initialization cause 5% performances loss.
6483 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006484 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006485 struct hlua *hlua;
6486
6487 hlua = pool_alloc(pool_head_hlua);
6488 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006489 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6490 return 0;
6491 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006492 HLUA_INIT(hlua);
6493 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006494 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006495 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6496 return 0;
6497 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006498 }
6499
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006500 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006501 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006502
6503 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006504 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006505 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6506 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006507 else
6508 error = "critical error";
6509 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006510 return 0;
6511 }
6512
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006513 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006514 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006515 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006516 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006517 return 0;
6518 }
6519
6520 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006521 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006522
6523 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006524 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006525 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006526 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006527 return 0;
6528 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006529 hlua_smp2lua(stream->hlua->T, smp);
6530 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006531
6532 /* push keywords in the stack. */
6533 if (arg_p) {
6534 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006535 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006536 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006537 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006538 return 0;
6539 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006540 hlua_arg2lua(stream->hlua->T, arg_p);
6541 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006542 }
6543 }
6544
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006545 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006546 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006547
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006548 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006549 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006550 }
6551
6552 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006553 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006554 /* finished. */
6555 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006556 /* If the stack is empty, the function fails. */
6557 if (lua_gettop(stream->hlua->T) <= 0)
6558 return 0;
6559
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006560 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006561 hlua_lua2smp(stream->hlua->T, -1, smp);
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02006562 /* dup the smp before popping the related lua value and
6563 * returning it to haproxy
6564 */
6565 smp_dup(smp);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006566 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006567 return 1;
6568
6569 /* yield. */
6570 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006571 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006572 return 0;
6573
6574 /* finished with error. */
6575 case HLUA_E_ERRMSG:
6576 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006577 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006578 fcn->name, lua_tostring(stream->hlua->T, -1));
6579 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006580 return 0;
6581
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006582 case HLUA_E_ETMOUT:
6583 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6584 return 0;
6585
6586 case HLUA_E_NOMEM:
6587 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6588 return 0;
6589
6590 case HLUA_E_YIELD:
6591 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6592 return 0;
6593
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006594 case HLUA_E_ERR:
6595 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006596 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006597 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006598
6599 default:
6600 return 0;
6601 }
6602}
6603
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006604/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6605 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006606 * resume sample-fetches. This function will be called by the sample
6607 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006608 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006609static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6610 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006611{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006612 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006613 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006614 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006615 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006616
Willy Tarreaube508f12016-03-10 11:47:01 +01006617 if (!stream)
6618 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006619
Willy Tarreau87b09662015-04-03 00:22:06 +02006620 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006621 * Lua context can be not initialized. This behavior
6622 * permits to save performances because a systematic
6623 * Lua initialization cause 5% performances loss.
6624 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006625 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006626 struct hlua *hlua;
6627
6628 hlua = pool_alloc(pool_head_hlua);
6629 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006630 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6631 return 0;
6632 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006633 hlua->T = NULL;
6634 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006635 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006636 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6637 return 0;
6638 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006639 }
6640
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006641 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006642 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006643
6644 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006645 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006646 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6647 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006648 else
6649 error = "critical error";
6650 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006651 return 0;
6652 }
6653
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006654 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006655 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006656 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006657 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006658 return 0;
6659 }
6660
6661 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006662 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006663
6664 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006665 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006666 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006667 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006668 return 0;
6669 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006670 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006671
6672 /* push keywords in the stack. */
6673 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6674 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006675 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006676 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006677 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006678 return 0;
6679 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006680 hlua_arg2lua(stream->hlua->T, arg_p);
6681 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006682 }
6683
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006684 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006685 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006686
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006687 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006688 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006689 }
6690
6691 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006692 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006693 /* finished. */
6694 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006695 /* If the stack is empty, the function fails. */
6696 if (lua_gettop(stream->hlua->T) <= 0)
6697 return 0;
6698
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006699 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006700 hlua_lua2smp(stream->hlua->T, -1, smp);
Aurelien DARRAGONd39fb742023-05-17 16:06:11 +02006701 /* dup the smp before popping the related lua value and
6702 * returning it to haproxy
6703 */
6704 smp_dup(smp);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006705 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006706
6707 /* Set the end of execution flag. */
6708 smp->flags &= ~SMP_F_MAY_CHANGE;
6709 return 1;
6710
6711 /* yield. */
6712 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006713 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006714 return 0;
6715
6716 /* finished with error. */
6717 case HLUA_E_ERRMSG:
6718 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006719 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006720 fcn->name, lua_tostring(stream->hlua->T, -1));
6721 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006722 return 0;
6723
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006724 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006725 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6726 return 0;
6727
6728 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006729 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6730 return 0;
6731
6732 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006733 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6734 return 0;
6735
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006736 case HLUA_E_ERR:
6737 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006738 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006739 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006740
6741 default:
6742 return 0;
6743 }
6744}
6745
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006746/* This function is an LUA binding used for registering
6747 * "sample-conv" functions. It expects a converter name used
6748 * in the haproxy configuration file, and an LUA function.
6749 */
6750__LJMP static int hlua_register_converters(lua_State *L)
6751{
6752 struct sample_conv_kw_list *sck;
6753 const char *name;
6754 int ref;
6755 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02006756 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006757 struct sample_conv *sc;
6758 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006759
6760 MAY_LJMP(check_args(L, 2, "register_converters"));
6761
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006762 if (hlua_gethlua(L)) {
6763 /* runtime processing */
6764 WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context"));
6765 }
6766
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006767 /* First argument : converter name. */
6768 name = MAY_LJMP(luaL_checkstring(L, 1));
6769
6770 /* Second argument : lua function. */
6771 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6772
Thierry Fournierf67442e2020-11-28 20:41:07 +01006773 /* Check if the converter is already registered */
6774 trash = get_trash_chunk();
6775 chunk_printf(trash, "lua.%s", name);
6776 sc = find_sample_conv(trash->area, trash->data);
6777 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006778 fcn = sc->private;
6779 if (fcn->function_ref[hlua_state_id] != -1) {
6780 ha_warning("Trying to register converter 'lua.%s' more than once. "
6781 "This will become a hard error in version 2.5.\n", name);
6782 }
6783 fcn->function_ref[hlua_state_id] = ref;
6784 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006785 }
6786
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006787 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006788 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006789 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02006790 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006791 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006792 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02006793 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006794
6795 /* Fill fcn. */
6796 fcn->name = strdup(name);
6797 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02006798 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006799 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006800
6801 /* List head */
6802 sck->list.n = sck->list.p = NULL;
6803
6804 /* converter keyword. */
6805 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006806 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006807 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02006808 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006809
6810 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6811 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006812 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 +01006813 sck->kw[0].val_args = NULL;
6814 sck->kw[0].in_type = SMP_T_STR;
6815 sck->kw[0].out_type = SMP_T_STR;
6816 sck->kw[0].private = fcn;
6817
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006818 /* Register this new converter */
6819 sample_register_convs(sck);
6820
6821 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02006822
6823 alloc_error:
6824 release_hlua_function(fcn);
6825 ha_free(&sck);
6826 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6827 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006828}
6829
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006830/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006831 * "sample-fetch" functions. It expects a converter name used
6832 * in the haproxy configuration file, and an LUA function.
6833 */
6834__LJMP static int hlua_register_fetches(lua_State *L)
6835{
6836 const char *name;
6837 int ref;
6838 int len;
6839 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02006840 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006841 struct sample_fetch *sf;
6842 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006843
6844 MAY_LJMP(check_args(L, 2, "register_fetches"));
6845
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01006846 if (hlua_gethlua(L)) {
6847 /* runtime processing */
6848 WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context"));
6849 }
6850
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006851 /* First argument : sample-fetch name. */
6852 name = MAY_LJMP(luaL_checkstring(L, 1));
6853
6854 /* Second argument : lua function. */
6855 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6856
Thierry Fournierf67442e2020-11-28 20:41:07 +01006857 /* Check if the sample-fetch is already registered */
6858 trash = get_trash_chunk();
6859 chunk_printf(trash, "lua.%s", name);
6860 sf = find_sample_fetch(trash->area, trash->data);
6861 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006862 fcn = sf->private;
6863 if (fcn->function_ref[hlua_state_id] != -1) {
6864 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6865 "This will become a hard error in version 2.5.\n", name);
6866 }
6867 fcn->function_ref[hlua_state_id] = ref;
6868 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006869 }
6870
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006871 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006872 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006873 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02006874 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006875 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006876 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02006877 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006878
6879 /* Fill fcn. */
6880 fcn->name = strdup(name);
6881 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02006882 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006883 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006884
6885 /* List head */
6886 sfk->list.n = sfk->list.p = NULL;
6887
6888 /* sample-fetch keyword. */
6889 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006890 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006891 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02006892 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006893
6894 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6895 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006896 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 +01006897 sfk->kw[0].val_args = NULL;
6898 sfk->kw[0].out_type = SMP_T_STR;
6899 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6900 sfk->kw[0].val = 0;
6901 sfk->kw[0].private = fcn;
6902
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006903 /* Register this new fetch. */
6904 sample_register_fetches(sfk);
6905
6906 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02006907
6908 alloc_error:
6909 release_hlua_function(fcn);
6910 ha_free(&sfk);
6911 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6912 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006913}
6914
Christopher Faulet501465d2020-02-26 14:54:16 +01006915/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006916 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006917__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006918{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006919 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006920 unsigned int delay;
6921 unsigned int wakeup_ms;
6922
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006923 /* Get hlua struct, or NULL if we execute from main lua state */
6924 hlua = hlua_gethlua(L);
6925 if (!hlua) {
6926 return 0;
6927 }
6928
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006929 MAY_LJMP(check_args(L, 1, "wake_time"));
6930
6931 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6932 wakeup_ms = tick_add(now_ms, delay);
6933 hlua->wake_time = wakeup_ms;
6934 return 0;
6935}
6936
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006937/* This function is a wrapper to execute each LUA function declared as an action
6938 * wrapper during the initialisation period. This function may return any
6939 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6940 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6941 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006942 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006943static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006944 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006945{
6946 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006947 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006948 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006949 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006950
6951 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006952 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6953 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6954 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6955 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006956 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006957 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006958 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006959 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006960
Willy Tarreau87b09662015-04-03 00:22:06 +02006961 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006962 * Lua context can be not initialized. This behavior
6963 * permits to save performances because a systematic
6964 * Lua initialization cause 5% performances loss.
6965 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006966 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006967 struct hlua *hlua;
6968
6969 hlua = pool_alloc(pool_head_hlua);
6970 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006971 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006972 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006973 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006974 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006975 HLUA_INIT(hlua);
6976 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006977 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 +01006978 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006979 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006980 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006981 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006982 }
6983
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006984 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006985 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006986
6987 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006988 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006989 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6990 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006991 else
6992 error = "critical error";
6993 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006994 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006995 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006996 }
6997
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006998 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006999 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007000 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007001 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007002 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007003 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007004 }
7005
7006 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007007 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 +01007008
Willy Tarreau87b09662015-04-03 00:22:06 +02007009 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007010 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007011 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007012 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007013 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007014 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007015 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007016 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007017
7018 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007019 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007020 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007021 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007022 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007023 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007024 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007025 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007026 lua_pushstring(s->hlua->T, *arg);
7027 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007028 }
7029
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007030 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007031 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007032
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007033 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007034 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007035 }
7036
7037 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01007038 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007039 /* finished. */
7040 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007041 /* Catch the return value */
7042 if (lua_gettop(s->hlua->T) > 0)
7043 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007044
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007045 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007046 if (act_ret == ACT_RET_YIELD) {
7047 if (flags & ACT_OPT_FINAL)
7048 goto err_yield;
7049
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007050 if (dir == SMP_OPT_DIR_REQ)
7051 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7052 s->hlua->wake_time);
7053 else
7054 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7055 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007056 }
7057 goto end;
7058
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007059 /* yield. */
7060 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007061 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007062 if (dir == SMP_OPT_DIR_REQ)
7063 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7064 s->hlua->wake_time);
7065 else
7066 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7067 s->hlua->wake_time);
7068
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007069 /* Some actions can be wake up when a "write" event
7070 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007071 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007072 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007073 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007074 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007075 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007076 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007077 act_ret = ACT_RET_YIELD;
7078 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007079
7080 /* finished with error. */
7081 case HLUA_E_ERRMSG:
7082 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007083 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007084 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007085 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007086 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007087
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007088 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007089 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007090 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007091
7092 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007093 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007094 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007095
7096 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007097 err_yield:
7098 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007099 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007100 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007101 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007102
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007103 case HLUA_E_ERR:
7104 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007105 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007106 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007107
7108 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007109 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007110 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007111
7112 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007113 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007114 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007115 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007116}
7117
Willy Tarreau144f84a2021-03-02 16:09:26 +01007118struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007119{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007120 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007121
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007122 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007123 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007124 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007125}
7126
7127static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7128{
7129 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007130 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007131 struct task *task;
7132 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007133 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007134
Willy Tarreaubafbe012017-11-24 17:34:44 +01007135 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007136 if (!hlua) {
7137 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007138 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007139 return 0;
7140 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007141 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007142 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007143 ctx->ctx.hlua_apptcp.flags = 0;
7144
7145 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007146 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007147 if (!task) {
7148 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007149 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007150 return 0;
7151 }
7152 task->nice = 0;
7153 task->context = ctx;
7154 task->process = hlua_applet_wakeup;
7155 ctx->ctx.hlua_apptcp.task = task;
7156
7157 /* In the execution wrappers linked with a stream, the
7158 * Lua context can be not initialized. This behavior
7159 * permits to save performances because a systematic
7160 * Lua initialization cause 5% performances loss.
7161 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007162 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 +02007163 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007164 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007165 return 0;
7166 }
7167
7168 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007169 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007170
7171 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007172 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007173 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7174 error = lua_tostring(hlua->T, -1);
7175 else
7176 error = "critical error";
7177 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007178 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007179 return 0;
7180 }
7181
7182 /* Check stack available size. */
7183 if (!lua_checkstack(hlua->T, 1)) {
7184 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007185 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007186 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007187 return 0;
7188 }
7189
7190 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007191 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007192
7193 /* Create and and push object stream in the stack. */
7194 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7195 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007196 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007197 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007198 return 0;
7199 }
7200 hlua->nargs = 1;
7201
7202 /* push keywords in the stack. */
7203 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7204 if (!lua_checkstack(hlua->T, 1)) {
7205 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007206 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007207 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007208 return 0;
7209 }
7210 lua_pushstring(hlua->T, *arg);
7211 hlua->nargs++;
7212 }
7213
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007214 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007215
7216 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007217 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007218 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007219
7220 return 1;
7221}
7222
Willy Tarreau60409db2019-08-21 14:14:50 +02007223void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007224{
7225 struct stream_interface *si = ctx->owner;
7226 struct stream *strm = si_strm(si);
7227 struct channel *res = si_ic(si);
7228 struct act_rule *rule = ctx->rule;
7229 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007230 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007231
7232 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007233 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7234 /* eat the whole request */
7235 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007236 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007237 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007238
7239 /* If the stream is disconnect or closed, ldo nothing. */
7240 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7241 return;
7242
7243 /* Execute the function. */
7244 switch (hlua_ctx_resume(hlua, 1)) {
7245 /* finished. */
7246 case HLUA_E_OK:
7247 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7248
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007249 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007250 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007251 res->flags |= CF_READ_NULL;
7252 si_shutr(si);
7253 return;
7254
7255 /* yield. */
7256 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007257 if (hlua->wake_time != TICK_ETERNITY)
7258 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007259 return;
7260
7261 /* finished with error. */
7262 case HLUA_E_ERRMSG:
7263 /* Display log. */
7264 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007265 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007266 lua_pop(hlua->T, 1);
7267 goto error;
7268
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007269 case HLUA_E_ETMOUT:
7270 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007271 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007272 goto error;
7273
7274 case HLUA_E_NOMEM:
7275 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007276 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007277 goto error;
7278
7279 case HLUA_E_YIELD: /* unexpected */
7280 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007281 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007282 goto error;
7283
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007284 case HLUA_E_ERR:
7285 /* Display log. */
7286 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007287 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007288 goto error;
7289
7290 default:
7291 goto error;
7292 }
7293
7294error:
7295
7296 /* For all other cases, just close the stream. */
7297 si_shutw(si);
7298 si_shutr(si);
7299 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7300}
7301
7302static void hlua_applet_tcp_release(struct appctx *ctx)
7303{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007304 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007305 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007306 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007307 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007308}
7309
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007310/* The function returns 1 if the initialisation is complete, 0 if
7311 * an errors occurs and -1 if more data are required for initializing
7312 * the applet.
7313 */
7314static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7315{
7316 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007317 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007318 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007319 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007320 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007321 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007322
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007323 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007324 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007325 if (!hlua) {
7326 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007327 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007328 return 0;
7329 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007330 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007331 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007332 ctx->ctx.hlua_apphttp.left_bytes = -1;
7333 ctx->ctx.hlua_apphttp.flags = 0;
7334
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007335 if (txn->req.flags & HTTP_MSGF_VER_11)
7336 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7337
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007338 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007339 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007340 if (!task) {
7341 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007342 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007343 return 0;
7344 }
7345 task->nice = 0;
7346 task->context = ctx;
7347 task->process = hlua_applet_wakeup;
7348 ctx->ctx.hlua_apphttp.task = task;
7349
7350 /* In the execution wrappers linked with a stream, the
7351 * Lua context can be not initialized. This behavior
7352 * permits to save performances because a systematic
7353 * Lua initialization cause 5% performances loss.
7354 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007355 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 +02007356 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007357 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007358 return 0;
7359 }
7360
7361 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007362 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007363
7364 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007365 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007366 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7367 error = lua_tostring(hlua->T, -1);
7368 else
7369 error = "critical error";
7370 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007371 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007372 return 0;
7373 }
7374
7375 /* Check stack available size. */
7376 if (!lua_checkstack(hlua->T, 1)) {
7377 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007378 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007379 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007380 return 0;
7381 }
7382
7383 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007384 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007385
7386 /* Create and and push object stream in the stack. */
7387 if (!hlua_applet_http_new(hlua->T, ctx)) {
7388 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007389 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007390 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007391 return 0;
7392 }
7393 hlua->nargs = 1;
7394
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007395 /* push keywords in the stack. */
7396 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7397 if (!lua_checkstack(hlua->T, 1)) {
7398 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007399 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007400 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007401 return 0;
7402 }
7403 lua_pushstring(hlua->T, *arg);
7404 hlua->nargs++;
7405 }
7406
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007407 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007408
7409 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007410 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007411
7412 return 1;
7413}
7414
Willy Tarreau60409db2019-08-21 14:14:50 +02007415void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007416{
7417 struct stream_interface *si = ctx->owner;
7418 struct stream *strm = si_strm(si);
7419 struct channel *req = si_oc(si);
7420 struct channel *res = si_ic(si);
7421 struct act_rule *rule = ctx->rule;
7422 struct proxy *px = strm->be;
7423 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7424 struct htx *req_htx, *res_htx;
7425
7426 res_htx = htx_from_buf(&res->buf);
7427
7428 /* If the stream is disconnect or closed, ldo nothing. */
7429 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7430 goto out;
7431
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007432 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007433 if (!b_size(&res->buf)) {
7434 si_rx_room_blk(si);
7435 goto out;
7436 }
7437 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007438 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007439 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7440
7441 /* Set the currently running flag. */
7442 if (!HLUA_IS_RUNNING(hlua) &&
7443 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007444 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007445 si_cant_get(si);
7446 goto out;
7447 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007448 }
7449
7450 /* Executes The applet if it is not done. */
7451 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7452
7453 /* Execute the function. */
7454 switch (hlua_ctx_resume(hlua, 1)) {
7455 /* finished. */
7456 case HLUA_E_OK:
7457 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7458 break;
7459
7460 /* yield. */
7461 case HLUA_E_AGAIN:
7462 if (hlua->wake_time != TICK_ETERNITY)
7463 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007464 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007465
7466 /* finished with error. */
7467 case HLUA_E_ERRMSG:
7468 /* Display log. */
7469 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007470 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007471 lua_pop(hlua->T, 1);
7472 goto error;
7473
7474 case HLUA_E_ETMOUT:
7475 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007476 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007477 goto error;
7478
7479 case HLUA_E_NOMEM:
7480 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007481 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007482 goto error;
7483
7484 case HLUA_E_YIELD: /* unexpected */
7485 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007486 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007487 goto error;
7488
7489 case HLUA_E_ERR:
7490 /* Display log. */
7491 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007492 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007493 goto error;
7494
7495 default:
7496 goto error;
7497 }
7498 }
7499
7500 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007501 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7502 goto done;
7503
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007504 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7505 goto error;
7506
Christopher Faulet868b3b12022-04-07 10:07:18 +02007507 /* no more data are expected. If the response buffer is empty
7508 * for a chunked message, be sure to add something (EOT block in
7509 * this case) to have something to send. It is important to be
7510 * sure the EOM flags will be handled by the endpoint.
7511 */
7512 if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) {
7513 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
7514 si_rx_room_blk(si);
7515 goto out;
7516 }
7517 channel_add_input(res, 1);
7518 }
7519
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007520 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet79c0fc72022-03-07 15:50:54 +01007521 res->flags |= CF_EOI;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007522 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7523 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007524 }
7525
7526 done:
7527 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007528 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007529 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007530 si_shutr(si);
7531 }
7532
7533 /* eat the whole request */
7534 if (co_data(req)) {
7535 req_htx = htx_from_buf(&req->buf);
7536 co_htx_skip(req, req_htx, co_data(req));
7537 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007538 }
7539 }
7540
7541 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007542 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007543 return;
7544
7545 error:
7546
7547 /* If we are in HTTP mode, and we are not send any
7548 * data, return a 500 server error in best effort:
7549 * if there is no room available in the buffer,
7550 * just close the connection.
7551 */
7552 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007553 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007554
7555 channel_erase(res);
7556 res->buf.data = b_data(err);
7557 memcpy(res->buf.area, b_head(err), b_data(err));
7558 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007559 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007560 }
7561 if (!(strm->flags & SF_ERR_MASK))
7562 strm->flags |= SF_ERR_RESOURCE;
7563 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7564 goto done;
7565}
7566
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007567static void hlua_applet_http_release(struct appctx *ctx)
7568{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007569 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007570 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007571 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007572 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007573}
7574
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007575/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007576 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007577 *
7578 * This function can fail with an abort() due to an Lua critical error.
7579 * We are in the configuration parsing process of HAProxy, this abort() is
7580 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007581 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007582static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7583 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007584{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007585 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007586 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007587
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007588 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007589 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007590 if (!rule->arg.hlua_rule) {
7591 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007592 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007593 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007594
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007595 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007596 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7597 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007598 if (!rule->arg.hlua_rule->args) {
7599 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007600 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007601 }
7602
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007603 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007604 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007605
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007606 /* Expect some arguments */
7607 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007608 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007609 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007610 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007611 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007612 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007613 if (!rule->arg.hlua_rule->args[i]) {
7614 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007615 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007616 }
7617 (*cur_arg)++;
7618 }
7619 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007620
Thierry FOURNIER42148732015-09-02 17:17:33 +02007621 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007622 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007623 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007624
7625 error:
7626 if (rule->arg.hlua_rule) {
7627 if (rule->arg.hlua_rule->args) {
7628 for (i = 0; i < fcn->nargs; i++)
7629 ha_free(&rule->arg.hlua_rule->args[i]);
7630 ha_free(&rule->arg.hlua_rule->args);
7631 }
7632 ha_free(&rule->arg.hlua_rule);
7633 }
7634 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007635}
7636
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007637static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7638 struct act_rule *rule, char **err)
7639{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007640 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007641
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007642 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007643 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007644 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007645 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007646 * the call of this analyzer.
7647 */
7648 if (rule->from != ACT_F_HTTP_REQ) {
7649 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7650 return ACT_RET_PRS_ERR;
7651 }
7652
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007653 /* Memory for the rule. */
7654 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7655 if (!rule->arg.hlua_rule) {
7656 memprintf(err, "out of memory error");
7657 return ACT_RET_PRS_ERR;
7658 }
7659
7660 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007661 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007662
7663 /* TODO: later accept arguments. */
7664 rule->arg.hlua_rule->args = NULL;
7665
7666 /* Add applet pointer in the rule. */
7667 rule->applet.obj_type = OBJ_TYPE_APPLET;
7668 rule->applet.name = fcn->name;
7669 rule->applet.init = hlua_applet_http_init;
7670 rule->applet.fct = hlua_applet_http_fct;
7671 rule->applet.release = hlua_applet_http_release;
7672 rule->applet.timeout = hlua_timeout_applet;
7673
7674 return ACT_RET_PRS_OK;
7675}
7676
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007677/* This function is an LUA binding used for registering
7678 * "sample-conv" functions. It expects a converter name used
7679 * in the haproxy configuration file, and an LUA function.
7680 */
7681__LJMP static int hlua_register_action(lua_State *L)
7682{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007683 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007684 const char *name;
7685 int ref;
7686 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007687 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007688 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007689 struct buffer *trash;
7690 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007691
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007692 /* Initialise the number of expected arguments at 0. */
7693 nargs = 0;
7694
7695 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7696 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007697
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01007698 if (hlua_gethlua(L)) {
7699 /* runtime processing */
7700 WILL_LJMP(luaL_error(L, "register_action: not available outside of body context"));
7701 }
7702
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007703 /* First argument : converter name. */
7704 name = MAY_LJMP(luaL_checkstring(L, 1));
7705
7706 /* Second argument : environment. */
7707 if (lua_type(L, 2) != LUA_TTABLE)
7708 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7709
7710 /* Third argument : lua function. */
7711 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7712
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007713 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007714 if (lua_gettop(L) >= 4)
7715 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7716
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007717 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007718 lua_pushnil(L);
7719 while (lua_next(L, 2) != 0) {
7720 if (lua_type(L, -1) != LUA_TSTRING)
7721 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7722
Thierry Fournierf67442e2020-11-28 20:41:07 +01007723 /* Check if action exists */
7724 trash = get_trash_chunk();
7725 chunk_printf(trash, "lua.%s", name);
7726 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7727 akw = tcp_req_cont_action(trash->area);
7728 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7729 akw = tcp_res_cont_action(trash->area);
7730 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7731 akw = action_http_req_custom(trash->area);
7732 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7733 akw = action_http_res_custom(trash->area);
7734 } else {
7735 akw = NULL;
7736 }
7737 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007738 fcn = akw->private;
7739 if (fcn->function_ref[hlua_state_id] != -1) {
7740 ha_warning("Trying to register action 'lua.%s' more than once. "
7741 "This will become a hard error in version 2.5.\n", name);
7742 }
7743 fcn->function_ref[hlua_state_id] = ref;
7744
7745 /* pop the environment string. */
7746 lua_pop(L, 1);
7747 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007748 }
7749
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007750 /* Check required environment. Only accepted "http" or "tcp". */
7751 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007752 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007753 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007754 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007755 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007756 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007757 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007758
7759 /* Fill fcn. */
7760 fcn->name = strdup(name);
7761 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007762 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007763 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007764
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007765 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007766 fcn->nargs = nargs;
7767
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007768 /* List head */
7769 akl->list.n = akl->list.p = NULL;
7770
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007771 /* action keyword. */
7772 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007773 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007774 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007775 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007776
7777 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7778
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007779 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007780 akl->kw[0].private = fcn;
7781 akl->kw[0].parse = action_register_lua;
7782
7783 /* select the action registering point. */
7784 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7785 tcp_req_cont_keywords_register(akl);
7786 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7787 tcp_res_cont_keywords_register(akl);
7788 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7789 http_req_keywords_register(akl);
7790 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7791 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007792 else {
7793 release_hlua_function(fcn);
7794 if (akl)
7795 ha_free((char **)&(akl->kw[0].kw));
7796 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007797 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007798 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7799 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007800 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007801
7802 /* pop the environment string. */
7803 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007804
7805 /* reset for next loop */
7806 akl = NULL;
7807 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007808 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007809 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007810
7811 alloc_error:
7812 release_hlua_function(fcn);
7813 ha_free(&akl);
7814 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7815 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007816}
7817
7818static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7819 struct act_rule *rule, char **err)
7820{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007821 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007822
Christopher Faulet280f85b2019-07-15 15:02:04 +02007823 if (px->mode == PR_MODE_HTTP) {
7824 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007825 return ACT_RET_PRS_ERR;
7826 }
7827
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007828 /* Memory for the rule. */
7829 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7830 if (!rule->arg.hlua_rule) {
7831 memprintf(err, "out of memory error");
7832 return ACT_RET_PRS_ERR;
7833 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007834
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007835 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007836 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007837
7838 /* TODO: later accept arguments. */
7839 rule->arg.hlua_rule->args = NULL;
7840
7841 /* Add applet pointer in the rule. */
7842 rule->applet.obj_type = OBJ_TYPE_APPLET;
7843 rule->applet.name = fcn->name;
7844 rule->applet.init = hlua_applet_tcp_init;
7845 rule->applet.fct = hlua_applet_tcp_fct;
7846 rule->applet.release = hlua_applet_tcp_release;
7847 rule->applet.timeout = hlua_timeout_applet;
7848
7849 return 0;
7850}
7851
7852/* This function is an LUA binding used for registering
7853 * "sample-conv" functions. It expects a converter name used
7854 * in the haproxy configuration file, and an LUA function.
7855 */
7856__LJMP static int hlua_register_service(lua_State *L)
7857{
7858 struct action_kw_list *akl;
7859 const char *name;
7860 const char *env;
7861 int ref;
7862 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007863 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007864 struct buffer *trash;
7865 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007866
7867 MAY_LJMP(check_args(L, 3, "register_service"));
7868
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01007869 if (hlua_gethlua(L)) {
7870 /* runtime processing */
7871 WILL_LJMP(luaL_error(L, "register_service: not available outside of body context"));
7872 }
7873
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007874 /* First argument : converter name. */
7875 name = MAY_LJMP(luaL_checkstring(L, 1));
7876
7877 /* Second argument : environment. */
7878 env = MAY_LJMP(luaL_checkstring(L, 2));
7879
7880 /* Third argument : lua function. */
7881 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7882
Thierry Fournierf67442e2020-11-28 20:41:07 +01007883 /* Check for service already registered */
7884 trash = get_trash_chunk();
7885 chunk_printf(trash, "lua.%s", name);
7886 akw = service_find(trash->area);
7887 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007888 fcn = akw->private;
7889 if (fcn->function_ref[hlua_state_id] != -1) {
7890 ha_warning("Trying to register service 'lua.%s' more than once. "
7891 "This will become a hard error in version 2.5.\n", name);
7892 }
7893 fcn->function_ref[hlua_state_id] = ref;
7894 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007895 }
7896
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007897 /* Allocate and fill the sample fetch keyword struct. */
7898 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7899 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007900 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007901 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007902 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007903 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007904
7905 /* Fill fcn. */
7906 len = strlen("<lua.>") + strlen(name) + 1;
7907 fcn->name = calloc(1, len);
7908 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007909 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007910 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007911 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007912
7913 /* List head */
7914 akl->list.n = akl->list.p = NULL;
7915
7916 /* converter keyword. */
7917 len = strlen("lua.") + strlen(name) + 1;
7918 akl->kw[0].kw = calloc(1, len);
7919 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007920 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007921
7922 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7923
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007924 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007925 if (strcmp(env, "tcp") == 0)
7926 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007927 else if (strcmp(env, "http") == 0)
7928 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007929 else {
7930 release_hlua_function(fcn);
7931 if (akl)
7932 ha_free((char **)&(akl->kw[0].kw));
7933 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007934 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007935 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02007936 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007937
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007938 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007939 akl->kw[0].private = fcn;
7940
7941 /* End of array. */
7942 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7943
7944 /* Register this new converter */
7945 service_keywords_register(akl);
7946
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007947 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007948
7949 alloc_error:
7950 release_hlua_function(fcn);
7951 ha_free(&akl);
7952 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7953 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007954}
7955
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007956/* This function initialises Lua cli handler. It copies the
7957 * arguments in the Lua stack and create channel IO objects.
7958 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007959static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007960{
7961 struct hlua *hlua;
7962 struct hlua_function *fcn;
7963 int i;
7964 const char *error;
7965
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007966 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007967 appctx->ctx.hlua_cli.fcn = private;
7968
Willy Tarreaubafbe012017-11-24 17:34:44 +01007969 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007970 if (!hlua) {
7971 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007972 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007973 }
7974 HLUA_INIT(hlua);
7975 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007976
7977 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007978 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007979 * applet_http. It is absolutely compatible.
7980 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007981 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007982 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007983 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007984 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007985 }
7986 appctx->ctx.hlua_cli.task->nice = 0;
7987 appctx->ctx.hlua_cli.task->context = appctx;
7988 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7989
7990 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01007991 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 +01007992 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007993 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007994 }
7995
7996 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007997 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007998 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7999 error = lua_tostring(hlua->T, -1);
8000 else
8001 error = "critical error";
8002 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
8003 goto error;
8004 }
8005
8006 /* Check stack available size. */
8007 if (!lua_checkstack(hlua->T, 2)) {
8008 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8009 goto error;
8010 }
8011
8012 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008013 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008014
8015 /* Once the arguments parsed, the CLI is like an AppletTCP,
8016 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008017 */
8018 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
8019 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8020 goto error;
8021 }
8022 hlua->nargs = 1;
8023
8024 /* push keywords in the stack. */
8025 for (i = 0; *args[i]; i++) {
8026 /* Check stack available size. */
8027 if (!lua_checkstack(hlua->T, 1)) {
8028 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8029 goto error;
8030 }
8031 lua_pushstring(hlua->T, args[i]);
8032 hlua->nargs++;
8033 }
8034
8035 /* We must initialize the execution timeouts. */
8036 hlua->max_time = hlua_timeout_session;
8037
8038 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008039 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008040
8041 /* It's ok */
8042 return 0;
8043
8044 /* It's not ok. */
8045error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008046 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008047 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008048 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008049 return 1;
8050}
8051
8052static int hlua_cli_io_handler_fct(struct appctx *appctx)
8053{
8054 struct hlua *hlua;
8055 struct stream_interface *si;
8056 struct hlua_function *fcn;
8057
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008058 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008059 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008060 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008061
8062 /* If the stream is disconnect or closed, ldo nothing. */
8063 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8064 return 1;
8065
8066 /* Execute the function. */
8067 switch (hlua_ctx_resume(hlua, 1)) {
8068
8069 /* finished. */
8070 case HLUA_E_OK:
8071 return 1;
8072
8073 /* yield. */
8074 case HLUA_E_AGAIN:
8075 /* We want write. */
8076 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008077 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008078 /* Set the timeout. */
8079 if (hlua->wake_time != TICK_ETERNITY)
8080 task_schedule(hlua->task, hlua->wake_time);
8081 return 0;
8082
8083 /* finished with error. */
8084 case HLUA_E_ERRMSG:
8085 /* Display log. */
8086 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8087 fcn->name, lua_tostring(hlua->T, -1));
8088 lua_pop(hlua->T, 1);
8089 return 1;
8090
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008091 case HLUA_E_ETMOUT:
8092 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8093 fcn->name);
8094 return 1;
8095
8096 case HLUA_E_NOMEM:
8097 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8098 fcn->name);
8099 return 1;
8100
8101 case HLUA_E_YIELD: /* unexpected */
8102 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8103 fcn->name);
8104 return 1;
8105
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008106 case HLUA_E_ERR:
8107 /* Display log. */
8108 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8109 fcn->name);
8110 return 1;
8111
8112 default:
8113 return 1;
8114 }
8115
8116 return 1;
8117}
8118
8119static void hlua_cli_io_release_fct(struct appctx *appctx)
8120{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008121 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008122 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008123}
8124
8125/* This function is an LUA binding used for registering
8126 * new keywords in the cli. It expects a list of keywords
8127 * which are the "path". It is limited to 5 keywords. A
8128 * description of the command, a function to be executed
8129 * for the parsing and a function for io handlers.
8130 */
8131__LJMP static int hlua_register_cli(lua_State *L)
8132{
8133 struct cli_kw_list *cli_kws;
8134 const char *message;
8135 int ref_io;
8136 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008137 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008138 int index;
8139 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008140 struct buffer *trash;
8141 const char *kw[5];
8142 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008143 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008144
8145 MAY_LJMP(check_args(L, 3, "register_cli"));
8146
Aurelien DARRAGON8985ab82023-02-24 09:43:54 +01008147 if (hlua_gethlua(L)) {
8148 /* runtime processing */
8149 WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context"));
8150 }
8151
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008152 /* First argument : an array of maximum 5 keywords. */
8153 if (!lua_istable(L, 1))
8154 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8155
8156 /* Second argument : string with contextual message. */
8157 message = MAY_LJMP(luaL_checkstring(L, 2));
8158
8159 /* Third and fourth argument : lua function. */
8160 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8161
Thierry Fournierf67442e2020-11-28 20:41:07 +01008162 /* Check for CLI service already registered */
8163 trash = get_trash_chunk();
8164 index = 0;
8165 lua_pushnil(L);
8166 memset(kw, 0, sizeof(kw));
8167 while (lua_next(L, 1) != 0) {
8168 if (index >= CLI_PREFIX_KW_NB)
8169 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8170 if (lua_type(L, -1) != LUA_TSTRING)
8171 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8172 kw[index] = lua_tostring(L, -1);
8173 if (index == 0)
8174 chunk_printf(trash, "%s", kw[index]);
8175 else
8176 chunk_appendf(trash, " %s", kw[index]);
8177 index++;
8178 lua_pop(L, 1);
8179 }
8180 cli_kw = cli_find_kw_exact((char **)kw);
8181 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008182 fcn = cli_kw->private;
8183 if (fcn->function_ref[hlua_state_id] != -1) {
8184 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8185 "This will become a hard error in version 2.5.\n", trash->area);
8186 }
8187 fcn->function_ref[hlua_state_id] = ref_io;
8188 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008189 }
8190
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008191 /* Allocate and fill the sample fetch keyword struct. */
8192 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008193 if (!cli_kws) {
8194 errmsg = "Lua out of memory error.";
8195 goto error;
8196 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008197 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008198 if (!fcn) {
8199 errmsg = "Lua out of memory error.";
8200 goto error;
8201 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008202
8203 /* Fill path. */
8204 index = 0;
8205 lua_pushnil(L);
8206 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008207 if (index >= 5) {
8208 errmsg = "1st argument must be a table with a maximum of 5 entries";
8209 goto error;
8210 }
8211 if (lua_type(L, -1) != LUA_TSTRING) {
8212 errmsg = "1st argument must be a table filled with strings";
8213 goto error;
8214 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008215 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008216 if (!cli_kws->kw[0].str_kw[index]) {
8217 errmsg = "Lua out of memory error.";
8218 goto error;
8219 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008220 index++;
8221 lua_pop(L, 1);
8222 }
8223
8224 /* Copy help message. */
8225 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008226 if (!cli_kws->kw[0].usage) {
8227 errmsg = "Lua out of memory error.";
8228 goto error;
8229 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008230
8231 /* Fill fcn io handler. */
8232 len = strlen("<lua.cli>") + 1;
8233 for (i = 0; i < index; i++)
8234 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8235 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008236 if (!fcn->name) {
8237 errmsg = "Lua out of memory error.";
8238 goto error;
8239 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008240 strncat((char *)fcn->name, "<lua.cli", len);
8241 for (i = 0; i < index; i++) {
8242 strncat((char *)fcn->name, ".", len);
8243 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8244 }
8245 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008246 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008247
8248 /* Fill last entries. */
8249 cli_kws->kw[0].private = fcn;
8250 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8251 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8252 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8253
8254 /* Register this new converter */
8255 cli_register_kw(cli_kws);
8256
8257 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008258
8259 error:
8260 release_hlua_function(fcn);
8261 if (cli_kws) {
8262 for (i = 0; i < index; i++)
8263 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8264 ha_free((char **)&(cli_kws->kw[0].usage));
8265 }
8266 ha_free(&cli_kws);
8267 WILL_LJMP(luaL_error(L, errmsg));
8268 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008269}
8270
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008271static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008272 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008273 char **err, unsigned int *timeout)
8274{
8275 const char *error;
8276
8277 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008278 if (error == PARSE_TIME_OVER) {
8279 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8280 args[1], args[0]);
8281 return -1;
8282 }
8283 else if (error == PARSE_TIME_UNDER) {
8284 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8285 args[1], args[0]);
8286 return -1;
8287 }
8288 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008289 memprintf(err, "%s: invalid timeout", args[0]);
8290 return -1;
8291 }
8292 return 0;
8293}
8294
8295static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008296 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008297 char **err)
8298{
8299 return hlua_read_timeout(args, section_type, curpx, defpx,
8300 file, line, err, &hlua_timeout_session);
8301}
8302
8303static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008304 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008305 char **err)
8306{
8307 return hlua_read_timeout(args, section_type, curpx, defpx,
8308 file, line, err, &hlua_timeout_task);
8309}
8310
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008311static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008312 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008313 char **err)
8314{
8315 return hlua_read_timeout(args, section_type, curpx, defpx,
8316 file, line, err, &hlua_timeout_applet);
8317}
8318
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008319static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008320 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008321 char **err)
8322{
8323 char *error;
8324
8325 hlua_nb_instruction = strtoll(args[1], &error, 10);
8326 if (*error != '\0') {
8327 memprintf(err, "%s: invalid number", args[0]);
8328 return -1;
8329 }
8330 return 0;
8331}
8332
Willy Tarreau32f61e22015-03-18 17:54:59 +01008333static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008334 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008335 char **err)
8336{
8337 char *error;
8338
8339 if (*(args[1]) == 0) {
8340 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8341 return -1;
8342 }
8343 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8344 if (*error != '\0') {
8345 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8346 return -1;
8347 }
8348 return 0;
8349}
8350
8351
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008352/* This function is called by the main configuration key "lua-load". It loads and
8353 * execute an lua file during the parsing of the HAProxy configuration file. It is
8354 * the main lua entry point.
8355 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008356 * This function runs with the HAProxy keywords API. It returns -1 if an error
8357 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008358 *
8359 * In some error case, LUA set an error message in top of the stack. This function
8360 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008361 *
8362 * This function can fail with an abort() due to an Lua critical error.
8363 * We are in the configuration parsing process of HAProxy, this abort() is
8364 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008365 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008366static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008367{
8368 int error;
8369
8370 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008371 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008372 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008373 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8374 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008375 return -1;
8376 }
8377
8378 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008379 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008380 switch (error) {
8381 case LUA_OK:
8382 break;
8383 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008384 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8385 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008386 return -1;
8387 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008388 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008389 return -1;
8390 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008391 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8392 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008393 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008394#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008395 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008396 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8397 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008398 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008399#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008400 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008401 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8402 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008403 return -1;
8404 }
8405
8406 return 0;
8407}
8408
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008409static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008410 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008411 char **err)
8412{
8413 if (*(args[1]) == 0) {
8414 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8415 return -1;
8416 }
8417
Thierry Fournier59f11be2020-11-29 00:37:41 +01008418 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008419 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008420 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008421 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008422}
8423
Thierry Fournier59f11be2020-11-29 00:37:41 +01008424static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008425 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008426 char **err)
8427{
8428 int len;
8429
8430 if (*(args[1]) == 0) {
8431 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8432 return -1;
8433 }
8434
8435 if (per_thread_load == NULL) {
8436 /* allocate the first entry large enough to store the final NULL */
8437 per_thread_load = calloc(1, sizeof(*per_thread_load));
8438 if (per_thread_load == NULL) {
8439 memprintf(err, "out of memory error");
8440 return -1;
8441 }
8442 }
8443
8444 /* count used entries */
8445 for (len = 0; per_thread_load[len] != NULL; len++)
8446 ;
8447
8448 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8449 if (per_thread_load == NULL) {
8450 memprintf(err, "out of memory error");
8451 return -1;
8452 }
8453
8454 per_thread_load[len] = strdup(args[1]);
8455 per_thread_load[len + 1] = NULL;
8456
8457 if (per_thread_load[len] == NULL) {
8458 memprintf(err, "out of memory error");
8459 return -1;
8460 }
8461
8462 /* loading for thread 1 only */
8463 hlua_state_id = 1;
8464 ha_set_tid(0);
8465 return hlua_load_state(args[1], hlua_states[1], err);
8466}
8467
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008468/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8469 * in the given <ctx>.
8470 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008471static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008472{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008473 lua_getglobal(L, "package"); /* push package variable */
8474 lua_pushstring(L, path); /* push given path */
8475 lua_pushstring(L, ";"); /* push semicolon */
8476 lua_getfield(L, -3, type); /* push old path */
8477 lua_concat(L, 3); /* concatenate to new path */
8478 lua_setfield(L, -2, type); /* store new path */
8479 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008480
8481 return 0;
8482}
8483
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008484static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008485 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008486 char **err)
8487{
8488 char *path;
8489 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008490 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008491
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008492 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008493 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008494 }
8495
8496 if (!(*args[1])) {
8497 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008498 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008499 }
8500 path = args[1];
8501
8502 if (*args[2]) {
8503 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8504 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008505 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008506 }
8507 type = args[2];
8508 }
8509
Thierry Fournier59f11be2020-11-29 00:37:41 +01008510 p = calloc(1, sizeof(*p));
8511 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008512 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008513 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008514 }
8515 p->path = strdup(path);
8516 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008517 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008518 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008519 }
8520 p->type = strdup(type);
8521 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008522 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008523 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008524 }
Willy Tarreau2b718102021-04-21 07:32:39 +02008525 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01008526
8527 hlua_prepend_path(hlua_states[0], type, path);
8528 hlua_prepend_path(hlua_states[1], type, path);
8529 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008530
8531err2:
8532 free(p->type);
8533 free(p->path);
8534err:
8535 free(p);
8536 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008537}
8538
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008539/* configuration keywords declaration */
8540static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008541 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008542 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008543 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008544 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8545 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008546 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008547 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008548 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008549 { 0, NULL, NULL },
8550}};
8551
Willy Tarreau0108d902018-11-25 19:14:37 +01008552INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8553
Christopher Fauletafd8f102018-11-08 11:34:21 +01008554
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008555/* This function can fail with an abort() due to an Lua critical error.
8556 * We are in the initialisation process of HAProxy, this abort() is
8557 * tolerated.
8558 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008559int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008560{
8561 struct hlua_init_function *init;
8562 const char *msg;
8563 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008564 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008565 const char *kind;
8566 const char *trace;
8567 int return_status = 1;
8568#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8569 int nres;
8570#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008571
Willy Tarreaucdb53462020-12-02 12:12:00 +01008572 /* disable memory limit checks if limit is not set */
8573 if (!hlua_global_allocator.limit)
8574 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8575
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008576 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008577 if (setjmp(safe_ljmp_env) != 0) {
8578 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008579 if (lua_type(L, -1) == LUA_TSTRING)
8580 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008581 else
8582 error = "critical error";
8583 fprintf(stderr, "Lua post-init: %s.\n", error);
8584 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008585 } else {
8586 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008587 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008588
Thierry Fournierb8cef172020-11-28 15:37:17 +01008589 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008590
Thierry Fournierc7492592020-11-28 23:57:24 +01008591 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008592 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008593
8594#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008595 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008596#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008597 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008598#endif
8599 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008600 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008601
8602 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008603 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008604 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008605
8606 case LUA_ERRERR:
8607 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008608 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008609 case LUA_ERRRUN:
8610 if (!kind)
8611 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008612 msg = lua_tostring(L, -1);
8613 lua_settop(L, 0); /* Empty the stack. */
8614 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008615 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008616 if (msg)
8617 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8618 else
8619 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8620 return_status = 0;
8621 break;
8622
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008623 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008624 /* Unknown error */
8625 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008626 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008627 case LUA_YIELD:
8628 /* yield is not configured at this step, this state doesn't happen */
8629 if (!kind)
8630 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008631 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008632 case LUA_ERRMEM:
8633 if (!kind)
8634 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008635 lua_settop(L, 0);
8636 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008637 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008638 ha_alert("Lua init: %s: %s\n", kind, trace);
8639 return_status = 0;
8640 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008641 }
Thierry Fournier670db242020-11-28 10:49:59 +01008642 if (!return_status)
8643 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008644 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008645
8646 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008647 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008648}
8649
Thierry Fournierb8cef172020-11-28 15:37:17 +01008650int hlua_post_init()
8651{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008652 int ret;
8653 int i;
8654 int errors;
8655 char *err = NULL;
8656 struct hlua_function *fcn;
8657
Willy Tarreau42afc592021-08-30 09:35:18 +02008658#if defined(USE_OPENSSL)
Thierry Fournierb8cef172020-11-28 15:37:17 +01008659 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008660 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008661 int saved_used_backed = global.ssl_used_backend;
8662 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008663 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008664 global.ssl_used_backend = saved_used_backed;
8665 }
8666#endif
8667
Thierry Fournierc7492592020-11-28 23:57:24 +01008668 /* Perform post init of common thread */
8669 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008670 ha_set_tid(0);
8671 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8672 if (ret == 0)
8673 return 0;
8674
8675 /* init remaining lua states and load files */
8676 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8677
8678 /* set thread context */
8679 ha_set_tid(hlua_state_id - 1);
8680
8681 /* Init lua state */
8682 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8683
8684 /* Load lua files */
8685 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8686 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8687 if (ret != 0) {
8688 ha_alert("Lua init: %s\n", err);
8689 return 0;
8690 }
8691 }
8692 }
8693
8694 /* Reset thread context */
8695 ha_set_tid(0);
8696
8697 /* Execute post init for all states */
8698 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8699
8700 /* set thread context */
8701 ha_set_tid(hlua_state_id - 1);
8702
8703 /* run post init */
8704 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8705 if (ret == 0)
8706 return 0;
8707 }
8708
8709 /* Reset thread context */
8710 ha_set_tid(0);
8711
8712 /* control functions registering. Each function must have:
8713 * - only the function_ref[0] set positive and all other to -1
8714 * - only the function_ref[0] set to -1 and all other positive
8715 * This ensure a same reference is not used both in shared
8716 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008717 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008718 * complicated to found for the end user.
8719 */
8720 errors = 0;
8721 list_for_each_entry(fcn, &referenced_functions, l) {
8722 ret = 0;
8723 for (i = 1; i < global.nbthread + 1; i++) {
8724 if (fcn->function_ref[i] == -1)
8725 ret--;
8726 else
8727 ret++;
8728 }
8729 if (abs(ret) != global.nbthread) {
8730 ha_alert("Lua function '%s' is not referenced in all thread. "
8731 "Expect function in all thread or in none thread.\n", fcn->name);
8732 errors++;
8733 continue;
8734 }
8735
8736 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008737 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8738 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008739 "exclusive.\n", fcn->name);
8740 errors++;
8741 }
8742 }
8743
8744 if (errors > 0)
8745 return 0;
8746
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008747 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008748 * -1 in order to have probably a segfault if someone use it
8749 */
8750 hlua_state_id = -1;
8751
8752 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008753}
8754
Willy Tarreau32f61e22015-03-18 17:54:59 +01008755/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8756 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8757 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008758 * allocation. <nsize> is the requested new size. A new allocation is
8759 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008760 * zero. This one verifies that the limits are respected but is optimized
8761 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreaue00ad052021-10-22 16:00:02 +02008762 *
8763 * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses
8764 * POSIX by making realloc(ptr,0) an effective free(), but others do not do
8765 * that and will simply allocate zero as if it were the result of malloc(0),
8766 * so mapping this onto realloc() will lead to memory leaks on non-glibc
8767 * systems.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008768 */
8769static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8770{
8771 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008772 size_t limit, old, new;
8773
Tim Duesterhus22586522021-01-08 10:35:33 +01008774 if (unlikely(!ptr && !nsize))
8775 return NULL;
8776
Willy Tarreaucdb53462020-12-02 12:12:00 +01008777 /* a limit of ~0 means unlimited and boot complete, so there's no need
8778 * for accounting anymore.
8779 */
Willy Tarreaue00ad052021-10-22 16:00:02 +02008780 if (likely(~zone->limit == 0)) {
8781 if (!nsize)
8782 ha_free(&ptr);
8783 else
8784 ptr = realloc(ptr, nsize);
8785 return ptr;
8786 }
Willy Tarreau32f61e22015-03-18 17:54:59 +01008787
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008788 if (!ptr)
8789 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008790
Willy Tarreaucdb53462020-12-02 12:12:00 +01008791 /* enforce strict limits across all threads */
8792 limit = zone->limit;
8793 old = _HA_ATOMIC_LOAD(&zone->allocated);
8794 do {
8795 new = old + nsize - osize;
8796 if (unlikely(nsize && limit && new > limit))
8797 return NULL;
8798 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008799
Willy Tarreaue00ad052021-10-22 16:00:02 +02008800 if (!nsize)
8801 ha_free(&ptr);
8802 else
8803 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008804
8805 if (unlikely(!ptr && nsize)) // failed
8806 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8807
8808 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008809 return ptr;
8810}
8811
Thierry Fournierecb83c22020-11-28 15:49:44 +01008812/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008813 * We are in the initialisation process of HAProxy, this abort() is
8814 * tolerated.
8815 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008816lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008817{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008818 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008819 int idx;
8820 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008821 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008822 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008823 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008824 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008825 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008826 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008827
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008828 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008829 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008830
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008831 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008832 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008833 *context = NULL;
8834
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008835 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008836 * the Lua function can fail with an abort. We are in the initialisation
8837 * process of HAProxy, this abort() is tolerated.
8838 */
8839
Thierry Fournier3c539322020-11-28 16:05:05 +01008840 /* Call post initialisation function in safe environment. */
8841 if (setjmp(safe_ljmp_env) != 0) {
8842 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008843 if (lua_type(L, -1) == LUA_TSTRING)
8844 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008845 else
8846 error_msg = "critical error";
8847 fprintf(stderr, "Lua init: %s.\n", error_msg);
8848 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008849 } else {
8850 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008851 }
8852
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008853 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008854 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008855#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8856#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8857#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008858 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008859#endif
8860#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008861 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008862#endif
8863#undef HLUA_PREPEND_PATH_TOSTRING
8864#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008865
Thierry Fournier59f11be2020-11-29 00:37:41 +01008866 /* Apply configured prepend path */
8867 list_for_each_entry(pp, &prepend_path_list, l)
8868 hlua_prepend_path(L, pp->type, pp->path);
8869
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008870 /*
8871 *
8872 * Create "core" object.
8873 *
8874 */
8875
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008876 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008877 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008878
Thierry Fournierecb83c22020-11-28 15:49:44 +01008879 /* set the thread id */
8880 hlua_class_const_int(L, "thread", thread_num);
8881
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008882 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008883 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008884 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008885
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008886 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008887 hlua_class_function(L, "register_init", hlua_register_init);
8888 hlua_class_function(L, "register_task", hlua_register_task);
8889 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8890 hlua_class_function(L, "register_converters", hlua_register_converters);
8891 hlua_class_function(L, "register_action", hlua_register_action);
8892 hlua_class_function(L, "register_service", hlua_register_service);
8893 hlua_class_function(L, "register_cli", hlua_register_cli);
8894 hlua_class_function(L, "yield", hlua_yield);
8895 hlua_class_function(L, "set_nice", hlua_set_nice);
8896 hlua_class_function(L, "sleep", hlua_sleep);
8897 hlua_class_function(L, "msleep", hlua_msleep);
8898 hlua_class_function(L, "add_acl", hlua_add_acl);
8899 hlua_class_function(L, "del_acl", hlua_del_acl);
8900 hlua_class_function(L, "set_map", hlua_set_map);
8901 hlua_class_function(L, "del_map", hlua_del_map);
8902 hlua_class_function(L, "tcp", hlua_socket_new);
8903 hlua_class_function(L, "log", hlua_log);
8904 hlua_class_function(L, "Debug", hlua_log_debug);
8905 hlua_class_function(L, "Info", hlua_log_info);
8906 hlua_class_function(L, "Warning", hlua_log_warning);
8907 hlua_class_function(L, "Alert", hlua_log_alert);
8908 hlua_class_function(L, "done", hlua_done);
8909 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008910
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008911 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008912
8913 /*
8914 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008915 * Create "act" object.
8916 *
8917 */
8918
8919 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008920 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008921
8922 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008923 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8924 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8925 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8926 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8927 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8928 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8929 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8930 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008931
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008932 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008933
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008934 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008935
8936 /*
8937 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008938 * Register class Map
8939 *
8940 */
8941
8942 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008943 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008944
8945 /* register pattern types. */
8946 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008947 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008948 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008949 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008950 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008951 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008952
8953 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008954 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008955
8956 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008957 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008958
Ilya Shipitsind4259502020-04-08 01:07:56 +05008959 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008960 lua_pushstring(L, "__index");
8961 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008962
8963 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008964 hlua_class_function(L, "lookup", hlua_map_lookup);
8965 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008966
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008967 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008968
Thierry Fournier45e78d72016-02-19 18:34:46 +01008969 /* Register previous table in the registry with reference and named entry.
8970 * The function hlua_register_metatable() pops the stack, so we
8971 * previously create a copy of the table.
8972 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008973 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8974 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008975
8976 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008977 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008978
8979 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008980 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008981
8982 /*
8983 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008984 * Register class Channel
8985 *
8986 */
8987
8988 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008989 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008990
Ilya Shipitsind4259502020-04-08 01:07:56 +05008991 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008992 lua_pushstring(L, "__index");
8993 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008994
8995 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008996 hlua_class_function(L, "get", hlua_channel_get);
8997 hlua_class_function(L, "dup", hlua_channel_dup);
8998 hlua_class_function(L, "getline", hlua_channel_getline);
8999 hlua_class_function(L, "set", hlua_channel_set);
9000 hlua_class_function(L, "append", hlua_channel_append);
9001 hlua_class_function(L, "send", hlua_channel_send);
9002 hlua_class_function(L, "forward", hlua_channel_forward);
9003 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
9004 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
9005 hlua_class_function(L, "is_full", hlua_channel_is_full);
9006 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009007
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009008 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009009
9010 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009011 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009012
9013 /*
9014 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009015 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009016 *
9017 */
9018
9019 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009020 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009021
Ilya Shipitsind4259502020-04-08 01:07:56 +05009022 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009023 lua_pushstring(L, "__index");
9024 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009025
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009026 /* Browse existing fetches and create the associated
9027 * object method.
9028 */
9029 sf = NULL;
9030 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009031 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9032 * by an underscore.
9033 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009034 strlcpy2(trash.area, sf->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009035 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009036 if (*p == '.' || *p == '-' || *p == '+')
9037 *p = '_';
9038
9039 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009040 lua_pushstring(L, trash.area);
9041 lua_pushlightuserdata(L, sf);
9042 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
9043 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009044 }
9045
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009046 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009047
9048 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009049 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009050
9051 /*
9052 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009053 * Register class Converters
9054 *
9055 */
9056
9057 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009058 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009059
9060 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009061 lua_pushstring(L, "__index");
9062 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009063
9064 /* Browse existing converters and create the associated
9065 * object method.
9066 */
9067 sc = NULL;
9068 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009069 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9070 * by an underscore.
9071 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009072 strlcpy2(trash.area, sc->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009073 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009074 if (*p == '.' || *p == '-' || *p == '+')
9075 *p = '_';
9076
9077 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009078 lua_pushstring(L, trash.area);
9079 lua_pushlightuserdata(L, sc);
9080 lua_pushcclosure(L, hlua_run_sample_conv, 1);
9081 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009082 }
9083
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009084 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009085
9086 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009087 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009088
9089 /*
9090 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009091 * Register class HTTP
9092 *
9093 */
9094
9095 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009096 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009097
Ilya Shipitsind4259502020-04-08 01:07:56 +05009098 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009099 lua_pushstring(L, "__index");
9100 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009101
9102 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009103 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9104 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9105 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9106 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9107 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9108 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9109 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9110 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9111 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9112 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009113
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009114 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9115 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9116 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9117 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9118 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9119 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9120 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009121
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009122 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009123
9124 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009125 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009126
9127 /*
9128 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009129 * Register class AppletTCP
9130 *
9131 */
9132
9133 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009134 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009135
Ilya Shipitsind4259502020-04-08 01:07:56 +05009136 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009137 lua_pushstring(L, "__index");
9138 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009139
9140 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009141 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9142 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9143 hlua_class_function(L, "send", hlua_applet_tcp_send);
9144 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9145 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9146 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9147 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9148 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009149
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009150 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009151
9152 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009153 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009154
9155 /*
9156 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009157 * Register class AppletHTTP
9158 *
9159 */
9160
9161 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009162 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009163
Ilya Shipitsind4259502020-04-08 01:07:56 +05009164 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009165 lua_pushstring(L, "__index");
9166 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009167
9168 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009169 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9170 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9171 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9172 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9173 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9174 hlua_class_function(L, "getline", hlua_applet_http_getline);
9175 hlua_class_function(L, "receive", hlua_applet_http_recv);
9176 hlua_class_function(L, "send", hlua_applet_http_send);
9177 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9178 hlua_class_function(L, "set_status", hlua_applet_http_status);
9179 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009180
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009181 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009182
9183 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009184 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009185
9186 /*
9187 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009188 * Register class TXN
9189 *
9190 */
9191
9192 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009193 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009194
Ilya Shipitsind4259502020-04-08 01:07:56 +05009195 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009196 lua_pushstring(L, "__index");
9197 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009198
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009199 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009200 hlua_class_function(L, "set_priv", hlua_set_priv);
9201 hlua_class_function(L, "get_priv", hlua_get_priv);
9202 hlua_class_function(L, "set_var", hlua_set_var);
9203 hlua_class_function(L, "unset_var", hlua_unset_var);
9204 hlua_class_function(L, "get_var", hlua_get_var);
9205 hlua_class_function(L, "done", hlua_txn_done);
9206 hlua_class_function(L, "reply", hlua_txn_reply_new);
9207 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9208 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9209 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9210 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9211 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9212 hlua_class_function(L, "deflog", hlua_txn_deflog);
9213 hlua_class_function(L, "log", hlua_txn_log);
9214 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9215 hlua_class_function(L, "Info", hlua_txn_log_info);
9216 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9217 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009218
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009219 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009220
9221 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009222 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009223
9224 /*
9225 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009226 * Register class reply
9227 *
9228 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009229 lua_newtable(L);
9230 lua_pushstring(L, "__index");
9231 lua_newtable(L);
9232 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9233 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9234 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9235 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9236 lua_settable(L, -3); /* Sets the __index entry. */
9237 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009238
9239
9240 /*
9241 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009242 * Register class Socket
9243 *
9244 */
9245
9246 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009247 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009248
Ilya Shipitsind4259502020-04-08 01:07:56 +05009249 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009250 lua_pushstring(L, "__index");
9251 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009252
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009253#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009254 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009255#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009256 hlua_class_function(L, "connect", hlua_socket_connect);
9257 hlua_class_function(L, "send", hlua_socket_send);
9258 hlua_class_function(L, "receive", hlua_socket_receive);
9259 hlua_class_function(L, "close", hlua_socket_close);
9260 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9261 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9262 hlua_class_function(L, "setoption", hlua_socket_setoption);
9263 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009264
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009265 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009266
9267 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009268 lua_pushstring(L, "__gc");
9269 lua_pushcclosure(L, hlua_socket_gc, 0);
9270 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009271
9272 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009273 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009274
Thierry Fournieraafc7772020-12-04 11:47:47 +01009275 lua_atpanic(L, hlua_panic_safe);
9276
9277 return L;
9278}
9279
9280void hlua_init(void) {
9281 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009282 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009283#ifdef USE_OPENSSL
9284 struct srv_kw *kw;
9285 int tmp_error;
9286 char *error;
9287 char *args[] = { /* SSL client configuration. */
9288 "ssl",
9289 "verify",
9290 "none",
9291 NULL
9292 };
9293#endif
9294
9295 /* Init post init function list head */
9296 for (i = 0; i < MAX_THREADS + 1; i++)
9297 LIST_INIT(&hlua_init_functions[i]);
9298
9299 /* Init state for common/shared lua parts */
9300 hlua_state_id = 0;
9301 ha_set_tid(0);
9302 hlua_states[0] = hlua_init_state(0);
9303
9304 /* Init state 1 for thread 0. We have at least one thread. */
9305 hlua_state_id = 1;
9306 ha_set_tid(0);
9307 hlua_states[1] = hlua_init_state(1);
9308
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009309 /* Proxy and server configuration initialisation. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009310 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg);
9311 if (!socket_proxy) {
9312 fprintf(stderr, "Lua init: %s\n", errmsg);
9313 exit(1);
9314 }
9315 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009316
9317 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009318 socket_tcp = new_server(socket_proxy);
9319 if (!socket_tcp) {
9320 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9321 exit(1);
9322 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009323
9324#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009325 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009326 socket_ssl = new_server(socket_proxy);
9327 if (!socket_ssl) {
9328 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9329 exit(1);
9330 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009331
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009332 socket_ssl->use_ssl = 1;
9333 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009334
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009335 for (i = 0; args[i] != NULL; i++) {
9336 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009337 /*
9338 *
9339 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009340 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009341 * features like client certificates and ssl_verify.
9342 *
9343 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009344 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009345 if (tmp_error != 0) {
9346 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9347 abort(); /* This must be never arrives because the command line
9348 not editable by the user. */
9349 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009350 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009351 }
9352 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009353#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009354
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009355}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009356
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009357static void hlua_deinit()
9358{
Willy Tarreau186f3762020-12-04 11:48:12 +01009359 int thr;
9360
9361 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9362 if (hlua_states[thr])
9363 lua_close(hlua_states[thr]);
9364 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009365
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009366 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009367
Willy Tarreau0f143af2021-03-05 10:41:48 +01009368#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009369 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009370#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009371
9372 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009373}
9374
9375REGISTER_POST_DEINIT(hlua_deinit);
9376
Willy Tarreau80713382018-11-26 10:19:54 +01009377static void hlua_register_build_options(void)
9378{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009379 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009380
Willy Tarreaubb57d942016-12-21 19:04:56 +01009381 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9382 hap_register_build_opts(ptr, 1);
9383}
Willy Tarreau80713382018-11-26 10:19:54 +01009384
9385INITCALL0(STG_REGISTER, hlua_register_build_options);