blob: 964c6177d56c517bcd557c807d22481f75acc13e [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:
741 argp[idx].data.prx = p;
742 argp[idx].type = ARGT_TAB;
743 argp[idx+1].type = ARGT_STOP;
744 break;
745
746 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200747 msg = "Mandatory argument expected";
748 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100749 break;
750 }
751 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200752 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100753 }
754
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500755 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100756 if ((mask & ARGT_MASK) == ARGT_STOP &&
757 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200758 msg = "Last argument expected";
759 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100760 }
761
762 if ((mask & ARGT_MASK) == ARGT_STOP &&
763 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200764 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100765 }
766
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200767 /* Convert some argument types. All string in argp[] are for not
768 * duplicated yet.
769 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100770 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100771 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200772 if (argp[idx].type != ARGT_SINT) {
773 msg = "integer expected";
774 goto error;
775 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100776 argp[idx].type = ARGT_SINT;
777 break;
778
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100779 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200780 if (argp[idx].type != ARGT_SINT) {
781 msg = "integer expected";
782 goto error;
783 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200784 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100785 break;
786
787 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200788 if (argp[idx].type != ARGT_SINT) {
789 msg = "integer expected";
790 goto error;
791 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200792 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100793 break;
794
795 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200796 if (argp[idx].type != ARGT_STR) {
797 msg = "string expected";
798 goto error;
799 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200800 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200801 if (!argp[idx].data.prx) {
802 msg = "frontend doesn't exist";
803 goto error;
804 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100805 argp[idx].type = ARGT_FE;
806 break;
807
808 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200809 if (argp[idx].type != ARGT_STR) {
810 msg = "string expected";
811 goto error;
812 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200813 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200814 if (!argp[idx].data.prx) {
815 msg = "backend doesn't exist";
816 goto error;
817 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100818 argp[idx].type = ARGT_BE;
819 break;
820
821 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200822 if (argp[idx].type != ARGT_STR) {
823 msg = "string expected";
824 goto error;
825 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200826 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200827 if (!argp[idx].data.t) {
828 msg = "table doesn't exist";
829 goto error;
830 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100831 argp[idx].type = ARGT_TAB;
832 break;
833
834 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200835 if (argp[idx].type != ARGT_STR) {
836 msg = "string expected";
837 goto error;
838 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200839 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100840 if (sname) {
841 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200842 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200843 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200844 if (!px) {
845 msg = "backend doesn't exist";
846 goto error;
847 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100848 }
849 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200850 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100851 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100852 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100853 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200854 if (!argp[idx].data.srv) {
855 msg = "server doesn't exist";
856 goto error;
857 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100858 argp[idx].type = ARGT_SRV;
859 break;
860
861 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200862 if (argp[idx].type != ARGT_STR) {
863 msg = "string expected";
864 goto error;
865 }
866 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
867 msg = "invalid IPv4 address";
868 goto error;
869 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100870 argp[idx].type = ARGT_IPV4;
871 break;
872
873 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200874 if (argp[idx].type == ARGT_SINT)
875 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
876 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200877 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
878 msg = "invalid IPv4 mask";
879 goto error;
880 }
881 }
882 else {
883 msg = "integer or string expected";
884 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200885 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100886 argp[idx].type = ARGT_MSK4;
887 break;
888
889 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200890 if (argp[idx].type != ARGT_STR) {
891 msg = "string expected";
892 goto error;
893 }
894 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
895 msg = "invalid IPv6 address";
896 goto error;
897 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100898 argp[idx].type = ARGT_IPV6;
899 break;
900
901 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200902 if (argp[idx].type == ARGT_SINT)
903 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
904 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200905 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
906 msg = "invalid IPv6 mask";
907 goto error;
908 }
909 }
910 else {
911 msg = "integer or string expected";
912 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200913 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100914 argp[idx].type = ARGT_MSK6;
915 break;
916
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200917 case ARGT_REG:
918 if (argp[idx].type != ARGT_STR) {
919 msg = "string expected";
920 goto error;
921 }
922 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
923 if (!reg) {
924 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
925 argp[idx].data.str.area, err);
926 free(err);
927 goto error;
928 }
929 argp[idx].type = ARGT_REG;
930 argp[idx].data.reg = reg;
931 break;
932
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100933 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200934 if (argp[idx].type != ARGT_STR) {
935 msg = "string expected";
936 goto error;
937 }
938 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100939 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200940 ul = p->uri_auth->userlist;
941 else
942 ul = auth_find_userlist(argp[idx].data.str.area);
943
944 if (!ul) {
945 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
946 goto error;
947 }
948 argp[idx].type = ARGT_USR;
949 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200950 break;
951
952 case ARGT_STR:
953 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
954 msg = "unable to duplicate string arg";
955 goto error;
956 }
957 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100958 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200959
Christopher Fauletd25d9262020-08-06 11:04:46 +0200960 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200961 msg = "type not yet supported";
962 goto error;
963 break;
964
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100965 }
966
967 /* Check for type of argument. */
968 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200969 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
970 arg_type_names[(mask & ARGT_MASK)],
971 arg_type_names[argp[idx].type & ARGT_MASK]);
972 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100973 }
974
975 /* Next argument. */
976 mask >>= ARGT_BITS;
977 idx++;
978 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200979 return 0;
980
981 error:
982 for (i = 0; i < idx; i++) {
983 if (argp[i].type == ARGT_STR)
984 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200985 else if (argp[i].type == ARGT_REG)
986 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200987 }
988 WILL_LJMP(luaL_argerror(L, first + idx, msg));
989 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100990}
991
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100992/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500993 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100994 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100995 *
996 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100997 * - hlua_sethlua : create the association between hlua context and lua_state.
998 */
999static inline struct hlua *hlua_gethlua(lua_State *L)
1000{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001001 struct hlua **hlua = lua_getextraspace(L);
1002 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001003}
1004static inline void hlua_sethlua(struct hlua *hlua)
1005{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001006 struct hlua **hlua_store = lua_getextraspace(hlua->T);
1007 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001008}
1009
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001010/* This function is used to send logs. It try to send on screen (stderr)
1011 * and on the default syslog server.
1012 */
1013static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
1014{
1015 struct tm tm;
1016 char *p;
1017
1018 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001019 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001020 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001021 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001022 /* Break the message if exceed the buffer size. */
1023 *(p-4) = ' ';
1024 *(p-3) = '.';
1025 *(p-2) = '.';
1026 *(p-1) = '.';
1027 break;
1028 }
Willy Tarreau90807112020-02-25 08:16:33 +01001029 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001030 *p = *msg;
1031 else
1032 *p = '.';
1033 }
1034 *p = '\0';
1035
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001036 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001037 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001038 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1039 return;
1040
Willy Tarreaua678b432015-08-28 10:14:59 +02001041 get_localtime(date.tv_sec, &tm);
1042 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001043 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001044 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001045 fflush(stderr);
1046 }
1047}
1048
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001049/* This function just ensure that the yield will be always
1050 * returned with a timeout and permit to set some flags
1051 */
1052__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001053 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001054{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001055 struct hlua *hlua;
1056
1057 /* Get hlua struct, or NULL if we execute from main lua state */
1058 hlua = hlua_gethlua(L);
1059 if (!hlua) {
1060 return;
1061 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001062
1063 /* Set the wake timeout. If timeout is required, we set
1064 * the expiration time.
1065 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001066 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001067
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001068 hlua->flags |= flags;
1069
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001070 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001071 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001072}
1073
Willy Tarreau87b09662015-04-03 00:22:06 +02001074/* This function initialises the Lua environment stored in the stream.
1075 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001076 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001077 *
1078 * This function is particular. it initialises a new Lua thread. If the
1079 * initialisation fails (example: out of memory error), the lua function
1080 * throws an error (longjmp).
1081 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001082 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001083 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001084 * threads appear, the safe environment set a lock to ensure only one
1085 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001086 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001087 *
1088 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001089 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001090 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001091 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001092 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001093 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001094 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001095 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001096int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001097{
1098 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001099 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001100 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001101 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001102 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001103 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001104 if (!already_safe) {
1105 if (!SET_SAFE_LJMP_PARENT(lua)) {
1106 lua->Tref = LUA_REFNIL;
1107 return 0;
1108 }
1109 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001110 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001111 if (!lua->T) {
1112 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001113 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001114 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001115 return 0;
1116 }
1117 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001118 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001119 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001120 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001121 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001122 return 1;
1123}
1124
Willy Tarreau87b09662015-04-03 00:22:06 +02001125/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001126 * is destroyed. The destroy also the memory context. The struct "lua"
1127 * is not freed.
1128 */
1129void hlua_ctx_destroy(struct hlua *lua)
1130{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001131 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001132 return;
1133
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001134 if (!lua->T)
1135 goto end;
1136
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001137 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001138 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001139
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001140 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001141 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001143 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001144
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001145 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001146 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001147 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001148 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001149 /* Forces a garbage collecting process. If the Lua program is finished
1150 * without error, we run the GC on the thread pointer. Its freed all
1151 * the unused memory.
1152 * If the thread is finnish with an error or is currently yielded,
1153 * it seems that the GC applied on the thread doesn't clean anything,
1154 * so e run the GC on the main thread.
1155 * NOTE: maybe this action locks all the Lua threads untiml the en of
1156 * the garbage collection.
1157 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001158 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001159 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001160 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001161 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001162 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001163 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001164
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001165 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001166
1167end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001168 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001169}
1170
1171/* This function is used to restore the Lua context when a coroutine
1172 * fails. This function copy the common memory between old coroutine
1173 * and the new coroutine. The old coroutine is destroyed, and its
1174 * replaced by the new coroutine.
1175 * If the flag "keep_msg" is set, the last entry of the old is assumed
1176 * as string error message and it is copied in the new stack.
1177 */
1178static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1179{
1180 lua_State *T;
1181 int new_ref;
1182
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001183 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001184 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001185 if (!T)
1186 return 0;
1187
1188 /* Copy last error message. */
1189 if (keep_msg)
1190 lua_xmove(lua->T, T, 1);
1191
1192 /* Copy data between the coroutines. */
1193 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1194 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001195 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196
1197 /* Destroy old data. */
1198 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1199
1200 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001201 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001202
1203 /* Fill the struct with the new coroutine values. */
1204 lua->Mref = new_ref;
1205 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001206 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207
1208 /* Set context. */
1209 hlua_sethlua(lua);
1210
1211 return 1;
1212}
1213
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001214void hlua_hook(lua_State *L, lua_Debug *ar)
1215{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001216 struct hlua *hlua;
1217
1218 /* Get hlua struct, or NULL if we execute from main lua state */
1219 hlua = hlua_gethlua(L);
1220 if (!hlua)
1221 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001222
1223 /* Lua cannot yield when its returning from a function,
1224 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001225 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001226 */
1227 if (lua_gethookmask(L) & LUA_MASKRET) {
1228 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1229 return;
1230 }
1231
1232 /* restore the interrupt condition. */
1233 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1234
1235 /* If we interrupt the Lua processing in yieldable state, we yield.
1236 * If the state is not yieldable, trying yield causes an error.
1237 */
1238 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001239 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001240
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001241 /* If we cannot yield, update the clock and check the timeout. */
1242 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001243 hlua->run_time += now_ms - hlua->start_time;
1244 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001245 lua_pushfstring(L, "execution timeout");
1246 WILL_LJMP(lua_error(L));
1247 }
1248
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001249 /* Update the start time. */
1250 hlua->start_time = now_ms;
1251
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001252 /* Try to interrupt the process at the end of the current
1253 * unyieldable function.
1254 */
1255 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001256}
1257
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001258/* This function start or resumes the Lua stack execution. If the flag
1259 * "yield_allowed" if no set and the LUA stack execution returns a yield
1260 * The function return an error.
1261 *
1262 * The function can returns 4 values:
1263 * - HLUA_E_OK : The execution is terminated without any errors.
1264 * - HLUA_E_AGAIN : The execution must continue at the next associated
1265 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001266 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001267 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001268 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001269 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001270 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001271 * LUA code.
1272 */
1273static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1274{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001275#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1276 int nres;
1277#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001278 int ret;
1279 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001280 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001281
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001282 /* Initialise run time counter. */
1283 if (!HLUA_IS_RUNNING(lua))
1284 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001285
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001286 /* Lock the whole Lua execution. This lock must be before the
1287 * label "resume_execution".
1288 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001289 if (lua->state_id == 0)
Willy Tarreau3eb2e472021-08-20 15:47:25 +02001290 lua_take_global_lock();
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001291
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001292resume_execution:
1293
1294 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1295 * instructions. it is used for preventing infinite loops.
1296 */
1297 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1298
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001299 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001300 HLUA_SET_RUN(lua);
1301 HLUA_CLR_CTRLYIELD(lua);
1302 HLUA_CLR_WAKERESWR(lua);
1303 HLUA_CLR_WAKEREQWR(lua);
Christopher Fauleta6e792f2021-08-04 17:58:21 +02001304 HLUA_CLR_NOYIELD(lua);
1305 if (!yield_allowed)
1306 HLUA_SET_NOYIELD(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001307
Christopher Fauletbc275a92020-02-26 14:55:16 +01001308 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001309 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001310 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001311
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001312 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001313#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001314 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001315#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001316 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001317#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001318 switch (ret) {
1319
1320 case LUA_OK:
1321 ret = HLUA_E_OK;
1322 break;
1323
1324 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001325 /* Check if the execution timeout is expired. It it is the case, we
1326 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001327 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001328 tv_update_date(0, 1);
1329 lua->run_time += now_ms - lua->start_time;
1330 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001331 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001332 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001333 break;
1334 }
1335 /* Process the forced yield. if the general yield is not allowed or
1336 * if no task were associated this the current Lua execution
1337 * coroutine, we resume the execution. Else we want to return in the
1338 * scheduler and we want to be waked up again, to continue the
1339 * current Lua execution. So we schedule our own task.
1340 */
1341 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001342 if (!yield_allowed || !lua->task)
1343 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001344 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001345 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001346 if (!yield_allowed) {
1347 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001348 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001349 break;
1350 }
1351 ret = HLUA_E_AGAIN;
1352 break;
1353
1354 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001355
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001356 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001357 * because the errors ares the only one mean to return immediately
1358 * from and lua execution.
1359 */
1360 if (lua->flags & HLUA_EXIT) {
1361 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001362 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001363 break;
1364 }
1365
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001366 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001367 if (!lua_checkstack(lua->T, 1)) {
1368 ret = HLUA_E_ERR;
1369 break;
1370 }
1371 msg = lua_tostring(lua->T, -1);
1372 lua_settop(lua->T, 0); /* Empty the stack. */
1373 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001374 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001375 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001376 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001377 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001378 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001379 ret = HLUA_E_ERRMSG;
1380 break;
1381
1382 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001383 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001384 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001385 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001386 break;
1387
1388 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001389 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001390 if (!lua_checkstack(lua->T, 1)) {
1391 ret = HLUA_E_ERR;
1392 break;
1393 }
1394 msg = lua_tostring(lua->T, -1);
1395 lua_settop(lua->T, 0); /* Empty the stack. */
1396 lua_pop(lua->T, 1);
1397 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001398 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001399 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001400 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001401 ret = HLUA_E_ERRMSG;
1402 break;
1403
1404 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001405 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001406 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001407 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001408 break;
1409 }
1410
1411 switch (ret) {
1412 case HLUA_E_AGAIN:
1413 break;
1414
1415 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001416 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001417 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001418 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001419 break;
1420
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001421 case HLUA_E_ETMOUT:
1422 case HLUA_E_NOMEM:
1423 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001424 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001425 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001426 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001427 hlua_ctx_renew(lua, 0);
1428 break;
1429
1430 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001431 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001432 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001433 break;
1434 }
1435
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001436 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001437 if (lua->state_id == 0)
Willy Tarreau3eb2e472021-08-20 15:47:25 +02001438 lua_drop_global_lock();
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001439
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001440 return ret;
1441}
1442
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001443/* This function exit the current code. */
1444__LJMP static int hlua_done(lua_State *L)
1445{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001446 struct hlua *hlua;
1447
1448 /* Get hlua struct, or NULL if we execute from main lua state */
1449 hlua = hlua_gethlua(L);
1450 if (!hlua)
1451 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001452
1453 hlua->flags |= HLUA_EXIT;
1454 WILL_LJMP(lua_error(L));
1455
1456 return 0;
1457}
1458
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001459/* This function is an LUA binding. It provides a function
1460 * for deleting ACL from a referenced ACL file.
1461 */
1462__LJMP static int hlua_del_acl(lua_State *L)
1463{
1464 const char *name;
1465 const char *key;
1466 struct pat_ref *ref;
1467
1468 MAY_LJMP(check_args(L, 2, "del_acl"));
1469
1470 name = MAY_LJMP(luaL_checkstring(L, 1));
1471 key = MAY_LJMP(luaL_checkstring(L, 2));
1472
1473 ref = pat_ref_lookup(name);
1474 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001475 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001476
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001477 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001478 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001479 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001480 return 0;
1481}
1482
1483/* This function is an LUA binding. It provides a function
1484 * for deleting map entry from a referenced map file.
1485 */
1486static int hlua_del_map(lua_State *L)
1487{
1488 const char *name;
1489 const char *key;
1490 struct pat_ref *ref;
1491
1492 MAY_LJMP(check_args(L, 2, "del_map"));
1493
1494 name = MAY_LJMP(luaL_checkstring(L, 1));
1495 key = MAY_LJMP(luaL_checkstring(L, 2));
1496
1497 ref = pat_ref_lookup(name);
1498 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001499 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001500
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001501 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001502 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001503 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001504 return 0;
1505}
1506
1507/* This function is an LUA binding. It provides a function
1508 * for adding ACL pattern from a referenced ACL file.
1509 */
1510static int hlua_add_acl(lua_State *L)
1511{
1512 const char *name;
1513 const char *key;
1514 struct pat_ref *ref;
1515
1516 MAY_LJMP(check_args(L, 2, "add_acl"));
1517
1518 name = MAY_LJMP(luaL_checkstring(L, 1));
1519 key = MAY_LJMP(luaL_checkstring(L, 2));
1520
1521 ref = pat_ref_lookup(name);
1522 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001523 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001524
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001525 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001526 if (pat_ref_find_elt(ref, key) == NULL)
1527 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001528 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001529 return 0;
1530}
1531
1532/* This function is an LUA binding. It provides a function
1533 * for setting map pattern and sample from a referenced map
1534 * file.
1535 */
1536static int hlua_set_map(lua_State *L)
1537{
1538 const char *name;
1539 const char *key;
1540 const char *value;
1541 struct pat_ref *ref;
1542
1543 MAY_LJMP(check_args(L, 3, "set_map"));
1544
1545 name = MAY_LJMP(luaL_checkstring(L, 1));
1546 key = MAY_LJMP(luaL_checkstring(L, 2));
1547 value = MAY_LJMP(luaL_checkstring(L, 3));
1548
1549 ref = pat_ref_lookup(name);
1550 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001551 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001552
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001553 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001554 if (pat_ref_find_elt(ref, key) != NULL)
1555 pat_ref_set(ref, key, value, NULL);
1556 else
1557 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001558 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001559 return 0;
1560}
1561
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001562/* A class is a lot of memory that contain data. This data can be a table,
1563 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001564 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001565 * the name of the object (_G[<name>] = <metable> ).
1566 *
1567 * A metable is a table that modify the standard behavior of a standard
1568 * access to the associated data. The entries of this new metatable are
1569 * defined as is:
1570 *
1571 * http://lua-users.org/wiki/MetatableEvents
1572 *
1573 * __index
1574 *
1575 * we access an absent field in a table, the result is nil. This is
1576 * true, but it is not the whole truth. Actually, such access triggers
1577 * the interpreter to look for an __index metamethod: If there is no
1578 * such method, as usually happens, then the access results in nil;
1579 * otherwise, the metamethod will provide the result.
1580 *
1581 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1582 * the key does not appear in the table, but the metatable has an __index
1583 * property:
1584 *
1585 * - if the value is a function, the function is called, passing in the
1586 * table and the key; the return value of that function is returned as
1587 * the result.
1588 *
1589 * - if the value is another table, the value of the key in that table is
1590 * asked for and returned (and if it doesn't exist in that table, but that
1591 * table's metatable has an __index property, then it continues on up)
1592 *
1593 * - Use "rawget(myTable,key)" to skip this metamethod.
1594 *
1595 * http://www.lua.org/pil/13.4.1.html
1596 *
1597 * __newindex
1598 *
1599 * Like __index, but control property assignment.
1600 *
1601 * __mode - Control weak references. A string value with one or both
1602 * of the characters 'k' and 'v' which specifies that the the
1603 * keys and/or values in the table are weak references.
1604 *
1605 * __call - Treat a table like a function. When a table is followed by
1606 * parenthesis such as "myTable( 'foo' )" and the metatable has
1607 * a __call key pointing to a function, that function is invoked
1608 * (passing any specified arguments) and the return value is
1609 * returned.
1610 *
1611 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1612 * called, if the metatable for myTable has a __metatable
1613 * key, the value of that key is returned instead of the
1614 * actual metatable.
1615 *
1616 * __tostring - Control string representation. When the builtin
1617 * "tostring( myTable )" function is called, if the metatable
1618 * for myTable has a __tostring property set to a function,
1619 * that function is invoked (passing myTable to it) and the
1620 * return value is used as the string representation.
1621 *
1622 * __len - Control table length. When the table length is requested using
1623 * the length operator ( '#' ), if the metatable for myTable has
1624 * a __len key pointing to a function, that function is invoked
1625 * (passing myTable to it) and the return value used as the value
1626 * of "#myTable".
1627 *
1628 * __gc - Userdata finalizer code. When userdata is set to be garbage
1629 * collected, if the metatable has a __gc field pointing to a
1630 * function, that function is first invoked, passing the userdata
1631 * to it. The __gc metamethod is not called for tables.
1632 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1633 *
1634 * Special metamethods for redefining standard operators:
1635 * http://www.lua.org/pil/13.1.html
1636 *
1637 * __add "+"
1638 * __sub "-"
1639 * __mul "*"
1640 * __div "/"
1641 * __unm "!"
1642 * __pow "^"
1643 * __concat ".."
1644 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001645 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001646 * http://www.lua.org/pil/13.2.html
1647 *
1648 * __eq "=="
1649 * __lt "<"
1650 * __le "<="
1651 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652
1653/*
1654 *
1655 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001656 * Class Map
1657 *
1658 *
1659 */
1660
1661/* Returns a struct hlua_map if the stack entry "ud" is
1662 * a class session, otherwise it throws an error.
1663 */
1664__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1665{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001666 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001667}
1668
1669/* This function is the map constructor. It don't need
1670 * the class Map object. It creates and return a new Map
1671 * object. It must be called only during "body" or "init"
1672 * context because it process some filesystem accesses.
1673 */
1674__LJMP static int hlua_map_new(struct lua_State *L)
1675{
1676 const char *fn;
1677 int match = PAT_MATCH_STR;
1678 struct sample_conv conv;
1679 const char *file = "";
1680 int line = 0;
1681 lua_Debug ar;
1682 char *err = NULL;
1683 struct arg args[2];
1684
1685 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1686 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1687
1688 fn = MAY_LJMP(luaL_checkstring(L, 1));
1689
1690 if (lua_gettop(L) >= 2) {
1691 match = MAY_LJMP(luaL_checkinteger(L, 2));
1692 if (match < 0 || match >= PAT_MATCH_NUM)
1693 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1694 }
1695
1696 /* Get Lua filename and line number. */
1697 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1698 lua_getinfo(L, "Sl", &ar); /* get info about it */
1699 if (ar.currentline > 0) { /* is there info? */
1700 file = ar.short_src;
1701 line = ar.currentline;
1702 }
1703 }
1704
1705 /* fill fake sample_conv struct. */
1706 conv.kw = ""; /* unused. */
1707 conv.process = NULL; /* unused. */
1708 conv.arg_mask = 0; /* unused. */
1709 conv.val_args = NULL; /* unused. */
1710 conv.out_type = SMP_T_STR;
1711 conv.private = (void *)(long)match;
1712 switch (match) {
1713 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1714 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1715 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1716 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1717 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1718 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1719 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001720 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001721 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1722 default:
1723 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1724 }
1725
1726 /* fill fake args. */
1727 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001728 args[0].data.str.area = strdup(fn);
1729 args[0].data.str.data = strlen(fn);
1730 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001731 args[1].type = ARGT_STOP;
1732
1733 /* load the map. */
1734 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001735 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001736 * free the err variable.
1737 */
1738 luaL_where(L, 1);
1739 lua_pushfstring(L, "'new': %s.", err);
1740 lua_concat(L, 2);
1741 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001742 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001743 WILL_LJMP(lua_error(L));
1744 }
1745
1746 /* create the lua object. */
1747 lua_newtable(L);
1748 lua_pushlightuserdata(L, args[0].data.map);
1749 lua_rawseti(L, -2, 0);
1750
1751 /* Pop a class Map metatable and affect it to the userdata. */
1752 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1753 lua_setmetatable(L, -2);
1754
1755
1756 return 1;
1757}
1758
1759__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1760{
1761 struct map_descriptor *desc;
1762 struct pattern *pat;
1763 struct sample smp;
1764
1765 MAY_LJMP(check_args(L, 2, "lookup"));
1766 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001767 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001768 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001769 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001770 }
1771 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001772 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001773 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001774 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 +01001775 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001776 }
1777
1778 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001779 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001780 if (str)
1781 lua_pushstring(L, "");
1782 else
1783 lua_pushnil(L);
1784 return 1;
1785 }
1786
1787 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001788 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001789 return 1;
1790}
1791
1792__LJMP static int hlua_map_lookup(struct lua_State *L)
1793{
1794 return _hlua_map_lookup(L, 0);
1795}
1796
1797__LJMP static int hlua_map_slookup(struct lua_State *L)
1798{
1799 return _hlua_map_lookup(L, 1);
1800}
1801
1802/*
1803 *
1804 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001805 * Class Socket
1806 *
1807 *
1808 */
1809
1810__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1811{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001812 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813}
1814
1815/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001816 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001817 * received.
1818 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001819static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001821 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001823 if (appctx->ctx.hlua_cosocket.die) {
1824 si_shutw(si);
1825 si_shutr(si);
1826 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001827 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1828 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001829 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1830 }
1831
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001832 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001833 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001834 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001835
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001836 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001837 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001838 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001839
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001840 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001841 * to be notified whenever the connection completes.
1842 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001843 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001844 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001845 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001846 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001847 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001848 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001849
1850 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001851 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001852
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001853 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001854 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001855 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001856
1857 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001858 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001859 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001860
1861 /* Some data were injected in the buffer, notify the stream
1862 * interface.
1863 */
1864 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001865 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001866
1867 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001868 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001869 */
1870 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001871 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872}
1873
Willy Tarreau87b09662015-04-03 00:22:06 +02001874/* This function is called when the "struct stream" is destroyed.
1875 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001876 * Wake all the pending signals.
1877 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001878static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001879{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001880 struct xref *peer;
1881
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001882 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001883 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1884 if (peer)
1885 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001886
1887 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001888 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1889 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890}
1891
1892/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001893 * uses this object. If the stream does not exists, just quit.
1894 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895 * pending signal can rest in the read and write lists. destroy
1896 * it.
1897 */
1898__LJMP static int hlua_socket_gc(lua_State *L)
1899{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001900 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001902 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001904 MAY_LJMP(check_args(L, 1, "__gc"));
1905
1906 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001907 peer = xref_get_peer_and_lock(&socket->xref);
1908 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001910 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001912 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001913 appctx->ctx.hlua_cosocket.die = 1;
1914 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001915
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001916 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001917 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001918 return 0;
1919}
1920
1921/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001922 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923 */
sada05ed3302018-05-11 11:48:18 -07001924__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001925{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001926 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001928 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001929 struct hlua *hlua;
1930
1931 /* Get hlua struct, or NULL if we execute from main lua state */
1932 hlua = hlua_gethlua(L);
1933 if (!hlua)
1934 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001935
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001936 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001937
1938 /* Check if we run on the same thread than the xreator thread.
1939 * We cannot access to the socket if the thread is different.
1940 */
1941 if (socket->tid != tid)
1942 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1943
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001944 peer = xref_get_peer_and_lock(&socket->xref);
1945 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001947
1948 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001949 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001950
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001951 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001952 appctx->ctx.hlua_cosocket.die = 1;
1953 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001954
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001955 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001956 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957 return 0;
1958}
1959
sada05ed3302018-05-11 11:48:18 -07001960/* The close function calls close_helper.
1961 */
1962__LJMP static int hlua_socket_close(lua_State *L)
1963{
1964 MAY_LJMP(check_args(L, 1, "close"));
1965 return hlua_socket_close_helper(L);
1966}
1967
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001968/* This Lua function assumes that the stack contain three parameters.
1969 * 1 - USERDATA containing a struct socket
1970 * 2 - INTEGER with values of the macro defined below
1971 * If the integer is -1, we must read at most one line.
1972 * If the integer is -2, we ust read all the data until the
1973 * end of the stream.
1974 * If the integer is positive value, we must read a number of
1975 * bytes corresponding to this value.
1976 */
1977#define HLSR_READ_LINE (-1)
1978#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001979__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980{
1981 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1982 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001983 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001984 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001985 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001987 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001988 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001989 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001990 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001991 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001992 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001993 struct stream_interface *si;
1994 struct stream *s;
1995 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001996 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001998 /* Get hlua struct, or NULL if we execute from main lua state */
1999 hlua = hlua_gethlua(L);
2000
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002001 /* Check if this lua stack is schedulable. */
2002 if (!hlua || !hlua->task)
2003 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
2004 "'frontend', 'backend' or 'task'"));
2005
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002006 /* Check if we run on the same thread than the xreator thread.
2007 * We cannot access to the socket if the thread is different.
2008 */
2009 if (socket->tid != tid)
2010 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2011
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002012 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002013 peer = xref_get_peer_and_lock(&socket->xref);
2014 if (!peer)
2015 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002016 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2017 si = appctx->owner;
2018 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002019
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002020 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002021 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002022 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002023 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002024 if (nblk < 0) /* Connection close. */
2025 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002026 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002028
2029 /* remove final \r\n. */
2030 if (nblk == 1) {
2031 if (blk1[len1-1] == '\n') {
2032 len1--;
2033 skip_at_end++;
2034 if (blk1[len1-1] == '\r') {
2035 len1--;
2036 skip_at_end++;
2037 }
2038 }
2039 }
2040 else {
2041 if (blk2[len2-1] == '\n') {
2042 len2--;
2043 skip_at_end++;
2044 if (blk2[len2-1] == '\r') {
2045 len2--;
2046 skip_at_end++;
2047 }
2048 }
2049 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002050 }
2051
2052 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002053 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002054 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002055 if (nblk < 0) /* Connection close. */
2056 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002057 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002058 goto connection_empty;
2059 }
2060
2061 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002062 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002063 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002064 if (nblk < 0) /* Connection close. */
2065 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002066 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 goto connection_empty;
2068
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002069 missing_bytes = wanted - socket->b.n;
2070 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002072 len1 = missing_bytes;
2073 } if (nblk == 2 && len1 + len2 > missing_bytes)
2074 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075 }
2076
2077 len = len1;
2078
2079 luaL_addlstring(&socket->b, blk1, len1);
2080 if (nblk == 2) {
2081 len += len2;
2082 luaL_addlstring(&socket->b, blk2, len2);
2083 }
2084
2085 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002086 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087
2088 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002089 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090
2091 /* If the pattern reclaim to read all the data
2092 * in the connection, got out.
2093 */
2094 if (wanted == HLSR_READ_ALL)
2095 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002096 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002097 goto connection_empty;
2098
2099 /* Return result. */
2100 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002101 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 return 1;
2103
2104connection_closed:
2105
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002106 xref_unlock(&socket->xref, peer);
2107
2108no_peer:
2109
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002110 /* If the buffer containds data. */
2111 if (socket->b.n > 0) {
2112 luaL_pushresult(&socket->b);
2113 return 1;
2114 }
2115 lua_pushnil(L);
2116 lua_pushstring(L, "connection closed.");
2117 return 2;
2118
2119connection_empty:
2120
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002121 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2122 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002123 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002124 }
2125 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002126 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127 return 0;
2128}
2129
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002130/* This Lua function gets two parameters. The first one can be string
2131 * or a number. If the string is "*l", the user requires one line. If
2132 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002133 * If the value is a number, the user require a number of bytes equal
2134 * to the value. The default value is "*l" (a line).
2135 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002136 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137 * integer takes this values:
2138 * -1 : read a line
2139 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002140 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002141 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002142 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002143 * concatenated with the read data.
2144 */
2145__LJMP static int hlua_socket_receive(struct lua_State *L)
2146{
2147 int wanted = HLSR_READ_LINE;
2148 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002149 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002150 char *error;
2151 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002152 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002153
2154 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2155 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2156
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002157 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002158
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002159 /* Check if we run on the same thread than the xreator thread.
2160 * We cannot access to the socket if the thread is different.
2161 */
2162 if (socket->tid != tid)
2163 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2164
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002165 /* check for pattern. */
2166 if (lua_gettop(L) >= 2) {
2167 type = lua_type(L, 2);
2168 if (type == LUA_TSTRING) {
2169 pattern = lua_tostring(L, 2);
2170 if (strcmp(pattern, "*a") == 0)
2171 wanted = HLSR_READ_ALL;
2172 else if (strcmp(pattern, "*l") == 0)
2173 wanted = HLSR_READ_LINE;
2174 else {
2175 wanted = strtoll(pattern, &error, 10);
2176 if (*error != '\0')
2177 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2178 }
2179 }
2180 else if (type == LUA_TNUMBER) {
2181 wanted = lua_tointeger(L, 2);
2182 if (wanted < 0)
2183 WILL_LJMP(luaL_error(L, "Unsupported size."));
2184 }
2185 }
2186
2187 /* Set pattern. */
2188 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002189
2190 /* Check if we would replace the top by itself. */
2191 if (lua_gettop(L) != 2)
2192 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002193
Christopher Fauletc31b2002021-05-03 10:11:13 +02002194 /* Save index of the top of the stack because since buffers are used, it
2195 * may change
2196 */
2197 lastarg = lua_gettop(L);
2198
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002199 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002200 luaL_buffinit(L, &socket->b);
2201
2202 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002203 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204 if (lua_type(L, 3) != LUA_TSTRING)
2205 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2206 pattern = lua_tolstring(L, 3, &len);
2207 luaL_addlstring(&socket->b, pattern, len);
2208 }
2209
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002210 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002211}
2212
2213/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002214 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002215 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002216static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002217{
2218 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002219 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002220 struct appctx *appctx;
2221 size_t buf_len;
2222 const char *buf;
2223 int len;
2224 int send_len;
2225 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002226 struct xref *peer;
2227 struct stream_interface *si;
2228 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002229
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002230 /* Get hlua struct, or NULL if we execute from main lua state */
2231 hlua = hlua_gethlua(L);
2232
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233 /* Check if this lua stack is schedulable. */
2234 if (!hlua || !hlua->task)
2235 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2236 "'frontend', 'backend' or 'task'"));
2237
2238 /* Get object */
2239 socket = MAY_LJMP(hlua_checksocket(L, 1));
2240 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002241 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002242
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002243 /* Check if we run on the same thread than the xreator thread.
2244 * We cannot access to the socket if the thread is different.
2245 */
2246 if (socket->tid != tid)
2247 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2248
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002249 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002250 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002251 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002252 lua_pushinteger(L, -1);
2253 return 1;
2254 }
2255 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2256 si = appctx->owner;
2257 s = si_strm(si);
2258
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002260 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002261 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 lua_pushinteger(L, -1);
2263 return 1;
2264 }
2265
2266 /* Update the input buffer data. */
2267 buf += sent;
2268 send_len = buf_len - sent;
2269
2270 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002271 if (sent >= buf_len) {
2272 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002273 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002274 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002275
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002276 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002277 * the request buffer if its not required.
2278 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002279 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002280 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002281 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002282 }
2283
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002284 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002285 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002286 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002288 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289
2290 /* send data */
2291 if (len < send_len)
2292 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002293 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294
2295 /* "Not enough space" (-1), "Buffer too little to contain
2296 * the data" (-2) are not expected because the available length
2297 * is tested.
2298 * Other unknown error are also not expected.
2299 */
2300 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002301 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002302 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002303
sada05ed3302018-05-11 11:48:18 -07002304 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002306 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002307 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 return 1;
2309 }
2310
2311 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002312 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002313
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002314 s->req.rex = TICK_ETERNITY;
2315 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316
2317 /* Update length sent. */
2318 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002319 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002320
2321 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002322 if (sent + len >= buf_len) {
2323 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002324 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002325 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326
2327hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002328 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2329 xref_unlock(&socket->xref, peer);
2330 WILL_LJMP(luaL_error(L, "out of memory"));
2331 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002332 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002333 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334 return 0;
2335}
2336
2337/* This function initiate the send of data. It just check the input
2338 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002339 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340 * "hlua_socket_write_yield" that can yield.
2341 *
2342 * The Lua function gets between 3 and 4 parameters. The first one is
2343 * the associated object. The second is a string buffer. The third is
2344 * a facultative integer that represents where is the buffer position
2345 * of the start of the data that can send. The first byte is the
2346 * position "1". The default value is "1". The fourth argument is a
2347 * facultative integer that represents where is the buffer position
2348 * of the end of the data that can send. The default is the last byte.
2349 */
2350static int hlua_socket_send(struct lua_State *L)
2351{
2352 int i;
2353 int j;
2354 const char *buf;
2355 size_t buf_len;
2356
2357 /* Check number of arguments. */
2358 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2359 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2360
2361 /* Get the string. */
2362 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2363
2364 /* Get and check j. */
2365 if (lua_gettop(L) == 4) {
2366 j = MAY_LJMP(luaL_checkinteger(L, 4));
2367 if (j < 0)
2368 j = buf_len + j + 1;
2369 if (j > buf_len)
2370 j = buf_len + 1;
2371 lua_pop(L, 1);
2372 }
2373 else
2374 j = buf_len;
2375
2376 /* Get and check i. */
2377 if (lua_gettop(L) == 3) {
2378 i = MAY_LJMP(luaL_checkinteger(L, 3));
2379 if (i < 0)
2380 i = buf_len + i + 1;
2381 if (i > buf_len)
2382 i = buf_len + 1;
2383 lua_pop(L, 1);
2384 } else
2385 i = 1;
2386
2387 /* Check bth i and j. */
2388 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002389 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390 return 1;
2391 }
2392 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002393 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002394 return 1;
2395 }
2396 if (i == 0)
2397 i = 1;
2398 if (j == 0)
2399 j = 1;
2400
2401 /* Pop the string. */
2402 lua_pop(L, 1);
2403
2404 /* Update the buffer length. */
2405 buf += i - 1;
2406 buf_len = j - i + 1;
2407 lua_pushlstring(L, buf, buf_len);
2408
2409 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002410 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002411
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002412 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002413}
2414
Willy Tarreau22b0a682015-06-17 19:43:49 +02002415#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2417{
2418 static char buffer[SOCKET_INFO_MAX_LEN];
2419 int ret;
2420 int len;
2421 char *p;
2422
2423 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2424 if (ret <= 0) {
2425 lua_pushnil(L);
2426 return 1;
2427 }
2428
2429 if (ret == AF_UNIX) {
2430 lua_pushstring(L, buffer+1);
2431 return 1;
2432 }
2433 else if (ret == AF_INET6) {
2434 buffer[0] = '[';
2435 len = strlen(buffer);
2436 buffer[len] = ']';
2437 len++;
2438 buffer[len] = ':';
2439 len++;
2440 p = buffer;
2441 }
2442 else if (ret == AF_INET) {
2443 p = buffer + 1;
2444 len = strlen(p);
2445 p[len] = ':';
2446 len++;
2447 }
2448 else {
2449 lua_pushnil(L);
2450 return 1;
2451 }
2452
2453 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2454 lua_pushnil(L);
2455 return 1;
2456 }
2457
2458 lua_pushstring(L, p);
2459 return 1;
2460}
2461
2462/* Returns information about the peer of the connection. */
2463__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2464{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002465 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002466 struct xref *peer;
2467 struct appctx *appctx;
2468 struct stream_interface *si;
2469 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002470 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002471
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472 MAY_LJMP(check_args(L, 1, "getpeername"));
2473
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002474 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002475
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002476 /* Check if we run on the same thread than the xreator thread.
2477 * We cannot access to the socket if the thread is different.
2478 */
2479 if (socket->tid != tid)
2480 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2481
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002482 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002483 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002484 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002485 lua_pushnil(L);
2486 return 1;
2487 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002488 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2489 si = appctx->owner;
2490 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002491
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002492 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002493 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002494 lua_pushnil(L);
2495 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002496 }
2497
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002498 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002499 xref_unlock(&socket->xref, peer);
2500 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002501}
2502
2503/* Returns information about my connection side. */
2504static int hlua_socket_getsockname(struct lua_State *L)
2505{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002506 struct hlua_socket *socket;
2507 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002508 struct appctx *appctx;
2509 struct xref *peer;
2510 struct stream_interface *si;
2511 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002512 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002513
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002514 MAY_LJMP(check_args(L, 1, "getsockname"));
2515
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002516 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002517
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002518 /* Check if we run on the same thread than the xreator thread.
2519 * We cannot access to the socket if the thread is different.
2520 */
2521 if (socket->tid != tid)
2522 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2523
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002524 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002525 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002526 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002527 lua_pushnil(L);
2528 return 1;
2529 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002530 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2531 si = appctx->owner;
2532 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002534 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002535 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002536 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002537 lua_pushnil(L);
2538 return 1;
2539 }
2540
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002541 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002542 xref_unlock(&socket->xref, peer);
2543 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002544}
2545
2546/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002547static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002548 .obj_type = OBJ_TYPE_APPLET,
2549 .name = "<LUA_TCP>",
2550 .fct = hlua_socket_handler,
2551 .release = hlua_socket_release,
2552};
2553
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002554__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002555{
2556 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002557 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002558 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002559 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 struct stream_interface *si;
2561 struct stream *s;
2562
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002563 /* Get hlua struct, or NULL if we execute from main lua state */
2564 hlua = hlua_gethlua(L);
2565 if (!hlua)
2566 return 0;
2567
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002568 /* Check if we run on the same thread than the xreator thread.
2569 * We cannot access to the socket if the thread is different.
2570 */
2571 if (socket->tid != tid)
2572 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2573
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002575 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002576 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002577 lua_pushnil(L);
2578 lua_pushstring(L, "Can't connect");
2579 return 2;
2580 }
2581 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2582 si = appctx->owner;
2583 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002585 /* Check if we run on the same thread than the xreator thread.
2586 * We cannot access to the socket if the thread is different.
2587 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002588 if (socket->tid != tid) {
2589 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002590 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002591 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002592
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002594 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002595 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596 lua_pushnil(L);
2597 lua_pushstring(L, "Can't connect");
2598 return 2;
2599 }
2600
Willy Tarreaue09101e2018-10-16 17:37:12 +02002601 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002602
2603 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002604 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002605 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606 lua_pushinteger(L, 1);
2607 return 1;
2608 }
2609
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002610 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2611 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002612 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002613 }
2614 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002615 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616 return 0;
2617}
2618
2619/* This function fail or initite the connection. */
2620__LJMP static int hlua_socket_connect(struct lua_State *L)
2621{
2622 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002623 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002624 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002625 struct hlua *hlua;
2626 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002627 int low, high;
2628 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002629 struct xref *peer;
2630 struct stream_interface *si;
2631 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002632
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002633 if (lua_gettop(L) < 2)
2634 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635
2636 /* Get args. */
2637 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002638
2639 /* Check if we run on the same thread than the xreator thread.
2640 * We cannot access to the socket if the thread is different.
2641 */
2642 if (socket->tid != tid)
2643 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2644
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002646 if (lua_gettop(L) >= 3) {
2647 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002648 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002649
Tim Duesterhus6edab862018-01-06 19:04:45 +01002650 /* Force the ip to end with a colon, to support IPv6 addresses
2651 * that are not enclosed within square brackets.
2652 */
2653 if (port > 0) {
2654 luaL_buffinit(L, &b);
2655 luaL_addstring(&b, ip);
2656 luaL_addchar(&b, ':');
2657 luaL_pushresult(&b);
2658 ip = lua_tolstring(L, lua_gettop(L), NULL);
2659 }
2660 }
2661
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002662 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002663 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002664 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002665 lua_pushnil(L);
2666 return 1;
2667 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002668
2669 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002670 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 +02002671 if (!addr) {
2672 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002673 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002674 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002675
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002677 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002678 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002679 if (port == -1) {
2680 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002681 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002682 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002683 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2684 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002685 if (port == -1) {
2686 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002687 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002688 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002689 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002690 }
2691 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002692
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002693 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2694 si = appctx->owner;
2695 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002696
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002697 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002698 xref_unlock(&socket->xref, peer);
2699 WILL_LJMP(luaL_error(L, "connect: internal error"));
2700 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002701 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002702
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002703 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002704 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002705 if (!hlua)
2706 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002707
2708 /* inform the stream that we want to be notified whenever the
2709 * connection completes.
2710 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002711 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002712 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002713 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002714
Willy Tarreauf31af932020-01-14 09:59:38 +01002715 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002716
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002717 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2718 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002719 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002720 }
2721 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002722
2723 task_wakeup(s->task, TASK_WOKEN_INIT);
2724 /* Return yield waiting for connection. */
2725
Willy Tarreau9635e032018-10-16 17:52:55 +02002726 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002727
2728 return 0;
2729}
2730
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002731#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002732__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2733{
2734 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002735 struct xref *peer;
2736 struct appctx *appctx;
2737 struct stream_interface *si;
2738 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002739
2740 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2741 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002742
2743 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002744 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002745 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002746 lua_pushnil(L);
2747 return 1;
2748 }
2749 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2750 si = appctx->owner;
2751 s = si_strm(si);
2752
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002753 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002754 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002755 return MAY_LJMP(hlua_socket_connect(L));
2756}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002757#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002758
2759__LJMP static int hlua_socket_setoption(struct lua_State *L)
2760{
2761 return 0;
2762}
2763
2764__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2765{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002766 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002767 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002768 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002769 struct xref *peer;
2770 struct appctx *appctx;
2771 struct stream_interface *si;
2772 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002773
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002774 MAY_LJMP(check_args(L, 2, "settimeout"));
2775
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002776 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002777
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002778 /* convert the timeout to millis */
2779 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002780
Thierry Fournier17a921b2018-03-08 09:59:02 +01002781 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002782 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002783 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2784
Mark Lakes56cc1252018-03-27 09:48:06 +02002785 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002786 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002787
2788 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002789 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002790 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002791
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002792 /* Check if we run on the same thread than the xreator thread.
2793 * We cannot access to the socket if the thread is different.
2794 */
2795 if (socket->tid != tid)
2796 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2797
Mark Lakes56cc1252018-03-27 09:48:06 +02002798 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002799 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002800 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002801 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2802 WILL_LJMP(lua_error(L));
2803 return 0;
2804 }
2805 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2806 si = appctx->owner;
2807 s = si_strm(si);
2808
Cyril Bonté7bb63452018-08-17 23:51:02 +02002809 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002810 s->req.rto = tmout;
2811 s->req.wto = tmout;
2812 s->res.rto = tmout;
2813 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002814 s->req.rex = tick_add_ifset(now_ms, tmout);
2815 s->req.wex = tick_add_ifset(now_ms, tmout);
2816 s->res.rex = tick_add_ifset(now_ms, tmout);
2817 s->res.wex = tick_add_ifset(now_ms, tmout);
2818
2819 s->task->expire = tick_add_ifset(now_ms, tmout);
2820 task_queue(s->task);
2821
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002822 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002823
Thierry Fourniere9636f12018-03-08 09:54:32 +01002824 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002825 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002826}
2827
2828__LJMP static int hlua_socket_new(lua_State *L)
2829{
2830 struct hlua_socket *socket;
2831 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002832 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002833 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002834
2835 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002836 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002837 hlua_pusherror(L, "socket: full stack");
2838 goto out_fail_conf;
2839 }
2840
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002841 /* Create the object: obj[0] = userdata. */
2842 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002843 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002844 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002845 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002846 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002847
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002848 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002849 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002850 hlua_pusherror(L, "socket: uninitialized pools.");
2851 goto out_fail_conf;
2852 }
2853
Willy Tarreau87b09662015-04-03 00:22:06 +02002854 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002855 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2856 lua_setmetatable(L, -2);
2857
Willy Tarreaud420a972015-04-06 00:39:18 +02002858 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002859 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002860 if (!appctx) {
2861 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002862 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002863 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002864
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002865 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002866 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002867 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2868 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002869
Willy Tarreaud420a972015-04-06 00:39:18 +02002870 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002871 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002872 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002873 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002874 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002875 }
2876
Christopher Faulet26256f82020-09-14 11:40:13 +02002877 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002878 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002879 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002880 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002881 }
2882
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002883 /* Initialise cross reference between stream and Lua socket object. */
2884 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002885
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002886 /* Configure "right" stream interface. this "si" is used to connect
2887 * and retrieve data from the server. The connection is initialized
2888 * with the "struct server".
2889 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002890 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002891
2892 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002893 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002894 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002895
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002896 return 1;
2897
Willy Tarreaud420a972015-04-06 00:39:18 +02002898 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002899 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002900 out_fail_sess:
2901 appctx_free(appctx);
2902 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002903 WILL_LJMP(lua_error(L));
2904 return 0;
2905}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002906
2907/*
2908 *
2909 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 * Class Channel
2911 *
2912 *
2913 */
2914
2915/* Returns the struct hlua_channel join to the class channel in the
2916 * stack entry "ud" or throws an argument error.
2917 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002918__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002920 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002921}
2922
Willy Tarreau47860ed2015-03-10 14:07:50 +01002923/* Pushes the channel onto the top of the stack. If the stask does not have a
2924 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002926static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002928 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002929 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002930 return 0;
2931
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002932 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002933 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002934 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002935
2936 /* Pop a class sesison metatable and affect it to the userdata. */
2937 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2938 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002939 return 1;
2940}
2941
2942/* Duplicate all the data present in the input channel and put it
2943 * in a string LUA variables. Returns -1 and push a nil value in
2944 * the stack if the channel is closed and all the data are consumed,
2945 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002946 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002947 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002948static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949{
2950 char *blk1;
2951 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002952 size_t len1;
2953 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002954 int ret;
2955 luaL_Buffer b;
2956
Willy Tarreau06d80a92017-10-19 14:32:15 +02002957 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02002958 if (unlikely(ret <= 0)) {
2959 if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) {
2960 lua_pushnil(L);
2961 return -1;
2962 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002963 return 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 }
2965
2966 luaL_buffinit(L, &b);
2967 luaL_addlstring(&b, blk1, len1);
2968 if (unlikely(ret == 2))
2969 luaL_addlstring(&b, blk2, len2);
2970 luaL_pushresult(&b);
2971
2972 if (unlikely(ret == 2))
2973 return len1 + len2;
2974 return len1;
2975}
2976
2977/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2978 * a yield. This function keep the data in the buffer.
2979 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002980__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002981{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002982 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002983
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002984 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2985
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01002986 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02002987 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002988 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002989 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002990
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002991 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002992 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993 return 1;
2994}
2995
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002996/* Check arguments for the function "hlua_channel_dup_yield". */
2997__LJMP static int hlua_channel_dup(lua_State *L)
2998{
2999 MAY_LJMP(check_args(L, 1, "dup"));
3000 MAY_LJMP(hlua_checkchannel(L, 1));
3001 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
3002}
3003
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003004/* "_hlua_channel_dup" wrapper. If no data are available, it returns
3005 * a yield. This function consumes the data in the buffer. It returns
3006 * a string containing the data or a nil pointer if no data are available
3007 * and the channel is closed.
3008 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003009__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003011 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003012 int ret;
3013
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003014 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003015
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003016 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003017 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003018 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003019 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003020
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003021 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02003023 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003024
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003025 if (unlikely(ret == -1))
3026 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003028 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003029 return 1;
3030}
3031
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003032/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003033__LJMP static int hlua_channel_get(lua_State *L)
3034{
3035 MAY_LJMP(check_args(L, 1, "get"));
3036 MAY_LJMP(hlua_checkchannel(L, 1));
3037 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3038}
3039
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003040/* This functions consumes and returns one line. If the channel is closed,
3041 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003042 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003043 * value.
3044 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003045__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003046{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003047 char *blk1;
3048 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003049 size_t len1;
3050 size_t len2;
3051 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003052 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053 int ret;
3054 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003055
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003056 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3057
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003058 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003059 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003060 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003061 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003062
Willy Tarreau06d80a92017-10-19 14:32:15 +02003063 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Christopher Faulet055310f2021-08-05 11:58:37 +02003064 if (ret == 0) {
3065 if (HLUA_CANT_YIELD(hlua_gethlua(L))) {
3066 _hlua_channel_dup(chn, L);
3067 return 1;
3068 }
Willy Tarreau9635e032018-10-16 17:52:55 +02003069 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet055310f2021-08-05 11:58:37 +02003070 }
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003071
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072 if (ret == -1) {
3073 lua_pushnil(L);
3074 return 1;
3075 }
3076
3077 luaL_buffinit(L, &b);
3078 luaL_addlstring(&b, blk1, len1);
3079 len = len1;
3080 if (unlikely(ret == 2)) {
3081 luaL_addlstring(&b, blk2, len2);
3082 len += len2;
3083 }
3084 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003085 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003086 return 1;
3087}
3088
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003089/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003090__LJMP static int hlua_channel_getline(lua_State *L)
3091{
3092 MAY_LJMP(check_args(L, 1, "getline"));
3093 MAY_LJMP(hlua_checkchannel(L, 1));
3094 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3095}
3096
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003097/* This function takes a string as input, and append it at the
3098 * input side of channel. If the data is too big, but a space
3099 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003100 * yields. If the data is bigger than the buffer, or if the
3101 * channel is closed, it returns -1. Otherwise, it returns the
3102 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003104__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003105{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003106 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107 size_t len;
3108 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3109 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3110 int ret;
3111 int max;
3112
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003113 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003114 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003115 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003116 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003117
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003118 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003119 * the request buffer if its not required.
3120 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003121 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003122 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003123 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003124 }
3125
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003126 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 if (max > len - l)
3128 max = len - l;
3129
Willy Tarreau06d80a92017-10-19 14:32:15 +02003130 ret = ci_putblk(chn, str + l, max);
Christopher Faulet055310f2021-08-05 11:58:37 +02003131 if (ret < 0) {
3132 if (ret == -2 || ret == -3 || HLUA_CANT_YIELD(hlua_gethlua(L))) {
3133 lua_pushinteger(L, -1);
3134 return 1;
3135 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003136 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003137 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003138 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003139 l += ret;
3140 lua_pop(L, 1);
3141 lua_pushinteger(L, l);
3142
Christopher Faulet055310f2021-08-05 11:58:37 +02003143 if (l < len) {
3144 /* If there are no space available, and the output buffer is
3145 * empty, we cannot add more data, so we cannot yield, we return
3146 * the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003147 */
Christopher Faulet055310f2021-08-05 11:58:37 +02003148 max = channel_recv_limit(chn) - b_data(&chn->buf);
3149 if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L)))
3150 return 1;
Willy Tarreau9635e032018-10-16 17:52:55 +02003151 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Faulet055310f2021-08-05 11:58:37 +02003152 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003153 return 1;
3154}
3155
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003156/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3157 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003158 * buffer size is too little for the data.
3159 */
3160__LJMP static int hlua_channel_append(lua_State *L)
3161{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003162 size_t len;
3163
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003165 MAY_LJMP(hlua_checkchannel(L, 1));
3166 MAY_LJMP(luaL_checklstring(L, 2, &len));
3167 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003168 lua_pushinteger(L, 0);
3169
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003170 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003171}
3172
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003173/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003174 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003175 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003176 * or -1 if the channel is closed or if the buffer size is too
3177 * little for the data.
3178 */
3179__LJMP static int hlua_channel_set(lua_State *L)
3180{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003181 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003182
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003183 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003184 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003185 lua_pushinteger(L, 0);
3186
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003187 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003188 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003189 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003190 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003191
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003192 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003193
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003194 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003195}
3196
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003197/* Append data in the output side of the buffer. This data is immediately
3198 * sent. The function returns the amount of data written. If the buffer
3199 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003200 * if the channel is closed.
3201 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003202__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003203{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003204 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003205 size_t len;
3206 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3207 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3208 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003209 struct hlua *hlua;
3210
3211 /* Get hlua struct, or NULL if we execute from main lua state */
3212 hlua = hlua_gethlua(L);
3213 if (!hlua) {
3214 lua_pushnil(L);
3215 return 1;
3216 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003217
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003218 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003219 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003220 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003221 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003222
Willy Tarreau47860ed2015-03-10 14:07:50 +01003223 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003224 lua_pushinteger(L, -1);
3225 return 1;
3226 }
3227
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003228 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003229 * the request buffer if its not required.
3230 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003231 if (chn->buf.size == 0) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003232 if (HLUA_CANT_YIELD(hlua_gethlua(L)))
3233 return 1;
Willy Tarreau4b962a42018-11-15 11:03:21 +01003234 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003235 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003236 }
3237
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003238 /* The written data will be immediately sent, so we can check
3239 * the available space without taking in account the reserve.
3240 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003241 * data, because the buffer will be flushed.
3242 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003243 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003244
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003245 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003246 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003247 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003248 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003249 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003250 return 1;
3251
3252 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003253 if (max > len - l)
3254 max = len - l;
3255
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003256 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003257 * detects a non contiguous buffer and realign it.
3258 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003259 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003260 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003261
3262 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003263 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003264
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003265 /* buffer replace considers that the input part is filled.
3266 * so, I must forward these new data in the output part.
3267 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003268 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003269
3270 l += max;
3271 lua_pop(L, 1);
3272 lua_pushinteger(L, l);
3273
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003274 if (l < len) {
Christopher Faulet055310f2021-08-05 11:58:37 +02003275 /* If there is no space available, and the output buffer is empty.
3276 * in this case, we cannot add more data, so we cannot yield,
3277 * we return the amount of copied data.
3278 */
3279 max = b_room(&chn->buf);
3280 if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L)))
3281 return 1;
3282
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003283 /* If we are waiting for space in the response buffer, we
3284 * must set the flag WAKERESWR. This flag required the task
3285 * wake up if any activity is detected on the response buffer.
3286 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003287 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003288 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003289 else
3290 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003291 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003292 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003293
3294 return 1;
3295}
3296
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003297/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003298 * yield the LUA process, and resume it without checking the
3299 * input arguments.
3300 */
3301__LJMP static int hlua_channel_send(lua_State *L)
3302{
3303 MAY_LJMP(check_args(L, 2, "send"));
3304 lua_pushinteger(L, 0);
3305
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003306 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003307}
3308
3309/* This function forward and amount of butes. The data pass from
3310 * the input side of the buffer to the output side, and can be
3311 * forwarded. This function never fails.
3312 *
3313 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003314 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003315 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003316__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003317{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003318 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003319 int len;
3320 int l;
3321 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003322 struct hlua *hlua;
3323
3324 /* Get hlua struct, or NULL if we execute from main lua state */
3325 hlua = hlua_gethlua(L);
3326 if (!hlua)
3327 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003328
3329 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003330
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003331 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003332 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003333 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003334 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003335
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003336 len = MAY_LJMP(luaL_checkinteger(L, 2));
3337 l = MAY_LJMP(luaL_checkinteger(L, -1));
3338
3339 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003340 if (max > ci_data(chn))
3341 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003342 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003343 l += max;
3344
3345 lua_pop(L, 1);
3346 lua_pushinteger(L, l);
3347
3348 /* Check if it miss bytes to forward. */
3349 if (l < len) {
3350 /* The the input channel or the output channel are closed, we
3351 * must return the amount of data forwarded.
3352 */
Christopher Faulet055310f2021-08-05 11:58:37 +02003353 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003354 return 1;
3355
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003356 /* If we are waiting for space data in the response buffer, we
3357 * must set the flag WAKERESWR. This flag required the task
3358 * wake up if any activity is detected on the response buffer.
3359 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003360 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003361 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003362 else
3363 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003364
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003365 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003366 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003367 }
3368
3369 return 1;
3370}
3371
3372/* Just check the input and prepare the stack for the previous
3373 * function "hlua_channel_forward_yield"
3374 */
3375__LJMP static int hlua_channel_forward(lua_State *L)
3376{
3377 MAY_LJMP(check_args(L, 2, "forward"));
3378 MAY_LJMP(hlua_checkchannel(L, 1));
3379 MAY_LJMP(luaL_checkinteger(L, 2));
3380
3381 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003382 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003383}
3384
3385/* Just returns the number of bytes available in the input
3386 * side of the buffer. This function never fails.
3387 */
3388__LJMP static int hlua_channel_get_in_len(lua_State *L)
3389{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003390 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003391
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003392 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003393 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003394 if (IS_HTX_STRM(chn_strm(chn))) {
3395 struct htx *htx = htxbuf(&chn->buf);
3396 lua_pushinteger(L, htx->data - co_data(chn));
3397 }
3398 else
3399 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003400 return 1;
3401}
3402
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003403/* Returns true if the channel is full. */
3404__LJMP static int hlua_channel_is_full(lua_State *L)
3405{
3406 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003407
3408 MAY_LJMP(check_args(L, 1, "is_full"));
3409 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003410 /* ignore the reserve, we are not on a producer side (ie in an
3411 * applet).
3412 */
3413 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003414 return 1;
3415}
3416
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003417/* Returns true if the channel is the response channel. */
3418__LJMP static int hlua_channel_is_resp(lua_State *L)
3419{
3420 struct channel *chn;
3421
3422 MAY_LJMP(check_args(L, 1, "is_resp"));
3423 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3424
3425 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3426 return 1;
3427}
3428
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003429/* Just returns the number of bytes available in the output
3430 * side of the buffer. This function never fails.
3431 */
3432__LJMP static int hlua_channel_get_out_len(lua_State *L)
3433{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003434 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003435
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003436 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003437 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003438 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003439 return 1;
3440}
3441
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003442/*
3443 *
3444 *
3445 * Class Fetches
3446 *
3447 *
3448 */
3449
3450/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003451 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003452 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003453__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003454{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003455 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003456}
3457
3458/* This function creates and push in the stack a fetch object according
3459 * with a current TXN.
3460 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003461static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003462{
Willy Tarreau7073c472015-04-06 11:15:40 +02003463 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003464
3465 /* Check stack size. */
3466 if (!lua_checkstack(L, 3))
3467 return 0;
3468
3469 /* Create the object: obj[0] = userdata.
3470 * Note that the base of the Fetches object is the
3471 * transaction object.
3472 */
3473 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003474 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003475 lua_rawseti(L, -2, 0);
3476
Willy Tarreau7073c472015-04-06 11:15:40 +02003477 hsmp->s = txn->s;
3478 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003479 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003480 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003481
3482 /* Pop a class sesison metatable and affect it to the userdata. */
3483 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3484 lua_setmetatable(L, -2);
3485
3486 return 1;
3487}
3488
3489/* This function is an LUA binding. It is called with each sample-fetch.
3490 * It uses closure argument to store the associated sample-fetch. It
3491 * returns only one argument or throws an error. An error is thrown
3492 * only if an error is encountered during the argument parsing. If
3493 * the "sample-fetch" function fails, nil is returned.
3494 */
3495__LJMP static int hlua_run_sample_fetch(lua_State *L)
3496{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003497 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003498 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003499 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003500 int i;
3501 struct sample smp;
3502
3503 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003504 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003505
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003506 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003507 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003508
Thierry FOURNIERca988662015-12-20 18:43:03 +01003509 /* Check execution authorization. */
3510 if (f->use & SMP_USE_HTTP_ANY &&
3511 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3512 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3513 "is not available in Lua services", f->kw);
3514 WILL_LJMP(lua_error(L));
3515 }
3516
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003517 /* Get extra arguments. */
3518 for (i = 0; i < lua_gettop(L) - 1; i++) {
3519 if (i >= ARGM_NBARGS)
3520 break;
3521 hlua_lua2arg(L, i + 2, &args[i]);
3522 }
3523 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003524 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003525
3526 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003527 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003528
3529 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003530 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003531 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003532 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003533 }
3534
3535 /* Initialise the sample. */
3536 memset(&smp, 0, sizeof(smp));
3537
3538 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003539 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003540 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003541 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003542 lua_pushstring(L, "");
3543 else
3544 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003545 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003546 }
3547
3548 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003549 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003550 hlua_smp2lua_str(L, &smp);
3551 else
3552 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003553
3554 end:
3555 for (i = 0; args[i].type != ARGT_STOP; i++) {
3556 if (args[i].type == ARGT_STR)
3557 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003558 else if (args[i].type == ARGT_REG)
3559 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003560 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003561 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003562
3563 error:
3564 for (i = 0; args[i].type != ARGT_STOP; i++) {
3565 if (args[i].type == ARGT_STR)
3566 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003567 else if (args[i].type == ARGT_REG)
3568 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003569 }
3570 WILL_LJMP(lua_error(L));
3571 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003572}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003573
3574/*
3575 *
3576 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003577 * Class Converters
3578 *
3579 *
3580 */
3581
3582/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003583 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003584 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003585__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003586{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003587 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003588}
3589
3590/* This function creates and push in the stack a Converters object
3591 * according with a current TXN.
3592 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003593static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003594{
Willy Tarreau7073c472015-04-06 11:15:40 +02003595 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003596
3597 /* Check stack size. */
3598 if (!lua_checkstack(L, 3))
3599 return 0;
3600
3601 /* Create the object: obj[0] = userdata.
3602 * Note that the base of the Converters object is the
3603 * same than the TXN object.
3604 */
3605 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003606 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003607 lua_rawseti(L, -2, 0);
3608
Willy Tarreau7073c472015-04-06 11:15:40 +02003609 hsmp->s = txn->s;
3610 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003611 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003612 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003613
Willy Tarreau87b09662015-04-03 00:22:06 +02003614 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003615 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3616 lua_setmetatable(L, -2);
3617
3618 return 1;
3619}
3620
3621/* This function is an LUA binding. It is called with each converter.
3622 * It uses closure argument to store the associated converter. It
3623 * returns only one argument or throws an error. An error is thrown
3624 * only if an error is encountered during the argument parsing. If
3625 * the converter function function fails, nil is returned.
3626 */
3627__LJMP static int hlua_run_sample_conv(lua_State *L)
3628{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003629 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003630 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003631 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003632 int i;
3633 struct sample smp;
3634
3635 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003636 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003637
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003638 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003639 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003640
3641 /* Get extra arguments. */
3642 for (i = 0; i < lua_gettop(L) - 2; i++) {
3643 if (i >= ARGM_NBARGS)
3644 break;
3645 hlua_lua2arg(L, i + 3, &args[i]);
3646 }
3647 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003648 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003649
3650 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003651 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003652
3653 /* Run the special args checker. */
3654 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3655 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003656 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003657 }
3658
3659 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003660 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003661 if (!hlua_lua2smp(L, 2, &smp)) {
3662 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003663 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003664 }
3665
Willy Tarreau1777ea62016-03-10 16:15:46 +01003666 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3667
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003668 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003669 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003670 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003671 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003672 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003673 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003674 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3675 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003676 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003677 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003678 }
3679
3680 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003681 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003682 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003683 lua_pushstring(L, "");
3684 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003685 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003686 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003687 }
3688
3689 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003690 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003691 hlua_smp2lua_str(L, &smp);
3692 else
3693 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003694 end:
3695 for (i = 0; args[i].type != ARGT_STOP; i++) {
3696 if (args[i].type == ARGT_STR)
3697 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003698 else if (args[i].type == ARGT_REG)
3699 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003700 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003701 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003702
3703 error:
3704 for (i = 0; args[i].type != ARGT_STOP; i++) {
3705 if (args[i].type == ARGT_STR)
3706 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003707 else if (args[i].type == ARGT_REG)
3708 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003709 }
3710 WILL_LJMP(lua_error(L));
3711 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003712}
3713
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003714/*
3715 *
3716 *
3717 * Class AppletTCP
3718 *
3719 *
3720 */
3721
3722/* Returns a struct hlua_txn if the stack entry "ud" is
3723 * a class stream, otherwise it throws an error.
3724 */
3725__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3726{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003727 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003728}
3729
3730/* This function creates and push in the stack an Applet object
3731 * according with a current TXN.
3732 */
3733static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3734{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003735 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003736 struct stream_interface *si = ctx->owner;
3737 struct stream *s = si_strm(si);
3738 struct proxy *p = s->be;
3739
3740 /* Check stack size. */
3741 if (!lua_checkstack(L, 3))
3742 return 0;
3743
3744 /* Create the object: obj[0] = userdata.
3745 * Note that the base of the Converters object is the
3746 * same than the TXN object.
3747 */
3748 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003749 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003750 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003751 luactx->appctx = ctx;
3752 luactx->htxn.s = s;
3753 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003754
3755 /* Create the "f" field that contains a list of fetches. */
3756 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003757 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003758 return 0;
3759 lua_settable(L, -3);
3760
3761 /* Create the "sf" field that contains a list of stringsafe fetches. */
3762 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003763 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003764 return 0;
3765 lua_settable(L, -3);
3766
3767 /* Create the "c" field that contains a list of converters. */
3768 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003769 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003770 return 0;
3771 lua_settable(L, -3);
3772
3773 /* Create the "sc" field that contains a list of stringsafe converters. */
3774 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003775 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003776 return 0;
3777 lua_settable(L, -3);
3778
3779 /* Pop a class stream metatable and affect it to the table. */
3780 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3781 lua_setmetatable(L, -2);
3782
3783 return 1;
3784}
3785
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003786__LJMP static int hlua_applet_tcp_set_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
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003794 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3795 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003796
3797 /* It is useles to retrieve the stream, but this function
3798 * runs only in a stream context.
3799 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003800 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003801 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003802 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003803
3804 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003805 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003806 hlua_lua2smp(L, 3, &smp);
3807
3808 /* Store the sample in a variable. */
3809 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003810
3811 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3812 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3813 else
3814 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3815
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003816 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003817}
3818
3819__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3820{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003821 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003822 struct stream *s;
3823 const char *name;
3824 size_t len;
3825 struct sample smp;
3826
3827 MAY_LJMP(check_args(L, 2, "unset_var"));
3828
3829 /* It is useles to retrieve the stream, but this function
3830 * runs only in a stream context.
3831 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003832 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003833 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003834 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003835
3836 /* Unset the variable. */
3837 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003838 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3839 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003840}
3841
3842__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3843{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003844 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003845 struct stream *s;
3846 const char *name;
3847 size_t len;
3848 struct sample smp;
3849
3850 MAY_LJMP(check_args(L, 2, "get_var"));
3851
3852 /* It is useles to retrieve the stream, but this function
3853 * runs only in a stream context.
3854 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003855 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003856 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003857 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003858
3859 smp_set_owner(&smp, s->be, s->sess, s, 0);
3860 if (!vars_get_by_name(name, len, &smp)) {
3861 lua_pushnil(L);
3862 return 1;
3863 }
3864
3865 return hlua_smp2lua(L, &smp);
3866}
3867
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003868__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3869{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003870 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3871 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003872 struct hlua *hlua;
3873
3874 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003875 if (!s->hlua)
3876 return 0;
3877 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003878
3879 MAY_LJMP(check_args(L, 2, "set_priv"));
3880
3881 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003882 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003883
3884 /* Get and store new value. */
3885 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3886 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3887
3888 return 0;
3889}
3890
3891__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3892{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003893 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3894 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003895 struct hlua *hlua;
3896
3897 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003898 if (!s->hlua) {
3899 lua_pushnil(L);
3900 return 1;
3901 }
3902 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003903
3904 /* Push configuration index in the stack. */
3905 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3906
3907 return 1;
3908}
3909
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003910/* If expected data not yet available, it returns a yield. This function
3911 * consumes the data in the buffer. It returns a string containing the
3912 * data. This string can be empty.
3913 */
3914__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3915{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003916 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3917 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003918 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003919 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003920 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003921 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003922 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003923
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003924 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003925 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003926
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003927 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003928 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003929 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003930 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003931 }
3932
3933 /* End of data: commit the total strings and return. */
3934 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003935 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003936 return 1;
3937 }
3938
3939 /* Ensure that the block 2 length is usable. */
3940 if (ret == 1)
3941 len2 = 0;
3942
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003943 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003944 luaL_addlstring(&luactx->b, blk1, len1);
3945 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003946
3947 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003948 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003949 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003950 return 1;
3951}
3952
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003953/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003954__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3955{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003956 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003957
3958 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003959 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003960
3961 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3962}
3963
3964/* If expected data not yet available, it returns a yield. This function
3965 * consumes the data in the buffer. It returns a string containing the
3966 * data. This string can be empty.
3967 */
3968__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3969{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003970 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3971 struct stream_interface *si = luactx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003972 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003973 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003974 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003975 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003976 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003977 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003978
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003979 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003980 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003981
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003982 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003983 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003984 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003985 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003986 }
3987
3988 /* End of data: commit the total strings and return. */
3989 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003990 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003991 return 1;
3992 }
3993
3994 /* Ensure that the block 2 length is usable. */
3995 if (ret == 1)
3996 len2 = 0;
3997
3998 if (len == -1) {
3999
4000 /* If len == -1, catenate all the data avalaile and
4001 * yield because we want to get all the data until
4002 * the end of data stream.
4003 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004004 luaL_addlstring(&luactx->b, blk1, len1);
4005 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004006 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004007 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004008 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004009
4010 } else {
4011
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004012 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004013 if (len1 > len)
4014 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004015 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004016 len -= len1;
4017
4018 /* Copy the second block. */
4019 if (len2 > len)
4020 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004021 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004022 len -= len2;
4023
4024 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004025 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004026
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004027 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004028 if (len > 0) {
4029 lua_pushinteger(L, len);
4030 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004031 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004032 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004033 }
4034
4035 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004036 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004037 return 1;
4038 }
4039
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004040 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004041 hlua_pusherror(L, "Lua: internal error");
4042 WILL_LJMP(lua_error(L));
4043 return 0;
4044}
4045
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004046/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004047__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4048{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004049 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004050 int len = -1;
4051
4052 if (lua_gettop(L) > 2)
4053 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4054 if (lua_gettop(L) >= 2) {
4055 len = MAY_LJMP(luaL_checkinteger(L, 2));
4056 lua_pop(L, 1);
4057 }
4058
4059 /* Confirm or set the required length */
4060 lua_pushinteger(L, len);
4061
4062 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004063 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004064
4065 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4066}
4067
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004068/* Append data in the output side of the buffer. This data is immediately
4069 * sent. The function returns the amount of data written. If the buffer
4070 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004071 * if the channel is closed.
4072 */
4073__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4074{
4075 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004076 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004077 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4078 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004079 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004080 struct channel *chn = si_ic(si);
4081 int max;
4082
4083 /* Get the max amount of data which can write as input in the channel. */
4084 max = channel_recv_max(chn);
4085 if (max > (len - l))
4086 max = len - l;
4087
4088 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004089 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004090
4091 /* update counters. */
4092 l += max;
4093 lua_pop(L, 1);
4094 lua_pushinteger(L, l);
4095
4096 /* If some data is not send, declares the situation to the
4097 * applet, and returns a yield.
4098 */
4099 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004100 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004101 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004102 }
4103
4104 return 1;
4105}
4106
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004107/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004108 * yield the LUA process, and resume it without checking the
4109 * input arguments.
4110 */
4111__LJMP static int hlua_applet_tcp_send(lua_State *L)
4112{
4113 MAY_LJMP(check_args(L, 2, "send"));
4114 lua_pushinteger(L, 0);
4115
4116 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4117}
4118
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004119/*
4120 *
4121 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004122 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004123 *
4124 *
4125 */
4126
4127/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004128 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004129 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004130__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004131{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004132 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004133}
4134
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004135/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004136 * according with a current TXN.
4137 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004138static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004139{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004140 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004141 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004142 struct stream_interface *si = ctx->owner;
4143 struct stream *s = si_strm(si);
4144 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004145 struct htx *htx;
4146 struct htx_blk *blk;
4147 struct htx_sl *sl;
4148 struct ist path;
4149 unsigned long long len = 0;
4150 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004151
4152 /* Check stack size. */
4153 if (!lua_checkstack(L, 3))
4154 return 0;
4155
4156 /* Create the object: obj[0] = userdata.
4157 * Note that the base of the Converters object is the
4158 * same than the TXN object.
4159 */
4160 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004161 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004162 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004163 luactx->appctx = ctx;
4164 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4165 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4166 luactx->htxn.s = s;
4167 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004168
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004169 /* Create the "f" field that contains a list of fetches. */
4170 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004171 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004172 return 0;
4173 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004174
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004175 /* Create the "sf" field that contains a list of stringsafe fetches. */
4176 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004177 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004178 return 0;
4179 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004180
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004181 /* Create the "c" field that contains a list of converters. */
4182 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004183 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004184 return 0;
4185 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004186
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004187 /* Create the "sc" field that contains a list of stringsafe converters. */
4188 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004189 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004190 return 0;
4191 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004192
Christopher Fauleta2097962019-07-15 16:25:33 +02004193 htx = htxbuf(&s->req.buf);
4194 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004195 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004196 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004197
Christopher Fauleta2097962019-07-15 16:25:33 +02004198 /* Stores the request method. */
4199 lua_pushstring(L, "method");
4200 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4201 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004202
Christopher Fauleta2097962019-07-15 16:25:33 +02004203 /* Stores the http version. */
4204 lua_pushstring(L, "version");
4205 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4206 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004207
Christopher Fauleta2097962019-07-15 16:25:33 +02004208 /* creates an array of headers. hlua_http_get_headers() crates and push
4209 * the array on the top of the stack.
4210 */
4211 lua_pushstring(L, "headers");
4212 htxn.s = s;
4213 htxn.p = px;
4214 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004215 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004216 return 0;
4217 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004218
Christopher Fauleta2097962019-07-15 16:25:33 +02004219 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004220 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004221 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004222
Christopher Fauleta2097962019-07-15 16:25:33 +02004223 p = path.ptr;
4224 end = path.ptr + path.len;
4225 q = p;
4226 while (q < end && *q != '?')
4227 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004228
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004229 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004230 lua_pushstring(L, "path");
4231 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004232 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004233
Christopher Fauleta2097962019-07-15 16:25:33 +02004234 /* Stores the query string. */
4235 lua_pushstring(L, "qs");
4236 if (*q == '?')
4237 q++;
4238 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004239 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004240 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004241
Christopher Fauleta2097962019-07-15 16:25:33 +02004242 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4243 struct htx_blk *blk = htx_get_blk(htx, pos);
4244 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004245
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004246 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004247 break;
4248 if (type == HTX_BLK_DATA)
4249 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004250 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004251 if (htx->extra != ULLONG_MAX)
4252 len += htx->extra;
4253
4254 /* Stores the request path. */
4255 lua_pushstring(L, "length");
4256 lua_pushinteger(L, len);
4257 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004258
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004259 /* Create an empty array of HTTP request headers. */
4260 lua_pushstring(L, "response");
4261 lua_newtable(L);
4262 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004263
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004264 /* Pop a class stream metatable and affect it to the table. */
4265 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4266 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004267
4268 return 1;
4269}
4270
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004271__LJMP static int hlua_applet_http_set_var(lua_State *L)
4272{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004273 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004274 struct stream *s;
4275 const char *name;
4276 size_t len;
4277 struct sample smp;
4278
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004279 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4280 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004281
4282 /* It is useles to retrieve the stream, but this function
4283 * runs only in a stream context.
4284 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004285 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004286 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004287 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004288
4289 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004290 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004291 hlua_lua2smp(L, 3, &smp);
4292
4293 /* Store the sample in a variable. */
4294 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004295
4296 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4297 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4298 else
4299 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4300
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004301 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004302}
4303
4304__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4305{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004306 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004307 struct stream *s;
4308 const char *name;
4309 size_t len;
4310 struct sample smp;
4311
4312 MAY_LJMP(check_args(L, 2, "unset_var"));
4313
4314 /* It is useles to retrieve the stream, but this function
4315 * runs only in a stream context.
4316 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004317 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004318 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004319 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004320
4321 /* Unset the variable. */
4322 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004323 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4324 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004325}
4326
4327__LJMP static int hlua_applet_http_get_var(lua_State *L)
4328{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004329 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004330 struct stream *s;
4331 const char *name;
4332 size_t len;
4333 struct sample smp;
4334
4335 MAY_LJMP(check_args(L, 2, "get_var"));
4336
4337 /* It is useles to retrieve the stream, but this function
4338 * runs only in a stream context.
4339 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004340 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004341 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004342 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004343
4344 smp_set_owner(&smp, s->be, s->sess, s, 0);
4345 if (!vars_get_by_name(name, len, &smp)) {
4346 lua_pushnil(L);
4347 return 1;
4348 }
4349
4350 return hlua_smp2lua(L, &smp);
4351}
4352
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004353__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4354{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004355 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4356 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004357 struct hlua *hlua;
4358
4359 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004360 if (!s->hlua)
4361 return 0;
4362 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004363
4364 MAY_LJMP(check_args(L, 2, "set_priv"));
4365
4366 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004367 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004368
4369 /* Get and store new value. */
4370 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4371 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4372
4373 return 0;
4374}
4375
4376__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4377{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004378 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4379 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004380 struct hlua *hlua;
4381
4382 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004383 if (!s->hlua) {
4384 lua_pushnil(L);
4385 return 1;
4386 }
4387 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004388
4389 /* Push configuration index in the stack. */
4390 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4391
4392 return 1;
4393}
4394
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004395/* If expected data not yet available, it returns a yield. This function
4396 * consumes the data in the buffer. It returns a string containing the
4397 * data. This string can be empty.
4398 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004399__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004400{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004401 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4402 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004403 struct channel *req = si_oc(si);
4404 struct htx *htx;
4405 struct htx_blk *blk;
4406 size_t count;
4407 int stop = 0;
4408
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004409 htx = htx_from_buf(&req->buf);
4410 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004411 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004412
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004413 while (count && !stop && blk) {
4414 enum htx_blk_type type = htx_get_blk_type(blk);
4415 uint32_t sz = htx_get_blksz(blk);
4416 struct ist v;
4417 uint32_t vlen;
4418 char *nl;
4419
4420 vlen = sz;
4421 if (vlen > count) {
4422 if (type != HTX_BLK_DATA)
4423 break;
4424 vlen = count;
4425 }
4426
4427 switch (type) {
4428 case HTX_BLK_UNUSED:
4429 break;
4430
4431 case HTX_BLK_DATA:
4432 v = htx_get_blk_value(htx, blk);
4433 v.len = vlen;
4434 nl = istchr(v, '\n');
4435 if (nl != NULL) {
4436 stop = 1;
4437 vlen = nl - v.ptr + 1;
4438 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004439 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004440 break;
4441
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004442 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004443 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004444 stop = 1;
4445 break;
4446
4447 default:
4448 break;
4449 }
4450
4451 co_set_data(req, co_data(req) - vlen);
4452 count -= vlen;
4453 if (sz == vlen)
4454 blk = htx_remove_blk(htx, blk);
4455 else {
4456 htx_cut_data_blk(htx, blk, vlen);
4457 break;
4458 }
4459 }
4460
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004461 /* The message was fully consumed and no more data are expected
4462 * (EOM flag set).
4463 */
4464 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4465 stop = 1;
4466
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004467 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004468 if (!stop) {
4469 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004470 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004471 }
4472
4473 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004474 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004475 return 1;
4476}
4477
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004478
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004479/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004480__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004481{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004482 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004483
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004484 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004485 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004486
Christopher Fauleta2097962019-07-15 16:25:33 +02004487 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004488}
4489
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004490/* If expected data not yet available, it returns a yield. This function
4491 * consumes the data in the buffer. It returns a string containing the
4492 * data. This string can be empty.
4493 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004494__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004495{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004496 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4497 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004498 struct channel *req = si_oc(si);
4499 struct htx *htx;
4500 struct htx_blk *blk;
4501 size_t count;
4502 int len;
4503
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004504 htx = htx_from_buf(&req->buf);
4505 len = MAY_LJMP(luaL_checkinteger(L, 2));
4506 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004507 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004508 while (count && len && blk) {
4509 enum htx_blk_type type = htx_get_blk_type(blk);
4510 uint32_t sz = htx_get_blksz(blk);
4511 struct ist v;
4512 uint32_t vlen;
4513
4514 vlen = sz;
4515 if (len > 0 && vlen > len)
4516 vlen = len;
4517 if (vlen > count) {
4518 if (type != HTX_BLK_DATA)
4519 break;
4520 vlen = count;
4521 }
4522
4523 switch (type) {
4524 case HTX_BLK_UNUSED:
4525 break;
4526
4527 case HTX_BLK_DATA:
4528 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004529 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004530 break;
4531
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004532 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004533 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004534 len = 0;
4535 break;
4536
4537 default:
4538 break;
4539 }
4540
4541 co_set_data(req, co_data(req) - vlen);
4542 count -= vlen;
4543 if (len > 0)
4544 len -= vlen;
4545 if (sz == vlen)
4546 blk = htx_remove_blk(htx, blk);
4547 else {
4548 htx_cut_data_blk(htx, blk, vlen);
4549 break;
4550 }
4551 }
4552
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004553 /* The message was fully consumed and no more data are expected
4554 * (EOM flag set).
4555 */
4556 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4557 len = 0;
4558
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004559 htx_to_buf(htx, &req->buf);
4560
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004561 /* If we are no other data available, yield waiting for new data. */
4562 if (len) {
4563 if (len > 0) {
4564 lua_pushinteger(L, len);
4565 lua_replace(L, 2);
4566 }
4567 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004568 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004569 }
4570
4571 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004572 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004573 return 1;
4574}
4575
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004576/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004577__LJMP static int hlua_applet_http_recv(lua_State *L)
4578{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004579 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004580 int len = -1;
4581
4582 /* Check arguments. */
4583 if (lua_gettop(L) > 2)
4584 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4585 if (lua_gettop(L) >= 2) {
4586 len = MAY_LJMP(luaL_checkinteger(L, 2));
4587 lua_pop(L, 1);
4588 }
4589
Christopher Fauleta2097962019-07-15 16:25:33 +02004590 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004591
Christopher Fauleta2097962019-07-15 16:25:33 +02004592 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004593 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004594
Christopher Fauleta2097962019-07-15 16:25:33 +02004595 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004596}
4597
4598/* Append data in the output side of the buffer. This data is immediately
4599 * sent. The function returns the amount of data written. If the buffer
4600 * cannot contain the data, the function yields. The function returns -1
4601 * if the channel is closed.
4602 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004603__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004604{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004605 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4606 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004607 struct channel *res = si_ic(si);
4608 struct htx *htx = htx_from_buf(&res->buf);
4609 const char *data;
4610 size_t len;
4611 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4612 int max;
4613
Christopher Faulet9060fc02019-07-03 11:39:30 +02004614 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004615 if (!max)
4616 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004617
4618 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4619
4620 /* Get the max amount of data which can write as input in the channel. */
4621 if (max > (len - l))
4622 max = len - l;
4623
4624 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004625 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004626 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004627
4628 /* update counters. */
4629 l += max;
4630 lua_pop(L, 1);
4631 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004632
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004633 /* If some data is not send, declares the situation to the
4634 * applet, and returns a yield.
4635 */
4636 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004637 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004638 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004639 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004640 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004641 }
4642
Christopher Fauleta2097962019-07-15 16:25:33 +02004643 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004644 return 1;
4645}
4646
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004647/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004648 * yield the LUA process, and resume it without checking the
4649 * input arguments.
4650 */
4651__LJMP static int hlua_applet_http_send(lua_State *L)
4652{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004653 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004654
4655 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004656 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004657 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4658 WILL_LJMP(lua_error(L));
4659 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004660
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004661 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004662 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004663
Christopher Fauleta2097962019-07-15 16:25:33 +02004664 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004665}
4666
4667__LJMP static int hlua_applet_http_addheader(lua_State *L)
4668{
4669 const char *name;
4670 int ret;
4671
4672 MAY_LJMP(hlua_checkapplet_http(L, 1));
4673 name = MAY_LJMP(luaL_checkstring(L, 2));
4674 MAY_LJMP(luaL_checkstring(L, 3));
4675
4676 /* Push in the stack the "response" entry. */
4677 ret = lua_getfield(L, 1, "response");
4678 if (ret != LUA_TTABLE) {
4679 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4680 "is expected as an array. %s found", lua_typename(L, ret));
4681 WILL_LJMP(lua_error(L));
4682 }
4683
4684 /* check if the header is already registered if it is not
4685 * the case, register it.
4686 */
4687 ret = lua_getfield(L, -1, name);
4688 if (ret == LUA_TNIL) {
4689
4690 /* Entry not found. */
4691 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4692
4693 /* Insert the new header name in the array in the top of the stack.
4694 * It left the new array in the top of the stack.
4695 */
4696 lua_newtable(L);
4697 lua_pushvalue(L, 2);
4698 lua_pushvalue(L, -2);
4699 lua_settable(L, -4);
4700
4701 } else if (ret != LUA_TTABLE) {
4702
4703 /* corruption error. */
4704 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4705 "is expected as an array. %s found", name, lua_typename(L, ret));
4706 WILL_LJMP(lua_error(L));
4707 }
4708
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004709 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004710 * the header value as new entry.
4711 */
4712 lua_pushvalue(L, 3);
4713 ret = lua_rawlen(L, -2);
4714 lua_rawseti(L, -2, ret + 1);
4715 lua_pushboolean(L, 1);
4716 return 1;
4717}
4718
4719__LJMP static int hlua_applet_http_status(lua_State *L)
4720{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004721 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004722 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004723 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004724
4725 if (status < 100 || status > 599) {
4726 lua_pushboolean(L, 0);
4727 return 1;
4728 }
4729
Willy Tarreau7e702d12021-04-28 17:59:21 +02004730 luactx->appctx->ctx.hlua_apphttp.status = status;
4731 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004732 lua_pushboolean(L, 1);
4733 return 1;
4734}
4735
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004736
Christopher Fauleta2097962019-07-15 16:25:33 +02004737__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004738{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004739 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4740 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004741 struct channel *res = si_ic(si);
4742 struct htx *htx;
4743 struct htx_sl *sl;
4744 struct h1m h1m;
4745 const char *status, *reason;
4746 const char *name, *value;
4747 size_t nlen, vlen;
4748 unsigned int flags;
4749
4750 /* Send the message at once. */
4751 htx = htx_from_buf(&res->buf);
4752 h1m_init_res(&h1m);
4753
4754 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004755 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4756 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004757 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02004758 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
4759 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004760 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4761 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4762 }
4763 else {
4764 flags = HTX_SL_F_IS_RESP;
4765 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4766 }
4767 if (!sl) {
4768 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004769 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004770 WILL_LJMP(lua_error(L));
4771 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004772 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004773
4774 /* Get the array associated to the field "response" in the object AppletHTTP. */
4775 lua_pushvalue(L, 0);
4776 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4777 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004778 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004779 WILL_LJMP(lua_error(L));
4780 }
4781
4782 /* Browse the list of headers. */
4783 lua_pushnil(L);
4784 while(lua_next(L, -2) != 0) {
4785 /* We expect a string as -2. */
4786 if (lua_type(L, -2) != LUA_TSTRING) {
4787 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004788 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004789 lua_typename(L, lua_type(L, -2)));
4790 WILL_LJMP(lua_error(L));
4791 }
4792 name = lua_tolstring(L, -2, &nlen);
4793
4794 /* We expect an array as -1. */
4795 if (lua_type(L, -1) != LUA_TTABLE) {
4796 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 +02004797 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004798 name,
4799 lua_typename(L, lua_type(L, -1)));
4800 WILL_LJMP(lua_error(L));
4801 }
4802
4803 /* Browse the table who is on the top of the stack. */
4804 lua_pushnil(L);
4805 while(lua_next(L, -2) != 0) {
4806 int id;
4807
4808 /* We expect a number as -2. */
4809 if (lua_type(L, -2) != LUA_TNUMBER) {
4810 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 +02004811 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004812 name,
4813 lua_typename(L, lua_type(L, -2)));
4814 WILL_LJMP(lua_error(L));
4815 }
4816 id = lua_tointeger(L, -2);
4817
4818 /* We expect a string as -2. */
4819 if (lua_type(L, -1) != LUA_TSTRING) {
4820 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 +02004821 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004822 name, id,
4823 lua_typename(L, lua_type(L, -1)));
4824 WILL_LJMP(lua_error(L));
4825 }
4826 value = lua_tolstring(L, -1, &vlen);
4827
4828 /* Simple Protocol checks. */
4829 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004830 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004831 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4832 struct ist v = ist2(value, vlen);
4833 int ret;
4834
4835 ret = h1_parse_cont_len_header(&h1m, &v);
4836 if (ret < 0) {
4837 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004838 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004839 name);
4840 WILL_LJMP(lua_error(L));
4841 }
4842 else if (ret == 0)
4843 goto next; /* Skip it */
4844 }
4845
4846 /* Add a new header */
4847 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4848 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004849 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004850 name);
4851 WILL_LJMP(lua_error(L));
4852 }
4853 next:
4854 /* Remove the array from the stack, and get next element with a remaining string. */
4855 lua_pop(L, 1);
4856 }
4857
4858 /* Remove the array from the stack, and get next element with a remaining string. */
4859 lua_pop(L, 1);
4860 }
4861
4862 if (h1m.flags & H1_MF_CHNK)
4863 h1m.flags &= ~H1_MF_CLEN;
4864 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4865 h1m.flags |= H1_MF_XFER_LEN;
4866
4867 /* Uset HTX start-line flags */
4868 if (h1m.flags & H1_MF_XFER_ENC)
4869 flags |= HTX_SL_F_XFER_ENC;
4870 if (h1m.flags & H1_MF_XFER_LEN) {
4871 flags |= HTX_SL_F_XFER_LEN;
4872 if (h1m.flags & H1_MF_CHNK)
4873 flags |= HTX_SL_F_CHNK;
4874 else if (h1m.flags & H1_MF_CLEN)
4875 flags |= HTX_SL_F_CLEN;
4876 if (h1m.body_len == 0)
4877 flags |= HTX_SL_F_BODYLESS;
4878 }
4879 sl->flags |= flags;
4880
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004881 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004882 * and the status code implies the presence of a message body, we must
4883 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004884 * for the keepalive compliance. If the applet announces a transfer-encoding
4885 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004886 */
4887 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02004888 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
4889 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
4890 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004891 /* Add a new header */
4892 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4893 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4894 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004895 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004896 WILL_LJMP(lua_error(L));
4897 }
4898 }
4899
4900 /* Finalize headers. */
4901 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4902 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004903 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004904 WILL_LJMP(lua_error(L));
4905 }
4906
4907 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4908 b_reset(&res->buf);
4909 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4910 WILL_LJMP(lua_error(L));
4911 }
4912
4913 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004914 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004915
4916 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004917 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004918 return 0;
4919
4920}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004921/* We will build the status line and the headers of the HTTP response.
4922 * We will try send at once if its not possible, we give back the hand
4923 * waiting for more room.
4924 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004925__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004926{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004927 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4928 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004929 struct channel *res = si_ic(si);
4930
4931 if (co_data(res)) {
4932 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004933 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004934 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004935 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004936}
4937
4938
Christopher Fauleta2097962019-07-15 16:25:33 +02004939__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004940{
Christopher Fauleta2097962019-07-15 16:25:33 +02004941 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004942}
4943
Christopher Fauleta2097962019-07-15 16:25:33 +02004944/*
4945 *
4946 *
4947 * Class HTTP
4948 *
4949 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004950 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004951
4952/* Returns a struct hlua_txn if the stack entry "ud" is
4953 * a class stream, otherwise it throws an error.
4954 */
4955__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004956{
Christopher Fauleta2097962019-07-15 16:25:33 +02004957 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4958}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004959
Christopher Fauleta2097962019-07-15 16:25:33 +02004960/* This function creates and push in the stack a HTTP object
4961 * according with a current TXN.
4962 */
4963static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4964{
4965 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004966
Christopher Fauleta2097962019-07-15 16:25:33 +02004967 /* Check stack size. */
4968 if (!lua_checkstack(L, 3))
4969 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004970
Christopher Fauleta2097962019-07-15 16:25:33 +02004971 /* Create the object: obj[0] = userdata.
4972 * Note that the base of the Converters object is the
4973 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004974 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004975 lua_newtable(L);
4976 htxn = lua_newuserdata(L, sizeof(*htxn));
4977 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004978
4979 htxn->s = txn->s;
4980 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004981 htxn->dir = txn->dir;
4982 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004983
4984 /* Pop a class stream metatable and affect it to the table. */
4985 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4986 lua_setmetatable(L, -2);
4987
4988 return 1;
4989}
4990
4991/* This function creates ans returns an array of HTTP headers.
4992 * This function does not fails. It is used as wrapper with the
4993 * 2 following functions.
4994 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004995__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004996{
Christopher Fauleta2097962019-07-15 16:25:33 +02004997 struct htx *htx;
4998 int32_t pos;
4999
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005000 /* Create the table. */
5001 lua_newtable(L);
5002
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005003
Christopher Fauleta2097962019-07-15 16:25:33 +02005004 htx = htxbuf(&msg->chn->buf);
5005 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5006 struct htx_blk *blk = htx_get_blk(htx, pos);
5007 enum htx_blk_type type = htx_get_blk_type(blk);
5008 struct ist n, v;
5009 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005010
Christopher Fauleta2097962019-07-15 16:25:33 +02005011 if (type == HTX_BLK_HDR) {
5012 n = htx_get_blk_name(htx,blk);
5013 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005014 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005015 else if (type == HTX_BLK_EOH)
5016 break;
5017 else
5018 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005019
Christopher Fauleta2097962019-07-15 16:25:33 +02005020 /* Check for existing entry:
5021 * assume that the table is on the top of the stack, and
5022 * push the key in the stack, the function lua_gettable()
5023 * perform the lookup.
5024 */
5025 lua_pushlstring(L, n.ptr, n.len);
5026 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005027
Christopher Fauleta2097962019-07-15 16:25:33 +02005028 switch (lua_type(L, -1)) {
5029 case LUA_TNIL:
5030 /* Table not found, create it. */
5031 lua_pop(L, 1); /* remove the nil value. */
5032 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5033 lua_newtable(L); /* create and push empty table. */
5034 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5035 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5036 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005037 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005038
Christopher Fauleta2097962019-07-15 16:25:33 +02005039 case LUA_TTABLE:
5040 /* Entry found: push the value in the table. */
5041 len = lua_rawlen(L, -1);
5042 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5043 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5044 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5045 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005046
Christopher Fauleta2097962019-07-15 16:25:33 +02005047 default:
5048 /* Other cases are errors. */
5049 hlua_pusherror(L, "internal error during the parsing of headers.");
5050 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005051 }
5052 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005053 return 1;
5054}
5055
5056__LJMP static int hlua_http_req_get_headers(lua_State *L)
5057{
5058 struct hlua_txn *htxn;
5059
5060 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5061 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5062
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005063 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005064 WILL_LJMP(lua_error(L));
5065
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005066 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005067}
5068
5069__LJMP static int hlua_http_res_get_headers(lua_State *L)
5070{
5071 struct hlua_txn *htxn;
5072
5073 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5074 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5075
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005076 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005077 WILL_LJMP(lua_error(L));
5078
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005079 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005080}
5081
5082/* This function replace full header, or just a value in
5083 * the request or in the response. It is a wrapper fir the
5084 * 4 following functions.
5085 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005086__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005087{
5088 size_t name_len;
5089 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5090 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5091 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005092 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005093 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005094
Dragan Dosen26743032019-04-30 15:54:36 +02005095 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005096 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5097
Christopher Fauleta2097962019-07-15 16:25:33 +02005098 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005099 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005100 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005101 return 0;
5102}
5103
5104__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5105{
5106 struct hlua_txn *htxn;
5107
5108 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5109 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5110
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005111 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005112 WILL_LJMP(lua_error(L));
5113
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005114 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005115}
5116
5117__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5118{
5119 struct hlua_txn *htxn;
5120
5121 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5122 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5123
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005124 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005125 WILL_LJMP(lua_error(L));
5126
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005127 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005128}
5129
5130__LJMP static int hlua_http_req_rep_val(lua_State *L)
5131{
5132 struct hlua_txn *htxn;
5133
5134 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5135 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5136
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005137 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005138 WILL_LJMP(lua_error(L));
5139
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005140 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005141}
5142
5143__LJMP static int hlua_http_res_rep_val(lua_State *L)
5144{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005145 struct hlua_txn *htxn;
5146
5147 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5148 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5149
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005150 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005151 WILL_LJMP(lua_error(L));
5152
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005153 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005154}
5155
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005156/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005157 * It is a wrapper for the 2 following functions.
5158 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005159__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005160{
5161 size_t len;
5162 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005163 struct htx *htx = htxbuf(&msg->chn->buf);
5164 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005165
Christopher Fauleta2097962019-07-15 16:25:33 +02005166 ctx.blk = NULL;
5167 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5168 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005169 return 0;
5170}
5171
5172__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5173{
5174 struct hlua_txn *htxn;
5175
5176 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5177 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5178
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005179 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005180 WILL_LJMP(lua_error(L));
5181
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005182 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005183}
5184
5185__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5186{
5187 struct hlua_txn *htxn;
5188
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005189 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005190 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5191
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005192 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005193 WILL_LJMP(lua_error(L));
5194
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005195 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005196}
5197
5198/* This function adds an header. It is a wrapper used by
5199 * the 2 following functions.
5200 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005201__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005202{
5203 size_t name_len;
5204 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5205 size_t value_len;
5206 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005207 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005208
Christopher Fauleta2097962019-07-15 16:25:33 +02005209 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5210 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005211 return 0;
5212}
5213
5214__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5215{
5216 struct hlua_txn *htxn;
5217
5218 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5219 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5220
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005221 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005222 WILL_LJMP(lua_error(L));
5223
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005224 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005225}
5226
5227__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5228{
5229 struct hlua_txn *htxn;
5230
5231 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5232 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5233
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005234 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005235 WILL_LJMP(lua_error(L));
5236
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005237 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005238}
5239
5240static int hlua_http_req_set_hdr(lua_State *L)
5241{
5242 struct hlua_txn *htxn;
5243
5244 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5245 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5246
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005247 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005248 WILL_LJMP(lua_error(L));
5249
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005250 hlua_http_del_hdr(L, &htxn->s->txn->req);
5251 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005252}
5253
5254static int hlua_http_res_set_hdr(lua_State *L)
5255{
5256 struct hlua_txn *htxn;
5257
5258 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5259 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5260
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005261 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005262 WILL_LJMP(lua_error(L));
5263
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005264 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5265 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005266}
5267
5268/* This function set the method. */
5269static int hlua_http_req_set_meth(lua_State *L)
5270{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005271 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005272 size_t name_len;
5273 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005274
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005275 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005276 WILL_LJMP(lua_error(L));
5277
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005278 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005279 return 1;
5280}
5281
5282/* This function set the method. */
5283static int hlua_http_req_set_path(lua_State *L)
5284{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005285 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005286 size_t name_len;
5287 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005288
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005289 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005290 WILL_LJMP(lua_error(L));
5291
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005292 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005293 return 1;
5294}
5295
5296/* This function set the query-string. */
5297static int hlua_http_req_set_query(lua_State *L)
5298{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005299 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005300 size_t name_len;
5301 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005302
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005303 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005304 WILL_LJMP(lua_error(L));
5305
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005306 /* Check length. */
5307 if (name_len > trash.size - 1) {
5308 lua_pushboolean(L, 0);
5309 return 1;
5310 }
5311
5312 /* Add the mark question as prefix. */
5313 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005314 trash.area[trash.data++] = '?';
5315 memcpy(trash.area + trash.data, name, name_len);
5316 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005317
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005318 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005319 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005320 return 1;
5321}
5322
5323/* This function set the uri. */
5324static int hlua_http_req_set_uri(lua_State *L)
5325{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005326 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005327 size_t name_len;
5328 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005329
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005330 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005331 WILL_LJMP(lua_error(L));
5332
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005333 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005334 return 1;
5335}
5336
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005337/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005338static int hlua_http_res_set_status(lua_State *L)
5339{
5340 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5341 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005342 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5343 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005344
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005345 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005346 WILL_LJMP(lua_error(L));
5347
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005348 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005349 return 0;
5350}
5351
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005352/*
5353 *
5354 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005355 * Class TXN
5356 *
5357 *
5358 */
5359
5360/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005361 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005362 */
5363__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5364{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005365 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005366}
5367
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005368__LJMP static int hlua_set_var(lua_State *L)
5369{
5370 struct hlua_txn *htxn;
5371 const char *name;
5372 size_t len;
5373 struct sample smp;
5374
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005375 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5376 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005377
5378 /* It is useles to retrieve the stream, but this function
5379 * runs only in a stream context.
5380 */
5381 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5382 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5383
5384 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005385 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005386 hlua_lua2smp(L, 3, &smp);
5387
5388 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005389 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005390
5391 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5392 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5393 else
5394 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5395
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005396 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005397}
5398
Christopher Faulet85d79c92016-11-09 16:54:56 +01005399__LJMP static int hlua_unset_var(lua_State *L)
5400{
5401 struct hlua_txn *htxn;
5402 const char *name;
5403 size_t len;
5404 struct sample smp;
5405
5406 MAY_LJMP(check_args(L, 2, "unset_var"));
5407
5408 /* It is useles to retrieve the stream, but this function
5409 * runs only in a stream context.
5410 */
5411 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5412 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5413
5414 /* Unset the variable. */
5415 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005416 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5417 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005418}
5419
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005420__LJMP static int hlua_get_var(lua_State *L)
5421{
5422 struct hlua_txn *htxn;
5423 const char *name;
5424 size_t len;
5425 struct sample smp;
5426
5427 MAY_LJMP(check_args(L, 2, "get_var"));
5428
5429 /* It is useles to retrieve the stream, but this function
5430 * runs only in a stream context.
5431 */
5432 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5433 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5434
Willy Tarreau7560dd42016-03-10 16:28:58 +01005435 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005436 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005437 lua_pushnil(L);
5438 return 1;
5439 }
5440
5441 return hlua_smp2lua(L, &smp);
5442}
5443
Willy Tarreau59551662015-03-10 14:23:13 +01005444__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005445{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005446 struct hlua *hlua;
5447
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005448 MAY_LJMP(check_args(L, 2, "set_priv"));
5449
Willy Tarreau87b09662015-04-03 00:22:06 +02005450 /* It is useles to retrieve the stream, but this function
5451 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005452 */
5453 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005454
5455 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005456 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005457 if (!hlua)
5458 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005459
5460 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005461 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005462
5463 /* Get and store new value. */
5464 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5465 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5466
5467 return 0;
5468}
5469
Willy Tarreau59551662015-03-10 14:23:13 +01005470__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005471{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005472 struct hlua *hlua;
5473
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005474 MAY_LJMP(check_args(L, 1, "get_priv"));
5475
Willy Tarreau87b09662015-04-03 00:22:06 +02005476 /* It is useles to retrieve the stream, but this function
5477 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005478 */
5479 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005480
5481 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005482 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005483 if (!hlua) {
5484 lua_pushnil(L);
5485 return 1;
5486 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005487
5488 /* Push configuration index in the stack. */
5489 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5490
5491 return 1;
5492}
5493
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005494/* Create stack entry containing a class TXN. This function
5495 * return 0 if the stack does not contains free slots,
5496 * otherwise it returns 1.
5497 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005498static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005499{
Willy Tarreaude491382015-04-06 11:04:28 +02005500 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005501
5502 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005503 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005504 return 0;
5505
5506 /* NOTE: The allocation never fails. The failure
5507 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005508 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005509 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005510 /* Create the object: obj[0] = userdata. */
5511 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005512 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005513 lua_rawseti(L, -2, 0);
5514
Willy Tarreaude491382015-04-06 11:04:28 +02005515 htxn->s = s;
5516 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005517 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005518 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005519
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005520 /* Create the "f" field that contains a list of fetches. */
5521 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005522 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005523 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005524 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005525
5526 /* Create the "sf" field that contains a list of stringsafe fetches. */
5527 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005528 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005529 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005530 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005531
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005532 /* Create the "c" field that contains a list of converters. */
5533 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005534 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005535 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005536 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005537
5538 /* Create the "sc" field that contains a list of stringsafe converters. */
5539 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005540 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005541 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005542 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005543
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005544 /* Create the "req" field that contains the request channel object. */
5545 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005546 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005547 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005548 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005549
5550 /* Create the "res" field that contains the response channel object. */
5551 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005552 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005553 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005554 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005555
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005556 /* Creates the HTTP object is the current proxy allows http. */
5557 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005558 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005559 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005560 return 0;
5561 }
5562 else
5563 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005564 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005565
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005566 /* Pop a class sesison metatable and affect it to the userdata. */
5567 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5568 lua_setmetatable(L, -2);
5569
5570 return 1;
5571}
5572
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005573__LJMP static int hlua_txn_deflog(lua_State *L)
5574{
5575 const char *msg;
5576 struct hlua_txn *htxn;
5577
5578 MAY_LJMP(check_args(L, 2, "deflog"));
5579 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5580 msg = MAY_LJMP(luaL_checkstring(L, 2));
5581
5582 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5583 return 0;
5584}
5585
5586__LJMP static int hlua_txn_log(lua_State *L)
5587{
5588 int level;
5589 const char *msg;
5590 struct hlua_txn *htxn;
5591
5592 MAY_LJMP(check_args(L, 3, "log"));
5593 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5594 level = MAY_LJMP(luaL_checkinteger(L, 2));
5595 msg = MAY_LJMP(luaL_checkstring(L, 3));
5596
5597 if (level < 0 || level >= NB_LOG_LEVELS)
5598 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5599
5600 hlua_sendlog(htxn->s->be, level, msg);
5601 return 0;
5602}
5603
5604__LJMP static int hlua_txn_log_debug(lua_State *L)
5605{
5606 const char *msg;
5607 struct hlua_txn *htxn;
5608
5609 MAY_LJMP(check_args(L, 2, "Debug"));
5610 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5611 msg = MAY_LJMP(luaL_checkstring(L, 2));
5612 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5613 return 0;
5614}
5615
5616__LJMP static int hlua_txn_log_info(lua_State *L)
5617{
5618 const char *msg;
5619 struct hlua_txn *htxn;
5620
5621 MAY_LJMP(check_args(L, 2, "Info"));
5622 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5623 msg = MAY_LJMP(luaL_checkstring(L, 2));
5624 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5625 return 0;
5626}
5627
5628__LJMP static int hlua_txn_log_warning(lua_State *L)
5629{
5630 const char *msg;
5631 struct hlua_txn *htxn;
5632
5633 MAY_LJMP(check_args(L, 2, "Warning"));
5634 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5635 msg = MAY_LJMP(luaL_checkstring(L, 2));
5636 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5637 return 0;
5638}
5639
5640__LJMP static int hlua_txn_log_alert(lua_State *L)
5641{
5642 const char *msg;
5643 struct hlua_txn *htxn;
5644
5645 MAY_LJMP(check_args(L, 2, "Alert"));
5646 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5647 msg = MAY_LJMP(luaL_checkstring(L, 2));
5648 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5649 return 0;
5650}
5651
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005652__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5653{
5654 struct hlua_txn *htxn;
5655 int ll;
5656
5657 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5658 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5659 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5660
5661 if (ll < 0 || ll > 7)
5662 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5663
5664 htxn->s->logs.level = ll;
5665 return 0;
5666}
5667
5668__LJMP static int hlua_txn_set_tos(lua_State *L)
5669{
5670 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005671 int tos;
5672
5673 MAY_LJMP(check_args(L, 2, "set_tos"));
5674 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5675 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5676
Willy Tarreau1a18b542018-12-11 16:37:42 +01005677 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005678 return 0;
5679}
5680
5681__LJMP static int hlua_txn_set_mark(lua_State *L)
5682{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005683 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005684 int mark;
5685
5686 MAY_LJMP(check_args(L, 2, "set_mark"));
5687 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5688 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5689
Lukas Tribus579e3e32019-08-11 18:03:45 +02005690 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005691 return 0;
5692}
5693
Patrick Hemmer268a7072018-05-11 12:52:31 -04005694__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5695{
5696 struct hlua_txn *htxn;
5697
5698 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5699 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5700 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5701 return 0;
5702}
5703
5704__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5705{
5706 struct hlua_txn *htxn;
5707
5708 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5709 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5710 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5711 return 0;
5712}
5713
Christopher Faulet700d9e82020-01-31 12:21:52 +01005714/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005715 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005716 * message and terminate the transaction. It returns 1 on success and 0 on
5717 * error. The Reply must be on top of the stack.
5718 */
5719__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5720{
5721 struct htx *htx;
5722 struct htx_sl *sl;
5723 struct h1m h1m;
5724 const char *status, *reason, *body;
5725 size_t status_len, reason_len, body_len;
5726 int ret, code, flags;
5727
5728 code = 200;
5729 status = "200";
5730 status_len = 3;
5731 ret = lua_getfield(L, -1, "status");
5732 if (ret == LUA_TNUMBER) {
5733 code = lua_tointeger(L, -1);
5734 status = lua_tolstring(L, -1, &status_len);
5735 }
5736 lua_pop(L, 1);
5737
5738 reason = http_get_reason(code);
5739 reason_len = strlen(reason);
5740 ret = lua_getfield(L, -1, "reason");
5741 if (ret == LUA_TSTRING)
5742 reason = lua_tolstring(L, -1, &reason_len);
5743 lua_pop(L, 1);
5744
5745 body = NULL;
5746 body_len = 0;
5747 ret = lua_getfield(L, -1, "body");
5748 if (ret == LUA_TSTRING)
5749 body = lua_tolstring(L, -1, &body_len);
5750 lua_pop(L, 1);
5751
5752 /* Prepare the response before inserting the headers */
5753 h1m_init_res(&h1m);
5754 htx = htx_from_buf(&s->res.buf);
5755 channel_htx_truncate(&s->res, htx);
5756 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5757 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5758 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5759 ist2(status, status_len), ist2(reason, reason_len));
5760 }
5761 else {
5762 flags = HTX_SL_F_IS_RESP;
5763 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5764 ist2(status, status_len), ist2(reason, reason_len));
5765 }
5766 if (!sl)
5767 goto fail;
5768 sl->info.res.status = code;
5769
5770 /* Push in the stack the "headers" entry. */
5771 ret = lua_getfield(L, -1, "headers");
5772 if (ret != LUA_TTABLE)
5773 goto skip_headers;
5774
5775 lua_pushnil(L);
5776 while (lua_next(L, -2) != 0) {
5777 struct ist name, value;
5778 const char *n, *v;
5779 size_t nlen, vlen;
5780
5781 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5782 /* Skip element if the key is not a string or if the value is not a table */
5783 goto next_hdr;
5784 }
5785
5786 n = lua_tolstring(L, -2, &nlen);
5787 name = ist2(n, nlen);
5788 if (isteqi(name, ist("content-length"))) {
5789 /* Always skip content-length header. It will be added
5790 * later with the correct len
5791 */
5792 goto next_hdr;
5793 }
5794
5795 /* Loop on header's values */
5796 lua_pushnil(L);
5797 while (lua_next(L, -2)) {
5798 if (!lua_isstring(L, -1)) {
5799 /* Skip the value if it is not a string */
5800 goto next_value;
5801 }
5802
5803 v = lua_tolstring(L, -1, &vlen);
5804 value = ist2(v, vlen);
5805
5806 if (isteqi(name, ist("transfer-encoding")))
5807 h1_parse_xfer_enc_header(&h1m, value);
5808 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5809 goto fail;
5810
5811 next_value:
5812 lua_pop(L, 1);
5813 }
5814
5815 next_hdr:
5816 lua_pop(L, 1);
5817 }
5818 skip_headers:
5819 lua_pop(L, 1);
5820
5821 /* Update h1m flags: CLEN is set if CHNK is not present */
5822 if (!(h1m.flags & H1_MF_CHNK)) {
5823 const char *clen = ultoa(body_len);
5824
5825 h1m.flags |= H1_MF_CLEN;
5826 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5827 goto fail;
5828 }
5829 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5830 h1m.flags |= H1_MF_XFER_LEN;
5831
5832 /* Update HTX start-line flags */
5833 if (h1m.flags & H1_MF_XFER_ENC)
5834 flags |= HTX_SL_F_XFER_ENC;
5835 if (h1m.flags & H1_MF_XFER_LEN) {
5836 flags |= HTX_SL_F_XFER_LEN;
5837 if (h1m.flags & H1_MF_CHNK)
5838 flags |= HTX_SL_F_CHNK;
5839 else if (h1m.flags & H1_MF_CLEN)
5840 flags |= HTX_SL_F_CLEN;
5841 if (h1m.body_len == 0)
5842 flags |= HTX_SL_F_BODYLESS;
5843 }
5844 sl->flags |= flags;
5845
5846
5847 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005848 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005849 goto fail;
5850
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005851 htx->flags |= HTX_FL_EOM;
5852
Christopher Faulet700d9e82020-01-31 12:21:52 +01005853 /* Now, forward the response and terminate the transaction */
5854 s->txn->status = code;
5855 htx_to_buf(htx, &s->res.buf);
5856 if (!http_forward_proxy_resp(s, 1))
5857 goto fail;
5858
5859 return 1;
5860
5861 fail:
5862 channel_htx_truncate(&s->res, htx);
5863 return 0;
5864}
5865
5866/* Terminate a transaction if called from a lua action. For TCP streams,
5867 * processing is just aborted. Nothing is returned to the client and all
5868 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5869 * is forwarded to the client before terminating the transaction. On success,
5870 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5871 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5872 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005873 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005874__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005875{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005876 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005877 struct stream *s;
5878 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005879
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005880 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005881
Christopher Faulet700d9e82020-01-31 12:21:52 +01005882 /* If the flags NOTERM is set, we cannot terminate the session, so we
5883 * just end the execution of the current lua code. */
5884 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005885 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005886
Christopher Faulet700d9e82020-01-31 12:21:52 +01005887 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005888 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005889 struct channel *req = &s->req;
5890 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005891
Christopher Faulet700d9e82020-01-31 12:21:52 +01005892 channel_auto_read(req);
5893 channel_abort(req);
5894 channel_auto_close(req);
5895 channel_erase(req);
5896
5897 res->wex = tick_add_ifset(now_ms, res->wto);
5898 channel_auto_read(res);
5899 channel_auto_close(res);
5900 channel_shutr_now(res);
5901
5902 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5903 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005904 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005905
Christopher Faulet700d9e82020-01-31 12:21:52 +01005906 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5907 /* No reply or invalid reply */
5908 s->txn->status = 0;
5909 http_reply_and_close(s, 0, NULL);
5910 }
5911 else {
5912 /* Remove extra args to have the reply on top of the stack */
5913 if (lua_gettop(L) > 2)
5914 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005915
Christopher Faulet700d9e82020-01-31 12:21:52 +01005916 if (!hlua_txn_forward_reply(L, s)) {
5917 if (!(s->flags & SF_ERR_MASK))
5918 s->flags |= SF_ERR_PRXCOND;
5919 lua_pushinteger(L, ACT_RET_ERR);
5920 WILL_LJMP(hlua_done(L));
5921 return 0; /* Never reached */
5922 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005923 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005924
Christopher Faulet700d9e82020-01-31 12:21:52 +01005925 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5926 if (htxn->dir == SMP_OPT_DIR_REQ) {
5927 /* let's log the request time */
5928 s->logs.tv_request = now;
5929 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02005930 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01005931 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005932
Christopher Faulet700d9e82020-01-31 12:21:52 +01005933 done:
5934 if (!(s->flags & SF_ERR_MASK))
5935 s->flags |= SF_ERR_LOCAL;
5936 if (!(s->flags & SF_FINST_MASK))
5937 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005938
Christopher Faulet4ad73102020-03-05 11:07:31 +01005939 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005940 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005941 return 0;
5942}
5943
Christopher Faulet700d9e82020-01-31 12:21:52 +01005944/*
5945 *
5946 *
5947 * Class REPLY
5948 *
5949 *
5950 */
5951
5952/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5953 * free slots, the function fails and returns 0;
5954 */
5955static int hlua_txn_reply_new(lua_State *L)
5956{
5957 struct hlua_txn *htxn;
5958 const char *reason, *body = NULL;
5959 int ret, status;
5960
5961 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005962 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005963 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5964 WILL_LJMP(lua_error(L));
5965 }
5966
5967 /* Default value */
5968 status = 200;
5969 reason = http_get_reason(status);
5970
5971 if (lua_istable(L, 2)) {
5972 /* load status and reason from the table argument at index 2 */
5973 ret = lua_getfield(L, 2, "status");
5974 if (ret == LUA_TNIL)
5975 goto reason;
5976 else if (ret != LUA_TNUMBER) {
5977 /* invalid status: ignore the reason */
5978 goto body;
5979 }
5980 status = lua_tointeger(L, -1);
5981
5982 reason:
5983 lua_pop(L, 1); /* restore the stack: remove status */
5984 ret = lua_getfield(L, 2, "reason");
5985 if (ret == LUA_TSTRING)
5986 reason = lua_tostring(L, -1);
5987
5988 body:
5989 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5990 ret = lua_getfield(L, 2, "body");
5991 if (ret == LUA_TSTRING)
5992 body = lua_tostring(L, -1);
5993 lua_pop(L, 1); /* restore the stack: remove body */
5994 }
5995
5996 /* Create the Reply table */
5997 lua_newtable(L);
5998
5999 /* Add status element */
6000 lua_pushstring(L, "status");
6001 lua_pushinteger(L, status);
6002 lua_settable(L, -3);
6003
6004 /* Add reason element */
6005 reason = http_get_reason(status);
6006 lua_pushstring(L, "reason");
6007 lua_pushstring(L, reason);
6008 lua_settable(L, -3);
6009
6010 /* Add body element, nil if undefined */
6011 lua_pushstring(L, "body");
6012 if (body)
6013 lua_pushstring(L, body);
6014 else
6015 lua_pushnil(L);
6016 lua_settable(L, -3);
6017
6018 /* Add headers element */
6019 lua_pushstring(L, "headers");
6020 lua_newtable(L);
6021
6022 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6023 if (lua_istable(L, 2)) {
6024 /* load headers from the table argument at index 2. If it is a table, copy it. */
6025 ret = lua_getfield(L, 2, "headers");
6026 if (ret == LUA_TTABLE) {
6027 /* stack: [ ... <headers:table>, <table> ] */
6028 lua_pushnil(L);
6029 while (lua_next(L, -2) != 0) {
6030 /* stack: [ ... <headers:table>, <table>, k, v] */
6031 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
6032 /* invalid value type, skip it */
6033 lua_pop(L, 1);
6034 continue;
6035 }
6036
6037
6038 /* Duplicate the key and swap it with the value. */
6039 lua_pushvalue(L, -2);
6040 lua_insert(L, -2);
6041 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6042
6043 lua_newtable(L);
6044 lua_insert(L, -2);
6045 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6046
6047 if (lua_isstring(L, -1)) {
6048 /* push the value in the inner table */
6049 lua_rawseti(L, -2, 1);
6050 }
6051 else { /* table */
6052 lua_pushnil(L);
6053 while (lua_next(L, -2) != 0) {
6054 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6055 if (!lua_isstring(L, -1)) {
6056 /* invalid value type, skip it*/
6057 lua_pop(L, 1);
6058 continue;
6059 }
6060 /* push the value in the inner table */
6061 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6062 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6063 }
6064 lua_pop(L, 1);
6065 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6066 }
6067
6068 /* push (k,v) on the stack in the headers table:
6069 * stack: [ ... <headers:table>, <table>, k, k, v ]
6070 */
6071 lua_settable(L, -5);
6072 /* stack: [ ... <headers:table>, <table>, k ] */
6073 }
6074 }
6075 lua_pop(L, 1);
6076 }
6077 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6078 lua_settable(L, -3);
6079 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6080
6081 /* Pop a class sesison metatable and affect it to the userdata. */
6082 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6083 lua_setmetatable(L, -2);
6084 return 1;
6085}
6086
6087/* Set the reply status code, and optionally the reason. If no reason is
6088 * provided, the default one corresponding to the status code is used.
6089 */
6090__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6091{
6092 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6093 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6094
6095 /* First argument (self) must be a table */
6096 luaL_checktype(L, 1, LUA_TTABLE);
6097
6098 if (status < 100 || status > 599) {
6099 lua_pushboolean(L, 0);
6100 return 1;
6101 }
6102 if (!reason)
6103 reason = http_get_reason(status);
6104
6105 lua_pushinteger(L, status);
6106 lua_setfield(L, 1, "status");
6107
6108 lua_pushstring(L, reason);
6109 lua_setfield(L, 1, "reason");
6110
6111 lua_pushboolean(L, 1);
6112 return 1;
6113}
6114
6115/* Add a header into the reply object. Each header name is associated to an
6116 * array of values in the "headers" table. If the header name is not found, a
6117 * new entry is created.
6118 */
6119__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6120{
6121 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6122 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6123 int ret;
6124
6125 /* First argument (self) must be a table */
6126 luaL_checktype(L, 1, LUA_TTABLE);
6127
6128 /* Push in the stack the "headers" entry. */
6129 ret = lua_getfield(L, 1, "headers");
6130 if (ret != LUA_TTABLE) {
6131 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6132 WILL_LJMP(lua_error(L));
6133 }
6134
6135 /* check if the header is already registered. If not, register it. */
6136 ret = lua_getfield(L, -1, name);
6137 if (ret == LUA_TNIL) {
6138 /* Entry not found. */
6139 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6140
6141 /* Insert the new header name in the array in the top of the stack.
6142 * It left the new array in the top of the stack.
6143 */
6144 lua_newtable(L);
6145 lua_pushstring(L, name);
6146 lua_pushvalue(L, -2);
6147 lua_settable(L, -4);
6148 }
6149 else if (ret != LUA_TTABLE) {
6150 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6151 WILL_LJMP(lua_error(L));
6152 }
6153
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006154 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006155 * the header value as new entry.
6156 */
6157 lua_pushstring(L, value);
6158 ret = lua_rawlen(L, -2);
6159 lua_rawseti(L, -2, ret + 1);
6160
6161 lua_pushboolean(L, 1);
6162 return 1;
6163}
6164
6165/* Remove all occurrences of a given header name. */
6166__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6167{
6168 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6169 int ret;
6170
6171 /* First argument (self) must be a table */
6172 luaL_checktype(L, 1, LUA_TTABLE);
6173
6174 /* Push in the stack the "headers" entry. */
6175 ret = lua_getfield(L, 1, "headers");
6176 if (ret != LUA_TTABLE) {
6177 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6178 WILL_LJMP(lua_error(L));
6179 }
6180
6181 lua_pushstring(L, name);
6182 lua_pushnil(L);
6183 lua_settable(L, -3);
6184
6185 lua_pushboolean(L, 1);
6186 return 1;
6187}
6188
6189/* Set the reply's body. Overwrite any existing entry. */
6190__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6191{
6192 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6193
6194 /* First argument (self) must be a table */
6195 luaL_checktype(L, 1, LUA_TTABLE);
6196
6197 lua_pushstring(L, payload);
6198 lua_setfield(L, 1, "body");
6199
6200 lua_pushboolean(L, 1);
6201 return 1;
6202}
6203
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006204__LJMP static int hlua_log(lua_State *L)
6205{
6206 int level;
6207 const char *msg;
6208
6209 MAY_LJMP(check_args(L, 2, "log"));
6210 level = MAY_LJMP(luaL_checkinteger(L, 1));
6211 msg = MAY_LJMP(luaL_checkstring(L, 2));
6212
6213 if (level < 0 || level >= NB_LOG_LEVELS)
6214 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6215
6216 hlua_sendlog(NULL, level, msg);
6217 return 0;
6218}
6219
6220__LJMP static int hlua_log_debug(lua_State *L)
6221{
6222 const char *msg;
6223
6224 MAY_LJMP(check_args(L, 1, "debug"));
6225 msg = MAY_LJMP(luaL_checkstring(L, 1));
6226 hlua_sendlog(NULL, LOG_DEBUG, msg);
6227 return 0;
6228}
6229
6230__LJMP static int hlua_log_info(lua_State *L)
6231{
6232 const char *msg;
6233
6234 MAY_LJMP(check_args(L, 1, "info"));
6235 msg = MAY_LJMP(luaL_checkstring(L, 1));
6236 hlua_sendlog(NULL, LOG_INFO, msg);
6237 return 0;
6238}
6239
6240__LJMP static int hlua_log_warning(lua_State *L)
6241{
6242 const char *msg;
6243
6244 MAY_LJMP(check_args(L, 1, "warning"));
6245 msg = MAY_LJMP(luaL_checkstring(L, 1));
6246 hlua_sendlog(NULL, LOG_WARNING, msg);
6247 return 0;
6248}
6249
6250__LJMP static int hlua_log_alert(lua_State *L)
6251{
6252 const char *msg;
6253
6254 MAY_LJMP(check_args(L, 1, "alert"));
6255 msg = MAY_LJMP(luaL_checkstring(L, 1));
6256 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006257 return 0;
6258}
6259
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006260__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006261{
6262 int wakeup_ms = lua_tointeger(L, -1);
6263 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006264 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006265 return 0;
6266}
6267
6268__LJMP static int hlua_sleep(lua_State *L)
6269{
6270 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006271 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006272
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006273 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006274
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006275 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006276 wakeup_ms = tick_add(now_ms, delay);
6277 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006278
Willy Tarreau9635e032018-10-16 17:52:55 +02006279 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006280 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006281}
6282
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006283__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006284{
6285 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006286 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006287
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006288 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006289
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006290 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006291 wakeup_ms = tick_add(now_ms, delay);
6292 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006293
Willy Tarreau9635e032018-10-16 17:52:55 +02006294 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006295 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006296}
6297
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006298/* This functionis an LUA binding. it permits to give back
6299 * the hand at the HAProxy scheduler. It is used when the
6300 * LUA processing consumes a lot of time.
6301 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006302__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006303{
6304 return 0;
6305}
6306
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006307__LJMP static int hlua_yield(lua_State *L)
6308{
Willy Tarreau9635e032018-10-16 17:52:55 +02006309 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006310 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006311}
6312
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006313/* This function change the nice of the currently executed
6314 * task. It is used set low or high priority at the current
6315 * task.
6316 */
Willy Tarreau59551662015-03-10 14:23:13 +01006317__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006318{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006319 struct hlua *hlua;
6320 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006321
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006322 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006323 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006324
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006325 /* Get hlua struct, or NULL if we execute from main lua state */
6326 hlua = hlua_gethlua(L);
6327
6328 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006329 if (!hlua || !hlua->task)
6330 return 0;
6331
6332 if (nice < -1024)
6333 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006334 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006335 nice = 1024;
6336
6337 hlua->task->nice = nice;
6338 return 0;
6339}
6340
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006341/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006342 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6343 * return an E_AGAIN signal, the emmiter of this signal must set a
6344 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006345 *
6346 * Task wrapper are longjmp safe because the only one Lua code
6347 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006348 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006349struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006350{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006351 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006352 enum hlua_exec status;
6353
Christopher Faulet5bc99722018-04-25 10:34:45 +02006354 if (task->thread_mask == MAX_THREADS_MASK)
6355 task_set_affinity(task, tid_bit);
6356
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006357 /* If it is the first call to the task, we must initialize the
6358 * execution timeouts.
6359 */
6360 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006361 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006362
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006363 /* Execute the Lua code. */
6364 status = hlua_ctx_resume(hlua, 1);
6365
6366 switch (status) {
6367 /* finished or yield */
6368 case HLUA_E_OK:
6369 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006370 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006371 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006372 break;
6373
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006374 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006375 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006376 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006377 break;
6378
6379 /* finished with error. */
6380 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006381 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006382 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006383 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006384 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006385 break;
6386
6387 case HLUA_E_ERR:
6388 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006389 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006390 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006391 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006392 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006393 break;
6394 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006395 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006396}
6397
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006398/* This function is an LUA binding that register LUA function to be
6399 * executed after the HAProxy configuration parsing and before the
6400 * HAProxy scheduler starts. This function expect only one LUA
6401 * argument that is a function. This function returns nothing, but
6402 * throws if an error is encountered.
6403 */
6404__LJMP static int hlua_register_init(lua_State *L)
6405{
6406 struct hlua_init_function *init;
6407 int ref;
6408
6409 MAY_LJMP(check_args(L, 1, "register_init"));
6410
6411 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6412
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006413 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006414 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006415 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006416
6417 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006418 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006419 return 0;
6420}
6421
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006422/* This functio is an LUA binding. It permits to register a task
6423 * executed in parallel of the main HAroxy activity. The task is
6424 * created and it is set in the HAProxy scheduler. It can be called
6425 * from the "init" section, "post init" or during the runtime.
6426 *
6427 * Lua prototype:
6428 *
6429 * <none> core.register_task(<function>)
6430 */
6431static int hlua_register_task(lua_State *L)
6432{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006433 struct hlua *hlua = NULL;
6434 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006435 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006436 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006437
6438 MAY_LJMP(check_args(L, 1, "register_task"));
6439
6440 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6441
Thierry Fournier75fc0292020-11-28 13:18:56 +01006442 /* Get the reference state. If the reference is NULL, L is the master
6443 * state, otherwise hlua->T is.
6444 */
6445 hlua = hlua_gethlua(L);
6446 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006447 /* we are in runtime processing */
6448 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006449 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006450 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006451 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006452
Willy Tarreaubafbe012017-11-24 17:34:44 +01006453 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006454 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006455 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006456 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006457
Thierry Fournier59f11be2020-11-29 00:37:41 +01006458 /* We are in the common lua state, execute the task anywhere,
6459 * otherwise, inherit the current thread identifier
6460 */
6461 if (state_id == 0)
6462 task = task_new(MAX_THREADS_MASK);
6463 else
6464 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006465 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006466 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006467
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006468 task->context = hlua;
6469 task->process = hlua_process_task;
6470
Thierry Fournier021d9862020-11-28 23:42:03 +01006471 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006472 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006473
6474 /* Restore the function in the stack. */
6475 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6476 hlua->nargs = 0;
6477
6478 /* Schedule task. */
6479 task_schedule(task, now_ms);
6480
6481 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006482
6483 alloc_error:
6484 task_destroy(task);
6485 hlua_ctx_destroy(hlua);
6486 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6487 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006488}
6489
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006490/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6491 * doesn't allow "yield" functions because the HAProxy engine cannot
6492 * resume converters.
6493 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006494static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006495{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006496 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006497 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006498 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006499
Willy Tarreaube508f12016-03-10 11:47:01 +01006500 if (!stream)
6501 return 0;
6502
Willy Tarreau87b09662015-04-03 00:22:06 +02006503 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006504 * Lua context can be not initialized. This behavior
6505 * permits to save performances because a systematic
6506 * Lua initialization cause 5% performances loss.
6507 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006508 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006509 struct hlua *hlua;
6510
6511 hlua = pool_alloc(pool_head_hlua);
6512 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006513 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6514 return 0;
6515 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006516 HLUA_INIT(hlua);
6517 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006518 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006519 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6520 return 0;
6521 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006522 }
6523
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006524 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006525 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006526
6527 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006528 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006529 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6530 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006531 else
6532 error = "critical error";
6533 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006534 return 0;
6535 }
6536
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006537 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006538 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006539 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006540 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006541 return 0;
6542 }
6543
6544 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006545 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006546
6547 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006548 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006549 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006550 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006551 return 0;
6552 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006553 hlua_smp2lua(stream->hlua->T, smp);
6554 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006555
6556 /* push keywords in the stack. */
6557 if (arg_p) {
6558 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006559 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006560 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006561 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006562 return 0;
6563 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006564 hlua_arg2lua(stream->hlua->T, arg_p);
6565 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006566 }
6567 }
6568
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006569 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006570 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006571
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006572 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006573 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006574 }
6575
6576 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006577 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006578 /* finished. */
6579 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006580 /* If the stack is empty, the function fails. */
6581 if (lua_gettop(stream->hlua->T) <= 0)
6582 return 0;
6583
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006584 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006585 hlua_lua2smp(stream->hlua->T, -1, smp);
6586 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006587 return 1;
6588
6589 /* yield. */
6590 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006591 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006592 return 0;
6593
6594 /* finished with error. */
6595 case HLUA_E_ERRMSG:
6596 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006597 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006598 fcn->name, lua_tostring(stream->hlua->T, -1));
6599 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006600 return 0;
6601
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006602 case HLUA_E_ETMOUT:
6603 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6604 return 0;
6605
6606 case HLUA_E_NOMEM:
6607 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6608 return 0;
6609
6610 case HLUA_E_YIELD:
6611 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6612 return 0;
6613
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006614 case HLUA_E_ERR:
6615 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006616 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006617 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006618
6619 default:
6620 return 0;
6621 }
6622}
6623
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006624/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6625 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006626 * resume sample-fetches. This function will be called by the sample
6627 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006628 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006629static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6630 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006631{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006632 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006633 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006634 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006635 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006636
Willy Tarreaube508f12016-03-10 11:47:01 +01006637 if (!stream)
6638 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006639
Willy Tarreau87b09662015-04-03 00:22:06 +02006640 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006641 * Lua context can be not initialized. This behavior
6642 * permits to save performances because a systematic
6643 * Lua initialization cause 5% performances loss.
6644 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006645 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006646 struct hlua *hlua;
6647
6648 hlua = pool_alloc(pool_head_hlua);
6649 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006650 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6651 return 0;
6652 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006653 hlua->T = NULL;
6654 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006655 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006656 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6657 return 0;
6658 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006659 }
6660
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006661 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006662 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006663
6664 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006665 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006666 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6667 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006668 else
6669 error = "critical error";
6670 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006671 return 0;
6672 }
6673
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006674 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006675 if (!lua_checkstack(stream->hlua->T, 2)) {
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 }
6680
6681 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006682 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006683
6684 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006685 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006686 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006687 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006688 return 0;
6689 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006690 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006691
6692 /* push keywords in the stack. */
6693 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6694 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006695 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006696 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006697 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006698 return 0;
6699 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006700 hlua_arg2lua(stream->hlua->T, arg_p);
6701 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006702 }
6703
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006704 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006705 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006706
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006707 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006708 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006709 }
6710
6711 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006712 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006713 /* finished. */
6714 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006715 /* If the stack is empty, the function fails. */
6716 if (lua_gettop(stream->hlua->T) <= 0)
6717 return 0;
6718
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006719 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006720 hlua_lua2smp(stream->hlua->T, -1, smp);
6721 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006722
6723 /* Set the end of execution flag. */
6724 smp->flags &= ~SMP_F_MAY_CHANGE;
6725 return 1;
6726
6727 /* yield. */
6728 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006729 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006730 return 0;
6731
6732 /* finished with error. */
6733 case HLUA_E_ERRMSG:
6734 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006735 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006736 fcn->name, lua_tostring(stream->hlua->T, -1));
6737 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006738 return 0;
6739
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006740 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006741 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6742 return 0;
6743
6744 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006745 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6746 return 0;
6747
6748 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006749 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6750 return 0;
6751
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006752 case HLUA_E_ERR:
6753 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006754 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006755 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006756
6757 default:
6758 return 0;
6759 }
6760}
6761
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006762/* This function is an LUA binding used for registering
6763 * "sample-conv" functions. It expects a converter name used
6764 * in the haproxy configuration file, and an LUA function.
6765 */
6766__LJMP static int hlua_register_converters(lua_State *L)
6767{
6768 struct sample_conv_kw_list *sck;
6769 const char *name;
6770 int ref;
6771 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02006772 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006773 struct sample_conv *sc;
6774 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006775
6776 MAY_LJMP(check_args(L, 2, "register_converters"));
6777
6778 /* First argument : converter name. */
6779 name = MAY_LJMP(luaL_checkstring(L, 1));
6780
6781 /* Second argument : lua function. */
6782 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6783
Thierry Fournierf67442e2020-11-28 20:41:07 +01006784 /* Check if the converter is already registered */
6785 trash = get_trash_chunk();
6786 chunk_printf(trash, "lua.%s", name);
6787 sc = find_sample_conv(trash->area, trash->data);
6788 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006789 fcn = sc->private;
6790 if (fcn->function_ref[hlua_state_id] != -1) {
6791 ha_warning("Trying to register converter 'lua.%s' more than once. "
6792 "This will become a hard error in version 2.5.\n", name);
6793 }
6794 fcn->function_ref[hlua_state_id] = ref;
6795 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006796 }
6797
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006798 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006799 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006800 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02006801 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006802 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006803 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02006804 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006805
6806 /* Fill fcn. */
6807 fcn->name = strdup(name);
6808 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02006809 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006810 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006811
6812 /* List head */
6813 sck->list.n = sck->list.p = NULL;
6814
6815 /* converter keyword. */
6816 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006817 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006818 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02006819 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006820
6821 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6822 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006823 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 +01006824 sck->kw[0].val_args = NULL;
6825 sck->kw[0].in_type = SMP_T_STR;
6826 sck->kw[0].out_type = SMP_T_STR;
6827 sck->kw[0].private = fcn;
6828
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006829 /* Register this new converter */
6830 sample_register_convs(sck);
6831
6832 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02006833
6834 alloc_error:
6835 release_hlua_function(fcn);
6836 ha_free(&sck);
6837 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6838 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006839}
6840
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006841/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006842 * "sample-fetch" functions. It expects a converter name used
6843 * in the haproxy configuration file, and an LUA function.
6844 */
6845__LJMP static int hlua_register_fetches(lua_State *L)
6846{
6847 const char *name;
6848 int ref;
6849 int len;
6850 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02006851 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006852 struct sample_fetch *sf;
6853 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006854
6855 MAY_LJMP(check_args(L, 2, "register_fetches"));
6856
6857 /* First argument : sample-fetch name. */
6858 name = MAY_LJMP(luaL_checkstring(L, 1));
6859
6860 /* Second argument : lua function. */
6861 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6862
Thierry Fournierf67442e2020-11-28 20:41:07 +01006863 /* Check if the sample-fetch is already registered */
6864 trash = get_trash_chunk();
6865 chunk_printf(trash, "lua.%s", name);
6866 sf = find_sample_fetch(trash->area, trash->data);
6867 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006868 fcn = sf->private;
6869 if (fcn->function_ref[hlua_state_id] != -1) {
6870 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6871 "This will become a hard error in version 2.5.\n", name);
6872 }
6873 fcn->function_ref[hlua_state_id] = ref;
6874 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006875 }
6876
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006877 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006878 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006879 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02006880 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006881 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006882 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02006883 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006884
6885 /* Fill fcn. */
6886 fcn->name = strdup(name);
6887 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02006888 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006889 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006890
6891 /* List head */
6892 sfk->list.n = sfk->list.p = NULL;
6893
6894 /* sample-fetch keyword. */
6895 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006896 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006897 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02006898 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006899
6900 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6901 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006902 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 +01006903 sfk->kw[0].val_args = NULL;
6904 sfk->kw[0].out_type = SMP_T_STR;
6905 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6906 sfk->kw[0].val = 0;
6907 sfk->kw[0].private = fcn;
6908
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006909 /* Register this new fetch. */
6910 sample_register_fetches(sfk);
6911
6912 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02006913
6914 alloc_error:
6915 release_hlua_function(fcn);
6916 ha_free(&sfk);
6917 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6918 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006919}
6920
Christopher Faulet501465d2020-02-26 14:54:16 +01006921/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006922 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006923__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006924{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006925 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006926 unsigned int delay;
6927 unsigned int wakeup_ms;
6928
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006929 /* Get hlua struct, or NULL if we execute from main lua state */
6930 hlua = hlua_gethlua(L);
6931 if (!hlua) {
6932 return 0;
6933 }
6934
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006935 MAY_LJMP(check_args(L, 1, "wake_time"));
6936
6937 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6938 wakeup_ms = tick_add(now_ms, delay);
6939 hlua->wake_time = wakeup_ms;
6940 return 0;
6941}
6942
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006943/* This function is a wrapper to execute each LUA function declared as an action
6944 * wrapper during the initialisation period. This function may return any
6945 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6946 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6947 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006948 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006949static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006950 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006951{
6952 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006953 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006954 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006955 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006956
6957 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006958 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6959 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6960 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6961 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006962 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006963 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006964 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006965 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006966
Willy Tarreau87b09662015-04-03 00:22:06 +02006967 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006968 * Lua context can be not initialized. This behavior
6969 * permits to save performances because a systematic
6970 * Lua initialization cause 5% performances loss.
6971 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006972 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006973 struct hlua *hlua;
6974
6975 hlua = pool_alloc(pool_head_hlua);
6976 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006977 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006978 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006979 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006980 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006981 HLUA_INIT(hlua);
6982 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006983 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 +01006984 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006985 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006986 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006987 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006988 }
6989
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006990 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006991 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006992
6993 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006994 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006995 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6996 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006997 else
6998 error = "critical error";
6999 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007000 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007001 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007002 }
7003
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007004 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007005 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007006 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007007 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007008 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007009 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007010 }
7011
7012 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007013 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 +01007014
Willy Tarreau87b09662015-04-03 00:22:06 +02007015 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007016 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007017 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007018 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007019 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007020 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007021 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007022 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007023
7024 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007025 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007026 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007027 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007028 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007029 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007030 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007031 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007032 lua_pushstring(s->hlua->T, *arg);
7033 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007034 }
7035
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007036 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007037 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007038
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007039 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007040 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007041 }
7042
7043 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01007044 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007045 /* finished. */
7046 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007047 /* Catch the return value */
7048 if (lua_gettop(s->hlua->T) > 0)
7049 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007050
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007051 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007052 if (act_ret == ACT_RET_YIELD) {
7053 if (flags & ACT_OPT_FINAL)
7054 goto err_yield;
7055
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007056 if (dir == SMP_OPT_DIR_REQ)
7057 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7058 s->hlua->wake_time);
7059 else
7060 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7061 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007062 }
7063 goto end;
7064
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007065 /* yield. */
7066 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007067 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007068 if (dir == SMP_OPT_DIR_REQ)
7069 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7070 s->hlua->wake_time);
7071 else
7072 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7073 s->hlua->wake_time);
7074
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007075 /* Some actions can be wake up when a "write" event
7076 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007077 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007078 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007079 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007080 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007081 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007082 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007083 act_ret = ACT_RET_YIELD;
7084 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007085
7086 /* finished with error. */
7087 case HLUA_E_ERRMSG:
7088 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007089 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007090 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007091 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007092 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007093
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007094 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007095 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007096 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007097
7098 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007099 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007100 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007101
7102 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007103 err_yield:
7104 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007105 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007106 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007107 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007108
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007109 case HLUA_E_ERR:
7110 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007111 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007112 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007113
7114 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007115 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007116 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007117
7118 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007119 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007120 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007121 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007122}
7123
Willy Tarreau144f84a2021-03-02 16:09:26 +01007124struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007125{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007126 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007127
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007128 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007129 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007130 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007131}
7132
7133static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7134{
7135 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007136 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007137 struct task *task;
7138 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007139 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007140
Willy Tarreaubafbe012017-11-24 17:34:44 +01007141 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007142 if (!hlua) {
7143 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007144 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007145 return 0;
7146 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007147 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007148 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007149 ctx->ctx.hlua_apptcp.flags = 0;
7150
7151 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007152 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007153 if (!task) {
7154 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007155 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007156 return 0;
7157 }
7158 task->nice = 0;
7159 task->context = ctx;
7160 task->process = hlua_applet_wakeup;
7161 ctx->ctx.hlua_apptcp.task = task;
7162
7163 /* In the execution wrappers linked with a stream, the
7164 * Lua context can be not initialized. This behavior
7165 * permits to save performances because a systematic
7166 * Lua initialization cause 5% performances loss.
7167 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007168 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 +02007169 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007170 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007171 return 0;
7172 }
7173
7174 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007175 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007176
7177 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007178 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007179 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7180 error = lua_tostring(hlua->T, -1);
7181 else
7182 error = "critical error";
7183 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007184 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007185 return 0;
7186 }
7187
7188 /* Check stack available size. */
7189 if (!lua_checkstack(hlua->T, 1)) {
7190 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007191 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007192 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007193 return 0;
7194 }
7195
7196 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007197 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007198
7199 /* Create and and push object stream in the stack. */
7200 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7201 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007202 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007203 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007204 return 0;
7205 }
7206 hlua->nargs = 1;
7207
7208 /* push keywords in the stack. */
7209 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7210 if (!lua_checkstack(hlua->T, 1)) {
7211 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007212 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007213 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007214 return 0;
7215 }
7216 lua_pushstring(hlua->T, *arg);
7217 hlua->nargs++;
7218 }
7219
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007220 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007221
7222 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007223 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007224 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007225
7226 return 1;
7227}
7228
Willy Tarreau60409db2019-08-21 14:14:50 +02007229void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007230{
7231 struct stream_interface *si = ctx->owner;
7232 struct stream *strm = si_strm(si);
7233 struct channel *res = si_ic(si);
7234 struct act_rule *rule = ctx->rule;
7235 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007236 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007237
7238 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007239 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7240 /* eat the whole request */
7241 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007242 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007243 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007244
7245 /* If the stream is disconnect or closed, ldo nothing. */
7246 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7247 return;
7248
7249 /* Execute the function. */
7250 switch (hlua_ctx_resume(hlua, 1)) {
7251 /* finished. */
7252 case HLUA_E_OK:
7253 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7254
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007255 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007256 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007257 res->flags |= CF_READ_NULL;
7258 si_shutr(si);
7259 return;
7260
7261 /* yield. */
7262 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007263 if (hlua->wake_time != TICK_ETERNITY)
7264 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007265 return;
7266
7267 /* finished with error. */
7268 case HLUA_E_ERRMSG:
7269 /* Display log. */
7270 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007271 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007272 lua_pop(hlua->T, 1);
7273 goto error;
7274
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007275 case HLUA_E_ETMOUT:
7276 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007277 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007278 goto error;
7279
7280 case HLUA_E_NOMEM:
7281 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007282 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007283 goto error;
7284
7285 case HLUA_E_YIELD: /* unexpected */
7286 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007287 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007288 goto error;
7289
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007290 case HLUA_E_ERR:
7291 /* Display log. */
7292 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007293 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007294 goto error;
7295
7296 default:
7297 goto error;
7298 }
7299
7300error:
7301
7302 /* For all other cases, just close the stream. */
7303 si_shutw(si);
7304 si_shutr(si);
7305 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7306}
7307
7308static void hlua_applet_tcp_release(struct appctx *ctx)
7309{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007310 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007311 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007312 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007313 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007314}
7315
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007316/* The function returns 1 if the initialisation is complete, 0 if
7317 * an errors occurs and -1 if more data are required for initializing
7318 * the applet.
7319 */
7320static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7321{
7322 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007323 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007324 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007325 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007326 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007327 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007328
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007329 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007330 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007331 if (!hlua) {
7332 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007333 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007334 return 0;
7335 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007336 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007337 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007338 ctx->ctx.hlua_apphttp.left_bytes = -1;
7339 ctx->ctx.hlua_apphttp.flags = 0;
7340
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007341 if (txn->req.flags & HTTP_MSGF_VER_11)
7342 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7343
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007344 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007345 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007346 if (!task) {
7347 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007348 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007349 return 0;
7350 }
7351 task->nice = 0;
7352 task->context = ctx;
7353 task->process = hlua_applet_wakeup;
7354 ctx->ctx.hlua_apphttp.task = task;
7355
7356 /* In the execution wrappers linked with a stream, the
7357 * Lua context can be not initialized. This behavior
7358 * permits to save performances because a systematic
7359 * Lua initialization cause 5% performances loss.
7360 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007361 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 +02007362 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007363 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007364 return 0;
7365 }
7366
7367 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007368 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007369
7370 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007371 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007372 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7373 error = lua_tostring(hlua->T, -1);
7374 else
7375 error = "critical error";
7376 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007377 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007378 return 0;
7379 }
7380
7381 /* Check stack available size. */
7382 if (!lua_checkstack(hlua->T, 1)) {
7383 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007384 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007385 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007386 return 0;
7387 }
7388
7389 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007390 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007391
7392 /* Create and and push object stream in the stack. */
7393 if (!hlua_applet_http_new(hlua->T, ctx)) {
7394 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007395 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007396 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007397 return 0;
7398 }
7399 hlua->nargs = 1;
7400
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007401 /* push keywords in the stack. */
7402 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7403 if (!lua_checkstack(hlua->T, 1)) {
7404 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007405 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007406 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007407 return 0;
7408 }
7409 lua_pushstring(hlua->T, *arg);
7410 hlua->nargs++;
7411 }
7412
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007413 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007414
7415 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007416 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007417
7418 return 1;
7419}
7420
Willy Tarreau60409db2019-08-21 14:14:50 +02007421void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007422{
7423 struct stream_interface *si = ctx->owner;
7424 struct stream *strm = si_strm(si);
7425 struct channel *req = si_oc(si);
7426 struct channel *res = si_ic(si);
7427 struct act_rule *rule = ctx->rule;
7428 struct proxy *px = strm->be;
7429 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7430 struct htx *req_htx, *res_htx;
7431
7432 res_htx = htx_from_buf(&res->buf);
7433
7434 /* If the stream is disconnect or closed, ldo nothing. */
7435 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7436 goto out;
7437
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007438 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007439 if (!b_size(&res->buf)) {
7440 si_rx_room_blk(si);
7441 goto out;
7442 }
7443 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007444 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007445 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7446
7447 /* Set the currently running flag. */
7448 if (!HLUA_IS_RUNNING(hlua) &&
7449 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007450 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007451 si_cant_get(si);
7452 goto out;
7453 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007454 }
7455
7456 /* Executes The applet if it is not done. */
7457 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7458
7459 /* Execute the function. */
7460 switch (hlua_ctx_resume(hlua, 1)) {
7461 /* finished. */
7462 case HLUA_E_OK:
7463 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7464 break;
7465
7466 /* yield. */
7467 case HLUA_E_AGAIN:
7468 if (hlua->wake_time != TICK_ETERNITY)
7469 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007470 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007471
7472 /* finished with error. */
7473 case HLUA_E_ERRMSG:
7474 /* Display log. */
7475 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007476 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007477 lua_pop(hlua->T, 1);
7478 goto error;
7479
7480 case HLUA_E_ETMOUT:
7481 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007482 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007483 goto error;
7484
7485 case HLUA_E_NOMEM:
7486 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007487 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007488 goto error;
7489
7490 case HLUA_E_YIELD: /* unexpected */
7491 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\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 case HLUA_E_ERR:
7496 /* Display log. */
7497 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007498 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007499 goto error;
7500
7501 default:
7502 goto error;
7503 }
7504 }
7505
7506 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007507 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7508 goto done;
7509
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007510 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7511 goto error;
7512
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007513 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
7514 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007515 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7516 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007517 }
7518
7519 done:
7520 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007521 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007522 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007523 si_shutr(si);
7524 }
7525
7526 /* eat the whole request */
7527 if (co_data(req)) {
7528 req_htx = htx_from_buf(&req->buf);
7529 co_htx_skip(req, req_htx, co_data(req));
7530 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007531 }
7532 }
7533
7534 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007535 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007536 return;
7537
7538 error:
7539
7540 /* If we are in HTTP mode, and we are not send any
7541 * data, return a 500 server error in best effort:
7542 * if there is no room available in the buffer,
7543 * just close the connection.
7544 */
7545 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007546 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007547
7548 channel_erase(res);
7549 res->buf.data = b_data(err);
7550 memcpy(res->buf.area, b_head(err), b_data(err));
7551 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007552 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007553 }
7554 if (!(strm->flags & SF_ERR_MASK))
7555 strm->flags |= SF_ERR_RESOURCE;
7556 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7557 goto done;
7558}
7559
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007560static void hlua_applet_http_release(struct appctx *ctx)
7561{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007562 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007563 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007564 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007565 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007566}
7567
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007568/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007569 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007570 *
7571 * This function can fail with an abort() due to an Lua critical error.
7572 * We are in the configuration parsing process of HAProxy, this abort() is
7573 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007574 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007575static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7576 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007577{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007578 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007579 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007580
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007581 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007582 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007583 if (!rule->arg.hlua_rule) {
7584 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007585 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007586 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007587
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007588 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007589 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7590 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007591 if (!rule->arg.hlua_rule->args) {
7592 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007593 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007594 }
7595
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007596 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007597 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007598
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007599 /* Expect some arguments */
7600 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007601 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007602 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007603 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007604 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007605 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007606 if (!rule->arg.hlua_rule->args[i]) {
7607 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007608 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007609 }
7610 (*cur_arg)++;
7611 }
7612 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007613
Thierry FOURNIER42148732015-09-02 17:17:33 +02007614 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007615 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007616 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007617
7618 error:
7619 if (rule->arg.hlua_rule) {
7620 if (rule->arg.hlua_rule->args) {
7621 for (i = 0; i < fcn->nargs; i++)
7622 ha_free(&rule->arg.hlua_rule->args[i]);
7623 ha_free(&rule->arg.hlua_rule->args);
7624 }
7625 ha_free(&rule->arg.hlua_rule);
7626 }
7627 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007628}
7629
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007630static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7631 struct act_rule *rule, char **err)
7632{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007633 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007634
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007635 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007636 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007637 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007638 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007639 * the call of this analyzer.
7640 */
7641 if (rule->from != ACT_F_HTTP_REQ) {
7642 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7643 return ACT_RET_PRS_ERR;
7644 }
7645
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007646 /* Memory for the rule. */
7647 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7648 if (!rule->arg.hlua_rule) {
7649 memprintf(err, "out of memory error");
7650 return ACT_RET_PRS_ERR;
7651 }
7652
7653 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007654 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007655
7656 /* TODO: later accept arguments. */
7657 rule->arg.hlua_rule->args = NULL;
7658
7659 /* Add applet pointer in the rule. */
7660 rule->applet.obj_type = OBJ_TYPE_APPLET;
7661 rule->applet.name = fcn->name;
7662 rule->applet.init = hlua_applet_http_init;
7663 rule->applet.fct = hlua_applet_http_fct;
7664 rule->applet.release = hlua_applet_http_release;
7665 rule->applet.timeout = hlua_timeout_applet;
7666
7667 return ACT_RET_PRS_OK;
7668}
7669
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007670/* This function is an LUA binding used for registering
7671 * "sample-conv" functions. It expects a converter name used
7672 * in the haproxy configuration file, and an LUA function.
7673 */
7674__LJMP static int hlua_register_action(lua_State *L)
7675{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007676 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007677 const char *name;
7678 int ref;
7679 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007680 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007681 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007682 struct buffer *trash;
7683 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007684
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007685 /* Initialise the number of expected arguments at 0. */
7686 nargs = 0;
7687
7688 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7689 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007690
7691 /* First argument : converter name. */
7692 name = MAY_LJMP(luaL_checkstring(L, 1));
7693
7694 /* Second argument : environment. */
7695 if (lua_type(L, 2) != LUA_TTABLE)
7696 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7697
7698 /* Third argument : lua function. */
7699 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7700
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007701 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007702 if (lua_gettop(L) >= 4)
7703 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7704
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007705 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007706 lua_pushnil(L);
7707 while (lua_next(L, 2) != 0) {
7708 if (lua_type(L, -1) != LUA_TSTRING)
7709 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7710
Thierry Fournierf67442e2020-11-28 20:41:07 +01007711 /* Check if action exists */
7712 trash = get_trash_chunk();
7713 chunk_printf(trash, "lua.%s", name);
7714 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7715 akw = tcp_req_cont_action(trash->area);
7716 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7717 akw = tcp_res_cont_action(trash->area);
7718 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7719 akw = action_http_req_custom(trash->area);
7720 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7721 akw = action_http_res_custom(trash->area);
7722 } else {
7723 akw = NULL;
7724 }
7725 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007726 fcn = akw->private;
7727 if (fcn->function_ref[hlua_state_id] != -1) {
7728 ha_warning("Trying to register action 'lua.%s' more than once. "
7729 "This will become a hard error in version 2.5.\n", name);
7730 }
7731 fcn->function_ref[hlua_state_id] = ref;
7732
7733 /* pop the environment string. */
7734 lua_pop(L, 1);
7735 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007736 }
7737
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007738 /* Check required environment. Only accepted "http" or "tcp". */
7739 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007740 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007741 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007742 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007743 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007744 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007745 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007746
7747 /* Fill fcn. */
7748 fcn->name = strdup(name);
7749 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007750 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007751 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007752
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007753 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007754 fcn->nargs = nargs;
7755
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007756 /* List head */
7757 akl->list.n = akl->list.p = NULL;
7758
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007759 /* action keyword. */
7760 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007761 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007762 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007763 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007764
7765 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7766
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007767 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007768 akl->kw[0].private = fcn;
7769 akl->kw[0].parse = action_register_lua;
7770
7771 /* select the action registering point. */
7772 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7773 tcp_req_cont_keywords_register(akl);
7774 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7775 tcp_res_cont_keywords_register(akl);
7776 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7777 http_req_keywords_register(akl);
7778 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7779 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007780 else {
7781 release_hlua_function(fcn);
7782 if (akl)
7783 ha_free((char **)&(akl->kw[0].kw));
7784 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007785 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007786 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7787 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007788 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007789
7790 /* pop the environment string. */
7791 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007792
7793 /* reset for next loop */
7794 akl = NULL;
7795 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007796 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007797 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007798
7799 alloc_error:
7800 release_hlua_function(fcn);
7801 ha_free(&akl);
7802 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7803 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007804}
7805
7806static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7807 struct act_rule *rule, char **err)
7808{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007809 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007810
Christopher Faulet280f85b2019-07-15 15:02:04 +02007811 if (px->mode == PR_MODE_HTTP) {
7812 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007813 return ACT_RET_PRS_ERR;
7814 }
7815
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007816 /* Memory for the rule. */
7817 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7818 if (!rule->arg.hlua_rule) {
7819 memprintf(err, "out of memory error");
7820 return ACT_RET_PRS_ERR;
7821 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007822
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007823 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007824 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007825
7826 /* TODO: later accept arguments. */
7827 rule->arg.hlua_rule->args = NULL;
7828
7829 /* Add applet pointer in the rule. */
7830 rule->applet.obj_type = OBJ_TYPE_APPLET;
7831 rule->applet.name = fcn->name;
7832 rule->applet.init = hlua_applet_tcp_init;
7833 rule->applet.fct = hlua_applet_tcp_fct;
7834 rule->applet.release = hlua_applet_tcp_release;
7835 rule->applet.timeout = hlua_timeout_applet;
7836
7837 return 0;
7838}
7839
7840/* This function is an LUA binding used for registering
7841 * "sample-conv" functions. It expects a converter name used
7842 * in the haproxy configuration file, and an LUA function.
7843 */
7844__LJMP static int hlua_register_service(lua_State *L)
7845{
7846 struct action_kw_list *akl;
7847 const char *name;
7848 const char *env;
7849 int ref;
7850 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007851 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007852 struct buffer *trash;
7853 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007854
7855 MAY_LJMP(check_args(L, 3, "register_service"));
7856
7857 /* First argument : converter name. */
7858 name = MAY_LJMP(luaL_checkstring(L, 1));
7859
7860 /* Second argument : environment. */
7861 env = MAY_LJMP(luaL_checkstring(L, 2));
7862
7863 /* Third argument : lua function. */
7864 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7865
Thierry Fournierf67442e2020-11-28 20:41:07 +01007866 /* Check for service already registered */
7867 trash = get_trash_chunk();
7868 chunk_printf(trash, "lua.%s", name);
7869 akw = service_find(trash->area);
7870 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007871 fcn = akw->private;
7872 if (fcn->function_ref[hlua_state_id] != -1) {
7873 ha_warning("Trying to register service 'lua.%s' more than once. "
7874 "This will become a hard error in version 2.5.\n", name);
7875 }
7876 fcn->function_ref[hlua_state_id] = ref;
7877 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007878 }
7879
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007880 /* Allocate and fill the sample fetch keyword struct. */
7881 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7882 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007883 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007884 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007885 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007886 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007887
7888 /* Fill fcn. */
7889 len = strlen("<lua.>") + strlen(name) + 1;
7890 fcn->name = calloc(1, len);
7891 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007892 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007893 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007894 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007895
7896 /* List head */
7897 akl->list.n = akl->list.p = NULL;
7898
7899 /* converter keyword. */
7900 len = strlen("lua.") + strlen(name) + 1;
7901 akl->kw[0].kw = calloc(1, len);
7902 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007903 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007904
7905 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7906
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007907 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007908 if (strcmp(env, "tcp") == 0)
7909 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007910 else if (strcmp(env, "http") == 0)
7911 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007912 else {
7913 release_hlua_function(fcn);
7914 if (akl)
7915 ha_free((char **)&(akl->kw[0].kw));
7916 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007917 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007918 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02007919 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007920
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007921 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007922 akl->kw[0].private = fcn;
7923
7924 /* End of array. */
7925 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7926
7927 /* Register this new converter */
7928 service_keywords_register(akl);
7929
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007930 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007931
7932 alloc_error:
7933 release_hlua_function(fcn);
7934 ha_free(&akl);
7935 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7936 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007937}
7938
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007939/* This function initialises Lua cli handler. It copies the
7940 * arguments in the Lua stack and create channel IO objects.
7941 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007942static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007943{
7944 struct hlua *hlua;
7945 struct hlua_function *fcn;
7946 int i;
7947 const char *error;
7948
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007949 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007950 appctx->ctx.hlua_cli.fcn = private;
7951
Willy Tarreaubafbe012017-11-24 17:34:44 +01007952 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007953 if (!hlua) {
7954 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007955 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007956 }
7957 HLUA_INIT(hlua);
7958 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007959
7960 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007961 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007962 * applet_http. It is absolutely compatible.
7963 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007964 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007965 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007966 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007967 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007968 }
7969 appctx->ctx.hlua_cli.task->nice = 0;
7970 appctx->ctx.hlua_cli.task->context = appctx;
7971 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7972
7973 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01007974 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 +01007975 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007976 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007977 }
7978
7979 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007980 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007981 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7982 error = lua_tostring(hlua->T, -1);
7983 else
7984 error = "critical error";
7985 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7986 goto error;
7987 }
7988
7989 /* Check stack available size. */
7990 if (!lua_checkstack(hlua->T, 2)) {
7991 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7992 goto error;
7993 }
7994
7995 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007996 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007997
7998 /* Once the arguments parsed, the CLI is like an AppletTCP,
7999 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008000 */
8001 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
8002 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8003 goto error;
8004 }
8005 hlua->nargs = 1;
8006
8007 /* push keywords in the stack. */
8008 for (i = 0; *args[i]; i++) {
8009 /* Check stack available size. */
8010 if (!lua_checkstack(hlua->T, 1)) {
8011 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8012 goto error;
8013 }
8014 lua_pushstring(hlua->T, args[i]);
8015 hlua->nargs++;
8016 }
8017
8018 /* We must initialize the execution timeouts. */
8019 hlua->max_time = hlua_timeout_session;
8020
8021 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008022 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008023
8024 /* It's ok */
8025 return 0;
8026
8027 /* It's not ok. */
8028error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008029 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008030 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008031 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008032 return 1;
8033}
8034
8035static int hlua_cli_io_handler_fct(struct appctx *appctx)
8036{
8037 struct hlua *hlua;
8038 struct stream_interface *si;
8039 struct hlua_function *fcn;
8040
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008041 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008042 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008043 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008044
8045 /* If the stream is disconnect or closed, ldo nothing. */
8046 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8047 return 1;
8048
8049 /* Execute the function. */
8050 switch (hlua_ctx_resume(hlua, 1)) {
8051
8052 /* finished. */
8053 case HLUA_E_OK:
8054 return 1;
8055
8056 /* yield. */
8057 case HLUA_E_AGAIN:
8058 /* We want write. */
8059 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008060 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008061 /* Set the timeout. */
8062 if (hlua->wake_time != TICK_ETERNITY)
8063 task_schedule(hlua->task, hlua->wake_time);
8064 return 0;
8065
8066 /* finished with error. */
8067 case HLUA_E_ERRMSG:
8068 /* Display log. */
8069 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8070 fcn->name, lua_tostring(hlua->T, -1));
8071 lua_pop(hlua->T, 1);
8072 return 1;
8073
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008074 case HLUA_E_ETMOUT:
8075 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8076 fcn->name);
8077 return 1;
8078
8079 case HLUA_E_NOMEM:
8080 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8081 fcn->name);
8082 return 1;
8083
8084 case HLUA_E_YIELD: /* unexpected */
8085 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8086 fcn->name);
8087 return 1;
8088
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008089 case HLUA_E_ERR:
8090 /* Display log. */
8091 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8092 fcn->name);
8093 return 1;
8094
8095 default:
8096 return 1;
8097 }
8098
8099 return 1;
8100}
8101
8102static void hlua_cli_io_release_fct(struct appctx *appctx)
8103{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008104 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008105 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008106}
8107
8108/* This function is an LUA binding used for registering
8109 * new keywords in the cli. It expects a list of keywords
8110 * which are the "path". It is limited to 5 keywords. A
8111 * description of the command, a function to be executed
8112 * for the parsing and a function for io handlers.
8113 */
8114__LJMP static int hlua_register_cli(lua_State *L)
8115{
8116 struct cli_kw_list *cli_kws;
8117 const char *message;
8118 int ref_io;
8119 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008120 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008121 int index;
8122 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008123 struct buffer *trash;
8124 const char *kw[5];
8125 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008126 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008127
8128 MAY_LJMP(check_args(L, 3, "register_cli"));
8129
8130 /* First argument : an array of maximum 5 keywords. */
8131 if (!lua_istable(L, 1))
8132 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8133
8134 /* Second argument : string with contextual message. */
8135 message = MAY_LJMP(luaL_checkstring(L, 2));
8136
8137 /* Third and fourth argument : lua function. */
8138 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8139
Thierry Fournierf67442e2020-11-28 20:41:07 +01008140 /* Check for CLI service already registered */
8141 trash = get_trash_chunk();
8142 index = 0;
8143 lua_pushnil(L);
8144 memset(kw, 0, sizeof(kw));
8145 while (lua_next(L, 1) != 0) {
8146 if (index >= CLI_PREFIX_KW_NB)
8147 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8148 if (lua_type(L, -1) != LUA_TSTRING)
8149 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8150 kw[index] = lua_tostring(L, -1);
8151 if (index == 0)
8152 chunk_printf(trash, "%s", kw[index]);
8153 else
8154 chunk_appendf(trash, " %s", kw[index]);
8155 index++;
8156 lua_pop(L, 1);
8157 }
8158 cli_kw = cli_find_kw_exact((char **)kw);
8159 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008160 fcn = cli_kw->private;
8161 if (fcn->function_ref[hlua_state_id] != -1) {
8162 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8163 "This will become a hard error in version 2.5.\n", trash->area);
8164 }
8165 fcn->function_ref[hlua_state_id] = ref_io;
8166 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008167 }
8168
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008169 /* Allocate and fill the sample fetch keyword struct. */
8170 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008171 if (!cli_kws) {
8172 errmsg = "Lua out of memory error.";
8173 goto error;
8174 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008175 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008176 if (!fcn) {
8177 errmsg = "Lua out of memory error.";
8178 goto error;
8179 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008180
8181 /* Fill path. */
8182 index = 0;
8183 lua_pushnil(L);
8184 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008185 if (index >= 5) {
8186 errmsg = "1st argument must be a table with a maximum of 5 entries";
8187 goto error;
8188 }
8189 if (lua_type(L, -1) != LUA_TSTRING) {
8190 errmsg = "1st argument must be a table filled with strings";
8191 goto error;
8192 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008193 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008194 if (!cli_kws->kw[0].str_kw[index]) {
8195 errmsg = "Lua out of memory error.";
8196 goto error;
8197 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008198 index++;
8199 lua_pop(L, 1);
8200 }
8201
8202 /* Copy help message. */
8203 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008204 if (!cli_kws->kw[0].usage) {
8205 errmsg = "Lua out of memory error.";
8206 goto error;
8207 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008208
8209 /* Fill fcn io handler. */
8210 len = strlen("<lua.cli>") + 1;
8211 for (i = 0; i < index; i++)
8212 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8213 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008214 if (!fcn->name) {
8215 errmsg = "Lua out of memory error.";
8216 goto error;
8217 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008218 strncat((char *)fcn->name, "<lua.cli", len);
8219 for (i = 0; i < index; i++) {
8220 strncat((char *)fcn->name, ".", len);
8221 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8222 }
8223 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008224 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008225
8226 /* Fill last entries. */
8227 cli_kws->kw[0].private = fcn;
8228 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8229 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8230 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8231
8232 /* Register this new converter */
8233 cli_register_kw(cli_kws);
8234
8235 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008236
8237 error:
8238 release_hlua_function(fcn);
8239 if (cli_kws) {
8240 for (i = 0; i < index; i++)
8241 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8242 ha_free((char **)&(cli_kws->kw[0].usage));
8243 }
8244 ha_free(&cli_kws);
8245 WILL_LJMP(luaL_error(L, errmsg));
8246 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008247}
8248
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008249static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008250 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008251 char **err, unsigned int *timeout)
8252{
8253 const char *error;
8254
8255 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008256 if (error == PARSE_TIME_OVER) {
8257 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8258 args[1], args[0]);
8259 return -1;
8260 }
8261 else if (error == PARSE_TIME_UNDER) {
8262 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8263 args[1], args[0]);
8264 return -1;
8265 }
8266 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008267 memprintf(err, "%s: invalid timeout", args[0]);
8268 return -1;
8269 }
8270 return 0;
8271}
8272
8273static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008274 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008275 char **err)
8276{
8277 return hlua_read_timeout(args, section_type, curpx, defpx,
8278 file, line, err, &hlua_timeout_session);
8279}
8280
8281static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008282 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008283 char **err)
8284{
8285 return hlua_read_timeout(args, section_type, curpx, defpx,
8286 file, line, err, &hlua_timeout_task);
8287}
8288
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008289static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008290 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008291 char **err)
8292{
8293 return hlua_read_timeout(args, section_type, curpx, defpx,
8294 file, line, err, &hlua_timeout_applet);
8295}
8296
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008297static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008298 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008299 char **err)
8300{
8301 char *error;
8302
8303 hlua_nb_instruction = strtoll(args[1], &error, 10);
8304 if (*error != '\0') {
8305 memprintf(err, "%s: invalid number", args[0]);
8306 return -1;
8307 }
8308 return 0;
8309}
8310
Willy Tarreau32f61e22015-03-18 17:54:59 +01008311static int hlua_parse_maxmem(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,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008313 char **err)
8314{
8315 char *error;
8316
8317 if (*(args[1]) == 0) {
8318 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8319 return -1;
8320 }
8321 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8322 if (*error != '\0') {
8323 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8324 return -1;
8325 }
8326 return 0;
8327}
8328
8329
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008330/* This function is called by the main configuration key "lua-load". It loads and
8331 * execute an lua file during the parsing of the HAProxy configuration file. It is
8332 * the main lua entry point.
8333 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008334 * This function runs with the HAProxy keywords API. It returns -1 if an error
8335 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008336 *
8337 * In some error case, LUA set an error message in top of the stack. This function
8338 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008339 *
8340 * This function can fail with an abort() due to an Lua critical error.
8341 * We are in the configuration parsing process of HAProxy, this abort() is
8342 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008343 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008344static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008345{
8346 int error;
8347
8348 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008349 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008350 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008351 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8352 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008353 return -1;
8354 }
8355
8356 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008357 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008358 switch (error) {
8359 case LUA_OK:
8360 break;
8361 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008362 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8363 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008364 return -1;
8365 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008366 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008367 return -1;
8368 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008369 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8370 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008371 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008372#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008373 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008374 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8375 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008376 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008377#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008378 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008379 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8380 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008381 return -1;
8382 }
8383
8384 return 0;
8385}
8386
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008387static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008388 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008389 char **err)
8390{
8391 if (*(args[1]) == 0) {
8392 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8393 return -1;
8394 }
8395
Thierry Fournier59f11be2020-11-29 00:37:41 +01008396 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008397 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008398 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008399 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008400}
8401
Thierry Fournier59f11be2020-11-29 00:37:41 +01008402static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008403 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008404 char **err)
8405{
8406 int len;
8407
8408 if (*(args[1]) == 0) {
8409 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8410 return -1;
8411 }
8412
8413 if (per_thread_load == NULL) {
8414 /* allocate the first entry large enough to store the final NULL */
8415 per_thread_load = calloc(1, sizeof(*per_thread_load));
8416 if (per_thread_load == NULL) {
8417 memprintf(err, "out of memory error");
8418 return -1;
8419 }
8420 }
8421
8422 /* count used entries */
8423 for (len = 0; per_thread_load[len] != NULL; len++)
8424 ;
8425
8426 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8427 if (per_thread_load == NULL) {
8428 memprintf(err, "out of memory error");
8429 return -1;
8430 }
8431
8432 per_thread_load[len] = strdup(args[1]);
8433 per_thread_load[len + 1] = NULL;
8434
8435 if (per_thread_load[len] == NULL) {
8436 memprintf(err, "out of memory error");
8437 return -1;
8438 }
8439
8440 /* loading for thread 1 only */
8441 hlua_state_id = 1;
8442 ha_set_tid(0);
8443 return hlua_load_state(args[1], hlua_states[1], err);
8444}
8445
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008446/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8447 * in the given <ctx>.
8448 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008449static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008450{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008451 lua_getglobal(L, "package"); /* push package variable */
8452 lua_pushstring(L, path); /* push given path */
8453 lua_pushstring(L, ";"); /* push semicolon */
8454 lua_getfield(L, -3, type); /* push old path */
8455 lua_concat(L, 3); /* concatenate to new path */
8456 lua_setfield(L, -2, type); /* store new path */
8457 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008458
8459 return 0;
8460}
8461
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008462static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008463 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008464 char **err)
8465{
8466 char *path;
8467 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008468 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008469
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008470 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008471 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008472 }
8473
8474 if (!(*args[1])) {
8475 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008476 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008477 }
8478 path = args[1];
8479
8480 if (*args[2]) {
8481 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8482 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008483 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008484 }
8485 type = args[2];
8486 }
8487
Thierry Fournier59f11be2020-11-29 00:37:41 +01008488 p = calloc(1, sizeof(*p));
8489 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008490 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008491 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008492 }
8493 p->path = strdup(path);
8494 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008495 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008496 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008497 }
8498 p->type = strdup(type);
8499 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008500 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008501 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008502 }
Willy Tarreau2b718102021-04-21 07:32:39 +02008503 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01008504
8505 hlua_prepend_path(hlua_states[0], type, path);
8506 hlua_prepend_path(hlua_states[1], type, path);
8507 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008508
8509err2:
8510 free(p->type);
8511 free(p->path);
8512err:
8513 free(p);
8514 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008515}
8516
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008517/* configuration keywords declaration */
8518static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008519 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008520 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008521 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008522 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8523 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008524 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008525 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008526 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008527 { 0, NULL, NULL },
8528}};
8529
Willy Tarreau0108d902018-11-25 19:14:37 +01008530INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8531
Christopher Fauletafd8f102018-11-08 11:34:21 +01008532
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008533/* This function can fail with an abort() due to an Lua critical error.
8534 * We are in the initialisation process of HAProxy, this abort() is
8535 * tolerated.
8536 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008537int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008538{
8539 struct hlua_init_function *init;
8540 const char *msg;
8541 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008542 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008543 const char *kind;
8544 const char *trace;
8545 int return_status = 1;
8546#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8547 int nres;
8548#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008549
Willy Tarreaucdb53462020-12-02 12:12:00 +01008550 /* disable memory limit checks if limit is not set */
8551 if (!hlua_global_allocator.limit)
8552 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8553
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008554 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008555 if (setjmp(safe_ljmp_env) != 0) {
8556 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008557 if (lua_type(L, -1) == LUA_TSTRING)
8558 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008559 else
8560 error = "critical error";
8561 fprintf(stderr, "Lua post-init: %s.\n", error);
8562 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008563 } else {
8564 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008565 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008566
Thierry Fournierb8cef172020-11-28 15:37:17 +01008567 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008568
Thierry Fournierc7492592020-11-28 23:57:24 +01008569 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008570 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008571
8572#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008573 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008574#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008575 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008576#endif
8577 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008578 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008579
8580 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008581 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008582 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008583
8584 case LUA_ERRERR:
8585 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008586 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008587 case LUA_ERRRUN:
8588 if (!kind)
8589 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008590 msg = lua_tostring(L, -1);
8591 lua_settop(L, 0); /* Empty the stack. */
8592 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008593 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008594 if (msg)
8595 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8596 else
8597 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8598 return_status = 0;
8599 break;
8600
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008601 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008602 /* Unknown error */
8603 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008604 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008605 case LUA_YIELD:
8606 /* yield is not configured at this step, this state doesn't happen */
8607 if (!kind)
8608 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008609 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008610 case LUA_ERRMEM:
8611 if (!kind)
8612 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008613 lua_settop(L, 0);
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 ha_alert("Lua init: %s: %s\n", kind, trace);
8617 return_status = 0;
8618 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008619 }
Thierry Fournier670db242020-11-28 10:49:59 +01008620 if (!return_status)
8621 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008622 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008623
8624 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008625 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008626}
8627
Thierry Fournierb8cef172020-11-28 15:37:17 +01008628int hlua_post_init()
8629{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008630 int ret;
8631 int i;
8632 int errors;
8633 char *err = NULL;
8634 struct hlua_function *fcn;
8635
Thierry Fournierb8cef172020-11-28 15:37:17 +01008636#if USE_OPENSSL
8637 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008638 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008639 int saved_used_backed = global.ssl_used_backend;
8640 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008641 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008642 global.ssl_used_backend = saved_used_backed;
8643 }
8644#endif
8645
Thierry Fournierc7492592020-11-28 23:57:24 +01008646 /* Perform post init of common thread */
8647 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008648 ha_set_tid(0);
8649 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8650 if (ret == 0)
8651 return 0;
8652
8653 /* init remaining lua states and load files */
8654 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8655
8656 /* set thread context */
8657 ha_set_tid(hlua_state_id - 1);
8658
8659 /* Init lua state */
8660 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8661
8662 /* Load lua files */
8663 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8664 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8665 if (ret != 0) {
8666 ha_alert("Lua init: %s\n", err);
8667 return 0;
8668 }
8669 }
8670 }
8671
8672 /* Reset thread context */
8673 ha_set_tid(0);
8674
8675 /* Execute post init for all states */
8676 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8677
8678 /* set thread context */
8679 ha_set_tid(hlua_state_id - 1);
8680
8681 /* run post init */
8682 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8683 if (ret == 0)
8684 return 0;
8685 }
8686
8687 /* Reset thread context */
8688 ha_set_tid(0);
8689
8690 /* control functions registering. Each function must have:
8691 * - only the function_ref[0] set positive and all other to -1
8692 * - only the function_ref[0] set to -1 and all other positive
8693 * This ensure a same reference is not used both in shared
8694 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008695 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008696 * complicated to found for the end user.
8697 */
8698 errors = 0;
8699 list_for_each_entry(fcn, &referenced_functions, l) {
8700 ret = 0;
8701 for (i = 1; i < global.nbthread + 1; i++) {
8702 if (fcn->function_ref[i] == -1)
8703 ret--;
8704 else
8705 ret++;
8706 }
8707 if (abs(ret) != global.nbthread) {
8708 ha_alert("Lua function '%s' is not referenced in all thread. "
8709 "Expect function in all thread or in none thread.\n", fcn->name);
8710 errors++;
8711 continue;
8712 }
8713
8714 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008715 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8716 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008717 "exclusive.\n", fcn->name);
8718 errors++;
8719 }
8720 }
8721
8722 if (errors > 0)
8723 return 0;
8724
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008725 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008726 * -1 in order to have probably a segfault if someone use it
8727 */
8728 hlua_state_id = -1;
8729
8730 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008731}
8732
Willy Tarreau32f61e22015-03-18 17:54:59 +01008733/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8734 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8735 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008736 * allocation. <nsize> is the requested new size. A new allocation is
8737 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008738 * zero. This one verifies that the limits are respected but is optimized
8739 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008740 */
8741static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8742{
8743 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008744 size_t limit, old, new;
8745
Tim Duesterhus22586522021-01-08 10:35:33 +01008746 if (unlikely(!ptr && !nsize))
8747 return NULL;
8748
Willy Tarreaucdb53462020-12-02 12:12:00 +01008749 /* a limit of ~0 means unlimited and boot complete, so there's no need
8750 * for accounting anymore.
8751 */
Christopher Fauletcc2c4f82021-03-24 14:52:24 +01008752 if (likely(~zone->limit == 0))
8753 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008754
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008755 if (!ptr)
8756 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008757
Willy Tarreaucdb53462020-12-02 12:12:00 +01008758 /* enforce strict limits across all threads */
8759 limit = zone->limit;
8760 old = _HA_ATOMIC_LOAD(&zone->allocated);
8761 do {
8762 new = old + nsize - osize;
8763 if (unlikely(nsize && limit && new > limit))
8764 return NULL;
8765 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008766
8767 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008768
8769 if (unlikely(!ptr && nsize)) // failed
8770 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8771
8772 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008773 return ptr;
8774}
8775
Thierry Fournierecb83c22020-11-28 15:49:44 +01008776/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008777 * We are in the initialisation process of HAProxy, this abort() is
8778 * tolerated.
8779 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008780lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008781{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008782 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008783 int idx;
8784 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008785 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008786 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008787 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008788 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008789 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008790 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008791
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008792 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008793 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008794
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008795 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008796 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008797 *context = NULL;
8798
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008799 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008800 * the Lua function can fail with an abort. We are in the initialisation
8801 * process of HAProxy, this abort() is tolerated.
8802 */
8803
Thierry Fournier3c539322020-11-28 16:05:05 +01008804 /* Call post initialisation function in safe environment. */
8805 if (setjmp(safe_ljmp_env) != 0) {
8806 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008807 if (lua_type(L, -1) == LUA_TSTRING)
8808 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008809 else
8810 error_msg = "critical error";
8811 fprintf(stderr, "Lua init: %s.\n", error_msg);
8812 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008813 } else {
8814 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008815 }
8816
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008817 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008818 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008819#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8820#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8821#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008822 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008823#endif
8824#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008825 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008826#endif
8827#undef HLUA_PREPEND_PATH_TOSTRING
8828#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008829
Thierry Fournier59f11be2020-11-29 00:37:41 +01008830 /* Apply configured prepend path */
8831 list_for_each_entry(pp, &prepend_path_list, l)
8832 hlua_prepend_path(L, pp->type, pp->path);
8833
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008834 /*
8835 *
8836 * Create "core" object.
8837 *
8838 */
8839
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008840 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008841 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008842
Thierry Fournierecb83c22020-11-28 15:49:44 +01008843 /* set the thread id */
8844 hlua_class_const_int(L, "thread", thread_num);
8845
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008846 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008847 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008848 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008849
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008850 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008851 hlua_class_function(L, "register_init", hlua_register_init);
8852 hlua_class_function(L, "register_task", hlua_register_task);
8853 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8854 hlua_class_function(L, "register_converters", hlua_register_converters);
8855 hlua_class_function(L, "register_action", hlua_register_action);
8856 hlua_class_function(L, "register_service", hlua_register_service);
8857 hlua_class_function(L, "register_cli", hlua_register_cli);
8858 hlua_class_function(L, "yield", hlua_yield);
8859 hlua_class_function(L, "set_nice", hlua_set_nice);
8860 hlua_class_function(L, "sleep", hlua_sleep);
8861 hlua_class_function(L, "msleep", hlua_msleep);
8862 hlua_class_function(L, "add_acl", hlua_add_acl);
8863 hlua_class_function(L, "del_acl", hlua_del_acl);
8864 hlua_class_function(L, "set_map", hlua_set_map);
8865 hlua_class_function(L, "del_map", hlua_del_map);
8866 hlua_class_function(L, "tcp", hlua_socket_new);
8867 hlua_class_function(L, "log", hlua_log);
8868 hlua_class_function(L, "Debug", hlua_log_debug);
8869 hlua_class_function(L, "Info", hlua_log_info);
8870 hlua_class_function(L, "Warning", hlua_log_warning);
8871 hlua_class_function(L, "Alert", hlua_log_alert);
8872 hlua_class_function(L, "done", hlua_done);
8873 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008874
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008875 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008876
8877 /*
8878 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008879 * Create "act" object.
8880 *
8881 */
8882
8883 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008884 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008885
8886 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008887 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8888 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8889 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8890 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8891 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8892 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8893 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8894 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008895
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008896 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008897
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008898 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008899
8900 /*
8901 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008902 * Register class Map
8903 *
8904 */
8905
8906 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008907 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008908
8909 /* register pattern types. */
8910 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008911 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008912 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008913 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008914 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008915 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008916
8917 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008918 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008919
8920 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008921 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008922
Ilya Shipitsind4259502020-04-08 01:07:56 +05008923 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008924 lua_pushstring(L, "__index");
8925 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008926
8927 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008928 hlua_class_function(L, "lookup", hlua_map_lookup);
8929 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008930
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008931 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008932
Thierry Fournier45e78d72016-02-19 18:34:46 +01008933 /* Register previous table in the registry with reference and named entry.
8934 * The function hlua_register_metatable() pops the stack, so we
8935 * previously create a copy of the table.
8936 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008937 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8938 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008939
8940 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008941 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008942
8943 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008944 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008945
8946 /*
8947 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008948 * Register class Channel
8949 *
8950 */
8951
8952 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008953 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008954
Ilya Shipitsind4259502020-04-08 01:07:56 +05008955 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008956 lua_pushstring(L, "__index");
8957 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008958
8959 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008960 hlua_class_function(L, "get", hlua_channel_get);
8961 hlua_class_function(L, "dup", hlua_channel_dup);
8962 hlua_class_function(L, "getline", hlua_channel_getline);
8963 hlua_class_function(L, "set", hlua_channel_set);
8964 hlua_class_function(L, "append", hlua_channel_append);
8965 hlua_class_function(L, "send", hlua_channel_send);
8966 hlua_class_function(L, "forward", hlua_channel_forward);
8967 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
8968 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
8969 hlua_class_function(L, "is_full", hlua_channel_is_full);
8970 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008971
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008972 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008973
8974 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008975 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008976
8977 /*
8978 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008979 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008980 *
8981 */
8982
8983 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008984 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008985
Ilya Shipitsind4259502020-04-08 01:07:56 +05008986 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008987 lua_pushstring(L, "__index");
8988 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008989
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008990 /* Browse existing fetches and create the associated
8991 * object method.
8992 */
8993 sf = NULL;
8994 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008995 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8996 * by an underscore.
8997 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02008998 strlcpy2(trash.area, sf->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008999 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009000 if (*p == '.' || *p == '-' || *p == '+')
9001 *p = '_';
9002
9003 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009004 lua_pushstring(L, trash.area);
9005 lua_pushlightuserdata(L, sf);
9006 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
9007 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009008 }
9009
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009010 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009011
9012 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009013 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009014
9015 /*
9016 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009017 * Register class Converters
9018 *
9019 */
9020
9021 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009022 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009023
9024 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009025 lua_pushstring(L, "__index");
9026 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009027
9028 /* Browse existing converters and create the associated
9029 * object method.
9030 */
9031 sc = NULL;
9032 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009033 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9034 * by an underscore.
9035 */
Willy Tarreaufa92f362021-08-26 16:48:53 +02009036 strlcpy2(trash.area, sc->kw, trash.size);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009037 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009038 if (*p == '.' || *p == '-' || *p == '+')
9039 *p = '_';
9040
9041 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009042 lua_pushstring(L, trash.area);
9043 lua_pushlightuserdata(L, sc);
9044 lua_pushcclosure(L, hlua_run_sample_conv, 1);
9045 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009046 }
9047
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009048 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009049
9050 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009051 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009052
9053 /*
9054 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009055 * Register class HTTP
9056 *
9057 */
9058
9059 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009060 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009061
Ilya Shipitsind4259502020-04-08 01:07:56 +05009062 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009063 lua_pushstring(L, "__index");
9064 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009065
9066 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009067 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9068 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9069 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9070 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9071 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9072 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9073 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9074 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9075 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9076 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009077
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009078 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9079 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9080 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9081 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9082 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9083 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9084 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009085
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009086 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009087
9088 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009089 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009090
9091 /*
9092 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009093 * Register class AppletTCP
9094 *
9095 */
9096
9097 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009098 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009099
Ilya Shipitsind4259502020-04-08 01:07:56 +05009100 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009101 lua_pushstring(L, "__index");
9102 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009103
9104 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009105 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9106 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9107 hlua_class_function(L, "send", hlua_applet_tcp_send);
9108 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9109 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9110 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9111 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9112 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009113
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009114 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009115
9116 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009117 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009118
9119 /*
9120 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009121 * Register class AppletHTTP
9122 *
9123 */
9124
9125 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009126 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009127
Ilya Shipitsind4259502020-04-08 01:07:56 +05009128 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009129 lua_pushstring(L, "__index");
9130 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009131
9132 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009133 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9134 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9135 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9136 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9137 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9138 hlua_class_function(L, "getline", hlua_applet_http_getline);
9139 hlua_class_function(L, "receive", hlua_applet_http_recv);
9140 hlua_class_function(L, "send", hlua_applet_http_send);
9141 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9142 hlua_class_function(L, "set_status", hlua_applet_http_status);
9143 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009144
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009145 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009146
9147 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009148 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009149
9150 /*
9151 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009152 * Register class TXN
9153 *
9154 */
9155
9156 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009157 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009158
Ilya Shipitsind4259502020-04-08 01:07:56 +05009159 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009160 lua_pushstring(L, "__index");
9161 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009162
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009163 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009164 hlua_class_function(L, "set_priv", hlua_set_priv);
9165 hlua_class_function(L, "get_priv", hlua_get_priv);
9166 hlua_class_function(L, "set_var", hlua_set_var);
9167 hlua_class_function(L, "unset_var", hlua_unset_var);
9168 hlua_class_function(L, "get_var", hlua_get_var);
9169 hlua_class_function(L, "done", hlua_txn_done);
9170 hlua_class_function(L, "reply", hlua_txn_reply_new);
9171 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9172 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9173 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9174 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9175 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9176 hlua_class_function(L, "deflog", hlua_txn_deflog);
9177 hlua_class_function(L, "log", hlua_txn_log);
9178 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9179 hlua_class_function(L, "Info", hlua_txn_log_info);
9180 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9181 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009182
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009183 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009184
9185 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009186 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009187
9188 /*
9189 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009190 * Register class reply
9191 *
9192 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009193 lua_newtable(L);
9194 lua_pushstring(L, "__index");
9195 lua_newtable(L);
9196 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9197 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9198 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9199 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9200 lua_settable(L, -3); /* Sets the __index entry. */
9201 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009202
9203
9204 /*
9205 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009206 * Register class Socket
9207 *
9208 */
9209
9210 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009211 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009212
Ilya Shipitsind4259502020-04-08 01:07:56 +05009213 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009214 lua_pushstring(L, "__index");
9215 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009216
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009217#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009218 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009219#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009220 hlua_class_function(L, "connect", hlua_socket_connect);
9221 hlua_class_function(L, "send", hlua_socket_send);
9222 hlua_class_function(L, "receive", hlua_socket_receive);
9223 hlua_class_function(L, "close", hlua_socket_close);
9224 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9225 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9226 hlua_class_function(L, "setoption", hlua_socket_setoption);
9227 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009228
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009229 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009230
9231 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009232 lua_pushstring(L, "__gc");
9233 lua_pushcclosure(L, hlua_socket_gc, 0);
9234 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009235
9236 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009237 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009238
Thierry Fournieraafc7772020-12-04 11:47:47 +01009239 lua_atpanic(L, hlua_panic_safe);
9240
9241 return L;
9242}
9243
9244void hlua_init(void) {
9245 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009246 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009247#ifdef USE_OPENSSL
9248 struct srv_kw *kw;
9249 int tmp_error;
9250 char *error;
9251 char *args[] = { /* SSL client configuration. */
9252 "ssl",
9253 "verify",
9254 "none",
9255 NULL
9256 };
9257#endif
9258
9259 /* Init post init function list head */
9260 for (i = 0; i < MAX_THREADS + 1; i++)
9261 LIST_INIT(&hlua_init_functions[i]);
9262
9263 /* Init state for common/shared lua parts */
9264 hlua_state_id = 0;
9265 ha_set_tid(0);
9266 hlua_states[0] = hlua_init_state(0);
9267
9268 /* Init state 1 for thread 0. We have at least one thread. */
9269 hlua_state_id = 1;
9270 ha_set_tid(0);
9271 hlua_states[1] = hlua_init_state(1);
9272
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009273 /* Proxy and server configuration initialisation. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009274 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg);
9275 if (!socket_proxy) {
9276 fprintf(stderr, "Lua init: %s\n", errmsg);
9277 exit(1);
9278 }
9279 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009280
9281 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009282 socket_tcp = new_server(socket_proxy);
9283 if (!socket_tcp) {
9284 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9285 exit(1);
9286 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009287
9288#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009289 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009290 socket_ssl = new_server(socket_proxy);
9291 if (!socket_ssl) {
9292 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9293 exit(1);
9294 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009295
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009296 socket_ssl->use_ssl = 1;
9297 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009298
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009299 for (i = 0; args[i] != NULL; i++) {
9300 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009301 /*
9302 *
9303 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009304 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009305 * features like client certificates and ssl_verify.
9306 *
9307 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009308 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009309 if (tmp_error != 0) {
9310 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9311 abort(); /* This must be never arrives because the command line
9312 not editable by the user. */
9313 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009314 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009315 }
9316 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009317#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009318
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009319}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009320
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009321static void hlua_deinit()
9322{
Willy Tarreau186f3762020-12-04 11:48:12 +01009323 int thr;
9324
9325 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9326 if (hlua_states[thr])
9327 lua_close(hlua_states[thr]);
9328 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009329
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009330 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009331
Willy Tarreau0f143af2021-03-05 10:41:48 +01009332#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009333 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009334#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009335
9336 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009337}
9338
9339REGISTER_POST_DEINIT(hlua_deinit);
9340
Willy Tarreau80713382018-11-26 10:19:54 +01009341static void hlua_register_build_options(void)
9342{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009343 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009344
Willy Tarreaubb57d942016-12-21 19:04:56 +01009345 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9346 hap_register_build_opts(ptr, 1);
9347}
Willy Tarreau80713382018-11-26 10:19:54 +01009348
9349INITCALL0(STG_REGISTER, hlua_register_build_options);