blob: 6a9073e311b4b609ff8b7377faefc9842c593d8e [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 Tarreau6a510902021-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
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100146#define SET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200147 ({ \
148 int ret; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100149 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100150 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 if (setjmp(safe_ljmp_env) != 0) { \
152 lua_atpanic(__L, hlua_panic_safe); \
153 ret = 0; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100154 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100155 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200156 } else { \
157 lua_atpanic(__L, hlua_panic_ljmp); \
158 ret = 1; \
159 } \
160 ret; \
161 })
162
163/* If we are the last function catching Lua errors, we
164 * must reset the panic function.
165 */
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100166#define RESET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200167 do { \
168 lua_atpanic(__L, hlua_panic_safe); \
Thierry Fournier021d9862020-11-28 23:42:03 +0100169 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100170 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200171 } while(0)
172
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100173#define SET_SAFE_LJMP(__HLUA) \
174 SET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
175
176#define RESET_SAFE_LJMP(__HLUA) \
177 RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
178
179#define SET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100180 SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100181
182#define RESET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100183 RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100184
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200185/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200186#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100187/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200189/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100190#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100191#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200192
Thierry Fournierafc63e22020-11-28 17:06:51 +0100193/* The main Lua execution context. The 0 index is the
194 * common state shared by all threads.
195 */
Willy Tarreau186f3762020-12-04 11:48:12 +0100196static lua_State *hlua_states[MAX_THREADS + 1];
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100197
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100198/* This is the memory pool containing struct lua for applets
199 * (including cli).
200 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100201DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100202
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100203/* Used for Socket connection. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +0100204static struct proxy *socket_proxy;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100205static struct server *socket_tcp;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100206#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100207static struct server *socket_ssl;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100208#endif
209
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100210/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100211struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100212
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100213/* The following variables contains the reference of the different
214 * Lua classes. These references are useful for identify metadata
215 * associated with an object.
216 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100217static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100218static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100219static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100220static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100221static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100222static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200223static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200224static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200225static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100226static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100227
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100228/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200229 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100230 * short timeout. Lua linked with tasks doesn't have a timeout
231 * because a task may remain alive during all the haproxy execution.
232 */
233static unsigned int hlua_timeout_session = 4000; /* session timeout. */
234static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200235static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100236
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100237/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
238 * it is used for preventing infinite loops.
239 *
240 * I test the scheer with an infinite loop containing one incrementation
241 * and one test. I run this loop between 10 seconds, I raise a ceil of
242 * 710M loops from one interrupt each 9000 instructions, so I fix the value
243 * to one interrupt each 10 000 instructions.
244 *
245 * configured | Number of
246 * instructions | loops executed
247 * between two | in milions
248 * forced yields |
249 * ---------------+---------------
250 * 10 | 160
251 * 500 | 670
252 * 1000 | 680
253 * 5000 | 700
254 * 7000 | 700
255 * 8000 | 700
256 * 9000 | 710 <- ceil
257 * 10000 | 710
258 * 100000 | 710
259 * 1000000 | 710
260 *
261 */
262static unsigned int hlua_nb_instruction = 10000;
263
Willy Tarreaucdb53462020-12-02 12:12:00 +0100264/* Descriptor for the memory allocation state. The limit is pre-initialised to
265 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
266 * is replaced with ~0 during post_init after everything was loaded. This way
267 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
268 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100269 */
270struct hlua_mem_allocator {
271 size_t allocated;
272 size_t limit;
273};
274
Willy Tarreaucdb53462020-12-02 12:12:00 +0100275static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100276
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100277/* These functions converts types between HAProxy internal args or
278 * sample and LUA types. Another function permits to check if the
279 * LUA stack contains arguments according with an required ARG_T
280 * format.
281 */
282static int hlua_arg2lua(lua_State *L, const struct arg *arg);
283static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100284__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100285 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100286static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100287static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100288static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
289
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100290__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200291
Thierry Fournier59f11be2020-11-29 00:37:41 +0100292struct prepend_path {
293 struct list l;
294 char *type;
295 char *path;
296};
297
298static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
299
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200300#define SEND_ERR(__be, __fmt, __args...) \
301 do { \
302 send_log(__be, LOG_ERR, __fmt, ## __args); \
303 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100304 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200305 } while (0)
306
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100307static inline struct hlua_function *new_hlua_function()
308{
309 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100310 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100311
312 fcn = calloc(1, sizeof(*fcn));
313 if (!fcn)
314 return NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200315 LIST_APPEND(&referenced_functions, &fcn->l);
Thierry Fournierc7492592020-11-28 23:57:24 +0100316 for (i = 0; i < MAX_THREADS + 1; i++)
317 fcn->function_ref[i] = -1;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100318 return fcn;
319}
320
Christopher Fauletdda44442021-04-12 14:05:43 +0200321static inline void release_hlua_function(struct hlua_function *fcn)
322{
323 if (!fcn)
324 return;
325 if (fcn->name)
326 ha_free(&fcn->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200327 LIST_DELETE(&fcn->l);
Christopher Fauletdda44442021-04-12 14:05:43 +0200328 ha_free(&fcn);
329}
330
Thierry Fournierc7492592020-11-28 23:57:24 +0100331/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
332static inline int fcn_ref_to_stack_id(struct hlua_function *fcn)
333{
334 if (fcn->function_ref[0] == -1)
335 return tid + 1;
336 return 0;
337}
338
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100339/* Used to check an Lua function type in the stack. It creates and
340 * returns a reference of the function. This function throws an
341 * error if the rgument is not a "function".
342 */
343__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
344{
345 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100346 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100347 WILL_LJMP(luaL_argerror(L, argno, msg));
348 }
349 lua_pushvalue(L, argno);
350 return luaL_ref(L, LUA_REGISTRYINDEX);
351}
352
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200353/* Return the string that is of the top of the stack. */
354const char *hlua_get_top_error_string(lua_State *L)
355{
356 if (lua_gettop(L) < 1)
357 return "unknown error";
358 if (lua_type(L, -1) != LUA_TSTRING)
359 return "unknown error";
360 return lua_tostring(L, -1);
361}
362
Christopher Fauletd09cc512021-03-24 14:48:45 +0100363__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200364{
365 lua_Debug ar;
366 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200367 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200368
369 while (lua_getstack(L, level++, &ar)) {
370
371 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100372 if (b_data(msg))
373 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200374
375 /* Fill fields:
376 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
377 * 'l': fills in the field currentline;
378 * 'n': fills in the field name and namewhat;
379 * 't': fills in the field istailcall;
380 */
381 lua_getinfo(L, "Slnt", &ar);
382
383 /* Append code localisation */
384 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100385 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200386 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100387 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200388
389 /*
390 * Get function name
391 *
392 * if namewhat is no empty, name is defined.
393 * what contains "Lua" for Lua function, "C" for C function,
394 * or "main" for main code.
395 */
396 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100397 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200398
399 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100400 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200401
402 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100403 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200404
405 else /* nothing left... */
406 chunk_appendf(msg, "?");
407
408
409 /* Display tailed call */
410 if (ar.istailcall)
411 chunk_appendf(msg, " ...");
412 }
413
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200415}
416
417
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100418/* This function check the number of arguments available in the
419 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500420 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100421 */
422__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
423{
424 if (lua_gettop(L) == nb)
425 return;
426 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
427}
428
Mark Lakes22154b42018-01-29 14:38:40 -0800429/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100430 * and the line number where the error is encountered.
431 */
432static int hlua_pusherror(lua_State *L, const char *fmt, ...)
433{
434 va_list argp;
435 va_start(argp, fmt);
436 luaL_where(L, 1);
437 lua_pushvfstring(L, fmt, argp);
438 va_end(argp);
439 lua_concat(L, 2);
440 return 1;
441}
442
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443/* This functions is used with sample fetch and converters. It
444 * converts the HAProxy configuration argument in a lua stack
445 * values.
446 *
447 * It takes an array of "arg", and each entry of the array is
448 * converted and pushed in the LUA stack.
449 */
450static int hlua_arg2lua(lua_State *L, const struct arg *arg)
451{
452 switch (arg->type) {
453 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 case ARGT_TIME:
455 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100456 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200460 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 break;
462
463 case ARGT_IPV4:
464 case ARGT_IPV6:
465 case ARGT_MSK4:
466 case ARGT_MSK6:
467 case ARGT_FE:
468 case ARGT_BE:
469 case ARGT_TAB:
470 case ARGT_SRV:
471 case ARGT_USR:
472 case ARGT_MAP:
473 default:
474 lua_pushnil(L);
475 break;
476 }
477 return 1;
478}
479
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500480/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100481 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500482 * with sample fetch wrappers. The input arguments are given to the
483 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100484 */
485static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
486{
487 switch (lua_type(L, ud)) {
488
489 case LUA_TNUMBER:
490 case LUA_TBOOLEAN:
491 arg->type = ARGT_SINT;
492 arg->data.sint = lua_tointeger(L, ud);
493 break;
494
495 case LUA_TSTRING:
496 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200497 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200498 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200499 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200500 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100501 break;
502
503 case LUA_TUSERDATA:
504 case LUA_TNIL:
505 case LUA_TTABLE:
506 case LUA_TFUNCTION:
507 case LUA_TTHREAD:
508 case LUA_TLIGHTUSERDATA:
509 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200510 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100511 break;
512 }
513 return 1;
514}
515
516/* the following functions are used to convert a struct sample
517 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500518 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100519 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100520static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100521{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200522 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100523 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100524 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200525 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100526 break;
527
528 case SMP_T_BIN:
529 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100531 break;
532
533 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200534 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100535 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
536 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
537 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
538 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
539 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
540 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
541 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
542 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
543 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200544 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100545 break;
546 default:
547 lua_pushnil(L);
548 break;
549 }
550 break;
551
552 case SMP_T_IPV4:
553 case SMP_T_IPV6:
554 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 if (sample_casts[smp->data.type][SMP_T_STR] &&
556 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200557 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100558 else
559 lua_pushnil(L);
560 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 default:
562 lua_pushnil(L);
563 break;
564 }
565 return 1;
566}
567
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100568/* the following functions are used to convert a struct sample
569 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500570 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100571 */
572static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
573{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200574 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100575
576 case SMP_T_BIN:
577 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200578 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100579 break;
580
581 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200582 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100583 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
584 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
585 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
586 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
587 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
588 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
589 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
590 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
591 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200592 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100593 break;
594 default:
595 lua_pushstring(L, "");
596 break;
597 }
598 break;
599
600 case SMP_T_SINT:
601 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100602 case SMP_T_IPV4:
603 case SMP_T_IPV6:
604 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200605 if (sample_casts[smp->data.type][SMP_T_STR] &&
606 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200607 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100608 else
609 lua_pushstring(L, "");
610 break;
611 default:
612 lua_pushstring(L, "");
613 break;
614 }
615 return 1;
616}
617
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100618/* the following functions are used to convert an Lua type in a
619 * struct sample. This is useful to provide data from a converter
620 * to the LUA code.
621 */
622static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
623{
624 switch (lua_type(L, ud)) {
625
626 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200627 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200628 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100629 break;
630
631
632 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200633 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200634 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100635 break;
636
637 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200638 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100639 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200640 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200641 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200642 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200643 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100644 break;
645
646 case LUA_TUSERDATA:
647 case LUA_TNIL:
648 case LUA_TTABLE:
649 case LUA_TFUNCTION:
650 case LUA_TTHREAD:
651 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200652 case LUA_TNONE:
653 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200654 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200655 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100656 break;
657 }
658 return 1;
659}
660
Ilya Shipitsind4259502020-04-08 01:07:56 +0500661/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800662 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100663 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100664 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500665 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Willy Tarreauee0d7272021-07-16 10:26:56 +0200666 * entries and that there is at least one stop at the last position.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100667 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100668__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100669 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100670{
671 int min_arg;
Willy Tarreauee0d7272021-07-16 10:26:56 +0200672 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100673 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200674 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200675 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200676 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200677 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100678
679 idx = 0;
680 min_arg = ARGM(mask);
681 mask >>= ARGM_BITS;
682
683 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200684 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100685
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100686 /* Check for mandatory arguments. */
687 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100688 if (idx < min_arg) {
689
690 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200691 if (idx > 0) {
692 msg = "Mandatory argument expected";
693 goto error;
694 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100695
696 /* If first argument have a certain type, some default values
697 * may be used. See the function smp_resolve_args().
698 */
699 switch (mask & ARGT_MASK) {
700
701 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200702 if (!(p->cap & PR_CAP_FE)) {
703 msg = "Mandatory argument expected";
704 goto error;
705 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100706 argp[idx].data.prx = p;
707 argp[idx].type = ARGT_FE;
708 argp[idx+1].type = ARGT_STOP;
709 break;
710
711 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200712 if (!(p->cap & PR_CAP_BE)) {
713 msg = "Mandatory argument expected";
714 goto error;
715 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100716 argp[idx].data.prx = p;
717 argp[idx].type = ARGT_BE;
718 argp[idx+1].type = ARGT_STOP;
719 break;
720
721 case ARGT_TAB:
722 argp[idx].data.prx = p;
723 argp[idx].type = ARGT_TAB;
724 argp[idx+1].type = ARGT_STOP;
725 break;
726
727 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200728 msg = "Mandatory argument expected";
729 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100730 break;
731 }
732 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200733 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100734 }
735
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500736 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100737 if ((mask & ARGT_MASK) == ARGT_STOP &&
738 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200739 msg = "Last argument expected";
740 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100741 }
742
743 if ((mask & ARGT_MASK) == ARGT_STOP &&
744 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200745 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100746 }
747
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200748 /* Convert some argument types. All string in argp[] are for not
749 * duplicated yet.
750 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100751 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100752 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200753 if (argp[idx].type != ARGT_SINT) {
754 msg = "integer expected";
755 goto error;
756 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100757 argp[idx].type = ARGT_SINT;
758 break;
759
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100760 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200761 if (argp[idx].type != ARGT_SINT) {
762 msg = "integer expected";
763 goto error;
764 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200765 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100766 break;
767
768 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200769 if (argp[idx].type != ARGT_SINT) {
770 msg = "integer expected";
771 goto error;
772 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200773 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100774 break;
775
776 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200777 if (argp[idx].type != ARGT_STR) {
778 msg = "string expected";
779 goto error;
780 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200781 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200782 if (!argp[idx].data.prx) {
783 msg = "frontend doesn't exist";
784 goto error;
785 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100786 argp[idx].type = ARGT_FE;
787 break;
788
789 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200790 if (argp[idx].type != ARGT_STR) {
791 msg = "string expected";
792 goto error;
793 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200794 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200795 if (!argp[idx].data.prx) {
796 msg = "backend doesn't exist";
797 goto error;
798 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100799 argp[idx].type = ARGT_BE;
800 break;
801
802 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200803 if (argp[idx].type != ARGT_STR) {
804 msg = "string expected";
805 goto error;
806 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200807 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200808 if (!argp[idx].data.t) {
809 msg = "table doesn't exist";
810 goto error;
811 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100812 argp[idx].type = ARGT_TAB;
813 break;
814
815 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200816 if (argp[idx].type != ARGT_STR) {
817 msg = "string expected";
818 goto error;
819 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200820 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100821 if (sname) {
822 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200823 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200824 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200825 if (!px) {
826 msg = "backend doesn't exist";
827 goto error;
828 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100829 }
830 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200831 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100832 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100833 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100834 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200835 if (!argp[idx].data.srv) {
836 msg = "server doesn't exist";
837 goto error;
838 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100839 argp[idx].type = ARGT_SRV;
840 break;
841
842 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200843 if (argp[idx].type != ARGT_STR) {
844 msg = "string expected";
845 goto error;
846 }
847 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
848 msg = "invalid IPv4 address";
849 goto error;
850 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100851 argp[idx].type = ARGT_IPV4;
852 break;
853
854 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200855 if (argp[idx].type == ARGT_SINT)
856 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
857 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200858 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
859 msg = "invalid IPv4 mask";
860 goto error;
861 }
862 }
863 else {
864 msg = "integer or string expected";
865 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200866 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100867 argp[idx].type = ARGT_MSK4;
868 break;
869
870 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200871 if (argp[idx].type != ARGT_STR) {
872 msg = "string expected";
873 goto error;
874 }
875 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
876 msg = "invalid IPv6 address";
877 goto error;
878 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100879 argp[idx].type = ARGT_IPV6;
880 break;
881
882 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200883 if (argp[idx].type == ARGT_SINT)
884 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
885 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200886 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
887 msg = "invalid IPv6 mask";
888 goto error;
889 }
890 }
891 else {
892 msg = "integer or string expected";
893 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200894 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100895 argp[idx].type = ARGT_MSK6;
896 break;
897
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200898 case ARGT_REG:
899 if (argp[idx].type != ARGT_STR) {
900 msg = "string expected";
901 goto error;
902 }
903 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
904 if (!reg) {
905 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
906 argp[idx].data.str.area, err);
907 free(err);
908 goto error;
909 }
910 argp[idx].type = ARGT_REG;
911 argp[idx].data.reg = reg;
912 break;
913
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100914 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200915 if (argp[idx].type != ARGT_STR) {
916 msg = "string expected";
917 goto error;
918 }
919 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100920 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200921 ul = p->uri_auth->userlist;
922 else
923 ul = auth_find_userlist(argp[idx].data.str.area);
924
925 if (!ul) {
926 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
927 goto error;
928 }
929 argp[idx].type = ARGT_USR;
930 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200931 break;
932
933 case ARGT_STR:
934 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
935 msg = "unable to duplicate string arg";
936 goto error;
937 }
938 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100939 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200940
Christopher Fauletd25d9262020-08-06 11:04:46 +0200941 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200942 msg = "type not yet supported";
943 goto error;
944 break;
945
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100946 }
947
948 /* Check for type of argument. */
949 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200950 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
951 arg_type_names[(mask & ARGT_MASK)],
952 arg_type_names[argp[idx].type & ARGT_MASK]);
953 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100954 }
955
956 /* Next argument. */
957 mask >>= ARGT_BITS;
958 idx++;
959 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200960 return 0;
961
962 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +0200963 free_args(argp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200964 WILL_LJMP(luaL_argerror(L, first + idx, msg));
965 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100966}
967
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100968/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500969 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100970 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100971 *
972 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100973 * - hlua_sethlua : create the association between hlua context and lua_state.
974 */
975static inline struct hlua *hlua_gethlua(lua_State *L)
976{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100977 struct hlua **hlua = lua_getextraspace(L);
978 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100979}
980static inline void hlua_sethlua(struct hlua *hlua)
981{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100982 struct hlua **hlua_store = lua_getextraspace(hlua->T);
983 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100984}
985
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100986/* This function is used to send logs. It try to send on screen (stderr)
987 * and on the default syslog server.
988 */
989static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
990{
991 struct tm tm;
992 char *p;
993
994 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200995 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100996 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200997 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200998 /* Break the message if exceed the buffer size. */
999 *(p-4) = ' ';
1000 *(p-3) = '.';
1001 *(p-2) = '.';
1002 *(p-1) = '.';
1003 break;
1004 }
Willy Tarreau90807112020-02-25 08:16:33 +01001005 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001006 *p = *msg;
1007 else
1008 *p = '.';
1009 }
1010 *p = '\0';
1011
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001012 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001013 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001014 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1015 return;
1016
Willy Tarreaua678b432015-08-28 10:14:59 +02001017 get_localtime(date.tv_sec, &tm);
1018 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001019 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001020 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001021 fflush(stderr);
1022 }
1023}
1024
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001025/* This function just ensure that the yield will be always
1026 * returned with a timeout and permit to set some flags
1027 */
1028__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001029 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001030{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001031 struct hlua *hlua;
1032
1033 /* Get hlua struct, or NULL if we execute from main lua state */
1034 hlua = hlua_gethlua(L);
1035 if (!hlua) {
1036 return;
1037 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001038
1039 /* Set the wake timeout. If timeout is required, we set
1040 * the expiration time.
1041 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001042 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001043
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001044 hlua->flags |= flags;
1045
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001046 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001047 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001048}
1049
Willy Tarreau87b09662015-04-03 00:22:06 +02001050/* This function initialises the Lua environment stored in the stream.
1051 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001052 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001053 *
1054 * This function is particular. it initialises a new Lua thread. If the
1055 * initialisation fails (example: out of memory error), the lua function
1056 * throws an error (longjmp).
1057 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001058 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001059 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001060 * threads appear, the safe environment set a lock to ensure only one
1061 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001062 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001063 *
1064 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001065 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001066 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001067 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001068 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001069 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001070 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001071 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001072int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001073{
1074 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001075 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001076 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001077 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001078 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001079 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001080 if (!already_safe) {
1081 if (!SET_SAFE_LJMP_PARENT(lua)) {
1082 lua->Tref = LUA_REFNIL;
1083 return 0;
1084 }
1085 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001086 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001087 if (!lua->T) {
1088 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001089 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001090 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001091 return 0;
1092 }
1093 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001094 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001095 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001096 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001097 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001098 return 1;
1099}
1100
Willy Tarreau87b09662015-04-03 00:22:06 +02001101/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001102 * is destroyed. The destroy also the memory context. The struct "lua"
1103 * is not freed.
1104 */
1105void hlua_ctx_destroy(struct hlua *lua)
1106{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001107 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001108 return;
1109
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001110 if (!lua->T)
1111 goto end;
1112
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001113 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001114 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001115
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001116 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001117 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001118 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001119 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001120
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001121 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001122 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001123 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001124 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001125 /* Forces a garbage collecting process. If the Lua program is finished
1126 * without error, we run the GC on the thread pointer. Its freed all
1127 * the unused memory.
1128 * If the thread is finnish with an error or is currently yielded,
1129 * it seems that the GC applied on the thread doesn't clean anything,
1130 * so e run the GC on the main thread.
1131 * NOTE: maybe this action locks all the Lua threads untiml the en of
1132 * the garbage collection.
1133 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001134 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001135 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001136 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001137 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001138 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001139 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001140
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001141 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001142
1143end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001144 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001145}
1146
1147/* This function is used to restore the Lua context when a coroutine
1148 * fails. This function copy the common memory between old coroutine
1149 * and the new coroutine. The old coroutine is destroyed, and its
1150 * replaced by the new coroutine.
1151 * If the flag "keep_msg" is set, the last entry of the old is assumed
1152 * as string error message and it is copied in the new stack.
1153 */
1154static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1155{
1156 lua_State *T;
1157 int new_ref;
1158
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001159 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001160 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001161 if (!T)
1162 return 0;
1163
1164 /* Copy last error message. */
1165 if (keep_msg)
1166 lua_xmove(lua->T, T, 1);
1167
1168 /* Copy data between the coroutines. */
1169 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1170 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001171 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172
1173 /* Destroy old data. */
1174 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1175
1176 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001177 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001178
1179 /* Fill the struct with the new coroutine values. */
1180 lua->Mref = new_ref;
1181 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001182 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001183
1184 /* Set context. */
1185 hlua_sethlua(lua);
1186
1187 return 1;
1188}
1189
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001190void hlua_hook(lua_State *L, lua_Debug *ar)
1191{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001192 struct hlua *hlua;
1193
1194 /* Get hlua struct, or NULL if we execute from main lua state */
1195 hlua = hlua_gethlua(L);
1196 if (!hlua)
1197 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001198
1199 /* Lua cannot yield when its returning from a function,
1200 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001201 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001202 */
1203 if (lua_gethookmask(L) & LUA_MASKRET) {
1204 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1205 return;
1206 }
1207
1208 /* restore the interrupt condition. */
1209 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1210
1211 /* If we interrupt the Lua processing in yieldable state, we yield.
1212 * If the state is not yieldable, trying yield causes an error.
1213 */
1214 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001215 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001216
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001217 /* If we cannot yield, update the clock and check the timeout. */
1218 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001219 hlua->run_time += now_ms - hlua->start_time;
1220 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001221 lua_pushfstring(L, "execution timeout");
1222 WILL_LJMP(lua_error(L));
1223 }
1224
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001225 /* Update the start time. */
1226 hlua->start_time = now_ms;
1227
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001228 /* Try to interrupt the process at the end of the current
1229 * unyieldable function.
1230 */
1231 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001232}
1233
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001234/* This function start or resumes the Lua stack execution. If the flag
1235 * "yield_allowed" if no set and the LUA stack execution returns a yield
1236 * The function return an error.
1237 *
1238 * The function can returns 4 values:
1239 * - HLUA_E_OK : The execution is terminated without any errors.
1240 * - HLUA_E_AGAIN : The execution must continue at the next associated
1241 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001242 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001243 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001244 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001245 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001246 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001247 * LUA code.
1248 */
1249static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1250{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001251#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1252 int nres;
1253#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001254 int ret;
1255 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001256 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001257
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001258 /* Initialise run time counter. */
1259 if (!HLUA_IS_RUNNING(lua))
1260 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001261
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001262 /* Lock the whole Lua execution. This lock must be before the
1263 * label "resume_execution".
1264 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001265 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001266 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001267
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001268resume_execution:
1269
1270 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1271 * instructions. it is used for preventing infinite loops.
1272 */
1273 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1274
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001275 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001276 HLUA_SET_RUN(lua);
1277 HLUA_CLR_CTRLYIELD(lua);
1278 HLUA_CLR_WAKERESWR(lua);
1279 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001280
Christopher Fauletbc275a92020-02-26 14:55:16 +01001281 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001282 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001283 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001284
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001285 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001286#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001287 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001288#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001289 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001290#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001291 switch (ret) {
1292
1293 case LUA_OK:
1294 ret = HLUA_E_OK;
1295 break;
1296
1297 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001298 /* Check if the execution timeout is expired. It it is the case, we
1299 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001300 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001301 tv_update_date(0, 1);
1302 lua->run_time += now_ms - lua->start_time;
1303 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001304 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001305 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001306 break;
1307 }
1308 /* Process the forced yield. if the general yield is not allowed or
1309 * if no task were associated this the current Lua execution
1310 * coroutine, we resume the execution. Else we want to return in the
1311 * scheduler and we want to be waked up again, to continue the
1312 * current Lua execution. So we schedule our own task.
1313 */
1314 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001315 if (!yield_allowed || !lua->task)
1316 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001317 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001318 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001319 if (!yield_allowed) {
1320 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001321 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001322 break;
1323 }
1324 ret = HLUA_E_AGAIN;
1325 break;
1326
1327 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001328
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001329 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001330 * because the errors ares the only one mean to return immediately
1331 * from and lua execution.
1332 */
1333 if (lua->flags & HLUA_EXIT) {
1334 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001335 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001336 break;
1337 }
1338
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001339 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001340 if (!lua_checkstack(lua->T, 1)) {
1341 ret = HLUA_E_ERR;
1342 break;
1343 }
1344 msg = lua_tostring(lua->T, -1);
1345 lua_settop(lua->T, 0); /* Empty the stack. */
1346 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001347 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001348 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001349 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001350 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001351 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001352 ret = HLUA_E_ERRMSG;
1353 break;
1354
1355 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001356 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001357 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001358 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001359 break;
1360
1361 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001362 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001363 if (!lua_checkstack(lua->T, 1)) {
1364 ret = HLUA_E_ERR;
1365 break;
1366 }
1367 msg = lua_tostring(lua->T, -1);
1368 lua_settop(lua->T, 0); /* Empty the stack. */
1369 lua_pop(lua->T, 1);
1370 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001371 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001372 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001373 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001374 ret = HLUA_E_ERRMSG;
1375 break;
1376
1377 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001378 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001379 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001380 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001381 break;
1382 }
1383
1384 switch (ret) {
1385 case HLUA_E_AGAIN:
1386 break;
1387
1388 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001389 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001390 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001391 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001392 break;
1393
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001394 case HLUA_E_ETMOUT:
1395 case HLUA_E_NOMEM:
1396 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001397 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001398 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001399 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001400 hlua_ctx_renew(lua, 0);
1401 break;
1402
1403 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001404 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001405 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001406 break;
1407 }
1408
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001409 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001410 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001411 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001412
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001413 return ret;
1414}
1415
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001416/* This function exit the current code. */
1417__LJMP static int hlua_done(lua_State *L)
1418{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001419 struct hlua *hlua;
1420
1421 /* Get hlua struct, or NULL if we execute from main lua state */
1422 hlua = hlua_gethlua(L);
1423 if (!hlua)
1424 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001425
1426 hlua->flags |= HLUA_EXIT;
1427 WILL_LJMP(lua_error(L));
1428
1429 return 0;
1430}
1431
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001432/* This function is an LUA binding. It provides a function
1433 * for deleting ACL from a referenced ACL file.
1434 */
1435__LJMP static int hlua_del_acl(lua_State *L)
1436{
1437 const char *name;
1438 const char *key;
1439 struct pat_ref *ref;
1440
1441 MAY_LJMP(check_args(L, 2, "del_acl"));
1442
1443 name = MAY_LJMP(luaL_checkstring(L, 1));
1444 key = MAY_LJMP(luaL_checkstring(L, 2));
1445
1446 ref = pat_ref_lookup(name);
1447 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001448 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001449
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001450 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001451 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001452 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001453 return 0;
1454}
1455
1456/* This function is an LUA binding. It provides a function
1457 * for deleting map entry from a referenced map file.
1458 */
1459static int hlua_del_map(lua_State *L)
1460{
1461 const char *name;
1462 const char *key;
1463 struct pat_ref *ref;
1464
1465 MAY_LJMP(check_args(L, 2, "del_map"));
1466
1467 name = MAY_LJMP(luaL_checkstring(L, 1));
1468 key = MAY_LJMP(luaL_checkstring(L, 2));
1469
1470 ref = pat_ref_lookup(name);
1471 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001472 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001473
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001474 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001475 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001476 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001477 return 0;
1478}
1479
1480/* This function is an LUA binding. It provides a function
1481 * for adding ACL pattern from a referenced ACL file.
1482 */
1483static int hlua_add_acl(lua_State *L)
1484{
1485 const char *name;
1486 const char *key;
1487 struct pat_ref *ref;
1488
1489 MAY_LJMP(check_args(L, 2, "add_acl"));
1490
1491 name = MAY_LJMP(luaL_checkstring(L, 1));
1492 key = MAY_LJMP(luaL_checkstring(L, 2));
1493
1494 ref = pat_ref_lookup(name);
1495 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001496 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001497
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001498 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001499 if (pat_ref_find_elt(ref, key) == NULL)
1500 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001501 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001502 return 0;
1503}
1504
1505/* This function is an LUA binding. It provides a function
1506 * for setting map pattern and sample from a referenced map
1507 * file.
1508 */
1509static int hlua_set_map(lua_State *L)
1510{
1511 const char *name;
1512 const char *key;
1513 const char *value;
1514 struct pat_ref *ref;
1515
1516 MAY_LJMP(check_args(L, 3, "set_map"));
1517
1518 name = MAY_LJMP(luaL_checkstring(L, 1));
1519 key = MAY_LJMP(luaL_checkstring(L, 2));
1520 value = MAY_LJMP(luaL_checkstring(L, 3));
1521
1522 ref = pat_ref_lookup(name);
1523 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001524 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001525
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001526 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001527 if (pat_ref_find_elt(ref, key) != NULL)
1528 pat_ref_set(ref, key, value, NULL);
1529 else
1530 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001531 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001532 return 0;
1533}
1534
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001535/* A class is a lot of memory that contain data. This data can be a table,
1536 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001537 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001538 * the name of the object (_G[<name>] = <metable> ).
1539 *
1540 * A metable is a table that modify the standard behavior of a standard
1541 * access to the associated data. The entries of this new metatable are
1542 * defined as is:
1543 *
1544 * http://lua-users.org/wiki/MetatableEvents
1545 *
1546 * __index
1547 *
1548 * we access an absent field in a table, the result is nil. This is
1549 * true, but it is not the whole truth. Actually, such access triggers
1550 * the interpreter to look for an __index metamethod: If there is no
1551 * such method, as usually happens, then the access results in nil;
1552 * otherwise, the metamethod will provide the result.
1553 *
1554 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1555 * the key does not appear in the table, but the metatable has an __index
1556 * property:
1557 *
1558 * - if the value is a function, the function is called, passing in the
1559 * table and the key; the return value of that function is returned as
1560 * the result.
1561 *
1562 * - if the value is another table, the value of the key in that table is
1563 * asked for and returned (and if it doesn't exist in that table, but that
1564 * table's metatable has an __index property, then it continues on up)
1565 *
1566 * - Use "rawget(myTable,key)" to skip this metamethod.
1567 *
1568 * http://www.lua.org/pil/13.4.1.html
1569 *
1570 * __newindex
1571 *
1572 * Like __index, but control property assignment.
1573 *
1574 * __mode - Control weak references. A string value with one or both
1575 * of the characters 'k' and 'v' which specifies that the the
1576 * keys and/or values in the table are weak references.
1577 *
1578 * __call - Treat a table like a function. When a table is followed by
1579 * parenthesis such as "myTable( 'foo' )" and the metatable has
1580 * a __call key pointing to a function, that function is invoked
1581 * (passing any specified arguments) and the return value is
1582 * returned.
1583 *
1584 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1585 * called, if the metatable for myTable has a __metatable
1586 * key, the value of that key is returned instead of the
1587 * actual metatable.
1588 *
1589 * __tostring - Control string representation. When the builtin
1590 * "tostring( myTable )" function is called, if the metatable
1591 * for myTable has a __tostring property set to a function,
1592 * that function is invoked (passing myTable to it) and the
1593 * return value is used as the string representation.
1594 *
1595 * __len - Control table length. When the table length is requested using
1596 * the length operator ( '#' ), if the metatable for myTable has
1597 * a __len key pointing to a function, that function is invoked
1598 * (passing myTable to it) and the return value used as the value
1599 * of "#myTable".
1600 *
1601 * __gc - Userdata finalizer code. When userdata is set to be garbage
1602 * collected, if the metatable has a __gc field pointing to a
1603 * function, that function is first invoked, passing the userdata
1604 * to it. The __gc metamethod is not called for tables.
1605 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1606 *
1607 * Special metamethods for redefining standard operators:
1608 * http://www.lua.org/pil/13.1.html
1609 *
1610 * __add "+"
1611 * __sub "-"
1612 * __mul "*"
1613 * __div "/"
1614 * __unm "!"
1615 * __pow "^"
1616 * __concat ".."
1617 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001618 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001619 * http://www.lua.org/pil/13.2.html
1620 *
1621 * __eq "=="
1622 * __lt "<"
1623 * __le "<="
1624 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001625
1626/*
1627 *
1628 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001629 * Class Map
1630 *
1631 *
1632 */
1633
1634/* Returns a struct hlua_map if the stack entry "ud" is
1635 * a class session, otherwise it throws an error.
1636 */
1637__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1638{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001639 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001640}
1641
1642/* This function is the map constructor. It don't need
1643 * the class Map object. It creates and return a new Map
1644 * object. It must be called only during "body" or "init"
1645 * context because it process some filesystem accesses.
1646 */
1647__LJMP static int hlua_map_new(struct lua_State *L)
1648{
1649 const char *fn;
1650 int match = PAT_MATCH_STR;
1651 struct sample_conv conv;
1652 const char *file = "";
1653 int line = 0;
1654 lua_Debug ar;
1655 char *err = NULL;
1656 struct arg args[2];
1657
1658 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1659 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1660
1661 fn = MAY_LJMP(luaL_checkstring(L, 1));
1662
1663 if (lua_gettop(L) >= 2) {
1664 match = MAY_LJMP(luaL_checkinteger(L, 2));
1665 if (match < 0 || match >= PAT_MATCH_NUM)
1666 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1667 }
1668
1669 /* Get Lua filename and line number. */
1670 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1671 lua_getinfo(L, "Sl", &ar); /* get info about it */
1672 if (ar.currentline > 0) { /* is there info? */
1673 file = ar.short_src;
1674 line = ar.currentline;
1675 }
1676 }
1677
1678 /* fill fake sample_conv struct. */
1679 conv.kw = ""; /* unused. */
1680 conv.process = NULL; /* unused. */
1681 conv.arg_mask = 0; /* unused. */
1682 conv.val_args = NULL; /* unused. */
1683 conv.out_type = SMP_T_STR;
1684 conv.private = (void *)(long)match;
1685 switch (match) {
1686 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1687 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1688 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1689 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1690 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1691 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1692 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001693 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001694 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1695 default:
1696 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1697 }
1698
1699 /* fill fake args. */
1700 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001701 args[0].data.str.area = strdup(fn);
1702 args[0].data.str.data = strlen(fn);
1703 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001704 args[1].type = ARGT_STOP;
1705
1706 /* load the map. */
1707 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001708 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001709 * free the err variable.
1710 */
1711 luaL_where(L, 1);
1712 lua_pushfstring(L, "'new': %s.", err);
1713 lua_concat(L, 2);
1714 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001715 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001716 WILL_LJMP(lua_error(L));
1717 }
1718
1719 /* create the lua object. */
1720 lua_newtable(L);
1721 lua_pushlightuserdata(L, args[0].data.map);
1722 lua_rawseti(L, -2, 0);
1723
1724 /* Pop a class Map metatable and affect it to the userdata. */
1725 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1726 lua_setmetatable(L, -2);
1727
1728
1729 return 1;
1730}
1731
1732__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1733{
1734 struct map_descriptor *desc;
1735 struct pattern *pat;
1736 struct sample smp;
1737
1738 MAY_LJMP(check_args(L, 2, "lookup"));
1739 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001740 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001741 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001742 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001743 }
1744 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001745 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001746 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001747 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 +01001748 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001749 }
1750
1751 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001752 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001753 if (str)
1754 lua_pushstring(L, "");
1755 else
1756 lua_pushnil(L);
1757 return 1;
1758 }
1759
1760 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001761 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001762 return 1;
1763}
1764
1765__LJMP static int hlua_map_lookup(struct lua_State *L)
1766{
1767 return _hlua_map_lookup(L, 0);
1768}
1769
1770__LJMP static int hlua_map_slookup(struct lua_State *L)
1771{
1772 return _hlua_map_lookup(L, 1);
1773}
1774
1775/*
1776 *
1777 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001778 * Class Socket
1779 *
1780 *
1781 */
1782
1783__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1784{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001785 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001786}
1787
1788/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001789 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790 * received.
1791 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001792static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001794 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001796 if (appctx->ctx.hlua_cosocket.die) {
1797 si_shutw(si);
1798 si_shutr(si);
1799 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001800 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1801 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001802 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1803 }
1804
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001805 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001806 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001807 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001808
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001809 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001810 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001811 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001812
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001813 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001814 * to be notified whenever the connection completes.
1815 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001816 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001817 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001818 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001819 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001820 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001821 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822
1823 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001824 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001826 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001827 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001828 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829
1830 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001831 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001832 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001833
1834 /* Some data were injected in the buffer, notify the stream
1835 * interface.
1836 */
1837 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001838 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001839
1840 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001841 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001842 */
1843 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001844 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001845}
1846
Willy Tarreau87b09662015-04-03 00:22:06 +02001847/* This function is called when the "struct stream" is destroyed.
1848 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001849 * Wake all the pending signals.
1850 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001851static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001852{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001853 struct xref *peer;
1854
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001856 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1857 if (peer)
1858 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001859
1860 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001861 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1862 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001863}
1864
1865/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001866 * uses this object. If the stream does not exists, just quit.
1867 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868 * pending signal can rest in the read and write lists. destroy
1869 * it.
1870 */
1871__LJMP static int hlua_socket_gc(lua_State *L)
1872{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001873 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001875 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001876
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001877 MAY_LJMP(check_args(L, 1, "__gc"));
1878
1879 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001880 peer = xref_get_peer_and_lock(&socket->xref);
1881 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001882 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001883 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001885 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001886 appctx->ctx.hlua_cosocket.die = 1;
1887 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001889 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001890 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891 return 0;
1892}
1893
1894/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001895 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896 */
sada05ed3302018-05-11 11:48:18 -07001897__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001898{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001899 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001901 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001902 struct hlua *hlua;
1903
1904 /* Get hlua struct, or NULL if we execute from main lua state */
1905 hlua = hlua_gethlua(L);
1906 if (!hlua)
1907 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001909 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001910
1911 /* Check if we run on the same thread than the xreator thread.
1912 * We cannot access to the socket if the thread is different.
1913 */
1914 if (socket->tid != tid)
1915 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1916
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001917 peer = xref_get_peer_and_lock(&socket->xref);
1918 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001919 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001920
1921 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001922 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001924 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001925 appctx->ctx.hlua_cosocket.die = 1;
1926 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001928 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001929 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930 return 0;
1931}
1932
sada05ed3302018-05-11 11:48:18 -07001933/* The close function calls close_helper.
1934 */
1935__LJMP static int hlua_socket_close(lua_State *L)
1936{
1937 MAY_LJMP(check_args(L, 1, "close"));
1938 return hlua_socket_close_helper(L);
1939}
1940
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001941/* This Lua function assumes that the stack contain three parameters.
1942 * 1 - USERDATA containing a struct socket
1943 * 2 - INTEGER with values of the macro defined below
1944 * If the integer is -1, we must read at most one line.
1945 * If the integer is -2, we ust read all the data until the
1946 * end of the stream.
1947 * If the integer is positive value, we must read a number of
1948 * bytes corresponding to this value.
1949 */
1950#define HLSR_READ_LINE (-1)
1951#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001952__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001953{
1954 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1955 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001956 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001958 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001960 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001961 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001962 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001963 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001964 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001965 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001966 struct stream_interface *si;
1967 struct stream *s;
1968 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001969 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001970
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001971 /* Get hlua struct, or NULL if we execute from main lua state */
1972 hlua = hlua_gethlua(L);
1973
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974 /* Check if this lua stack is schedulable. */
1975 if (!hlua || !hlua->task)
1976 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1977 "'frontend', 'backend' or 'task'"));
1978
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001979 /* Check if we run on the same thread than the xreator thread.
1980 * We cannot access to the socket if the thread is different.
1981 */
1982 if (socket->tid != tid)
1983 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1984
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001985 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001986 peer = xref_get_peer_and_lock(&socket->xref);
1987 if (!peer)
1988 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001989 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1990 si = appctx->owner;
1991 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001992
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001993 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001994 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001995 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001996 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997 if (nblk < 0) /* Connection close. */
1998 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001999 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002001
2002 /* remove final \r\n. */
2003 if (nblk == 1) {
2004 if (blk1[len1-1] == '\n') {
2005 len1--;
2006 skip_at_end++;
2007 if (blk1[len1-1] == '\r') {
2008 len1--;
2009 skip_at_end++;
2010 }
2011 }
2012 }
2013 else {
2014 if (blk2[len2-1] == '\n') {
2015 len2--;
2016 skip_at_end++;
2017 if (blk2[len2-1] == '\r') {
2018 len2--;
2019 skip_at_end++;
2020 }
2021 }
2022 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002023 }
2024
2025 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002027 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028 if (nblk < 0) /* Connection close. */
2029 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002030 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002031 goto connection_empty;
2032 }
2033
2034 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002035 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002036 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037 if (nblk < 0) /* Connection close. */
2038 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002039 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002040 goto connection_empty;
2041
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002042 missing_bytes = wanted - socket->b.n;
2043 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002045 len1 = missing_bytes;
2046 } if (nblk == 2 && len1 + len2 > missing_bytes)
2047 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 }
2049
2050 len = len1;
2051
2052 luaL_addlstring(&socket->b, blk1, len1);
2053 if (nblk == 2) {
2054 len += len2;
2055 luaL_addlstring(&socket->b, blk2, len2);
2056 }
2057
2058 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002059 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060
2061 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002062 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063
2064 /* If the pattern reclaim to read all the data
2065 * in the connection, got out.
2066 */
2067 if (wanted == HLSR_READ_ALL)
2068 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002069 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070 goto connection_empty;
2071
2072 /* Return result. */
2073 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002074 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075 return 1;
2076
2077connection_closed:
2078
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002079 xref_unlock(&socket->xref, peer);
2080
2081no_peer:
2082
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083 /* If the buffer containds data. */
2084 if (socket->b.n > 0) {
2085 luaL_pushresult(&socket->b);
2086 return 1;
2087 }
2088 lua_pushnil(L);
2089 lua_pushstring(L, "connection closed.");
2090 return 2;
2091
2092connection_empty:
2093
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002094 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2095 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002096 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002097 }
2098 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002099 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100 return 0;
2101}
2102
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002103/* This Lua function gets two parameters. The first one can be string
2104 * or a number. If the string is "*l", the user requires one line. If
2105 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002106 * If the value is a number, the user require a number of bytes equal
2107 * to the value. The default value is "*l" (a line).
2108 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002109 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002110 * integer takes this values:
2111 * -1 : read a line
2112 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002113 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002114 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002115 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116 * concatenated with the read data.
2117 */
2118__LJMP static int hlua_socket_receive(struct lua_State *L)
2119{
2120 int wanted = HLSR_READ_LINE;
2121 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002122 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002123 char *error;
2124 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002125 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002126
2127 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2128 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2129
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002130 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002131
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002132 /* Check if we run on the same thread than the xreator thread.
2133 * We cannot access to the socket if the thread is different.
2134 */
2135 if (socket->tid != tid)
2136 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2137
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002138 /* check for pattern. */
2139 if (lua_gettop(L) >= 2) {
2140 type = lua_type(L, 2);
2141 if (type == LUA_TSTRING) {
2142 pattern = lua_tostring(L, 2);
2143 if (strcmp(pattern, "*a") == 0)
2144 wanted = HLSR_READ_ALL;
2145 else if (strcmp(pattern, "*l") == 0)
2146 wanted = HLSR_READ_LINE;
2147 else {
2148 wanted = strtoll(pattern, &error, 10);
2149 if (*error != '\0')
2150 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2151 }
2152 }
2153 else if (type == LUA_TNUMBER) {
2154 wanted = lua_tointeger(L, 2);
2155 if (wanted < 0)
2156 WILL_LJMP(luaL_error(L, "Unsupported size."));
2157 }
2158 }
2159
2160 /* Set pattern. */
2161 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002162
2163 /* Check if we would replace the top by itself. */
2164 if (lua_gettop(L) != 2)
2165 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166
Christopher Fauletc31b2002021-05-03 10:11:13 +02002167 /* Save index of the top of the stack because since buffers are used, it
2168 * may change
2169 */
2170 lastarg = lua_gettop(L);
2171
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002172 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173 luaL_buffinit(L, &socket->b);
2174
2175 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002176 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177 if (lua_type(L, 3) != LUA_TSTRING)
2178 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2179 pattern = lua_tolstring(L, 3, &len);
2180 luaL_addlstring(&socket->b, pattern, len);
2181 }
2182
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002183 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002184}
2185
2186/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002187 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002188 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002189static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002190{
2191 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002192 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002193 struct appctx *appctx;
2194 size_t buf_len;
2195 const char *buf;
2196 int len;
2197 int send_len;
2198 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002199 struct xref *peer;
2200 struct stream_interface *si;
2201 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002203 /* Get hlua struct, or NULL if we execute from main lua state */
2204 hlua = hlua_gethlua(L);
2205
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002206 /* Check if this lua stack is schedulable. */
2207 if (!hlua || !hlua->task)
2208 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2209 "'frontend', 'backend' or 'task'"));
2210
2211 /* Get object */
2212 socket = MAY_LJMP(hlua_checksocket(L, 1));
2213 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002214 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002215
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002216 /* Check if we run on the same thread than the xreator thread.
2217 * We cannot access to the socket if the thread is different.
2218 */
2219 if (socket->tid != tid)
2220 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2221
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002222 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002223 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002224 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002225 lua_pushinteger(L, -1);
2226 return 1;
2227 }
2228 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2229 si = appctx->owner;
2230 s = si_strm(si);
2231
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002232 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002233 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002234 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002235 lua_pushinteger(L, -1);
2236 return 1;
2237 }
2238
2239 /* Update the input buffer data. */
2240 buf += sent;
2241 send_len = buf_len - sent;
2242
2243 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002244 if (sent >= buf_len) {
2245 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002246 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002247 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002248
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002249 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002250 * the request buffer if its not required.
2251 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002252 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002253 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002254 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002255 }
2256
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002257 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002258 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002259 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002260 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002261 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262
2263 /* send data */
2264 if (len < send_len)
2265 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002266 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267
2268 /* "Not enough space" (-1), "Buffer too little to contain
2269 * the data" (-2) are not expected because the available length
2270 * is tested.
2271 * Other unknown error are also not expected.
2272 */
2273 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002274 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002275 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002276
sada05ed3302018-05-11 11:48:18 -07002277 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002279 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002280 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281 return 1;
2282 }
2283
2284 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002285 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002286
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002287 s->req.rex = TICK_ETERNITY;
2288 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289
2290 /* Update length sent. */
2291 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002292 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002293
2294 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 if (sent + len >= buf_len) {
2296 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002298 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299
2300hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002301 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2302 xref_unlock(&socket->xref, peer);
2303 WILL_LJMP(luaL_error(L, "out of memory"));
2304 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002305 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002306 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 return 0;
2308}
2309
2310/* This function initiate the send of data. It just check the input
2311 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002312 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002313 * "hlua_socket_write_yield" that can yield.
2314 *
2315 * The Lua function gets between 3 and 4 parameters. The first one is
2316 * the associated object. The second is a string buffer. The third is
2317 * a facultative integer that represents where is the buffer position
2318 * of the start of the data that can send. The first byte is the
2319 * position "1". The default value is "1". The fourth argument is a
2320 * facultative integer that represents where is the buffer position
2321 * of the end of the data that can send. The default is the last byte.
2322 */
2323static int hlua_socket_send(struct lua_State *L)
2324{
2325 int i;
2326 int j;
2327 const char *buf;
2328 size_t buf_len;
2329
2330 /* Check number of arguments. */
2331 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2332 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2333
2334 /* Get the string. */
2335 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2336
2337 /* Get and check j. */
2338 if (lua_gettop(L) == 4) {
2339 j = MAY_LJMP(luaL_checkinteger(L, 4));
2340 if (j < 0)
2341 j = buf_len + j + 1;
2342 if (j > buf_len)
2343 j = buf_len + 1;
2344 lua_pop(L, 1);
2345 }
2346 else
2347 j = buf_len;
2348
2349 /* Get and check i. */
2350 if (lua_gettop(L) == 3) {
2351 i = MAY_LJMP(luaL_checkinteger(L, 3));
2352 if (i < 0)
2353 i = buf_len + i + 1;
2354 if (i > buf_len)
2355 i = buf_len + 1;
2356 lua_pop(L, 1);
2357 } else
2358 i = 1;
2359
2360 /* Check bth i and j. */
2361 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002362 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002363 return 1;
2364 }
2365 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002366 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367 return 1;
2368 }
2369 if (i == 0)
2370 i = 1;
2371 if (j == 0)
2372 j = 1;
2373
2374 /* Pop the string. */
2375 lua_pop(L, 1);
2376
2377 /* Update the buffer length. */
2378 buf += i - 1;
2379 buf_len = j - i + 1;
2380 lua_pushlstring(L, buf, buf_len);
2381
2382 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002383 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002384
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002385 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002386}
2387
Willy Tarreau22b0a682015-06-17 19:43:49 +02002388#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2390{
2391 static char buffer[SOCKET_INFO_MAX_LEN];
2392 int ret;
2393 int len;
2394 char *p;
2395
2396 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2397 if (ret <= 0) {
2398 lua_pushnil(L);
2399 return 1;
2400 }
2401
2402 if (ret == AF_UNIX) {
2403 lua_pushstring(L, buffer+1);
2404 return 1;
2405 }
2406 else if (ret == AF_INET6) {
2407 buffer[0] = '[';
2408 len = strlen(buffer);
2409 buffer[len] = ']';
2410 len++;
2411 buffer[len] = ':';
2412 len++;
2413 p = buffer;
2414 }
2415 else if (ret == AF_INET) {
2416 p = buffer + 1;
2417 len = strlen(p);
2418 p[len] = ':';
2419 len++;
2420 }
2421 else {
2422 lua_pushnil(L);
2423 return 1;
2424 }
2425
2426 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2427 lua_pushnil(L);
2428 return 1;
2429 }
2430
2431 lua_pushstring(L, p);
2432 return 1;
2433}
2434
2435/* Returns information about the peer of the connection. */
2436__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2437{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002438 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002439 struct xref *peer;
2440 struct appctx *appctx;
2441 struct stream_interface *si;
2442 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002444
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445 MAY_LJMP(check_args(L, 1, "getpeername"));
2446
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002447 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002448
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002449 /* Check if we run on the same thread than the xreator thread.
2450 * We cannot access to the socket if the thread is different.
2451 */
2452 if (socket->tid != tid)
2453 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2454
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002455 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002456 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002457 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002458 lua_pushnil(L);
2459 return 1;
2460 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002461 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2462 si = appctx->owner;
2463 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002464
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002465 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002466 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002467 lua_pushnil(L);
2468 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002469 }
2470
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002471 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002472 xref_unlock(&socket->xref, peer);
2473 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002474}
2475
2476/* Returns information about my connection side. */
2477static int hlua_socket_getsockname(struct lua_State *L)
2478{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002479 struct hlua_socket *socket;
2480 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002481 struct appctx *appctx;
2482 struct xref *peer;
2483 struct stream_interface *si;
2484 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002485 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002486
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002487 MAY_LJMP(check_args(L, 1, "getsockname"));
2488
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002489 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002490
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002491 /* Check if we run on the same thread than the xreator thread.
2492 * We cannot access to the socket if the thread is different.
2493 */
2494 if (socket->tid != tid)
2495 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2496
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002497 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002498 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002499 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500 lua_pushnil(L);
2501 return 1;
2502 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002503 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2504 si = appctx->owner;
2505 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002506
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002507 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002508 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002509 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002510 lua_pushnil(L);
2511 return 1;
2512 }
2513
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002514 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002515 xref_unlock(&socket->xref, peer);
2516 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002517}
2518
2519/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002520static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002521 .obj_type = OBJ_TYPE_APPLET,
2522 .name = "<LUA_TCP>",
2523 .fct = hlua_socket_handler,
2524 .release = hlua_socket_release,
2525};
2526
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002527__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002528{
2529 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002530 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002531 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002532 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002533 struct stream_interface *si;
2534 struct stream *s;
2535
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002536 /* Get hlua struct, or NULL if we execute from main lua state */
2537 hlua = hlua_gethlua(L);
2538 if (!hlua)
2539 return 0;
2540
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002541 /* Check if we run on the same thread than the xreator thread.
2542 * We cannot access to the socket if the thread is different.
2543 */
2544 if (socket->tid != tid)
2545 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2546
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002547 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002548 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002549 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002550 lua_pushnil(L);
2551 lua_pushstring(L, "Can't connect");
2552 return 2;
2553 }
2554 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2555 si = appctx->owner;
2556 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002557
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002558 /* Check if we run on the same thread than the xreator thread.
2559 * We cannot access to the socket if the thread is different.
2560 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002561 if (socket->tid != tid) {
2562 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002563 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002564 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002565
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002566 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002567 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002568 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002569 lua_pushnil(L);
2570 lua_pushstring(L, "Can't connect");
2571 return 2;
2572 }
2573
Willy Tarreaue09101e2018-10-16 17:37:12 +02002574 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002575
2576 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002577 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002578 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002579 lua_pushinteger(L, 1);
2580 return 1;
2581 }
2582
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002583 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2584 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002585 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002586 }
2587 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002588 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002589 return 0;
2590}
2591
2592/* This function fail or initite the connection. */
2593__LJMP static int hlua_socket_connect(struct lua_State *L)
2594{
2595 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002596 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002597 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002598 struct hlua *hlua;
2599 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002600 int low, high;
2601 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002602 struct xref *peer;
2603 struct stream_interface *si;
2604 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002606 if (lua_gettop(L) < 2)
2607 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002608
2609 /* Get args. */
2610 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002611
2612 /* Check if we run on the same thread than the xreator thread.
2613 * We cannot access to the socket if the thread is different.
2614 */
2615 if (socket->tid != tid)
2616 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2617
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002619 if (lua_gettop(L) >= 3) {
2620 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002621 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622
Tim Duesterhus6edab862018-01-06 19:04:45 +01002623 /* Force the ip to end with a colon, to support IPv6 addresses
2624 * that are not enclosed within square brackets.
2625 */
2626 if (port > 0) {
2627 luaL_buffinit(L, &b);
2628 luaL_addstring(&b, ip);
2629 luaL_addchar(&b, ':');
2630 luaL_pushresult(&b);
2631 ip = lua_tolstring(L, lua_gettop(L), NULL);
2632 }
2633 }
2634
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002635 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002636 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002637 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002638 lua_pushnil(L);
2639 return 1;
2640 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641
2642 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002643 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 +02002644 if (!addr) {
2645 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002646 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002647 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002648
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002649 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002650 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002651 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002652 if (port == -1) {
2653 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002654 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002655 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002656 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2657 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002658 if (port == -1) {
2659 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002660 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002661 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002662 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002663 }
2664 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002665
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002666 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2667 si = appctx->owner;
2668 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002669
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002670 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002671 xref_unlock(&socket->xref, peer);
2672 WILL_LJMP(luaL_error(L, "connect: internal error"));
2673 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002674 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002675
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002676 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002677 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002678 if (!hlua)
2679 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002680
2681 /* inform the stream that we want to be notified whenever the
2682 * connection completes.
2683 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002684 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002685 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002686 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002687
Willy Tarreauf31af932020-01-14 09:59:38 +01002688 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002689
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002690 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2691 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002692 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002693 }
2694 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002695
2696 task_wakeup(s->task, TASK_WOKEN_INIT);
2697 /* Return yield waiting for connection. */
2698
Willy Tarreau9635e032018-10-16 17:52:55 +02002699 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002700
2701 return 0;
2702}
2703
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002704#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002705__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2706{
2707 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002708 struct xref *peer;
2709 struct appctx *appctx;
2710 struct stream_interface *si;
2711 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002712
2713 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2714 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002715
2716 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002717 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002718 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002719 lua_pushnil(L);
2720 return 1;
2721 }
2722 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2723 si = appctx->owner;
2724 s = si_strm(si);
2725
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002726 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002727 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002728 return MAY_LJMP(hlua_socket_connect(L));
2729}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002730#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002731
2732__LJMP static int hlua_socket_setoption(struct lua_State *L)
2733{
2734 return 0;
2735}
2736
2737__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2738{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002739 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002740 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002741 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002742 struct xref *peer;
2743 struct appctx *appctx;
2744 struct stream_interface *si;
2745 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002746
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002747 MAY_LJMP(check_args(L, 2, "settimeout"));
2748
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002749 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002750
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002751 /* convert the timeout to millis */
2752 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002753
Thierry Fournier17a921b2018-03-08 09:59:02 +01002754 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002755 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002756 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2757
Mark Lakes56cc1252018-03-27 09:48:06 +02002758 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002759 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002760
2761 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002762 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002763 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002764
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002765 /* Check if we run on the same thread than the xreator thread.
2766 * We cannot access to the socket if the thread is different.
2767 */
2768 if (socket->tid != tid)
2769 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2770
Mark Lakes56cc1252018-03-27 09:48:06 +02002771 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002772 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002773 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002774 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2775 WILL_LJMP(lua_error(L));
2776 return 0;
2777 }
2778 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2779 si = appctx->owner;
2780 s = si_strm(si);
2781
Cyril Bonté7bb63452018-08-17 23:51:02 +02002782 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002783 s->req.rto = tmout;
2784 s->req.wto = tmout;
2785 s->res.rto = tmout;
2786 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002787 s->req.rex = tick_add_ifset(now_ms, tmout);
2788 s->req.wex = tick_add_ifset(now_ms, tmout);
2789 s->res.rex = tick_add_ifset(now_ms, tmout);
2790 s->res.wex = tick_add_ifset(now_ms, tmout);
2791
2792 s->task->expire = tick_add_ifset(now_ms, tmout);
2793 task_queue(s->task);
2794
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002795 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002796
Thierry Fourniere9636f12018-03-08 09:54:32 +01002797 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002798 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002799}
2800
2801__LJMP static int hlua_socket_new(lua_State *L)
2802{
2803 struct hlua_socket *socket;
2804 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002805 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002806 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002807
2808 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002809 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002810 hlua_pusherror(L, "socket: full stack");
2811 goto out_fail_conf;
2812 }
2813
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002814 /* Create the object: obj[0] = userdata. */
2815 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002816 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002817 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002818 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002819 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002820
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002821 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002822 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002823 hlua_pusherror(L, "socket: uninitialized pools.");
2824 goto out_fail_conf;
2825 }
2826
Willy Tarreau87b09662015-04-03 00:22:06 +02002827 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002828 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2829 lua_setmetatable(L, -2);
2830
Willy Tarreaud420a972015-04-06 00:39:18 +02002831 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002832 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002833 if (!appctx) {
2834 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002835 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002836 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002837
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002838 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002839 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002840 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2841 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002842
Willy Tarreaud420a972015-04-06 00:39:18 +02002843 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002844 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002845 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002846 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002847 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002848 }
2849
Christopher Faulet26256f82020-09-14 11:40:13 +02002850 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002851 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002852 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002853 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002854 }
2855
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002856 /* Initialise cross reference between stream and Lua socket object. */
2857 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002858
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002859 /* Configure "right" stream interface. this "si" is used to connect
2860 * and retrieve data from the server. The connection is initialized
2861 * with the "struct server".
2862 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002863 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002864
2865 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002866 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002867 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002868
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002869 return 1;
2870
Willy Tarreaud420a972015-04-06 00:39:18 +02002871 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002872 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002873 out_fail_sess:
2874 appctx_free(appctx);
2875 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002876 WILL_LJMP(lua_error(L));
2877 return 0;
2878}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002879
2880/*
2881 *
2882 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002883 * Class Channel
2884 *
2885 *
2886 */
2887
2888/* Returns the struct hlua_channel join to the class channel in the
2889 * stack entry "ud" or throws an argument error.
2890 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002891__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002893 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894}
2895
Willy Tarreau47860ed2015-03-10 14:07:50 +01002896/* Pushes the channel onto the top of the stack. If the stask does not have a
2897 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002898 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002899static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002902 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002903 return 0;
2904
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002905 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002906 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002907 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908
2909 /* Pop a class sesison metatable and affect it to the userdata. */
2910 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2911 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 return 1;
2913}
2914
2915/* Duplicate all the data present in the input channel and put it
2916 * in a string LUA variables. Returns -1 and push a nil value in
2917 * the stack if the channel is closed and all the data are consumed,
2918 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002919 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002921static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922{
2923 char *blk1;
2924 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002925 size_t len1;
2926 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 int ret;
2928 luaL_Buffer b;
2929
Willy Tarreau06d80a92017-10-19 14:32:15 +02002930 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931 if (unlikely(ret == 0))
2932 return 0;
2933
2934 if (unlikely(ret < 0)) {
2935 lua_pushnil(L);
2936 return -1;
2937 }
2938
2939 luaL_buffinit(L, &b);
2940 luaL_addlstring(&b, blk1, len1);
2941 if (unlikely(ret == 2))
2942 luaL_addlstring(&b, blk2, len2);
2943 luaL_pushresult(&b);
2944
2945 if (unlikely(ret == 2))
2946 return len1 + len2;
2947 return len1;
2948}
2949
2950/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2951 * a yield. This function keep the data in the buffer.
2952 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002953__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002954{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002955 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002956
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002957 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2958
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01002959 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02002960 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002961 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002962 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002963
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002965 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002966 return 1;
2967}
2968
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002969/* Check arguments for the function "hlua_channel_dup_yield". */
2970__LJMP static int hlua_channel_dup(lua_State *L)
2971{
2972 MAY_LJMP(check_args(L, 1, "dup"));
2973 MAY_LJMP(hlua_checkchannel(L, 1));
2974 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2975}
2976
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2978 * a yield. This function consumes the data in the buffer. It returns
2979 * a string containing the data or a nil pointer if no data are available
2980 * and the channel is closed.
2981 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002982__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002984 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002985 int ret;
2986
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002987 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002988
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01002989 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02002990 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002991 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002992 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002993
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002994 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002995 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002996 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002997
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 if (unlikely(ret == -1))
2999 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003001 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002 return 1;
3003}
3004
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003005/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003006__LJMP static int hlua_channel_get(lua_State *L)
3007{
3008 MAY_LJMP(check_args(L, 1, "get"));
3009 MAY_LJMP(hlua_checkchannel(L, 1));
3010 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3011}
3012
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003013/* This functions consumes and returns one line. If the channel is closed,
3014 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003015 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016 * value.
3017 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003018__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003019{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003020 char *blk1;
3021 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003022 size_t len1;
3023 size_t len2;
3024 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003025 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 int ret;
3027 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003028
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003029 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3030
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003031 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003032 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003033 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003034 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003035
Willy Tarreau06d80a92017-10-19 14:32:15 +02003036 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02003038 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003039
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003040 if (ret == -1) {
3041 lua_pushnil(L);
3042 return 1;
3043 }
3044
3045 luaL_buffinit(L, &b);
3046 luaL_addlstring(&b, blk1, len1);
3047 len = len1;
3048 if (unlikely(ret == 2)) {
3049 luaL_addlstring(&b, blk2, len2);
3050 len += len2;
3051 }
3052 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003053 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054 return 1;
3055}
3056
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003057/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003058__LJMP static int hlua_channel_getline(lua_State *L)
3059{
3060 MAY_LJMP(check_args(L, 1, "getline"));
3061 MAY_LJMP(hlua_checkchannel(L, 1));
3062 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3063}
3064
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065/* This function takes a string as input, and append it at the
3066 * input side of channel. If the data is too big, but a space
3067 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003068 * yields. If the data is bigger than the buffer, or if the
3069 * channel is closed, it returns -1. Otherwise, it returns the
3070 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003072__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003074 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003075 size_t len;
3076 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3077 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3078 int ret;
3079 int max;
3080
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003081 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003082 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003083 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003084 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003085
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003086 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003087 * the request buffer if its not required.
3088 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003089 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003090 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003091 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003092 }
3093
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003094 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003095 if (max > len - l)
3096 max = len - l;
3097
Willy Tarreau06d80a92017-10-19 14:32:15 +02003098 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003099 if (ret == -2 || ret == -3) {
3100 lua_pushinteger(L, -1);
3101 return 1;
3102 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003103 if (ret == -1) {
3104 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003105 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003106 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107 l += ret;
3108 lua_pop(L, 1);
3109 lua_pushinteger(L, l);
3110
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003111 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003112 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003113 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003114 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003115 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003116 */
3117 return 1;
3118 }
3119 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003120 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003121 return 1;
3122}
3123
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003124/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3125 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003126 * buffer size is too little for the data.
3127 */
3128__LJMP static int hlua_channel_append(lua_State *L)
3129{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003130 size_t len;
3131
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003132 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003133 MAY_LJMP(hlua_checkchannel(L, 1));
3134 MAY_LJMP(luaL_checklstring(L, 2, &len));
3135 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003136 lua_pushinteger(L, 0);
3137
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003138 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003139}
3140
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003141/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003142 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003143 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003144 * or -1 if the channel is closed or if the buffer size is too
3145 * little for the data.
3146 */
3147__LJMP static int hlua_channel_set(lua_State *L)
3148{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003149 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003150
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003151 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003152 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003153 lua_pushinteger(L, 0);
3154
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003155 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003156 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003157 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003158 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003159
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003160 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003161
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003162 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003163}
3164
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003165/* Append data in the output side of the buffer. This data is immediately
3166 * sent. The function returns the amount of data written. If the buffer
3167 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003168 * if the channel is closed.
3169 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003170__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003171{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003172 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003173 size_t len;
3174 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3175 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3176 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003177 struct hlua *hlua;
3178
3179 /* Get hlua struct, or NULL if we execute from main lua state */
3180 hlua = hlua_gethlua(L);
3181 if (!hlua) {
3182 lua_pushnil(L);
3183 return 1;
3184 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003185
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003186 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003187 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003188 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003189 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003190
Willy Tarreau47860ed2015-03-10 14:07:50 +01003191 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003192 lua_pushinteger(L, -1);
3193 return 1;
3194 }
3195
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003196 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003197 * the request buffer if its not required.
3198 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003199 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003200 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003201 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003202 }
3203
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003204 /* The written data will be immediately sent, so we can check
3205 * the available space without taking in account the reserve.
3206 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003207 * data, because the buffer will be flushed.
3208 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003209 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003210
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003211 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003212 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003213 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003214 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003215 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003216 return 1;
3217
3218 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003219 if (max > len - l)
3220 max = len - l;
3221
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003222 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003223 * detects a non contiguous buffer and realign it.
3224 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003225 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003226 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003227
3228 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003229 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003230
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003231 /* buffer replace considers that the input part is filled.
3232 * so, I must forward these new data in the output part.
3233 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003234 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003235
3236 l += max;
3237 lua_pop(L, 1);
3238 lua_pushinteger(L, l);
3239
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003240 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003241 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003242 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003243 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003244 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003245 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003246 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003247
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003248 if (l < len) {
3249 /* If we are waiting for space in the response buffer, we
3250 * must set the flag WAKERESWR. This flag required the task
3251 * wake up if any activity is detected on the response buffer.
3252 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003253 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003254 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003255 else
3256 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003257 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003258 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003259
3260 return 1;
3261}
3262
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003263/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003264 * yield the LUA process, and resume it without checking the
3265 * input arguments.
3266 */
3267__LJMP static int hlua_channel_send(lua_State *L)
3268{
3269 MAY_LJMP(check_args(L, 2, "send"));
3270 lua_pushinteger(L, 0);
3271
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003272 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003273}
3274
3275/* This function forward and amount of butes. The data pass from
3276 * the input side of the buffer to the output side, and can be
3277 * forwarded. This function never fails.
3278 *
3279 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003280 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003281 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003282__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003283{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003284 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003285 int len;
3286 int l;
3287 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003288 struct hlua *hlua;
3289
3290 /* Get hlua struct, or NULL if we execute from main lua state */
3291 hlua = hlua_gethlua(L);
3292 if (!hlua)
3293 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003294
3295 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003296
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003297 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003298 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003299 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003300 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003301
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003302 len = MAY_LJMP(luaL_checkinteger(L, 2));
3303 l = MAY_LJMP(luaL_checkinteger(L, -1));
3304
3305 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003306 if (max > ci_data(chn))
3307 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003308 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003309 l += max;
3310
3311 lua_pop(L, 1);
3312 lua_pushinteger(L, l);
3313
3314 /* Check if it miss bytes to forward. */
3315 if (l < len) {
3316 /* The the input channel or the output channel are closed, we
3317 * must return the amount of data forwarded.
3318 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003319 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003320 return 1;
3321
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003322 /* If we are waiting for space data in the response buffer, we
3323 * must set the flag WAKERESWR. This flag required the task
3324 * wake up if any activity is detected on the response buffer.
3325 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003326 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003327 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003328 else
3329 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003330
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003331 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003332 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003333 }
3334
3335 return 1;
3336}
3337
3338/* Just check the input and prepare the stack for the previous
3339 * function "hlua_channel_forward_yield"
3340 */
3341__LJMP static int hlua_channel_forward(lua_State *L)
3342{
3343 MAY_LJMP(check_args(L, 2, "forward"));
3344 MAY_LJMP(hlua_checkchannel(L, 1));
3345 MAY_LJMP(luaL_checkinteger(L, 2));
3346
3347 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003348 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003349}
3350
3351/* Just returns the number of bytes available in the input
3352 * side of the buffer. This function never fails.
3353 */
3354__LJMP static int hlua_channel_get_in_len(lua_State *L)
3355{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003356 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003357
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003358 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003359 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003360 if (IS_HTX_STRM(chn_strm(chn))) {
3361 struct htx *htx = htxbuf(&chn->buf);
3362 lua_pushinteger(L, htx->data - co_data(chn));
3363 }
3364 else
3365 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003366 return 1;
3367}
3368
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003369/* Returns true if the channel is full. */
3370__LJMP static int hlua_channel_is_full(lua_State *L)
3371{
3372 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003373
3374 MAY_LJMP(check_args(L, 1, "is_full"));
3375 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003376 /* ignore the reserve, we are not on a producer side (ie in an
3377 * applet).
3378 */
3379 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003380 return 1;
3381}
3382
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003383/* Returns true if the channel is the response channel. */
3384__LJMP static int hlua_channel_is_resp(lua_State *L)
3385{
3386 struct channel *chn;
3387
3388 MAY_LJMP(check_args(L, 1, "is_resp"));
3389 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3390
3391 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3392 return 1;
3393}
3394
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003395/* Just returns the number of bytes available in the output
3396 * side of the buffer. This function never fails.
3397 */
3398__LJMP static int hlua_channel_get_out_len(lua_State *L)
3399{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003400 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003401
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003402 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003403 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003404 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003405 return 1;
3406}
3407
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003408/*
3409 *
3410 *
3411 * Class Fetches
3412 *
3413 *
3414 */
3415
3416/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003417 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003418 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003419__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003420{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003421 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003422}
3423
3424/* This function creates and push in the stack a fetch object according
3425 * with a current TXN.
3426 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003427static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003428{
Willy Tarreau7073c472015-04-06 11:15:40 +02003429 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003430
3431 /* Check stack size. */
3432 if (!lua_checkstack(L, 3))
3433 return 0;
3434
3435 /* Create the object: obj[0] = userdata.
3436 * Note that the base of the Fetches object is the
3437 * transaction object.
3438 */
3439 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003440 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003441 lua_rawseti(L, -2, 0);
3442
Willy Tarreau7073c472015-04-06 11:15:40 +02003443 hsmp->s = txn->s;
3444 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003445 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003446 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003447
3448 /* Pop a class sesison metatable and affect it to the userdata. */
3449 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3450 lua_setmetatable(L, -2);
3451
3452 return 1;
3453}
3454
3455/* This function is an LUA binding. It is called with each sample-fetch.
3456 * It uses closure argument to store the associated sample-fetch. It
3457 * returns only one argument or throws an error. An error is thrown
3458 * only if an error is encountered during the argument parsing. If
3459 * the "sample-fetch" function fails, nil is returned.
3460 */
3461__LJMP static int hlua_run_sample_fetch(lua_State *L)
3462{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003463 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003464 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003465 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003466 int i;
3467 struct sample smp;
3468
3469 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003470 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003471
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003472 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003473 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003474
Thierry FOURNIERca988662015-12-20 18:43:03 +01003475 /* Check execution authorization. */
3476 if (f->use & SMP_USE_HTTP_ANY &&
3477 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3478 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3479 "is not available in Lua services", f->kw);
3480 WILL_LJMP(lua_error(L));
3481 }
3482
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003483 /* Get extra arguments. */
3484 for (i = 0; i < lua_gettop(L) - 1; i++) {
3485 if (i >= ARGM_NBARGS)
3486 break;
3487 hlua_lua2arg(L, i + 2, &args[i]);
3488 }
3489 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003490 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003491
3492 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003493 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003494
3495 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003496 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003497 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003498 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003499 }
3500
3501 /* Initialise the sample. */
3502 memset(&smp, 0, sizeof(smp));
3503
3504 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003505 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003506 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003507 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003508 lua_pushstring(L, "");
3509 else
3510 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003511 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003512 }
3513
3514 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003515 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003516 hlua_smp2lua_str(L, &smp);
3517 else
3518 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003519
3520 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003521 free_args(args);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003522 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003523
3524 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003525 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003526 WILL_LJMP(lua_error(L));
3527 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003528}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003529
3530/*
3531 *
3532 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003533 * Class Converters
3534 *
3535 *
3536 */
3537
3538/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003539 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003540 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003541__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003542{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003543 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003544}
3545
3546/* This function creates and push in the stack a Converters object
3547 * according with a current TXN.
3548 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003549static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003550{
Willy Tarreau7073c472015-04-06 11:15:40 +02003551 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003552
3553 /* Check stack size. */
3554 if (!lua_checkstack(L, 3))
3555 return 0;
3556
3557 /* Create the object: obj[0] = userdata.
3558 * Note that the base of the Converters object is the
3559 * same than the TXN object.
3560 */
3561 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003562 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003563 lua_rawseti(L, -2, 0);
3564
Willy Tarreau7073c472015-04-06 11:15:40 +02003565 hsmp->s = txn->s;
3566 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003567 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003568 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003569
Willy Tarreau87b09662015-04-03 00:22:06 +02003570 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003571 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3572 lua_setmetatable(L, -2);
3573
3574 return 1;
3575}
3576
3577/* This function is an LUA binding. It is called with each converter.
3578 * It uses closure argument to store the associated converter. It
3579 * returns only one argument or throws an error. An error is thrown
3580 * only if an error is encountered during the argument parsing. If
3581 * the converter function function fails, nil is returned.
3582 */
3583__LJMP static int hlua_run_sample_conv(lua_State *L)
3584{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003585 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003586 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003587 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003588 int i;
3589 struct sample smp;
3590
3591 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003592 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003593
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003594 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003595 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003596
3597 /* Get extra arguments. */
3598 for (i = 0; i < lua_gettop(L) - 2; i++) {
3599 if (i >= ARGM_NBARGS)
3600 break;
3601 hlua_lua2arg(L, i + 3, &args[i]);
3602 }
3603 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003604 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003605
3606 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003607 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003608
3609 /* Run the special args checker. */
3610 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3611 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003612 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003613 }
3614
3615 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003616 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003617 if (!hlua_lua2smp(L, 2, &smp)) {
3618 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003619 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003620 }
3621
Willy Tarreau1777ea62016-03-10 16:15:46 +01003622 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3623
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003624 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003625 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003626 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003627 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003628 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003629 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003630 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3631 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003632 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003633 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003634 }
3635
3636 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003637 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003638 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003639 lua_pushstring(L, "");
3640 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003641 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003642 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003643 }
3644
3645 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003646 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003647 hlua_smp2lua_str(L, &smp);
3648 else
3649 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003650 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003651 free_args(args);
Willy Tarreaua678b432015-08-28 10:14:59 +02003652 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003653
3654 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003655 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003656 WILL_LJMP(lua_error(L));
3657 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003658}
3659
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003660/*
3661 *
3662 *
3663 * Class AppletTCP
3664 *
3665 *
3666 */
3667
3668/* Returns a struct hlua_txn if the stack entry "ud" is
3669 * a class stream, otherwise it throws an error.
3670 */
3671__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3672{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003673 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003674}
3675
3676/* This function creates and push in the stack an Applet object
3677 * according with a current TXN.
3678 */
3679static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3680{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003681 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003682 struct stream_interface *si = ctx->owner;
3683 struct stream *s = si_strm(si);
3684 struct proxy *p = s->be;
3685
3686 /* Check stack size. */
3687 if (!lua_checkstack(L, 3))
3688 return 0;
3689
3690 /* Create the object: obj[0] = userdata.
3691 * Note that the base of the Converters object is the
3692 * same than the TXN object.
3693 */
3694 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003695 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003696 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003697 luactx->appctx = ctx;
3698 luactx->htxn.s = s;
3699 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003700
3701 /* Create the "f" field that contains a list of fetches. */
3702 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003703 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003704 return 0;
3705 lua_settable(L, -3);
3706
3707 /* Create the "sf" field that contains a list of stringsafe fetches. */
3708 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003709 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003710 return 0;
3711 lua_settable(L, -3);
3712
3713 /* Create the "c" field that contains a list of converters. */
3714 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003715 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716 return 0;
3717 lua_settable(L, -3);
3718
3719 /* Create the "sc" field that contains a list of stringsafe converters. */
3720 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003721 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003722 return 0;
3723 lua_settable(L, -3);
3724
3725 /* Pop a class stream metatable and affect it to the table. */
3726 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3727 lua_setmetatable(L, -2);
3728
3729 return 1;
3730}
3731
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003732__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3733{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003734 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003735 struct stream *s;
3736 const char *name;
3737 size_t len;
3738 struct sample smp;
3739
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003740 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3741 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003742
3743 /* It is useles to retrieve the stream, but this function
3744 * runs only in a stream context.
3745 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003746 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003747 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003748 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003749
3750 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003751 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003752 hlua_lua2smp(L, 3, &smp);
3753
3754 /* Store the sample in a variable. */
3755 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003756
3757 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3758 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3759 else
3760 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3761
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003762 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003763}
3764
3765__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3766{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003767 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003768 struct stream *s;
3769 const char *name;
3770 size_t len;
3771 struct sample smp;
3772
3773 MAY_LJMP(check_args(L, 2, "unset_var"));
3774
3775 /* It is useles to retrieve the stream, but this function
3776 * runs only in a stream context.
3777 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003778 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003779 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003780 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003781
3782 /* Unset the variable. */
3783 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003784 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3785 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003786}
3787
3788__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3789{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003790 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003791 struct stream *s;
3792 const char *name;
3793 size_t len;
3794 struct sample smp;
3795
3796 MAY_LJMP(check_args(L, 2, "get_var"));
3797
3798 /* It is useles to retrieve the stream, but this function
3799 * runs only in a stream context.
3800 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003801 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003802 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02003803 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003804
3805 smp_set_owner(&smp, s->be, s->sess, s, 0);
3806 if (!vars_get_by_name(name, len, &smp)) {
3807 lua_pushnil(L);
3808 return 1;
3809 }
3810
3811 return hlua_smp2lua(L, &smp);
3812}
3813
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003814__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3815{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003816 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3817 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003818 struct hlua *hlua;
3819
3820 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003821 if (!s->hlua)
3822 return 0;
3823 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003824
3825 MAY_LJMP(check_args(L, 2, "set_priv"));
3826
3827 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003828 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003829
3830 /* Get and store new value. */
3831 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3832 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3833
3834 return 0;
3835}
3836
3837__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3838{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003839 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3840 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003841 struct hlua *hlua;
3842
3843 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003844 if (!s->hlua) {
3845 lua_pushnil(L);
3846 return 1;
3847 }
3848 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003849
3850 /* Push configuration index in the stack. */
3851 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3852
3853 return 1;
3854}
3855
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003856/* If expected data not yet available, it returns a yield. This function
3857 * consumes the data in the buffer. It returns a string containing the
3858 * data. This string can be empty.
3859 */
3860__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3861{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003862 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3863 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003864 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003865 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003866 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003867 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003868 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003869
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003870 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003871 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003872
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003873 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003874 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003875 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003876 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003877 }
3878
3879 /* End of data: commit the total strings and return. */
3880 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003881 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003882 return 1;
3883 }
3884
3885 /* Ensure that the block 2 length is usable. */
3886 if (ret == 1)
3887 len2 = 0;
3888
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003889 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003890 luaL_addlstring(&luactx->b, blk1, len1);
3891 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003892
3893 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003894 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003895 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003896 return 1;
3897}
3898
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003899/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003900__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3901{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003902 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003903
3904 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003905 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003906
3907 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3908}
3909
3910/* 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_recv_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;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003918 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003919 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003920 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003921 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003922 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003923 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003924
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003925 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003926 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003927
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003928 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003929 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003930 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003931 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003932 }
3933
3934 /* End of data: commit the total strings and return. */
3935 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02003936 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003937 return 1;
3938 }
3939
3940 /* Ensure that the block 2 length is usable. */
3941 if (ret == 1)
3942 len2 = 0;
3943
3944 if (len == -1) {
3945
3946 /* If len == -1, catenate all the data avalaile and
3947 * yield because we want to get all the data until
3948 * the end of data stream.
3949 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003950 luaL_addlstring(&luactx->b, blk1, len1);
3951 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003952 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003953 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003954 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003955
3956 } else {
3957
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003958 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003959 if (len1 > len)
3960 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02003961 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003962 len -= len1;
3963
3964 /* Copy the second block. */
3965 if (len2 > len)
3966 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02003967 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003968 len -= len2;
3969
3970 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003971 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003972
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003973 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003974 if (len > 0) {
3975 lua_pushinteger(L, len);
3976 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003977 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003978 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003979 }
3980
3981 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02003982 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003983 return 1;
3984 }
3985
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003986 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003987 hlua_pusherror(L, "Lua: internal error");
3988 WILL_LJMP(lua_error(L));
3989 return 0;
3990}
3991
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003992/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003993__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3994{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003995 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003996 int len = -1;
3997
3998 if (lua_gettop(L) > 2)
3999 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4000 if (lua_gettop(L) >= 2) {
4001 len = MAY_LJMP(luaL_checkinteger(L, 2));
4002 lua_pop(L, 1);
4003 }
4004
4005 /* Confirm or set the required length */
4006 lua_pushinteger(L, len);
4007
4008 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004009 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004010
4011 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4012}
4013
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004014/* Append data in the output side of the buffer. This data is immediately
4015 * sent. The function returns the amount of data written. If the buffer
4016 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004017 * if the channel is closed.
4018 */
4019__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4020{
4021 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004022 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004023 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4024 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004025 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004026 struct channel *chn = si_ic(si);
4027 int max;
4028
4029 /* Get the max amount of data which can write as input in the channel. */
4030 max = channel_recv_max(chn);
4031 if (max > (len - l))
4032 max = len - l;
4033
4034 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004035 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004036
4037 /* update counters. */
4038 l += max;
4039 lua_pop(L, 1);
4040 lua_pushinteger(L, l);
4041
4042 /* If some data is not send, declares the situation to the
4043 * applet, and returns a yield.
4044 */
4045 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004046 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004047 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004048 }
4049
4050 return 1;
4051}
4052
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004053/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004054 * yield the LUA process, and resume it without checking the
4055 * input arguments.
4056 */
4057__LJMP static int hlua_applet_tcp_send(lua_State *L)
4058{
4059 MAY_LJMP(check_args(L, 2, "send"));
4060 lua_pushinteger(L, 0);
4061
4062 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4063}
4064
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004065/*
4066 *
4067 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004068 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004069 *
4070 *
4071 */
4072
4073/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004074 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004075 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004076__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004077{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004078 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004079}
4080
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004081/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004082 * according with a current TXN.
4083 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004084static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004085{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004086 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004087 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004088 struct stream_interface *si = ctx->owner;
4089 struct stream *s = si_strm(si);
4090 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004091 struct htx *htx;
4092 struct htx_blk *blk;
4093 struct htx_sl *sl;
4094 struct ist path;
4095 unsigned long long len = 0;
4096 int32_t pos;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004097 struct http_uri_parser parser;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004098
4099 /* Check stack size. */
4100 if (!lua_checkstack(L, 3))
4101 return 0;
4102
4103 /* Create the object: obj[0] = userdata.
4104 * Note that the base of the Converters object is the
4105 * same than the TXN object.
4106 */
4107 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004108 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004109 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004110 luactx->appctx = ctx;
4111 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4112 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4113 luactx->htxn.s = s;
4114 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004115
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004116 /* Create the "f" field that contains a list of fetches. */
4117 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004118 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004119 return 0;
4120 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004121
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004122 /* Create the "sf" field that contains a list of stringsafe fetches. */
4123 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004124 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004125 return 0;
4126 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004127
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004128 /* Create the "c" field that contains a list of converters. */
4129 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004130 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004131 return 0;
4132 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004133
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004134 /* Create the "sc" field that contains a list of stringsafe converters. */
4135 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004136 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004137 return 0;
4138 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004139
Christopher Fauleta2097962019-07-15 16:25:33 +02004140 htx = htxbuf(&s->req.buf);
4141 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004142 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004143 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004144
Christopher Fauleta2097962019-07-15 16:25:33 +02004145 /* Stores the request method. */
4146 lua_pushstring(L, "method");
4147 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4148 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004149
Christopher Fauleta2097962019-07-15 16:25:33 +02004150 /* Stores the http version. */
4151 lua_pushstring(L, "version");
4152 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4153 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004154
Christopher Fauleta2097962019-07-15 16:25:33 +02004155 /* creates an array of headers. hlua_http_get_headers() crates and push
4156 * the array on the top of the stack.
4157 */
4158 lua_pushstring(L, "headers");
4159 htxn.s = s;
4160 htxn.p = px;
4161 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004162 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004163 return 0;
4164 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004165
Amaury Denoyellec453f952021-07-06 11:40:12 +02004166 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4167 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004168 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004169 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004170
Christopher Fauleta2097962019-07-15 16:25:33 +02004171 p = path.ptr;
4172 end = path.ptr + path.len;
4173 q = p;
4174 while (q < end && *q != '?')
4175 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004176
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004177 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004178 lua_pushstring(L, "path");
4179 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004180 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004181
Christopher Fauleta2097962019-07-15 16:25:33 +02004182 /* Stores the query string. */
4183 lua_pushstring(L, "qs");
4184 if (*q == '?')
4185 q++;
4186 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004187 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004188 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004189
Christopher Fauleta2097962019-07-15 16:25:33 +02004190 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4191 struct htx_blk *blk = htx_get_blk(htx, pos);
4192 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004193
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004194 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004195 break;
4196 if (type == HTX_BLK_DATA)
4197 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004198 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004199 if (htx->extra != ULLONG_MAX)
4200 len += htx->extra;
4201
4202 /* Stores the request path. */
4203 lua_pushstring(L, "length");
4204 lua_pushinteger(L, len);
4205 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004206
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004207 /* Create an empty array of HTTP request headers. */
4208 lua_pushstring(L, "response");
4209 lua_newtable(L);
4210 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004211
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004212 /* Pop a class stream metatable and affect it to the table. */
4213 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4214 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004215
4216 return 1;
4217}
4218
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004219__LJMP static int hlua_applet_http_set_var(lua_State *L)
4220{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004221 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004222 struct stream *s;
4223 const char *name;
4224 size_t len;
4225 struct sample smp;
4226
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004227 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4228 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004229
4230 /* It is useles to retrieve the stream, but this function
4231 * runs only in a stream context.
4232 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004233 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004234 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004235 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004236
4237 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004238 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004239 hlua_lua2smp(L, 3, &smp);
4240
4241 /* Store the sample in a variable. */
4242 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004243
4244 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4245 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4246 else
4247 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4248
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004249 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004250}
4251
4252__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4253{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004254 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004255 struct stream *s;
4256 const char *name;
4257 size_t len;
4258 struct sample smp;
4259
4260 MAY_LJMP(check_args(L, 2, "unset_var"));
4261
4262 /* It is useles to retrieve the stream, but this function
4263 * runs only in a stream context.
4264 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004265 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004266 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004267 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004268
4269 /* Unset the variable. */
4270 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004271 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4272 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004273}
4274
4275__LJMP static int hlua_applet_http_get_var(lua_State *L)
4276{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004277 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004278 struct stream *s;
4279 const char *name;
4280 size_t len;
4281 struct sample smp;
4282
4283 MAY_LJMP(check_args(L, 2, "get_var"));
4284
4285 /* It is useles to retrieve the stream, but this function
4286 * runs only in a stream context.
4287 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004288 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004289 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004290 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004291
4292 smp_set_owner(&smp, s->be, s->sess, s, 0);
4293 if (!vars_get_by_name(name, len, &smp)) {
4294 lua_pushnil(L);
4295 return 1;
4296 }
4297
4298 return hlua_smp2lua(L, &smp);
4299}
4300
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004301__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4302{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004303 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4304 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004305 struct hlua *hlua;
4306
4307 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004308 if (!s->hlua)
4309 return 0;
4310 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004311
4312 MAY_LJMP(check_args(L, 2, "set_priv"));
4313
4314 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004315 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004316
4317 /* Get and store new value. */
4318 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4319 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4320
4321 return 0;
4322}
4323
4324__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4325{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004326 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4327 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004328 struct hlua *hlua;
4329
4330 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004331 if (!s->hlua) {
4332 lua_pushnil(L);
4333 return 1;
4334 }
4335 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004336
4337 /* Push configuration index in the stack. */
4338 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4339
4340 return 1;
4341}
4342
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004343/* If expected data not yet available, it returns a yield. This function
4344 * consumes the data in the buffer. It returns a string containing the
4345 * data. This string can be empty.
4346 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004347__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004348{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004349 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4350 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004351 struct channel *req = si_oc(si);
4352 struct htx *htx;
4353 struct htx_blk *blk;
4354 size_t count;
4355 int stop = 0;
4356
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004357 htx = htx_from_buf(&req->buf);
4358 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004359 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004360
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004361 while (count && !stop && blk) {
4362 enum htx_blk_type type = htx_get_blk_type(blk);
4363 uint32_t sz = htx_get_blksz(blk);
4364 struct ist v;
4365 uint32_t vlen;
4366 char *nl;
4367
4368 vlen = sz;
4369 if (vlen > count) {
4370 if (type != HTX_BLK_DATA)
4371 break;
4372 vlen = count;
4373 }
4374
4375 switch (type) {
4376 case HTX_BLK_UNUSED:
4377 break;
4378
4379 case HTX_BLK_DATA:
4380 v = htx_get_blk_value(htx, blk);
4381 v.len = vlen;
4382 nl = istchr(v, '\n');
4383 if (nl != NULL) {
4384 stop = 1;
4385 vlen = nl - v.ptr + 1;
4386 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004387 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004388 break;
4389
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004390 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004391 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004392 stop = 1;
4393 break;
4394
4395 default:
4396 break;
4397 }
4398
4399 co_set_data(req, co_data(req) - vlen);
4400 count -= vlen;
4401 if (sz == vlen)
4402 blk = htx_remove_blk(htx, blk);
4403 else {
4404 htx_cut_data_blk(htx, blk, vlen);
4405 break;
4406 }
4407 }
4408
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004409 /* The message was fully consumed and no more data are expected
4410 * (EOM flag set).
4411 */
4412 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4413 stop = 1;
4414
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004415 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004416 if (!stop) {
4417 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004418 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004419 }
4420
4421 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004422 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004423 return 1;
4424}
4425
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004426
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004427/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004428__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004429{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004430 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004431
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004432 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004433 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004434
Christopher Fauleta2097962019-07-15 16:25:33 +02004435 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004436}
4437
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004438/* If expected data not yet available, it returns a yield. This function
4439 * consumes the data in the buffer. It returns a string containing the
4440 * data. This string can be empty.
4441 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004442__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004443{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004444 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4445 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004446 struct channel *req = si_oc(si);
4447 struct htx *htx;
4448 struct htx_blk *blk;
4449 size_t count;
4450 int len;
4451
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004452 htx = htx_from_buf(&req->buf);
4453 len = MAY_LJMP(luaL_checkinteger(L, 2));
4454 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004455 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004456 while (count && len && blk) {
4457 enum htx_blk_type type = htx_get_blk_type(blk);
4458 uint32_t sz = htx_get_blksz(blk);
4459 struct ist v;
4460 uint32_t vlen;
4461
4462 vlen = sz;
4463 if (len > 0 && vlen > len)
4464 vlen = len;
4465 if (vlen > count) {
4466 if (type != HTX_BLK_DATA)
4467 break;
4468 vlen = count;
4469 }
4470
4471 switch (type) {
4472 case HTX_BLK_UNUSED:
4473 break;
4474
4475 case HTX_BLK_DATA:
4476 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004477 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004478 break;
4479
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004480 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004481 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004482 len = 0;
4483 break;
4484
4485 default:
4486 break;
4487 }
4488
4489 co_set_data(req, co_data(req) - vlen);
4490 count -= vlen;
4491 if (len > 0)
4492 len -= vlen;
4493 if (sz == vlen)
4494 blk = htx_remove_blk(htx, blk);
4495 else {
4496 htx_cut_data_blk(htx, blk, vlen);
4497 break;
4498 }
4499 }
4500
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004501 /* The message was fully consumed and no more data are expected
4502 * (EOM flag set).
4503 */
4504 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4505 len = 0;
4506
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004507 htx_to_buf(htx, &req->buf);
4508
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004509 /* If we are no other data available, yield waiting for new data. */
4510 if (len) {
4511 if (len > 0) {
4512 lua_pushinteger(L, len);
4513 lua_replace(L, 2);
4514 }
4515 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004516 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004517 }
4518
4519 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004520 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004521 return 1;
4522}
4523
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004524/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004525__LJMP static int hlua_applet_http_recv(lua_State *L)
4526{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004527 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004528 int len = -1;
4529
4530 /* Check arguments. */
4531 if (lua_gettop(L) > 2)
4532 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4533 if (lua_gettop(L) >= 2) {
4534 len = MAY_LJMP(luaL_checkinteger(L, 2));
4535 lua_pop(L, 1);
4536 }
4537
Christopher Fauleta2097962019-07-15 16:25:33 +02004538 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004539
Christopher Fauleta2097962019-07-15 16:25:33 +02004540 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004541 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004542
Christopher Fauleta2097962019-07-15 16:25:33 +02004543 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004544}
4545
4546/* Append data in the output side of the buffer. This data is immediately
4547 * sent. The function returns the amount of data written. If the buffer
4548 * cannot contain the data, the function yields. The function returns -1
4549 * if the channel is closed.
4550 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004551__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004552{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004553 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4554 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004555 struct channel *res = si_ic(si);
4556 struct htx *htx = htx_from_buf(&res->buf);
4557 const char *data;
4558 size_t len;
4559 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4560 int max;
4561
Christopher Faulet9060fc02019-07-03 11:39:30 +02004562 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004563 if (!max)
4564 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004565
4566 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4567
4568 /* Get the max amount of data which can write as input in the channel. */
4569 if (max > (len - l))
4570 max = len - l;
4571
4572 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004573 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004574 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004575
4576 /* update counters. */
4577 l += max;
4578 lua_pop(L, 1);
4579 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004580
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004581 /* If some data is not send, declares the situation to the
4582 * applet, and returns a yield.
4583 */
4584 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004585 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004586 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004587 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004588 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004589 }
4590
Christopher Fauleta2097962019-07-15 16:25:33 +02004591 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004592 return 1;
4593}
4594
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004595/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004596 * yield the LUA process, and resume it without checking the
4597 * input arguments.
4598 */
4599__LJMP static int hlua_applet_http_send(lua_State *L)
4600{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004601 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004602
4603 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004604 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004605 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4606 WILL_LJMP(lua_error(L));
4607 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004608
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004609 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004610 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004611
Christopher Fauleta2097962019-07-15 16:25:33 +02004612 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004613}
4614
4615__LJMP static int hlua_applet_http_addheader(lua_State *L)
4616{
4617 const char *name;
4618 int ret;
4619
4620 MAY_LJMP(hlua_checkapplet_http(L, 1));
4621 name = MAY_LJMP(luaL_checkstring(L, 2));
4622 MAY_LJMP(luaL_checkstring(L, 3));
4623
4624 /* Push in the stack the "response" entry. */
4625 ret = lua_getfield(L, 1, "response");
4626 if (ret != LUA_TTABLE) {
4627 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4628 "is expected as an array. %s found", lua_typename(L, ret));
4629 WILL_LJMP(lua_error(L));
4630 }
4631
4632 /* check if the header is already registered if it is not
4633 * the case, register it.
4634 */
4635 ret = lua_getfield(L, -1, name);
4636 if (ret == LUA_TNIL) {
4637
4638 /* Entry not found. */
4639 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4640
4641 /* Insert the new header name in the array in the top of the stack.
4642 * It left the new array in the top of the stack.
4643 */
4644 lua_newtable(L);
4645 lua_pushvalue(L, 2);
4646 lua_pushvalue(L, -2);
4647 lua_settable(L, -4);
4648
4649 } else if (ret != LUA_TTABLE) {
4650
4651 /* corruption error. */
4652 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4653 "is expected as an array. %s found", name, lua_typename(L, ret));
4654 WILL_LJMP(lua_error(L));
4655 }
4656
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004657 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004658 * the header value as new entry.
4659 */
4660 lua_pushvalue(L, 3);
4661 ret = lua_rawlen(L, -2);
4662 lua_rawseti(L, -2, ret + 1);
4663 lua_pushboolean(L, 1);
4664 return 1;
4665}
4666
4667__LJMP static int hlua_applet_http_status(lua_State *L)
4668{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004669 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004670 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004671 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004672
4673 if (status < 100 || status > 599) {
4674 lua_pushboolean(L, 0);
4675 return 1;
4676 }
4677
Willy Tarreau7e702d12021-04-28 17:59:21 +02004678 luactx->appctx->ctx.hlua_apphttp.status = status;
4679 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004680 lua_pushboolean(L, 1);
4681 return 1;
4682}
4683
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004684
Christopher Fauleta2097962019-07-15 16:25:33 +02004685__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004686{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004687 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4688 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004689 struct channel *res = si_ic(si);
4690 struct htx *htx;
4691 struct htx_sl *sl;
4692 struct h1m h1m;
4693 const char *status, *reason;
4694 const char *name, *value;
4695 size_t nlen, vlen;
4696 unsigned int flags;
4697
4698 /* Send the message at once. */
4699 htx = htx_from_buf(&res->buf);
4700 h1m_init_res(&h1m);
4701
4702 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004703 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4704 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004705 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02004706 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
4707 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004708 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4709 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4710 }
4711 else {
4712 flags = HTX_SL_F_IS_RESP;
4713 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4714 }
4715 if (!sl) {
4716 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004717 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004718 WILL_LJMP(lua_error(L));
4719 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004720 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004721
4722 /* Get the array associated to the field "response" in the object AppletHTTP. */
4723 lua_pushvalue(L, 0);
4724 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4725 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004726 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004727 WILL_LJMP(lua_error(L));
4728 }
4729
4730 /* Browse the list of headers. */
4731 lua_pushnil(L);
4732 while(lua_next(L, -2) != 0) {
4733 /* We expect a string as -2. */
4734 if (lua_type(L, -2) != LUA_TSTRING) {
4735 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004736 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004737 lua_typename(L, lua_type(L, -2)));
4738 WILL_LJMP(lua_error(L));
4739 }
4740 name = lua_tolstring(L, -2, &nlen);
4741
4742 /* We expect an array as -1. */
4743 if (lua_type(L, -1) != LUA_TTABLE) {
4744 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 +02004745 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004746 name,
4747 lua_typename(L, lua_type(L, -1)));
4748 WILL_LJMP(lua_error(L));
4749 }
4750
4751 /* Browse the table who is on the top of the stack. */
4752 lua_pushnil(L);
4753 while(lua_next(L, -2) != 0) {
4754 int id;
4755
4756 /* We expect a number as -2. */
4757 if (lua_type(L, -2) != LUA_TNUMBER) {
4758 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 +02004759 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004760 name,
4761 lua_typename(L, lua_type(L, -2)));
4762 WILL_LJMP(lua_error(L));
4763 }
4764 id = lua_tointeger(L, -2);
4765
4766 /* We expect a string as -2. */
4767 if (lua_type(L, -1) != LUA_TSTRING) {
4768 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 +02004769 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004770 name, id,
4771 lua_typename(L, lua_type(L, -1)));
4772 WILL_LJMP(lua_error(L));
4773 }
4774 value = lua_tolstring(L, -1, &vlen);
4775
4776 /* Simple Protocol checks. */
4777 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004778 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004779 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4780 struct ist v = ist2(value, vlen);
4781 int ret;
4782
4783 ret = h1_parse_cont_len_header(&h1m, &v);
4784 if (ret < 0) {
4785 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004786 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004787 name);
4788 WILL_LJMP(lua_error(L));
4789 }
4790 else if (ret == 0)
4791 goto next; /* Skip it */
4792 }
4793
4794 /* Add a new header */
4795 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4796 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\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 WILL_LJMP(lua_error(L));
4800 }
4801 next:
4802 /* Remove the array from the stack, and get next element with a remaining string. */
4803 lua_pop(L, 1);
4804 }
4805
4806 /* Remove the array from the stack, and get next element with a remaining string. */
4807 lua_pop(L, 1);
4808 }
4809
4810 if (h1m.flags & H1_MF_CHNK)
4811 h1m.flags &= ~H1_MF_CLEN;
4812 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4813 h1m.flags |= H1_MF_XFER_LEN;
4814
4815 /* Uset HTX start-line flags */
4816 if (h1m.flags & H1_MF_XFER_ENC)
4817 flags |= HTX_SL_F_XFER_ENC;
4818 if (h1m.flags & H1_MF_XFER_LEN) {
4819 flags |= HTX_SL_F_XFER_LEN;
4820 if (h1m.flags & H1_MF_CHNK)
4821 flags |= HTX_SL_F_CHNK;
4822 else if (h1m.flags & H1_MF_CLEN)
4823 flags |= HTX_SL_F_CLEN;
4824 if (h1m.body_len == 0)
4825 flags |= HTX_SL_F_BODYLESS;
4826 }
4827 sl->flags |= flags;
4828
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004829 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004830 * and the status code implies the presence of a message body, we must
4831 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004832 * for the keepalive compliance. If the applet announces a transfer-encoding
4833 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004834 */
4835 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02004836 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
4837 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
4838 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004839 /* Add a new header */
4840 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4841 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4842 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004843 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004844 WILL_LJMP(lua_error(L));
4845 }
4846 }
4847
4848 /* Finalize headers. */
4849 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4850 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004851 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004852 WILL_LJMP(lua_error(L));
4853 }
4854
4855 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4856 b_reset(&res->buf);
4857 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4858 WILL_LJMP(lua_error(L));
4859 }
4860
4861 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004862 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004863
4864 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004865 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004866 return 0;
4867
4868}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004869/* We will build the status line and the headers of the HTTP response.
4870 * We will try send at once if its not possible, we give back the hand
4871 * waiting for more room.
4872 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004873__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004874{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004875 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4876 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004877 struct channel *res = si_ic(si);
4878
4879 if (co_data(res)) {
4880 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004881 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004882 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004883 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004884}
4885
4886
Christopher Fauleta2097962019-07-15 16:25:33 +02004887__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004888{
Christopher Fauleta2097962019-07-15 16:25:33 +02004889 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004890}
4891
Christopher Fauleta2097962019-07-15 16:25:33 +02004892/*
4893 *
4894 *
4895 * Class HTTP
4896 *
4897 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004898 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004899
4900/* Returns a struct hlua_txn if the stack entry "ud" is
4901 * a class stream, otherwise it throws an error.
4902 */
4903__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004904{
Christopher Fauleta2097962019-07-15 16:25:33 +02004905 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4906}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004907
Christopher Fauleta2097962019-07-15 16:25:33 +02004908/* This function creates and push in the stack a HTTP object
4909 * according with a current TXN.
4910 */
4911static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4912{
4913 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004914
Christopher Fauleta2097962019-07-15 16:25:33 +02004915 /* Check stack size. */
4916 if (!lua_checkstack(L, 3))
4917 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004918
Christopher Fauleta2097962019-07-15 16:25:33 +02004919 /* Create the object: obj[0] = userdata.
4920 * Note that the base of the Converters object is the
4921 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004922 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004923 lua_newtable(L);
4924 htxn = lua_newuserdata(L, sizeof(*htxn));
4925 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004926
4927 htxn->s = txn->s;
4928 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004929 htxn->dir = txn->dir;
4930 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004931
4932 /* Pop a class stream metatable and affect it to the table. */
4933 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4934 lua_setmetatable(L, -2);
4935
4936 return 1;
4937}
4938
4939/* This function creates ans returns an array of HTTP headers.
4940 * This function does not fails. It is used as wrapper with the
4941 * 2 following functions.
4942 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004943__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004944{
Christopher Fauleta2097962019-07-15 16:25:33 +02004945 struct htx *htx;
4946 int32_t pos;
4947
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004948 /* Create the table. */
4949 lua_newtable(L);
4950
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004951
Christopher Fauleta2097962019-07-15 16:25:33 +02004952 htx = htxbuf(&msg->chn->buf);
4953 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4954 struct htx_blk *blk = htx_get_blk(htx, pos);
4955 enum htx_blk_type type = htx_get_blk_type(blk);
4956 struct ist n, v;
4957 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004958
Christopher Fauleta2097962019-07-15 16:25:33 +02004959 if (type == HTX_BLK_HDR) {
4960 n = htx_get_blk_name(htx,blk);
4961 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004962 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004963 else if (type == HTX_BLK_EOH)
4964 break;
4965 else
4966 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004967
Christopher Fauleta2097962019-07-15 16:25:33 +02004968 /* Check for existing entry:
4969 * assume that the table is on the top of the stack, and
4970 * push the key in the stack, the function lua_gettable()
4971 * perform the lookup.
4972 */
4973 lua_pushlstring(L, n.ptr, n.len);
4974 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004975
Christopher Fauleta2097962019-07-15 16:25:33 +02004976 switch (lua_type(L, -1)) {
4977 case LUA_TNIL:
4978 /* Table not found, create it. */
4979 lua_pop(L, 1); /* remove the nil value. */
4980 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4981 lua_newtable(L); /* create and push empty table. */
4982 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4983 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4984 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004985 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004986
Christopher Fauleta2097962019-07-15 16:25:33 +02004987 case LUA_TTABLE:
4988 /* Entry found: push the value in the table. */
4989 len = lua_rawlen(L, -1);
4990 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4991 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4992 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4993 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004994
Christopher Fauleta2097962019-07-15 16:25:33 +02004995 default:
4996 /* Other cases are errors. */
4997 hlua_pusherror(L, "internal error during the parsing of headers.");
4998 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004999 }
5000 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005001 return 1;
5002}
5003
5004__LJMP static int hlua_http_req_get_headers(lua_State *L)
5005{
5006 struct hlua_txn *htxn;
5007
5008 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5009 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5010
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005011 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005012 WILL_LJMP(lua_error(L));
5013
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005014 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005015}
5016
5017__LJMP static int hlua_http_res_get_headers(lua_State *L)
5018{
5019 struct hlua_txn *htxn;
5020
5021 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5022 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5023
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005024 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005025 WILL_LJMP(lua_error(L));
5026
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005027 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005028}
5029
5030/* This function replace full header, or just a value in
5031 * the request or in the response. It is a wrapper fir the
5032 * 4 following functions.
5033 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005034__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005035{
5036 size_t name_len;
5037 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5038 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5039 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005040 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005041 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005042
Dragan Dosen26743032019-04-30 15:54:36 +02005043 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005044 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5045
Christopher Fauleta2097962019-07-15 16:25:33 +02005046 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005047 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005048 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005049 return 0;
5050}
5051
5052__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5053{
5054 struct hlua_txn *htxn;
5055
5056 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5057 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5058
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005059 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005060 WILL_LJMP(lua_error(L));
5061
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005062 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005063}
5064
5065__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5066{
5067 struct hlua_txn *htxn;
5068
5069 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5070 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5071
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005072 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005073 WILL_LJMP(lua_error(L));
5074
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005075 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005076}
5077
5078__LJMP static int hlua_http_req_rep_val(lua_State *L)
5079{
5080 struct hlua_txn *htxn;
5081
5082 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5083 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5084
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005085 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005086 WILL_LJMP(lua_error(L));
5087
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005088 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005089}
5090
5091__LJMP static int hlua_http_res_rep_val(lua_State *L)
5092{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005093 struct hlua_txn *htxn;
5094
5095 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5096 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5097
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005098 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005099 WILL_LJMP(lua_error(L));
5100
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005101 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005102}
5103
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005104/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005105 * It is a wrapper for the 2 following functions.
5106 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005107__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005108{
5109 size_t len;
5110 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005111 struct htx *htx = htxbuf(&msg->chn->buf);
5112 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005113
Christopher Fauleta2097962019-07-15 16:25:33 +02005114 ctx.blk = NULL;
5115 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5116 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005117 return 0;
5118}
5119
5120__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5121{
5122 struct hlua_txn *htxn;
5123
5124 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5125 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5126
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005127 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005128 WILL_LJMP(lua_error(L));
5129
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005130 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005131}
5132
5133__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5134{
5135 struct hlua_txn *htxn;
5136
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005137 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005138 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5139
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005140 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005141 WILL_LJMP(lua_error(L));
5142
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005143 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005144}
5145
5146/* This function adds an header. It is a wrapper used by
5147 * the 2 following functions.
5148 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005149__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005150{
5151 size_t name_len;
5152 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5153 size_t value_len;
5154 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005155 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005156
Christopher Fauleta2097962019-07-15 16:25:33 +02005157 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5158 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005159 return 0;
5160}
5161
5162__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5163{
5164 struct hlua_txn *htxn;
5165
5166 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5167 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5168
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005169 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005170 WILL_LJMP(lua_error(L));
5171
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005172 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005173}
5174
5175__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5176{
5177 struct hlua_txn *htxn;
5178
5179 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5180 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5181
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005182 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005183 WILL_LJMP(lua_error(L));
5184
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005185 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005186}
5187
5188static int hlua_http_req_set_hdr(lua_State *L)
5189{
5190 struct hlua_txn *htxn;
5191
5192 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5193 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5194
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005195 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005196 WILL_LJMP(lua_error(L));
5197
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005198 hlua_http_del_hdr(L, &htxn->s->txn->req);
5199 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005200}
5201
5202static int hlua_http_res_set_hdr(lua_State *L)
5203{
5204 struct hlua_txn *htxn;
5205
5206 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5207 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5208
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005209 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005210 WILL_LJMP(lua_error(L));
5211
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005212 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5213 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005214}
5215
5216/* This function set the method. */
5217static int hlua_http_req_set_meth(lua_State *L)
5218{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005219 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005220 size_t name_len;
5221 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005222
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005223 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005224 WILL_LJMP(lua_error(L));
5225
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005226 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005227 return 1;
5228}
5229
5230/* This function set the method. */
5231static int hlua_http_req_set_path(lua_State *L)
5232{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005233 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005234 size_t name_len;
5235 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005236
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005237 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005238 WILL_LJMP(lua_error(L));
5239
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005240 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005241 return 1;
5242}
5243
5244/* This function set the query-string. */
5245static int hlua_http_req_set_query(lua_State *L)
5246{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005247 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005248 size_t name_len;
5249 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005250
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005251 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005252 WILL_LJMP(lua_error(L));
5253
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005254 /* Check length. */
5255 if (name_len > trash.size - 1) {
5256 lua_pushboolean(L, 0);
5257 return 1;
5258 }
5259
5260 /* Add the mark question as prefix. */
5261 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005262 trash.area[trash.data++] = '?';
5263 memcpy(trash.area + trash.data, name, name_len);
5264 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005265
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005266 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005267 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005268 return 1;
5269}
5270
5271/* This function set the uri. */
5272static int hlua_http_req_set_uri(lua_State *L)
5273{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005274 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005275 size_t name_len;
5276 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005277
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005278 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005279 WILL_LJMP(lua_error(L));
5280
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005281 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005282 return 1;
5283}
5284
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005285/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005286static int hlua_http_res_set_status(lua_State *L)
5287{
5288 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5289 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005290 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5291 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005292
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005293 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005294 WILL_LJMP(lua_error(L));
5295
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005296 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005297 return 0;
5298}
5299
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005300/*
5301 *
5302 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005303 * Class TXN
5304 *
5305 *
5306 */
5307
5308/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005309 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005310 */
5311__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5312{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005313 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005314}
5315
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005316__LJMP static int hlua_set_var(lua_State *L)
5317{
5318 struct hlua_txn *htxn;
5319 const char *name;
5320 size_t len;
5321 struct sample smp;
5322
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005323 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5324 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005325
5326 /* It is useles to retrieve the stream, but this function
5327 * runs only in a stream context.
5328 */
5329 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5330 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5331
5332 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005333 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005334 hlua_lua2smp(L, 3, &smp);
5335
5336 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005337 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005338
5339 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5340 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5341 else
5342 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5343
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005344 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005345}
5346
Christopher Faulet85d79c92016-11-09 16:54:56 +01005347__LJMP static int hlua_unset_var(lua_State *L)
5348{
5349 struct hlua_txn *htxn;
5350 const char *name;
5351 size_t len;
5352 struct sample smp;
5353
5354 MAY_LJMP(check_args(L, 2, "unset_var"));
5355
5356 /* It is useles to retrieve the stream, but this function
5357 * runs only in a stream context.
5358 */
5359 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5360 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5361
5362 /* Unset the variable. */
5363 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005364 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5365 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005366}
5367
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005368__LJMP static int hlua_get_var(lua_State *L)
5369{
5370 struct hlua_txn *htxn;
5371 const char *name;
5372 size_t len;
5373 struct sample smp;
5374
5375 MAY_LJMP(check_args(L, 2, "get_var"));
5376
5377 /* It is useles to retrieve the stream, but this function
5378 * runs only in a stream context.
5379 */
5380 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5381 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5382
Willy Tarreau7560dd42016-03-10 16:28:58 +01005383 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005384 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005385 lua_pushnil(L);
5386 return 1;
5387 }
5388
5389 return hlua_smp2lua(L, &smp);
5390}
5391
Willy Tarreau59551662015-03-10 14:23:13 +01005392__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005393{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005394 struct hlua *hlua;
5395
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005396 MAY_LJMP(check_args(L, 2, "set_priv"));
5397
Willy Tarreau87b09662015-04-03 00:22:06 +02005398 /* It is useles to retrieve the stream, but this function
5399 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005400 */
5401 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005402
5403 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005404 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005405 if (!hlua)
5406 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005407
5408 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005409 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005410
5411 /* Get and store new value. */
5412 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5413 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5414
5415 return 0;
5416}
5417
Willy Tarreau59551662015-03-10 14:23:13 +01005418__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005419{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005420 struct hlua *hlua;
5421
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005422 MAY_LJMP(check_args(L, 1, "get_priv"));
5423
Willy Tarreau87b09662015-04-03 00:22:06 +02005424 /* It is useles to retrieve the stream, but this function
5425 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005426 */
5427 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005428
5429 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005430 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005431 if (!hlua) {
5432 lua_pushnil(L);
5433 return 1;
5434 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005435
5436 /* Push configuration index in the stack. */
5437 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5438
5439 return 1;
5440}
5441
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005442/* Create stack entry containing a class TXN. This function
5443 * return 0 if the stack does not contains free slots,
5444 * otherwise it returns 1.
5445 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005446static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005447{
Willy Tarreaude491382015-04-06 11:04:28 +02005448 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005449
5450 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005451 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005452 return 0;
5453
5454 /* NOTE: The allocation never fails. The failure
5455 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005456 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005457 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005458 /* Create the object: obj[0] = userdata. */
5459 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005460 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005461 lua_rawseti(L, -2, 0);
5462
Willy Tarreaude491382015-04-06 11:04:28 +02005463 htxn->s = s;
5464 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005465 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005466 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005467
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005468 /* Create the "f" field that contains a list of fetches. */
5469 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005470 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005471 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005472 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005473
5474 /* Create the "sf" field that contains a list of stringsafe fetches. */
5475 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005476 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005477 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005478 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005479
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005480 /* Create the "c" field that contains a list of converters. */
5481 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005482 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005483 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005484 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005485
5486 /* Create the "sc" field that contains a list of stringsafe converters. */
5487 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005488 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005489 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005490 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005491
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005492 /* Create the "req" field that contains the request channel object. */
5493 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005494 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005495 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005496 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005497
5498 /* Create the "res" field that contains the response channel object. */
5499 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005500 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005501 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005502 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005503
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005504 /* Creates the HTTP object is the current proxy allows http. */
5505 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005506 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005507 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005508 return 0;
5509 }
5510 else
5511 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005512 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005513
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005514 /* Pop a class sesison metatable and affect it to the userdata. */
5515 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5516 lua_setmetatable(L, -2);
5517
5518 return 1;
5519}
5520
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005521__LJMP static int hlua_txn_deflog(lua_State *L)
5522{
5523 const char *msg;
5524 struct hlua_txn *htxn;
5525
5526 MAY_LJMP(check_args(L, 2, "deflog"));
5527 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5528 msg = MAY_LJMP(luaL_checkstring(L, 2));
5529
5530 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5531 return 0;
5532}
5533
5534__LJMP static int hlua_txn_log(lua_State *L)
5535{
5536 int level;
5537 const char *msg;
5538 struct hlua_txn *htxn;
5539
5540 MAY_LJMP(check_args(L, 3, "log"));
5541 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5542 level = MAY_LJMP(luaL_checkinteger(L, 2));
5543 msg = MAY_LJMP(luaL_checkstring(L, 3));
5544
5545 if (level < 0 || level >= NB_LOG_LEVELS)
5546 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5547
5548 hlua_sendlog(htxn->s->be, level, msg);
5549 return 0;
5550}
5551
5552__LJMP static int hlua_txn_log_debug(lua_State *L)
5553{
5554 const char *msg;
5555 struct hlua_txn *htxn;
5556
5557 MAY_LJMP(check_args(L, 2, "Debug"));
5558 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5559 msg = MAY_LJMP(luaL_checkstring(L, 2));
5560 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5561 return 0;
5562}
5563
5564__LJMP static int hlua_txn_log_info(lua_State *L)
5565{
5566 const char *msg;
5567 struct hlua_txn *htxn;
5568
5569 MAY_LJMP(check_args(L, 2, "Info"));
5570 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5571 msg = MAY_LJMP(luaL_checkstring(L, 2));
5572 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5573 return 0;
5574}
5575
5576__LJMP static int hlua_txn_log_warning(lua_State *L)
5577{
5578 const char *msg;
5579 struct hlua_txn *htxn;
5580
5581 MAY_LJMP(check_args(L, 2, "Warning"));
5582 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5583 msg = MAY_LJMP(luaL_checkstring(L, 2));
5584 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5585 return 0;
5586}
5587
5588__LJMP static int hlua_txn_log_alert(lua_State *L)
5589{
5590 const char *msg;
5591 struct hlua_txn *htxn;
5592
5593 MAY_LJMP(check_args(L, 2, "Alert"));
5594 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5595 msg = MAY_LJMP(luaL_checkstring(L, 2));
5596 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5597 return 0;
5598}
5599
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005600__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5601{
5602 struct hlua_txn *htxn;
5603 int ll;
5604
5605 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5606 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5607 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5608
5609 if (ll < 0 || ll > 7)
5610 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5611
5612 htxn->s->logs.level = ll;
5613 return 0;
5614}
5615
5616__LJMP static int hlua_txn_set_tos(lua_State *L)
5617{
5618 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005619 int tos;
5620
5621 MAY_LJMP(check_args(L, 2, "set_tos"));
5622 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5623 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5624
Willy Tarreau1a18b542018-12-11 16:37:42 +01005625 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005626 return 0;
5627}
5628
5629__LJMP static int hlua_txn_set_mark(lua_State *L)
5630{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005631 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005632 int mark;
5633
5634 MAY_LJMP(check_args(L, 2, "set_mark"));
5635 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5636 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5637
Lukas Tribus579e3e32019-08-11 18:03:45 +02005638 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005639 return 0;
5640}
5641
Patrick Hemmer268a7072018-05-11 12:52:31 -04005642__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5643{
5644 struct hlua_txn *htxn;
5645
5646 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5647 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5648 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5649 return 0;
5650}
5651
5652__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5653{
5654 struct hlua_txn *htxn;
5655
5656 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5657 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5658 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5659 return 0;
5660}
5661
Christopher Faulet700d9e82020-01-31 12:21:52 +01005662/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005663 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005664 * message and terminate the transaction. It returns 1 on success and 0 on
5665 * error. The Reply must be on top of the stack.
5666 */
5667__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5668{
5669 struct htx *htx;
5670 struct htx_sl *sl;
5671 struct h1m h1m;
5672 const char *status, *reason, *body;
5673 size_t status_len, reason_len, body_len;
5674 int ret, code, flags;
5675
5676 code = 200;
5677 status = "200";
5678 status_len = 3;
5679 ret = lua_getfield(L, -1, "status");
5680 if (ret == LUA_TNUMBER) {
5681 code = lua_tointeger(L, -1);
5682 status = lua_tolstring(L, -1, &status_len);
5683 }
5684 lua_pop(L, 1);
5685
5686 reason = http_get_reason(code);
5687 reason_len = strlen(reason);
5688 ret = lua_getfield(L, -1, "reason");
5689 if (ret == LUA_TSTRING)
5690 reason = lua_tolstring(L, -1, &reason_len);
5691 lua_pop(L, 1);
5692
5693 body = NULL;
5694 body_len = 0;
5695 ret = lua_getfield(L, -1, "body");
5696 if (ret == LUA_TSTRING)
5697 body = lua_tolstring(L, -1, &body_len);
5698 lua_pop(L, 1);
5699
5700 /* Prepare the response before inserting the headers */
5701 h1m_init_res(&h1m);
5702 htx = htx_from_buf(&s->res.buf);
5703 channel_htx_truncate(&s->res, htx);
5704 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5705 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5706 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5707 ist2(status, status_len), ist2(reason, reason_len));
5708 }
5709 else {
5710 flags = HTX_SL_F_IS_RESP;
5711 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5712 ist2(status, status_len), ist2(reason, reason_len));
5713 }
5714 if (!sl)
5715 goto fail;
5716 sl->info.res.status = code;
5717
5718 /* Push in the stack the "headers" entry. */
5719 ret = lua_getfield(L, -1, "headers");
5720 if (ret != LUA_TTABLE)
5721 goto skip_headers;
5722
5723 lua_pushnil(L);
5724 while (lua_next(L, -2) != 0) {
5725 struct ist name, value;
5726 const char *n, *v;
5727 size_t nlen, vlen;
5728
5729 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5730 /* Skip element if the key is not a string or if the value is not a table */
5731 goto next_hdr;
5732 }
5733
5734 n = lua_tolstring(L, -2, &nlen);
5735 name = ist2(n, nlen);
5736 if (isteqi(name, ist("content-length"))) {
5737 /* Always skip content-length header. It will be added
5738 * later with the correct len
5739 */
5740 goto next_hdr;
5741 }
5742
5743 /* Loop on header's values */
5744 lua_pushnil(L);
5745 while (lua_next(L, -2)) {
5746 if (!lua_isstring(L, -1)) {
5747 /* Skip the value if it is not a string */
5748 goto next_value;
5749 }
5750
5751 v = lua_tolstring(L, -1, &vlen);
5752 value = ist2(v, vlen);
5753
5754 if (isteqi(name, ist("transfer-encoding")))
5755 h1_parse_xfer_enc_header(&h1m, value);
5756 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5757 goto fail;
5758
5759 next_value:
5760 lua_pop(L, 1);
5761 }
5762
5763 next_hdr:
5764 lua_pop(L, 1);
5765 }
5766 skip_headers:
5767 lua_pop(L, 1);
5768
5769 /* Update h1m flags: CLEN is set if CHNK is not present */
5770 if (!(h1m.flags & H1_MF_CHNK)) {
5771 const char *clen = ultoa(body_len);
5772
5773 h1m.flags |= H1_MF_CLEN;
5774 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5775 goto fail;
5776 }
5777 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5778 h1m.flags |= H1_MF_XFER_LEN;
5779
5780 /* Update HTX start-line flags */
5781 if (h1m.flags & H1_MF_XFER_ENC)
5782 flags |= HTX_SL_F_XFER_ENC;
5783 if (h1m.flags & H1_MF_XFER_LEN) {
5784 flags |= HTX_SL_F_XFER_LEN;
5785 if (h1m.flags & H1_MF_CHNK)
5786 flags |= HTX_SL_F_CHNK;
5787 else if (h1m.flags & H1_MF_CLEN)
5788 flags |= HTX_SL_F_CLEN;
5789 if (h1m.body_len == 0)
5790 flags |= HTX_SL_F_BODYLESS;
5791 }
5792 sl->flags |= flags;
5793
5794
5795 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005796 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005797 goto fail;
5798
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005799 htx->flags |= HTX_FL_EOM;
5800
Christopher Faulet700d9e82020-01-31 12:21:52 +01005801 /* Now, forward the response and terminate the transaction */
5802 s->txn->status = code;
5803 htx_to_buf(htx, &s->res.buf);
5804 if (!http_forward_proxy_resp(s, 1))
5805 goto fail;
5806
5807 return 1;
5808
5809 fail:
5810 channel_htx_truncate(&s->res, htx);
5811 return 0;
5812}
5813
5814/* Terminate a transaction if called from a lua action. For TCP streams,
5815 * processing is just aborted. Nothing is returned to the client and all
5816 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5817 * is forwarded to the client before terminating the transaction. On success,
5818 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5819 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5820 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005821 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005822__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005823{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005824 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005825 struct stream *s;
5826 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005827
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005828 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005829
Christopher Faulet700d9e82020-01-31 12:21:52 +01005830 /* If the flags NOTERM is set, we cannot terminate the session, so we
5831 * just end the execution of the current lua code. */
5832 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005833 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005834
Christopher Faulet700d9e82020-01-31 12:21:52 +01005835 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005836 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005837 struct channel *req = &s->req;
5838 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005839
Christopher Faulet700d9e82020-01-31 12:21:52 +01005840 channel_auto_read(req);
5841 channel_abort(req);
5842 channel_auto_close(req);
5843 channel_erase(req);
5844
5845 res->wex = tick_add_ifset(now_ms, res->wto);
5846 channel_auto_read(res);
5847 channel_auto_close(res);
5848 channel_shutr_now(res);
5849
5850 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5851 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005852 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005853
Christopher Faulet700d9e82020-01-31 12:21:52 +01005854 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5855 /* No reply or invalid reply */
5856 s->txn->status = 0;
5857 http_reply_and_close(s, 0, NULL);
5858 }
5859 else {
5860 /* Remove extra args to have the reply on top of the stack */
5861 if (lua_gettop(L) > 2)
5862 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005863
Christopher Faulet700d9e82020-01-31 12:21:52 +01005864 if (!hlua_txn_forward_reply(L, s)) {
5865 if (!(s->flags & SF_ERR_MASK))
5866 s->flags |= SF_ERR_PRXCOND;
5867 lua_pushinteger(L, ACT_RET_ERR);
5868 WILL_LJMP(hlua_done(L));
5869 return 0; /* Never reached */
5870 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005871 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005872
Christopher Faulet700d9e82020-01-31 12:21:52 +01005873 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5874 if (htxn->dir == SMP_OPT_DIR_REQ) {
5875 /* let's log the request time */
5876 s->logs.tv_request = now;
5877 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02005878 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01005879 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005880
Christopher Faulet700d9e82020-01-31 12:21:52 +01005881 done:
5882 if (!(s->flags & SF_ERR_MASK))
5883 s->flags |= SF_ERR_LOCAL;
5884 if (!(s->flags & SF_FINST_MASK))
5885 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005886
Christopher Faulet4ad73102020-03-05 11:07:31 +01005887 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005888 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005889 return 0;
5890}
5891
Christopher Faulet700d9e82020-01-31 12:21:52 +01005892/*
5893 *
5894 *
5895 * Class REPLY
5896 *
5897 *
5898 */
5899
5900/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5901 * free slots, the function fails and returns 0;
5902 */
5903static int hlua_txn_reply_new(lua_State *L)
5904{
5905 struct hlua_txn *htxn;
5906 const char *reason, *body = NULL;
5907 int ret, status;
5908
5909 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005910 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005911 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5912 WILL_LJMP(lua_error(L));
5913 }
5914
5915 /* Default value */
5916 status = 200;
5917 reason = http_get_reason(status);
5918
5919 if (lua_istable(L, 2)) {
5920 /* load status and reason from the table argument at index 2 */
5921 ret = lua_getfield(L, 2, "status");
5922 if (ret == LUA_TNIL)
5923 goto reason;
5924 else if (ret != LUA_TNUMBER) {
5925 /* invalid status: ignore the reason */
5926 goto body;
5927 }
5928 status = lua_tointeger(L, -1);
5929
5930 reason:
5931 lua_pop(L, 1); /* restore the stack: remove status */
5932 ret = lua_getfield(L, 2, "reason");
5933 if (ret == LUA_TSTRING)
5934 reason = lua_tostring(L, -1);
5935
5936 body:
5937 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5938 ret = lua_getfield(L, 2, "body");
5939 if (ret == LUA_TSTRING)
5940 body = lua_tostring(L, -1);
5941 lua_pop(L, 1); /* restore the stack: remove body */
5942 }
5943
5944 /* Create the Reply table */
5945 lua_newtable(L);
5946
5947 /* Add status element */
5948 lua_pushstring(L, "status");
5949 lua_pushinteger(L, status);
5950 lua_settable(L, -3);
5951
5952 /* Add reason element */
5953 reason = http_get_reason(status);
5954 lua_pushstring(L, "reason");
5955 lua_pushstring(L, reason);
5956 lua_settable(L, -3);
5957
5958 /* Add body element, nil if undefined */
5959 lua_pushstring(L, "body");
5960 if (body)
5961 lua_pushstring(L, body);
5962 else
5963 lua_pushnil(L);
5964 lua_settable(L, -3);
5965
5966 /* Add headers element */
5967 lua_pushstring(L, "headers");
5968 lua_newtable(L);
5969
5970 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5971 if (lua_istable(L, 2)) {
5972 /* load headers from the table argument at index 2. If it is a table, copy it. */
5973 ret = lua_getfield(L, 2, "headers");
5974 if (ret == LUA_TTABLE) {
5975 /* stack: [ ... <headers:table>, <table> ] */
5976 lua_pushnil(L);
5977 while (lua_next(L, -2) != 0) {
5978 /* stack: [ ... <headers:table>, <table>, k, v] */
5979 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5980 /* invalid value type, skip it */
5981 lua_pop(L, 1);
5982 continue;
5983 }
5984
5985
5986 /* Duplicate the key and swap it with the value. */
5987 lua_pushvalue(L, -2);
5988 lua_insert(L, -2);
5989 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5990
5991 lua_newtable(L);
5992 lua_insert(L, -2);
5993 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5994
5995 if (lua_isstring(L, -1)) {
5996 /* push the value in the inner table */
5997 lua_rawseti(L, -2, 1);
5998 }
5999 else { /* table */
6000 lua_pushnil(L);
6001 while (lua_next(L, -2) != 0) {
6002 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6003 if (!lua_isstring(L, -1)) {
6004 /* invalid value type, skip it*/
6005 lua_pop(L, 1);
6006 continue;
6007 }
6008 /* push the value in the inner table */
6009 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6010 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6011 }
6012 lua_pop(L, 1);
6013 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6014 }
6015
6016 /* push (k,v) on the stack in the headers table:
6017 * stack: [ ... <headers:table>, <table>, k, k, v ]
6018 */
6019 lua_settable(L, -5);
6020 /* stack: [ ... <headers:table>, <table>, k ] */
6021 }
6022 }
6023 lua_pop(L, 1);
6024 }
6025 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6026 lua_settable(L, -3);
6027 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6028
6029 /* Pop a class sesison metatable and affect it to the userdata. */
6030 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6031 lua_setmetatable(L, -2);
6032 return 1;
6033}
6034
6035/* Set the reply status code, and optionally the reason. If no reason is
6036 * provided, the default one corresponding to the status code is used.
6037 */
6038__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6039{
6040 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6041 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6042
6043 /* First argument (self) must be a table */
6044 luaL_checktype(L, 1, LUA_TTABLE);
6045
6046 if (status < 100 || status > 599) {
6047 lua_pushboolean(L, 0);
6048 return 1;
6049 }
6050 if (!reason)
6051 reason = http_get_reason(status);
6052
6053 lua_pushinteger(L, status);
6054 lua_setfield(L, 1, "status");
6055
6056 lua_pushstring(L, reason);
6057 lua_setfield(L, 1, "reason");
6058
6059 lua_pushboolean(L, 1);
6060 return 1;
6061}
6062
6063/* Add a header into the reply object. Each header name is associated to an
6064 * array of values in the "headers" table. If the header name is not found, a
6065 * new entry is created.
6066 */
6067__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6068{
6069 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6070 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6071 int ret;
6072
6073 /* First argument (self) must be a table */
6074 luaL_checktype(L, 1, LUA_TTABLE);
6075
6076 /* Push in the stack the "headers" entry. */
6077 ret = lua_getfield(L, 1, "headers");
6078 if (ret != LUA_TTABLE) {
6079 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6080 WILL_LJMP(lua_error(L));
6081 }
6082
6083 /* check if the header is already registered. If not, register it. */
6084 ret = lua_getfield(L, -1, name);
6085 if (ret == LUA_TNIL) {
6086 /* Entry not found. */
6087 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6088
6089 /* Insert the new header name in the array in the top of the stack.
6090 * It left the new array in the top of the stack.
6091 */
6092 lua_newtable(L);
6093 lua_pushstring(L, name);
6094 lua_pushvalue(L, -2);
6095 lua_settable(L, -4);
6096 }
6097 else if (ret != LUA_TTABLE) {
6098 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6099 WILL_LJMP(lua_error(L));
6100 }
6101
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006102 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006103 * the header value as new entry.
6104 */
6105 lua_pushstring(L, value);
6106 ret = lua_rawlen(L, -2);
6107 lua_rawseti(L, -2, ret + 1);
6108
6109 lua_pushboolean(L, 1);
6110 return 1;
6111}
6112
6113/* Remove all occurrences of a given header name. */
6114__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6115{
6116 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6117 int ret;
6118
6119 /* First argument (self) must be a table */
6120 luaL_checktype(L, 1, LUA_TTABLE);
6121
6122 /* Push in the stack the "headers" entry. */
6123 ret = lua_getfield(L, 1, "headers");
6124 if (ret != LUA_TTABLE) {
6125 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6126 WILL_LJMP(lua_error(L));
6127 }
6128
6129 lua_pushstring(L, name);
6130 lua_pushnil(L);
6131 lua_settable(L, -3);
6132
6133 lua_pushboolean(L, 1);
6134 return 1;
6135}
6136
6137/* Set the reply's body. Overwrite any existing entry. */
6138__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6139{
6140 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6141
6142 /* First argument (self) must be a table */
6143 luaL_checktype(L, 1, LUA_TTABLE);
6144
6145 lua_pushstring(L, payload);
6146 lua_setfield(L, 1, "body");
6147
6148 lua_pushboolean(L, 1);
6149 return 1;
6150}
6151
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006152__LJMP static int hlua_log(lua_State *L)
6153{
6154 int level;
6155 const char *msg;
6156
6157 MAY_LJMP(check_args(L, 2, "log"));
6158 level = MAY_LJMP(luaL_checkinteger(L, 1));
6159 msg = MAY_LJMP(luaL_checkstring(L, 2));
6160
6161 if (level < 0 || level >= NB_LOG_LEVELS)
6162 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6163
6164 hlua_sendlog(NULL, level, msg);
6165 return 0;
6166}
6167
6168__LJMP static int hlua_log_debug(lua_State *L)
6169{
6170 const char *msg;
6171
6172 MAY_LJMP(check_args(L, 1, "debug"));
6173 msg = MAY_LJMP(luaL_checkstring(L, 1));
6174 hlua_sendlog(NULL, LOG_DEBUG, msg);
6175 return 0;
6176}
6177
6178__LJMP static int hlua_log_info(lua_State *L)
6179{
6180 const char *msg;
6181
6182 MAY_LJMP(check_args(L, 1, "info"));
6183 msg = MAY_LJMP(luaL_checkstring(L, 1));
6184 hlua_sendlog(NULL, LOG_INFO, msg);
6185 return 0;
6186}
6187
6188__LJMP static int hlua_log_warning(lua_State *L)
6189{
6190 const char *msg;
6191
6192 MAY_LJMP(check_args(L, 1, "warning"));
6193 msg = MAY_LJMP(luaL_checkstring(L, 1));
6194 hlua_sendlog(NULL, LOG_WARNING, msg);
6195 return 0;
6196}
6197
6198__LJMP static int hlua_log_alert(lua_State *L)
6199{
6200 const char *msg;
6201
6202 MAY_LJMP(check_args(L, 1, "alert"));
6203 msg = MAY_LJMP(luaL_checkstring(L, 1));
6204 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006205 return 0;
6206}
6207
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006208__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006209{
6210 int wakeup_ms = lua_tointeger(L, -1);
6211 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006212 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006213 return 0;
6214}
6215
6216__LJMP static int hlua_sleep(lua_State *L)
6217{
6218 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006219 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006220
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006221 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006222
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006223 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006224 wakeup_ms = tick_add(now_ms, delay);
6225 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006226
Willy Tarreau9635e032018-10-16 17:52:55 +02006227 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006228 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006229}
6230
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006231__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006232{
6233 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006234 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006235
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006236 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006237
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006238 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006239 wakeup_ms = tick_add(now_ms, delay);
6240 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006241
Willy Tarreau9635e032018-10-16 17:52:55 +02006242 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006243 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006244}
6245
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006246/* This functionis an LUA binding. it permits to give back
6247 * the hand at the HAProxy scheduler. It is used when the
6248 * LUA processing consumes a lot of time.
6249 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006250__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006251{
6252 return 0;
6253}
6254
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006255__LJMP static int hlua_yield(lua_State *L)
6256{
Willy Tarreau9635e032018-10-16 17:52:55 +02006257 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006258 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006259}
6260
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006261/* This function change the nice of the currently executed
6262 * task. It is used set low or high priority at the current
6263 * task.
6264 */
Willy Tarreau59551662015-03-10 14:23:13 +01006265__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006266{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006267 struct hlua *hlua;
6268 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006269
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006270 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006271 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006272
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006273 /* Get hlua struct, or NULL if we execute from main lua state */
6274 hlua = hlua_gethlua(L);
6275
6276 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006277 if (!hlua || !hlua->task)
6278 return 0;
6279
6280 if (nice < -1024)
6281 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006282 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006283 nice = 1024;
6284
6285 hlua->task->nice = nice;
6286 return 0;
6287}
6288
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006289/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006290 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6291 * return an E_AGAIN signal, the emmiter of this signal must set a
6292 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006293 *
6294 * Task wrapper are longjmp safe because the only one Lua code
6295 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006296 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006297struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006298{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006299 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006300 enum hlua_exec status;
6301
Christopher Faulet5bc99722018-04-25 10:34:45 +02006302 if (task->thread_mask == MAX_THREADS_MASK)
6303 task_set_affinity(task, tid_bit);
6304
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006305 /* If it is the first call to the task, we must initialize the
6306 * execution timeouts.
6307 */
6308 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006309 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006310
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006311 /* Execute the Lua code. */
6312 status = hlua_ctx_resume(hlua, 1);
6313
6314 switch (status) {
6315 /* finished or yield */
6316 case HLUA_E_OK:
6317 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006318 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006319 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006320 break;
6321
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006322 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006323 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006324 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006325 break;
6326
6327 /* finished with error. */
6328 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006329 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006330 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006331 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006332 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006333 break;
6334
6335 case HLUA_E_ERR:
6336 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006337 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006338 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006339 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006340 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006341 break;
6342 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006343 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006344}
6345
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006346/* This function is an LUA binding that register LUA function to be
6347 * executed after the HAProxy configuration parsing and before the
6348 * HAProxy scheduler starts. This function expect only one LUA
6349 * argument that is a function. This function returns nothing, but
6350 * throws if an error is encountered.
6351 */
6352__LJMP static int hlua_register_init(lua_State *L)
6353{
6354 struct hlua_init_function *init;
6355 int ref;
6356
6357 MAY_LJMP(check_args(L, 1, "register_init"));
6358
6359 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6360
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006361 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006362 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006363 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006364
6365 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006366 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006367 return 0;
6368}
6369
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006370/* This functio is an LUA binding. It permits to register a task
6371 * executed in parallel of the main HAroxy activity. The task is
6372 * created and it is set in the HAProxy scheduler. It can be called
6373 * from the "init" section, "post init" or during the runtime.
6374 *
6375 * Lua prototype:
6376 *
6377 * <none> core.register_task(<function>)
6378 */
6379static int hlua_register_task(lua_State *L)
6380{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006381 struct hlua *hlua = NULL;
6382 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006383 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006384 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006385
6386 MAY_LJMP(check_args(L, 1, "register_task"));
6387
6388 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6389
Thierry Fournier75fc0292020-11-28 13:18:56 +01006390 /* Get the reference state. If the reference is NULL, L is the master
6391 * state, otherwise hlua->T is.
6392 */
6393 hlua = hlua_gethlua(L);
6394 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006395 /* we are in runtime processing */
6396 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006397 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006398 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006399 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006400
Willy Tarreaubafbe012017-11-24 17:34:44 +01006401 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006402 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006403 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006404 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006405
Thierry Fournier59f11be2020-11-29 00:37:41 +01006406 /* We are in the common lua state, execute the task anywhere,
6407 * otherwise, inherit the current thread identifier
6408 */
6409 if (state_id == 0)
6410 task = task_new(MAX_THREADS_MASK);
6411 else
6412 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006413 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006414 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006415
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006416 task->context = hlua;
6417 task->process = hlua_process_task;
6418
Thierry Fournier021d9862020-11-28 23:42:03 +01006419 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006420 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006421
6422 /* Restore the function in the stack. */
6423 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6424 hlua->nargs = 0;
6425
6426 /* Schedule task. */
6427 task_schedule(task, now_ms);
6428
6429 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006430
6431 alloc_error:
6432 task_destroy(task);
6433 hlua_ctx_destroy(hlua);
6434 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6435 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006436}
6437
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006438/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6439 * doesn't allow "yield" functions because the HAProxy engine cannot
6440 * resume converters.
6441 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006442static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006443{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006444 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006445 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006446 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006447
Willy Tarreaube508f12016-03-10 11:47:01 +01006448 if (!stream)
6449 return 0;
6450
Willy Tarreau87b09662015-04-03 00:22:06 +02006451 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006452 * Lua context can be not initialized. This behavior
6453 * permits to save performances because a systematic
6454 * Lua initialization cause 5% performances loss.
6455 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006456 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006457 struct hlua *hlua;
6458
6459 hlua = pool_alloc(pool_head_hlua);
6460 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006461 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6462 return 0;
6463 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006464 HLUA_INIT(hlua);
6465 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006466 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006467 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6468 return 0;
6469 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006470 }
6471
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006472 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006473 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006474
6475 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006476 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006477 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6478 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006479 else
6480 error = "critical error";
6481 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006482 return 0;
6483 }
6484
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006485 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006486 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006487 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006488 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006489 return 0;
6490 }
6491
6492 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006493 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006494
6495 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006496 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006497 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006498 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006499 return 0;
6500 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006501 hlua_smp2lua(stream->hlua->T, smp);
6502 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006503
6504 /* push keywords in the stack. */
6505 if (arg_p) {
6506 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006507 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006508 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006509 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006510 return 0;
6511 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006512 hlua_arg2lua(stream->hlua->T, arg_p);
6513 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006514 }
6515 }
6516
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006517 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006518 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006519
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006520 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006521 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006522 }
6523
6524 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006525 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006526 /* finished. */
6527 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006528 /* If the stack is empty, the function fails. */
6529 if (lua_gettop(stream->hlua->T) <= 0)
6530 return 0;
6531
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006532 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006533 hlua_lua2smp(stream->hlua->T, -1, smp);
6534 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006535 return 1;
6536
6537 /* yield. */
6538 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006539 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006540 return 0;
6541
6542 /* finished with error. */
6543 case HLUA_E_ERRMSG:
6544 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006545 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006546 fcn->name, lua_tostring(stream->hlua->T, -1));
6547 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006548 return 0;
6549
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006550 case HLUA_E_ETMOUT:
6551 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6552 return 0;
6553
6554 case HLUA_E_NOMEM:
6555 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6556 return 0;
6557
6558 case HLUA_E_YIELD:
6559 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6560 return 0;
6561
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006562 case HLUA_E_ERR:
6563 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006564 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006565 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006566
6567 default:
6568 return 0;
6569 }
6570}
6571
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006572/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6573 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006574 * resume sample-fetches. This function will be called by the sample
6575 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006576 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006577static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6578 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006579{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006580 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006581 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006582 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006583 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006584
Willy Tarreaube508f12016-03-10 11:47:01 +01006585 if (!stream)
6586 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006587
Willy Tarreau87b09662015-04-03 00:22:06 +02006588 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006589 * Lua context can be not initialized. This behavior
6590 * permits to save performances because a systematic
6591 * Lua initialization cause 5% performances loss.
6592 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006593 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006594 struct hlua *hlua;
6595
6596 hlua = pool_alloc(pool_head_hlua);
6597 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006598 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6599 return 0;
6600 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006601 hlua->T = NULL;
6602 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006603 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006604 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6605 return 0;
6606 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006607 }
6608
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006609 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006611
6612 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006613 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006614 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6615 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006616 else
6617 error = "critical error";
6618 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006619 return 0;
6620 }
6621
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006622 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006623 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006624 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006625 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006626 return 0;
6627 }
6628
6629 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006630 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006631
6632 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006633 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006634 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006635 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006636 return 0;
6637 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006638 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006639
6640 /* push keywords in the stack. */
6641 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6642 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006643 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006644 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006645 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006646 return 0;
6647 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006648 hlua_arg2lua(stream->hlua->T, arg_p);
6649 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006650 }
6651
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006652 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006653 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006654
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006655 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006656 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006657 }
6658
6659 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006660 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006661 /* finished. */
6662 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006663 /* If the stack is empty, the function fails. */
6664 if (lua_gettop(stream->hlua->T) <= 0)
6665 return 0;
6666
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006667 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006668 hlua_lua2smp(stream->hlua->T, -1, smp);
6669 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006670
6671 /* Set the end of execution flag. */
6672 smp->flags &= ~SMP_F_MAY_CHANGE;
6673 return 1;
6674
6675 /* yield. */
6676 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006677 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006678 return 0;
6679
6680 /* finished with error. */
6681 case HLUA_E_ERRMSG:
6682 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006683 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006684 fcn->name, lua_tostring(stream->hlua->T, -1));
6685 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006686 return 0;
6687
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006688 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006689 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6690 return 0;
6691
6692 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006693 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6694 return 0;
6695
6696 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006697 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6698 return 0;
6699
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006700 case HLUA_E_ERR:
6701 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006702 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006703 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006704
6705 default:
6706 return 0;
6707 }
6708}
6709
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006710/* This function is an LUA binding used for registering
6711 * "sample-conv" functions. It expects a converter name used
6712 * in the haproxy configuration file, and an LUA function.
6713 */
6714__LJMP static int hlua_register_converters(lua_State *L)
6715{
6716 struct sample_conv_kw_list *sck;
6717 const char *name;
6718 int ref;
6719 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02006720 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006721 struct sample_conv *sc;
6722 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006723
6724 MAY_LJMP(check_args(L, 2, "register_converters"));
6725
6726 /* First argument : converter name. */
6727 name = MAY_LJMP(luaL_checkstring(L, 1));
6728
6729 /* Second argument : lua function. */
6730 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6731
Thierry Fournierf67442e2020-11-28 20:41:07 +01006732 /* Check if the converter is already registered */
6733 trash = get_trash_chunk();
6734 chunk_printf(trash, "lua.%s", name);
6735 sc = find_sample_conv(trash->area, trash->data);
6736 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006737 fcn = sc->private;
6738 if (fcn->function_ref[hlua_state_id] != -1) {
6739 ha_warning("Trying to register converter 'lua.%s' more than once. "
6740 "This will become a hard error in version 2.5.\n", name);
6741 }
6742 fcn->function_ref[hlua_state_id] = ref;
6743 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006744 }
6745
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006746 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006747 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006748 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02006749 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006750 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006751 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02006752 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006753
6754 /* Fill fcn. */
6755 fcn->name = strdup(name);
6756 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02006757 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006758 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006759
6760 /* List head */
6761 sck->list.n = sck->list.p = NULL;
6762
6763 /* converter keyword. */
6764 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006765 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006766 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02006767 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006768
6769 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6770 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006771 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 +01006772 sck->kw[0].val_args = NULL;
6773 sck->kw[0].in_type = SMP_T_STR;
6774 sck->kw[0].out_type = SMP_T_STR;
6775 sck->kw[0].private = fcn;
6776
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006777 /* Register this new converter */
6778 sample_register_convs(sck);
6779
6780 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02006781
6782 alloc_error:
6783 release_hlua_function(fcn);
6784 ha_free(&sck);
6785 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6786 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006787}
6788
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006789/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006790 * "sample-fetch" functions. It expects a converter name used
6791 * in the haproxy configuration file, and an LUA function.
6792 */
6793__LJMP static int hlua_register_fetches(lua_State *L)
6794{
6795 const char *name;
6796 int ref;
6797 int len;
6798 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02006799 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006800 struct sample_fetch *sf;
6801 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006802
6803 MAY_LJMP(check_args(L, 2, "register_fetches"));
6804
6805 /* First argument : sample-fetch name. */
6806 name = MAY_LJMP(luaL_checkstring(L, 1));
6807
6808 /* Second argument : lua function. */
6809 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6810
Thierry Fournierf67442e2020-11-28 20:41:07 +01006811 /* Check if the sample-fetch is already registered */
6812 trash = get_trash_chunk();
6813 chunk_printf(trash, "lua.%s", name);
6814 sf = find_sample_fetch(trash->area, trash->data);
6815 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006816 fcn = sf->private;
6817 if (fcn->function_ref[hlua_state_id] != -1) {
6818 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6819 "This will become a hard error in version 2.5.\n", name);
6820 }
6821 fcn->function_ref[hlua_state_id] = ref;
6822 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006823 }
6824
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006825 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006826 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006827 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02006828 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006829 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006830 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02006831 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006832
6833 /* Fill fcn. */
6834 fcn->name = strdup(name);
6835 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02006836 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01006837 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006838
6839 /* List head */
6840 sfk->list.n = sfk->list.p = NULL;
6841
6842 /* sample-fetch keyword. */
6843 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006844 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006845 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02006846 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006847
6848 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6849 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006850 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 +01006851 sfk->kw[0].val_args = NULL;
6852 sfk->kw[0].out_type = SMP_T_STR;
6853 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6854 sfk->kw[0].val = 0;
6855 sfk->kw[0].private = fcn;
6856
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006857 /* Register this new fetch. */
6858 sample_register_fetches(sfk);
6859
6860 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02006861
6862 alloc_error:
6863 release_hlua_function(fcn);
6864 ha_free(&sfk);
6865 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6866 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006867}
6868
Christopher Faulet501465d2020-02-26 14:54:16 +01006869/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006870 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006871__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006872{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006873 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006874 unsigned int delay;
6875 unsigned int wakeup_ms;
6876
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006877 /* Get hlua struct, or NULL if we execute from main lua state */
6878 hlua = hlua_gethlua(L);
6879 if (!hlua) {
6880 return 0;
6881 }
6882
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006883 MAY_LJMP(check_args(L, 1, "wake_time"));
6884
6885 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6886 wakeup_ms = tick_add(now_ms, delay);
6887 hlua->wake_time = wakeup_ms;
6888 return 0;
6889}
6890
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006891/* This function is a wrapper to execute each LUA function declared as an action
6892 * wrapper during the initialisation period. This function may return any
6893 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6894 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6895 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006896 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006897static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006898 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006899{
6900 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006901 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006902 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006903 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006904
6905 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006906 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6907 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6908 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6909 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006910 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006911 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006912 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006913 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006914
Willy Tarreau87b09662015-04-03 00:22:06 +02006915 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006916 * Lua context can be not initialized. This behavior
6917 * permits to save performances because a systematic
6918 * Lua initialization cause 5% performances loss.
6919 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006920 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006921 struct hlua *hlua;
6922
6923 hlua = pool_alloc(pool_head_hlua);
6924 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006925 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006926 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006927 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006928 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006929 HLUA_INIT(hlua);
6930 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006931 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 +01006932 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006933 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006934 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006935 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006936 }
6937
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006938 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006939 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006940
6941 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006942 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006943 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6944 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006945 else
6946 error = "critical error";
6947 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006948 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006949 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006950 }
6951
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006952 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006953 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006954 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006955 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006956 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006957 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006958 }
6959
6960 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006961 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 +01006962
Willy Tarreau87b09662015-04-03 00:22:06 +02006963 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006964 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006965 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006966 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006967 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006968 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006969 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006970 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006971
6972 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006973 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006974 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006975 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006976 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006977 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006978 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006979 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006980 lua_pushstring(s->hlua->T, *arg);
6981 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006982 }
6983
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006984 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006985 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006986
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006987 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006988 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006989 }
6990
6991 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006992 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006993 /* finished. */
6994 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006995 /* Catch the return value */
6996 if (lua_gettop(s->hlua->T) > 0)
6997 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006998
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006999 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007000 if (act_ret == ACT_RET_YIELD) {
7001 if (flags & ACT_OPT_FINAL)
7002 goto err_yield;
7003
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007004 if (dir == SMP_OPT_DIR_REQ)
7005 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7006 s->hlua->wake_time);
7007 else
7008 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7009 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007010 }
7011 goto end;
7012
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007013 /* yield. */
7014 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007015 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007016 if (dir == SMP_OPT_DIR_REQ)
7017 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7018 s->hlua->wake_time);
7019 else
7020 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7021 s->hlua->wake_time);
7022
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007023 /* Some actions can be wake up when a "write" event
7024 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007025 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007026 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007027 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007028 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007029 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007030 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007031 act_ret = ACT_RET_YIELD;
7032 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007033
7034 /* finished with error. */
7035 case HLUA_E_ERRMSG:
7036 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007037 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007038 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007039 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007040 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007041
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007042 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007043 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007044 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007045
7046 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007047 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007048 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007049
7050 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007051 err_yield:
7052 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007053 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007054 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007055 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007056
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007057 case HLUA_E_ERR:
7058 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007059 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007060 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007061
7062 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007063 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007064 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007065
7066 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007067 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007068 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007069 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007070}
7071
Willy Tarreau144f84a2021-03-02 16:09:26 +01007072struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007073{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007074 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007075
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007076 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007077 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007078 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007079}
7080
7081static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7082{
7083 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007084 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007085 struct task *task;
7086 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007087 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007088
Willy Tarreaubafbe012017-11-24 17:34:44 +01007089 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007090 if (!hlua) {
7091 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007092 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007093 return 0;
7094 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007095 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007096 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007097 ctx->ctx.hlua_apptcp.flags = 0;
7098
7099 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007100 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007101 if (!task) {
7102 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007103 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007104 return 0;
7105 }
7106 task->nice = 0;
7107 task->context = ctx;
7108 task->process = hlua_applet_wakeup;
7109 ctx->ctx.hlua_apptcp.task = task;
7110
7111 /* In the execution wrappers linked with a stream, the
7112 * Lua context can be not initialized. This behavior
7113 * permits to save performances because a systematic
7114 * Lua initialization cause 5% performances loss.
7115 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007116 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 +02007117 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007118 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007119 return 0;
7120 }
7121
7122 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007123 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007124
7125 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007126 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007127 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7128 error = lua_tostring(hlua->T, -1);
7129 else
7130 error = "critical error";
7131 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007132 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007133 return 0;
7134 }
7135
7136 /* Check stack available size. */
7137 if (!lua_checkstack(hlua->T, 1)) {
7138 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007139 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007140 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007141 return 0;
7142 }
7143
7144 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007145 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007146
7147 /* Create and and push object stream in the stack. */
7148 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7149 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007150 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007151 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007152 return 0;
7153 }
7154 hlua->nargs = 1;
7155
7156 /* push keywords in the stack. */
7157 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7158 if (!lua_checkstack(hlua->T, 1)) {
7159 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007160 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007161 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007162 return 0;
7163 }
7164 lua_pushstring(hlua->T, *arg);
7165 hlua->nargs++;
7166 }
7167
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007168 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007169
7170 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007171 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007172 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007173
7174 return 1;
7175}
7176
Willy Tarreau60409db2019-08-21 14:14:50 +02007177void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007178{
7179 struct stream_interface *si = ctx->owner;
7180 struct stream *strm = si_strm(si);
7181 struct channel *res = si_ic(si);
7182 struct act_rule *rule = ctx->rule;
7183 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007184 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007185
7186 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007187 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7188 /* eat the whole request */
7189 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007190 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007191 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007192
7193 /* If the stream is disconnect or closed, ldo nothing. */
7194 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7195 return;
7196
7197 /* Execute the function. */
7198 switch (hlua_ctx_resume(hlua, 1)) {
7199 /* finished. */
7200 case HLUA_E_OK:
7201 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7202
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007203 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007204 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007205 res->flags |= CF_READ_NULL;
7206 si_shutr(si);
7207 return;
7208
7209 /* yield. */
7210 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007211 if (hlua->wake_time != TICK_ETERNITY)
7212 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007213 return;
7214
7215 /* finished with error. */
7216 case HLUA_E_ERRMSG:
7217 /* Display log. */
7218 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007219 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007220 lua_pop(hlua->T, 1);
7221 goto error;
7222
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007223 case HLUA_E_ETMOUT:
7224 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007225 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007226 goto error;
7227
7228 case HLUA_E_NOMEM:
7229 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007230 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007231 goto error;
7232
7233 case HLUA_E_YIELD: /* unexpected */
7234 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007235 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007236 goto error;
7237
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007238 case HLUA_E_ERR:
7239 /* Display log. */
7240 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007241 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007242 goto error;
7243
7244 default:
7245 goto error;
7246 }
7247
7248error:
7249
7250 /* For all other cases, just close the stream. */
7251 si_shutw(si);
7252 si_shutr(si);
7253 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7254}
7255
7256static void hlua_applet_tcp_release(struct appctx *ctx)
7257{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007258 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007259 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007260 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007261 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007262}
7263
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007264/* The function returns 1 if the initialisation is complete, 0 if
7265 * an errors occurs and -1 if more data are required for initializing
7266 * the applet.
7267 */
7268static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7269{
7270 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007271 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007272 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007273 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007274 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007275 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007276
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007277 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007278 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007279 if (!hlua) {
7280 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007281 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007282 return 0;
7283 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007284 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007285 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007286 ctx->ctx.hlua_apphttp.left_bytes = -1;
7287 ctx->ctx.hlua_apphttp.flags = 0;
7288
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007289 if (txn->req.flags & HTTP_MSGF_VER_11)
7290 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7291
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007292 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007293 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007294 if (!task) {
7295 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007296 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007297 return 0;
7298 }
7299 task->nice = 0;
7300 task->context = ctx;
7301 task->process = hlua_applet_wakeup;
7302 ctx->ctx.hlua_apphttp.task = task;
7303
7304 /* In the execution wrappers linked with a stream, the
7305 * Lua context can be not initialized. This behavior
7306 * permits to save performances because a systematic
7307 * Lua initialization cause 5% performances loss.
7308 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007309 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 +02007310 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007311 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007312 return 0;
7313 }
7314
7315 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007316 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007317
7318 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007319 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007320 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7321 error = lua_tostring(hlua->T, -1);
7322 else
7323 error = "critical error";
7324 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007325 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007326 return 0;
7327 }
7328
7329 /* Check stack available size. */
7330 if (!lua_checkstack(hlua->T, 1)) {
7331 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007332 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007333 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007334 return 0;
7335 }
7336
7337 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007338 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007339
7340 /* Create and and push object stream in the stack. */
7341 if (!hlua_applet_http_new(hlua->T, ctx)) {
7342 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007343 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007344 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007345 return 0;
7346 }
7347 hlua->nargs = 1;
7348
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007349 /* push keywords in the stack. */
7350 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7351 if (!lua_checkstack(hlua->T, 1)) {
7352 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007353 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007354 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007355 return 0;
7356 }
7357 lua_pushstring(hlua->T, *arg);
7358 hlua->nargs++;
7359 }
7360
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007361 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007362
7363 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007364 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007365
7366 return 1;
7367}
7368
Willy Tarreau60409db2019-08-21 14:14:50 +02007369void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007370{
7371 struct stream_interface *si = ctx->owner;
7372 struct stream *strm = si_strm(si);
7373 struct channel *req = si_oc(si);
7374 struct channel *res = si_ic(si);
7375 struct act_rule *rule = ctx->rule;
7376 struct proxy *px = strm->be;
7377 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7378 struct htx *req_htx, *res_htx;
7379
7380 res_htx = htx_from_buf(&res->buf);
7381
7382 /* If the stream is disconnect or closed, ldo nothing. */
7383 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7384 goto out;
7385
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007386 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007387 if (!b_size(&res->buf)) {
7388 si_rx_room_blk(si);
7389 goto out;
7390 }
7391 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007392 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007393 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7394
7395 /* Set the currently running flag. */
7396 if (!HLUA_IS_RUNNING(hlua) &&
7397 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007398 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007399 si_cant_get(si);
7400 goto out;
7401 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007402 }
7403
7404 /* Executes The applet if it is not done. */
7405 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7406
7407 /* Execute the function. */
7408 switch (hlua_ctx_resume(hlua, 1)) {
7409 /* finished. */
7410 case HLUA_E_OK:
7411 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7412 break;
7413
7414 /* yield. */
7415 case HLUA_E_AGAIN:
7416 if (hlua->wake_time != TICK_ETERNITY)
7417 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007418 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007419
7420 /* finished with error. */
7421 case HLUA_E_ERRMSG:
7422 /* Display log. */
7423 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007424 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007425 lua_pop(hlua->T, 1);
7426 goto error;
7427
7428 case HLUA_E_ETMOUT:
7429 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007430 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007431 goto error;
7432
7433 case HLUA_E_NOMEM:
7434 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007435 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007436 goto error;
7437
7438 case HLUA_E_YIELD: /* unexpected */
7439 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007440 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007441 goto error;
7442
7443 case HLUA_E_ERR:
7444 /* Display log. */
7445 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007446 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007447 goto error;
7448
7449 default:
7450 goto error;
7451 }
7452 }
7453
7454 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007455 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7456 goto done;
7457
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007458 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7459 goto error;
7460
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007461 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
7462 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007463 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7464 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007465 }
7466
7467 done:
7468 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007469 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007470 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007471 si_shutr(si);
7472 }
7473
7474 /* eat the whole request */
7475 if (co_data(req)) {
7476 req_htx = htx_from_buf(&req->buf);
7477 co_htx_skip(req, req_htx, co_data(req));
7478 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007479 }
7480 }
7481
7482 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007483 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007484 return;
7485
7486 error:
7487
7488 /* If we are in HTTP mode, and we are not send any
7489 * data, return a 500 server error in best effort:
7490 * if there is no room available in the buffer,
7491 * just close the connection.
7492 */
7493 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007494 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007495
7496 channel_erase(res);
7497 res->buf.data = b_data(err);
7498 memcpy(res->buf.area, b_head(err), b_data(err));
7499 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007500 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007501 }
7502 if (!(strm->flags & SF_ERR_MASK))
7503 strm->flags |= SF_ERR_RESOURCE;
7504 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7505 goto done;
7506}
7507
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007508static void hlua_applet_http_release(struct appctx *ctx)
7509{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007510 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007511 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007512 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007513 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007514}
7515
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007516/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007517 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007518 *
7519 * This function can fail with an abort() due to an Lua critical error.
7520 * We are in the configuration parsing process of HAProxy, this abort() is
7521 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007522 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007523static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7524 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007525{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007526 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007527 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007528
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007529 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007530 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007531 if (!rule->arg.hlua_rule) {
7532 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007533 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007534 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007535
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007536 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007537 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7538 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007539 if (!rule->arg.hlua_rule->args) {
7540 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007541 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007542 }
7543
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007544 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007545 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007546
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007547 /* Expect some arguments */
7548 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007549 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007550 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007551 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007552 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007553 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007554 if (!rule->arg.hlua_rule->args[i]) {
7555 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007556 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007557 }
7558 (*cur_arg)++;
7559 }
7560 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007561
Thierry FOURNIER42148732015-09-02 17:17:33 +02007562 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007563 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007564 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007565
7566 error:
7567 if (rule->arg.hlua_rule) {
7568 if (rule->arg.hlua_rule->args) {
7569 for (i = 0; i < fcn->nargs; i++)
7570 ha_free(&rule->arg.hlua_rule->args[i]);
7571 ha_free(&rule->arg.hlua_rule->args);
7572 }
7573 ha_free(&rule->arg.hlua_rule);
7574 }
7575 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007576}
7577
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007578static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7579 struct act_rule *rule, char **err)
7580{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007581 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007582
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007583 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007584 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007585 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007586 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007587 * the call of this analyzer.
7588 */
7589 if (rule->from != ACT_F_HTTP_REQ) {
7590 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7591 return ACT_RET_PRS_ERR;
7592 }
7593
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007594 /* Memory for the rule. */
7595 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7596 if (!rule->arg.hlua_rule) {
7597 memprintf(err, "out of memory error");
7598 return ACT_RET_PRS_ERR;
7599 }
7600
7601 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007602 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007603
7604 /* TODO: later accept arguments. */
7605 rule->arg.hlua_rule->args = NULL;
7606
7607 /* Add applet pointer in the rule. */
7608 rule->applet.obj_type = OBJ_TYPE_APPLET;
7609 rule->applet.name = fcn->name;
7610 rule->applet.init = hlua_applet_http_init;
7611 rule->applet.fct = hlua_applet_http_fct;
7612 rule->applet.release = hlua_applet_http_release;
7613 rule->applet.timeout = hlua_timeout_applet;
7614
7615 return ACT_RET_PRS_OK;
7616}
7617
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007618/* This function is an LUA binding used for registering
7619 * "sample-conv" functions. It expects a converter name used
7620 * in the haproxy configuration file, and an LUA function.
7621 */
7622__LJMP static int hlua_register_action(lua_State *L)
7623{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007624 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007625 const char *name;
7626 int ref;
7627 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007628 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007629 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007630 struct buffer *trash;
7631 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007632
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007633 /* Initialise the number of expected arguments at 0. */
7634 nargs = 0;
7635
7636 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7637 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007638
7639 /* First argument : converter name. */
7640 name = MAY_LJMP(luaL_checkstring(L, 1));
7641
7642 /* Second argument : environment. */
7643 if (lua_type(L, 2) != LUA_TTABLE)
7644 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7645
7646 /* Third argument : lua function. */
7647 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7648
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007649 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007650 if (lua_gettop(L) >= 4)
7651 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7652
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007653 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007654 lua_pushnil(L);
7655 while (lua_next(L, 2) != 0) {
7656 if (lua_type(L, -1) != LUA_TSTRING)
7657 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7658
Thierry Fournierf67442e2020-11-28 20:41:07 +01007659 /* Check if action exists */
7660 trash = get_trash_chunk();
7661 chunk_printf(trash, "lua.%s", name);
7662 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7663 akw = tcp_req_cont_action(trash->area);
7664 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7665 akw = tcp_res_cont_action(trash->area);
7666 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7667 akw = action_http_req_custom(trash->area);
7668 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7669 akw = action_http_res_custom(trash->area);
7670 } else {
7671 akw = NULL;
7672 }
7673 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007674 fcn = akw->private;
7675 if (fcn->function_ref[hlua_state_id] != -1) {
7676 ha_warning("Trying to register action 'lua.%s' more than once. "
7677 "This will become a hard error in version 2.5.\n", name);
7678 }
7679 fcn->function_ref[hlua_state_id] = ref;
7680
7681 /* pop the environment string. */
7682 lua_pop(L, 1);
7683 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007684 }
7685
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007686 /* Check required environment. Only accepted "http" or "tcp". */
7687 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007688 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007689 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007690 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007691 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007692 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007693 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007694
7695 /* Fill fcn. */
7696 fcn->name = strdup(name);
7697 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007698 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007699 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007700
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007701 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007702 fcn->nargs = nargs;
7703
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007704 /* List head */
7705 akl->list.n = akl->list.p = NULL;
7706
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007707 /* action keyword. */
7708 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007709 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007710 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007711 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007712
7713 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7714
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007715 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007716 akl->kw[0].private = fcn;
7717 akl->kw[0].parse = action_register_lua;
7718
7719 /* select the action registering point. */
7720 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7721 tcp_req_cont_keywords_register(akl);
7722 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7723 tcp_res_cont_keywords_register(akl);
7724 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7725 http_req_keywords_register(akl);
7726 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7727 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007728 else {
7729 release_hlua_function(fcn);
7730 if (akl)
7731 ha_free((char **)&(akl->kw[0].kw));
7732 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007733 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007734 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7735 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007736 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007737
7738 /* pop the environment string. */
7739 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007740
7741 /* reset for next loop */
7742 akl = NULL;
7743 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007744 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007745 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007746
7747 alloc_error:
7748 release_hlua_function(fcn);
7749 ha_free(&akl);
7750 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7751 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007752}
7753
7754static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7755 struct act_rule *rule, char **err)
7756{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007757 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007758
Christopher Faulet280f85b2019-07-15 15:02:04 +02007759 if (px->mode == PR_MODE_HTTP) {
7760 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007761 return ACT_RET_PRS_ERR;
7762 }
7763
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007764 /* Memory for the rule. */
7765 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7766 if (!rule->arg.hlua_rule) {
7767 memprintf(err, "out of memory error");
7768 return ACT_RET_PRS_ERR;
7769 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007770
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007771 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007772 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007773
7774 /* TODO: later accept arguments. */
7775 rule->arg.hlua_rule->args = NULL;
7776
7777 /* Add applet pointer in the rule. */
7778 rule->applet.obj_type = OBJ_TYPE_APPLET;
7779 rule->applet.name = fcn->name;
7780 rule->applet.init = hlua_applet_tcp_init;
7781 rule->applet.fct = hlua_applet_tcp_fct;
7782 rule->applet.release = hlua_applet_tcp_release;
7783 rule->applet.timeout = hlua_timeout_applet;
7784
7785 return 0;
7786}
7787
7788/* This function is an LUA binding used for registering
7789 * "sample-conv" functions. It expects a converter name used
7790 * in the haproxy configuration file, and an LUA function.
7791 */
7792__LJMP static int hlua_register_service(lua_State *L)
7793{
7794 struct action_kw_list *akl;
7795 const char *name;
7796 const char *env;
7797 int ref;
7798 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007799 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007800 struct buffer *trash;
7801 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007802
7803 MAY_LJMP(check_args(L, 3, "register_service"));
7804
7805 /* First argument : converter name. */
7806 name = MAY_LJMP(luaL_checkstring(L, 1));
7807
7808 /* Second argument : environment. */
7809 env = MAY_LJMP(luaL_checkstring(L, 2));
7810
7811 /* Third argument : lua function. */
7812 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7813
Thierry Fournierf67442e2020-11-28 20:41:07 +01007814 /* Check for service already registered */
7815 trash = get_trash_chunk();
7816 chunk_printf(trash, "lua.%s", name);
7817 akw = service_find(trash->area);
7818 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007819 fcn = akw->private;
7820 if (fcn->function_ref[hlua_state_id] != -1) {
7821 ha_warning("Trying to register service 'lua.%s' more than once. "
7822 "This will become a hard error in version 2.5.\n", name);
7823 }
7824 fcn->function_ref[hlua_state_id] = ref;
7825 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007826 }
7827
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007828 /* Allocate and fill the sample fetch keyword struct. */
7829 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7830 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007831 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007832 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007833 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007834 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007835
7836 /* Fill fcn. */
7837 len = strlen("<lua.>") + strlen(name) + 1;
7838 fcn->name = calloc(1, len);
7839 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007840 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007841 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007842 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007843
7844 /* List head */
7845 akl->list.n = akl->list.p = NULL;
7846
7847 /* converter keyword. */
7848 len = strlen("lua.") + strlen(name) + 1;
7849 akl->kw[0].kw = calloc(1, len);
7850 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02007851 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007852
7853 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7854
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007855 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007856 if (strcmp(env, "tcp") == 0)
7857 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007858 else if (strcmp(env, "http") == 0)
7859 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007860 else {
7861 release_hlua_function(fcn);
7862 if (akl)
7863 ha_free((char **)&(akl->kw[0].kw));
7864 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007865 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007866 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02007867 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007868
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007869 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007870 akl->kw[0].private = fcn;
7871
7872 /* End of array. */
7873 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7874
7875 /* Register this new converter */
7876 service_keywords_register(akl);
7877
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007878 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02007879
7880 alloc_error:
7881 release_hlua_function(fcn);
7882 ha_free(&akl);
7883 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7884 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007885}
7886
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007887/* This function initialises Lua cli handler. It copies the
7888 * arguments in the Lua stack and create channel IO objects.
7889 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007890static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007891{
7892 struct hlua *hlua;
7893 struct hlua_function *fcn;
7894 int i;
7895 const char *error;
7896
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007897 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007898 appctx->ctx.hlua_cli.fcn = private;
7899
Willy Tarreaubafbe012017-11-24 17:34:44 +01007900 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007901 if (!hlua) {
7902 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007903 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007904 }
7905 HLUA_INIT(hlua);
7906 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007907
7908 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007909 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007910 * applet_http. It is absolutely compatible.
7911 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007912 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007913 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007914 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007915 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007916 }
7917 appctx->ctx.hlua_cli.task->nice = 0;
7918 appctx->ctx.hlua_cli.task->context = appctx;
7919 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7920
7921 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01007922 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 +01007923 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007924 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007925 }
7926
7927 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007928 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007929 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7930 error = lua_tostring(hlua->T, -1);
7931 else
7932 error = "critical error";
7933 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7934 goto error;
7935 }
7936
7937 /* Check stack available size. */
7938 if (!lua_checkstack(hlua->T, 2)) {
7939 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7940 goto error;
7941 }
7942
7943 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007944 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007945
7946 /* Once the arguments parsed, the CLI is like an AppletTCP,
7947 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007948 */
7949 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7950 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7951 goto error;
7952 }
7953 hlua->nargs = 1;
7954
7955 /* push keywords in the stack. */
7956 for (i = 0; *args[i]; i++) {
7957 /* Check stack available size. */
7958 if (!lua_checkstack(hlua->T, 1)) {
7959 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7960 goto error;
7961 }
7962 lua_pushstring(hlua->T, args[i]);
7963 hlua->nargs++;
7964 }
7965
7966 /* We must initialize the execution timeouts. */
7967 hlua->max_time = hlua_timeout_session;
7968
7969 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007970 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007971
7972 /* It's ok */
7973 return 0;
7974
7975 /* It's not ok. */
7976error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007977 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007978 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007979 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007980 return 1;
7981}
7982
7983static int hlua_cli_io_handler_fct(struct appctx *appctx)
7984{
7985 struct hlua *hlua;
7986 struct stream_interface *si;
7987 struct hlua_function *fcn;
7988
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007989 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007990 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007991 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007992
7993 /* If the stream is disconnect or closed, ldo nothing. */
7994 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7995 return 1;
7996
7997 /* Execute the function. */
7998 switch (hlua_ctx_resume(hlua, 1)) {
7999
8000 /* finished. */
8001 case HLUA_E_OK:
8002 return 1;
8003
8004 /* yield. */
8005 case HLUA_E_AGAIN:
8006 /* We want write. */
8007 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008008 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008009 /* Set the timeout. */
8010 if (hlua->wake_time != TICK_ETERNITY)
8011 task_schedule(hlua->task, hlua->wake_time);
8012 return 0;
8013
8014 /* finished with error. */
8015 case HLUA_E_ERRMSG:
8016 /* Display log. */
8017 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8018 fcn->name, lua_tostring(hlua->T, -1));
8019 lua_pop(hlua->T, 1);
8020 return 1;
8021
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008022 case HLUA_E_ETMOUT:
8023 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8024 fcn->name);
8025 return 1;
8026
8027 case HLUA_E_NOMEM:
8028 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8029 fcn->name);
8030 return 1;
8031
8032 case HLUA_E_YIELD: /* unexpected */
8033 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8034 fcn->name);
8035 return 1;
8036
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008037 case HLUA_E_ERR:
8038 /* Display log. */
8039 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8040 fcn->name);
8041 return 1;
8042
8043 default:
8044 return 1;
8045 }
8046
8047 return 1;
8048}
8049
8050static void hlua_cli_io_release_fct(struct appctx *appctx)
8051{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008052 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008053 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008054}
8055
8056/* This function is an LUA binding used for registering
8057 * new keywords in the cli. It expects a list of keywords
8058 * which are the "path". It is limited to 5 keywords. A
8059 * description of the command, a function to be executed
8060 * for the parsing and a function for io handlers.
8061 */
8062__LJMP static int hlua_register_cli(lua_State *L)
8063{
8064 struct cli_kw_list *cli_kws;
8065 const char *message;
8066 int ref_io;
8067 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008068 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008069 int index;
8070 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008071 struct buffer *trash;
8072 const char *kw[5];
8073 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008074 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008075
8076 MAY_LJMP(check_args(L, 3, "register_cli"));
8077
8078 /* First argument : an array of maximum 5 keywords. */
8079 if (!lua_istable(L, 1))
8080 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8081
8082 /* Second argument : string with contextual message. */
8083 message = MAY_LJMP(luaL_checkstring(L, 2));
8084
8085 /* Third and fourth argument : lua function. */
8086 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8087
Thierry Fournierf67442e2020-11-28 20:41:07 +01008088 /* Check for CLI service already registered */
8089 trash = get_trash_chunk();
8090 index = 0;
8091 lua_pushnil(L);
8092 memset(kw, 0, sizeof(kw));
8093 while (lua_next(L, 1) != 0) {
8094 if (index >= CLI_PREFIX_KW_NB)
8095 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8096 if (lua_type(L, -1) != LUA_TSTRING)
8097 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8098 kw[index] = lua_tostring(L, -1);
8099 if (index == 0)
8100 chunk_printf(trash, "%s", kw[index]);
8101 else
8102 chunk_appendf(trash, " %s", kw[index]);
8103 index++;
8104 lua_pop(L, 1);
8105 }
8106 cli_kw = cli_find_kw_exact((char **)kw);
8107 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008108 fcn = cli_kw->private;
8109 if (fcn->function_ref[hlua_state_id] != -1) {
8110 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8111 "This will become a hard error in version 2.5.\n", trash->area);
8112 }
8113 fcn->function_ref[hlua_state_id] = ref_io;
8114 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008115 }
8116
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008117 /* Allocate and fill the sample fetch keyword struct. */
8118 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008119 if (!cli_kws) {
8120 errmsg = "Lua out of memory error.";
8121 goto error;
8122 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008123 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008124 if (!fcn) {
8125 errmsg = "Lua out of memory error.";
8126 goto error;
8127 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008128
8129 /* Fill path. */
8130 index = 0;
8131 lua_pushnil(L);
8132 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008133 if (index >= 5) {
8134 errmsg = "1st argument must be a table with a maximum of 5 entries";
8135 goto error;
8136 }
8137 if (lua_type(L, -1) != LUA_TSTRING) {
8138 errmsg = "1st argument must be a table filled with strings";
8139 goto error;
8140 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008141 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008142 if (!cli_kws->kw[0].str_kw[index]) {
8143 errmsg = "Lua out of memory error.";
8144 goto error;
8145 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008146 index++;
8147 lua_pop(L, 1);
8148 }
8149
8150 /* Copy help message. */
8151 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008152 if (!cli_kws->kw[0].usage) {
8153 errmsg = "Lua out of memory error.";
8154 goto error;
8155 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008156
8157 /* Fill fcn io handler. */
8158 len = strlen("<lua.cli>") + 1;
8159 for (i = 0; i < index; i++)
8160 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8161 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008162 if (!fcn->name) {
8163 errmsg = "Lua out of memory error.";
8164 goto error;
8165 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008166 strncat((char *)fcn->name, "<lua.cli", len);
8167 for (i = 0; i < index; i++) {
8168 strncat((char *)fcn->name, ".", len);
8169 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8170 }
8171 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008172 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008173
8174 /* Fill last entries. */
8175 cli_kws->kw[0].private = fcn;
8176 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8177 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8178 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8179
8180 /* Register this new converter */
8181 cli_register_kw(cli_kws);
8182
8183 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008184
8185 error:
8186 release_hlua_function(fcn);
8187 if (cli_kws) {
8188 for (i = 0; i < index; i++)
8189 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8190 ha_free((char **)&(cli_kws->kw[0].usage));
8191 }
8192 ha_free(&cli_kws);
8193 WILL_LJMP(luaL_error(L, errmsg));
8194 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008195}
8196
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008197static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008198 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008199 char **err, unsigned int *timeout)
8200{
8201 const char *error;
8202
8203 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008204 if (error == PARSE_TIME_OVER) {
8205 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8206 args[1], args[0]);
8207 return -1;
8208 }
8209 else if (error == PARSE_TIME_UNDER) {
8210 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8211 args[1], args[0]);
8212 return -1;
8213 }
8214 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008215 memprintf(err, "%s: invalid timeout", args[0]);
8216 return -1;
8217 }
8218 return 0;
8219}
8220
8221static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008222 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008223 char **err)
8224{
8225 return hlua_read_timeout(args, section_type, curpx, defpx,
8226 file, line, err, &hlua_timeout_session);
8227}
8228
8229static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008230 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008231 char **err)
8232{
8233 return hlua_read_timeout(args, section_type, curpx, defpx,
8234 file, line, err, &hlua_timeout_task);
8235}
8236
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008237static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008238 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008239 char **err)
8240{
8241 return hlua_read_timeout(args, section_type, curpx, defpx,
8242 file, line, err, &hlua_timeout_applet);
8243}
8244
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008245static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008246 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008247 char **err)
8248{
8249 char *error;
8250
8251 hlua_nb_instruction = strtoll(args[1], &error, 10);
8252 if (*error != '\0') {
8253 memprintf(err, "%s: invalid number", args[0]);
8254 return -1;
8255 }
8256 return 0;
8257}
8258
Willy Tarreau32f61e22015-03-18 17:54:59 +01008259static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008260 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008261 char **err)
8262{
8263 char *error;
8264
8265 if (*(args[1]) == 0) {
8266 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8267 return -1;
8268 }
8269 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8270 if (*error != '\0') {
8271 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8272 return -1;
8273 }
8274 return 0;
8275}
8276
8277
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008278/* This function is called by the main configuration key "lua-load". It loads and
8279 * execute an lua file during the parsing of the HAProxy configuration file. It is
8280 * the main lua entry point.
8281 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008282 * This function runs with the HAProxy keywords API. It returns -1 if an error
8283 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008284 *
8285 * In some error case, LUA set an error message in top of the stack. This function
8286 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008287 *
8288 * This function can fail with an abort() due to an Lua critical error.
8289 * We are in the configuration parsing process of HAProxy, this abort() is
8290 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008291 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008292static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008293{
8294 int error;
8295
8296 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008297 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008298 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008299 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8300 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008301 return -1;
8302 }
8303
8304 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008305 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008306 switch (error) {
8307 case LUA_OK:
8308 break;
8309 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008310 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8311 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008312 return -1;
8313 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008314 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008315 return -1;
8316 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008317 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8318 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008319 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008320#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008321 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008322 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8323 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008324 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008325#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008326 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008327 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8328 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008329 return -1;
8330 }
8331
8332 return 0;
8333}
8334
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008335static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008336 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008337 char **err)
8338{
8339 if (*(args[1]) == 0) {
8340 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8341 return -1;
8342 }
8343
Thierry Fournier59f11be2020-11-29 00:37:41 +01008344 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008345 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008346 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008347 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008348}
8349
Thierry Fournier59f11be2020-11-29 00:37:41 +01008350static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008351 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008352 char **err)
8353{
8354 int len;
8355
8356 if (*(args[1]) == 0) {
8357 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8358 return -1;
8359 }
8360
8361 if (per_thread_load == NULL) {
8362 /* allocate the first entry large enough to store the final NULL */
8363 per_thread_load = calloc(1, sizeof(*per_thread_load));
8364 if (per_thread_load == NULL) {
8365 memprintf(err, "out of memory error");
8366 return -1;
8367 }
8368 }
8369
8370 /* count used entries */
8371 for (len = 0; per_thread_load[len] != NULL; len++)
8372 ;
8373
8374 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8375 if (per_thread_load == NULL) {
8376 memprintf(err, "out of memory error");
8377 return -1;
8378 }
8379
8380 per_thread_load[len] = strdup(args[1]);
8381 per_thread_load[len + 1] = NULL;
8382
8383 if (per_thread_load[len] == NULL) {
8384 memprintf(err, "out of memory error");
8385 return -1;
8386 }
8387
8388 /* loading for thread 1 only */
8389 hlua_state_id = 1;
8390 ha_set_tid(0);
8391 return hlua_load_state(args[1], hlua_states[1], err);
8392}
8393
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008394/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8395 * in the given <ctx>.
8396 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008397static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008398{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008399 lua_getglobal(L, "package"); /* push package variable */
8400 lua_pushstring(L, path); /* push given path */
8401 lua_pushstring(L, ";"); /* push semicolon */
8402 lua_getfield(L, -3, type); /* push old path */
8403 lua_concat(L, 3); /* concatenate to new path */
8404 lua_setfield(L, -2, type); /* store new path */
8405 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008406
8407 return 0;
8408}
8409
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008410static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008411 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008412 char **err)
8413{
8414 char *path;
8415 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008416 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008417
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008418 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008419 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008420 }
8421
8422 if (!(*args[1])) {
8423 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008424 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008425 }
8426 path = args[1];
8427
8428 if (*args[2]) {
8429 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8430 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008431 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008432 }
8433 type = args[2];
8434 }
8435
Thierry Fournier59f11be2020-11-29 00:37:41 +01008436 p = calloc(1, sizeof(*p));
8437 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008438 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008439 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008440 }
8441 p->path = strdup(path);
8442 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008443 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008444 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008445 }
8446 p->type = strdup(type);
8447 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008448 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008449 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008450 }
Willy Tarreau2b718102021-04-21 07:32:39 +02008451 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01008452
8453 hlua_prepend_path(hlua_states[0], type, path);
8454 hlua_prepend_path(hlua_states[1], type, path);
8455 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008456
8457err2:
8458 free(p->type);
8459 free(p->path);
8460err:
8461 free(p);
8462 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008463}
8464
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008465/* configuration keywords declaration */
8466static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008467 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008468 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008469 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008470 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8471 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008472 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008473 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008474 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008475 { 0, NULL, NULL },
8476}};
8477
Willy Tarreau0108d902018-11-25 19:14:37 +01008478INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8479
Christopher Fauletafd8f102018-11-08 11:34:21 +01008480
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008481/* This function can fail with an abort() due to an Lua critical error.
8482 * We are in the initialisation process of HAProxy, this abort() is
8483 * tolerated.
8484 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008485int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008486{
8487 struct hlua_init_function *init;
8488 const char *msg;
8489 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008490 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008491 const char *kind;
8492 const char *trace;
8493 int return_status = 1;
8494#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8495 int nres;
8496#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008497
Willy Tarreaucdb53462020-12-02 12:12:00 +01008498 /* disable memory limit checks if limit is not set */
8499 if (!hlua_global_allocator.limit)
8500 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8501
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008502 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008503 if (setjmp(safe_ljmp_env) != 0) {
8504 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008505 if (lua_type(L, -1) == LUA_TSTRING)
8506 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008507 else
8508 error = "critical error";
8509 fprintf(stderr, "Lua post-init: %s.\n", error);
8510 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008511 } else {
8512 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008513 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008514
Thierry Fournierb8cef172020-11-28 15:37:17 +01008515 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008516
Thierry Fournierc7492592020-11-28 23:57:24 +01008517 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008518 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008519
8520#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008521 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008522#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008523 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008524#endif
8525 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008526 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008527
8528 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008529 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008530 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008531
8532 case LUA_ERRERR:
8533 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008534 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008535 case LUA_ERRRUN:
8536 if (!kind)
8537 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008538 msg = lua_tostring(L, -1);
8539 lua_settop(L, 0); /* Empty the stack. */
8540 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008541 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008542 if (msg)
8543 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8544 else
8545 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8546 return_status = 0;
8547 break;
8548
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008549 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008550 /* Unknown error */
8551 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008552 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008553 case LUA_YIELD:
8554 /* yield is not configured at this step, this state doesn't happen */
8555 if (!kind)
8556 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008557 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008558 case LUA_ERRMEM:
8559 if (!kind)
8560 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008561 lua_settop(L, 0);
8562 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008563 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008564 ha_alert("Lua init: %s: %s\n", kind, trace);
8565 return_status = 0;
8566 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008567 }
Thierry Fournier670db242020-11-28 10:49:59 +01008568 if (!return_status)
8569 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008570 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008571
8572 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008573 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008574}
8575
Thierry Fournierb8cef172020-11-28 15:37:17 +01008576int hlua_post_init()
8577{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008578 int ret;
8579 int i;
8580 int errors;
8581 char *err = NULL;
8582 struct hlua_function *fcn;
8583
Thierry Fournierb8cef172020-11-28 15:37:17 +01008584#if USE_OPENSSL
8585 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008586 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008587 int saved_used_backed = global.ssl_used_backend;
8588 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008589 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008590 global.ssl_used_backend = saved_used_backed;
8591 }
8592#endif
8593
Thierry Fournierc7492592020-11-28 23:57:24 +01008594 /* Perform post init of common thread */
8595 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008596 ha_set_tid(0);
8597 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8598 if (ret == 0)
8599 return 0;
8600
8601 /* init remaining lua states and load files */
8602 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8603
8604 /* set thread context */
8605 ha_set_tid(hlua_state_id - 1);
8606
8607 /* Init lua state */
8608 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8609
8610 /* Load lua files */
8611 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8612 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8613 if (ret != 0) {
8614 ha_alert("Lua init: %s\n", err);
8615 return 0;
8616 }
8617 }
8618 }
8619
8620 /* Reset thread context */
8621 ha_set_tid(0);
8622
8623 /* Execute post init for all states */
8624 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8625
8626 /* set thread context */
8627 ha_set_tid(hlua_state_id - 1);
8628
8629 /* run post init */
8630 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8631 if (ret == 0)
8632 return 0;
8633 }
8634
8635 /* Reset thread context */
8636 ha_set_tid(0);
8637
8638 /* control functions registering. Each function must have:
8639 * - only the function_ref[0] set positive and all other to -1
8640 * - only the function_ref[0] set to -1 and all other positive
8641 * This ensure a same reference is not used both in shared
8642 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008643 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008644 * complicated to found for the end user.
8645 */
8646 errors = 0;
8647 list_for_each_entry(fcn, &referenced_functions, l) {
8648 ret = 0;
8649 for (i = 1; i < global.nbthread + 1; i++) {
8650 if (fcn->function_ref[i] == -1)
8651 ret--;
8652 else
8653 ret++;
8654 }
8655 if (abs(ret) != global.nbthread) {
8656 ha_alert("Lua function '%s' is not referenced in all thread. "
8657 "Expect function in all thread or in none thread.\n", fcn->name);
8658 errors++;
8659 continue;
8660 }
8661
8662 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008663 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8664 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008665 "exclusive.\n", fcn->name);
8666 errors++;
8667 }
8668 }
8669
8670 if (errors > 0)
8671 return 0;
8672
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008673 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008674 * -1 in order to have probably a segfault if someone use it
8675 */
8676 hlua_state_id = -1;
8677
8678 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008679}
8680
Willy Tarreau32f61e22015-03-18 17:54:59 +01008681/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8682 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8683 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008684 * allocation. <nsize> is the requested new size. A new allocation is
8685 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008686 * zero. This one verifies that the limits are respected but is optimized
8687 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008688 */
8689static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8690{
8691 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008692 size_t limit, old, new;
8693
Tim Duesterhus22586522021-01-08 10:35:33 +01008694 if (unlikely(!ptr && !nsize))
8695 return NULL;
8696
Willy Tarreaucdb53462020-12-02 12:12:00 +01008697 /* a limit of ~0 means unlimited and boot complete, so there's no need
8698 * for accounting anymore.
8699 */
Christopher Fauletcc2c4f82021-03-24 14:52:24 +01008700 if (likely(~zone->limit == 0))
8701 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008702
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008703 if (!ptr)
8704 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008705
Willy Tarreaucdb53462020-12-02 12:12:00 +01008706 /* enforce strict limits across all threads */
8707 limit = zone->limit;
8708 old = _HA_ATOMIC_LOAD(&zone->allocated);
8709 do {
8710 new = old + nsize - osize;
8711 if (unlikely(nsize && limit && new > limit))
8712 return NULL;
8713 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008714
8715 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008716
8717 if (unlikely(!ptr && nsize)) // failed
8718 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8719
8720 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008721 return ptr;
8722}
8723
Thierry Fournierecb83c22020-11-28 15:49:44 +01008724/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008725 * We are in the initialisation process of HAProxy, this abort() is
8726 * tolerated.
8727 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008728lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008729{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008730 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008731 int idx;
8732 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008733 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008734 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008735 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008736 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008737 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008738 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008739
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008740 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008741 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008742
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008743 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008744 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008745 *context = NULL;
8746
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008747 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008748 * the Lua function can fail with an abort. We are in the initialisation
8749 * process of HAProxy, this abort() is tolerated.
8750 */
8751
Thierry Fournier3c539322020-11-28 16:05:05 +01008752 /* Call post initialisation function in safe environment. */
8753 if (setjmp(safe_ljmp_env) != 0) {
8754 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008755 if (lua_type(L, -1) == LUA_TSTRING)
8756 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008757 else
8758 error_msg = "critical error";
8759 fprintf(stderr, "Lua init: %s.\n", error_msg);
8760 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008761 } else {
8762 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008763 }
8764
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008765 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008766 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008767#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8768#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8769#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008770 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008771#endif
8772#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008773 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008774#endif
8775#undef HLUA_PREPEND_PATH_TOSTRING
8776#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008777
Thierry Fournier59f11be2020-11-29 00:37:41 +01008778 /* Apply configured prepend path */
8779 list_for_each_entry(pp, &prepend_path_list, l)
8780 hlua_prepend_path(L, pp->type, pp->path);
8781
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008782 /*
8783 *
8784 * Create "core" object.
8785 *
8786 */
8787
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008788 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008789 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008790
Thierry Fournierecb83c22020-11-28 15:49:44 +01008791 /* set the thread id */
8792 hlua_class_const_int(L, "thread", thread_num);
8793
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008794 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008795 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008796 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008797
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008798 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008799 hlua_class_function(L, "register_init", hlua_register_init);
8800 hlua_class_function(L, "register_task", hlua_register_task);
8801 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8802 hlua_class_function(L, "register_converters", hlua_register_converters);
8803 hlua_class_function(L, "register_action", hlua_register_action);
8804 hlua_class_function(L, "register_service", hlua_register_service);
8805 hlua_class_function(L, "register_cli", hlua_register_cli);
8806 hlua_class_function(L, "yield", hlua_yield);
8807 hlua_class_function(L, "set_nice", hlua_set_nice);
8808 hlua_class_function(L, "sleep", hlua_sleep);
8809 hlua_class_function(L, "msleep", hlua_msleep);
8810 hlua_class_function(L, "add_acl", hlua_add_acl);
8811 hlua_class_function(L, "del_acl", hlua_del_acl);
8812 hlua_class_function(L, "set_map", hlua_set_map);
8813 hlua_class_function(L, "del_map", hlua_del_map);
8814 hlua_class_function(L, "tcp", hlua_socket_new);
8815 hlua_class_function(L, "log", hlua_log);
8816 hlua_class_function(L, "Debug", hlua_log_debug);
8817 hlua_class_function(L, "Info", hlua_log_info);
8818 hlua_class_function(L, "Warning", hlua_log_warning);
8819 hlua_class_function(L, "Alert", hlua_log_alert);
8820 hlua_class_function(L, "done", hlua_done);
8821 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008822
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008823 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008824
8825 /*
8826 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008827 * Create "act" object.
8828 *
8829 */
8830
8831 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008832 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008833
8834 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008835 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8836 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8837 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8838 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8839 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8840 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8841 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8842 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008843
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008844 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008845
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008846 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008847
8848 /*
8849 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008850 * Register class Map
8851 *
8852 */
8853
8854 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008855 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008856
8857 /* register pattern types. */
8858 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008859 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008860 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008861 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008862 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008863 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008864
8865 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008866 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008867
8868 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008869 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008870
Ilya Shipitsind4259502020-04-08 01:07:56 +05008871 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008872 lua_pushstring(L, "__index");
8873 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008874
8875 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008876 hlua_class_function(L, "lookup", hlua_map_lookup);
8877 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008878
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008879 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008880
Thierry Fournier45e78d72016-02-19 18:34:46 +01008881 /* Register previous table in the registry with reference and named entry.
8882 * The function hlua_register_metatable() pops the stack, so we
8883 * previously create a copy of the table.
8884 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008885 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8886 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008887
8888 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008889 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008890
8891 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008892 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008893
8894 /*
8895 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008896 * Register class Channel
8897 *
8898 */
8899
8900 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008901 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008902
Ilya Shipitsind4259502020-04-08 01:07:56 +05008903 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008904 lua_pushstring(L, "__index");
8905 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008906
8907 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008908 hlua_class_function(L, "get", hlua_channel_get);
8909 hlua_class_function(L, "dup", hlua_channel_dup);
8910 hlua_class_function(L, "getline", hlua_channel_getline);
8911 hlua_class_function(L, "set", hlua_channel_set);
8912 hlua_class_function(L, "append", hlua_channel_append);
8913 hlua_class_function(L, "send", hlua_channel_send);
8914 hlua_class_function(L, "forward", hlua_channel_forward);
8915 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
8916 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
8917 hlua_class_function(L, "is_full", hlua_channel_is_full);
8918 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008919
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008920 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008921
8922 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008923 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008924
8925 /*
8926 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008927 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008928 *
8929 */
8930
8931 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008932 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008933
Ilya Shipitsind4259502020-04-08 01:07:56 +05008934 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008935 lua_pushstring(L, "__index");
8936 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008937
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008938 /* Browse existing fetches and create the associated
8939 * object method.
8940 */
8941 sf = NULL;
8942 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008943 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8944 * by an underscore.
8945 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008946 strncpy(trash.area, sf->kw, trash.size);
8947 trash.area[trash.size - 1] = '\0';
8948 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008949 if (*p == '.' || *p == '-' || *p == '+')
8950 *p = '_';
8951
8952 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008953 lua_pushstring(L, trash.area);
8954 lua_pushlightuserdata(L, sf);
8955 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
8956 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008957 }
8958
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008959 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008960
8961 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008962 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008963
8964 /*
8965 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008966 * Register class Converters
8967 *
8968 */
8969
8970 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008971 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008972
8973 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008974 lua_pushstring(L, "__index");
8975 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008976
8977 /* Browse existing converters and create the associated
8978 * object method.
8979 */
8980 sc = NULL;
8981 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008982 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8983 * by an underscore.
8984 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008985 strncpy(trash.area, sc->kw, trash.size);
8986 trash.area[trash.size - 1] = '\0';
8987 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008988 if (*p == '.' || *p == '-' || *p == '+')
8989 *p = '_';
8990
8991 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008992 lua_pushstring(L, trash.area);
8993 lua_pushlightuserdata(L, sc);
8994 lua_pushcclosure(L, hlua_run_sample_conv, 1);
8995 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008996 }
8997
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008998 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008999
9000 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009001 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009002
9003 /*
9004 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009005 * Register class HTTP
9006 *
9007 */
9008
9009 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009010 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009011
Ilya Shipitsind4259502020-04-08 01:07:56 +05009012 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009013 lua_pushstring(L, "__index");
9014 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009015
9016 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009017 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9018 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9019 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9020 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9021 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9022 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9023 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9024 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9025 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9026 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009027
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009028 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9029 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9030 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9031 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9032 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9033 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9034 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009035
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009036 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009037
9038 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009039 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009040
9041 /*
9042 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009043 * Register class AppletTCP
9044 *
9045 */
9046
9047 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009048 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009049
Ilya Shipitsind4259502020-04-08 01:07:56 +05009050 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009051 lua_pushstring(L, "__index");
9052 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009053
9054 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009055 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9056 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9057 hlua_class_function(L, "send", hlua_applet_tcp_send);
9058 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9059 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9060 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9061 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9062 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009063
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009064 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009065
9066 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009067 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009068
9069 /*
9070 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009071 * Register class AppletHTTP
9072 *
9073 */
9074
9075 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009076 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009077
Ilya Shipitsind4259502020-04-08 01:07:56 +05009078 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009079 lua_pushstring(L, "__index");
9080 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009081
9082 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009083 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9084 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9085 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9086 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9087 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9088 hlua_class_function(L, "getline", hlua_applet_http_getline);
9089 hlua_class_function(L, "receive", hlua_applet_http_recv);
9090 hlua_class_function(L, "send", hlua_applet_http_send);
9091 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9092 hlua_class_function(L, "set_status", hlua_applet_http_status);
9093 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009094
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009095 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009096
9097 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009098 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009099
9100 /*
9101 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009102 * Register class TXN
9103 *
9104 */
9105
9106 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009107 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009108
Ilya Shipitsind4259502020-04-08 01:07:56 +05009109 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009110 lua_pushstring(L, "__index");
9111 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009112
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009113 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009114 hlua_class_function(L, "set_priv", hlua_set_priv);
9115 hlua_class_function(L, "get_priv", hlua_get_priv);
9116 hlua_class_function(L, "set_var", hlua_set_var);
9117 hlua_class_function(L, "unset_var", hlua_unset_var);
9118 hlua_class_function(L, "get_var", hlua_get_var);
9119 hlua_class_function(L, "done", hlua_txn_done);
9120 hlua_class_function(L, "reply", hlua_txn_reply_new);
9121 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9122 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9123 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9124 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9125 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9126 hlua_class_function(L, "deflog", hlua_txn_deflog);
9127 hlua_class_function(L, "log", hlua_txn_log);
9128 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9129 hlua_class_function(L, "Info", hlua_txn_log_info);
9130 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9131 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009132
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009133 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009134
9135 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009136 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009137
9138 /*
9139 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009140 * Register class reply
9141 *
9142 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009143 lua_newtable(L);
9144 lua_pushstring(L, "__index");
9145 lua_newtable(L);
9146 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9147 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9148 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9149 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9150 lua_settable(L, -3); /* Sets the __index entry. */
9151 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009152
9153
9154 /*
9155 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009156 * Register class Socket
9157 *
9158 */
9159
9160 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009161 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009162
Ilya Shipitsind4259502020-04-08 01:07:56 +05009163 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009164 lua_pushstring(L, "__index");
9165 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009166
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009167#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009168 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009169#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009170 hlua_class_function(L, "connect", hlua_socket_connect);
9171 hlua_class_function(L, "send", hlua_socket_send);
9172 hlua_class_function(L, "receive", hlua_socket_receive);
9173 hlua_class_function(L, "close", hlua_socket_close);
9174 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9175 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9176 hlua_class_function(L, "setoption", hlua_socket_setoption);
9177 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009178
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009179 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009180
9181 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009182 lua_pushstring(L, "__gc");
9183 lua_pushcclosure(L, hlua_socket_gc, 0);
9184 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009185
9186 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009187 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009188
Thierry Fournieraafc7772020-12-04 11:47:47 +01009189 lua_atpanic(L, hlua_panic_safe);
9190
9191 return L;
9192}
9193
9194void hlua_init(void) {
9195 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009196 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009197#ifdef USE_OPENSSL
9198 struct srv_kw *kw;
9199 int tmp_error;
9200 char *error;
9201 char *args[] = { /* SSL client configuration. */
9202 "ssl",
9203 "verify",
9204 "none",
9205 NULL
9206 };
9207#endif
9208
9209 /* Init post init function list head */
9210 for (i = 0; i < MAX_THREADS + 1; i++)
9211 LIST_INIT(&hlua_init_functions[i]);
9212
9213 /* Init state for common/shared lua parts */
9214 hlua_state_id = 0;
9215 ha_set_tid(0);
9216 hlua_states[0] = hlua_init_state(0);
9217
9218 /* Init state 1 for thread 0. We have at least one thread. */
9219 hlua_state_id = 1;
9220 ha_set_tid(0);
9221 hlua_states[1] = hlua_init_state(1);
9222
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009223 /* Proxy and server configuration initialisation. */
William Lallemand6bb77b92021-07-28 15:48:16 +02009224 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg);
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009225 if (!socket_proxy) {
9226 fprintf(stderr, "Lua init: %s\n", errmsg);
9227 exit(1);
9228 }
9229 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009230
9231 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009232 socket_tcp = new_server(socket_proxy);
9233 if (!socket_tcp) {
9234 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9235 exit(1);
9236 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009237
9238#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009239 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009240 socket_ssl = new_server(socket_proxy);
9241 if (!socket_ssl) {
9242 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9243 exit(1);
9244 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009245
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009246 socket_ssl->use_ssl = 1;
9247 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009248
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009249 for (i = 0; args[i] != NULL; i++) {
9250 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009251 /*
9252 *
9253 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009254 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009255 * features like client certificates and ssl_verify.
9256 *
9257 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009258 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009259 if (tmp_error != 0) {
9260 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9261 abort(); /* This must be never arrives because the command line
9262 not editable by the user. */
9263 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009264 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009265 }
9266 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009267#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009268
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009269}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009270
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009271static void hlua_deinit()
9272{
Willy Tarreau186f3762020-12-04 11:48:12 +01009273 int thr;
9274
9275 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9276 if (hlua_states[thr])
9277 lua_close(hlua_states[thr]);
9278 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009279
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009280 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009281
Willy Tarreau0f143af2021-03-05 10:41:48 +01009282#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009283 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009284#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009285
9286 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009287}
9288
9289REGISTER_POST_DEINIT(hlua_deinit);
9290
Willy Tarreau80713382018-11-26 10:19:54 +01009291static void hlua_register_build_options(void)
9292{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009293 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009294
Willy Tarreaubb57d942016-12-21 19:04:56 +01009295 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9296 hap_register_build_opts(ptr, 1);
9297}
Willy Tarreau80713382018-11-26 10:19:54 +01009298
9299INITCALL0(STG_REGISTER, hlua_register_build_options);