blob: 5c2d3ccd674fe0005b5ab4cbaa1f6968fd670c79 [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
Christopher Fauletba9e21d2020-02-25 10:20:04 +0100353/* Used to check an Lua table type in the stack. It creates and
354 * returns a reference of the table. This function throws an
355 * error if the rgument is not a "table".
356 */
357__LJMP unsigned int hlua_checktable(lua_State *L, int argno)
358{
359 if (!lua_istable(L, argno)) {
360 const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno));
361 WILL_LJMP(luaL_argerror(L, argno, msg));
362 }
363 lua_pushvalue(L, argno);
364 return luaL_ref(L, LUA_REGISTRYINDEX);
365}
366
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200367/* Return the string that is of the top of the stack. */
368const char *hlua_get_top_error_string(lua_State *L)
369{
370 if (lua_gettop(L) < 1)
371 return "unknown error";
372 if (lua_type(L, -1) != LUA_TSTRING)
373 return "unknown error";
374 return lua_tostring(L, -1);
375}
376
Christopher Fauletd09cc512021-03-24 14:48:45 +0100377__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200378{
379 lua_Debug ar;
380 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200381 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200382
383 while (lua_getstack(L, level++, &ar)) {
384
385 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100386 if (b_data(msg))
387 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200388
389 /* Fill fields:
390 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
391 * 'l': fills in the field currentline;
392 * 'n': fills in the field name and namewhat;
393 * 't': fills in the field istailcall;
394 */
395 lua_getinfo(L, "Slnt", &ar);
396
397 /* Append code localisation */
398 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100399 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200400 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100401 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200402
403 /*
404 * Get function name
405 *
406 * if namewhat is no empty, name is defined.
407 * what contains "Lua" for Lua function, "C" for C function,
408 * or "main" for main code.
409 */
410 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100411 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200412
413 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100414 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200415
416 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100417 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200418
419 else /* nothing left... */
420 chunk_appendf(msg, "?");
421
422
423 /* Display tailed call */
424 if (ar.istailcall)
425 chunk_appendf(msg, " ...");
426 }
427
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200428 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200429}
430
431
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100432/* This function check the number of arguments available in the
433 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500434 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100435 */
436__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
437{
438 if (lua_gettop(L) == nb)
439 return;
440 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
441}
442
Mark Lakes22154b42018-01-29 14:38:40 -0800443/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100444 * and the line number where the error is encountered.
445 */
446static int hlua_pusherror(lua_State *L, const char *fmt, ...)
447{
448 va_list argp;
449 va_start(argp, fmt);
450 luaL_where(L, 1);
451 lua_pushvfstring(L, fmt, argp);
452 va_end(argp);
453 lua_concat(L, 2);
454 return 1;
455}
456
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457/* This functions is used with sample fetch and converters. It
458 * converts the HAProxy configuration argument in a lua stack
459 * values.
460 *
461 * It takes an array of "arg", and each entry of the array is
462 * converted and pushed in the LUA stack.
463 */
464static int hlua_arg2lua(lua_State *L, const struct arg *arg)
465{
466 switch (arg->type) {
467 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100468 case ARGT_TIME:
469 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100470 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100471 break;
472
473 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200474 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100475 break;
476
477 case ARGT_IPV4:
478 case ARGT_IPV6:
479 case ARGT_MSK4:
480 case ARGT_MSK6:
481 case ARGT_FE:
482 case ARGT_BE:
483 case ARGT_TAB:
484 case ARGT_SRV:
485 case ARGT_USR:
486 case ARGT_MAP:
487 default:
488 lua_pushnil(L);
489 break;
490 }
491 return 1;
492}
493
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500494/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100495 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500496 * with sample fetch wrappers. The input arguments are given to the
497 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100498 */
499static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
500{
501 switch (lua_type(L, ud)) {
502
503 case LUA_TNUMBER:
504 case LUA_TBOOLEAN:
505 arg->type = ARGT_SINT;
506 arg->data.sint = lua_tointeger(L, ud);
507 break;
508
509 case LUA_TSTRING:
510 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200511 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200512 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200513 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200514 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100515 break;
516
517 case LUA_TUSERDATA:
518 case LUA_TNIL:
519 case LUA_TTABLE:
520 case LUA_TFUNCTION:
521 case LUA_TTHREAD:
522 case LUA_TLIGHTUSERDATA:
523 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200524 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100525 break;
526 }
527 return 1;
528}
529
530/* the following functions are used to convert a struct sample
531 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500532 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100533 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100534static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100535{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200536 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100537 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100538 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200539 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540 break;
541
542 case SMP_T_BIN:
543 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200544 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100545 break;
546
547 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200548 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100549 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
550 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
551 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
552 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
553 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
554 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
555 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
556 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
557 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200558 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100559 break;
560 default:
561 lua_pushnil(L);
562 break;
563 }
564 break;
565
566 case SMP_T_IPV4:
567 case SMP_T_IPV6:
568 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200569 if (sample_casts[smp->data.type][SMP_T_STR] &&
570 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200571 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100572 else
573 lua_pushnil(L);
574 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100575 default:
576 lua_pushnil(L);
577 break;
578 }
579 return 1;
580}
581
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100582/* the following functions are used to convert a struct sample
583 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500584 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100585 */
586static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
587{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200588 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100589
590 case SMP_T_BIN:
591 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200592 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100593 break;
594
595 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200596 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100597 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
598 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
599 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
600 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
601 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
602 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
603 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
604 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
605 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200606 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100607 break;
608 default:
609 lua_pushstring(L, "");
610 break;
611 }
612 break;
613
614 case SMP_T_SINT:
615 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100616 case SMP_T_IPV4:
617 case SMP_T_IPV6:
618 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200619 if (sample_casts[smp->data.type][SMP_T_STR] &&
620 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200621 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100622 else
623 lua_pushstring(L, "");
624 break;
625 default:
626 lua_pushstring(L, "");
627 break;
628 }
629 return 1;
630}
631
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100632/* the following functions are used to convert an Lua type in a
633 * struct sample. This is useful to provide data from a converter
634 * to the LUA code.
635 */
636static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
637{
638 switch (lua_type(L, ud)) {
639
640 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200641 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200642 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100643 break;
644
645
646 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200647 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200648 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100649 break;
650
651 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200652 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200654 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200655 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200656 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200657 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100658 break;
659
660 case LUA_TUSERDATA:
661 case LUA_TNIL:
662 case LUA_TTABLE:
663 case LUA_TFUNCTION:
664 case LUA_TTHREAD:
665 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200666 case LUA_TNONE:
667 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200668 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200669 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100670 break;
671 }
672 return 1;
673}
674
Ilya Shipitsind4259502020-04-08 01:07:56 +0500675/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800676 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100677 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100678 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500679 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Willy Tarreauee0d7272021-07-16 10:26:56 +0200680 * entries and that there is at least one stop at the last position.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100681 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100682__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100683 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100684{
685 int min_arg;
Willy Tarreauee0d7272021-07-16 10:26:56 +0200686 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100687 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200688 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200689 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200690 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200691 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100692
693 idx = 0;
694 min_arg = ARGM(mask);
695 mask >>= ARGM_BITS;
696
697 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200698 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100699
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100700 /* Check for mandatory arguments. */
701 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100702 if (idx < min_arg) {
703
704 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200705 if (idx > 0) {
706 msg = "Mandatory argument expected";
707 goto error;
708 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100709
710 /* If first argument have a certain type, some default values
711 * may be used. See the function smp_resolve_args().
712 */
713 switch (mask & ARGT_MASK) {
714
715 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200716 if (!(p->cap & PR_CAP_FE)) {
717 msg = "Mandatory argument expected";
718 goto error;
719 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100720 argp[idx].data.prx = p;
721 argp[idx].type = ARGT_FE;
722 argp[idx+1].type = ARGT_STOP;
723 break;
724
725 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200726 if (!(p->cap & PR_CAP_BE)) {
727 msg = "Mandatory argument expected";
728 goto error;
729 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100730 argp[idx].data.prx = p;
731 argp[idx].type = ARGT_BE;
732 argp[idx+1].type = ARGT_STOP;
733 break;
734
735 case ARGT_TAB:
736 argp[idx].data.prx = p;
737 argp[idx].type = ARGT_TAB;
738 argp[idx+1].type = ARGT_STOP;
739 break;
740
741 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200742 msg = "Mandatory argument expected";
743 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100744 break;
745 }
746 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200747 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100748 }
749
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500750 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100751 if ((mask & ARGT_MASK) == ARGT_STOP &&
752 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200753 msg = "Last argument expected";
754 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100755 }
756
757 if ((mask & ARGT_MASK) == ARGT_STOP &&
758 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200759 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100760 }
761
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200762 /* Convert some argument types. All string in argp[] are for not
763 * duplicated yet.
764 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100765 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100766 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200767 if (argp[idx].type != ARGT_SINT) {
768 msg = "integer expected";
769 goto error;
770 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100771 argp[idx].type = ARGT_SINT;
772 break;
773
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100774 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200775 if (argp[idx].type != ARGT_SINT) {
776 msg = "integer expected";
777 goto error;
778 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200779 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100780 break;
781
782 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200783 if (argp[idx].type != ARGT_SINT) {
784 msg = "integer expected";
785 goto error;
786 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200787 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100788 break;
789
790 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200791 if (argp[idx].type != ARGT_STR) {
792 msg = "string expected";
793 goto error;
794 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200795 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200796 if (!argp[idx].data.prx) {
797 msg = "frontend doesn't exist";
798 goto error;
799 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100800 argp[idx].type = ARGT_FE;
801 break;
802
803 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200804 if (argp[idx].type != ARGT_STR) {
805 msg = "string expected";
806 goto error;
807 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200808 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200809 if (!argp[idx].data.prx) {
810 msg = "backend doesn't exist";
811 goto error;
812 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100813 argp[idx].type = ARGT_BE;
814 break;
815
816 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200817 if (argp[idx].type != ARGT_STR) {
818 msg = "string expected";
819 goto error;
820 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200821 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200822 if (!argp[idx].data.t) {
823 msg = "table doesn't exist";
824 goto error;
825 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100826 argp[idx].type = ARGT_TAB;
827 break;
828
829 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200830 if (argp[idx].type != ARGT_STR) {
831 msg = "string expected";
832 goto error;
833 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200834 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100835 if (sname) {
836 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200837 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200838 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200839 if (!px) {
840 msg = "backend doesn't exist";
841 goto error;
842 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100843 }
844 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200845 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100846 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100847 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100848 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200849 if (!argp[idx].data.srv) {
850 msg = "server doesn't exist";
851 goto error;
852 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100853 argp[idx].type = ARGT_SRV;
854 break;
855
856 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200857 if (argp[idx].type != ARGT_STR) {
858 msg = "string expected";
859 goto error;
860 }
861 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
862 msg = "invalid IPv4 address";
863 goto error;
864 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100865 argp[idx].type = ARGT_IPV4;
866 break;
867
868 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200869 if (argp[idx].type == ARGT_SINT)
870 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
871 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200872 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
873 msg = "invalid IPv4 mask";
874 goto error;
875 }
876 }
877 else {
878 msg = "integer or string expected";
879 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200880 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100881 argp[idx].type = ARGT_MSK4;
882 break;
883
884 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200885 if (argp[idx].type != ARGT_STR) {
886 msg = "string expected";
887 goto error;
888 }
889 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
890 msg = "invalid IPv6 address";
891 goto error;
892 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100893 argp[idx].type = ARGT_IPV6;
894 break;
895
896 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200897 if (argp[idx].type == ARGT_SINT)
898 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
899 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200900 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
901 msg = "invalid IPv6 mask";
902 goto error;
903 }
904 }
905 else {
906 msg = "integer or string expected";
907 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200908 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100909 argp[idx].type = ARGT_MSK6;
910 break;
911
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200912 case ARGT_REG:
913 if (argp[idx].type != ARGT_STR) {
914 msg = "string expected";
915 goto error;
916 }
917 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
918 if (!reg) {
919 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
920 argp[idx].data.str.area, err);
921 free(err);
922 goto error;
923 }
924 argp[idx].type = ARGT_REG;
925 argp[idx].data.reg = reg;
926 break;
927
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100928 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200929 if (argp[idx].type != ARGT_STR) {
930 msg = "string expected";
931 goto error;
932 }
933 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100934 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200935 ul = p->uri_auth->userlist;
936 else
937 ul = auth_find_userlist(argp[idx].data.str.area);
938
939 if (!ul) {
940 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
941 goto error;
942 }
943 argp[idx].type = ARGT_USR;
944 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200945 break;
946
947 case ARGT_STR:
948 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
949 msg = "unable to duplicate string arg";
950 goto error;
951 }
952 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100953 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200954
Christopher Fauletd25d9262020-08-06 11:04:46 +0200955 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200956 msg = "type not yet supported";
957 goto error;
958 break;
959
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100960 }
961
962 /* Check for type of argument. */
963 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200964 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
965 arg_type_names[(mask & ARGT_MASK)],
966 arg_type_names[argp[idx].type & ARGT_MASK]);
967 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100968 }
969
970 /* Next argument. */
971 mask >>= ARGT_BITS;
972 idx++;
973 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200974 return 0;
975
976 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +0200977 free_args(argp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200978 WILL_LJMP(luaL_argerror(L, first + idx, msg));
979 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100980}
981
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100982/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500983 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100984 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100985 *
986 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100987 * - hlua_sethlua : create the association between hlua context and lua_state.
988 */
989static inline struct hlua *hlua_gethlua(lua_State *L)
990{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100991 struct hlua **hlua = lua_getextraspace(L);
992 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100993}
994static inline void hlua_sethlua(struct hlua *hlua)
995{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100996 struct hlua **hlua_store = lua_getextraspace(hlua->T);
997 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100998}
999
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001000/* This function is used to send logs. It try to send on screen (stderr)
1001 * and on the default syslog server.
1002 */
1003static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
1004{
1005 struct tm tm;
1006 char *p;
1007
1008 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001009 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001010 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001011 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001012 /* Break the message if exceed the buffer size. */
1013 *(p-4) = ' ';
1014 *(p-3) = '.';
1015 *(p-2) = '.';
1016 *(p-1) = '.';
1017 break;
1018 }
Willy Tarreau90807112020-02-25 08:16:33 +01001019 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001020 *p = *msg;
1021 else
1022 *p = '.';
1023 }
1024 *p = '\0';
1025
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001026 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001027 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001028 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1029 return;
1030
Willy Tarreaua678b432015-08-28 10:14:59 +02001031 get_localtime(date.tv_sec, &tm);
1032 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001033 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001034 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001035 fflush(stderr);
1036 }
1037}
1038
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001039/* This function just ensure that the yield will be always
1040 * returned with a timeout and permit to set some flags
1041 */
1042__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001043 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001044{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001045 struct hlua *hlua;
1046
1047 /* Get hlua struct, or NULL if we execute from main lua state */
1048 hlua = hlua_gethlua(L);
1049 if (!hlua) {
1050 return;
1051 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001052
1053 /* Set the wake timeout. If timeout is required, we set
1054 * the expiration time.
1055 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001056 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001057
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001058 hlua->flags |= flags;
1059
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001060 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001061 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001062}
1063
Willy Tarreau87b09662015-04-03 00:22:06 +02001064/* This function initialises the Lua environment stored in the stream.
1065 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001067 *
1068 * This function is particular. it initialises a new Lua thread. If the
1069 * initialisation fails (example: out of memory error), the lua function
1070 * throws an error (longjmp).
1071 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001072 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001073 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001074 * threads appear, the safe environment set a lock to ensure only one
1075 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001076 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001077 *
1078 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001079 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001080 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001081 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001082 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001083 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001084 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001085 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001086int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001087{
1088 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001089 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001090 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001091 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001092 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001093 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001094 if (!already_safe) {
1095 if (!SET_SAFE_LJMP_PARENT(lua)) {
1096 lua->Tref = LUA_REFNIL;
1097 return 0;
1098 }
1099 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001100 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001101 if (!lua->T) {
1102 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001103 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001104 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001105 return 0;
1106 }
1107 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001108 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001109 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001110 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001111 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001112 return 1;
1113}
1114
Willy Tarreau87b09662015-04-03 00:22:06 +02001115/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001116 * is destroyed. The destroy also the memory context. The struct "lua"
1117 * is not freed.
1118 */
1119void hlua_ctx_destroy(struct hlua *lua)
1120{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001121 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001122 return;
1123
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001124 if (!lua->T)
1125 goto end;
1126
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001127 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001128 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001129
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001130 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001131 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001132 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001133 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001134
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 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001138 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001139 /* Forces a garbage collecting process. If the Lua program is finished
1140 * without error, we run the GC on the thread pointer. Its freed all
1141 * the unused memory.
1142 * If the thread is finnish with an error or is currently yielded,
1143 * it seems that the GC applied on the thread doesn't clean anything,
1144 * so e run the GC on the main thread.
1145 * NOTE: maybe this action locks all the Lua threads untiml the en of
1146 * the garbage collection.
1147 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001148 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001149 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001150 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001151 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001152 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001153 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001154
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001155 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001156
1157end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001158 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001159}
1160
1161/* This function is used to restore the Lua context when a coroutine
1162 * fails. This function copy the common memory between old coroutine
1163 * and the new coroutine. The old coroutine is destroyed, and its
1164 * replaced by the new coroutine.
1165 * If the flag "keep_msg" is set, the last entry of the old is assumed
1166 * as string error message and it is copied in the new stack.
1167 */
1168static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1169{
1170 lua_State *T;
1171 int new_ref;
1172
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001174 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001175 if (!T)
1176 return 0;
1177
1178 /* Copy last error message. */
1179 if (keep_msg)
1180 lua_xmove(lua->T, T, 1);
1181
1182 /* Copy data between the coroutines. */
1183 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1184 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001185 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001186
1187 /* Destroy old data. */
1188 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1189
1190 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001191 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001192
1193 /* Fill the struct with the new coroutine values. */
1194 lua->Mref = new_ref;
1195 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001196 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001197
1198 /* Set context. */
1199 hlua_sethlua(lua);
1200
1201 return 1;
1202}
1203
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001204void hlua_hook(lua_State *L, lua_Debug *ar)
1205{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001206 struct hlua *hlua;
1207
1208 /* Get hlua struct, or NULL if we execute from main lua state */
1209 hlua = hlua_gethlua(L);
1210 if (!hlua)
1211 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001212
1213 /* Lua cannot yield when its returning from a function,
1214 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001215 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001216 */
1217 if (lua_gethookmask(L) & LUA_MASKRET) {
1218 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1219 return;
1220 }
1221
1222 /* restore the interrupt condition. */
1223 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1224
1225 /* If we interrupt the Lua processing in yieldable state, we yield.
1226 * If the state is not yieldable, trying yield causes an error.
1227 */
1228 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001229 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001230
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001231 /* If we cannot yield, update the clock and check the timeout. */
1232 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001233 hlua->run_time += now_ms - hlua->start_time;
1234 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001235 lua_pushfstring(L, "execution timeout");
1236 WILL_LJMP(lua_error(L));
1237 }
1238
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001239 /* Update the start time. */
1240 hlua->start_time = now_ms;
1241
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001242 /* Try to interrupt the process at the end of the current
1243 * unyieldable function.
1244 */
1245 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001246}
1247
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001248/* This function start or resumes the Lua stack execution. If the flag
1249 * "yield_allowed" if no set and the LUA stack execution returns a yield
1250 * The function return an error.
1251 *
1252 * The function can returns 4 values:
1253 * - HLUA_E_OK : The execution is terminated without any errors.
1254 * - HLUA_E_AGAIN : The execution must continue at the next associated
1255 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001256 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001257 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001258 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001259 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001260 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001261 * LUA code.
1262 */
1263static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1264{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001265#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1266 int nres;
1267#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001268 int ret;
1269 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001270 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001271
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001272 /* Initialise run time counter. */
1273 if (!HLUA_IS_RUNNING(lua))
1274 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001275
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001276 /* Lock the whole Lua execution. This lock must be before the
1277 * label "resume_execution".
1278 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001279 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001280 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001281
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001282resume_execution:
1283
1284 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1285 * instructions. it is used for preventing infinite loops.
1286 */
1287 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1288
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001289 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001290 HLUA_SET_RUN(lua);
1291 HLUA_CLR_CTRLYIELD(lua);
1292 HLUA_CLR_WAKERESWR(lua);
1293 HLUA_CLR_WAKEREQWR(lua);
Christopher Faulet1f43a342021-08-04 17:58:21 +02001294 HLUA_CLR_NOYIELD(lua);
1295 if (!yield_allowed)
1296 HLUA_SET_NOYIELD(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001297
Christopher Fauletbc275a92020-02-26 14:55:16 +01001298 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001299 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001300 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001301
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001302 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001303#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001304 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001305#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001306 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001307#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001308 switch (ret) {
1309
1310 case LUA_OK:
1311 ret = HLUA_E_OK;
1312 break;
1313
1314 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001315 /* Check if the execution timeout is expired. It it is the case, we
1316 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001317 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001318 tv_update_date(0, 1);
1319 lua->run_time += now_ms - lua->start_time;
1320 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001321 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001322 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001323 break;
1324 }
1325 /* Process the forced yield. if the general yield is not allowed or
1326 * if no task were associated this the current Lua execution
1327 * coroutine, we resume the execution. Else we want to return in the
1328 * scheduler and we want to be waked up again, to continue the
1329 * current Lua execution. So we schedule our own task.
1330 */
1331 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001332 if (!yield_allowed || !lua->task)
1333 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001334 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001335 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001336 if (!yield_allowed) {
1337 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001338 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001339 break;
1340 }
1341 ret = HLUA_E_AGAIN;
1342 break;
1343
1344 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001345
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001346 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001347 * because the errors ares the only one mean to return immediately
1348 * from and lua execution.
1349 */
1350 if (lua->flags & HLUA_EXIT) {
1351 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001352 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001353 break;
1354 }
1355
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001356 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001357 if (!lua_checkstack(lua->T, 1)) {
1358 ret = HLUA_E_ERR;
1359 break;
1360 }
1361 msg = lua_tostring(lua->T, -1);
1362 lua_settop(lua->T, 0); /* Empty the stack. */
1363 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001364 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001365 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001366 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001367 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001368 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001369 ret = HLUA_E_ERRMSG;
1370 break;
1371
1372 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001373 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001374 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001375 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001376 break;
1377
1378 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001379 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001380 if (!lua_checkstack(lua->T, 1)) {
1381 ret = HLUA_E_ERR;
1382 break;
1383 }
1384 msg = lua_tostring(lua->T, -1);
1385 lua_settop(lua->T, 0); /* Empty the stack. */
1386 lua_pop(lua->T, 1);
1387 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001388 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001389 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001390 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001391 ret = HLUA_E_ERRMSG;
1392 break;
1393
1394 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001395 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001396 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001397 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001398 break;
1399 }
1400
1401 switch (ret) {
1402 case HLUA_E_AGAIN:
1403 break;
1404
1405 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001406 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001407 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001408 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001409 break;
1410
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001411 case HLUA_E_ETMOUT:
1412 case HLUA_E_NOMEM:
1413 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001414 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001415 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001416 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001417 hlua_ctx_renew(lua, 0);
1418 break;
1419
1420 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001421 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001422 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001423 break;
1424 }
1425
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001426 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001427 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001428 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001429
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001430 return ret;
1431}
1432
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001433/* This function exit the current code. */
1434__LJMP static int hlua_done(lua_State *L)
1435{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001436 struct hlua *hlua;
1437
1438 /* Get hlua struct, or NULL if we execute from main lua state */
1439 hlua = hlua_gethlua(L);
1440 if (!hlua)
1441 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001442
1443 hlua->flags |= HLUA_EXIT;
1444 WILL_LJMP(lua_error(L));
1445
1446 return 0;
1447}
1448
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001449/* This function is an LUA binding. It provides a function
1450 * for deleting ACL from a referenced ACL file.
1451 */
1452__LJMP static int hlua_del_acl(lua_State *L)
1453{
1454 const char *name;
1455 const char *key;
1456 struct pat_ref *ref;
1457
1458 MAY_LJMP(check_args(L, 2, "del_acl"));
1459
1460 name = MAY_LJMP(luaL_checkstring(L, 1));
1461 key = MAY_LJMP(luaL_checkstring(L, 2));
1462
1463 ref = pat_ref_lookup(name);
1464 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001465 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001466
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001467 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001468 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001469 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001470 return 0;
1471}
1472
1473/* This function is an LUA binding. It provides a function
1474 * for deleting map entry from a referenced map file.
1475 */
1476static int hlua_del_map(lua_State *L)
1477{
1478 const char *name;
1479 const char *key;
1480 struct pat_ref *ref;
1481
1482 MAY_LJMP(check_args(L, 2, "del_map"));
1483
1484 name = MAY_LJMP(luaL_checkstring(L, 1));
1485 key = MAY_LJMP(luaL_checkstring(L, 2));
1486
1487 ref = pat_ref_lookup(name);
1488 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001489 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001490
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001491 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001492 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001493 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001494 return 0;
1495}
1496
1497/* This function is an LUA binding. It provides a function
1498 * for adding ACL pattern from a referenced ACL file.
1499 */
1500static int hlua_add_acl(lua_State *L)
1501{
1502 const char *name;
1503 const char *key;
1504 struct pat_ref *ref;
1505
1506 MAY_LJMP(check_args(L, 2, "add_acl"));
1507
1508 name = MAY_LJMP(luaL_checkstring(L, 1));
1509 key = MAY_LJMP(luaL_checkstring(L, 2));
1510
1511 ref = pat_ref_lookup(name);
1512 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001513 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001514
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001515 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001516 if (pat_ref_find_elt(ref, key) == NULL)
1517 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001518 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001519 return 0;
1520}
1521
1522/* This function is an LUA binding. It provides a function
1523 * for setting map pattern and sample from a referenced map
1524 * file.
1525 */
1526static int hlua_set_map(lua_State *L)
1527{
1528 const char *name;
1529 const char *key;
1530 const char *value;
1531 struct pat_ref *ref;
1532
1533 MAY_LJMP(check_args(L, 3, "set_map"));
1534
1535 name = MAY_LJMP(luaL_checkstring(L, 1));
1536 key = MAY_LJMP(luaL_checkstring(L, 2));
1537 value = MAY_LJMP(luaL_checkstring(L, 3));
1538
1539 ref = pat_ref_lookup(name);
1540 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001541 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001542
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001543 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001544 if (pat_ref_find_elt(ref, key) != NULL)
1545 pat_ref_set(ref, key, value, NULL);
1546 else
1547 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001548 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001549 return 0;
1550}
1551
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001552/* A class is a lot of memory that contain data. This data can be a table,
1553 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001554 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001555 * the name of the object (_G[<name>] = <metable> ).
1556 *
1557 * A metable is a table that modify the standard behavior of a standard
1558 * access to the associated data. The entries of this new metatable are
1559 * defined as is:
1560 *
1561 * http://lua-users.org/wiki/MetatableEvents
1562 *
1563 * __index
1564 *
1565 * we access an absent field in a table, the result is nil. This is
1566 * true, but it is not the whole truth. Actually, such access triggers
1567 * the interpreter to look for an __index metamethod: If there is no
1568 * such method, as usually happens, then the access results in nil;
1569 * otherwise, the metamethod will provide the result.
1570 *
1571 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1572 * the key does not appear in the table, but the metatable has an __index
1573 * property:
1574 *
1575 * - if the value is a function, the function is called, passing in the
1576 * table and the key; the return value of that function is returned as
1577 * the result.
1578 *
1579 * - if the value is another table, the value of the key in that table is
1580 * asked for and returned (and if it doesn't exist in that table, but that
1581 * table's metatable has an __index property, then it continues on up)
1582 *
1583 * - Use "rawget(myTable,key)" to skip this metamethod.
1584 *
1585 * http://www.lua.org/pil/13.4.1.html
1586 *
1587 * __newindex
1588 *
1589 * Like __index, but control property assignment.
1590 *
1591 * __mode - Control weak references. A string value with one or both
1592 * of the characters 'k' and 'v' which specifies that the the
1593 * keys and/or values in the table are weak references.
1594 *
1595 * __call - Treat a table like a function. When a table is followed by
1596 * parenthesis such as "myTable( 'foo' )" and the metatable has
1597 * a __call key pointing to a function, that function is invoked
1598 * (passing any specified arguments) and the return value is
1599 * returned.
1600 *
1601 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1602 * called, if the metatable for myTable has a __metatable
1603 * key, the value of that key is returned instead of the
1604 * actual metatable.
1605 *
1606 * __tostring - Control string representation. When the builtin
1607 * "tostring( myTable )" function is called, if the metatable
1608 * for myTable has a __tostring property set to a function,
1609 * that function is invoked (passing myTable to it) and the
1610 * return value is used as the string representation.
1611 *
1612 * __len - Control table length. When the table length is requested using
1613 * the length operator ( '#' ), if the metatable for myTable has
1614 * a __len key pointing to a function, that function is invoked
1615 * (passing myTable to it) and the return value used as the value
1616 * of "#myTable".
1617 *
1618 * __gc - Userdata finalizer code. When userdata is set to be garbage
1619 * collected, if the metatable has a __gc field pointing to a
1620 * function, that function is first invoked, passing the userdata
1621 * to it. The __gc metamethod is not called for tables.
1622 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1623 *
1624 * Special metamethods for redefining standard operators:
1625 * http://www.lua.org/pil/13.1.html
1626 *
1627 * __add "+"
1628 * __sub "-"
1629 * __mul "*"
1630 * __div "/"
1631 * __unm "!"
1632 * __pow "^"
1633 * __concat ".."
1634 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001635 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001636 * http://www.lua.org/pil/13.2.html
1637 *
1638 * __eq "=="
1639 * __lt "<"
1640 * __le "<="
1641 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001642
1643/*
1644 *
1645 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001646 * Class Map
1647 *
1648 *
1649 */
1650
1651/* Returns a struct hlua_map if the stack entry "ud" is
1652 * a class session, otherwise it throws an error.
1653 */
1654__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1655{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001656 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001657}
1658
1659/* This function is the map constructor. It don't need
1660 * the class Map object. It creates and return a new Map
1661 * object. It must be called only during "body" or "init"
1662 * context because it process some filesystem accesses.
1663 */
1664__LJMP static int hlua_map_new(struct lua_State *L)
1665{
1666 const char *fn;
1667 int match = PAT_MATCH_STR;
1668 struct sample_conv conv;
1669 const char *file = "";
1670 int line = 0;
1671 lua_Debug ar;
1672 char *err = NULL;
1673 struct arg args[2];
1674
1675 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1676 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1677
1678 fn = MAY_LJMP(luaL_checkstring(L, 1));
1679
1680 if (lua_gettop(L) >= 2) {
1681 match = MAY_LJMP(luaL_checkinteger(L, 2));
1682 if (match < 0 || match >= PAT_MATCH_NUM)
1683 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1684 }
1685
1686 /* Get Lua filename and line number. */
1687 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1688 lua_getinfo(L, "Sl", &ar); /* get info about it */
1689 if (ar.currentline > 0) { /* is there info? */
1690 file = ar.short_src;
1691 line = ar.currentline;
1692 }
1693 }
1694
1695 /* fill fake sample_conv struct. */
1696 conv.kw = ""; /* unused. */
1697 conv.process = NULL; /* unused. */
1698 conv.arg_mask = 0; /* unused. */
1699 conv.val_args = NULL; /* unused. */
1700 conv.out_type = SMP_T_STR;
1701 conv.private = (void *)(long)match;
1702 switch (match) {
1703 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1704 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1705 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1706 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1707 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1708 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1709 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001710 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001711 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1712 default:
1713 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1714 }
1715
1716 /* fill fake args. */
1717 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001718 args[0].data.str.area = strdup(fn);
1719 args[0].data.str.data = strlen(fn);
1720 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001721 args[1].type = ARGT_STOP;
1722
1723 /* load the map. */
1724 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001725 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001726 * free the err variable.
1727 */
1728 luaL_where(L, 1);
1729 lua_pushfstring(L, "'new': %s.", err);
1730 lua_concat(L, 2);
1731 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001732 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001733 WILL_LJMP(lua_error(L));
1734 }
1735
1736 /* create the lua object. */
1737 lua_newtable(L);
1738 lua_pushlightuserdata(L, args[0].data.map);
1739 lua_rawseti(L, -2, 0);
1740
1741 /* Pop a class Map metatable and affect it to the userdata. */
1742 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1743 lua_setmetatable(L, -2);
1744
1745
1746 return 1;
1747}
1748
1749__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1750{
1751 struct map_descriptor *desc;
1752 struct pattern *pat;
1753 struct sample smp;
1754
1755 MAY_LJMP(check_args(L, 2, "lookup"));
1756 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001757 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001758 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001759 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001760 }
1761 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001762 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001763 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001764 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 +01001765 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001766 }
1767
1768 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001769 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001770 if (str)
1771 lua_pushstring(L, "");
1772 else
1773 lua_pushnil(L);
1774 return 1;
1775 }
1776
1777 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001778 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001779 return 1;
1780}
1781
1782__LJMP static int hlua_map_lookup(struct lua_State *L)
1783{
1784 return _hlua_map_lookup(L, 0);
1785}
1786
1787__LJMP static int hlua_map_slookup(struct lua_State *L)
1788{
1789 return _hlua_map_lookup(L, 1);
1790}
1791
1792/*
1793 *
1794 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795 * Class Socket
1796 *
1797 *
1798 */
1799
1800__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1801{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001802 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001803}
1804
1805/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001806 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001807 * received.
1808 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001809static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001810{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001811 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001812
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001813 if (appctx->ctx.hlua_cosocket.die) {
1814 si_shutw(si);
1815 si_shutr(si);
1816 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001817 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1818 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001819 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1820 }
1821
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001822 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001823 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001824 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001825
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001826 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001827 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001828 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001829
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001830 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001831 * to be notified whenever the connection completes.
1832 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001833 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001834 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001835 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001836 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001837 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001838 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001839
1840 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001841 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001842
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001843 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001844 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001845 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846
1847 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001848 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001849 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001850
1851 /* Some data were injected in the buffer, notify the stream
1852 * interface.
1853 */
1854 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001855 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001856
1857 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001858 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001859 */
1860 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001861 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001862}
1863
Willy Tarreau87b09662015-04-03 00:22:06 +02001864/* This function is called when the "struct stream" is destroyed.
1865 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 * Wake all the pending signals.
1867 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001868static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001870 struct xref *peer;
1871
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001873 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1874 if (peer)
1875 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001876
1877 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001878 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1879 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880}
1881
1882/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001883 * uses this object. If the stream does not exists, just quit.
1884 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885 * pending signal can rest in the read and write lists. destroy
1886 * it.
1887 */
1888__LJMP static int hlua_socket_gc(lua_State *L)
1889{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001890 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001892 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001894 MAY_LJMP(check_args(L, 1, "__gc"));
1895
1896 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001897 peer = xref_get_peer_and_lock(&socket->xref);
1898 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001900 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001902 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001903 appctx->ctx.hlua_cosocket.die = 1;
1904 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001906 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001907 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 return 0;
1909}
1910
1911/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001912 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001913 */
sada05ed3302018-05-11 11:48:18 -07001914__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001915{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001916 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001917 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001918 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001919 struct hlua *hlua;
1920
1921 /* Get hlua struct, or NULL if we execute from main lua state */
1922 hlua = hlua_gethlua(L);
1923 if (!hlua)
1924 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001925
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001926 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001927
1928 /* Check if we run on the same thread than the xreator thread.
1929 * We cannot access to the socket if the thread is different.
1930 */
1931 if (socket->tid != tid)
1932 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1933
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001934 peer = xref_get_peer_and_lock(&socket->xref);
1935 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001937
1938 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001939 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001940
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001941 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001942 appctx->ctx.hlua_cosocket.die = 1;
1943 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001944
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001945 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001946 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947 return 0;
1948}
1949
sada05ed3302018-05-11 11:48:18 -07001950/* The close function calls close_helper.
1951 */
1952__LJMP static int hlua_socket_close(lua_State *L)
1953{
1954 MAY_LJMP(check_args(L, 1, "close"));
1955 return hlua_socket_close_helper(L);
1956}
1957
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958/* This Lua function assumes that the stack contain three parameters.
1959 * 1 - USERDATA containing a struct socket
1960 * 2 - INTEGER with values of the macro defined below
1961 * If the integer is -1, we must read at most one line.
1962 * If the integer is -2, we ust read all the data until the
1963 * end of the stream.
1964 * If the integer is positive value, we must read a number of
1965 * bytes corresponding to this value.
1966 */
1967#define HLSR_READ_LINE (-1)
1968#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001969__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001970{
1971 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1972 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001973 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001975 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001977 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001978 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001979 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001980 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001981 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001982 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001983 struct stream_interface *si;
1984 struct stream *s;
1985 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001986 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001987
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001988 /* Get hlua struct, or NULL if we execute from main lua state */
1989 hlua = hlua_gethlua(L);
1990
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001991 /* Check if this lua stack is schedulable. */
1992 if (!hlua || !hlua->task)
1993 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1994 "'frontend', 'backend' or 'task'"));
1995
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001996 /* Check if we run on the same thread than the xreator thread.
1997 * We cannot access to the socket if the thread is different.
1998 */
1999 if (socket->tid != tid)
2000 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2001
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002002 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002003 peer = xref_get_peer_and_lock(&socket->xref);
2004 if (!peer)
2005 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002006 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2007 si = appctx->owner;
2008 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002009
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002010 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002012 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002013 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014 if (nblk < 0) /* Connection close. */
2015 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002016 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002018
2019 /* remove final \r\n. */
2020 if (nblk == 1) {
2021 if (blk1[len1-1] == '\n') {
2022 len1--;
2023 skip_at_end++;
2024 if (blk1[len1-1] == '\r') {
2025 len1--;
2026 skip_at_end++;
2027 }
2028 }
2029 }
2030 else {
2031 if (blk2[len2-1] == '\n') {
2032 len2--;
2033 skip_at_end++;
2034 if (blk2[len2-1] == '\r') {
2035 len2--;
2036 skip_at_end++;
2037 }
2038 }
2039 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002040 }
2041
2042 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002044 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045 if (nblk < 0) /* Connection close. */
2046 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002047 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 goto connection_empty;
2049 }
2050
2051 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002052 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002053 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002054 if (nblk < 0) /* Connection close. */
2055 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002056 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002057 goto connection_empty;
2058
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002059 missing_bytes = wanted - socket->b.n;
2060 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002061 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002062 len1 = missing_bytes;
2063 } if (nblk == 2 && len1 + len2 > missing_bytes)
2064 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002065 }
2066
2067 len = len1;
2068
2069 luaL_addlstring(&socket->b, blk1, len1);
2070 if (nblk == 2) {
2071 len += len2;
2072 luaL_addlstring(&socket->b, blk2, len2);
2073 }
2074
2075 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002076 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077
2078 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002079 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002080
2081 /* If the pattern reclaim to read all the data
2082 * in the connection, got out.
2083 */
2084 if (wanted == HLSR_READ_ALL)
2085 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002086 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087 goto connection_empty;
2088
2089 /* Return result. */
2090 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002091 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002092 return 1;
2093
2094connection_closed:
2095
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002096 xref_unlock(&socket->xref, peer);
2097
2098no_peer:
2099
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100 /* If the buffer containds data. */
2101 if (socket->b.n > 0) {
2102 luaL_pushresult(&socket->b);
2103 return 1;
2104 }
2105 lua_pushnil(L);
2106 lua_pushstring(L, "connection closed.");
2107 return 2;
2108
2109connection_empty:
2110
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002111 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2112 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002113 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002114 }
2115 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002116 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002117 return 0;
2118}
2119
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002120/* This Lua function gets two parameters. The first one can be string
2121 * or a number. If the string is "*l", the user requires one line. If
2122 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002123 * If the value is a number, the user require a number of bytes equal
2124 * to the value. The default value is "*l" (a line).
2125 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002126 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127 * integer takes this values:
2128 * -1 : read a line
2129 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002130 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002131 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002132 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002133 * concatenated with the read data.
2134 */
2135__LJMP static int hlua_socket_receive(struct lua_State *L)
2136{
2137 int wanted = HLSR_READ_LINE;
2138 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002139 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002140 char *error;
2141 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002142 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002143
2144 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2145 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2146
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002147 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002149 /* Check if we run on the same thread than the xreator thread.
2150 * We cannot access to the socket if the thread is different.
2151 */
2152 if (socket->tid != tid)
2153 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2154
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002155 /* check for pattern. */
2156 if (lua_gettop(L) >= 2) {
2157 type = lua_type(L, 2);
2158 if (type == LUA_TSTRING) {
2159 pattern = lua_tostring(L, 2);
2160 if (strcmp(pattern, "*a") == 0)
2161 wanted = HLSR_READ_ALL;
2162 else if (strcmp(pattern, "*l") == 0)
2163 wanted = HLSR_READ_LINE;
2164 else {
2165 wanted = strtoll(pattern, &error, 10);
2166 if (*error != '\0')
2167 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2168 }
2169 }
2170 else if (type == LUA_TNUMBER) {
2171 wanted = lua_tointeger(L, 2);
2172 if (wanted < 0)
2173 WILL_LJMP(luaL_error(L, "Unsupported size."));
2174 }
2175 }
2176
2177 /* Set pattern. */
2178 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002179
2180 /* Check if we would replace the top by itself. */
2181 if (lua_gettop(L) != 2)
2182 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002183
Christopher Fauletc31b2002021-05-03 10:11:13 +02002184 /* Save index of the top of the stack because since buffers are used, it
2185 * may change
2186 */
2187 lastarg = lua_gettop(L);
2188
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002189 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002190 luaL_buffinit(L, &socket->b);
2191
2192 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002193 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002194 if (lua_type(L, 3) != LUA_TSTRING)
2195 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2196 pattern = lua_tolstring(L, 3, &len);
2197 luaL_addlstring(&socket->b, pattern, len);
2198 }
2199
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002200 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201}
2202
2203/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002204 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002205 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002206static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002207{
2208 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002209 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002210 struct appctx *appctx;
2211 size_t buf_len;
2212 const char *buf;
2213 int len;
2214 int send_len;
2215 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002216 struct xref *peer;
2217 struct stream_interface *si;
2218 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002219
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002220 /* Get hlua struct, or NULL if we execute from main lua state */
2221 hlua = hlua_gethlua(L);
2222
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002223 /* Check if this lua stack is schedulable. */
2224 if (!hlua || !hlua->task)
2225 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2226 "'frontend', 'backend' or 'task'"));
2227
2228 /* Get object */
2229 socket = MAY_LJMP(hlua_checksocket(L, 1));
2230 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002231 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002232
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002233 /* Check if we run on the same thread than the xreator thread.
2234 * We cannot access to the socket if the thread is different.
2235 */
2236 if (socket->tid != tid)
2237 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2238
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002239 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002240 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002241 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002242 lua_pushinteger(L, -1);
2243 return 1;
2244 }
2245 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2246 si = appctx->owner;
2247 s = si_strm(si);
2248
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002250 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002251 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002252 lua_pushinteger(L, -1);
2253 return 1;
2254 }
2255
2256 /* Update the input buffer data. */
2257 buf += sent;
2258 send_len = buf_len - sent;
2259
2260 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002261 if (sent >= buf_len) {
2262 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002264 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002266 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002267 * the request buffer if its not required.
2268 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002269 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002270 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002271 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002272 }
2273
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002274 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002275 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002276 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002277 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002278 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279
2280 /* send data */
2281 if (len < send_len)
2282 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002283 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284
2285 /* "Not enough space" (-1), "Buffer too little to contain
2286 * the data" (-2) are not expected because the available length
2287 * is tested.
2288 * Other unknown error are also not expected.
2289 */
2290 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002291 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002292 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002293
sada05ed3302018-05-11 11:48:18 -07002294 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002295 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002296 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002297 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 return 1;
2299 }
2300
2301 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002302 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002303
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002304 s->req.rex = TICK_ETERNITY;
2305 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306
2307 /* Update length sent. */
2308 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002309 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310
2311 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002312 if (sent + len >= buf_len) {
2313 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002314 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002315 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316
2317hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002318 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2319 xref_unlock(&socket->xref, peer);
2320 WILL_LJMP(luaL_error(L, "out of memory"));
2321 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002322 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002323 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002324 return 0;
2325}
2326
2327/* This function initiate the send of data. It just check the input
2328 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002329 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002330 * "hlua_socket_write_yield" that can yield.
2331 *
2332 * The Lua function gets between 3 and 4 parameters. The first one is
2333 * the associated object. The second is a string buffer. The third is
2334 * a facultative integer that represents where is the buffer position
2335 * of the start of the data that can send. The first byte is the
2336 * position "1". The default value is "1". The fourth argument is a
2337 * facultative integer that represents where is the buffer position
2338 * of the end of the data that can send. The default is the last byte.
2339 */
2340static int hlua_socket_send(struct lua_State *L)
2341{
2342 int i;
2343 int j;
2344 const char *buf;
2345 size_t buf_len;
2346
2347 /* Check number of arguments. */
2348 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2349 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2350
2351 /* Get the string. */
2352 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2353
2354 /* Get and check j. */
2355 if (lua_gettop(L) == 4) {
2356 j = MAY_LJMP(luaL_checkinteger(L, 4));
2357 if (j < 0)
2358 j = buf_len + j + 1;
2359 if (j > buf_len)
2360 j = buf_len + 1;
2361 lua_pop(L, 1);
2362 }
2363 else
2364 j = buf_len;
2365
2366 /* Get and check i. */
2367 if (lua_gettop(L) == 3) {
2368 i = MAY_LJMP(luaL_checkinteger(L, 3));
2369 if (i < 0)
2370 i = buf_len + i + 1;
2371 if (i > buf_len)
2372 i = buf_len + 1;
2373 lua_pop(L, 1);
2374 } else
2375 i = 1;
2376
2377 /* Check bth i and j. */
2378 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002379 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002380 return 1;
2381 }
2382 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002383 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002384 return 1;
2385 }
2386 if (i == 0)
2387 i = 1;
2388 if (j == 0)
2389 j = 1;
2390
2391 /* Pop the string. */
2392 lua_pop(L, 1);
2393
2394 /* Update the buffer length. */
2395 buf += i - 1;
2396 buf_len = j - i + 1;
2397 lua_pushlstring(L, buf, buf_len);
2398
2399 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002400 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002401
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002402 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403}
2404
Willy Tarreau22b0a682015-06-17 19:43:49 +02002405#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002406__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2407{
2408 static char buffer[SOCKET_INFO_MAX_LEN];
2409 int ret;
2410 int len;
2411 char *p;
2412
2413 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2414 if (ret <= 0) {
2415 lua_pushnil(L);
2416 return 1;
2417 }
2418
2419 if (ret == AF_UNIX) {
2420 lua_pushstring(L, buffer+1);
2421 return 1;
2422 }
2423 else if (ret == AF_INET6) {
2424 buffer[0] = '[';
2425 len = strlen(buffer);
2426 buffer[len] = ']';
2427 len++;
2428 buffer[len] = ':';
2429 len++;
2430 p = buffer;
2431 }
2432 else if (ret == AF_INET) {
2433 p = buffer + 1;
2434 len = strlen(p);
2435 p[len] = ':';
2436 len++;
2437 }
2438 else {
2439 lua_pushnil(L);
2440 return 1;
2441 }
2442
2443 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2444 lua_pushnil(L);
2445 return 1;
2446 }
2447
2448 lua_pushstring(L, p);
2449 return 1;
2450}
2451
2452/* Returns information about the peer of the connection. */
2453__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2454{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002455 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002456 struct xref *peer;
2457 struct appctx *appctx;
2458 struct stream_interface *si;
2459 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002460 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002461
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002462 MAY_LJMP(check_args(L, 1, "getpeername"));
2463
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002464 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002465
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002466 /* Check if we run on the same thread than the xreator thread.
2467 * We cannot access to the socket if the thread is different.
2468 */
2469 if (socket->tid != tid)
2470 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2471
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002472 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002473 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002474 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002475 lua_pushnil(L);
2476 return 1;
2477 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002478 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2479 si = appctx->owner;
2480 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002481
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002482 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002483 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002484 lua_pushnil(L);
2485 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002486 }
2487
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002488 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002489 xref_unlock(&socket->xref, peer);
2490 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002491}
2492
2493/* Returns information about my connection side. */
2494static int hlua_socket_getsockname(struct lua_State *L)
2495{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002496 struct hlua_socket *socket;
2497 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002498 struct appctx *appctx;
2499 struct xref *peer;
2500 struct stream_interface *si;
2501 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002502 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002503
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002504 MAY_LJMP(check_args(L, 1, "getsockname"));
2505
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002506 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002507
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002508 /* Check if we run on the same thread than the xreator thread.
2509 * We cannot access to the socket if the thread is different.
2510 */
2511 if (socket->tid != tid)
2512 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2513
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002514 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002515 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002516 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002517 lua_pushnil(L);
2518 return 1;
2519 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002520 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2521 si = appctx->owner;
2522 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002523
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002524 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002525 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002526 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002527 lua_pushnil(L);
2528 return 1;
2529 }
2530
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002531 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002532 xref_unlock(&socket->xref, peer);
2533 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002534}
2535
2536/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002537static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538 .obj_type = OBJ_TYPE_APPLET,
2539 .name = "<LUA_TCP>",
2540 .fct = hlua_socket_handler,
2541 .release = hlua_socket_release,
2542};
2543
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002544__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002545{
2546 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002547 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002548 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002549 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002550 struct stream_interface *si;
2551 struct stream *s;
2552
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002553 /* Get hlua struct, or NULL if we execute from main lua state */
2554 hlua = hlua_gethlua(L);
2555 if (!hlua)
2556 return 0;
2557
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 */
2561 if (socket->tid != tid)
2562 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2563
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002564 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002565 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002566 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002567 lua_pushnil(L);
2568 lua_pushstring(L, "Can't connect");
2569 return 2;
2570 }
2571 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2572 si = appctx->owner;
2573 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002574
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002575 /* Check if we run on the same thread than the xreator thread.
2576 * We cannot access to the socket if the thread is different.
2577 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002578 if (socket->tid != tid) {
2579 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002580 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002581 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002582
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002583 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002584 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002585 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002586 lua_pushnil(L);
2587 lua_pushstring(L, "Can't connect");
2588 return 2;
2589 }
2590
Willy Tarreaue09101e2018-10-16 17:37:12 +02002591 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002592
2593 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002594 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002595 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596 lua_pushinteger(L, 1);
2597 return 1;
2598 }
2599
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002600 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2601 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002602 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002603 }
2604 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002605 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606 return 0;
2607}
2608
2609/* This function fail or initite the connection. */
2610__LJMP static int hlua_socket_connect(struct lua_State *L)
2611{
2612 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002613 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002615 struct hlua *hlua;
2616 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002617 int low, high;
2618 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002619 struct xref *peer;
2620 struct stream_interface *si;
2621 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002623 if (lua_gettop(L) < 2)
2624 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002625
2626 /* Get args. */
2627 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002628
2629 /* Check if we run on the same thread than the xreator thread.
2630 * We cannot access to the socket if the thread is different.
2631 */
2632 if (socket->tid != tid)
2633 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2634
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002636 if (lua_gettop(L) >= 3) {
2637 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002638 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002639
Tim Duesterhus6edab862018-01-06 19:04:45 +01002640 /* Force the ip to end with a colon, to support IPv6 addresses
2641 * that are not enclosed within square brackets.
2642 */
2643 if (port > 0) {
2644 luaL_buffinit(L, &b);
2645 luaL_addstring(&b, ip);
2646 luaL_addchar(&b, ':');
2647 luaL_pushresult(&b);
2648 ip = lua_tolstring(L, lua_gettop(L), NULL);
2649 }
2650 }
2651
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002652 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002653 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002654 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002655 lua_pushnil(L);
2656 return 1;
2657 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658
2659 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002660 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 +02002661 if (!addr) {
2662 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002663 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002664 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002665
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002666 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002667 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002668 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002669 if (port == -1) {
2670 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002671 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002672 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002673 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2674 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002675 if (port == -1) {
2676 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002677 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002678 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002679 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002680 }
2681 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002682
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002683 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2684 si = appctx->owner;
2685 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002686
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002687 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002688 xref_unlock(&socket->xref, peer);
2689 WILL_LJMP(luaL_error(L, "connect: internal error"));
2690 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002691 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002692
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002693 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002694 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002695 if (!hlua)
2696 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002697
2698 /* inform the stream that we want to be notified whenever the
2699 * connection completes.
2700 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002701 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002702 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002703 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002704
Willy Tarreauf31af932020-01-14 09:59:38 +01002705 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002706
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002707 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2708 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002709 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002710 }
2711 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002712
2713 task_wakeup(s->task, TASK_WOKEN_INIT);
2714 /* Return yield waiting for connection. */
2715
Willy Tarreau9635e032018-10-16 17:52:55 +02002716 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002717
2718 return 0;
2719}
2720
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002721#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002722__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2723{
2724 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002725 struct xref *peer;
2726 struct appctx *appctx;
2727 struct stream_interface *si;
2728 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002729
2730 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2731 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002732
2733 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002734 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002735 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002736 lua_pushnil(L);
2737 return 1;
2738 }
2739 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2740 si = appctx->owner;
2741 s = si_strm(si);
2742
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002743 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002744 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002745 return MAY_LJMP(hlua_socket_connect(L));
2746}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002747#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002748
2749__LJMP static int hlua_socket_setoption(struct lua_State *L)
2750{
2751 return 0;
2752}
2753
2754__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2755{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002756 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002757 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002758 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002759 struct xref *peer;
2760 struct appctx *appctx;
2761 struct stream_interface *si;
2762 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002763
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002764 MAY_LJMP(check_args(L, 2, "settimeout"));
2765
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002766 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002767
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002768 /* convert the timeout to millis */
2769 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002770
Thierry Fournier17a921b2018-03-08 09:59:02 +01002771 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002772 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002773 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2774
Mark Lakes56cc1252018-03-27 09:48:06 +02002775 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002776 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002777
2778 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002779 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002780 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002781
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002782 /* Check if we run on the same thread than the xreator thread.
2783 * We cannot access to the socket if the thread is different.
2784 */
2785 if (socket->tid != tid)
2786 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2787
Mark Lakes56cc1252018-03-27 09:48:06 +02002788 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002789 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002790 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002791 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2792 WILL_LJMP(lua_error(L));
2793 return 0;
2794 }
2795 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2796 si = appctx->owner;
2797 s = si_strm(si);
2798
Cyril Bonté7bb63452018-08-17 23:51:02 +02002799 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002800 s->req.rto = tmout;
2801 s->req.wto = tmout;
2802 s->res.rto = tmout;
2803 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002804 s->req.rex = tick_add_ifset(now_ms, tmout);
2805 s->req.wex = tick_add_ifset(now_ms, tmout);
2806 s->res.rex = tick_add_ifset(now_ms, tmout);
2807 s->res.wex = tick_add_ifset(now_ms, tmout);
2808
2809 s->task->expire = tick_add_ifset(now_ms, tmout);
2810 task_queue(s->task);
2811
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002812 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002813
Thierry Fourniere9636f12018-03-08 09:54:32 +01002814 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002815 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002816}
2817
2818__LJMP static int hlua_socket_new(lua_State *L)
2819{
2820 struct hlua_socket *socket;
2821 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002822 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002823 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002824
2825 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002826 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002827 hlua_pusherror(L, "socket: full stack");
2828 goto out_fail_conf;
2829 }
2830
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002831 /* Create the object: obj[0] = userdata. */
2832 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002833 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002834 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002835 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002836 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002837
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002838 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002839 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002840 hlua_pusherror(L, "socket: uninitialized pools.");
2841 goto out_fail_conf;
2842 }
2843
Willy Tarreau87b09662015-04-03 00:22:06 +02002844 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002845 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2846 lua_setmetatable(L, -2);
2847
Willy Tarreaud420a972015-04-06 00:39:18 +02002848 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002849 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002850 if (!appctx) {
2851 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002852 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002853 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002854
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002855 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002856 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002857 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2858 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002859
Willy Tarreaud420a972015-04-06 00:39:18 +02002860 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002861 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002862 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002863 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002864 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002865 }
2866
Christopher Faulet26256f82020-09-14 11:40:13 +02002867 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002868 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002869 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002870 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002871 }
2872
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002873 /* Initialise cross reference between stream and Lua socket object. */
2874 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002875
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002876 /* Configure "right" stream interface. this "si" is used to connect
2877 * and retrieve data from the server. The connection is initialized
2878 * with the "struct server".
2879 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002880 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002881
2882 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002883 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002884 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002885
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002886 return 1;
2887
Willy Tarreaud420a972015-04-06 00:39:18 +02002888 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002889 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002890 out_fail_sess:
2891 appctx_free(appctx);
2892 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002893 WILL_LJMP(lua_error(L));
2894 return 0;
2895}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002896
2897/*
2898 *
2899 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900 * Class Channel
2901 *
2902 *
2903 */
2904
2905/* Returns the struct hlua_channel join to the class channel in the
2906 * stack entry "ud" or throws an argument error.
2907 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002908__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002909{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002910 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911}
2912
Willy Tarreau47860ed2015-03-10 14:07:50 +01002913/* Pushes the channel onto the top of the stack. If the stask does not have a
2914 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002916static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002917{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002919 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920 return 0;
2921
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002922 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002923 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002924 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925
2926 /* Pop a class sesison metatable and affect it to the userdata. */
2927 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2928 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 return 1;
2930}
2931
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002932/* Copies <len> bytes of data present in the channel's buffer, starting at the
2933* offset <offset>, and put it in a LUA string variable. It is the caller
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002934* responsibility to ensure <len> and <offset> are valid. It always return the
2935* length of the built string. <len> may be 0, in this case, an empty string is
2936* created and 0 is returned.
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002937*/
2938static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002939{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002940 size_t block1, block2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941 luaL_Buffer b;
2942
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002943 block1 = len;
2944 if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)))
2945 block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset));
2946 block2 = len - block1;
2947
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 luaL_buffinit(L, &b);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002949 luaL_addlstring(&b, b_peek(&chn->buf, offset), block1);
2950 if (block2)
2951 luaL_addlstring(&b, b_orig(&chn->buf), block2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952 luaL_pushresult(&b);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002953 return len;
2954}
2955
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002956/* Inserts the string <str> to the channel's buffer at the offset <offset>. This
2957 * function returns -1 if data cannot be copied. Otherwise, it returns the
2958 * number of bytes copied.
2959 */
2960static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset)
2961{
2962 int ret = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002963
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002964 /* Nothing to do, just return */
2965 if (unlikely(istlen(str) == 0))
2966 goto end;
2967
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002968 if (istlen(str) > c_room(chn)) {
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002969 ret = -1;
2970 goto end;
2971 }
2972 ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str));
2973
2974 end:
2975 return ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002976}
2977
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002978/* Removes <len> bytes of data at the absolute position <offset>.
2979 */
2980static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len)
2981{
2982 size_t end = offset + len;
2983
2984 if (b_peek(&chn->buf, end) != b_tail(&chn->buf))
2985 b_move(&chn->buf, b_peek_ofs(&chn->buf, end),
2986 b_data(&chn->buf) - end, -len);
2987 b_sub(&chn->buf, len);
2988}
2989
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002990/* Copies input data in the channel's buffer. It is possible to set a specific
2991 * offset (0 by default) and a length (all remaining input data starting for the
2992 * offset by default). If there is not enough input data and more data can be
2993 * received, this function yields.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002995__LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002997 struct channel *chn;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02002998 int offset = 0, len = 0;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002999
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003000 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003001
3002 if (!ci_data(chn) && channel_input_closed(chn)) {
3003 lua_pushnil(L);
3004 return 1;
3005 }
3006
3007 if (lua_gettop(L) > 1) {
3008 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3009 if (offset < 0)
3010 offset = MAX(0, ci_data(chn) + offset);
3011 if (offset > ci_data(chn)) {
3012 lua_pushfstring(L, "offset out of range.");
3013 WILL_LJMP(lua_error(L));
3014 }
3015 }
3016 len = ci_data(chn) - offset;
3017 if (lua_gettop(L) == 3) {
3018 len = MAY_LJMP(luaL_checkinteger(L, 3));
3019 if (!len)
3020 goto dup;
3021 if (len == -1)
3022 len = global.tune.bufsize;
3023 if (len < 0) {
3024 lua_pushfstring(L, "length out of range.");
3025 WILL_LJMP(lua_error(L));
3026 }
3027 }
3028
3029 if (offset + len > ci_data(chn)) {
3030 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3031 /* Yield waiting for more data, as requested */
3032 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0));
3033 }
3034 len = ci_data(chn) - offset;
3035 }
3036
3037 dup:
3038 _hlua_channel_dup(chn, L, co_data(chn) + offset, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003039 return 1;
3040}
3041
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003042/* Copies the first line (including the trailing LF) of input data in the
3043 * channel's buffer. It is possible to set a specific offset (0 by default) and
3044 * a length (all remaining input data starting for the offset by default). If
3045 * there is not enough input data and more data can be received, the function
3046 * yields. If a length is explicitly specified, no more data are
3047 * copied. Otherwise, if no LF is found and more data can be received, this
3048 * function yields.
3049 */
3050__LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003051{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003052 struct channel *chn;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003053 int l, offset = 0, len = 0;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003054
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003055 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003056
3057 if (!ci_data(chn) && channel_input_closed(chn)) {
3058 lua_pushnil(L);
3059 return 1;
3060 }
3061
3062 if (lua_gettop(L) > 1) {
3063 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3064 if (offset < 0)
3065 offset = MAX(0, ci_data(chn) + offset);
3066 if (offset > ci_data(chn)) {
3067 lua_pushfstring(L, "offset out of range.");
3068 WILL_LJMP(lua_error(L));
3069 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003070 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003071 len = ci_data(chn) - offset;
3072 if (lua_gettop(L) == 3) {
3073 len = MAY_LJMP(luaL_checkinteger(L, 3));
3074 if (!len)
3075 goto dup;
3076 if (len == -1)
3077 len = global.tune.bufsize;
3078 if (len < 0) {
3079 lua_pushfstring(L, "length out of range.");
3080 WILL_LJMP(lua_error(L));
3081 }
3082 }
3083
3084 for (l = 0; l < len; l++) {
3085 if (l + offset >= ci_data(chn))
3086 break;
3087 if (*(b_peek(&chn->buf, co_data(chn) + offset + l)) == '\n') {
3088 len = l+1;
3089 goto dup;
3090 }
3091 }
3092
3093 if (offset + len > ci_data(chn)) {
3094 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3095 /* Yield waiting for more data */
3096 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0));
3097 }
3098 len = ci_data(chn) - offset;
3099 }
3100
3101 dup:
3102 _hlua_channel_dup(chn, L, co_data(chn) + offset, len);
3103 return 1;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003104}
3105
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003106/* [ DEPRECATED ]
3107 *
3108 * Duplicate all input data foud in the channel's buffer. The data are not
3109 * removed from the buffer. This function relies on _hlua_channel_dup().
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003110 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003111__LJMP static int hlua_channel_dup(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003112{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003113 struct channel *chn;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003114 int offset = 0, len = 0;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003115
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003116 MAY_LJMP(check_args(L, 1, "dup"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003117 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003118 if (IS_HTX_STRM(chn_strm(chn))) {
3119 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3120 WILL_LJMP(lua_error(L));
3121 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003122 offset = co_data(chn);
3123 len = ci_data(chn);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003124
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003125 if (!ci_data(chn) && channel_input_closed(chn)) {
3126 lua_pushnil(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 return 1;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003128 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003129
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003130 _hlua_channel_dup(chn, L, offset, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003131 return 1;
3132}
3133
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003134/* [ DEPRECATED ]
3135 *
3136 * Get all input data foud in the channel's buffer. The data are removed from
3137 * the buffer after the copy. This function relies on _hlua_channel_dup() and
3138 * _hlua_channel_delete().
3139 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003140__LJMP static int hlua_channel_get(lua_State *L)
3141{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003142 struct channel *chn;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003143 int offset = 0, len = 0;
3144 int ret;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003145
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003146 MAY_LJMP(check_args(L, 1, "get"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003147 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3148 if (IS_HTX_STRM(chn_strm(chn))) {
3149 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3150 WILL_LJMP(lua_error(L));
3151 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003152 offset = co_data(chn);
3153 len = ci_data(chn);
3154
3155 if (!ci_data(chn) && channel_input_closed(chn)) {
3156 lua_pushnil(L);
3157 return 1;
3158 }
3159
3160 ret = _hlua_channel_dup(chn, L, offset, len);
3161 _hlua_channel_delete(chn, offset, ret);
3162 return 1;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003163}
3164
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003165/* This functions consumes and returns one line. If the channel is closed,
3166 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003167 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003168 * value.
3169 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003170__LJMP static int hlua_channel_getline_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;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003173 size_t l;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003174 int offset = 0, len = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003175 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003176
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003177 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003178 offset = co_data(chn);
3179 len = ci_data(chn);
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003180
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003181 if (!ci_data(chn) && channel_input_closed(chn)) {
3182 lua_pushnil(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003183 return 1;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003184 }
3185
3186 for (l = 0; l < len; l++) {
3187 if (*(b_peek(&chn->buf, offset+l)) == '\n') {
3188 len = l+1;
3189 goto dup;
3190 }
3191 }
3192
3193 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3194 /* Yield waiting for more data */
3195 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
3196 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003197
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003198 dup:
3199 ret = _hlua_channel_dup(chn, L, co_data(chn) + offset, len);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003200 _hlua_channel_delete(chn, offset, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003201 return 1;
3202}
3203
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003204/* [ DEPRECATED ]
3205 *
3206 * Check arguments for the function "hlua_channel_getline_yield".
3207 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003208__LJMP static int hlua_channel_getline(lua_State *L)
3209{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003210 struct channel *chn;
3211
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003212 MAY_LJMP(check_args(L, 1, "getline"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003213 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3214 if (IS_HTX_STRM(chn_strm(chn))) {
3215 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3216 WILL_LJMP(lua_error(L));
3217 }
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003218 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3219}
3220
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003221/* Retrieves a given amount of input data at the given offset. By default all
3222 * available input data are returned. The offset may be negactive to start from
3223 * the end of input data. The length may be -1 to set it to the maximum buffer
3224 * size.
3225 */
3226__LJMP static int hlua_channel_get_data(lua_State *L)
3227{
3228 struct channel *chn;
3229
3230 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3231 WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments"));
3232 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3233 if (IS_HTX_STRM(chn_strm(chn))) {
3234 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3235 WILL_LJMP(lua_error(L));
3236 }
3237 return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0));
3238}
3239
3240/* Retrieves a given amount of input data at the given offset. By default all
3241 * available input data are returned. The offset may be negactive to start from
3242 * the end of input data. The length may be -1 to set it to the maximum buffer
3243 * size.
3244 */
3245__LJMP static int hlua_channel_get_line(lua_State *L)
3246{
3247 struct channel *chn;
3248
3249 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3250 WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments"));
3251 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3252 if (IS_HTX_STRM(chn_strm(chn))) {
3253 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3254 WILL_LJMP(lua_error(L));
3255 }
3256 return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0));
3257}
3258
3259/* Appends a string into the input side of channel. It returns the length of the
3260 * written string, or -1 if the channel is closed or if the buffer size is too
3261 * little for the data. 0 may be returned if nothing is copied. This function
3262 * does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003263 */
Christopher Faulet23976d92021-08-06 09:59:49 +02003264__LJMP static int hlua_channel_append(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003265{
Christopher Faulet23976d92021-08-06 09:59:49 +02003266 struct channel *chn;
3267 const char *str;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003268 size_t len;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003269 int ret;
Christopher Faulet23976d92021-08-06 09:59:49 +02003270
3271 MAY_LJMP(check_args(L, 2, "append"));
3272 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3273 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003274 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003275 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003276 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003277 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003278
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003279 ret = _hlua_channel_insert(chn, L, ist2(str, len), co_data(chn) + ci_data(chn));
3280 lua_pushinteger(L, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003281 return 1;
3282}
3283
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003284/* Prepends a string into the input side of channel. It returns the length of the
3285 * written string, or -1 if the channel is closed or if the buffer size is too
3286 * little for the data. 0 may be returned if nothing is copied. This function
3287 * does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003288 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003289__LJMP static int hlua_channel_prepend(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003290{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003291 struct channel *chn;
Christopher Faulet23976d92021-08-06 09:59:49 +02003292 const char *str;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003293 size_t sz;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003294 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003295
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003296 MAY_LJMP(check_args(L, 2, "prepend"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003297 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003298 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3299 if (IS_HTX_STRM(chn_strm(chn))) {
3300 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3301 WILL_LJMP(lua_error(L));
3302 }
3303
3304 ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn));
3305 lua_pushinteger(L, ret);
3306 return 1;
3307}
3308
3309/* Inserts a given amount of input data at the given offset by a string
3310 * content. By default the string is appended at the end of input data. It
3311 * returns the length of the written string, or -1 if the channel is closed or
3312 * if the buffer size is too little for the data.
3313 */
3314__LJMP static int hlua_channel_insert_data(lua_State *L)
3315{
3316 struct channel *chn;
3317 const char *str;
3318 size_t sz;
3319 int ret, offset;
3320
3321 if (lua_gettop(L) < 2 || lua_gettop(L) > 3)
3322 WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments"));
3323 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3324 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3325 offset = ci_data(chn);
3326 if (lua_gettop(L) > 2)
3327 offset = MAY_LJMP(luaL_checkinteger(L, 3));
3328 if (IS_HTX_STRM(chn_strm(chn))) {
3329 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3330 WILL_LJMP(lua_error(L));
3331 }
3332
3333 if (offset < 0)
3334 offset = MAX(0, ci_data(chn) + offset);
3335
3336 if (offset > ci_data(chn)) {
3337 lua_pushfstring(L, "offset out of range.");
3338 WILL_LJMP(lua_error(L));
3339 }
3340
3341 ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn) + offset);
3342 lua_pushinteger(L, ret);
3343 return 1;
3344}
3345/* Replaces a given amount of input data at the given offset by a string
3346 * content. By default all remaining data are removed (offset = 0 and len =
3347 * -1). It returns the length of the written string, or -1 if the channel is
3348 * closed or if the buffer size is too little for the data.
3349 */
3350__LJMP static int hlua_channel_set_data(lua_State *L)
3351{
3352 struct channel *chn;
3353 const char *str;
3354 size_t sz;
3355 int ret, offset = 0, len = -1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003356
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003357 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
3358 WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments"));
3359 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3360 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3361 if (lua_gettop(L) > 2)
3362 offset = MAY_LJMP(luaL_checkinteger(L, 3));
3363 if (lua_gettop(L) == 4)
3364 len = MAY_LJMP(luaL_checkinteger(L, 4));
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003365 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003366 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003367 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003368 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003369
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003370 if (offset < 0)
3371 offset = MAX(0, ci_data(chn) + offset);
3372 if (len < 0)
3373 len = ci_data(chn) - offset;
3374
3375 if (offset + len > ci_data(chn)) {
3376 lua_pushfstring(L, "offset or length out of range.");
3377 WILL_LJMP(lua_error(L));
3378 }
3379
Christopher Faulet23976d92021-08-06 09:59:49 +02003380 /* Be sure we can copied the string once input data will be removed. */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003381 if (sz > c_room(chn) + len)
Christopher Faulet23976d92021-08-06 09:59:49 +02003382 lua_pushinteger(L, -1);
3383 else {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003384 _hlua_channel_delete(chn, co_data(chn) + offset, len);
3385 ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn) + offset);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003386 lua_pushinteger(L, ret);
Christopher Faulet23976d92021-08-06 09:59:49 +02003387 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003388 return 1;
3389}
3390
3391/* Removes a given amount of input data at the given offset. By default all
3392 * intput data are removed (offset = 0 and len = -1). It returns the amount of
3393 * the removed data.
3394 */
3395__LJMP static int hlua_channel_del_data(lua_State *L)
3396{
3397 struct channel *chn;
3398 int offset = 0, len = -1;
3399
3400 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3401 WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments"));
3402 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3403 if (lua_gettop(L) > 1)
3404 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3405 if (lua_gettop(L) == 3)
3406 len = MAY_LJMP(luaL_checkinteger(L, 3));
3407 if (IS_HTX_STRM(chn_strm(chn))) {
3408 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3409 WILL_LJMP(lua_error(L));
3410 }
3411
3412 if (offset < 0)
3413 offset = MAX(0, ci_data(chn) + offset);
3414 if (len < 0)
3415 len = ci_data(chn) - offset;
3416
3417 if (offset + len > ci_data(chn)) {
3418 lua_pushfstring(L, "offset or length out of range.");
3419 WILL_LJMP(lua_error(L));
3420 }
3421
3422 _hlua_channel_delete(chn, co_data(chn) + offset, len);
3423 lua_pushinteger(L, len);
Christopher Faulet23976d92021-08-06 09:59:49 +02003424 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003425}
3426
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003427/* Append data in the output side of the buffer. This data is immediately
3428 * sent. The function returns the amount of data written. If the buffer
3429 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003430 * if the channel is closed.
3431 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003432__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003433{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003434 struct channel *chn;
3435 const char *str;
3436 size_t sz, len;
3437 int l, ret;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003438 struct hlua *hlua;
3439
3440 /* Get hlua struct, or NULL if we execute from main lua state */
3441 hlua = hlua_gethlua(L);
3442 if (!hlua) {
3443 lua_pushnil(L);
3444 return 1;
3445 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003446
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003447 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3448 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3449 l = MAY_LJMP(luaL_checkinteger(L, 3));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003450
Willy Tarreau47860ed2015-03-10 14:07:50 +01003451 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003452 lua_pushinteger(L, -1);
3453 return 1;
3454 }
3455
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003456 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003457 * the request buffer if its not required.
3458 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003459 if (chn->buf.size == 0) {
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003460 if (HLUA_CANT_YIELD(hlua_gethlua(L)))
3461 return 1;
Willy Tarreau4b962a42018-11-15 11:03:21 +01003462 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003463 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003464 }
3465
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003466 len = c_room(chn);
3467 if (len > sz - l)
3468 len = sz - l;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003469
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003470 ret = _hlua_channel_insert(chn, L, ist2(str+l, len), co_data(chn));
3471 if (ret == -1) {
3472 lua_pop(L, 1);
3473 lua_pushinteger(L, -1);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003474 return 1;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003475 }
3476 if (ret) {
3477 c_adv(chn, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003478
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003479 l += ret;
3480 lua_pop(L, 1);
3481 lua_pushinteger(L, l);
3482 }
3483 if (l < sz) {
3484 /* Yield only if the channel's output is not empty.
3485 * Otherwise it means we cannot add more data. */
3486 if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L)))
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003487 return 1;
3488
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003489 /* If we are waiting for space in the response buffer, we
3490 * must set the flag WAKERESWR. This flag required the task
3491 * wake up if any activity is detected on the response buffer.
3492 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003493 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003494 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003495 else
3496 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003497 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003498 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003499
3500 return 1;
3501}
3502
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003503/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003504 * yield the LUA process, and resume it without checking the
3505 * input arguments.
3506 */
3507__LJMP static int hlua_channel_send(lua_State *L)
3508{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003509 struct channel *chn;
3510
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003511 MAY_LJMP(check_args(L, 2, "send"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003512 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3513 if (IS_HTX_STRM(chn_strm(chn))) {
3514 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3515 WILL_LJMP(lua_error(L));
3516 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003517 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003518 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003519}
3520
3521/* This function forward and amount of butes. The data pass from
3522 * the input side of the buffer to the output side, and can be
3523 * forwarded. This function never fails.
3524 *
3525 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003526 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003527 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003528__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003529{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003530 struct channel *chn;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003531 size_t len;
3532 int l, max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003533 struct hlua *hlua;
3534
3535 /* Get hlua struct, or NULL if we execute from main lua state */
3536 hlua = hlua_gethlua(L);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003537 if (!hlua) {
3538 lua_pushnil(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003539 return 1;
Thierry Fournier77016da2020-08-15 14:35:51 +02003540 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003541
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003542 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003543 len = MAY_LJMP(luaL_checkinteger(L, 2));
3544 l = MAY_LJMP(luaL_checkinteger(L, -1));
3545
3546 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003547 if (max > ci_data(chn))
3548 max = ci_data(chn);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003549
Willy Tarreau47860ed2015-03-10 14:07:50 +01003550 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003551
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003552 l += max;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003553 lua_pop(L, 1);
3554 lua_pushinteger(L, l);
3555
3556 /* Check if it miss bytes to forward. */
3557 if (l < len) {
3558 /* The the input channel or the output channel are closed, we
3559 * must return the amount of data forwarded.
3560 */
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003561 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003562 return 1;
3563
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003564 /* If we are waiting for space data in the response buffer, we
3565 * must set the flag WAKERESWR. This flag required the task
3566 * wake up if any activity is detected on the response buffer.
3567 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003568 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003569 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003570 else
3571 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003572
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003573 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003574 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003575 }
3576
3577 return 1;
3578}
3579
3580/* Just check the input and prepare the stack for the previous
3581 * function "hlua_channel_forward_yield"
3582 */
3583__LJMP static int hlua_channel_forward(lua_State *L)
3584{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003585 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003586
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003587 MAY_LJMP(check_args(L, 2, "forward"));
3588 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3589 if (IS_HTX_STRM(chn_strm(chn))) {
3590 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3591 WILL_LJMP(lua_error(L));
3592 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003593 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003594 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003595}
3596
3597/* Just returns the number of bytes available in the input
3598 * side of the buffer. This function never fails.
3599 */
3600__LJMP static int hlua_channel_get_in_len(lua_State *L)
3601{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003602 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003603
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003604 MAY_LJMP(check_args(L, 1, "input"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003605 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003606 if (IS_HTX_STRM(chn_strm(chn))) {
3607 struct htx *htx = htxbuf(&chn->buf);
3608 lua_pushinteger(L, htx->data - co_data(chn));
3609 }
3610 else
3611 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003612 return 1;
3613}
3614
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003615/* Returns true if the channel is full. */
3616__LJMP static int hlua_channel_is_full(lua_State *L)
3617{
3618 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003619
3620 MAY_LJMP(check_args(L, 1, "is_full"));
3621 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003622 /* ignore the reserve, we are not on a producer side (ie in an
3623 * applet).
3624 */
3625 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003626 return 1;
3627}
3628
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003629/* Returns true if the channel may still receive data. */
3630__LJMP static int hlua_channel_may_recv(lua_State *L)
3631{
3632 struct channel *chn;
3633
3634 MAY_LJMP(check_args(L, 1, "may_recv"));
3635 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3636 lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn)));
3637 return 1;
3638}
3639
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003640/* Returns true if the channel is the response channel. */
3641__LJMP static int hlua_channel_is_resp(lua_State *L)
3642{
3643 struct channel *chn;
3644
3645 MAY_LJMP(check_args(L, 1, "is_resp"));
3646 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3647
3648 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3649 return 1;
3650}
3651
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003652/* Just returns the number of bytes available in the output
3653 * side of the buffer. This function never fails.
3654 */
3655__LJMP static int hlua_channel_get_out_len(lua_State *L)
3656{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003657 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003658
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003659 MAY_LJMP(check_args(L, 1, "output"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003660 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003661 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003662 return 1;
3663}
3664
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003665/*
3666 *
3667 *
3668 * Class Fetches
3669 *
3670 *
3671 */
3672
3673/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003674 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003675 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003676__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003677{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003678 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003679}
3680
3681/* This function creates and push in the stack a fetch object according
3682 * with a current TXN.
3683 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003684static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003685{
Willy Tarreau7073c472015-04-06 11:15:40 +02003686 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003687
3688 /* Check stack size. */
3689 if (!lua_checkstack(L, 3))
3690 return 0;
3691
3692 /* Create the object: obj[0] = userdata.
3693 * Note that the base of the Fetches object is the
3694 * transaction object.
3695 */
3696 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003697 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003698 lua_rawseti(L, -2, 0);
3699
Willy Tarreau7073c472015-04-06 11:15:40 +02003700 hsmp->s = txn->s;
3701 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003702 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003703 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003704
3705 /* Pop a class sesison metatable and affect it to the userdata. */
3706 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3707 lua_setmetatable(L, -2);
3708
3709 return 1;
3710}
3711
3712/* This function is an LUA binding. It is called with each sample-fetch.
3713 * It uses closure argument to store the associated sample-fetch. It
3714 * returns only one argument or throws an error. An error is thrown
3715 * only if an error is encountered during the argument parsing. If
3716 * the "sample-fetch" function fails, nil is returned.
3717 */
3718__LJMP static int hlua_run_sample_fetch(lua_State *L)
3719{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003720 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003721 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003722 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003723 int i;
3724 struct sample smp;
3725
3726 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003727 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003728
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003729 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003730 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003731
Thierry FOURNIERca988662015-12-20 18:43:03 +01003732 /* Check execution authorization. */
3733 if (f->use & SMP_USE_HTTP_ANY &&
3734 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3735 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3736 "is not available in Lua services", f->kw);
3737 WILL_LJMP(lua_error(L));
3738 }
3739
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003740 /* Get extra arguments. */
3741 for (i = 0; i < lua_gettop(L) - 1; i++) {
3742 if (i >= ARGM_NBARGS)
3743 break;
3744 hlua_lua2arg(L, i + 2, &args[i]);
3745 }
3746 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003747 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003748
3749 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003750 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003751
3752 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003753 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003754 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003755 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003756 }
3757
3758 /* Initialise the sample. */
3759 memset(&smp, 0, sizeof(smp));
3760
3761 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003762 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003763 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003764 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003765 lua_pushstring(L, "");
3766 else
3767 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003768 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003769 }
3770
3771 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003772 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003773 hlua_smp2lua_str(L, &smp);
3774 else
3775 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003776
3777 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003778 free_args(args);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003779 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003780
3781 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003782 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003783 WILL_LJMP(lua_error(L));
3784 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003785}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003786
3787/*
3788 *
3789 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003790 * Class Converters
3791 *
3792 *
3793 */
3794
3795/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003796 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003797 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003798__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003799{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003800 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003801}
3802
3803/* This function creates and push in the stack a Converters object
3804 * according with a current TXN.
3805 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003806static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003807{
Willy Tarreau7073c472015-04-06 11:15:40 +02003808 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003809
3810 /* Check stack size. */
3811 if (!lua_checkstack(L, 3))
3812 return 0;
3813
3814 /* Create the object: obj[0] = userdata.
3815 * Note that the base of the Converters object is the
3816 * same than the TXN object.
3817 */
3818 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003819 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003820 lua_rawseti(L, -2, 0);
3821
Willy Tarreau7073c472015-04-06 11:15:40 +02003822 hsmp->s = txn->s;
3823 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003824 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003825 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003826
Willy Tarreau87b09662015-04-03 00:22:06 +02003827 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003828 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3829 lua_setmetatable(L, -2);
3830
3831 return 1;
3832}
3833
3834/* This function is an LUA binding. It is called with each converter.
3835 * It uses closure argument to store the associated converter. It
3836 * returns only one argument or throws an error. An error is thrown
3837 * only if an error is encountered during the argument parsing. If
3838 * the converter function function fails, nil is returned.
3839 */
3840__LJMP static int hlua_run_sample_conv(lua_State *L)
3841{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003842 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003843 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003844 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003845 int i;
3846 struct sample smp;
3847
3848 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003849 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003850
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003851 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003852 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003853
3854 /* Get extra arguments. */
3855 for (i = 0; i < lua_gettop(L) - 2; i++) {
3856 if (i >= ARGM_NBARGS)
3857 break;
3858 hlua_lua2arg(L, i + 3, &args[i]);
3859 }
3860 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003861 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003862
3863 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003864 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003865
3866 /* Run the special args checker. */
3867 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3868 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003869 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003870 }
3871
3872 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003873 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003874 if (!hlua_lua2smp(L, 2, &smp)) {
3875 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003876 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003877 }
3878
Willy Tarreau1777ea62016-03-10 16:15:46 +01003879 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3880
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003881 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003882 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003883 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003884 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003885 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003886 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003887 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3888 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003889 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003890 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003891 }
3892
3893 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003894 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003895 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003896 lua_pushstring(L, "");
3897 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003898 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003899 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003900 }
3901
3902 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003903 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003904 hlua_smp2lua_str(L, &smp);
3905 else
3906 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003907 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003908 free_args(args);
Willy Tarreaua678b432015-08-28 10:14:59 +02003909 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003910
3911 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003912 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003913 WILL_LJMP(lua_error(L));
3914 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003915}
3916
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003917/*
3918 *
3919 *
3920 * Class AppletTCP
3921 *
3922 *
3923 */
3924
3925/* Returns a struct hlua_txn if the stack entry "ud" is
3926 * a class stream, otherwise it throws an error.
3927 */
3928__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3929{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003930 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003931}
3932
3933/* This function creates and push in the stack an Applet object
3934 * according with a current TXN.
3935 */
3936static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3937{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003938 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003939 struct stream_interface *si = ctx->owner;
3940 struct stream *s = si_strm(si);
3941 struct proxy *p = s->be;
3942
3943 /* Check stack size. */
3944 if (!lua_checkstack(L, 3))
3945 return 0;
3946
3947 /* Create the object: obj[0] = userdata.
3948 * Note that the base of the Converters object is the
3949 * same than the TXN object.
3950 */
3951 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003952 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003953 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02003954 luactx->appctx = ctx;
3955 luactx->htxn.s = s;
3956 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003957
3958 /* Create the "f" field that contains a list of fetches. */
3959 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003960 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003961 return 0;
3962 lua_settable(L, -3);
3963
3964 /* Create the "sf" field that contains a list of stringsafe fetches. */
3965 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003966 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003967 return 0;
3968 lua_settable(L, -3);
3969
3970 /* Create the "c" field that contains a list of converters. */
3971 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003972 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003973 return 0;
3974 lua_settable(L, -3);
3975
3976 /* Create the "sc" field that contains a list of stringsafe converters. */
3977 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02003978 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003979 return 0;
3980 lua_settable(L, -3);
3981
3982 /* Pop a class stream metatable and affect it to the table. */
3983 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3984 lua_setmetatable(L, -2);
3985
3986 return 1;
3987}
3988
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003989__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3990{
Willy Tarreau7e702d12021-04-28 17:59:21 +02003991 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003992 struct stream *s;
3993 const char *name;
3994 size_t len;
3995 struct sample smp;
3996
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003997 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3998 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003999
4000 /* It is useles to retrieve the stream, but this function
4001 * runs only in a stream context.
4002 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004003 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004004 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004005 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004006
4007 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004008 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004009 hlua_lua2smp(L, 3, &smp);
4010
4011 /* Store the sample in a variable. */
4012 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004013
4014 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4015 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4016 else
4017 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4018
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004019 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004020}
4021
4022__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
4023{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004024 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004025 struct stream *s;
4026 const char *name;
4027 size_t len;
4028 struct sample smp;
4029
4030 MAY_LJMP(check_args(L, 2, "unset_var"));
4031
4032 /* It is useles to retrieve the stream, but this function
4033 * runs only in a stream context.
4034 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004035 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004036 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004037 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004038
4039 /* Unset the variable. */
4040 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004041 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4042 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004043}
4044
4045__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
4046{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004047 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004048 struct stream *s;
4049 const char *name;
4050 size_t len;
4051 struct sample smp;
4052
4053 MAY_LJMP(check_args(L, 2, "get_var"));
4054
4055 /* It is useles to retrieve the stream, but this function
4056 * runs only in a stream context.
4057 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004058 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004059 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004060 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004061
4062 smp_set_owner(&smp, s->be, s->sess, s, 0);
4063 if (!vars_get_by_name(name, len, &smp)) {
4064 lua_pushnil(L);
4065 return 1;
4066 }
4067
4068 return hlua_smp2lua(L, &smp);
4069}
4070
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004071__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
4072{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004073 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4074 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004075 struct hlua *hlua;
4076
4077 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004078 if (!s->hlua)
4079 return 0;
4080 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004081
4082 MAY_LJMP(check_args(L, 2, "set_priv"));
4083
4084 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004085 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004086
4087 /* Get and store new value. */
4088 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4089 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4090
4091 return 0;
4092}
4093
4094__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
4095{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004096 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4097 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004098 struct hlua *hlua;
4099
4100 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004101 if (!s->hlua) {
4102 lua_pushnil(L);
4103 return 1;
4104 }
4105 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004106
4107 /* Push configuration index in the stack. */
4108 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4109
4110 return 1;
4111}
4112
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004113/* If expected data not yet available, it returns a yield. This function
4114 * consumes the data in the buffer. It returns a string containing the
4115 * data. This string can be empty.
4116 */
4117__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
4118{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004119 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4120 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004121 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004122 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004123 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004124 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004125 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004126
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004127 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004128 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004129
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004130 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004131 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004132 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004133 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004134 }
4135
4136 /* End of data: commit the total strings and return. */
4137 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004138 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004139 return 1;
4140 }
4141
4142 /* Ensure that the block 2 length is usable. */
4143 if (ret == 1)
4144 len2 = 0;
4145
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004146 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004147 luaL_addlstring(&luactx->b, blk1, len1);
4148 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004149
4150 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004151 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004152 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004153 return 1;
4154}
4155
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004156/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004157__LJMP static int hlua_applet_tcp_getline(lua_State *L)
4158{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004159 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004160
4161 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004162 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004163
4164 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
4165}
4166
4167/* If expected data not yet available, it returns a yield. This function
4168 * consumes the data in the buffer. It returns a string containing the
4169 * data. This string can be empty.
4170 */
4171__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
4172{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004173 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4174 struct stream_interface *si = luactx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004175 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004176 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004177 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004178 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004179 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004180 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004181
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004182 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004183 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004184
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004185 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004186 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004187 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004188 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004189 }
4190
4191 /* End of data: commit the total strings and return. */
4192 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004193 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004194 return 1;
4195 }
4196
4197 /* Ensure that the block 2 length is usable. */
4198 if (ret == 1)
4199 len2 = 0;
4200
4201 if (len == -1) {
4202
4203 /* If len == -1, catenate all the data avalaile and
4204 * yield because we want to get all the data until
4205 * the end of data stream.
4206 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004207 luaL_addlstring(&luactx->b, blk1, len1);
4208 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004209 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004210 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004211 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004212
4213 } else {
4214
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004215 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004216 if (len1 > len)
4217 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004218 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004219 len -= len1;
4220
4221 /* Copy the second block. */
4222 if (len2 > len)
4223 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004224 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004225 len -= len2;
4226
4227 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004228 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004229
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004230 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004231 if (len > 0) {
4232 lua_pushinteger(L, len);
4233 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004234 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004235 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004236 }
4237
4238 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004239 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004240 return 1;
4241 }
4242
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004243 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004244 hlua_pusherror(L, "Lua: internal error");
4245 WILL_LJMP(lua_error(L));
4246 return 0;
4247}
4248
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004249/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004250__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4251{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004252 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004253 int len = -1;
4254
4255 if (lua_gettop(L) > 2)
4256 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4257 if (lua_gettop(L) >= 2) {
4258 len = MAY_LJMP(luaL_checkinteger(L, 2));
4259 lua_pop(L, 1);
4260 }
4261
4262 /* Confirm or set the required length */
4263 lua_pushinteger(L, len);
4264
4265 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004266 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004267
4268 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4269}
4270
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004271/* Append data in the output side of the buffer. This data is immediately
4272 * sent. The function returns the amount of data written. If the buffer
4273 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004274 * if the channel is closed.
4275 */
4276__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4277{
4278 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004279 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004280 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4281 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004282 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004283 struct channel *chn = si_ic(si);
4284 int max;
4285
4286 /* Get the max amount of data which can write as input in the channel. */
4287 max = channel_recv_max(chn);
4288 if (max > (len - l))
4289 max = len - l;
4290
4291 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004292 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004293
4294 /* update counters. */
4295 l += max;
4296 lua_pop(L, 1);
4297 lua_pushinteger(L, l);
4298
4299 /* If some data is not send, declares the situation to the
4300 * applet, and returns a yield.
4301 */
4302 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004303 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004304 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004305 }
4306
4307 return 1;
4308}
4309
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004310/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004311 * yield the LUA process, and resume it without checking the
4312 * input arguments.
4313 */
4314__LJMP static int hlua_applet_tcp_send(lua_State *L)
4315{
4316 MAY_LJMP(check_args(L, 2, "send"));
4317 lua_pushinteger(L, 0);
4318
4319 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4320}
4321
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004322/*
4323 *
4324 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004325 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004326 *
4327 *
4328 */
4329
4330/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004331 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004332 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004333__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004334{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004335 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004336}
4337
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004338/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004339 * according with a current TXN.
4340 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004341static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004342{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004343 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004344 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004345 struct stream_interface *si = ctx->owner;
4346 struct stream *s = si_strm(si);
4347 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004348 struct htx *htx;
4349 struct htx_blk *blk;
4350 struct htx_sl *sl;
4351 struct ist path;
4352 unsigned long long len = 0;
4353 int32_t pos;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004354 struct http_uri_parser parser;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004355
4356 /* Check stack size. */
4357 if (!lua_checkstack(L, 3))
4358 return 0;
4359
4360 /* Create the object: obj[0] = userdata.
4361 * Note that the base of the Converters object is the
4362 * same than the TXN object.
4363 */
4364 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004365 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004366 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004367 luactx->appctx = ctx;
4368 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4369 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4370 luactx->htxn.s = s;
4371 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004372
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004373 /* Create the "f" field that contains a list of fetches. */
4374 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004375 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004376 return 0;
4377 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004378
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004379 /* Create the "sf" field that contains a list of stringsafe fetches. */
4380 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004381 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004382 return 0;
4383 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004384
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004385 /* Create the "c" field that contains a list of converters. */
4386 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004387 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004388 return 0;
4389 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004390
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004391 /* Create the "sc" field that contains a list of stringsafe converters. */
4392 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004393 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004394 return 0;
4395 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004396
Christopher Fauleta2097962019-07-15 16:25:33 +02004397 htx = htxbuf(&s->req.buf);
4398 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004399 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004400 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004401
Christopher Fauleta2097962019-07-15 16:25:33 +02004402 /* Stores the request method. */
4403 lua_pushstring(L, "method");
4404 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4405 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004406
Christopher Fauleta2097962019-07-15 16:25:33 +02004407 /* Stores the http version. */
4408 lua_pushstring(L, "version");
4409 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4410 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004411
Christopher Fauleta2097962019-07-15 16:25:33 +02004412 /* creates an array of headers. hlua_http_get_headers() crates and push
4413 * the array on the top of the stack.
4414 */
4415 lua_pushstring(L, "headers");
4416 htxn.s = s;
4417 htxn.p = px;
4418 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004419 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004420 return 0;
4421 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004422
Amaury Denoyellec453f952021-07-06 11:40:12 +02004423 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4424 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004425 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004426 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004427
Christopher Fauleta2097962019-07-15 16:25:33 +02004428 p = path.ptr;
4429 end = path.ptr + path.len;
4430 q = p;
4431 while (q < end && *q != '?')
4432 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004433
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004434 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004435 lua_pushstring(L, "path");
4436 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004437 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004438
Christopher Fauleta2097962019-07-15 16:25:33 +02004439 /* Stores the query string. */
4440 lua_pushstring(L, "qs");
4441 if (*q == '?')
4442 q++;
4443 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004444 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004445 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004446
Christopher Fauleta2097962019-07-15 16:25:33 +02004447 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4448 struct htx_blk *blk = htx_get_blk(htx, pos);
4449 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004450
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004451 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004452 break;
4453 if (type == HTX_BLK_DATA)
4454 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004455 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004456 if (htx->extra != ULLONG_MAX)
4457 len += htx->extra;
4458
4459 /* Stores the request path. */
4460 lua_pushstring(L, "length");
4461 lua_pushinteger(L, len);
4462 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004463
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004464 /* Create an empty array of HTTP request headers. */
4465 lua_pushstring(L, "response");
4466 lua_newtable(L);
4467 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004468
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004469 /* Pop a class stream metatable and affect it to the table. */
4470 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4471 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004472
4473 return 1;
4474}
4475
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004476__LJMP static int hlua_applet_http_set_var(lua_State *L)
4477{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004478 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004479 struct stream *s;
4480 const char *name;
4481 size_t len;
4482 struct sample smp;
4483
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004484 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4485 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004486
4487 /* It is useles to retrieve the stream, but this function
4488 * runs only in a stream context.
4489 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004490 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004491 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004492 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004493
4494 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004495 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004496 hlua_lua2smp(L, 3, &smp);
4497
4498 /* Store the sample in a variable. */
4499 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004500
4501 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4502 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4503 else
4504 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4505
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004506 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004507}
4508
4509__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4510{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004511 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004512 struct stream *s;
4513 const char *name;
4514 size_t len;
4515 struct sample smp;
4516
4517 MAY_LJMP(check_args(L, 2, "unset_var"));
4518
4519 /* It is useles to retrieve the stream, but this function
4520 * runs only in a stream context.
4521 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004522 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004523 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004524 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004525
4526 /* Unset the variable. */
4527 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004528 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4529 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004530}
4531
4532__LJMP static int hlua_applet_http_get_var(lua_State *L)
4533{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004534 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004535 struct stream *s;
4536 const char *name;
4537 size_t len;
4538 struct sample smp;
4539
4540 MAY_LJMP(check_args(L, 2, "get_var"));
4541
4542 /* It is useles to retrieve the stream, but this function
4543 * runs only in a stream context.
4544 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004545 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004546 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004547 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004548
4549 smp_set_owner(&smp, s->be, s->sess, s, 0);
4550 if (!vars_get_by_name(name, len, &smp)) {
4551 lua_pushnil(L);
4552 return 1;
4553 }
4554
4555 return hlua_smp2lua(L, &smp);
4556}
4557
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004558__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4559{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004560 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4561 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004562 struct hlua *hlua;
4563
4564 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004565 if (!s->hlua)
4566 return 0;
4567 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004568
4569 MAY_LJMP(check_args(L, 2, "set_priv"));
4570
4571 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004572 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004573
4574 /* Get and store new value. */
4575 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4576 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4577
4578 return 0;
4579}
4580
4581__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4582{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004583 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4584 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004585 struct hlua *hlua;
4586
4587 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004588 if (!s->hlua) {
4589 lua_pushnil(L);
4590 return 1;
4591 }
4592 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004593
4594 /* Push configuration index in the stack. */
4595 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4596
4597 return 1;
4598}
4599
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004600/* If expected data not yet available, it returns a yield. This function
4601 * consumes the data in the buffer. It returns a string containing the
4602 * data. This string can be empty.
4603 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004604__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004605{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004606 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4607 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004608 struct channel *req = si_oc(si);
4609 struct htx *htx;
4610 struct htx_blk *blk;
4611 size_t count;
4612 int stop = 0;
4613
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004614 htx = htx_from_buf(&req->buf);
4615 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004616 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004617
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004618 while (count && !stop && blk) {
4619 enum htx_blk_type type = htx_get_blk_type(blk);
4620 uint32_t sz = htx_get_blksz(blk);
4621 struct ist v;
4622 uint32_t vlen;
4623 char *nl;
4624
4625 vlen = sz;
4626 if (vlen > count) {
4627 if (type != HTX_BLK_DATA)
4628 break;
4629 vlen = count;
4630 }
4631
4632 switch (type) {
4633 case HTX_BLK_UNUSED:
4634 break;
4635
4636 case HTX_BLK_DATA:
4637 v = htx_get_blk_value(htx, blk);
4638 v.len = vlen;
4639 nl = istchr(v, '\n');
4640 if (nl != NULL) {
4641 stop = 1;
4642 vlen = nl - v.ptr + 1;
4643 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004644 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004645 break;
4646
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004647 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004648 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004649 stop = 1;
4650 break;
4651
4652 default:
4653 break;
4654 }
4655
4656 co_set_data(req, co_data(req) - vlen);
4657 count -= vlen;
4658 if (sz == vlen)
4659 blk = htx_remove_blk(htx, blk);
4660 else {
4661 htx_cut_data_blk(htx, blk, vlen);
4662 break;
4663 }
4664 }
4665
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004666 /* The message was fully consumed and no more data are expected
4667 * (EOM flag set).
4668 */
4669 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4670 stop = 1;
4671
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004672 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004673 if (!stop) {
4674 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004675 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004676 }
4677
4678 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004679 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004680 return 1;
4681}
4682
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004683
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004684/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004685__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004686{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004687 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004688
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004689 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004690 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004691
Christopher Fauleta2097962019-07-15 16:25:33 +02004692 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004693}
4694
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004695/* If expected data not yet available, it returns a yield. This function
4696 * consumes the data in the buffer. It returns a string containing the
4697 * data. This string can be empty.
4698 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004699__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004700{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004701 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4702 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004703 struct channel *req = si_oc(si);
4704 struct htx *htx;
4705 struct htx_blk *blk;
4706 size_t count;
4707 int len;
4708
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004709 htx = htx_from_buf(&req->buf);
4710 len = MAY_LJMP(luaL_checkinteger(L, 2));
4711 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004712 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004713 while (count && len && blk) {
4714 enum htx_blk_type type = htx_get_blk_type(blk);
4715 uint32_t sz = htx_get_blksz(blk);
4716 struct ist v;
4717 uint32_t vlen;
4718
4719 vlen = sz;
4720 if (len > 0 && vlen > len)
4721 vlen = len;
4722 if (vlen > count) {
4723 if (type != HTX_BLK_DATA)
4724 break;
4725 vlen = count;
4726 }
4727
4728 switch (type) {
4729 case HTX_BLK_UNUSED:
4730 break;
4731
4732 case HTX_BLK_DATA:
4733 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004734 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004735 break;
4736
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004737 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004738 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004739 len = 0;
4740 break;
4741
4742 default:
4743 break;
4744 }
4745
4746 co_set_data(req, co_data(req) - vlen);
4747 count -= vlen;
4748 if (len > 0)
4749 len -= vlen;
4750 if (sz == vlen)
4751 blk = htx_remove_blk(htx, blk);
4752 else {
4753 htx_cut_data_blk(htx, blk, vlen);
4754 break;
4755 }
4756 }
4757
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004758 /* The message was fully consumed and no more data are expected
4759 * (EOM flag set).
4760 */
4761 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4762 len = 0;
4763
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004764 htx_to_buf(htx, &req->buf);
4765
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004766 /* If we are no other data available, yield waiting for new data. */
4767 if (len) {
4768 if (len > 0) {
4769 lua_pushinteger(L, len);
4770 lua_replace(L, 2);
4771 }
4772 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004773 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004774 }
4775
4776 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004777 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004778 return 1;
4779}
4780
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004781/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004782__LJMP static int hlua_applet_http_recv(lua_State *L)
4783{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004784 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004785 int len = -1;
4786
4787 /* Check arguments. */
4788 if (lua_gettop(L) > 2)
4789 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4790 if (lua_gettop(L) >= 2) {
4791 len = MAY_LJMP(luaL_checkinteger(L, 2));
4792 lua_pop(L, 1);
4793 }
4794
Christopher Fauleta2097962019-07-15 16:25:33 +02004795 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004796
Christopher Fauleta2097962019-07-15 16:25:33 +02004797 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004798 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004799
Christopher Fauleta2097962019-07-15 16:25:33 +02004800 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004801}
4802
4803/* Append data in the output side of the buffer. This data is immediately
4804 * sent. The function returns the amount of data written. If the buffer
4805 * cannot contain the data, the function yields. The function returns -1
4806 * if the channel is closed.
4807 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004808__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004809{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004810 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4811 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004812 struct channel *res = si_ic(si);
4813 struct htx *htx = htx_from_buf(&res->buf);
4814 const char *data;
4815 size_t len;
4816 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4817 int max;
4818
Christopher Faulet9060fc02019-07-03 11:39:30 +02004819 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004820 if (!max)
4821 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004822
4823 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4824
4825 /* Get the max amount of data which can write as input in the channel. */
4826 if (max > (len - l))
4827 max = len - l;
4828
4829 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004830 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004831 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004832
4833 /* update counters. */
4834 l += max;
4835 lua_pop(L, 1);
4836 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004837
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004838 /* If some data is not send, declares the situation to the
4839 * applet, and returns a yield.
4840 */
4841 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004842 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004843 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004844 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004845 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004846 }
4847
Christopher Fauleta2097962019-07-15 16:25:33 +02004848 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004849 return 1;
4850}
4851
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004852/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004853 * yield the LUA process, and resume it without checking the
4854 * input arguments.
4855 */
4856__LJMP static int hlua_applet_http_send(lua_State *L)
4857{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004858 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004859
4860 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004861 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004862 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4863 WILL_LJMP(lua_error(L));
4864 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004865
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004866 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004867 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004868
Christopher Fauleta2097962019-07-15 16:25:33 +02004869 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004870}
4871
4872__LJMP static int hlua_applet_http_addheader(lua_State *L)
4873{
4874 const char *name;
4875 int ret;
4876
4877 MAY_LJMP(hlua_checkapplet_http(L, 1));
4878 name = MAY_LJMP(luaL_checkstring(L, 2));
4879 MAY_LJMP(luaL_checkstring(L, 3));
4880
4881 /* Push in the stack the "response" entry. */
4882 ret = lua_getfield(L, 1, "response");
4883 if (ret != LUA_TTABLE) {
4884 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4885 "is expected as an array. %s found", lua_typename(L, ret));
4886 WILL_LJMP(lua_error(L));
4887 }
4888
4889 /* check if the header is already registered if it is not
4890 * the case, register it.
4891 */
4892 ret = lua_getfield(L, -1, name);
4893 if (ret == LUA_TNIL) {
4894
4895 /* Entry not found. */
4896 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4897
4898 /* Insert the new header name in the array in the top of the stack.
4899 * It left the new array in the top of the stack.
4900 */
4901 lua_newtable(L);
4902 lua_pushvalue(L, 2);
4903 lua_pushvalue(L, -2);
4904 lua_settable(L, -4);
4905
4906 } else if (ret != LUA_TTABLE) {
4907
4908 /* corruption error. */
4909 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4910 "is expected as an array. %s found", name, lua_typename(L, ret));
4911 WILL_LJMP(lua_error(L));
4912 }
4913
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004914 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004915 * the header value as new entry.
4916 */
4917 lua_pushvalue(L, 3);
4918 ret = lua_rawlen(L, -2);
4919 lua_rawseti(L, -2, ret + 1);
4920 lua_pushboolean(L, 1);
4921 return 1;
4922}
4923
4924__LJMP static int hlua_applet_http_status(lua_State *L)
4925{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004926 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004927 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004928 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004929
4930 if (status < 100 || status > 599) {
4931 lua_pushboolean(L, 0);
4932 return 1;
4933 }
4934
Willy Tarreau7e702d12021-04-28 17:59:21 +02004935 luactx->appctx->ctx.hlua_apphttp.status = status;
4936 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004937 lua_pushboolean(L, 1);
4938 return 1;
4939}
4940
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004941
Christopher Fauleta2097962019-07-15 16:25:33 +02004942__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004943{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004944 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4945 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004946 struct channel *res = si_ic(si);
4947 struct htx *htx;
4948 struct htx_sl *sl;
4949 struct h1m h1m;
4950 const char *status, *reason;
4951 const char *name, *value;
4952 size_t nlen, vlen;
4953 unsigned int flags;
4954
4955 /* Send the message at once. */
4956 htx = htx_from_buf(&res->buf);
4957 h1m_init_res(&h1m);
4958
4959 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004960 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4961 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004962 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02004963 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
4964 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004965 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4966 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4967 }
4968 else {
4969 flags = HTX_SL_F_IS_RESP;
4970 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4971 }
4972 if (!sl) {
4973 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004974 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004975 WILL_LJMP(lua_error(L));
4976 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004977 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004978
4979 /* Get the array associated to the field "response" in the object AppletHTTP. */
4980 lua_pushvalue(L, 0);
4981 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4982 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004983 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004984 WILL_LJMP(lua_error(L));
4985 }
4986
4987 /* Browse the list of headers. */
4988 lua_pushnil(L);
4989 while(lua_next(L, -2) != 0) {
4990 /* We expect a string as -2. */
4991 if (lua_type(L, -2) != LUA_TSTRING) {
4992 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02004993 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004994 lua_typename(L, lua_type(L, -2)));
4995 WILL_LJMP(lua_error(L));
4996 }
4997 name = lua_tolstring(L, -2, &nlen);
4998
4999 /* We expect an array as -1. */
5000 if (lua_type(L, -1) != LUA_TTABLE) {
5001 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 +02005002 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005003 name,
5004 lua_typename(L, lua_type(L, -1)));
5005 WILL_LJMP(lua_error(L));
5006 }
5007
5008 /* Browse the table who is on the top of the stack. */
5009 lua_pushnil(L);
5010 while(lua_next(L, -2) != 0) {
5011 int id;
5012
5013 /* We expect a number as -2. */
5014 if (lua_type(L, -2) != LUA_TNUMBER) {
5015 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 +02005016 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005017 name,
5018 lua_typename(L, lua_type(L, -2)));
5019 WILL_LJMP(lua_error(L));
5020 }
5021 id = lua_tointeger(L, -2);
5022
5023 /* We expect a string as -2. */
5024 if (lua_type(L, -1) != LUA_TSTRING) {
5025 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 +02005026 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005027 name, id,
5028 lua_typename(L, lua_type(L, -1)));
5029 WILL_LJMP(lua_error(L));
5030 }
5031 value = lua_tolstring(L, -1, &vlen);
5032
5033 /* Simple Protocol checks. */
5034 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005035 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005036 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
5037 struct ist v = ist2(value, vlen);
5038 int ret;
5039
5040 ret = h1_parse_cont_len_header(&h1m, &v);
5041 if (ret < 0) {
5042 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005043 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005044 name);
5045 WILL_LJMP(lua_error(L));
5046 }
5047 else if (ret == 0)
5048 goto next; /* Skip it */
5049 }
5050
5051 /* Add a new header */
5052 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
5053 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005054 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005055 name);
5056 WILL_LJMP(lua_error(L));
5057 }
5058 next:
5059 /* Remove the array from the stack, and get next element with a remaining string. */
5060 lua_pop(L, 1);
5061 }
5062
5063 /* Remove the array from the stack, and get next element with a remaining string. */
5064 lua_pop(L, 1);
5065 }
5066
5067 if (h1m.flags & H1_MF_CHNK)
5068 h1m.flags &= ~H1_MF_CLEN;
5069 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5070 h1m.flags |= H1_MF_XFER_LEN;
5071
5072 /* Uset HTX start-line flags */
5073 if (h1m.flags & H1_MF_XFER_ENC)
5074 flags |= HTX_SL_F_XFER_ENC;
5075 if (h1m.flags & H1_MF_XFER_LEN) {
5076 flags |= HTX_SL_F_XFER_LEN;
5077 if (h1m.flags & H1_MF_CHNK)
5078 flags |= HTX_SL_F_CHNK;
5079 else if (h1m.flags & H1_MF_CLEN)
5080 flags |= HTX_SL_F_CLEN;
5081 if (h1m.body_len == 0)
5082 flags |= HTX_SL_F_BODYLESS;
5083 }
5084 sl->flags |= flags;
5085
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07005086 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005087 * and the status code implies the presence of a message body, we must
5088 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005089 * for the keepalive compliance. If the applet announces a transfer-encoding
5090 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005091 */
5092 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02005093 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
5094 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
5095 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005096 /* Add a new header */
5097 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
5098 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
5099 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005100 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005101 WILL_LJMP(lua_error(L));
5102 }
5103 }
5104
5105 /* Finalize headers. */
5106 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
5107 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005108 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005109 WILL_LJMP(lua_error(L));
5110 }
5111
5112 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
5113 b_reset(&res->buf);
5114 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5115 WILL_LJMP(lua_error(L));
5116 }
5117
5118 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01005119 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005120
5121 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005122 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005123 return 0;
5124
5125}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005126/* We will build the status line and the headers of the HTTP response.
5127 * We will try send at once if its not possible, we give back the hand
5128 * waiting for more room.
5129 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005130__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005131{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005132 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
5133 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005134 struct channel *res = si_ic(si);
5135
5136 if (co_data(res)) {
5137 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02005138 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005139 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005140 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005141}
5142
5143
Christopher Fauleta2097962019-07-15 16:25:33 +02005144__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005145{
Christopher Fauleta2097962019-07-15 16:25:33 +02005146 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005147}
5148
Christopher Fauleta2097962019-07-15 16:25:33 +02005149/*
5150 *
5151 *
5152 * Class HTTP
5153 *
5154 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005155 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005156
5157/* Returns a struct hlua_txn if the stack entry "ud" is
5158 * a class stream, otherwise it throws an error.
5159 */
5160__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005161{
Christopher Fauleta2097962019-07-15 16:25:33 +02005162 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
5163}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005164
Christopher Fauleta2097962019-07-15 16:25:33 +02005165/* This function creates and push in the stack a HTTP object
5166 * according with a current TXN.
5167 */
5168static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5169{
5170 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005171
Christopher Fauleta2097962019-07-15 16:25:33 +02005172 /* Check stack size. */
5173 if (!lua_checkstack(L, 3))
5174 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005175
Christopher Fauleta2097962019-07-15 16:25:33 +02005176 /* Create the object: obj[0] = userdata.
5177 * Note that the base of the Converters object is the
5178 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005179 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005180 lua_newtable(L);
5181 htxn = lua_newuserdata(L, sizeof(*htxn));
5182 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005183
5184 htxn->s = txn->s;
5185 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005186 htxn->dir = txn->dir;
5187 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005188
5189 /* Pop a class stream metatable and affect it to the table. */
5190 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5191 lua_setmetatable(L, -2);
5192
5193 return 1;
5194}
5195
5196/* This function creates ans returns an array of HTTP headers.
5197 * This function does not fails. It is used as wrapper with the
5198 * 2 following functions.
5199 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005200__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005201{
Christopher Fauleta2097962019-07-15 16:25:33 +02005202 struct htx *htx;
5203 int32_t pos;
5204
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005205 /* Create the table. */
5206 lua_newtable(L);
5207
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005208
Christopher Fauleta2097962019-07-15 16:25:33 +02005209 htx = htxbuf(&msg->chn->buf);
5210 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5211 struct htx_blk *blk = htx_get_blk(htx, pos);
5212 enum htx_blk_type type = htx_get_blk_type(blk);
5213 struct ist n, v;
5214 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005215
Christopher Fauleta2097962019-07-15 16:25:33 +02005216 if (type == HTX_BLK_HDR) {
5217 n = htx_get_blk_name(htx,blk);
5218 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005219 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005220 else if (type == HTX_BLK_EOH)
5221 break;
5222 else
5223 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005224
Christopher Fauleta2097962019-07-15 16:25:33 +02005225 /* Check for existing entry:
5226 * assume that the table is on the top of the stack, and
5227 * push the key in the stack, the function lua_gettable()
5228 * perform the lookup.
5229 */
5230 lua_pushlstring(L, n.ptr, n.len);
5231 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005232
Christopher Fauleta2097962019-07-15 16:25:33 +02005233 switch (lua_type(L, -1)) {
5234 case LUA_TNIL:
5235 /* Table not found, create it. */
5236 lua_pop(L, 1); /* remove the nil value. */
5237 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5238 lua_newtable(L); /* create and push empty table. */
5239 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5240 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5241 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005242 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005243
Christopher Fauleta2097962019-07-15 16:25:33 +02005244 case LUA_TTABLE:
5245 /* Entry found: push the value in the table. */
5246 len = lua_rawlen(L, -1);
5247 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5248 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5249 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5250 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005251
Christopher Fauleta2097962019-07-15 16:25:33 +02005252 default:
5253 /* Other cases are errors. */
5254 hlua_pusherror(L, "internal error during the parsing of headers.");
5255 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005256 }
5257 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005258 return 1;
5259}
5260
5261__LJMP static int hlua_http_req_get_headers(lua_State *L)
5262{
5263 struct hlua_txn *htxn;
5264
5265 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5266 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5267
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005268 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005269 WILL_LJMP(lua_error(L));
5270
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005271 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005272}
5273
5274__LJMP static int hlua_http_res_get_headers(lua_State *L)
5275{
5276 struct hlua_txn *htxn;
5277
5278 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5279 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5280
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005281 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005282 WILL_LJMP(lua_error(L));
5283
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005284 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005285}
5286
5287/* This function replace full header, or just a value in
5288 * the request or in the response. It is a wrapper fir the
5289 * 4 following functions.
5290 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005291__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005292{
5293 size_t name_len;
5294 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5295 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5296 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005297 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005298 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005299
Dragan Dosen26743032019-04-30 15:54:36 +02005300 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005301 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5302
Christopher Fauleta2097962019-07-15 16:25:33 +02005303 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005304 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005305 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005306 return 0;
5307}
5308
5309__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5310{
5311 struct hlua_txn *htxn;
5312
5313 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5314 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5315
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005316 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005317 WILL_LJMP(lua_error(L));
5318
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005319 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005320}
5321
5322__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5323{
5324 struct hlua_txn *htxn;
5325
5326 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5327 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5328
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005329 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005330 WILL_LJMP(lua_error(L));
5331
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005332 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005333}
5334
5335__LJMP static int hlua_http_req_rep_val(lua_State *L)
5336{
5337 struct hlua_txn *htxn;
5338
5339 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5340 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5341
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005342 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005343 WILL_LJMP(lua_error(L));
5344
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005345 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005346}
5347
5348__LJMP static int hlua_http_res_rep_val(lua_State *L)
5349{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005350 struct hlua_txn *htxn;
5351
5352 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5353 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5354
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005355 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005356 WILL_LJMP(lua_error(L));
5357
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005358 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005359}
5360
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005361/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005362 * It is a wrapper for the 2 following functions.
5363 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005364__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005365{
5366 size_t len;
5367 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005368 struct htx *htx = htxbuf(&msg->chn->buf);
5369 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005370
Christopher Fauleta2097962019-07-15 16:25:33 +02005371 ctx.blk = NULL;
5372 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5373 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005374 return 0;
5375}
5376
5377__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5378{
5379 struct hlua_txn *htxn;
5380
5381 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5382 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5383
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005384 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005385 WILL_LJMP(lua_error(L));
5386
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005387 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005388}
5389
5390__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5391{
5392 struct hlua_txn *htxn;
5393
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005394 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005395 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5396
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005397 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005398 WILL_LJMP(lua_error(L));
5399
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005400 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005401}
5402
5403/* This function adds an header. It is a wrapper used by
5404 * the 2 following functions.
5405 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005406__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005407{
5408 size_t name_len;
5409 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5410 size_t value_len;
5411 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005412 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005413
Christopher Fauleta2097962019-07-15 16:25:33 +02005414 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5415 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005416 return 0;
5417}
5418
5419__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5420{
5421 struct hlua_txn *htxn;
5422
5423 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5424 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5425
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005426 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005427 WILL_LJMP(lua_error(L));
5428
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005429 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005430}
5431
5432__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5433{
5434 struct hlua_txn *htxn;
5435
5436 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5437 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5438
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005439 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005440 WILL_LJMP(lua_error(L));
5441
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005442 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005443}
5444
5445static int hlua_http_req_set_hdr(lua_State *L)
5446{
5447 struct hlua_txn *htxn;
5448
5449 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5450 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5451
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005452 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005453 WILL_LJMP(lua_error(L));
5454
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005455 hlua_http_del_hdr(L, &htxn->s->txn->req);
5456 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005457}
5458
5459static int hlua_http_res_set_hdr(lua_State *L)
5460{
5461 struct hlua_txn *htxn;
5462
5463 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5464 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5465
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005466 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005467 WILL_LJMP(lua_error(L));
5468
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005469 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5470 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005471}
5472
5473/* This function set the method. */
5474static int hlua_http_req_set_meth(lua_State *L)
5475{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005476 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005477 size_t name_len;
5478 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005479
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005480 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005481 WILL_LJMP(lua_error(L));
5482
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005483 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005484 return 1;
5485}
5486
5487/* This function set the method. */
5488static int hlua_http_req_set_path(lua_State *L)
5489{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005490 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005491 size_t name_len;
5492 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005493
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005494 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005495 WILL_LJMP(lua_error(L));
5496
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005497 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005498 return 1;
5499}
5500
5501/* This function set the query-string. */
5502static int hlua_http_req_set_query(lua_State *L)
5503{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005504 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005505 size_t name_len;
5506 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005507
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005508 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005509 WILL_LJMP(lua_error(L));
5510
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005511 /* Check length. */
5512 if (name_len > trash.size - 1) {
5513 lua_pushboolean(L, 0);
5514 return 1;
5515 }
5516
5517 /* Add the mark question as prefix. */
5518 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005519 trash.area[trash.data++] = '?';
5520 memcpy(trash.area + trash.data, name, name_len);
5521 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005522
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005523 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005524 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005525 return 1;
5526}
5527
5528/* This function set the uri. */
5529static int hlua_http_req_set_uri(lua_State *L)
5530{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005531 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005532 size_t name_len;
5533 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005534
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005535 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005536 WILL_LJMP(lua_error(L));
5537
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005538 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005539 return 1;
5540}
5541
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005542/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005543static int hlua_http_res_set_status(lua_State *L)
5544{
5545 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5546 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005547 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5548 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005549
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005550 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005551 WILL_LJMP(lua_error(L));
5552
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005553 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005554 return 0;
5555}
5556
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005557/*
5558 *
5559 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005560 * Class TXN
5561 *
5562 *
5563 */
5564
5565/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005566 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005567 */
5568__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5569{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005570 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005571}
5572
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005573__LJMP static int hlua_set_var(lua_State *L)
5574{
5575 struct hlua_txn *htxn;
5576 const char *name;
5577 size_t len;
5578 struct sample smp;
5579
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005580 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5581 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005582
5583 /* It is useles to retrieve the stream, but this function
5584 * runs only in a stream context.
5585 */
5586 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5587 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5588
5589 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005590 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005591 hlua_lua2smp(L, 3, &smp);
5592
5593 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005594 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005595
5596 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5597 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5598 else
5599 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5600
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005601 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005602}
5603
Christopher Faulet85d79c92016-11-09 16:54:56 +01005604__LJMP static int hlua_unset_var(lua_State *L)
5605{
5606 struct hlua_txn *htxn;
5607 const char *name;
5608 size_t len;
5609 struct sample smp;
5610
5611 MAY_LJMP(check_args(L, 2, "unset_var"));
5612
5613 /* It is useles to retrieve the stream, but this function
5614 * runs only in a stream context.
5615 */
5616 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5617 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5618
5619 /* Unset the variable. */
5620 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005621 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5622 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005623}
5624
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005625__LJMP static int hlua_get_var(lua_State *L)
5626{
5627 struct hlua_txn *htxn;
5628 const char *name;
5629 size_t len;
5630 struct sample smp;
5631
5632 MAY_LJMP(check_args(L, 2, "get_var"));
5633
5634 /* It is useles to retrieve the stream, but this function
5635 * runs only in a stream context.
5636 */
5637 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5638 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5639
Willy Tarreau7560dd42016-03-10 16:28:58 +01005640 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005641 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005642 lua_pushnil(L);
5643 return 1;
5644 }
5645
5646 return hlua_smp2lua(L, &smp);
5647}
5648
Willy Tarreau59551662015-03-10 14:23:13 +01005649__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005650{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005651 struct hlua *hlua;
5652
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005653 MAY_LJMP(check_args(L, 2, "set_priv"));
5654
Willy Tarreau87b09662015-04-03 00:22:06 +02005655 /* It is useles to retrieve the stream, but this function
5656 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005657 */
5658 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005659
5660 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005661 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005662 if (!hlua)
5663 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005664
5665 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005666 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005667
5668 /* Get and store new value. */
5669 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5670 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5671
5672 return 0;
5673}
5674
Willy Tarreau59551662015-03-10 14:23:13 +01005675__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005676{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005677 struct hlua *hlua;
5678
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005679 MAY_LJMP(check_args(L, 1, "get_priv"));
5680
Willy Tarreau87b09662015-04-03 00:22:06 +02005681 /* It is useles to retrieve the stream, but this function
5682 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005683 */
5684 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005685
5686 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005687 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005688 if (!hlua) {
5689 lua_pushnil(L);
5690 return 1;
5691 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005692
5693 /* Push configuration index in the stack. */
5694 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5695
5696 return 1;
5697}
5698
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005699/* Create stack entry containing a class TXN. This function
5700 * return 0 if the stack does not contains free slots,
5701 * otherwise it returns 1.
5702 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005703static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005704{
Willy Tarreaude491382015-04-06 11:04:28 +02005705 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005706
5707 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005708 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005709 return 0;
5710
5711 /* NOTE: The allocation never fails. The failure
5712 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005713 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005714 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005715 /* Create the object: obj[0] = userdata. */
5716 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005717 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005718 lua_rawseti(L, -2, 0);
5719
Willy Tarreaude491382015-04-06 11:04:28 +02005720 htxn->s = s;
5721 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005722 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005723 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005724
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005725 /* Create the "f" field that contains a list of fetches. */
5726 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005727 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005728 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005729 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005730
5731 /* Create the "sf" field that contains a list of stringsafe fetches. */
5732 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005733 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005734 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005735 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005736
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005737 /* Create the "c" field that contains a list of converters. */
5738 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005739 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005740 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005741 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005742
5743 /* Create the "sc" field that contains a list of stringsafe converters. */
5744 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005745 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005746 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005747 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005748
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005749 /* Create the "req" field that contains the request channel object. */
5750 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005751 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005752 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005753 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005754
5755 /* Create the "res" field that contains the response channel object. */
5756 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005757 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005758 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005759 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005760
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005761 /* Creates the HTTP object is the current proxy allows http. */
5762 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005763 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005764 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005765 return 0;
5766 }
5767 else
5768 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005769 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005770
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005771 /* Pop a class sesison metatable and affect it to the userdata. */
5772 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5773 lua_setmetatable(L, -2);
5774
5775 return 1;
5776}
5777
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005778__LJMP static int hlua_txn_deflog(lua_State *L)
5779{
5780 const char *msg;
5781 struct hlua_txn *htxn;
5782
5783 MAY_LJMP(check_args(L, 2, "deflog"));
5784 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5785 msg = MAY_LJMP(luaL_checkstring(L, 2));
5786
5787 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5788 return 0;
5789}
5790
5791__LJMP static int hlua_txn_log(lua_State *L)
5792{
5793 int level;
5794 const char *msg;
5795 struct hlua_txn *htxn;
5796
5797 MAY_LJMP(check_args(L, 3, "log"));
5798 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5799 level = MAY_LJMP(luaL_checkinteger(L, 2));
5800 msg = MAY_LJMP(luaL_checkstring(L, 3));
5801
5802 if (level < 0 || level >= NB_LOG_LEVELS)
5803 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5804
5805 hlua_sendlog(htxn->s->be, level, msg);
5806 return 0;
5807}
5808
5809__LJMP static int hlua_txn_log_debug(lua_State *L)
5810{
5811 const char *msg;
5812 struct hlua_txn *htxn;
5813
5814 MAY_LJMP(check_args(L, 2, "Debug"));
5815 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5816 msg = MAY_LJMP(luaL_checkstring(L, 2));
5817 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5818 return 0;
5819}
5820
5821__LJMP static int hlua_txn_log_info(lua_State *L)
5822{
5823 const char *msg;
5824 struct hlua_txn *htxn;
5825
5826 MAY_LJMP(check_args(L, 2, "Info"));
5827 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5828 msg = MAY_LJMP(luaL_checkstring(L, 2));
5829 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5830 return 0;
5831}
5832
5833__LJMP static int hlua_txn_log_warning(lua_State *L)
5834{
5835 const char *msg;
5836 struct hlua_txn *htxn;
5837
5838 MAY_LJMP(check_args(L, 2, "Warning"));
5839 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5840 msg = MAY_LJMP(luaL_checkstring(L, 2));
5841 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5842 return 0;
5843}
5844
5845__LJMP static int hlua_txn_log_alert(lua_State *L)
5846{
5847 const char *msg;
5848 struct hlua_txn *htxn;
5849
5850 MAY_LJMP(check_args(L, 2, "Alert"));
5851 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5852 msg = MAY_LJMP(luaL_checkstring(L, 2));
5853 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5854 return 0;
5855}
5856
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005857__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5858{
5859 struct hlua_txn *htxn;
5860 int ll;
5861
5862 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5863 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5864 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5865
5866 if (ll < 0 || ll > 7)
5867 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5868
5869 htxn->s->logs.level = ll;
5870 return 0;
5871}
5872
5873__LJMP static int hlua_txn_set_tos(lua_State *L)
5874{
5875 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005876 int tos;
5877
5878 MAY_LJMP(check_args(L, 2, "set_tos"));
5879 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5880 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5881
Willy Tarreau1a18b542018-12-11 16:37:42 +01005882 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005883 return 0;
5884}
5885
5886__LJMP static int hlua_txn_set_mark(lua_State *L)
5887{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005888 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005889 int mark;
5890
5891 MAY_LJMP(check_args(L, 2, "set_mark"));
5892 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5893 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5894
Lukas Tribus579e3e32019-08-11 18:03:45 +02005895 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005896 return 0;
5897}
5898
Patrick Hemmer268a7072018-05-11 12:52:31 -04005899__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5900{
5901 struct hlua_txn *htxn;
5902
5903 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5904 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5905 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5906 return 0;
5907}
5908
5909__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5910{
5911 struct hlua_txn *htxn;
5912
5913 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5914 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5915 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5916 return 0;
5917}
5918
Christopher Faulet700d9e82020-01-31 12:21:52 +01005919/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005920 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005921 * message and terminate the transaction. It returns 1 on success and 0 on
5922 * error. The Reply must be on top of the stack.
5923 */
5924__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5925{
5926 struct htx *htx;
5927 struct htx_sl *sl;
5928 struct h1m h1m;
5929 const char *status, *reason, *body;
5930 size_t status_len, reason_len, body_len;
5931 int ret, code, flags;
5932
5933 code = 200;
5934 status = "200";
5935 status_len = 3;
5936 ret = lua_getfield(L, -1, "status");
5937 if (ret == LUA_TNUMBER) {
5938 code = lua_tointeger(L, -1);
5939 status = lua_tolstring(L, -1, &status_len);
5940 }
5941 lua_pop(L, 1);
5942
5943 reason = http_get_reason(code);
5944 reason_len = strlen(reason);
5945 ret = lua_getfield(L, -1, "reason");
5946 if (ret == LUA_TSTRING)
5947 reason = lua_tolstring(L, -1, &reason_len);
5948 lua_pop(L, 1);
5949
5950 body = NULL;
5951 body_len = 0;
5952 ret = lua_getfield(L, -1, "body");
5953 if (ret == LUA_TSTRING)
5954 body = lua_tolstring(L, -1, &body_len);
5955 lua_pop(L, 1);
5956
5957 /* Prepare the response before inserting the headers */
5958 h1m_init_res(&h1m);
5959 htx = htx_from_buf(&s->res.buf);
5960 channel_htx_truncate(&s->res, htx);
5961 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5962 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5963 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5964 ist2(status, status_len), ist2(reason, reason_len));
5965 }
5966 else {
5967 flags = HTX_SL_F_IS_RESP;
5968 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5969 ist2(status, status_len), ist2(reason, reason_len));
5970 }
5971 if (!sl)
5972 goto fail;
5973 sl->info.res.status = code;
5974
5975 /* Push in the stack the "headers" entry. */
5976 ret = lua_getfield(L, -1, "headers");
5977 if (ret != LUA_TTABLE)
5978 goto skip_headers;
5979
5980 lua_pushnil(L);
5981 while (lua_next(L, -2) != 0) {
5982 struct ist name, value;
5983 const char *n, *v;
5984 size_t nlen, vlen;
5985
5986 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5987 /* Skip element if the key is not a string or if the value is not a table */
5988 goto next_hdr;
5989 }
5990
5991 n = lua_tolstring(L, -2, &nlen);
5992 name = ist2(n, nlen);
5993 if (isteqi(name, ist("content-length"))) {
5994 /* Always skip content-length header. It will be added
5995 * later with the correct len
5996 */
5997 goto next_hdr;
5998 }
5999
6000 /* Loop on header's values */
6001 lua_pushnil(L);
6002 while (lua_next(L, -2)) {
6003 if (!lua_isstring(L, -1)) {
6004 /* Skip the value if it is not a string */
6005 goto next_value;
6006 }
6007
6008 v = lua_tolstring(L, -1, &vlen);
6009 value = ist2(v, vlen);
6010
6011 if (isteqi(name, ist("transfer-encoding")))
6012 h1_parse_xfer_enc_header(&h1m, value);
6013 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
6014 goto fail;
6015
6016 next_value:
6017 lua_pop(L, 1);
6018 }
6019
6020 next_hdr:
6021 lua_pop(L, 1);
6022 }
6023 skip_headers:
6024 lua_pop(L, 1);
6025
6026 /* Update h1m flags: CLEN is set if CHNK is not present */
6027 if (!(h1m.flags & H1_MF_CHNK)) {
6028 const char *clen = ultoa(body_len);
6029
6030 h1m.flags |= H1_MF_CLEN;
6031 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
6032 goto fail;
6033 }
6034 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
6035 h1m.flags |= H1_MF_XFER_LEN;
6036
6037 /* Update HTX start-line flags */
6038 if (h1m.flags & H1_MF_XFER_ENC)
6039 flags |= HTX_SL_F_XFER_ENC;
6040 if (h1m.flags & H1_MF_XFER_LEN) {
6041 flags |= HTX_SL_F_XFER_LEN;
6042 if (h1m.flags & H1_MF_CHNK)
6043 flags |= HTX_SL_F_CHNK;
6044 else if (h1m.flags & H1_MF_CLEN)
6045 flags |= HTX_SL_F_CLEN;
6046 if (h1m.body_len == 0)
6047 flags |= HTX_SL_F_BODYLESS;
6048 }
6049 sl->flags |= flags;
6050
6051
6052 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006053 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01006054 goto fail;
6055
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006056 htx->flags |= HTX_FL_EOM;
6057
Christopher Faulet700d9e82020-01-31 12:21:52 +01006058 /* Now, forward the response and terminate the transaction */
6059 s->txn->status = code;
6060 htx_to_buf(htx, &s->res.buf);
6061 if (!http_forward_proxy_resp(s, 1))
6062 goto fail;
6063
6064 return 1;
6065
6066 fail:
6067 channel_htx_truncate(&s->res, htx);
6068 return 0;
6069}
6070
6071/* Terminate a transaction if called from a lua action. For TCP streams,
6072 * processing is just aborted. Nothing is returned to the client and all
6073 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
6074 * is forwarded to the client before terminating the transaction. On success,
6075 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
6076 * with ACT_RET_ERR code. If this function is not called from a lua action, it
6077 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006078 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006079__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006080{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006081 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01006082 struct stream *s;
6083 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006084
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006085 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006086
Christopher Faulet700d9e82020-01-31 12:21:52 +01006087 /* If the flags NOTERM is set, we cannot terminate the session, so we
6088 * just end the execution of the current lua code. */
6089 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006090 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006091
Christopher Faulet700d9e82020-01-31 12:21:52 +01006092 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006093 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01006094 struct channel *req = &s->req;
6095 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006096
Christopher Faulet700d9e82020-01-31 12:21:52 +01006097 channel_auto_read(req);
6098 channel_abort(req);
6099 channel_auto_close(req);
6100 channel_erase(req);
6101
6102 res->wex = tick_add_ifset(now_ms, res->wto);
6103 channel_auto_read(res);
6104 channel_auto_close(res);
6105 channel_shutr_now(res);
6106
6107 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
6108 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02006109 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006110
Christopher Faulet700d9e82020-01-31 12:21:52 +01006111 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
6112 /* No reply or invalid reply */
6113 s->txn->status = 0;
6114 http_reply_and_close(s, 0, NULL);
6115 }
6116 else {
6117 /* Remove extra args to have the reply on top of the stack */
6118 if (lua_gettop(L) > 2)
6119 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006120
Christopher Faulet700d9e82020-01-31 12:21:52 +01006121 if (!hlua_txn_forward_reply(L, s)) {
6122 if (!(s->flags & SF_ERR_MASK))
6123 s->flags |= SF_ERR_PRXCOND;
6124 lua_pushinteger(L, ACT_RET_ERR);
6125 WILL_LJMP(hlua_done(L));
6126 return 0; /* Never reached */
6127 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02006128 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006129
Christopher Faulet700d9e82020-01-31 12:21:52 +01006130 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
6131 if (htxn->dir == SMP_OPT_DIR_REQ) {
6132 /* let's log the request time */
6133 s->logs.tv_request = now;
6134 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02006135 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01006136 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02006137
Christopher Faulet700d9e82020-01-31 12:21:52 +01006138 done:
6139 if (!(s->flags & SF_ERR_MASK))
6140 s->flags |= SF_ERR_LOCAL;
6141 if (!(s->flags & SF_FINST_MASK))
6142 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02006143
Christopher Faulet4ad73102020-03-05 11:07:31 +01006144 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006145 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006146 return 0;
6147}
6148
Christopher Faulet700d9e82020-01-31 12:21:52 +01006149/*
6150 *
6151 *
6152 * Class REPLY
6153 *
6154 *
6155 */
6156
6157/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
6158 * free slots, the function fails and returns 0;
6159 */
6160static int hlua_txn_reply_new(lua_State *L)
6161{
6162 struct hlua_txn *htxn;
6163 const char *reason, *body = NULL;
6164 int ret, status;
6165
6166 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006167 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01006168 hlua_pusherror(L, "txn object is not an HTTP transaction.");
6169 WILL_LJMP(lua_error(L));
6170 }
6171
6172 /* Default value */
6173 status = 200;
6174 reason = http_get_reason(status);
6175
6176 if (lua_istable(L, 2)) {
6177 /* load status and reason from the table argument at index 2 */
6178 ret = lua_getfield(L, 2, "status");
6179 if (ret == LUA_TNIL)
6180 goto reason;
6181 else if (ret != LUA_TNUMBER) {
6182 /* invalid status: ignore the reason */
6183 goto body;
6184 }
6185 status = lua_tointeger(L, -1);
6186
6187 reason:
6188 lua_pop(L, 1); /* restore the stack: remove status */
6189 ret = lua_getfield(L, 2, "reason");
6190 if (ret == LUA_TSTRING)
6191 reason = lua_tostring(L, -1);
6192
6193 body:
6194 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
6195 ret = lua_getfield(L, 2, "body");
6196 if (ret == LUA_TSTRING)
6197 body = lua_tostring(L, -1);
6198 lua_pop(L, 1); /* restore the stack: remove body */
6199 }
6200
6201 /* Create the Reply table */
6202 lua_newtable(L);
6203
6204 /* Add status element */
6205 lua_pushstring(L, "status");
6206 lua_pushinteger(L, status);
6207 lua_settable(L, -3);
6208
6209 /* Add reason element */
6210 reason = http_get_reason(status);
6211 lua_pushstring(L, "reason");
6212 lua_pushstring(L, reason);
6213 lua_settable(L, -3);
6214
6215 /* Add body element, nil if undefined */
6216 lua_pushstring(L, "body");
6217 if (body)
6218 lua_pushstring(L, body);
6219 else
6220 lua_pushnil(L);
6221 lua_settable(L, -3);
6222
6223 /* Add headers element */
6224 lua_pushstring(L, "headers");
6225 lua_newtable(L);
6226
6227 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6228 if (lua_istable(L, 2)) {
6229 /* load headers from the table argument at index 2. If it is a table, copy it. */
6230 ret = lua_getfield(L, 2, "headers");
6231 if (ret == LUA_TTABLE) {
6232 /* stack: [ ... <headers:table>, <table> ] */
6233 lua_pushnil(L);
6234 while (lua_next(L, -2) != 0) {
6235 /* stack: [ ... <headers:table>, <table>, k, v] */
6236 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
6237 /* invalid value type, skip it */
6238 lua_pop(L, 1);
6239 continue;
6240 }
6241
6242
6243 /* Duplicate the key and swap it with the value. */
6244 lua_pushvalue(L, -2);
6245 lua_insert(L, -2);
6246 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6247
6248 lua_newtable(L);
6249 lua_insert(L, -2);
6250 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6251
6252 if (lua_isstring(L, -1)) {
6253 /* push the value in the inner table */
6254 lua_rawseti(L, -2, 1);
6255 }
6256 else { /* table */
6257 lua_pushnil(L);
6258 while (lua_next(L, -2) != 0) {
6259 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6260 if (!lua_isstring(L, -1)) {
6261 /* invalid value type, skip it*/
6262 lua_pop(L, 1);
6263 continue;
6264 }
6265 /* push the value in the inner table */
6266 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6267 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6268 }
6269 lua_pop(L, 1);
6270 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6271 }
6272
6273 /* push (k,v) on the stack in the headers table:
6274 * stack: [ ... <headers:table>, <table>, k, k, v ]
6275 */
6276 lua_settable(L, -5);
6277 /* stack: [ ... <headers:table>, <table>, k ] */
6278 }
6279 }
6280 lua_pop(L, 1);
6281 }
6282 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6283 lua_settable(L, -3);
6284 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6285
6286 /* Pop a class sesison metatable and affect it to the userdata. */
6287 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6288 lua_setmetatable(L, -2);
6289 return 1;
6290}
6291
6292/* Set the reply status code, and optionally the reason. If no reason is
6293 * provided, the default one corresponding to the status code is used.
6294 */
6295__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6296{
6297 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6298 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6299
6300 /* First argument (self) must be a table */
6301 luaL_checktype(L, 1, LUA_TTABLE);
6302
6303 if (status < 100 || status > 599) {
6304 lua_pushboolean(L, 0);
6305 return 1;
6306 }
6307 if (!reason)
6308 reason = http_get_reason(status);
6309
6310 lua_pushinteger(L, status);
6311 lua_setfield(L, 1, "status");
6312
6313 lua_pushstring(L, reason);
6314 lua_setfield(L, 1, "reason");
6315
6316 lua_pushboolean(L, 1);
6317 return 1;
6318}
6319
6320/* Add a header into the reply object. Each header name is associated to an
6321 * array of values in the "headers" table. If the header name is not found, a
6322 * new entry is created.
6323 */
6324__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6325{
6326 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6327 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6328 int ret;
6329
6330 /* First argument (self) must be a table */
6331 luaL_checktype(L, 1, LUA_TTABLE);
6332
6333 /* Push in the stack the "headers" entry. */
6334 ret = lua_getfield(L, 1, "headers");
6335 if (ret != LUA_TTABLE) {
6336 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6337 WILL_LJMP(lua_error(L));
6338 }
6339
6340 /* check if the header is already registered. If not, register it. */
6341 ret = lua_getfield(L, -1, name);
6342 if (ret == LUA_TNIL) {
6343 /* Entry not found. */
6344 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6345
6346 /* Insert the new header name in the array in the top of the stack.
6347 * It left the new array in the top of the stack.
6348 */
6349 lua_newtable(L);
6350 lua_pushstring(L, name);
6351 lua_pushvalue(L, -2);
6352 lua_settable(L, -4);
6353 }
6354 else if (ret != LUA_TTABLE) {
6355 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6356 WILL_LJMP(lua_error(L));
6357 }
6358
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006359 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006360 * the header value as new entry.
6361 */
6362 lua_pushstring(L, value);
6363 ret = lua_rawlen(L, -2);
6364 lua_rawseti(L, -2, ret + 1);
6365
6366 lua_pushboolean(L, 1);
6367 return 1;
6368}
6369
6370/* Remove all occurrences of a given header name. */
6371__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6372{
6373 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6374 int ret;
6375
6376 /* First argument (self) must be a table */
6377 luaL_checktype(L, 1, LUA_TTABLE);
6378
6379 /* Push in the stack the "headers" entry. */
6380 ret = lua_getfield(L, 1, "headers");
6381 if (ret != LUA_TTABLE) {
6382 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6383 WILL_LJMP(lua_error(L));
6384 }
6385
6386 lua_pushstring(L, name);
6387 lua_pushnil(L);
6388 lua_settable(L, -3);
6389
6390 lua_pushboolean(L, 1);
6391 return 1;
6392}
6393
6394/* Set the reply's body. Overwrite any existing entry. */
6395__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6396{
6397 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6398
6399 /* First argument (self) must be a table */
6400 luaL_checktype(L, 1, LUA_TTABLE);
6401
6402 lua_pushstring(L, payload);
6403 lua_setfield(L, 1, "body");
6404
6405 lua_pushboolean(L, 1);
6406 return 1;
6407}
6408
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006409__LJMP static int hlua_log(lua_State *L)
6410{
6411 int level;
6412 const char *msg;
6413
6414 MAY_LJMP(check_args(L, 2, "log"));
6415 level = MAY_LJMP(luaL_checkinteger(L, 1));
6416 msg = MAY_LJMP(luaL_checkstring(L, 2));
6417
6418 if (level < 0 || level >= NB_LOG_LEVELS)
6419 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6420
6421 hlua_sendlog(NULL, level, msg);
6422 return 0;
6423}
6424
6425__LJMP static int hlua_log_debug(lua_State *L)
6426{
6427 const char *msg;
6428
6429 MAY_LJMP(check_args(L, 1, "debug"));
6430 msg = MAY_LJMP(luaL_checkstring(L, 1));
6431 hlua_sendlog(NULL, LOG_DEBUG, msg);
6432 return 0;
6433}
6434
6435__LJMP static int hlua_log_info(lua_State *L)
6436{
6437 const char *msg;
6438
6439 MAY_LJMP(check_args(L, 1, "info"));
6440 msg = MAY_LJMP(luaL_checkstring(L, 1));
6441 hlua_sendlog(NULL, LOG_INFO, msg);
6442 return 0;
6443}
6444
6445__LJMP static int hlua_log_warning(lua_State *L)
6446{
6447 const char *msg;
6448
6449 MAY_LJMP(check_args(L, 1, "warning"));
6450 msg = MAY_LJMP(luaL_checkstring(L, 1));
6451 hlua_sendlog(NULL, LOG_WARNING, msg);
6452 return 0;
6453}
6454
6455__LJMP static int hlua_log_alert(lua_State *L)
6456{
6457 const char *msg;
6458
6459 MAY_LJMP(check_args(L, 1, "alert"));
6460 msg = MAY_LJMP(luaL_checkstring(L, 1));
6461 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006462 return 0;
6463}
6464
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006465__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006466{
6467 int wakeup_ms = lua_tointeger(L, -1);
6468 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006469 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006470 return 0;
6471}
6472
6473__LJMP static int hlua_sleep(lua_State *L)
6474{
6475 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006476 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006477
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006478 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006479
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006480 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006481 wakeup_ms = tick_add(now_ms, delay);
6482 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006483
Willy Tarreau9635e032018-10-16 17:52:55 +02006484 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006485 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006486}
6487
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006488__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006489{
6490 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006491 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006492
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006493 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006494
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006495 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006496 wakeup_ms = tick_add(now_ms, delay);
6497 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006498
Willy Tarreau9635e032018-10-16 17:52:55 +02006499 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006500 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006501}
6502
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006503/* This functionis an LUA binding. it permits to give back
6504 * the hand at the HAProxy scheduler. It is used when the
6505 * LUA processing consumes a lot of time.
6506 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006507__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006508{
6509 return 0;
6510}
6511
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006512__LJMP static int hlua_yield(lua_State *L)
6513{
Willy Tarreau9635e032018-10-16 17:52:55 +02006514 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006515 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006516}
6517
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006518/* This function change the nice of the currently executed
6519 * task. It is used set low or high priority at the current
6520 * task.
6521 */
Willy Tarreau59551662015-03-10 14:23:13 +01006522__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006523{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006524 struct hlua *hlua;
6525 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006526
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006527 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006528 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006529
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006530 /* Get hlua struct, or NULL if we execute from main lua state */
6531 hlua = hlua_gethlua(L);
6532
6533 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006534 if (!hlua || !hlua->task)
6535 return 0;
6536
6537 if (nice < -1024)
6538 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006539 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006540 nice = 1024;
6541
6542 hlua->task->nice = nice;
6543 return 0;
6544}
6545
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006546/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006547 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6548 * return an E_AGAIN signal, the emmiter of this signal must set a
6549 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006550 *
6551 * Task wrapper are longjmp safe because the only one Lua code
6552 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006553 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006554struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006555{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006556 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006557 enum hlua_exec status;
6558
Christopher Faulet5bc99722018-04-25 10:34:45 +02006559 if (task->thread_mask == MAX_THREADS_MASK)
6560 task_set_affinity(task, tid_bit);
6561
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006562 /* If it is the first call to the task, we must initialize the
6563 * execution timeouts.
6564 */
6565 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006566 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006567
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006568 /* Execute the Lua code. */
6569 status = hlua_ctx_resume(hlua, 1);
6570
6571 switch (status) {
6572 /* finished or yield */
6573 case HLUA_E_OK:
6574 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006575 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006576 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006577 break;
6578
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006579 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006580 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006581 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006582 break;
6583
6584 /* finished with error. */
6585 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006586 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006587 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006588 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006589 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006590 break;
6591
6592 case HLUA_E_ERR:
6593 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006594 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006595 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006596 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006597 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006598 break;
6599 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006600 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006601}
6602
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006603/* This function is an LUA binding that register LUA function to be
6604 * executed after the HAProxy configuration parsing and before the
6605 * HAProxy scheduler starts. This function expect only one LUA
6606 * argument that is a function. This function returns nothing, but
6607 * throws if an error is encountered.
6608 */
6609__LJMP static int hlua_register_init(lua_State *L)
6610{
6611 struct hlua_init_function *init;
6612 int ref;
6613
6614 MAY_LJMP(check_args(L, 1, "register_init"));
6615
6616 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6617
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006618 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006619 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006620 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006621
6622 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006623 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006624 return 0;
6625}
6626
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006627/* This functio is an LUA binding. It permits to register a task
6628 * executed in parallel of the main HAroxy activity. The task is
6629 * created and it is set in the HAProxy scheduler. It can be called
6630 * from the "init" section, "post init" or during the runtime.
6631 *
6632 * Lua prototype:
6633 *
6634 * <none> core.register_task(<function>)
6635 */
6636static int hlua_register_task(lua_State *L)
6637{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006638 struct hlua *hlua = NULL;
6639 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006640 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006641 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006642
6643 MAY_LJMP(check_args(L, 1, "register_task"));
6644
6645 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6646
Thierry Fournier75fc0292020-11-28 13:18:56 +01006647 /* Get the reference state. If the reference is NULL, L is the master
6648 * state, otherwise hlua->T is.
6649 */
6650 hlua = hlua_gethlua(L);
6651 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006652 /* we are in runtime processing */
6653 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006654 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006655 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006656 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006657
Willy Tarreaubafbe012017-11-24 17:34:44 +01006658 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006659 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006660 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006661 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006662
Thierry Fournier59f11be2020-11-29 00:37:41 +01006663 /* We are in the common lua state, execute the task anywhere,
6664 * otherwise, inherit the current thread identifier
6665 */
6666 if (state_id == 0)
6667 task = task_new(MAX_THREADS_MASK);
6668 else
6669 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006670 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006671 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006672
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006673 task->context = hlua;
6674 task->process = hlua_process_task;
6675
Thierry Fournier021d9862020-11-28 23:42:03 +01006676 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006677 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006678
6679 /* Restore the function in the stack. */
6680 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6681 hlua->nargs = 0;
6682
6683 /* Schedule task. */
6684 task_schedule(task, now_ms);
6685
6686 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006687
6688 alloc_error:
6689 task_destroy(task);
6690 hlua_ctx_destroy(hlua);
6691 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6692 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006693}
6694
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006695/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6696 * doesn't allow "yield" functions because the HAProxy engine cannot
6697 * resume converters.
6698 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006699static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006700{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006701 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006702 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006703 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006704
Willy Tarreaube508f12016-03-10 11:47:01 +01006705 if (!stream)
6706 return 0;
6707
Willy Tarreau87b09662015-04-03 00:22:06 +02006708 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006709 * Lua context can be not initialized. This behavior
6710 * permits to save performances because a systematic
6711 * Lua initialization cause 5% performances loss.
6712 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006713 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006714 struct hlua *hlua;
6715
6716 hlua = pool_alloc(pool_head_hlua);
6717 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006718 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6719 return 0;
6720 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006721 HLUA_INIT(hlua);
6722 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006723 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006724 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6725 return 0;
6726 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006727 }
6728
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006729 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006730 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006731
6732 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006733 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006734 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6735 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006736 else
6737 error = "critical error";
6738 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006739 return 0;
6740 }
6741
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006742 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006743 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006744 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006745 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006746 return 0;
6747 }
6748
6749 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006750 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006751
6752 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006753 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006754 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006755 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006756 return 0;
6757 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006758 hlua_smp2lua(stream->hlua->T, smp);
6759 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006760
6761 /* push keywords in the stack. */
6762 if (arg_p) {
6763 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006764 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006765 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006766 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006767 return 0;
6768 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006769 hlua_arg2lua(stream->hlua->T, arg_p);
6770 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006771 }
6772 }
6773
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006774 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006775 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006776
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006777 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006778 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006779 }
6780
6781 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006782 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006783 /* finished. */
6784 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006785 /* If the stack is empty, the function fails. */
6786 if (lua_gettop(stream->hlua->T) <= 0)
6787 return 0;
6788
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006789 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006790 hlua_lua2smp(stream->hlua->T, -1, smp);
6791 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006792 return 1;
6793
6794 /* yield. */
6795 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006796 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006797 return 0;
6798
6799 /* finished with error. */
6800 case HLUA_E_ERRMSG:
6801 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006802 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006803 fcn->name, lua_tostring(stream->hlua->T, -1));
6804 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006805 return 0;
6806
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006807 case HLUA_E_ETMOUT:
6808 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6809 return 0;
6810
6811 case HLUA_E_NOMEM:
6812 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6813 return 0;
6814
6815 case HLUA_E_YIELD:
6816 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6817 return 0;
6818
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006819 case HLUA_E_ERR:
6820 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006821 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006822 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006823
6824 default:
6825 return 0;
6826 }
6827}
6828
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006829/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6830 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006831 * resume sample-fetches. This function will be called by the sample
6832 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006833 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006834static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6835 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006836{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006837 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006838 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006839 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006840 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006841
Willy Tarreaube508f12016-03-10 11:47:01 +01006842 if (!stream)
6843 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006844
Willy Tarreau87b09662015-04-03 00:22:06 +02006845 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006846 * Lua context can be not initialized. This behavior
6847 * permits to save performances because a systematic
6848 * Lua initialization cause 5% performances loss.
6849 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006850 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006851 struct hlua *hlua;
6852
6853 hlua = pool_alloc(pool_head_hlua);
6854 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006855 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6856 return 0;
6857 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006858 hlua->T = NULL;
6859 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006860 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006861 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6862 return 0;
6863 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006864 }
6865
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006866 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006867 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006868
6869 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006870 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006871 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6872 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006873 else
6874 error = "critical error";
6875 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006876 return 0;
6877 }
6878
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006879 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006880 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006881 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006882 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006883 return 0;
6884 }
6885
6886 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006887 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006888
6889 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006890 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006891 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006892 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006893 return 0;
6894 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006895 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006896
6897 /* push keywords in the stack. */
6898 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6899 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006900 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006901 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006902 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006903 return 0;
6904 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006905 hlua_arg2lua(stream->hlua->T, arg_p);
6906 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006907 }
6908
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006909 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006910 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006911
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006912 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006913 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006914 }
6915
6916 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006917 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006918 /* finished. */
6919 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006920 /* If the stack is empty, the function fails. */
6921 if (lua_gettop(stream->hlua->T) <= 0)
6922 return 0;
6923
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006924 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006925 hlua_lua2smp(stream->hlua->T, -1, smp);
6926 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006927
6928 /* Set the end of execution flag. */
6929 smp->flags &= ~SMP_F_MAY_CHANGE;
6930 return 1;
6931
6932 /* yield. */
6933 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006934 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006935 return 0;
6936
6937 /* finished with error. */
6938 case HLUA_E_ERRMSG:
6939 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006940 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006941 fcn->name, lua_tostring(stream->hlua->T, -1));
6942 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006943 return 0;
6944
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006945 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006946 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6947 return 0;
6948
6949 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006950 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6951 return 0;
6952
6953 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006954 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6955 return 0;
6956
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006957 case HLUA_E_ERR:
6958 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006959 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006960 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006961
6962 default:
6963 return 0;
6964 }
6965}
6966
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006967/* This function is an LUA binding used for registering
6968 * "sample-conv" functions. It expects a converter name used
6969 * in the haproxy configuration file, and an LUA function.
6970 */
6971__LJMP static int hlua_register_converters(lua_State *L)
6972{
6973 struct sample_conv_kw_list *sck;
6974 const char *name;
6975 int ref;
6976 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02006977 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006978 struct sample_conv *sc;
6979 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006980
6981 MAY_LJMP(check_args(L, 2, "register_converters"));
6982
6983 /* First argument : converter name. */
6984 name = MAY_LJMP(luaL_checkstring(L, 1));
6985
6986 /* Second argument : lua function. */
6987 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6988
Thierry Fournierf67442e2020-11-28 20:41:07 +01006989 /* Check if the converter is already registered */
6990 trash = get_trash_chunk();
6991 chunk_printf(trash, "lua.%s", name);
6992 sc = find_sample_conv(trash->area, trash->data);
6993 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006994 fcn = sc->private;
6995 if (fcn->function_ref[hlua_state_id] != -1) {
6996 ha_warning("Trying to register converter 'lua.%s' more than once. "
6997 "This will become a hard error in version 2.5.\n", name);
6998 }
6999 fcn->function_ref[hlua_state_id] = ref;
7000 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007001 }
7002
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007003 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007004 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007005 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02007006 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007007 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007008 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02007009 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007010
7011 /* Fill fcn. */
7012 fcn->name = strdup(name);
7013 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02007014 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007015 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007016
7017 /* List head */
7018 sck->list.n = sck->list.p = NULL;
7019
7020 /* converter keyword. */
7021 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007022 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007023 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02007024 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007025
7026 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
7027 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01007028 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 +01007029 sck->kw[0].val_args = NULL;
7030 sck->kw[0].in_type = SMP_T_STR;
7031 sck->kw[0].out_type = SMP_T_STR;
7032 sck->kw[0].private = fcn;
7033
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007034 /* Register this new converter */
7035 sample_register_convs(sck);
7036
7037 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02007038
7039 alloc_error:
7040 release_hlua_function(fcn);
7041 ha_free(&sck);
7042 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7043 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007044}
7045
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007046/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007047 * "sample-fetch" functions. It expects a converter name used
7048 * in the haproxy configuration file, and an LUA function.
7049 */
7050__LJMP static int hlua_register_fetches(lua_State *L)
7051{
7052 const char *name;
7053 int ref;
7054 int len;
7055 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02007056 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007057 struct sample_fetch *sf;
7058 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007059
7060 MAY_LJMP(check_args(L, 2, "register_fetches"));
7061
7062 /* First argument : sample-fetch name. */
7063 name = MAY_LJMP(luaL_checkstring(L, 1));
7064
7065 /* Second argument : lua function. */
7066 ref = MAY_LJMP(hlua_checkfunction(L, 2));
7067
Thierry Fournierf67442e2020-11-28 20:41:07 +01007068 /* Check if the sample-fetch is already registered */
7069 trash = get_trash_chunk();
7070 chunk_printf(trash, "lua.%s", name);
7071 sf = find_sample_fetch(trash->area, trash->data);
7072 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007073 fcn = sf->private;
7074 if (fcn->function_ref[hlua_state_id] != -1) {
7075 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
7076 "This will become a hard error in version 2.5.\n", name);
7077 }
7078 fcn->function_ref[hlua_state_id] = ref;
7079 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007080 }
7081
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007082 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007083 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007084 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02007085 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007086 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007087 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02007088 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007089
7090 /* Fill fcn. */
7091 fcn->name = strdup(name);
7092 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02007093 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007094 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007095
7096 /* List head */
7097 sfk->list.n = sfk->list.p = NULL;
7098
7099 /* sample-fetch keyword. */
7100 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007101 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007102 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02007103 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007104
7105 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
7106 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01007107 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 +01007108 sfk->kw[0].val_args = NULL;
7109 sfk->kw[0].out_type = SMP_T_STR;
7110 sfk->kw[0].use = SMP_USE_HTTP_ANY;
7111 sfk->kw[0].val = 0;
7112 sfk->kw[0].private = fcn;
7113
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007114 /* Register this new fetch. */
7115 sample_register_fetches(sfk);
7116
7117 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02007118
7119 alloc_error:
7120 release_hlua_function(fcn);
7121 ha_free(&sfk);
7122 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7123 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007124}
7125
Christopher Faulet501465d2020-02-26 14:54:16 +01007126/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007127 */
Christopher Faulet501465d2020-02-26 14:54:16 +01007128__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007129{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007130 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007131 unsigned int delay;
7132 unsigned int wakeup_ms;
7133
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007134 /* Get hlua struct, or NULL if we execute from main lua state */
7135 hlua = hlua_gethlua(L);
7136 if (!hlua) {
7137 return 0;
7138 }
7139
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007140 MAY_LJMP(check_args(L, 1, "wake_time"));
7141
7142 delay = MAY_LJMP(luaL_checkinteger(L, 1));
7143 wakeup_ms = tick_add(now_ms, delay);
7144 hlua->wake_time = wakeup_ms;
7145 return 0;
7146}
7147
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007148/* This function is a wrapper to execute each LUA function declared as an action
7149 * wrapper during the initialisation period. This function may return any
7150 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
7151 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
7152 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007153 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007154static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02007155 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007156{
7157 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007158 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007159 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007160 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007161
7162 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01007163 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
7164 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
7165 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
7166 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007167 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007168 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007169 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007170 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007171
Willy Tarreau87b09662015-04-03 00:22:06 +02007172 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007173 * Lua context can be not initialized. This behavior
7174 * permits to save performances because a systematic
7175 * Lua initialization cause 5% performances loss.
7176 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007177 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007178 struct hlua *hlua;
7179
7180 hlua = pool_alloc(pool_head_hlua);
7181 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007182 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007183 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007184 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007185 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007186 HLUA_INIT(hlua);
7187 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01007188 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 +01007189 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007190 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007191 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007192 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007193 }
7194
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007195 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007196 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007197
7198 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007199 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007200 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
7201 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01007202 else
7203 error = "critical error";
7204 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007205 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007206 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007207 }
7208
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007209 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007210 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007211 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007212 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007213 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007214 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007215 }
7216
7217 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007218 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 +01007219
Willy Tarreau87b09662015-04-03 00:22:06 +02007220 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007221 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007222 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007223 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007224 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007225 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007226 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007227 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007228
7229 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007230 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007231 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007232 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007233 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007234 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007235 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007236 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007237 lua_pushstring(s->hlua->T, *arg);
7238 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007239 }
7240
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007241 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007242 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007243
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007244 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007245 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007246 }
7247
7248 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01007249 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007250 /* finished. */
7251 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007252 /* Catch the return value */
7253 if (lua_gettop(s->hlua->T) > 0)
7254 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007255
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007256 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007257 if (act_ret == ACT_RET_YIELD) {
7258 if (flags & ACT_OPT_FINAL)
7259 goto err_yield;
7260
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007261 if (dir == SMP_OPT_DIR_REQ)
7262 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7263 s->hlua->wake_time);
7264 else
7265 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7266 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007267 }
7268 goto end;
7269
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007270 /* yield. */
7271 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007272 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007273 if (dir == SMP_OPT_DIR_REQ)
7274 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7275 s->hlua->wake_time);
7276 else
7277 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7278 s->hlua->wake_time);
7279
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007280 /* Some actions can be wake up when a "write" event
7281 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007282 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007283 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007284 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007285 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007286 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007287 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007288 act_ret = ACT_RET_YIELD;
7289 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007290
7291 /* finished with error. */
7292 case HLUA_E_ERRMSG:
7293 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007294 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007295 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007296 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007297 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007298
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007299 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007300 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007301 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007302
7303 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007304 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007305 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007306
7307 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007308 err_yield:
7309 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007310 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007311 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007312 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007313
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007314 case HLUA_E_ERR:
7315 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007316 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007317 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007318
7319 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007320 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007321 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007322
7323 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007324 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007325 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007326 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007327}
7328
Willy Tarreau144f84a2021-03-02 16:09:26 +01007329struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007330{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007331 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007332
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007333 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007334 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007335 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007336}
7337
7338static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7339{
7340 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007341 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007342 struct task *task;
7343 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007344 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007345
Willy Tarreaubafbe012017-11-24 17:34:44 +01007346 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007347 if (!hlua) {
7348 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007349 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007350 return 0;
7351 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007352 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007353 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007354 ctx->ctx.hlua_apptcp.flags = 0;
7355
7356 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007357 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007358 if (!task) {
7359 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007360 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007361 return 0;
7362 }
7363 task->nice = 0;
7364 task->context = ctx;
7365 task->process = hlua_applet_wakeup;
7366 ctx->ctx.hlua_apptcp.task = task;
7367
7368 /* In the execution wrappers linked with a stream, the
7369 * Lua context can be not initialized. This behavior
7370 * permits to save performances because a systematic
7371 * Lua initialization cause 5% performances loss.
7372 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007373 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 +02007374 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007375 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007376 return 0;
7377 }
7378
7379 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007380 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007381
7382 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007383 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007384 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7385 error = lua_tostring(hlua->T, -1);
7386 else
7387 error = "critical error";
7388 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007389 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007390 return 0;
7391 }
7392
7393 /* Check stack available size. */
7394 if (!lua_checkstack(hlua->T, 1)) {
7395 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007396 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007397 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007398 return 0;
7399 }
7400
7401 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007402 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007403
7404 /* Create and and push object stream in the stack. */
7405 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7406 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007407 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007408 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007409 return 0;
7410 }
7411 hlua->nargs = 1;
7412
7413 /* push keywords in the stack. */
7414 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7415 if (!lua_checkstack(hlua->T, 1)) {
7416 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007417 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007418 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007419 return 0;
7420 }
7421 lua_pushstring(hlua->T, *arg);
7422 hlua->nargs++;
7423 }
7424
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007425 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007426
7427 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007428 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007429 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007430
7431 return 1;
7432}
7433
Willy Tarreau60409db2019-08-21 14:14:50 +02007434void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007435{
7436 struct stream_interface *si = ctx->owner;
7437 struct stream *strm = si_strm(si);
7438 struct channel *res = si_ic(si);
7439 struct act_rule *rule = ctx->rule;
7440 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007441 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007442
7443 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007444 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7445 /* eat the whole request */
7446 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007447 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007448 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007449
7450 /* If the stream is disconnect or closed, ldo nothing. */
7451 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7452 return;
7453
7454 /* Execute the function. */
7455 switch (hlua_ctx_resume(hlua, 1)) {
7456 /* finished. */
7457 case HLUA_E_OK:
7458 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7459
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007460 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007461 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007462 res->flags |= CF_READ_NULL;
7463 si_shutr(si);
7464 return;
7465
7466 /* yield. */
7467 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007468 if (hlua->wake_time != TICK_ETERNITY)
7469 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007470 return;
7471
7472 /* finished with error. */
7473 case HLUA_E_ERRMSG:
7474 /* Display log. */
7475 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007476 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007477 lua_pop(hlua->T, 1);
7478 goto error;
7479
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007480 case HLUA_E_ETMOUT:
7481 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007482 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007483 goto error;
7484
7485 case HLUA_E_NOMEM:
7486 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007487 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007488 goto error;
7489
7490 case HLUA_E_YIELD: /* unexpected */
7491 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007492 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007493 goto error;
7494
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007495 case HLUA_E_ERR:
7496 /* Display log. */
7497 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007498 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007499 goto error;
7500
7501 default:
7502 goto error;
7503 }
7504
7505error:
7506
7507 /* For all other cases, just close the stream. */
7508 si_shutw(si);
7509 si_shutr(si);
7510 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7511}
7512
7513static void hlua_applet_tcp_release(struct appctx *ctx)
7514{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007515 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007516 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007517 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007518 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007519}
7520
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007521/* The function returns 1 if the initialisation is complete, 0 if
7522 * an errors occurs and -1 if more data are required for initializing
7523 * the applet.
7524 */
7525static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7526{
7527 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007528 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007529 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007530 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007531 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007532 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007533
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007534 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007535 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007536 if (!hlua) {
7537 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007538 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007539 return 0;
7540 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007541 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007542 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007543 ctx->ctx.hlua_apphttp.left_bytes = -1;
7544 ctx->ctx.hlua_apphttp.flags = 0;
7545
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007546 if (txn->req.flags & HTTP_MSGF_VER_11)
7547 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7548
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007549 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007550 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007551 if (!task) {
7552 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007553 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007554 return 0;
7555 }
7556 task->nice = 0;
7557 task->context = ctx;
7558 task->process = hlua_applet_wakeup;
7559 ctx->ctx.hlua_apphttp.task = task;
7560
7561 /* In the execution wrappers linked with a stream, the
7562 * Lua context can be not initialized. This behavior
7563 * permits to save performances because a systematic
7564 * Lua initialization cause 5% performances loss.
7565 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007566 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 +02007567 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007568 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007569 return 0;
7570 }
7571
7572 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007573 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007574
7575 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007576 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007577 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7578 error = lua_tostring(hlua->T, -1);
7579 else
7580 error = "critical error";
7581 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007582 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007583 return 0;
7584 }
7585
7586 /* Check stack available size. */
7587 if (!lua_checkstack(hlua->T, 1)) {
7588 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007589 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007590 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007591 return 0;
7592 }
7593
7594 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007595 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007596
7597 /* Create and and push object stream in the stack. */
7598 if (!hlua_applet_http_new(hlua->T, ctx)) {
7599 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007600 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007601 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007602 return 0;
7603 }
7604 hlua->nargs = 1;
7605
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007606 /* push keywords in the stack. */
7607 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7608 if (!lua_checkstack(hlua->T, 1)) {
7609 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007610 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007611 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007612 return 0;
7613 }
7614 lua_pushstring(hlua->T, *arg);
7615 hlua->nargs++;
7616 }
7617
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007618 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007619
7620 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007621 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007622
7623 return 1;
7624}
7625
Willy Tarreau60409db2019-08-21 14:14:50 +02007626void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007627{
7628 struct stream_interface *si = ctx->owner;
7629 struct stream *strm = si_strm(si);
7630 struct channel *req = si_oc(si);
7631 struct channel *res = si_ic(si);
7632 struct act_rule *rule = ctx->rule;
7633 struct proxy *px = strm->be;
7634 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7635 struct htx *req_htx, *res_htx;
7636
7637 res_htx = htx_from_buf(&res->buf);
7638
7639 /* If the stream is disconnect or closed, ldo nothing. */
7640 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7641 goto out;
7642
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007643 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007644 if (!b_size(&res->buf)) {
7645 si_rx_room_blk(si);
7646 goto out;
7647 }
7648 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007649 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007650 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7651
7652 /* Set the currently running flag. */
7653 if (!HLUA_IS_RUNNING(hlua) &&
7654 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007655 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007656 si_cant_get(si);
7657 goto out;
7658 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007659 }
7660
7661 /* Executes The applet if it is not done. */
7662 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7663
7664 /* Execute the function. */
7665 switch (hlua_ctx_resume(hlua, 1)) {
7666 /* finished. */
7667 case HLUA_E_OK:
7668 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7669 break;
7670
7671 /* yield. */
7672 case HLUA_E_AGAIN:
7673 if (hlua->wake_time != TICK_ETERNITY)
7674 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007675 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007676
7677 /* finished with error. */
7678 case HLUA_E_ERRMSG:
7679 /* Display log. */
7680 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007681 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007682 lua_pop(hlua->T, 1);
7683 goto error;
7684
7685 case HLUA_E_ETMOUT:
7686 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007687 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007688 goto error;
7689
7690 case HLUA_E_NOMEM:
7691 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007692 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007693 goto error;
7694
7695 case HLUA_E_YIELD: /* unexpected */
7696 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007697 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007698 goto error;
7699
7700 case HLUA_E_ERR:
7701 /* Display log. */
7702 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007703 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007704 goto error;
7705
7706 default:
7707 goto error;
7708 }
7709 }
7710
7711 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007712 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7713 goto done;
7714
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007715 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7716 goto error;
7717
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007718 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
7719 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007720 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7721 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007722 }
7723
7724 done:
7725 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007726 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007727 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007728 si_shutr(si);
7729 }
7730
7731 /* eat the whole request */
7732 if (co_data(req)) {
7733 req_htx = htx_from_buf(&req->buf);
7734 co_htx_skip(req, req_htx, co_data(req));
7735 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007736 }
7737 }
7738
7739 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007740 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007741 return;
7742
7743 error:
7744
7745 /* If we are in HTTP mode, and we are not send any
7746 * data, return a 500 server error in best effort:
7747 * if there is no room available in the buffer,
7748 * just close the connection.
7749 */
7750 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007751 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007752
7753 channel_erase(res);
7754 res->buf.data = b_data(err);
7755 memcpy(res->buf.area, b_head(err), b_data(err));
7756 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007757 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007758 }
7759 if (!(strm->flags & SF_ERR_MASK))
7760 strm->flags |= SF_ERR_RESOURCE;
7761 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7762 goto done;
7763}
7764
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007765static void hlua_applet_http_release(struct appctx *ctx)
7766{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007767 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007768 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007769 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007770 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007771}
7772
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007773/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007774 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007775 *
7776 * This function can fail with an abort() due to an Lua critical error.
7777 * We are in the configuration parsing process of HAProxy, this abort() is
7778 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007779 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007780static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7781 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007782{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007783 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007784 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007785
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007786 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007787 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007788 if (!rule->arg.hlua_rule) {
7789 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007790 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007791 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007792
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007793 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007794 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7795 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007796 if (!rule->arg.hlua_rule->args) {
7797 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007798 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007799 }
7800
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007801 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007802 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007803
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007804 /* Expect some arguments */
7805 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007806 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007807 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007808 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007809 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007810 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007811 if (!rule->arg.hlua_rule->args[i]) {
7812 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007813 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007814 }
7815 (*cur_arg)++;
7816 }
7817 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007818
Thierry FOURNIER42148732015-09-02 17:17:33 +02007819 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007820 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007821 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007822
7823 error:
7824 if (rule->arg.hlua_rule) {
7825 if (rule->arg.hlua_rule->args) {
7826 for (i = 0; i < fcn->nargs; i++)
7827 ha_free(&rule->arg.hlua_rule->args[i]);
7828 ha_free(&rule->arg.hlua_rule->args);
7829 }
7830 ha_free(&rule->arg.hlua_rule);
7831 }
7832 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007833}
7834
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007835static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7836 struct act_rule *rule, char **err)
7837{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007838 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007839
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007840 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007841 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007842 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007843 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007844 * the call of this analyzer.
7845 */
7846 if (rule->from != ACT_F_HTTP_REQ) {
7847 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7848 return ACT_RET_PRS_ERR;
7849 }
7850
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007851 /* Memory for the rule. */
7852 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7853 if (!rule->arg.hlua_rule) {
7854 memprintf(err, "out of memory error");
7855 return ACT_RET_PRS_ERR;
7856 }
7857
7858 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007859 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007860
7861 /* TODO: later accept arguments. */
7862 rule->arg.hlua_rule->args = NULL;
7863
7864 /* Add applet pointer in the rule. */
7865 rule->applet.obj_type = OBJ_TYPE_APPLET;
7866 rule->applet.name = fcn->name;
7867 rule->applet.init = hlua_applet_http_init;
7868 rule->applet.fct = hlua_applet_http_fct;
7869 rule->applet.release = hlua_applet_http_release;
7870 rule->applet.timeout = hlua_timeout_applet;
7871
7872 return ACT_RET_PRS_OK;
7873}
7874
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007875/* This function is an LUA binding used for registering
7876 * "sample-conv" functions. It expects a converter name used
7877 * in the haproxy configuration file, and an LUA function.
7878 */
7879__LJMP static int hlua_register_action(lua_State *L)
7880{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007881 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007882 const char *name;
7883 int ref;
7884 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007885 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007886 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007887 struct buffer *trash;
7888 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007889
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007890 /* Initialise the number of expected arguments at 0. */
7891 nargs = 0;
7892
7893 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7894 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007895
7896 /* First argument : converter name. */
7897 name = MAY_LJMP(luaL_checkstring(L, 1));
7898
7899 /* Second argument : environment. */
7900 if (lua_type(L, 2) != LUA_TTABLE)
7901 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7902
7903 /* Third argument : lua function. */
7904 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7905
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007906 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007907 if (lua_gettop(L) >= 4)
7908 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7909
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007910 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007911 lua_pushnil(L);
7912 while (lua_next(L, 2) != 0) {
7913 if (lua_type(L, -1) != LUA_TSTRING)
7914 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7915
Thierry Fournierf67442e2020-11-28 20:41:07 +01007916 /* Check if action exists */
7917 trash = get_trash_chunk();
7918 chunk_printf(trash, "lua.%s", name);
7919 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7920 akw = tcp_req_cont_action(trash->area);
7921 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7922 akw = tcp_res_cont_action(trash->area);
7923 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7924 akw = action_http_req_custom(trash->area);
7925 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7926 akw = action_http_res_custom(trash->area);
7927 } else {
7928 akw = NULL;
7929 }
7930 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007931 fcn = akw->private;
7932 if (fcn->function_ref[hlua_state_id] != -1) {
7933 ha_warning("Trying to register action 'lua.%s' more than once. "
7934 "This will become a hard error in version 2.5.\n", name);
7935 }
7936 fcn->function_ref[hlua_state_id] = ref;
7937
7938 /* pop the environment string. */
7939 lua_pop(L, 1);
7940 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007941 }
7942
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007943 /* Check required environment. Only accepted "http" or "tcp". */
7944 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007945 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007946 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007947 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007948 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007949 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007950 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007951
7952 /* Fill fcn. */
7953 fcn->name = strdup(name);
7954 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007955 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007956 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007957
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007958 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007959 fcn->nargs = nargs;
7960
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007961 /* List head */
7962 akl->list.n = akl->list.p = NULL;
7963
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007964 /* action keyword. */
7965 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007966 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007967 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007968 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007969
7970 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7971
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02007972 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007973 akl->kw[0].private = fcn;
7974 akl->kw[0].parse = action_register_lua;
7975
7976 /* select the action registering point. */
7977 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7978 tcp_req_cont_keywords_register(akl);
7979 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7980 tcp_res_cont_keywords_register(akl);
7981 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7982 http_req_keywords_register(akl);
7983 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7984 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007985 else {
7986 release_hlua_function(fcn);
7987 if (akl)
7988 ha_free((char **)&(akl->kw[0].kw));
7989 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007990 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007991 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7992 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007993 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007994
7995 /* pop the environment string. */
7996 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007997
7998 /* reset for next loop */
7999 akl = NULL;
8000 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008001 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008002 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008003
8004 alloc_error:
8005 release_hlua_function(fcn);
8006 ha_free(&akl);
8007 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8008 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008009}
8010
8011static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
8012 struct act_rule *rule, char **err)
8013{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02008014 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008015
Christopher Faulet280f85b2019-07-15 15:02:04 +02008016 if (px->mode == PR_MODE_HTTP) {
8017 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01008018 return ACT_RET_PRS_ERR;
8019 }
8020
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008021 /* Memory for the rule. */
8022 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
8023 if (!rule->arg.hlua_rule) {
8024 memprintf(err, "out of memory error");
8025 return ACT_RET_PRS_ERR;
8026 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008027
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008028 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01008029 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008030
8031 /* TODO: later accept arguments. */
8032 rule->arg.hlua_rule->args = NULL;
8033
8034 /* Add applet pointer in the rule. */
8035 rule->applet.obj_type = OBJ_TYPE_APPLET;
8036 rule->applet.name = fcn->name;
8037 rule->applet.init = hlua_applet_tcp_init;
8038 rule->applet.fct = hlua_applet_tcp_fct;
8039 rule->applet.release = hlua_applet_tcp_release;
8040 rule->applet.timeout = hlua_timeout_applet;
8041
8042 return 0;
8043}
8044
8045/* This function is an LUA binding used for registering
8046 * "sample-conv" functions. It expects a converter name used
8047 * in the haproxy configuration file, and an LUA function.
8048 */
8049__LJMP static int hlua_register_service(lua_State *L)
8050{
8051 struct action_kw_list *akl;
8052 const char *name;
8053 const char *env;
8054 int ref;
8055 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02008056 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008057 struct buffer *trash;
8058 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008059
8060 MAY_LJMP(check_args(L, 3, "register_service"));
8061
8062 /* First argument : converter name. */
8063 name = MAY_LJMP(luaL_checkstring(L, 1));
8064
8065 /* Second argument : environment. */
8066 env = MAY_LJMP(luaL_checkstring(L, 2));
8067
8068 /* Third argument : lua function. */
8069 ref = MAY_LJMP(hlua_checkfunction(L, 3));
8070
Thierry Fournierf67442e2020-11-28 20:41:07 +01008071 /* Check for service already registered */
8072 trash = get_trash_chunk();
8073 chunk_printf(trash, "lua.%s", name);
8074 akw = service_find(trash->area);
8075 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008076 fcn = akw->private;
8077 if (fcn->function_ref[hlua_state_id] != -1) {
8078 ha_warning("Trying to register service 'lua.%s' more than once. "
8079 "This will become a hard error in version 2.5.\n", name);
8080 }
8081 fcn->function_ref[hlua_state_id] = ref;
8082 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008083 }
8084
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008085 /* Allocate and fill the sample fetch keyword struct. */
8086 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
8087 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02008088 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008089 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008090 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02008091 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008092
8093 /* Fill fcn. */
8094 len = strlen("<lua.>") + strlen(name) + 1;
8095 fcn->name = calloc(1, len);
8096 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02008097 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008098 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01008099 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008100
8101 /* List head */
8102 akl->list.n = akl->list.p = NULL;
8103
8104 /* converter keyword. */
8105 len = strlen("lua.") + strlen(name) + 1;
8106 akl->kw[0].kw = calloc(1, len);
8107 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02008108 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008109
8110 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
8111
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01008112 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008113 if (strcmp(env, "tcp") == 0)
8114 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008115 else if (strcmp(env, "http") == 0)
8116 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02008117 else {
8118 release_hlua_function(fcn);
8119 if (akl)
8120 ha_free((char **)&(akl->kw[0].kw));
8121 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008122 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01008123 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02008124 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008125
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02008126 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008127 akl->kw[0].private = fcn;
8128
8129 /* End of array. */
8130 memset(&akl->kw[1], 0, sizeof(*akl->kw));
8131
8132 /* Register this new converter */
8133 service_keywords_register(akl);
8134
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008135 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02008136
8137 alloc_error:
8138 release_hlua_function(fcn);
8139 ha_free(&akl);
8140 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8141 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008142}
8143
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008144/* This function initialises Lua cli handler. It copies the
8145 * arguments in the Lua stack and create channel IO objects.
8146 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02008147static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008148{
8149 struct hlua *hlua;
8150 struct hlua_function *fcn;
8151 int i;
8152 const char *error;
8153
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008154 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008155 appctx->ctx.hlua_cli.fcn = private;
8156
Willy Tarreaubafbe012017-11-24 17:34:44 +01008157 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008158 if (!hlua) {
8159 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008160 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008161 }
8162 HLUA_INIT(hlua);
8163 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008164
8165 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05008166 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008167 * applet_http. It is absolutely compatible.
8168 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01008169 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008170 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01008171 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008172 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008173 }
8174 appctx->ctx.hlua_cli.task->nice = 0;
8175 appctx->ctx.hlua_cli.task->context = appctx;
8176 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
8177
8178 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01008179 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 +01008180 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008181 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008182 }
8183
8184 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008185 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008186 if (lua_type(hlua->T, -1) == LUA_TSTRING)
8187 error = lua_tostring(hlua->T, -1);
8188 else
8189 error = "critical error";
8190 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
8191 goto error;
8192 }
8193
8194 /* Check stack available size. */
8195 if (!lua_checkstack(hlua->T, 2)) {
8196 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8197 goto error;
8198 }
8199
8200 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008201 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008202
8203 /* Once the arguments parsed, the CLI is like an AppletTCP,
8204 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008205 */
8206 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
8207 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8208 goto error;
8209 }
8210 hlua->nargs = 1;
8211
8212 /* push keywords in the stack. */
8213 for (i = 0; *args[i]; i++) {
8214 /* Check stack available size. */
8215 if (!lua_checkstack(hlua->T, 1)) {
8216 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8217 goto error;
8218 }
8219 lua_pushstring(hlua->T, args[i]);
8220 hlua->nargs++;
8221 }
8222
8223 /* We must initialize the execution timeouts. */
8224 hlua->max_time = hlua_timeout_session;
8225
8226 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008227 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008228
8229 /* It's ok */
8230 return 0;
8231
8232 /* It's not ok. */
8233error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008234 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008235 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008236 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008237 return 1;
8238}
8239
8240static int hlua_cli_io_handler_fct(struct appctx *appctx)
8241{
8242 struct hlua *hlua;
8243 struct stream_interface *si;
8244 struct hlua_function *fcn;
8245
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008246 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008247 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008248 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008249
8250 /* If the stream is disconnect or closed, ldo nothing. */
8251 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8252 return 1;
8253
8254 /* Execute the function. */
8255 switch (hlua_ctx_resume(hlua, 1)) {
8256
8257 /* finished. */
8258 case HLUA_E_OK:
8259 return 1;
8260
8261 /* yield. */
8262 case HLUA_E_AGAIN:
8263 /* We want write. */
8264 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008265 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008266 /* Set the timeout. */
8267 if (hlua->wake_time != TICK_ETERNITY)
8268 task_schedule(hlua->task, hlua->wake_time);
8269 return 0;
8270
8271 /* finished with error. */
8272 case HLUA_E_ERRMSG:
8273 /* Display log. */
8274 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8275 fcn->name, lua_tostring(hlua->T, -1));
8276 lua_pop(hlua->T, 1);
8277 return 1;
8278
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008279 case HLUA_E_ETMOUT:
8280 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8281 fcn->name);
8282 return 1;
8283
8284 case HLUA_E_NOMEM:
8285 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8286 fcn->name);
8287 return 1;
8288
8289 case HLUA_E_YIELD: /* unexpected */
8290 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8291 fcn->name);
8292 return 1;
8293
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008294 case HLUA_E_ERR:
8295 /* Display log. */
8296 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8297 fcn->name);
8298 return 1;
8299
8300 default:
8301 return 1;
8302 }
8303
8304 return 1;
8305}
8306
8307static void hlua_cli_io_release_fct(struct appctx *appctx)
8308{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008309 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008310 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008311}
8312
8313/* This function is an LUA binding used for registering
8314 * new keywords in the cli. It expects a list of keywords
8315 * which are the "path". It is limited to 5 keywords. A
8316 * description of the command, a function to be executed
8317 * for the parsing and a function for io handlers.
8318 */
8319__LJMP static int hlua_register_cli(lua_State *L)
8320{
8321 struct cli_kw_list *cli_kws;
8322 const char *message;
8323 int ref_io;
8324 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008325 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008326 int index;
8327 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008328 struct buffer *trash;
8329 const char *kw[5];
8330 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008331 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008332
8333 MAY_LJMP(check_args(L, 3, "register_cli"));
8334
8335 /* First argument : an array of maximum 5 keywords. */
8336 if (!lua_istable(L, 1))
8337 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8338
8339 /* Second argument : string with contextual message. */
8340 message = MAY_LJMP(luaL_checkstring(L, 2));
8341
8342 /* Third and fourth argument : lua function. */
8343 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8344
Thierry Fournierf67442e2020-11-28 20:41:07 +01008345 /* Check for CLI service already registered */
8346 trash = get_trash_chunk();
8347 index = 0;
8348 lua_pushnil(L);
8349 memset(kw, 0, sizeof(kw));
8350 while (lua_next(L, 1) != 0) {
8351 if (index >= CLI_PREFIX_KW_NB)
8352 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8353 if (lua_type(L, -1) != LUA_TSTRING)
8354 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8355 kw[index] = lua_tostring(L, -1);
8356 if (index == 0)
8357 chunk_printf(trash, "%s", kw[index]);
8358 else
8359 chunk_appendf(trash, " %s", kw[index]);
8360 index++;
8361 lua_pop(L, 1);
8362 }
8363 cli_kw = cli_find_kw_exact((char **)kw);
8364 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008365 fcn = cli_kw->private;
8366 if (fcn->function_ref[hlua_state_id] != -1) {
8367 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8368 "This will become a hard error in version 2.5.\n", trash->area);
8369 }
8370 fcn->function_ref[hlua_state_id] = ref_io;
8371 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008372 }
8373
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008374 /* Allocate and fill the sample fetch keyword struct. */
8375 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008376 if (!cli_kws) {
8377 errmsg = "Lua out of memory error.";
8378 goto error;
8379 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008380 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008381 if (!fcn) {
8382 errmsg = "Lua out of memory error.";
8383 goto error;
8384 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008385
8386 /* Fill path. */
8387 index = 0;
8388 lua_pushnil(L);
8389 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008390 if (index >= 5) {
8391 errmsg = "1st argument must be a table with a maximum of 5 entries";
8392 goto error;
8393 }
8394 if (lua_type(L, -1) != LUA_TSTRING) {
8395 errmsg = "1st argument must be a table filled with strings";
8396 goto error;
8397 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008398 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008399 if (!cli_kws->kw[0].str_kw[index]) {
8400 errmsg = "Lua out of memory error.";
8401 goto error;
8402 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008403 index++;
8404 lua_pop(L, 1);
8405 }
8406
8407 /* Copy help message. */
8408 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008409 if (!cli_kws->kw[0].usage) {
8410 errmsg = "Lua out of memory error.";
8411 goto error;
8412 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008413
8414 /* Fill fcn io handler. */
8415 len = strlen("<lua.cli>") + 1;
8416 for (i = 0; i < index; i++)
8417 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8418 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008419 if (!fcn->name) {
8420 errmsg = "Lua out of memory error.";
8421 goto error;
8422 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008423 strncat((char *)fcn->name, "<lua.cli", len);
8424 for (i = 0; i < index; i++) {
8425 strncat((char *)fcn->name, ".", len);
8426 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8427 }
8428 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008429 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008430
8431 /* Fill last entries. */
8432 cli_kws->kw[0].private = fcn;
8433 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8434 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8435 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8436
8437 /* Register this new converter */
8438 cli_register_kw(cli_kws);
8439
8440 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008441
8442 error:
8443 release_hlua_function(fcn);
8444 if (cli_kws) {
8445 for (i = 0; i < index; i++)
8446 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8447 ha_free((char **)&(cli_kws->kw[0].usage));
8448 }
8449 ha_free(&cli_kws);
8450 WILL_LJMP(luaL_error(L, errmsg));
8451 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008452}
8453
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008454static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008455 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008456 char **err, unsigned int *timeout)
8457{
8458 const char *error;
8459
8460 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008461 if (error == PARSE_TIME_OVER) {
8462 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8463 args[1], args[0]);
8464 return -1;
8465 }
8466 else if (error == PARSE_TIME_UNDER) {
8467 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8468 args[1], args[0]);
8469 return -1;
8470 }
8471 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008472 memprintf(err, "%s: invalid timeout", args[0]);
8473 return -1;
8474 }
8475 return 0;
8476}
8477
8478static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008479 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008480 char **err)
8481{
8482 return hlua_read_timeout(args, section_type, curpx, defpx,
8483 file, line, err, &hlua_timeout_session);
8484}
8485
8486static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008487 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008488 char **err)
8489{
8490 return hlua_read_timeout(args, section_type, curpx, defpx,
8491 file, line, err, &hlua_timeout_task);
8492}
8493
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008494static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008495 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008496 char **err)
8497{
8498 return hlua_read_timeout(args, section_type, curpx, defpx,
8499 file, line, err, &hlua_timeout_applet);
8500}
8501
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008502static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008503 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008504 char **err)
8505{
8506 char *error;
8507
8508 hlua_nb_instruction = strtoll(args[1], &error, 10);
8509 if (*error != '\0') {
8510 memprintf(err, "%s: invalid number", args[0]);
8511 return -1;
8512 }
8513 return 0;
8514}
8515
Willy Tarreau32f61e22015-03-18 17:54:59 +01008516static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008517 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008518 char **err)
8519{
8520 char *error;
8521
8522 if (*(args[1]) == 0) {
8523 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8524 return -1;
8525 }
8526 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8527 if (*error != '\0') {
8528 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8529 return -1;
8530 }
8531 return 0;
8532}
8533
8534
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008535/* This function is called by the main configuration key "lua-load". It loads and
8536 * execute an lua file during the parsing of the HAProxy configuration file. It is
8537 * the main lua entry point.
8538 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008539 * This function runs with the HAProxy keywords API. It returns -1 if an error
8540 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008541 *
8542 * In some error case, LUA set an error message in top of the stack. This function
8543 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008544 *
8545 * This function can fail with an abort() due to an Lua critical error.
8546 * We are in the configuration parsing process of HAProxy, this abort() is
8547 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008548 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008549static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008550{
8551 int error;
8552
8553 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008554 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008555 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008556 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8557 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008558 return -1;
8559 }
8560
8561 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008562 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008563 switch (error) {
8564 case LUA_OK:
8565 break;
8566 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008567 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8568 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008569 return -1;
8570 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008571 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008572 return -1;
8573 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008574 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8575 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008576 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008577#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008578 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008579 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8580 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008581 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008582#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008583 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008584 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8585 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008586 return -1;
8587 }
8588
8589 return 0;
8590}
8591
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008592static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008593 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008594 char **err)
8595{
8596 if (*(args[1]) == 0) {
8597 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8598 return -1;
8599 }
8600
Thierry Fournier59f11be2020-11-29 00:37:41 +01008601 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008602 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008603 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008604 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008605}
8606
Thierry Fournier59f11be2020-11-29 00:37:41 +01008607static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008608 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008609 char **err)
8610{
8611 int len;
8612
8613 if (*(args[1]) == 0) {
8614 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8615 return -1;
8616 }
8617
8618 if (per_thread_load == NULL) {
8619 /* allocate the first entry large enough to store the final NULL */
8620 per_thread_load = calloc(1, sizeof(*per_thread_load));
8621 if (per_thread_load == NULL) {
8622 memprintf(err, "out of memory error");
8623 return -1;
8624 }
8625 }
8626
8627 /* count used entries */
8628 for (len = 0; per_thread_load[len] != NULL; len++)
8629 ;
8630
8631 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8632 if (per_thread_load == NULL) {
8633 memprintf(err, "out of memory error");
8634 return -1;
8635 }
8636
8637 per_thread_load[len] = strdup(args[1]);
8638 per_thread_load[len + 1] = NULL;
8639
8640 if (per_thread_load[len] == NULL) {
8641 memprintf(err, "out of memory error");
8642 return -1;
8643 }
8644
8645 /* loading for thread 1 only */
8646 hlua_state_id = 1;
8647 ha_set_tid(0);
8648 return hlua_load_state(args[1], hlua_states[1], err);
8649}
8650
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008651/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8652 * in the given <ctx>.
8653 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008654static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008655{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008656 lua_getglobal(L, "package"); /* push package variable */
8657 lua_pushstring(L, path); /* push given path */
8658 lua_pushstring(L, ";"); /* push semicolon */
8659 lua_getfield(L, -3, type); /* push old path */
8660 lua_concat(L, 3); /* concatenate to new path */
8661 lua_setfield(L, -2, type); /* store new path */
8662 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008663
8664 return 0;
8665}
8666
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008667static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008668 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008669 char **err)
8670{
8671 char *path;
8672 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008673 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008674
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008675 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008676 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008677 }
8678
8679 if (!(*args[1])) {
8680 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008681 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008682 }
8683 path = args[1];
8684
8685 if (*args[2]) {
8686 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8687 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008688 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008689 }
8690 type = args[2];
8691 }
8692
Thierry Fournier59f11be2020-11-29 00:37:41 +01008693 p = calloc(1, sizeof(*p));
8694 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008695 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008696 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008697 }
8698 p->path = strdup(path);
8699 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008700 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008701 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008702 }
8703 p->type = strdup(type);
8704 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008705 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008706 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008707 }
Willy Tarreau2b718102021-04-21 07:32:39 +02008708 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01008709
8710 hlua_prepend_path(hlua_states[0], type, path);
8711 hlua_prepend_path(hlua_states[1], type, path);
8712 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008713
8714err2:
8715 free(p->type);
8716 free(p->path);
8717err:
8718 free(p);
8719 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008720}
8721
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008722/* configuration keywords declaration */
8723static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008724 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008725 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008726 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008727 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8728 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008729 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008730 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008731 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008732 { 0, NULL, NULL },
8733}};
8734
Willy Tarreau0108d902018-11-25 19:14:37 +01008735INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8736
Christopher Fauletafd8f102018-11-08 11:34:21 +01008737
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008738/* This function can fail with an abort() due to an Lua critical error.
8739 * We are in the initialisation process of HAProxy, this abort() is
8740 * tolerated.
8741 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008742int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008743{
8744 struct hlua_init_function *init;
8745 const char *msg;
8746 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008747 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008748 const char *kind;
8749 const char *trace;
8750 int return_status = 1;
8751#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8752 int nres;
8753#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008754
Willy Tarreaucdb53462020-12-02 12:12:00 +01008755 /* disable memory limit checks if limit is not set */
8756 if (!hlua_global_allocator.limit)
8757 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8758
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008759 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008760 if (setjmp(safe_ljmp_env) != 0) {
8761 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008762 if (lua_type(L, -1) == LUA_TSTRING)
8763 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008764 else
8765 error = "critical error";
8766 fprintf(stderr, "Lua post-init: %s.\n", error);
8767 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008768 } else {
8769 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008770 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008771
Thierry Fournierb8cef172020-11-28 15:37:17 +01008772 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008773
Thierry Fournierc7492592020-11-28 23:57:24 +01008774 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008775 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008776
8777#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008778 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008779#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008780 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008781#endif
8782 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008783 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008784
8785 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008786 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008787 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008788
8789 case LUA_ERRERR:
8790 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008791 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008792 case LUA_ERRRUN:
8793 if (!kind)
8794 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008795 msg = lua_tostring(L, -1);
8796 lua_settop(L, 0); /* Empty the stack. */
8797 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008798 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008799 if (msg)
8800 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8801 else
8802 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8803 return_status = 0;
8804 break;
8805
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008806 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008807 /* Unknown error */
8808 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008809 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008810 case LUA_YIELD:
8811 /* yield is not configured at this step, this state doesn't happen */
8812 if (!kind)
8813 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008814 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008815 case LUA_ERRMEM:
8816 if (!kind)
8817 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008818 lua_settop(L, 0);
8819 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01008820 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01008821 ha_alert("Lua init: %s: %s\n", kind, trace);
8822 return_status = 0;
8823 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008824 }
Thierry Fournier670db242020-11-28 10:49:59 +01008825 if (!return_status)
8826 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008827 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008828
8829 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008830 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008831}
8832
Thierry Fournierb8cef172020-11-28 15:37:17 +01008833int hlua_post_init()
8834{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008835 int ret;
8836 int i;
8837 int errors;
8838 char *err = NULL;
8839 struct hlua_function *fcn;
8840
Thierry Fournierb8cef172020-11-28 15:37:17 +01008841#if USE_OPENSSL
8842 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008843 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008844 int saved_used_backed = global.ssl_used_backend;
8845 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01008846 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008847 global.ssl_used_backend = saved_used_backed;
8848 }
8849#endif
8850
Thierry Fournierc7492592020-11-28 23:57:24 +01008851 /* Perform post init of common thread */
8852 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008853 ha_set_tid(0);
8854 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8855 if (ret == 0)
8856 return 0;
8857
8858 /* init remaining lua states and load files */
8859 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8860
8861 /* set thread context */
8862 ha_set_tid(hlua_state_id - 1);
8863
8864 /* Init lua state */
8865 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8866
8867 /* Load lua files */
8868 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8869 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8870 if (ret != 0) {
8871 ha_alert("Lua init: %s\n", err);
8872 return 0;
8873 }
8874 }
8875 }
8876
8877 /* Reset thread context */
8878 ha_set_tid(0);
8879
8880 /* Execute post init for all states */
8881 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8882
8883 /* set thread context */
8884 ha_set_tid(hlua_state_id - 1);
8885
8886 /* run post init */
8887 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8888 if (ret == 0)
8889 return 0;
8890 }
8891
8892 /* Reset thread context */
8893 ha_set_tid(0);
8894
8895 /* control functions registering. Each function must have:
8896 * - only the function_ref[0] set positive and all other to -1
8897 * - only the function_ref[0] set to -1 and all other positive
8898 * This ensure a same reference is not used both in shared
8899 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008900 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008901 * complicated to found for the end user.
8902 */
8903 errors = 0;
8904 list_for_each_entry(fcn, &referenced_functions, l) {
8905 ret = 0;
8906 for (i = 1; i < global.nbthread + 1; i++) {
8907 if (fcn->function_ref[i] == -1)
8908 ret--;
8909 else
8910 ret++;
8911 }
8912 if (abs(ret) != global.nbthread) {
8913 ha_alert("Lua function '%s' is not referenced in all thread. "
8914 "Expect function in all thread or in none thread.\n", fcn->name);
8915 errors++;
8916 continue;
8917 }
8918
8919 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008920 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8921 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008922 "exclusive.\n", fcn->name);
8923 errors++;
8924 }
8925 }
8926
8927 if (errors > 0)
8928 return 0;
8929
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008930 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008931 * -1 in order to have probably a segfault if someone use it
8932 */
8933 hlua_state_id = -1;
8934
8935 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008936}
8937
Willy Tarreau32f61e22015-03-18 17:54:59 +01008938/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8939 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8940 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008941 * allocation. <nsize> is the requested new size. A new allocation is
8942 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008943 * zero. This one verifies that the limits are respected but is optimized
8944 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008945 */
8946static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8947{
8948 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008949 size_t limit, old, new;
8950
Tim Duesterhus22586522021-01-08 10:35:33 +01008951 if (unlikely(!ptr && !nsize))
8952 return NULL;
8953
Willy Tarreaucdb53462020-12-02 12:12:00 +01008954 /* a limit of ~0 means unlimited and boot complete, so there's no need
8955 * for accounting anymore.
8956 */
Christopher Fauletcc2c4f82021-03-24 14:52:24 +01008957 if (likely(~zone->limit == 0))
8958 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008959
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008960 if (!ptr)
8961 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008962
Willy Tarreaucdb53462020-12-02 12:12:00 +01008963 /* enforce strict limits across all threads */
8964 limit = zone->limit;
8965 old = _HA_ATOMIC_LOAD(&zone->allocated);
8966 do {
8967 new = old + nsize - osize;
8968 if (unlikely(nsize && limit && new > limit))
8969 return NULL;
8970 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008971
8972 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008973
8974 if (unlikely(!ptr && nsize)) // failed
8975 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8976
8977 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008978 return ptr;
8979}
8980
Thierry Fournierecb83c22020-11-28 15:49:44 +01008981/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008982 * We are in the initialisation process of HAProxy, this abort() is
8983 * tolerated.
8984 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008985lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008986{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008987 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008988 int idx;
8989 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008990 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008991 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008992 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008993 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008994 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008995 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008996
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008997 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008998 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008999
Thierry Fournier4234dbd2020-11-28 13:18:23 +01009000 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009001 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01009002 *context = NULL;
9003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009004 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009005 * the Lua function can fail with an abort. We are in the initialisation
9006 * process of HAProxy, this abort() is tolerated.
9007 */
9008
Thierry Fournier3c539322020-11-28 16:05:05 +01009009 /* Call post initialisation function in safe environment. */
9010 if (setjmp(safe_ljmp_env) != 0) {
9011 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009012 if (lua_type(L, -1) == LUA_TSTRING)
9013 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01009014 else
9015 error_msg = "critical error";
9016 fprintf(stderr, "Lua init: %s.\n", error_msg);
9017 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01009018 } else {
9019 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01009020 }
9021
Thierry FOURNIER380d0932015-01-23 14:27:52 +01009022 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009023 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01009024#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
9025#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
9026#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009027 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01009028#endif
9029#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009030 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01009031#endif
9032#undef HLUA_PREPEND_PATH_TOSTRING
9033#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009034
Thierry Fournier59f11be2020-11-29 00:37:41 +01009035 /* Apply configured prepend path */
9036 list_for_each_entry(pp, &prepend_path_list, l)
9037 hlua_prepend_path(L, pp->type, pp->path);
9038
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009039 /*
9040 *
9041 * Create "core" object.
9042 *
9043 */
9044
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01009045 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009046 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009047
Thierry Fournierecb83c22020-11-28 15:49:44 +01009048 /* set the thread id */
9049 hlua_class_const_int(L, "thread", thread_num);
9050
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009051 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01009052 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009053 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009054
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009055 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009056 hlua_class_function(L, "register_init", hlua_register_init);
9057 hlua_class_function(L, "register_task", hlua_register_task);
9058 hlua_class_function(L, "register_fetches", hlua_register_fetches);
9059 hlua_class_function(L, "register_converters", hlua_register_converters);
9060 hlua_class_function(L, "register_action", hlua_register_action);
9061 hlua_class_function(L, "register_service", hlua_register_service);
9062 hlua_class_function(L, "register_cli", hlua_register_cli);
9063 hlua_class_function(L, "yield", hlua_yield);
9064 hlua_class_function(L, "set_nice", hlua_set_nice);
9065 hlua_class_function(L, "sleep", hlua_sleep);
9066 hlua_class_function(L, "msleep", hlua_msleep);
9067 hlua_class_function(L, "add_acl", hlua_add_acl);
9068 hlua_class_function(L, "del_acl", hlua_del_acl);
9069 hlua_class_function(L, "set_map", hlua_set_map);
9070 hlua_class_function(L, "del_map", hlua_del_map);
9071 hlua_class_function(L, "tcp", hlua_socket_new);
9072 hlua_class_function(L, "log", hlua_log);
9073 hlua_class_function(L, "Debug", hlua_log_debug);
9074 hlua_class_function(L, "Info", hlua_log_info);
9075 hlua_class_function(L, "Warning", hlua_log_warning);
9076 hlua_class_function(L, "Alert", hlua_log_alert);
9077 hlua_class_function(L, "done", hlua_done);
9078 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009079
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009080 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009081
9082 /*
9083 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009084 * Create "act" object.
9085 *
9086 */
9087
9088 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009089 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009090
9091 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009092 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
9093 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
9094 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
9095 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
9096 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
9097 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
9098 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
9099 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009100
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009101 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01009102
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009103 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009104
9105 /*
9106 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009107 * Register class Map
9108 *
9109 */
9110
9111 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009112 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009113
9114 /* register pattern types. */
9115 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009116 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009117 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009118 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009119 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009120 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009121
9122 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009123 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009124
9125 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009126 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009127
Ilya Shipitsind4259502020-04-08 01:07:56 +05009128 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009129 lua_pushstring(L, "__index");
9130 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009131
9132 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009133 hlua_class_function(L, "lookup", hlua_map_lookup);
9134 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009135
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009136 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009137
Thierry Fournier45e78d72016-02-19 18:34:46 +01009138 /* Register previous table in the registry with reference and named entry.
9139 * The function hlua_register_metatable() pops the stack, so we
9140 * previously create a copy of the table.
9141 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009142 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
9143 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009144
9145 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009146 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009147
9148 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009149 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009150
9151 /*
9152 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009153 * Register class Channel
9154 *
9155 */
9156
9157 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009158 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009159
Ilya Shipitsind4259502020-04-08 01:07:56 +05009160 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009161 lua_pushstring(L, "__index");
9162 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009163
9164 /* Register . */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02009165 hlua_class_function(L, "data", hlua_channel_get_data);
9166 hlua_class_function(L, "line", hlua_channel_get_line);
9167 hlua_class_function(L, "set", hlua_channel_set_data);
9168 hlua_class_function(L, "remove", hlua_channel_del_data);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009169 hlua_class_function(L, "append", hlua_channel_append);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02009170 hlua_class_function(L, "prepend", hlua_channel_prepend);
9171 hlua_class_function(L, "insert", hlua_channel_insert_data);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009172 hlua_class_function(L, "send", hlua_channel_send);
9173 hlua_class_function(L, "forward", hlua_channel_forward);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02009174 hlua_class_function(L, "input", hlua_channel_get_in_len);
9175 hlua_class_function(L, "output", hlua_channel_get_out_len);
9176 hlua_class_function(L, "may_recv", hlua_channel_may_recv);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009177 hlua_class_function(L, "is_full", hlua_channel_is_full);
9178 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009179
Christopher Faulet6a79fc12021-08-06 16:02:36 +02009180 /* Deprecated API */
9181 hlua_class_function(L, "get", hlua_channel_get);
9182 hlua_class_function(L, "dup", hlua_channel_dup);
9183 hlua_class_function(L, "getline", hlua_channel_getline);
9184 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
9185 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
9186
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009187 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009188
9189 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009190 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009191
9192 /*
9193 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009194 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009195 *
9196 */
9197
9198 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009199 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009200
Ilya Shipitsind4259502020-04-08 01:07:56 +05009201 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009202 lua_pushstring(L, "__index");
9203 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009204
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009205 /* Browse existing fetches and create the associated
9206 * object method.
9207 */
9208 sf = NULL;
9209 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009210 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9211 * by an underscore.
9212 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009213 strncpy(trash.area, sf->kw, trash.size);
9214 trash.area[trash.size - 1] = '\0';
9215 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009216 if (*p == '.' || *p == '-' || *p == '+')
9217 *p = '_';
9218
9219 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009220 lua_pushstring(L, trash.area);
9221 lua_pushlightuserdata(L, sf);
9222 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
9223 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009224 }
9225
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009226 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009227
9228 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009229 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009230
9231 /*
9232 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009233 * Register class Converters
9234 *
9235 */
9236
9237 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009238 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009239
9240 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009241 lua_pushstring(L, "__index");
9242 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009243
9244 /* Browse existing converters and create the associated
9245 * object method.
9246 */
9247 sc = NULL;
9248 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009249 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9250 * by an underscore.
9251 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009252 strncpy(trash.area, sc->kw, trash.size);
9253 trash.area[trash.size - 1] = '\0';
9254 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009255 if (*p == '.' || *p == '-' || *p == '+')
9256 *p = '_';
9257
9258 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009259 lua_pushstring(L, trash.area);
9260 lua_pushlightuserdata(L, sc);
9261 lua_pushcclosure(L, hlua_run_sample_conv, 1);
9262 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009263 }
9264
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009265 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009266
9267 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009268 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009269
9270 /*
9271 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009272 * Register class HTTP
9273 *
9274 */
9275
9276 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009277 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009278
Ilya Shipitsind4259502020-04-08 01:07:56 +05009279 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009280 lua_pushstring(L, "__index");
9281 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009282
9283 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009284 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9285 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9286 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9287 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9288 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9289 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9290 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9291 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9292 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9293 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009294
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009295 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9296 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9297 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9298 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9299 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9300 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9301 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009302
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009303 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009304
9305 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009306 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009307
9308 /*
9309 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009310 * Register class AppletTCP
9311 *
9312 */
9313
9314 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009315 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009316
Ilya Shipitsind4259502020-04-08 01:07:56 +05009317 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009318 lua_pushstring(L, "__index");
9319 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009320
9321 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009322 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9323 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9324 hlua_class_function(L, "send", hlua_applet_tcp_send);
9325 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9326 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9327 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9328 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9329 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009330
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009331 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009332
9333 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009334 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009335
9336 /*
9337 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009338 * Register class AppletHTTP
9339 *
9340 */
9341
9342 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009343 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009344
Ilya Shipitsind4259502020-04-08 01:07:56 +05009345 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009346 lua_pushstring(L, "__index");
9347 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009348
9349 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009350 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9351 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9352 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9353 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9354 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9355 hlua_class_function(L, "getline", hlua_applet_http_getline);
9356 hlua_class_function(L, "receive", hlua_applet_http_recv);
9357 hlua_class_function(L, "send", hlua_applet_http_send);
9358 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9359 hlua_class_function(L, "set_status", hlua_applet_http_status);
9360 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009361
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009362 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009363
9364 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009365 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009366
9367 /*
9368 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009369 * Register class TXN
9370 *
9371 */
9372
9373 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009374 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009375
Ilya Shipitsind4259502020-04-08 01:07:56 +05009376 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009377 lua_pushstring(L, "__index");
9378 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009379
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009380 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009381 hlua_class_function(L, "set_priv", hlua_set_priv);
9382 hlua_class_function(L, "get_priv", hlua_get_priv);
9383 hlua_class_function(L, "set_var", hlua_set_var);
9384 hlua_class_function(L, "unset_var", hlua_unset_var);
9385 hlua_class_function(L, "get_var", hlua_get_var);
9386 hlua_class_function(L, "done", hlua_txn_done);
9387 hlua_class_function(L, "reply", hlua_txn_reply_new);
9388 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9389 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9390 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9391 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9392 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9393 hlua_class_function(L, "deflog", hlua_txn_deflog);
9394 hlua_class_function(L, "log", hlua_txn_log);
9395 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9396 hlua_class_function(L, "Info", hlua_txn_log_info);
9397 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9398 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009399
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009400 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009401
9402 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009403 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009404
9405 /*
9406 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009407 * Register class reply
9408 *
9409 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009410 lua_newtable(L);
9411 lua_pushstring(L, "__index");
9412 lua_newtable(L);
9413 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9414 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9415 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9416 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9417 lua_settable(L, -3); /* Sets the __index entry. */
9418 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009419
9420
9421 /*
9422 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009423 * Register class Socket
9424 *
9425 */
9426
9427 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009428 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009429
Ilya Shipitsind4259502020-04-08 01:07:56 +05009430 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009431 lua_pushstring(L, "__index");
9432 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009433
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009434#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009435 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009436#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009437 hlua_class_function(L, "connect", hlua_socket_connect);
9438 hlua_class_function(L, "send", hlua_socket_send);
9439 hlua_class_function(L, "receive", hlua_socket_receive);
9440 hlua_class_function(L, "close", hlua_socket_close);
9441 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9442 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9443 hlua_class_function(L, "setoption", hlua_socket_setoption);
9444 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009445
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009446 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009447
9448 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009449 lua_pushstring(L, "__gc");
9450 lua_pushcclosure(L, hlua_socket_gc, 0);
9451 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009452
9453 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009454 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009455
Thierry Fournieraafc7772020-12-04 11:47:47 +01009456 lua_atpanic(L, hlua_panic_safe);
9457
9458 return L;
9459}
9460
9461void hlua_init(void) {
9462 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009463 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009464#ifdef USE_OPENSSL
9465 struct srv_kw *kw;
9466 int tmp_error;
9467 char *error;
9468 char *args[] = { /* SSL client configuration. */
9469 "ssl",
9470 "verify",
9471 "none",
9472 NULL
9473 };
9474#endif
9475
9476 /* Init post init function list head */
9477 for (i = 0; i < MAX_THREADS + 1; i++)
9478 LIST_INIT(&hlua_init_functions[i]);
9479
9480 /* Init state for common/shared lua parts */
9481 hlua_state_id = 0;
9482 ha_set_tid(0);
9483 hlua_states[0] = hlua_init_state(0);
9484
9485 /* Init state 1 for thread 0. We have at least one thread. */
9486 hlua_state_id = 1;
9487 ha_set_tid(0);
9488 hlua_states[1] = hlua_init_state(1);
9489
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009490 /* Proxy and server configuration initialisation. */
William Lallemand6bb77b92021-07-28 15:48:16 +02009491 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg);
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009492 if (!socket_proxy) {
9493 fprintf(stderr, "Lua init: %s\n", errmsg);
9494 exit(1);
9495 }
9496 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009497
9498 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009499 socket_tcp = new_server(socket_proxy);
9500 if (!socket_tcp) {
9501 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
9502 exit(1);
9503 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009504
9505#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009506 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009507 socket_ssl = new_server(socket_proxy);
9508 if (!socket_ssl) {
9509 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
9510 exit(1);
9511 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009512
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009513 socket_ssl->use_ssl = 1;
9514 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009515
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009516 for (i = 0; args[i] != NULL; i++) {
9517 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009518 /*
9519 *
9520 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009521 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009522 * features like client certificates and ssl_verify.
9523 *
9524 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009525 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009526 if (tmp_error != 0) {
9527 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9528 abort(); /* This must be never arrives because the command line
9529 not editable by the user. */
9530 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009531 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009532 }
9533 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009534#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009535
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009536}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009537
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009538static void hlua_deinit()
9539{
Willy Tarreau186f3762020-12-04 11:48:12 +01009540 int thr;
9541
9542 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9543 if (hlua_states[thr])
9544 lua_close(hlua_states[thr]);
9545 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009546
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009547 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009548
Willy Tarreau0f143af2021-03-05 10:41:48 +01009549#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009550 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009551#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01009552
9553 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009554}
9555
9556REGISTER_POST_DEINIT(hlua_deinit);
9557
Willy Tarreau80713382018-11-26 10:19:54 +01009558static void hlua_register_build_options(void)
9559{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009560 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009561
Willy Tarreaubb57d942016-12-21 19:04:56 +01009562 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9563 hap_register_build_opts(ptr, 1);
9564}
Willy Tarreau80713382018-11-26 10:19:54 +01009565
9566INITCALL0(STG_REGISTER, hlua_register_build_options);