blob: f2ed684a3b8770118d4e78e81c9d21034f866140 [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>
Christopher Faulet69c581a2021-05-31 08:54:04 +020036#include <haproxy/filters.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020037#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020038#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020039#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020040#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020041#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020043#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020044#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020045#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020046#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020047#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020048#include <haproxy/payload.h>
Willy Tarreau3d6ee402021-05-08 20:28:07 +020049#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020050#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020051#include <haproxy/sample.h>
Willy Tarreau198e92a2021-03-05 10:23:32 +010052#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020053#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020054#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020055#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020056#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020057#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020058#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020059#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020060#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020061#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020062#include <haproxy/xref.h>
63
Thierry FOURNIER380d0932015-01-23 14:27:52 +010064
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010065/* Lua uses longjmp to perform yield or throwing errors. This
66 * macro is used only for identifying the function that can
67 * not return because a longjmp is executed.
68 * __LJMP marks a prototype of hlua file that can use longjmp.
69 * WILL_LJMP() marks an lua function that will use longjmp.
70 * MAY_LJMP() marks an lua function that may use longjmp.
71 */
72#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020073#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010074#define MAY_LJMP(func) func
75
Thierry FOURNIERbabae282015-09-17 11:36:37 +020076/* This couple of function executes securely some Lua calls outside of
77 * the lua runtime environment. Each Lua call can return a longjmp
78 * if it encounter a memory error.
79 *
80 * Lua documentation extract:
81 *
82 * If an error happens outside any protected environment, Lua calls
83 * a panic function (see lua_atpanic) and then calls abort, thus
84 * exiting the host application. Your panic function can avoid this
85 * exit by never returning (e.g., doing a long jump to your own
86 * recovery point outside Lua).
87 *
88 * The panic function runs as if it were a message handler (see
89 * §2.3); in particular, the error message is at the top of the
90 * stack. However, there is no guarantee about stack space. To push
91 * anything on the stack, the panic function must first check the
92 * available space (see §4.2).
93 *
94 * We must check all the Lua entry point. This includes:
95 * - The include/proto/hlua.h exported functions
96 * - the task wrapper function
97 * - The action wrapper function
98 * - The converters wrapper function
99 * - The sample-fetch wrapper functions
100 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500101 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800102 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200103 *
104 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
105 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
106 * because they must be exists in the program stack when the longjmp
107 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200108 *
109 * Note that the Lua processing is not really thread safe. It provides
110 * heavy system which consists to add our own lock function in the Lua
111 * code and recompile the library. This system will probably not accepted
112 * by maintainers of various distribs.
113 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500114 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200115 * quick looking on the Lua sources displays a lua_lock() a the start
116 * of function and a lua_unlock() at the end of the function. So I
117 * conclude that the Lua thread safe mode just perform a mutex around
118 * all execution. So I prefer to do this in the HAProxy code, it will be
119 * easier for distro maintainers.
120 *
121 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
122 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
123 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200124 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100125__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200126THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200127static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau6a510902021-07-14 19:41:25 +0200128static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200129
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100130/* This is the chained list of struct hlua_function referenced
131 * for haproxy action, sample-fetches, converters, cli and
132 * applet bindings. It is used for a post-initialisation control.
133 */
134static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions);
135
Thierry Fournierc7492592020-11-28 23:57:24 +0100136/* This variable is used only during initialization to identify the Lua state
137 * currently being initialized. 0 is the common lua state, 1 to n are the Lua
138 * states dedicated to each thread (in this case hlua_state_id==tid+1).
139 */
140static int hlua_state_id;
141
Thierry Fournier59f11be2020-11-29 00:37:41 +0100142/* This is a NULL-terminated list of lua file which are referenced to load per thread */
143static char **per_thread_load = NULL;
144
145lua_State *hlua_init_state(int thread_id);
146
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100147#define SET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200148 ({ \
149 int ret; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100150 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100151 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200152 if (setjmp(safe_ljmp_env) != 0) { \
153 lua_atpanic(__L, hlua_panic_safe); \
154 ret = 0; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100155 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100156 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200157 } else { \
158 lua_atpanic(__L, hlua_panic_ljmp); \
159 ret = 1; \
160 } \
161 ret; \
162 })
163
164/* If we are the last function catching Lua errors, we
165 * must reset the panic function.
166 */
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100167#define RESET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200168 do { \
169 lua_atpanic(__L, hlua_panic_safe); \
Thierry Fournier021d9862020-11-28 23:42:03 +0100170 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100171 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200172 } while(0)
173
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100174#define SET_SAFE_LJMP(__HLUA) \
175 SET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
176
177#define RESET_SAFE_LJMP(__HLUA) \
178 RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
179
180#define SET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100181 SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100182
183#define RESET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100184 RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100185
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200186/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200187#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100188/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200189#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200190/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100191#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100192#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200193
Thierry Fournierafc63e22020-11-28 17:06:51 +0100194/* The main Lua execution context. The 0 index is the
195 * common state shared by all threads.
196 */
Willy Tarreau186f3762020-12-04 11:48:12 +0100197static lua_State *hlua_states[MAX_THREADS + 1];
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100198
Christopher Faulet9f55a502020-02-25 15:21:02 +0100199#define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001
200
Christopher Faulet69c581a2021-05-31 08:54:04 +0200201struct hlua_reg_filter {
202 char *name;
203 int flt_ref[MAX_THREADS + 1];
204 int fun_ref[MAX_THREADS + 1];
205 struct list l;
206};
207
208struct hlua_flt_config {
209 struct hlua_reg_filter *reg;
210 int ref[MAX_THREADS + 1];
211 char **args;
212};
213
214struct hlua_flt_ctx {
215 int ref; /* ref to the filter lua object */
216 struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */
217 unsigned int cur_off[2]; /* current offset (0: request, 1: response) */
218 unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */
219 unsigned int flags; /* HLUA_FLT_CTX_FL_* */
220};
221
222DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx));
223
Christopher Faulet9f55a502020-02-25 15:21:02 +0100224static int hlua_filter_from_payload(struct filter *filter);
225
Christopher Faulet69c581a2021-05-31 08:54:04 +0200226/* This is the chained list of struct hlua_flt referenced
227 * for haproxy filters. It is used for a post-initialisation control.
228 */
229static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters);
230
231
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100232/* This is the memory pool containing struct lua for applets
233 * (including cli).
234 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100235DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100236
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100237/* Used for Socket connection. */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +0100238static struct proxy *socket_proxy;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100239static struct server *socket_tcp;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100240#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +0100241static struct server *socket_ssl;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100242#endif
243
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100244/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100245struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100246
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100247/* The following variables contains the reference of the different
248 * Lua classes. These references are useful for identify metadata
249 * associated with an object.
250 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100251static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100252static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100253static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100254static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100255static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100256static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200257static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200258static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200259static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100260static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100261
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100262/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200263 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100264 * short timeout. Lua linked with tasks doesn't have a timeout
265 * because a task may remain alive during all the haproxy execution.
266 */
267static unsigned int hlua_timeout_session = 4000; /* session timeout. */
268static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200269static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100270
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100271/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
272 * it is used for preventing infinite loops.
273 *
274 * I test the scheer with an infinite loop containing one incrementation
275 * and one test. I run this loop between 10 seconds, I raise a ceil of
276 * 710M loops from one interrupt each 9000 instructions, so I fix the value
277 * to one interrupt each 10 000 instructions.
278 *
279 * configured | Number of
280 * instructions | loops executed
281 * between two | in milions
282 * forced yields |
283 * ---------------+---------------
284 * 10 | 160
285 * 500 | 670
286 * 1000 | 680
287 * 5000 | 700
288 * 7000 | 700
289 * 8000 | 700
290 * 9000 | 710 <- ceil
291 * 10000 | 710
292 * 100000 | 710
293 * 1000000 | 710
294 *
295 */
296static unsigned int hlua_nb_instruction = 10000;
297
Willy Tarreaucdb53462020-12-02 12:12:00 +0100298/* Descriptor for the memory allocation state. The limit is pre-initialised to
299 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
300 * is replaced with ~0 during post_init after everything was loaded. This way
301 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
302 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100303 */
304struct hlua_mem_allocator {
305 size_t allocated;
306 size_t limit;
307};
308
Willy Tarreaucdb53462020-12-02 12:12:00 +0100309static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100310
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100311/* These functions converts types between HAProxy internal args or
312 * sample and LUA types. Another function permits to check if the
313 * LUA stack contains arguments according with an required ARG_T
314 * format.
315 */
316static int hlua_arg2lua(lua_State *L, const struct arg *arg);
317static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100318__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100319 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100320static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100321static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100322static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
323
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100324__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200325
Thierry Fournier59f11be2020-11-29 00:37:41 +0100326struct prepend_path {
327 struct list l;
328 char *type;
329 char *path;
330};
331
332static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
333
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200334#define SEND_ERR(__be, __fmt, __args...) \
335 do { \
336 send_log(__be, LOG_ERR, __fmt, ## __args); \
337 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100338 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200339 } while (0)
340
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100341static inline struct hlua_function *new_hlua_function()
342{
343 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100344 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100345
346 fcn = calloc(1, sizeof(*fcn));
347 if (!fcn)
348 return NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200349 LIST_APPEND(&referenced_functions, &fcn->l);
Thierry Fournierc7492592020-11-28 23:57:24 +0100350 for (i = 0; i < MAX_THREADS + 1; i++)
351 fcn->function_ref[i] = -1;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100352 return fcn;
353}
354
Christopher Fauletdda44442021-04-12 14:05:43 +0200355static inline void release_hlua_function(struct hlua_function *fcn)
356{
357 if (!fcn)
358 return;
359 if (fcn->name)
360 ha_free(&fcn->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200361 LIST_DELETE(&fcn->l);
Christopher Fauletdda44442021-04-12 14:05:43 +0200362 ha_free(&fcn);
363}
364
Thierry Fournierc7492592020-11-28 23:57:24 +0100365/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
366static inline int fcn_ref_to_stack_id(struct hlua_function *fcn)
367{
368 if (fcn->function_ref[0] == -1)
369 return tid + 1;
370 return 0;
371}
372
Christopher Faulet69c581a2021-05-31 08:54:04 +0200373/* Create a new registered filter. Only its name is filled */
374static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name)
375{
376 struct hlua_reg_filter *reg_flt;
377 int i;
378
379 reg_flt = calloc(1, sizeof(*reg_flt));
380 if (!reg_flt)
381 return NULL;
382 reg_flt->name = strdup(name);
383 if (!reg_flt->name) {
384 free(reg_flt);
385 return NULL;
386 }
387 LIST_APPEND(&referenced_filters, &reg_flt->l);
388 for (i = 0; i < MAX_THREADS + 1; i++) {
389 reg_flt->flt_ref[i] = -1;
390 reg_flt->fun_ref[i] = -1;
391 }
392 return reg_flt;
393}
394
395/* Release a registered filter */
396static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt)
397{
398 if (!reg_flt)
399 return;
400 if (reg_flt->name)
401 ha_free(&reg_flt->name);
402 LIST_DELETE(&reg_flt->l);
403 ha_free(&reg_flt);
404}
405
406/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
407static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt)
408{
409 if (reg_flt->fun_ref[0] == -1)
410 return tid + 1;
411 return 0;
412}
413
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100414/* Used to check an Lua function type in the stack. It creates and
415 * returns a reference of the function. This function throws an
416 * error if the rgument is not a "function".
417 */
418__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
419{
420 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100421 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100422 WILL_LJMP(luaL_argerror(L, argno, msg));
423 }
424 lua_pushvalue(L, argno);
425 return luaL_ref(L, LUA_REGISTRYINDEX);
426}
427
Christopher Fauletba9e21d2020-02-25 10:20:04 +0100428/* Used to check an Lua table type in the stack. It creates and
429 * returns a reference of the table. This function throws an
430 * error if the rgument is not a "table".
431 */
432__LJMP unsigned int hlua_checktable(lua_State *L, int argno)
433{
434 if (!lua_istable(L, argno)) {
435 const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno));
436 WILL_LJMP(luaL_argerror(L, argno, msg));
437 }
438 lua_pushvalue(L, argno);
439 return luaL_ref(L, LUA_REGISTRYINDEX);
440}
441
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200442/* Return the string that is of the top of the stack. */
443const char *hlua_get_top_error_string(lua_State *L)
444{
445 if (lua_gettop(L) < 1)
446 return "unknown error";
447 if (lua_type(L, -1) != LUA_TSTRING)
448 return "unknown error";
449 return lua_tostring(L, -1);
450}
451
Christopher Fauletd09cc512021-03-24 14:48:45 +0100452__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200453{
454 lua_Debug ar;
455 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200456 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200457
458 while (lua_getstack(L, level++, &ar)) {
459
460 /* Add separator */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100461 if (b_data(msg))
462 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200463
464 /* Fill fields:
465 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
466 * 'l': fills in the field currentline;
467 * 'n': fills in the field name and namewhat;
468 * 't': fills in the field istailcall;
469 */
470 lua_getinfo(L, "Slnt", &ar);
471
472 /* Append code localisation */
473 if (ar.currentline > 0)
Christopher Fauletd09cc512021-03-24 14:48:45 +0100474 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200475 else
Christopher Fauletd09cc512021-03-24 14:48:45 +0100476 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200477
478 /*
479 * Get function name
480 *
481 * if namewhat is no empty, name is defined.
482 * what contains "Lua" for Lua function, "C" for C function,
483 * or "main" for main code.
484 */
485 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100486 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200487
488 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100489 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200490
491 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Fauletd09cc512021-03-24 14:48:45 +0100492 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200493
494 else /* nothing left... */
495 chunk_appendf(msg, "?");
496
497
498 /* Display tailed call */
499 if (ar.istailcall)
500 chunk_appendf(msg, " ...");
501 }
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200504}
505
506
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100507/* This function check the number of arguments available in the
508 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500509 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100510 */
511__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
512{
513 if (lua_gettop(L) == nb)
514 return;
515 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
516}
517
Mark Lakes22154b42018-01-29 14:38:40 -0800518/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100519 * and the line number where the error is encountered.
520 */
521static int hlua_pusherror(lua_State *L, const char *fmt, ...)
522{
523 va_list argp;
524 va_start(argp, fmt);
525 luaL_where(L, 1);
526 lua_pushvfstring(L, fmt, argp);
527 va_end(argp);
528 lua_concat(L, 2);
529 return 1;
530}
531
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100532/* This functions is used with sample fetch and converters. It
533 * converts the HAProxy configuration argument in a lua stack
534 * values.
535 *
536 * It takes an array of "arg", and each entry of the array is
537 * converted and pushed in the LUA stack.
538 */
539static int hlua_arg2lua(lua_State *L, const struct arg *arg)
540{
541 switch (arg->type) {
542 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100543 case ARGT_TIME:
544 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100545 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100546 break;
547
548 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200549 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100550 break;
551
552 case ARGT_IPV4:
553 case ARGT_IPV6:
554 case ARGT_MSK4:
555 case ARGT_MSK6:
556 case ARGT_FE:
557 case ARGT_BE:
558 case ARGT_TAB:
559 case ARGT_SRV:
560 case ARGT_USR:
561 case ARGT_MAP:
562 default:
563 lua_pushnil(L);
564 break;
565 }
566 return 1;
567}
568
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500569/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500571 * with sample fetch wrappers. The input arguments are given to the
572 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100573 */
574static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
575{
576 switch (lua_type(L, ud)) {
577
578 case LUA_TNUMBER:
579 case LUA_TBOOLEAN:
580 arg->type = ARGT_SINT;
581 arg->data.sint = lua_tointeger(L, ud);
582 break;
583
584 case LUA_TSTRING:
585 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200586 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200587 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200588 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200589 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590 break;
591
592 case LUA_TUSERDATA:
593 case LUA_TNIL:
594 case LUA_TTABLE:
595 case LUA_TFUNCTION:
596 case LUA_TTHREAD:
597 case LUA_TLIGHTUSERDATA:
598 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200599 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100600 break;
601 }
602 return 1;
603}
604
605/* the following functions are used to convert a struct sample
606 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500607 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100608 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100609static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200611 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100612 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100613 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200614 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100615 break;
616
617 case SMP_T_BIN:
618 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200619 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100620 break;
621
622 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200623 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100624 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
625 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
626 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
627 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
628 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
629 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
630 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
631 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
632 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100634 break;
635 default:
636 lua_pushnil(L);
637 break;
638 }
639 break;
640
641 case SMP_T_IPV4:
642 case SMP_T_IPV6:
643 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200644 if (sample_casts[smp->data.type][SMP_T_STR] &&
645 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200646 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100647 else
648 lua_pushnil(L);
649 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100650 default:
651 lua_pushnil(L);
652 break;
653 }
654 return 1;
655}
656
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100657/* the following functions are used to convert a struct sample
658 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500659 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100660 */
661static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
662{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200663 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100664
665 case SMP_T_BIN:
666 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200667 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100668 break;
669
670 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200671 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100672 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
673 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
674 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
675 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
676 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
677 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
678 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
679 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
680 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200681 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100682 break;
683 default:
684 lua_pushstring(L, "");
685 break;
686 }
687 break;
688
689 case SMP_T_SINT:
690 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100691 case SMP_T_IPV4:
692 case SMP_T_IPV6:
693 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200694 if (sample_casts[smp->data.type][SMP_T_STR] &&
695 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100697 else
698 lua_pushstring(L, "");
699 break;
700 default:
701 lua_pushstring(L, "");
702 break;
703 }
704 return 1;
705}
706
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100707/* the following functions are used to convert an Lua type in a
708 * struct sample. This is useful to provide data from a converter
709 * to the LUA code.
710 */
711static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
712{
713 switch (lua_type(L, ud)) {
714
715 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200716 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200717 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100718 break;
719
720
721 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200722 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200723 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100724 break;
725
726 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200727 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100728 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200729 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200730 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200731 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200732 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100733 break;
734
735 case LUA_TUSERDATA:
736 case LUA_TNIL:
737 case LUA_TTABLE:
738 case LUA_TFUNCTION:
739 case LUA_TTHREAD:
740 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200741 case LUA_TNONE:
742 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200743 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200744 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100745 break;
746 }
747 return 1;
748}
749
Ilya Shipitsind4259502020-04-08 01:07:56 +0500750/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800751 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100752 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100753 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500754 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Willy Tarreauee0d7272021-07-16 10:26:56 +0200755 * entries and that there is at least one stop at the last position.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100756 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100757__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100758 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100759{
760 int min_arg;
Willy Tarreauee0d7272021-07-16 10:26:56 +0200761 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100762 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200763 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200764 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200765 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200766 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100767
768 idx = 0;
769 min_arg = ARGM(mask);
770 mask >>= ARGM_BITS;
771
772 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200773 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100774
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100775 /* Check for mandatory arguments. */
776 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100777 if (idx < min_arg) {
778
779 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200780 if (idx > 0) {
781 msg = "Mandatory argument expected";
782 goto error;
783 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100784
785 /* If first argument have a certain type, some default values
786 * may be used. See the function smp_resolve_args().
787 */
788 switch (mask & ARGT_MASK) {
789
790 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200791 if (!(p->cap & PR_CAP_FE)) {
792 msg = "Mandatory argument expected";
793 goto error;
794 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100795 argp[idx].data.prx = p;
796 argp[idx].type = ARGT_FE;
797 argp[idx+1].type = ARGT_STOP;
798 break;
799
800 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200801 if (!(p->cap & PR_CAP_BE)) {
802 msg = "Mandatory argument expected";
803 goto error;
804 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100805 argp[idx].data.prx = p;
806 argp[idx].type = ARGT_BE;
807 argp[idx+1].type = ARGT_STOP;
808 break;
809
810 case ARGT_TAB:
811 argp[idx].data.prx = p;
812 argp[idx].type = ARGT_TAB;
813 argp[idx+1].type = ARGT_STOP;
814 break;
815
816 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200817 msg = "Mandatory argument expected";
818 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100819 break;
820 }
821 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200822 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100823 }
824
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500825 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100826 if ((mask & ARGT_MASK) == ARGT_STOP &&
827 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200828 msg = "Last argument expected";
829 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100830 }
831
832 if ((mask & ARGT_MASK) == ARGT_STOP &&
833 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200834 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100835 }
836
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200837 /* Convert some argument types. All string in argp[] are for not
838 * duplicated yet.
839 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100840 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100841 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200842 if (argp[idx].type != ARGT_SINT) {
843 msg = "integer expected";
844 goto error;
845 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100846 argp[idx].type = ARGT_SINT;
847 break;
848
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100849 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200850 if (argp[idx].type != ARGT_SINT) {
851 msg = "integer expected";
852 goto error;
853 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200854 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100855 break;
856
857 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200858 if (argp[idx].type != ARGT_SINT) {
859 msg = "integer expected";
860 goto error;
861 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200862 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100863 break;
864
865 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200866 if (argp[idx].type != ARGT_STR) {
867 msg = "string expected";
868 goto error;
869 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200870 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200871 if (!argp[idx].data.prx) {
872 msg = "frontend doesn't exist";
873 goto error;
874 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100875 argp[idx].type = ARGT_FE;
876 break;
877
878 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200879 if (argp[idx].type != ARGT_STR) {
880 msg = "string expected";
881 goto error;
882 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200883 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200884 if (!argp[idx].data.prx) {
885 msg = "backend doesn't exist";
886 goto error;
887 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100888 argp[idx].type = ARGT_BE;
889 break;
890
891 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200892 if (argp[idx].type != ARGT_STR) {
893 msg = "string expected";
894 goto error;
895 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200896 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200897 if (!argp[idx].data.t) {
898 msg = "table doesn't exist";
899 goto error;
900 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100901 argp[idx].type = ARGT_TAB;
902 break;
903
904 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200905 if (argp[idx].type != ARGT_STR) {
906 msg = "string expected";
907 goto error;
908 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200909 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100910 if (sname) {
911 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200912 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200913 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200914 if (!px) {
915 msg = "backend doesn't exist";
916 goto error;
917 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100918 }
919 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200920 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100921 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100922 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100923 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200924 if (!argp[idx].data.srv) {
925 msg = "server doesn't exist";
926 goto error;
927 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100928 argp[idx].type = ARGT_SRV;
929 break;
930
931 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200932 if (argp[idx].type != ARGT_STR) {
933 msg = "string expected";
934 goto error;
935 }
936 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
937 msg = "invalid IPv4 address";
938 goto error;
939 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100940 argp[idx].type = ARGT_IPV4;
941 break;
942
943 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200944 if (argp[idx].type == ARGT_SINT)
945 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
946 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200947 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
948 msg = "invalid IPv4 mask";
949 goto error;
950 }
951 }
952 else {
953 msg = "integer or string expected";
954 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200955 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100956 argp[idx].type = ARGT_MSK4;
957 break;
958
959 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200960 if (argp[idx].type != ARGT_STR) {
961 msg = "string expected";
962 goto error;
963 }
964 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
965 msg = "invalid IPv6 address";
966 goto error;
967 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100968 argp[idx].type = ARGT_IPV6;
969 break;
970
971 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200972 if (argp[idx].type == ARGT_SINT)
973 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
974 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200975 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
976 msg = "invalid IPv6 mask";
977 goto error;
978 }
979 }
980 else {
981 msg = "integer or string expected";
982 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200983 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100984 argp[idx].type = ARGT_MSK6;
985 break;
986
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200987 case ARGT_REG:
988 if (argp[idx].type != ARGT_STR) {
989 msg = "string expected";
990 goto error;
991 }
992 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
993 if (!reg) {
994 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
995 argp[idx].data.str.area, err);
996 free(err);
997 goto error;
998 }
999 argp[idx].type = ARGT_REG;
1000 argp[idx].data.reg = reg;
1001 break;
1002
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +01001003 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +02001004 if (argp[idx].type != ARGT_STR) {
1005 msg = "string expected";
1006 goto error;
1007 }
1008 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001009 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +02001010 ul = p->uri_auth->userlist;
1011 else
1012 ul = auth_find_userlist(argp[idx].data.str.area);
1013
1014 if (!ul) {
1015 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
1016 goto error;
1017 }
1018 argp[idx].type = ARGT_USR;
1019 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001020 break;
1021
1022 case ARGT_STR:
1023 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
1024 msg = "unable to duplicate string arg";
1025 goto error;
1026 }
1027 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001028 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001029
Christopher Fauletd25d9262020-08-06 11:04:46 +02001030 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +02001031 msg = "type not yet supported";
1032 goto error;
1033 break;
1034
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001035 }
1036
1037 /* Check for type of argument. */
1038 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001039 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
1040 arg_type_names[(mask & ARGT_MASK)],
1041 arg_type_names[argp[idx].type & ARGT_MASK]);
1042 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001043 }
1044
1045 /* Next argument. */
1046 mask >>= ARGT_BITS;
1047 idx++;
1048 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001049 return 0;
1050
1051 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02001052 free_args(argp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02001053 WILL_LJMP(luaL_argerror(L, first + idx, msg));
1054 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +01001055}
1056
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001058 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001059 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001060 *
1061 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001062 * - hlua_sethlua : create the association between hlua context and lua_state.
1063 */
1064static inline struct hlua *hlua_gethlua(lua_State *L)
1065{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001066 struct hlua **hlua = lua_getextraspace(L);
1067 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068}
1069static inline void hlua_sethlua(struct hlua *hlua)
1070{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +01001071 struct hlua **hlua_store = lua_getextraspace(hlua->T);
1072 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001073}
1074
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001075/* This function is used to send logs. It try to send on screen (stderr)
1076 * and on the default syslog server.
1077 */
1078static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
1079{
1080 struct tm tm;
1081 char *p;
1082
1083 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001084 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001085 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001086 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001087 /* Break the message if exceed the buffer size. */
1088 *(p-4) = ' ';
1089 *(p-3) = '.';
1090 *(p-2) = '.';
1091 *(p-1) = '.';
1092 break;
1093 }
Willy Tarreau90807112020-02-25 08:16:33 +01001094 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001095 *p = *msg;
1096 else
1097 *p = '.';
1098 }
1099 *p = '\0';
1100
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001101 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001102 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001103 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1104 return;
1105
Willy Tarreaua678b432015-08-28 10:14:59 +02001106 get_localtime(date.tv_sec, &tm);
1107 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001108 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001109 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001110 fflush(stderr);
1111 }
1112}
1113
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001114/* This function just ensure that the yield will be always
1115 * returned with a timeout and permit to set some flags
1116 */
1117__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001118 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001119{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001120 struct hlua *hlua;
1121
1122 /* Get hlua struct, or NULL if we execute from main lua state */
1123 hlua = hlua_gethlua(L);
1124 if (!hlua) {
1125 return;
1126 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001127
1128 /* Set the wake timeout. If timeout is required, we set
1129 * the expiration time.
1130 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001131 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001132
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001133 hlua->flags |= flags;
1134
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001135 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001136 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001137}
1138
Willy Tarreau87b09662015-04-03 00:22:06 +02001139/* This function initialises the Lua environment stored in the stream.
1140 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001141 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001142 *
1143 * This function is particular. it initialises a new Lua thread. If the
1144 * initialisation fails (example: out of memory error), the lua function
1145 * throws an error (longjmp).
1146 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001147 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001148 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001149 * threads appear, the safe environment set a lock to ensure only one
1150 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001151 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001152 *
1153 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001154 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001155 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001156 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001157 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001158 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001159 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001161int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162{
1163 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001164 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001165 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001166 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001167 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001168 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001169 if (!already_safe) {
1170 if (!SET_SAFE_LJMP_PARENT(lua)) {
1171 lua->Tref = LUA_REFNIL;
1172 return 0;
1173 }
1174 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001175 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001176 if (!lua->T) {
1177 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001178 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001179 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001180 return 0;
1181 }
1182 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001183 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001184 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001185 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001186 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001187 return 1;
1188}
1189
Willy Tarreau87b09662015-04-03 00:22:06 +02001190/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001191 * is destroyed. The destroy also the memory context. The struct "lua"
1192 * is not freed.
1193 */
1194void hlua_ctx_destroy(struct hlua *lua)
1195{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001196 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001197 return;
1198
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001199 if (!lua->T)
1200 goto end;
1201
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001202 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001203 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001204
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001205 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001206 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001208 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001209
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001210 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001211 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001212 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001213 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001214 /* Forces a garbage collecting process. If the Lua program is finished
1215 * without error, we run the GC on the thread pointer. Its freed all
1216 * the unused memory.
1217 * If the thread is finnish with an error or is currently yielded,
1218 * it seems that the GC applied on the thread doesn't clean anything,
1219 * so e run the GC on the main thread.
1220 * NOTE: maybe this action locks all the Lua threads untiml the en of
1221 * the garbage collection.
1222 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001223 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001224 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001225 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001226 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001227 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001228 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001229
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001230 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001231
1232end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001233 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001234}
1235
1236/* This function is used to restore the Lua context when a coroutine
1237 * fails. This function copy the common memory between old coroutine
1238 * and the new coroutine. The old coroutine is destroyed, and its
1239 * replaced by the new coroutine.
1240 * If the flag "keep_msg" is set, the last entry of the old is assumed
1241 * as string error message and it is copied in the new stack.
1242 */
1243static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1244{
1245 lua_State *T;
1246 int new_ref;
1247
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001248 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001249 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001250 if (!T)
1251 return 0;
1252
1253 /* Copy last error message. */
1254 if (keep_msg)
1255 lua_xmove(lua->T, T, 1);
1256
1257 /* Copy data between the coroutines. */
1258 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1259 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001260 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001261
1262 /* Destroy old data. */
1263 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1264
1265 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001266 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001267
1268 /* Fill the struct with the new coroutine values. */
1269 lua->Mref = new_ref;
1270 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001271 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001272
1273 /* Set context. */
1274 hlua_sethlua(lua);
1275
1276 return 1;
1277}
1278
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001279void hlua_hook(lua_State *L, lua_Debug *ar)
1280{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001281 struct hlua *hlua;
1282
1283 /* Get hlua struct, or NULL if we execute from main lua state */
1284 hlua = hlua_gethlua(L);
1285 if (!hlua)
1286 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001287
1288 /* Lua cannot yield when its returning from a function,
1289 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001290 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001291 */
1292 if (lua_gethookmask(L) & LUA_MASKRET) {
1293 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1294 return;
1295 }
1296
1297 /* restore the interrupt condition. */
1298 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1299
1300 /* If we interrupt the Lua processing in yieldable state, we yield.
1301 * If the state is not yieldable, trying yield causes an error.
1302 */
1303 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001304 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001305
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001306 /* If we cannot yield, update the clock and check the timeout. */
1307 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001308 hlua->run_time += now_ms - hlua->start_time;
1309 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001310 lua_pushfstring(L, "execution timeout");
1311 WILL_LJMP(lua_error(L));
1312 }
1313
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001314 /* Update the start time. */
1315 hlua->start_time = now_ms;
1316
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001317 /* Try to interrupt the process at the end of the current
1318 * unyieldable function.
1319 */
1320 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001321}
1322
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001323/* This function start or resumes the Lua stack execution. If the flag
1324 * "yield_allowed" if no set and the LUA stack execution returns a yield
1325 * The function return an error.
1326 *
1327 * The function can returns 4 values:
1328 * - HLUA_E_OK : The execution is terminated without any errors.
1329 * - HLUA_E_AGAIN : The execution must continue at the next associated
1330 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001331 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001332 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001333 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001334 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001335 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001336 * LUA code.
1337 */
1338static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1339{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001340#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1341 int nres;
1342#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001343 int ret;
1344 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001345 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001346
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001347 /* Initialise run time counter. */
1348 if (!HLUA_IS_RUNNING(lua))
1349 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001350
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001351 /* Lock the whole Lua execution. This lock must be before the
1352 * label "resume_execution".
1353 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001354 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001355 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001356
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001357resume_execution:
1358
1359 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1360 * instructions. it is used for preventing infinite loops.
1361 */
1362 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1363
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001364 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001365 HLUA_SET_RUN(lua);
1366 HLUA_CLR_CTRLYIELD(lua);
1367 HLUA_CLR_WAKERESWR(lua);
1368 HLUA_CLR_WAKEREQWR(lua);
Christopher Faulet1f43a342021-08-04 17:58:21 +02001369 HLUA_CLR_NOYIELD(lua);
1370 if (!yield_allowed)
1371 HLUA_SET_NOYIELD(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001372
Christopher Fauletbc275a92020-02-26 14:55:16 +01001373 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001374 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001375 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001376
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001377 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001378#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001379 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001380#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001381 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001382#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001383 switch (ret) {
1384
1385 case LUA_OK:
1386 ret = HLUA_E_OK;
1387 break;
1388
1389 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001390 /* Check if the execution timeout is expired. It it is the case, we
1391 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001392 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001393 tv_update_date(0, 1);
1394 lua->run_time += now_ms - lua->start_time;
1395 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001396 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001397 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001398 break;
1399 }
1400 /* Process the forced yield. if the general yield is not allowed or
1401 * if no task were associated this the current Lua execution
1402 * coroutine, we resume the execution. Else we want to return in the
1403 * scheduler and we want to be waked up again, to continue the
1404 * current Lua execution. So we schedule our own task.
1405 */
1406 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001407 if (!yield_allowed || !lua->task)
1408 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001409 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001410 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001411 if (!yield_allowed) {
1412 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001413 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001414 break;
1415 }
1416 ret = HLUA_E_AGAIN;
1417 break;
1418
1419 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001420
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001421 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001422 * because the errors ares the only one mean to return immediately
1423 * from and lua execution.
1424 */
1425 if (lua->flags & HLUA_EXIT) {
1426 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001427 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001428 break;
1429 }
1430
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001431 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001432 if (!lua_checkstack(lua->T, 1)) {
1433 ret = HLUA_E_ERR;
1434 break;
1435 }
1436 msg = lua_tostring(lua->T, -1);
1437 lua_settop(lua->T, 0); /* Empty the stack. */
1438 lua_pop(lua->T, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01001439 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001440 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001441 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001442 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001443 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001444 ret = HLUA_E_ERRMSG;
1445 break;
1446
1447 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001448 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001449 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001450 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001451 break;
1452
1453 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001454 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001455 if (!lua_checkstack(lua->T, 1)) {
1456 ret = HLUA_E_ERR;
1457 break;
1458 }
1459 msg = lua_tostring(lua->T, -1);
1460 lua_settop(lua->T, 0); /* Empty the stack. */
1461 lua_pop(lua->T, 1);
1462 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001463 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001464 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001465 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001466 ret = HLUA_E_ERRMSG;
1467 break;
1468
1469 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001470 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001471 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001472 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001473 break;
1474 }
1475
1476 switch (ret) {
1477 case HLUA_E_AGAIN:
1478 break;
1479
1480 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001481 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001482 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001483 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001484 break;
1485
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001486 case HLUA_E_ETMOUT:
1487 case HLUA_E_NOMEM:
1488 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001489 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001490 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001491 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001492 hlua_ctx_renew(lua, 0);
1493 break;
1494
1495 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001496 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001497 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001498 break;
1499 }
1500
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001501 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001502 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001503 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001504
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001505 return ret;
1506}
1507
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001508/* This function exit the current code. */
1509__LJMP static int hlua_done(lua_State *L)
1510{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001511 struct hlua *hlua;
1512
1513 /* Get hlua struct, or NULL if we execute from main lua state */
1514 hlua = hlua_gethlua(L);
1515 if (!hlua)
1516 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001517
1518 hlua->flags |= HLUA_EXIT;
1519 WILL_LJMP(lua_error(L));
1520
1521 return 0;
1522}
1523
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001524/* This function is an LUA binding. It provides a function
1525 * for deleting ACL from a referenced ACL file.
1526 */
1527__LJMP static int hlua_del_acl(lua_State *L)
1528{
1529 const char *name;
1530 const char *key;
1531 struct pat_ref *ref;
1532
1533 MAY_LJMP(check_args(L, 2, "del_acl"));
1534
1535 name = MAY_LJMP(luaL_checkstring(L, 1));
1536 key = MAY_LJMP(luaL_checkstring(L, 2));
1537
1538 ref = pat_ref_lookup(name);
1539 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001540 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001541
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001542 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001543 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001544 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001545 return 0;
1546}
1547
1548/* This function is an LUA binding. It provides a function
1549 * for deleting map entry from a referenced map file.
1550 */
1551static int hlua_del_map(lua_State *L)
1552{
1553 const char *name;
1554 const char *key;
1555 struct pat_ref *ref;
1556
1557 MAY_LJMP(check_args(L, 2, "del_map"));
1558
1559 name = MAY_LJMP(luaL_checkstring(L, 1));
1560 key = MAY_LJMP(luaL_checkstring(L, 2));
1561
1562 ref = pat_ref_lookup(name);
1563 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001564 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001565
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001566 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001567 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001568 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001569 return 0;
1570}
1571
1572/* This function is an LUA binding. It provides a function
1573 * for adding ACL pattern from a referenced ACL file.
1574 */
1575static int hlua_add_acl(lua_State *L)
1576{
1577 const char *name;
1578 const char *key;
1579 struct pat_ref *ref;
1580
1581 MAY_LJMP(check_args(L, 2, "add_acl"));
1582
1583 name = MAY_LJMP(luaL_checkstring(L, 1));
1584 key = MAY_LJMP(luaL_checkstring(L, 2));
1585
1586 ref = pat_ref_lookup(name);
1587 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001588 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001589
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001590 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001591 if (pat_ref_find_elt(ref, key) == NULL)
1592 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001593 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001594 return 0;
1595}
1596
1597/* This function is an LUA binding. It provides a function
1598 * for setting map pattern and sample from a referenced map
1599 * file.
1600 */
1601static int hlua_set_map(lua_State *L)
1602{
1603 const char *name;
1604 const char *key;
1605 const char *value;
1606 struct pat_ref *ref;
1607
1608 MAY_LJMP(check_args(L, 3, "set_map"));
1609
1610 name = MAY_LJMP(luaL_checkstring(L, 1));
1611 key = MAY_LJMP(luaL_checkstring(L, 2));
1612 value = MAY_LJMP(luaL_checkstring(L, 3));
1613
1614 ref = pat_ref_lookup(name);
1615 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001616 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001617
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001618 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001619 if (pat_ref_find_elt(ref, key) != NULL)
1620 pat_ref_set(ref, key, value, NULL);
1621 else
1622 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001623 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001624 return 0;
1625}
1626
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001627/* A class is a lot of memory that contain data. This data can be a table,
1628 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001629 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001630 * the name of the object (_G[<name>] = <metable> ).
1631 *
1632 * A metable is a table that modify the standard behavior of a standard
1633 * access to the associated data. The entries of this new metatable are
1634 * defined as is:
1635 *
1636 * http://lua-users.org/wiki/MetatableEvents
1637 *
1638 * __index
1639 *
1640 * we access an absent field in a table, the result is nil. This is
1641 * true, but it is not the whole truth. Actually, such access triggers
1642 * the interpreter to look for an __index metamethod: If there is no
1643 * such method, as usually happens, then the access results in nil;
1644 * otherwise, the metamethod will provide the result.
1645 *
1646 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1647 * the key does not appear in the table, but the metatable has an __index
1648 * property:
1649 *
1650 * - if the value is a function, the function is called, passing in the
1651 * table and the key; the return value of that function is returned as
1652 * the result.
1653 *
1654 * - if the value is another table, the value of the key in that table is
1655 * asked for and returned (and if it doesn't exist in that table, but that
1656 * table's metatable has an __index property, then it continues on up)
1657 *
1658 * - Use "rawget(myTable,key)" to skip this metamethod.
1659 *
1660 * http://www.lua.org/pil/13.4.1.html
1661 *
1662 * __newindex
1663 *
1664 * Like __index, but control property assignment.
1665 *
1666 * __mode - Control weak references. A string value with one or both
1667 * of the characters 'k' and 'v' which specifies that the the
1668 * keys and/or values in the table are weak references.
1669 *
1670 * __call - Treat a table like a function. When a table is followed by
1671 * parenthesis such as "myTable( 'foo' )" and the metatable has
1672 * a __call key pointing to a function, that function is invoked
1673 * (passing any specified arguments) and the return value is
1674 * returned.
1675 *
1676 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1677 * called, if the metatable for myTable has a __metatable
1678 * key, the value of that key is returned instead of the
1679 * actual metatable.
1680 *
1681 * __tostring - Control string representation. When the builtin
1682 * "tostring( myTable )" function is called, if the metatable
1683 * for myTable has a __tostring property set to a function,
1684 * that function is invoked (passing myTable to it) and the
1685 * return value is used as the string representation.
1686 *
1687 * __len - Control table length. When the table length is requested using
1688 * the length operator ( '#' ), if the metatable for myTable has
1689 * a __len key pointing to a function, that function is invoked
1690 * (passing myTable to it) and the return value used as the value
1691 * of "#myTable".
1692 *
1693 * __gc - Userdata finalizer code. When userdata is set to be garbage
1694 * collected, if the metatable has a __gc field pointing to a
1695 * function, that function is first invoked, passing the userdata
1696 * to it. The __gc metamethod is not called for tables.
1697 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1698 *
1699 * Special metamethods for redefining standard operators:
1700 * http://www.lua.org/pil/13.1.html
1701 *
1702 * __add "+"
1703 * __sub "-"
1704 * __mul "*"
1705 * __div "/"
1706 * __unm "!"
1707 * __pow "^"
1708 * __concat ".."
1709 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001710 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001711 * http://www.lua.org/pil/13.2.html
1712 *
1713 * __eq "=="
1714 * __lt "<"
1715 * __le "<="
1716 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001717
1718/*
1719 *
1720 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001721 * Class Map
1722 *
1723 *
1724 */
1725
1726/* Returns a struct hlua_map if the stack entry "ud" is
1727 * a class session, otherwise it throws an error.
1728 */
1729__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1730{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001731 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001732}
1733
1734/* This function is the map constructor. It don't need
1735 * the class Map object. It creates and return a new Map
1736 * object. It must be called only during "body" or "init"
1737 * context because it process some filesystem accesses.
1738 */
1739__LJMP static int hlua_map_new(struct lua_State *L)
1740{
1741 const char *fn;
1742 int match = PAT_MATCH_STR;
1743 struct sample_conv conv;
1744 const char *file = "";
1745 int line = 0;
1746 lua_Debug ar;
1747 char *err = NULL;
1748 struct arg args[2];
1749
1750 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1751 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1752
1753 fn = MAY_LJMP(luaL_checkstring(L, 1));
1754
1755 if (lua_gettop(L) >= 2) {
1756 match = MAY_LJMP(luaL_checkinteger(L, 2));
1757 if (match < 0 || match >= PAT_MATCH_NUM)
1758 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1759 }
1760
1761 /* Get Lua filename and line number. */
1762 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1763 lua_getinfo(L, "Sl", &ar); /* get info about it */
1764 if (ar.currentline > 0) { /* is there info? */
1765 file = ar.short_src;
1766 line = ar.currentline;
1767 }
1768 }
1769
1770 /* fill fake sample_conv struct. */
1771 conv.kw = ""; /* unused. */
1772 conv.process = NULL; /* unused. */
1773 conv.arg_mask = 0; /* unused. */
1774 conv.val_args = NULL; /* unused. */
1775 conv.out_type = SMP_T_STR;
1776 conv.private = (void *)(long)match;
1777 switch (match) {
1778 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1779 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1780 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1781 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1782 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1783 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1784 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001785 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001786 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1787 default:
1788 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1789 }
1790
1791 /* fill fake args. */
1792 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001793 args[0].data.str.area = strdup(fn);
1794 args[0].data.str.data = strlen(fn);
1795 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001796 args[1].type = ARGT_STOP;
1797
1798 /* load the map. */
1799 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001800 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001801 * free the err variable.
1802 */
1803 luaL_where(L, 1);
1804 lua_pushfstring(L, "'new': %s.", err);
1805 lua_concat(L, 2);
1806 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001807 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001808 WILL_LJMP(lua_error(L));
1809 }
1810
1811 /* create the lua object. */
1812 lua_newtable(L);
1813 lua_pushlightuserdata(L, args[0].data.map);
1814 lua_rawseti(L, -2, 0);
1815
1816 /* Pop a class Map metatable and affect it to the userdata. */
1817 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1818 lua_setmetatable(L, -2);
1819
1820
1821 return 1;
1822}
1823
1824__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1825{
1826 struct map_descriptor *desc;
1827 struct pattern *pat;
1828 struct sample smp;
1829
1830 MAY_LJMP(check_args(L, 2, "lookup"));
1831 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001832 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001833 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001834 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001835 }
1836 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001837 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001838 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001839 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 +01001840 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001841 }
1842
1843 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001844 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001845 if (str)
1846 lua_pushstring(L, "");
1847 else
1848 lua_pushnil(L);
1849 return 1;
1850 }
1851
1852 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001853 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001854 return 1;
1855}
1856
1857__LJMP static int hlua_map_lookup(struct lua_State *L)
1858{
1859 return _hlua_map_lookup(L, 0);
1860}
1861
1862__LJMP static int hlua_map_slookup(struct lua_State *L)
1863{
1864 return _hlua_map_lookup(L, 1);
1865}
1866
1867/*
1868 *
1869 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001870 * Class Socket
1871 *
1872 *
1873 */
1874
1875__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1876{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001877 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001878}
1879
1880/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001881 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001882 * received.
1883 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001884static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001886 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001887
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001888 if (appctx->ctx.hlua_cosocket.die) {
1889 si_shutw(si);
1890 si_shutr(si);
1891 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001892 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1893 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001894 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1895 }
1896
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001897 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001898 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001899 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001900
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001901 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001902 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001903 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001904
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001905 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001906 * to be notified whenever the connection completes.
1907 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001908 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001909 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001910 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001911 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001912 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001913 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914
1915 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001916 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001917
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001918 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001919 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001920 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921
1922 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001923 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001924 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001925
1926 /* Some data were injected in the buffer, notify the stream
1927 * interface.
1928 */
1929 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001930 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001931
1932 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001933 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001934 */
1935 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001936 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001937}
1938
Willy Tarreau87b09662015-04-03 00:22:06 +02001939/* This function is called when the "struct stream" is destroyed.
1940 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001941 * Wake all the pending signals.
1942 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001943static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001944{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001945 struct xref *peer;
1946
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001948 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1949 if (peer)
1950 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001951
1952 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001953 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1954 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001955}
1956
1957/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001958 * uses this object. If the stream does not exists, just quit.
1959 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 * pending signal can rest in the read and write lists. destroy
1961 * it.
1962 */
1963__LJMP static int hlua_socket_gc(lua_State *L)
1964{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001965 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001966 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001967 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001968
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001969 MAY_LJMP(check_args(L, 1, "__gc"));
1970
1971 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001972 peer = xref_get_peer_and_lock(&socket->xref);
1973 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001975 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001977 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001978 appctx->ctx.hlua_cosocket.die = 1;
1979 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001981 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001982 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001983 return 0;
1984}
1985
1986/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001987 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988 */
sada05ed3302018-05-11 11:48:18 -07001989__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001990{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001991 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001992 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001993 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001994 struct hlua *hlua;
1995
1996 /* Get hlua struct, or NULL if we execute from main lua state */
1997 hlua = hlua_gethlua(L);
1998 if (!hlua)
1999 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002001 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002002
2003 /* Check if we run on the same thread than the xreator thread.
2004 * We cannot access to the socket if the thread is different.
2005 */
2006 if (socket->tid != tid)
2007 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2008
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002009 peer = xref_get_peer_and_lock(&socket->xref);
2010 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01002012
2013 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002014 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002015
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002016 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002017 appctx->ctx.hlua_cosocket.die = 1;
2018 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002019
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002020 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002021 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002022 return 0;
2023}
2024
sada05ed3302018-05-11 11:48:18 -07002025/* The close function calls close_helper.
2026 */
2027__LJMP static int hlua_socket_close(lua_State *L)
2028{
2029 MAY_LJMP(check_args(L, 1, "close"));
2030 return hlua_socket_close_helper(L);
2031}
2032
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002033/* This Lua function assumes that the stack contain three parameters.
2034 * 1 - USERDATA containing a struct socket
2035 * 2 - INTEGER with values of the macro defined below
2036 * If the integer is -1, we must read at most one line.
2037 * If the integer is -2, we ust read all the data until the
2038 * end of the stream.
2039 * If the integer is positive value, we must read a number of
2040 * bytes corresponding to this value.
2041 */
2042#define HLSR_READ_LINE (-1)
2043#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002044__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045{
2046 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2047 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002048 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002050 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02002052 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002053 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02002054 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002055 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002056 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01002057 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002058 struct stream_interface *si;
2059 struct stream *s;
2060 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002061 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002062
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002063 /* Get hlua struct, or NULL if we execute from main lua state */
2064 hlua = hlua_gethlua(L);
2065
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066 /* Check if this lua stack is schedulable. */
2067 if (!hlua || !hlua->task)
2068 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
2069 "'frontend', 'backend' or 'task'"));
2070
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002071 /* Check if we run on the same thread than the xreator thread.
2072 * We cannot access to the socket if the thread is different.
2073 */
2074 if (socket->tid != tid)
2075 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2076
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002077 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002078 peer = xref_get_peer_and_lock(&socket->xref);
2079 if (!peer)
2080 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002081 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2082 si = appctx->owner;
2083 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002085 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002088 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002089 if (nblk < 0) /* Connection close. */
2090 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002091 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002092 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002093
2094 /* remove final \r\n. */
2095 if (nblk == 1) {
2096 if (blk1[len1-1] == '\n') {
2097 len1--;
2098 skip_at_end++;
2099 if (blk1[len1-1] == '\r') {
2100 len1--;
2101 skip_at_end++;
2102 }
2103 }
2104 }
2105 else {
2106 if (blk2[len2-1] == '\n') {
2107 len2--;
2108 skip_at_end++;
2109 if (blk2[len2-1] == '\r') {
2110 len2--;
2111 skip_at_end++;
2112 }
2113 }
2114 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002115 }
2116
2117 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002118 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002119 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002120 if (nblk < 0) /* Connection close. */
2121 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002122 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002123 goto connection_empty;
2124 }
2125
2126 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002128 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002129 if (nblk < 0) /* Connection close. */
2130 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002131 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002132 goto connection_empty;
2133
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002134 missing_bytes = wanted - socket->b.n;
2135 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002136 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002137 len1 = missing_bytes;
2138 } if (nblk == 2 && len1 + len2 > missing_bytes)
2139 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002140 }
2141
2142 len = len1;
2143
2144 luaL_addlstring(&socket->b, blk1, len1);
2145 if (nblk == 2) {
2146 len += len2;
2147 luaL_addlstring(&socket->b, blk2, len2);
2148 }
2149
2150 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002151 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002152
2153 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002154 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002155
2156 /* If the pattern reclaim to read all the data
2157 * in the connection, got out.
2158 */
2159 if (wanted == HLSR_READ_ALL)
2160 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002161 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002162 goto connection_empty;
2163
2164 /* Return result. */
2165 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002166 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002167 return 1;
2168
2169connection_closed:
2170
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002171 xref_unlock(&socket->xref, peer);
2172
2173no_peer:
2174
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175 /* If the buffer containds data. */
2176 if (socket->b.n > 0) {
2177 luaL_pushresult(&socket->b);
2178 return 1;
2179 }
2180 lua_pushnil(L);
2181 lua_pushstring(L, "connection closed.");
2182 return 2;
2183
2184connection_empty:
2185
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002186 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2187 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002188 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002189 }
2190 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002191 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002192 return 0;
2193}
2194
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002195/* This Lua function gets two parameters. The first one can be string
2196 * or a number. If the string is "*l", the user requires one line. If
2197 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198 * If the value is a number, the user require a number of bytes equal
2199 * to the value. The default value is "*l" (a line).
2200 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002201 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202 * integer takes this values:
2203 * -1 : read a line
2204 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002205 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002206 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002207 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002208 * concatenated with the read data.
2209 */
2210__LJMP static int hlua_socket_receive(struct lua_State *L)
2211{
2212 int wanted = HLSR_READ_LINE;
2213 const char *pattern;
Christopher Fauletc31b2002021-05-03 10:11:13 +02002214 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002215 char *error;
2216 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002217 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002218
2219 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2220 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2221
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002222 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002223
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002224 /* Check if we run on the same thread than the xreator thread.
2225 * We cannot access to the socket if the thread is different.
2226 */
2227 if (socket->tid != tid)
2228 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2229
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230 /* check for pattern. */
2231 if (lua_gettop(L) >= 2) {
2232 type = lua_type(L, 2);
2233 if (type == LUA_TSTRING) {
2234 pattern = lua_tostring(L, 2);
2235 if (strcmp(pattern, "*a") == 0)
2236 wanted = HLSR_READ_ALL;
2237 else if (strcmp(pattern, "*l") == 0)
2238 wanted = HLSR_READ_LINE;
2239 else {
2240 wanted = strtoll(pattern, &error, 10);
2241 if (*error != '\0')
2242 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2243 }
2244 }
2245 else if (type == LUA_TNUMBER) {
2246 wanted = lua_tointeger(L, 2);
2247 if (wanted < 0)
2248 WILL_LJMP(luaL_error(L, "Unsupported size."));
2249 }
2250 }
2251
2252 /* Set pattern. */
2253 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002254
2255 /* Check if we would replace the top by itself. */
2256 if (lua_gettop(L) != 2)
2257 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258
Christopher Fauletc31b2002021-05-03 10:11:13 +02002259 /* Save index of the top of the stack because since buffers are used, it
2260 * may change
2261 */
2262 lastarg = lua_gettop(L);
2263
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002264 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265 luaL_buffinit(L, &socket->b);
2266
2267 /* Check prefix. */
Christopher Fauletc31b2002021-05-03 10:11:13 +02002268 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002269 if (lua_type(L, 3) != LUA_TSTRING)
2270 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2271 pattern = lua_tolstring(L, 3, &len);
2272 luaL_addlstring(&socket->b, pattern, len);
2273 }
2274
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002275 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002276}
2277
2278/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002279 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002281static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002282{
2283 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002284 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285 struct appctx *appctx;
2286 size_t buf_len;
2287 const char *buf;
2288 int len;
2289 int send_len;
2290 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002291 struct xref *peer;
2292 struct stream_interface *si;
2293 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002295 /* Get hlua struct, or NULL if we execute from main lua state */
2296 hlua = hlua_gethlua(L);
2297
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 /* Check if this lua stack is schedulable. */
2299 if (!hlua || !hlua->task)
2300 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2301 "'frontend', 'backend' or 'task'"));
2302
2303 /* Get object */
2304 socket = MAY_LJMP(hlua_checksocket(L, 1));
2305 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002306 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002308 /* Check if we run on the same thread than the xreator thread.
2309 * We cannot access to the socket if the thread is different.
2310 */
2311 if (socket->tid != tid)
2312 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2313
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002314 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002315 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002316 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002317 lua_pushinteger(L, -1);
2318 return 1;
2319 }
2320 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2321 si = appctx->owner;
2322 s = si_strm(si);
2323
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002324 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002325 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002326 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002327 lua_pushinteger(L, -1);
2328 return 1;
2329 }
2330
2331 /* Update the input buffer data. */
2332 buf += sent;
2333 send_len = buf_len - sent;
2334
2335 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002336 if (sent >= buf_len) {
2337 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002338 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002339 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002341 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002342 * the request buffer if its not required.
2343 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002344 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002345 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002346 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002347 }
2348
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002349 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002350 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002351 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002353 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002354
2355 /* send data */
2356 if (len < send_len)
2357 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002358 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359
2360 /* "Not enough space" (-1), "Buffer too little to contain
2361 * the data" (-2) are not expected because the available length
2362 * is tested.
2363 * Other unknown error are also not expected.
2364 */
2365 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002366 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002367 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002368
sada05ed3302018-05-11 11:48:18 -07002369 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002370 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002371 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002372 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002373 return 1;
2374 }
2375
2376 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002377 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002378
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002379 s->req.rex = TICK_ETERNITY;
2380 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002381
2382 /* Update length sent. */
2383 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002384 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385
2386 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002387 if (sent + len >= buf_len) {
2388 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002390 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002391
2392hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002393 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2394 xref_unlock(&socket->xref, peer);
2395 WILL_LJMP(luaL_error(L, "out of memory"));
2396 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002397 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002398 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399 return 0;
2400}
2401
2402/* This function initiate the send of data. It just check the input
2403 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002404 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002405 * "hlua_socket_write_yield" that can yield.
2406 *
2407 * The Lua function gets between 3 and 4 parameters. The first one is
2408 * the associated object. The second is a string buffer. The third is
2409 * a facultative integer that represents where is the buffer position
2410 * of the start of the data that can send. The first byte is the
2411 * position "1". The default value is "1". The fourth argument is a
2412 * facultative integer that represents where is the buffer position
2413 * of the end of the data that can send. The default is the last byte.
2414 */
2415static int hlua_socket_send(struct lua_State *L)
2416{
2417 int i;
2418 int j;
2419 const char *buf;
2420 size_t buf_len;
2421
2422 /* Check number of arguments. */
2423 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2424 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2425
2426 /* Get the string. */
2427 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2428
2429 /* Get and check j. */
2430 if (lua_gettop(L) == 4) {
2431 j = MAY_LJMP(luaL_checkinteger(L, 4));
2432 if (j < 0)
2433 j = buf_len + j + 1;
2434 if (j > buf_len)
2435 j = buf_len + 1;
2436 lua_pop(L, 1);
2437 }
2438 else
2439 j = buf_len;
2440
2441 /* Get and check i. */
2442 if (lua_gettop(L) == 3) {
2443 i = MAY_LJMP(luaL_checkinteger(L, 3));
2444 if (i < 0)
2445 i = buf_len + i + 1;
2446 if (i > buf_len)
2447 i = buf_len + 1;
2448 lua_pop(L, 1);
2449 } else
2450 i = 1;
2451
2452 /* Check bth i and j. */
2453 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002454 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002455 return 1;
2456 }
2457 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002458 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002459 return 1;
2460 }
2461 if (i == 0)
2462 i = 1;
2463 if (j == 0)
2464 j = 1;
2465
2466 /* Pop the string. */
2467 lua_pop(L, 1);
2468
2469 /* Update the buffer length. */
2470 buf += i - 1;
2471 buf_len = j - i + 1;
2472 lua_pushlstring(L, buf, buf_len);
2473
2474 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002475 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002476
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002477 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002478}
2479
Willy Tarreau22b0a682015-06-17 19:43:49 +02002480#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002481__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2482{
2483 static char buffer[SOCKET_INFO_MAX_LEN];
2484 int ret;
2485 int len;
2486 char *p;
2487
2488 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2489 if (ret <= 0) {
2490 lua_pushnil(L);
2491 return 1;
2492 }
2493
2494 if (ret == AF_UNIX) {
2495 lua_pushstring(L, buffer+1);
2496 return 1;
2497 }
2498 else if (ret == AF_INET6) {
2499 buffer[0] = '[';
2500 len = strlen(buffer);
2501 buffer[len] = ']';
2502 len++;
2503 buffer[len] = ':';
2504 len++;
2505 p = buffer;
2506 }
2507 else if (ret == AF_INET) {
2508 p = buffer + 1;
2509 len = strlen(p);
2510 p[len] = ':';
2511 len++;
2512 }
2513 else {
2514 lua_pushnil(L);
2515 return 1;
2516 }
2517
2518 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2519 lua_pushnil(L);
2520 return 1;
2521 }
2522
2523 lua_pushstring(L, p);
2524 return 1;
2525}
2526
2527/* Returns information about the peer of the connection. */
2528__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2529{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002530 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002531 struct xref *peer;
2532 struct appctx *appctx;
2533 struct stream_interface *si;
2534 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002535 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002536
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002537 MAY_LJMP(check_args(L, 1, "getpeername"));
2538
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002539 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002540
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002541 /* Check if we run on the same thread than the xreator thread.
2542 * We cannot access to the socket if the thread is different.
2543 */
2544 if (socket->tid != tid)
2545 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2546
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002547 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002548 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002549 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002550 lua_pushnil(L);
2551 return 1;
2552 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002553 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2554 si = appctx->owner;
2555 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002556
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002557 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002558 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002559 lua_pushnil(L);
2560 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002561 }
2562
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002563 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002564 xref_unlock(&socket->xref, peer);
2565 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002566}
2567
2568/* Returns information about my connection side. */
2569static int hlua_socket_getsockname(struct lua_State *L)
2570{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002571 struct hlua_socket *socket;
2572 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002573 struct appctx *appctx;
2574 struct xref *peer;
2575 struct stream_interface *si;
2576 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002577 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002578
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002579 MAY_LJMP(check_args(L, 1, "getsockname"));
2580
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002581 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002582
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002583 /* Check if we run on the same thread than the xreator thread.
2584 * We cannot access to the socket if the thread is different.
2585 */
2586 if (socket->tid != tid)
2587 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2588
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002589 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002590 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002591 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002592 lua_pushnil(L);
2593 return 1;
2594 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002595 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2596 si = appctx->owner;
2597 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002599 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002600 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002601 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002602 lua_pushnil(L);
2603 return 1;
2604 }
2605
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002606 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002607 xref_unlock(&socket->xref, peer);
2608 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609}
2610
2611/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002612static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002613 .obj_type = OBJ_TYPE_APPLET,
2614 .name = "<LUA_TCP>",
2615 .fct = hlua_socket_handler,
2616 .release = hlua_socket_release,
2617};
2618
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002619__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620{
2621 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002622 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002623 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002624 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002625 struct stream_interface *si;
2626 struct stream *s;
2627
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002628 /* Get hlua struct, or NULL if we execute from main lua state */
2629 hlua = hlua_gethlua(L);
2630 if (!hlua)
2631 return 0;
2632
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002633 /* Check if we run on the same thread than the xreator thread.
2634 * We cannot access to the socket if the thread is different.
2635 */
2636 if (socket->tid != tid)
2637 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2638
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002639 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002640 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002641 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002642 lua_pushnil(L);
2643 lua_pushstring(L, "Can't connect");
2644 return 2;
2645 }
2646 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2647 si = appctx->owner;
2648 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002649
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002650 /* Check if we run on the same thread than the xreator thread.
2651 * We cannot access to the socket if the thread is different.
2652 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002653 if (socket->tid != tid) {
2654 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002655 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002656 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002657
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002659 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002660 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002661 lua_pushnil(L);
2662 lua_pushstring(L, "Can't connect");
2663 return 2;
2664 }
2665
Willy Tarreaue09101e2018-10-16 17:37:12 +02002666 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002667
2668 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002669 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002670 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002671 lua_pushinteger(L, 1);
2672 return 1;
2673 }
2674
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002675 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2676 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002677 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002678 }
2679 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002680 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002681 return 0;
2682}
2683
2684/* This function fail or initite the connection. */
2685__LJMP static int hlua_socket_connect(struct lua_State *L)
2686{
2687 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002688 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002689 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002690 struct hlua *hlua;
2691 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002692 int low, high;
2693 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002694 struct xref *peer;
2695 struct stream_interface *si;
2696 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002697
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002698 if (lua_gettop(L) < 2)
2699 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002700
2701 /* Get args. */
2702 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002703
2704 /* Check if we run on the same thread than the xreator thread.
2705 * We cannot access to the socket if the thread is different.
2706 */
2707 if (socket->tid != tid)
2708 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2709
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002711 if (lua_gettop(L) >= 3) {
2712 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002713 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002714
Tim Duesterhus6edab862018-01-06 19:04:45 +01002715 /* Force the ip to end with a colon, to support IPv6 addresses
2716 * that are not enclosed within square brackets.
2717 */
2718 if (port > 0) {
2719 luaL_buffinit(L, &b);
2720 luaL_addstring(&b, ip);
2721 luaL_addchar(&b, ':');
2722 luaL_pushresult(&b);
2723 ip = lua_tolstring(L, lua_gettop(L), NULL);
2724 }
2725 }
2726
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002727 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002728 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002729 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002730 lua_pushnil(L);
2731 return 1;
2732 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002733
2734 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002735 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 +02002736 if (!addr) {
2737 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002738 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002739 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002740
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002741 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002742 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002743 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002744 if (port == -1) {
2745 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002746 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002747 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002748 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2749 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002750 if (port == -1) {
2751 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002752 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002753 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002754 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002755 }
2756 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002757
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002758 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2759 si = appctx->owner;
2760 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002761
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002762 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002763 xref_unlock(&socket->xref, peer);
2764 WILL_LJMP(luaL_error(L, "connect: internal error"));
2765 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002766 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002767
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002768 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002769 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002770 if (!hlua)
2771 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002772
2773 /* inform the stream that we want to be notified whenever the
2774 * connection completes.
2775 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002776 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002777 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002778 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002779
Willy Tarreauf31af932020-01-14 09:59:38 +01002780 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002781
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002782 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2783 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002784 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002785 }
2786 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002787
2788 task_wakeup(s->task, TASK_WOKEN_INIT);
2789 /* Return yield waiting for connection. */
2790
Willy Tarreau9635e032018-10-16 17:52:55 +02002791 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002792
2793 return 0;
2794}
2795
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002796#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002797__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2798{
2799 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002800 struct xref *peer;
2801 struct appctx *appctx;
2802 struct stream_interface *si;
2803 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002804
2805 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2806 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002807
2808 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002809 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002810 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002811 lua_pushnil(L);
2812 return 1;
2813 }
2814 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2815 si = appctx->owner;
2816 s = si_strm(si);
2817
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002818 s->target = &socket_ssl->obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002819 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002820 return MAY_LJMP(hlua_socket_connect(L));
2821}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002822#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002823
2824__LJMP static int hlua_socket_setoption(struct lua_State *L)
2825{
2826 return 0;
2827}
2828
2829__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2830{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002831 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002832 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002833 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002834 struct xref *peer;
2835 struct appctx *appctx;
2836 struct stream_interface *si;
2837 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002838
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002839 MAY_LJMP(check_args(L, 2, "settimeout"));
2840
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002841 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002842
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002843 /* convert the timeout to millis */
2844 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002845
Thierry Fournier17a921b2018-03-08 09:59:02 +01002846 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002847 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002848 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2849
Mark Lakes56cc1252018-03-27 09:48:06 +02002850 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002851 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002852
2853 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002854 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002855 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002856
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002857 /* Check if we run on the same thread than the xreator thread.
2858 * We cannot access to the socket if the thread is different.
2859 */
2860 if (socket->tid != tid)
2861 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2862
Mark Lakes56cc1252018-03-27 09:48:06 +02002863 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002864 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002865 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002866 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2867 WILL_LJMP(lua_error(L));
2868 return 0;
2869 }
2870 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2871 si = appctx->owner;
2872 s = si_strm(si);
2873
Cyril Bonté7bb63452018-08-17 23:51:02 +02002874 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002875 s->req.rto = tmout;
2876 s->req.wto = tmout;
2877 s->res.rto = tmout;
2878 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002879 s->req.rex = tick_add_ifset(now_ms, tmout);
2880 s->req.wex = tick_add_ifset(now_ms, tmout);
2881 s->res.rex = tick_add_ifset(now_ms, tmout);
2882 s->res.wex = tick_add_ifset(now_ms, tmout);
2883
2884 s->task->expire = tick_add_ifset(now_ms, tmout);
2885 task_queue(s->task);
2886
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002887 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002888
Thierry Fourniere9636f12018-03-08 09:54:32 +01002889 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002890 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002891}
2892
2893__LJMP static int hlua_socket_new(lua_State *L)
2894{
2895 struct hlua_socket *socket;
2896 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002897 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002898 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002899
2900 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002901 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002902 hlua_pusherror(L, "socket: full stack");
2903 goto out_fail_conf;
2904 }
2905
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002906 /* Create the object: obj[0] = userdata. */
2907 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002908 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002909 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002910 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002911 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002912
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002913 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002914 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002915 hlua_pusherror(L, "socket: uninitialized pools.");
2916 goto out_fail_conf;
2917 }
2918
Willy Tarreau87b09662015-04-03 00:22:06 +02002919 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002920 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2921 lua_setmetatable(L, -2);
2922
Willy Tarreaud420a972015-04-06 00:39:18 +02002923 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002924 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002925 if (!appctx) {
2926 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002927 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002928 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002929
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002930 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002931 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002932 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2933 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002934
Willy Tarreaud420a972015-04-06 00:39:18 +02002935 /* Now create a session, task and stream for this applet */
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +01002936 sess = session_new(socket_proxy, NULL, &appctx->obj_type);
Willy Tarreaud420a972015-04-06 00:39:18 +02002937 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002938 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002939 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002940 }
2941
Christopher Faulet26256f82020-09-14 11:40:13 +02002942 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002943 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002944 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002945 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002946 }
2947
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002948 /* Initialise cross reference between stream and Lua socket object. */
2949 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002950
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002951 /* Configure "right" stream interface. this "si" is used to connect
2952 * and retrieve data from the server. The connection is initialized
2953 * with the "struct server".
2954 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002955 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002956
2957 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002958 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01002959 strm->target = &socket_tcp->obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002960
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002961 return 1;
2962
Willy Tarreaud420a972015-04-06 00:39:18 +02002963 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002964 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002965 out_fail_sess:
2966 appctx_free(appctx);
2967 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002968 WILL_LJMP(lua_error(L));
2969 return 0;
2970}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002971
2972/*
2973 *
2974 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002975 * Class Channel
2976 *
2977 *
2978 */
2979
2980/* Returns the struct hlua_channel join to the class channel in the
2981 * stack entry "ud" or throws an argument error.
2982 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002983__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002984{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002985 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986}
2987
Willy Tarreau47860ed2015-03-10 14:07:50 +01002988/* Pushes the channel onto the top of the stack. If the stask does not have a
2989 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002991static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002992{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002994 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002995 return 0;
2996
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002997 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002998 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002999 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000
3001 /* Pop a class sesison metatable and affect it to the userdata. */
3002 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
3003 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003004 return 1;
3005}
3006
Christopher Faulet9f55a502020-02-25 15:21:02 +01003007/* Helper function returning a filter attached to a channel at the position <ud>
3008 * in the stack, filling the current offset and length of the filter. If no
3009 * filter is attached, NULL is returned and <offet> and <len> are not
3010 * initialized.
3011 */
3012static __maybe_unused struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len)
3013{
3014 struct filter *filter = NULL;
3015
3016 if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) {
3017 struct hlua_flt_ctx *flt_ctx;
3018
3019 filter = lua_touserdata (L, -1);
3020 flt_ctx = filter->ctx;
3021 if (hlua_filter_from_payload(filter)) {
3022 *offset = flt_ctx->cur_off[CHN_IDX(chn)];
3023 *len = flt_ctx->cur_len[CHN_IDX(chn)];
3024 }
3025 }
3026
3027 lua_pop(L, 1);
3028 return filter;
3029}
3030
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003031/* Copies <len> bytes of data present in the channel's buffer, starting at the
3032* offset <offset>, and put it in a LUA string variable. It is the caller
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003033* responsibility to ensure <len> and <offset> are valid. It always return the
3034* length of the built string. <len> may be 0, in this case, an empty string is
3035* created and 0 is returned.
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003036*/
3037static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003038{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003039 size_t block1, block2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003040 luaL_Buffer b;
3041
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003042 block1 = len;
3043 if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)))
3044 block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset));
3045 block2 = len - block1;
3046
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003047 luaL_buffinit(L, &b);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003048 luaL_addlstring(&b, b_peek(&chn->buf, offset), block1);
3049 if (block2)
3050 luaL_addlstring(&b, b_orig(&chn->buf), block2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051 luaL_pushresult(&b);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003052 return len;
3053}
3054
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003055/* Inserts the string <str> to the channel's buffer at the offset <offset>. This
3056 * function returns -1 if data cannot be copied. Otherwise, it returns the
3057 * number of bytes copied.
3058 */
3059static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset)
3060{
3061 int ret = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003062
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003063 /* Nothing to do, just return */
3064 if (unlikely(istlen(str) == 0))
3065 goto end;
3066
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003067 if (istlen(str) > c_room(chn)) {
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003068 ret = -1;
3069 goto end;
3070 }
3071 ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str));
3072
3073 end:
3074 return ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003075}
3076
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003077/* Removes <len> bytes of data at the absolute position <offset>.
3078 */
3079static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len)
3080{
3081 size_t end = offset + len;
3082
3083 if (b_peek(&chn->buf, end) != b_tail(&chn->buf))
3084 b_move(&chn->buf, b_peek_ofs(&chn->buf, end),
3085 b_data(&chn->buf) - end, -len);
3086 b_sub(&chn->buf, len);
3087}
3088
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003089/* Copies input data in the channel's buffer. It is possible to set a specific
3090 * offset (0 by default) and a length (all remaining input data starting for the
3091 * offset by default). If there is not enough input data and more data can be
3092 * received, this function yields.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003094__LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003095{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003096 struct channel *chn;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003097 int offset = 0, len = 0;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003098
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003099 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003100
3101 if (!ci_data(chn) && channel_input_closed(chn)) {
3102 lua_pushnil(L);
3103 return 1;
3104 }
3105
3106 if (lua_gettop(L) > 1) {
3107 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3108 if (offset < 0)
3109 offset = MAX(0, ci_data(chn) + offset);
3110 if (offset > ci_data(chn)) {
3111 lua_pushfstring(L, "offset out of range.");
3112 WILL_LJMP(lua_error(L));
3113 }
3114 }
3115 len = ci_data(chn) - offset;
3116 if (lua_gettop(L) == 3) {
3117 len = MAY_LJMP(luaL_checkinteger(L, 3));
3118 if (!len)
3119 goto dup;
3120 if (len == -1)
3121 len = global.tune.bufsize;
3122 if (len < 0) {
3123 lua_pushfstring(L, "length out of range.");
3124 WILL_LJMP(lua_error(L));
3125 }
3126 }
3127
3128 if (offset + len > ci_data(chn)) {
3129 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3130 /* Yield waiting for more data, as requested */
3131 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0));
3132 }
3133 len = ci_data(chn) - offset;
3134 }
3135
3136 dup:
3137 _hlua_channel_dup(chn, L, co_data(chn) + offset, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003138 return 1;
3139}
3140
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003141/* Copies the first line (including the trailing LF) of input data in the
3142 * channel's buffer. It is possible to set a specific offset (0 by default) and
3143 * a length (all remaining input data starting for the offset by default). If
3144 * there is not enough input data and more data can be received, the function
3145 * yields. If a length is explicitly specified, no more data are
3146 * copied. Otherwise, if no LF is found and more data can be received, this
3147 * function yields.
3148 */
3149__LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003150{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003151 struct channel *chn;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003152 int l, offset = 0, len = 0;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003153
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003154 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003155
3156 if (!ci_data(chn) && channel_input_closed(chn)) {
3157 lua_pushnil(L);
3158 return 1;
3159 }
3160
3161 if (lua_gettop(L) > 1) {
3162 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3163 if (offset < 0)
3164 offset = MAX(0, ci_data(chn) + offset);
3165 if (offset > ci_data(chn)) {
3166 lua_pushfstring(L, "offset out of range.");
3167 WILL_LJMP(lua_error(L));
3168 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003169 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003170 len = ci_data(chn) - offset;
3171 if (lua_gettop(L) == 3) {
3172 len = MAY_LJMP(luaL_checkinteger(L, 3));
3173 if (!len)
3174 goto dup;
3175 if (len == -1)
3176 len = global.tune.bufsize;
3177 if (len < 0) {
3178 lua_pushfstring(L, "length out of range.");
3179 WILL_LJMP(lua_error(L));
3180 }
3181 }
3182
3183 for (l = 0; l < len; l++) {
3184 if (l + offset >= ci_data(chn))
3185 break;
3186 if (*(b_peek(&chn->buf, co_data(chn) + offset + l)) == '\n') {
3187 len = l+1;
3188 goto dup;
3189 }
3190 }
3191
3192 if (offset + len > ci_data(chn)) {
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_get_line_yield, TICK_ETERNITY, 0));
3196 }
3197 len = ci_data(chn) - offset;
3198 }
3199
3200 dup:
3201 _hlua_channel_dup(chn, L, co_data(chn) + offset, len);
3202 return 1;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003203}
3204
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003205/* [ DEPRECATED ]
3206 *
3207 * Duplicate all input data foud in the channel's buffer. The data are not
3208 * removed from the buffer. This function relies on _hlua_channel_dup().
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003209 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003210__LJMP static int hlua_channel_dup(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003211{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003212 struct channel *chn;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003213 int offset = 0, len = 0;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003214
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003215 MAY_LJMP(check_args(L, 1, "dup"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003216 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003217 if (IS_HTX_STRM(chn_strm(chn))) {
3218 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3219 WILL_LJMP(lua_error(L));
3220 }
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003221 offset = co_data(chn);
3222 len = ci_data(chn);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003223
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003224 if (!ci_data(chn) && channel_input_closed(chn)) {
3225 lua_pushnil(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003226 return 1;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003227 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003228
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003229 _hlua_channel_dup(chn, L, offset, len);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003230 return 1;
3231}
3232
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003233/* [ DEPRECATED ]
3234 *
3235 * Get all input data foud in the channel's buffer. The data are removed from
3236 * the buffer after the copy. This function relies on _hlua_channel_dup() and
3237 * _hlua_channel_delete().
3238 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003239__LJMP static int hlua_channel_get(lua_State *L)
3240{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003241 struct channel *chn;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003242 int offset = 0, len = 0;
3243 int ret;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003244
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003245 MAY_LJMP(check_args(L, 1, "get"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003246 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3247 if (IS_HTX_STRM(chn_strm(chn))) {
3248 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3249 WILL_LJMP(lua_error(L));
3250 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003251 offset = co_data(chn);
3252 len = ci_data(chn);
3253
3254 if (!ci_data(chn) && channel_input_closed(chn)) {
3255 lua_pushnil(L);
3256 return 1;
3257 }
3258
3259 ret = _hlua_channel_dup(chn, L, offset, len);
3260 _hlua_channel_delete(chn, offset, ret);
3261 return 1;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003262}
3263
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003264/* This functions consumes and returns one line. If the channel is closed,
3265 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003266 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003267 * value.
3268 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003269__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003270{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003271 struct channel *chn;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003272 size_t l;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003273 int offset = 0, len = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003274 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003275
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003276 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003277 offset = co_data(chn);
3278 len = ci_data(chn);
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003279
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003280 if (!ci_data(chn) && channel_input_closed(chn)) {
3281 lua_pushnil(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003282 return 1;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003283 }
3284
3285 for (l = 0; l < len; l++) {
3286 if (*(b_peek(&chn->buf, offset+l)) == '\n') {
3287 len = l+1;
3288 goto dup;
3289 }
3290 }
3291
3292 if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
3293 /* Yield waiting for more data */
3294 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
3295 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003296
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003297 dup:
3298 ret = _hlua_channel_dup(chn, L, co_data(chn) + offset, len);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003299 _hlua_channel_delete(chn, offset, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003300 return 1;
3301}
3302
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003303/* [ DEPRECATED ]
3304 *
3305 * Check arguments for the function "hlua_channel_getline_yield".
3306 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003307__LJMP static int hlua_channel_getline(lua_State *L)
3308{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003309 struct channel *chn;
3310
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003311 MAY_LJMP(check_args(L, 1, "getline"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003312 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3313 if (IS_HTX_STRM(chn_strm(chn))) {
3314 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3315 WILL_LJMP(lua_error(L));
3316 }
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003317 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3318}
3319
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003320/* Retrieves a given amount of input data at the given offset. By default all
3321 * available input data are returned. The offset may be negactive to start from
3322 * the end of input data. The length may be -1 to set it to the maximum buffer
3323 * size.
3324 */
3325__LJMP static int hlua_channel_get_data(lua_State *L)
3326{
3327 struct channel *chn;
3328
3329 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3330 WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments"));
3331 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3332 if (IS_HTX_STRM(chn_strm(chn))) {
3333 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3334 WILL_LJMP(lua_error(L));
3335 }
3336 return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0));
3337}
3338
3339/* Retrieves a given amount of input data at the given offset. By default all
3340 * available input data are returned. The offset may be negactive to start from
3341 * the end of input data. The length may be -1 to set it to the maximum buffer
3342 * size.
3343 */
3344__LJMP static int hlua_channel_get_line(lua_State *L)
3345{
3346 struct channel *chn;
3347
3348 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3349 WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments"));
3350 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3351 if (IS_HTX_STRM(chn_strm(chn))) {
3352 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3353 WILL_LJMP(lua_error(L));
3354 }
3355 return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0));
3356}
3357
3358/* Appends a string into the input side of channel. It returns the length of the
3359 * written string, or -1 if the channel is closed or if the buffer size is too
3360 * little for the data. 0 may be returned if nothing is copied. This function
3361 * does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003362 */
Christopher Faulet23976d92021-08-06 09:59:49 +02003363__LJMP static int hlua_channel_append(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003364{
Christopher Faulet23976d92021-08-06 09:59:49 +02003365 struct channel *chn;
3366 const char *str;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003367 size_t len;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003368 int ret;
Christopher Faulet23976d92021-08-06 09:59:49 +02003369
3370 MAY_LJMP(check_args(L, 2, "append"));
3371 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3372 str = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003373 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003374 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003375 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003376 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003377
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003378 ret = _hlua_channel_insert(chn, L, ist2(str, len), co_data(chn) + ci_data(chn));
3379 lua_pushinteger(L, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003380 return 1;
3381}
3382
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003383/* Prepends a string into the input side of channel. It returns the length of the
3384 * written string, or -1 if the channel is closed or if the buffer size is too
3385 * little for the data. 0 may be returned if nothing is copied. This function
3386 * does not yield.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003387 */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003388__LJMP static int hlua_channel_prepend(lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003389{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003390 struct channel *chn;
Christopher Faulet23976d92021-08-06 09:59:49 +02003391 const char *str;
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003392 size_t sz;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003393 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003394
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003395 MAY_LJMP(check_args(L, 2, "prepend"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003396 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003397 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3398 if (IS_HTX_STRM(chn_strm(chn))) {
3399 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3400 WILL_LJMP(lua_error(L));
3401 }
3402
3403 ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn));
3404 lua_pushinteger(L, ret);
3405 return 1;
3406}
3407
3408/* Inserts a given amount of input data at the given offset by a string
3409 * content. By default the string is appended at the end of input data. It
3410 * returns the length of the written string, or -1 if the channel is closed or
3411 * if the buffer size is too little for the data.
3412 */
3413__LJMP static int hlua_channel_insert_data(lua_State *L)
3414{
3415 struct channel *chn;
3416 const char *str;
3417 size_t sz;
3418 int ret, offset;
3419
3420 if (lua_gettop(L) < 2 || lua_gettop(L) > 3)
3421 WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments"));
3422 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3423 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3424 offset = ci_data(chn);
3425 if (lua_gettop(L) > 2)
3426 offset = MAY_LJMP(luaL_checkinteger(L, 3));
3427 if (IS_HTX_STRM(chn_strm(chn))) {
3428 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3429 WILL_LJMP(lua_error(L));
3430 }
3431
3432 if (offset < 0)
3433 offset = MAX(0, ci_data(chn) + offset);
3434
3435 if (offset > ci_data(chn)) {
3436 lua_pushfstring(L, "offset out of range.");
3437 WILL_LJMP(lua_error(L));
3438 }
3439
3440 ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn) + offset);
3441 lua_pushinteger(L, ret);
3442 return 1;
3443}
3444/* Replaces a given amount of input data at the given offset by a string
3445 * content. By default all remaining data are removed (offset = 0 and len =
3446 * -1). It returns the length of the written string, or -1 if the channel is
3447 * closed or if the buffer size is too little for the data.
3448 */
3449__LJMP static int hlua_channel_set_data(lua_State *L)
3450{
3451 struct channel *chn;
3452 const char *str;
3453 size_t sz;
3454 int ret, offset = 0, len = -1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003455
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003456 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
3457 WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments"));
3458 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3459 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3460 if (lua_gettop(L) > 2)
3461 offset = MAY_LJMP(luaL_checkinteger(L, 3));
3462 if (lua_gettop(L) == 4)
3463 len = MAY_LJMP(luaL_checkinteger(L, 4));
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01003464 if (IS_HTX_STRM(chn_strm(chn))) {
Thierry Fournier77016da2020-08-15 14:35:51 +02003465 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003466 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003467 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003468
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003469 if (offset < 0)
3470 offset = MAX(0, ci_data(chn) + offset);
3471 if (len < 0)
3472 len = ci_data(chn) - offset;
3473
3474 if (offset + len > ci_data(chn)) {
3475 lua_pushfstring(L, "offset or length out of range.");
3476 WILL_LJMP(lua_error(L));
3477 }
3478
Christopher Faulet23976d92021-08-06 09:59:49 +02003479 /* Be sure we can copied the string once input data will be removed. */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003480 if (sz > c_room(chn) + len)
Christopher Faulet23976d92021-08-06 09:59:49 +02003481 lua_pushinteger(L, -1);
3482 else {
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003483 _hlua_channel_delete(chn, co_data(chn) + offset, len);
3484 ret = _hlua_channel_insert(chn, L, ist2(str, sz), co_data(chn) + offset);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003485 lua_pushinteger(L, ret);
Christopher Faulet23976d92021-08-06 09:59:49 +02003486 }
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003487 return 1;
3488}
3489
3490/* Removes a given amount of input data at the given offset. By default all
3491 * intput data are removed (offset = 0 and len = -1). It returns the amount of
3492 * the removed data.
3493 */
3494__LJMP static int hlua_channel_del_data(lua_State *L)
3495{
3496 struct channel *chn;
3497 int offset = 0, len = -1;
3498
3499 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
3500 WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments"));
3501 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3502 if (lua_gettop(L) > 1)
3503 offset = MAY_LJMP(luaL_checkinteger(L, 2));
3504 if (lua_gettop(L) == 3)
3505 len = MAY_LJMP(luaL_checkinteger(L, 3));
3506 if (IS_HTX_STRM(chn_strm(chn))) {
3507 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3508 WILL_LJMP(lua_error(L));
3509 }
3510
3511 if (offset < 0)
3512 offset = MAX(0, ci_data(chn) + offset);
3513 if (len < 0)
3514 len = ci_data(chn) - offset;
3515
3516 if (offset + len > ci_data(chn)) {
3517 lua_pushfstring(L, "offset or length out of range.");
3518 WILL_LJMP(lua_error(L));
3519 }
3520
3521 _hlua_channel_delete(chn, co_data(chn) + offset, len);
3522 lua_pushinteger(L, len);
Christopher Faulet23976d92021-08-06 09:59:49 +02003523 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003524}
3525
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003526/* Append data in the output side of the buffer. This data is immediately
3527 * sent. The function returns the amount of data written. If the buffer
3528 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003529 * if the channel is closed.
3530 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003531__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003532{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003533 struct channel *chn;
3534 const char *str;
3535 size_t sz, len;
3536 int l, ret;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003537 struct hlua *hlua;
3538
3539 /* Get hlua struct, or NULL if we execute from main lua state */
3540 hlua = hlua_gethlua(L);
3541 if (!hlua) {
3542 lua_pushnil(L);
3543 return 1;
3544 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003545
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003546 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3547 str = MAY_LJMP(luaL_checklstring(L, 2, &sz));
3548 l = MAY_LJMP(luaL_checkinteger(L, 3));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003549
Willy Tarreau47860ed2015-03-10 14:07:50 +01003550 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003551 lua_pushinteger(L, -1);
3552 return 1;
3553 }
3554
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003555 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003556 * the request buffer if its not required.
3557 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003558 if (chn->buf.size == 0) {
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003559 if (HLUA_CANT_YIELD(hlua_gethlua(L)))
3560 return 1;
Willy Tarreau4b962a42018-11-15 11:03:21 +01003561 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003562 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003563 }
3564
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003565 len = c_room(chn);
3566 if (len > sz - l)
3567 len = sz - l;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003568
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003569 ret = _hlua_channel_insert(chn, L, ist2(str+l, len), co_data(chn));
3570 if (ret == -1) {
3571 lua_pop(L, 1);
3572 lua_pushinteger(L, -1);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003573 return 1;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003574 }
3575 if (ret) {
3576 c_adv(chn, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003577
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003578 l += ret;
3579 lua_pop(L, 1);
3580 lua_pushinteger(L, l);
3581 }
3582 if (l < sz) {
3583 /* Yield only if the channel's output is not empty.
3584 * Otherwise it means we cannot add more data. */
3585 if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L)))
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003586 return 1;
3587
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003588 /* If we are waiting for space in the response buffer, we
3589 * must set the flag WAKERESWR. This flag required the task
3590 * wake up if any activity is detected on the response buffer.
3591 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003592 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003593 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003594 else
3595 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003596 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003597 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003598
3599 return 1;
3600}
3601
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003602/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003603 * yield the LUA process, and resume it without checking the
3604 * input arguments.
3605 */
3606__LJMP static int hlua_channel_send(lua_State *L)
3607{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003608 struct channel *chn;
3609
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003610 MAY_LJMP(check_args(L, 2, "send"));
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003611 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3612 if (IS_HTX_STRM(chn_strm(chn))) {
3613 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3614 WILL_LJMP(lua_error(L));
3615 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003616 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003617 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003618}
3619
3620/* This function forward and amount of butes. The data pass from
3621 * the input side of the buffer to the output side, and can be
3622 * forwarded. This function never fails.
3623 *
3624 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003625 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003626 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003627__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003628{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003629 struct channel *chn;
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003630 size_t len;
3631 int l, max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003632 struct hlua *hlua;
3633
3634 /* Get hlua struct, or NULL if we execute from main lua state */
3635 hlua = hlua_gethlua(L);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003636 if (!hlua) {
3637 lua_pushnil(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003638 return 1;
Thierry Fournier77016da2020-08-15 14:35:51 +02003639 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003640
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003641 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003642 len = MAY_LJMP(luaL_checkinteger(L, 2));
3643 l = MAY_LJMP(luaL_checkinteger(L, -1));
3644
3645 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003646 if (max > ci_data(chn))
3647 max = ci_data(chn);
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003648
Willy Tarreau47860ed2015-03-10 14:07:50 +01003649 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003650
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003651 l += max;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003652 lua_pop(L, 1);
3653 lua_pushinteger(L, l);
3654
3655 /* Check if it miss bytes to forward. */
3656 if (l < len) {
3657 /* The the input channel or the output channel are closed, we
3658 * must return the amount of data forwarded.
3659 */
Christopher Faulet2e60aa42021-08-05 11:58:37 +02003660 if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L)))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003661 return 1;
3662
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003663 /* If we are waiting for space data in the response buffer, we
3664 * must set the flag WAKERESWR. This flag required the task
3665 * wake up if any activity is detected on the response buffer.
3666 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003667 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003668 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003669 else
3670 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003671
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003672 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003673 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003674 }
3675
3676 return 1;
3677}
3678
3679/* Just check the input and prepare the stack for the previous
3680 * function "hlua_channel_forward_yield"
3681 */
3682__LJMP static int hlua_channel_forward(lua_State *L)
3683{
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003684 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003685
Christopher Faulet9a6ffda2021-08-06 13:49:54 +02003686 MAY_LJMP(check_args(L, 2, "forward"));
3687 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3688 if (IS_HTX_STRM(chn_strm(chn))) {
3689 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
3690 WILL_LJMP(lua_error(L));
3691 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003692 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003693 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003694}
3695
3696/* Just returns the number of bytes available in the input
3697 * side of the buffer. This function never fails.
3698 */
3699__LJMP static int hlua_channel_get_in_len(lua_State *L)
3700{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003701 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003702
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003703 MAY_LJMP(check_args(L, 1, "input"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003704 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003705 if (IS_HTX_STRM(chn_strm(chn))) {
3706 struct htx *htx = htxbuf(&chn->buf);
3707 lua_pushinteger(L, htx->data - co_data(chn));
3708 }
3709 else
3710 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003711 return 1;
3712}
3713
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003714/* Returns true if the channel is full. */
3715__LJMP static int hlua_channel_is_full(lua_State *L)
3716{
3717 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003718
3719 MAY_LJMP(check_args(L, 1, "is_full"));
3720 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003721 /* ignore the reserve, we are not on a producer side (ie in an
3722 * applet).
3723 */
3724 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003725 return 1;
3726}
3727
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003728/* Returns true if the channel may still receive data. */
3729__LJMP static int hlua_channel_may_recv(lua_State *L)
3730{
3731 struct channel *chn;
3732
3733 MAY_LJMP(check_args(L, 1, "may_recv"));
3734 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3735 lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn)));
3736 return 1;
3737}
3738
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003739/* Returns true if the channel is the response channel. */
3740__LJMP static int hlua_channel_is_resp(lua_State *L)
3741{
3742 struct channel *chn;
3743
3744 MAY_LJMP(check_args(L, 1, "is_resp"));
3745 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3746
3747 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3748 return 1;
3749}
3750
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003751/* Just returns the number of bytes available in the output
3752 * side of the buffer. This function never fails.
3753 */
3754__LJMP static int hlua_channel_get_out_len(lua_State *L)
3755{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003756 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003757
Christopher Faulet6a79fc12021-08-06 16:02:36 +02003758 MAY_LJMP(check_args(L, 1, "output"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003759 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003760 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003761 return 1;
3762}
3763
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003764/*
3765 *
3766 *
3767 * Class Fetches
3768 *
3769 *
3770 */
3771
3772/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003773 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003774 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003775__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003776{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003777 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003778}
3779
3780/* This function creates and push in the stack a fetch object according
3781 * with a current TXN.
3782 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003783static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003784{
Willy Tarreau7073c472015-04-06 11:15:40 +02003785 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003786
3787 /* Check stack size. */
3788 if (!lua_checkstack(L, 3))
3789 return 0;
3790
3791 /* Create the object: obj[0] = userdata.
3792 * Note that the base of the Fetches object is the
3793 * transaction object.
3794 */
3795 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003796 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003797 lua_rawseti(L, -2, 0);
3798
Willy Tarreau7073c472015-04-06 11:15:40 +02003799 hsmp->s = txn->s;
3800 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003801 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003802 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003803
3804 /* Pop a class sesison metatable and affect it to the userdata. */
3805 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3806 lua_setmetatable(L, -2);
3807
3808 return 1;
3809}
3810
3811/* This function is an LUA binding. It is called with each sample-fetch.
3812 * It uses closure argument to store the associated sample-fetch. It
3813 * returns only one argument or throws an error. An error is thrown
3814 * only if an error is encountered during the argument parsing. If
3815 * the "sample-fetch" function fails, nil is returned.
3816 */
3817__LJMP static int hlua_run_sample_fetch(lua_State *L)
3818{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003819 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003820 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003821 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003822 int i;
3823 struct sample smp;
3824
3825 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003826 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003827
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003828 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003829 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003830
Thierry FOURNIERca988662015-12-20 18:43:03 +01003831 /* Check execution authorization. */
3832 if (f->use & SMP_USE_HTTP_ANY &&
3833 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3834 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3835 "is not available in Lua services", f->kw);
3836 WILL_LJMP(lua_error(L));
3837 }
3838
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003839 /* Get extra arguments. */
3840 for (i = 0; i < lua_gettop(L) - 1; i++) {
3841 if (i >= ARGM_NBARGS)
3842 break;
3843 hlua_lua2arg(L, i + 2, &args[i]);
3844 }
3845 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003846 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003847
3848 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003849 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003850
3851 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003852 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003853 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003854 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003855 }
3856
3857 /* Initialise the sample. */
3858 memset(&smp, 0, sizeof(smp));
3859
3860 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003861 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003862 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003863 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003864 lua_pushstring(L, "");
3865 else
3866 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003867 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003868 }
3869
3870 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003871 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003872 hlua_smp2lua_str(L, &smp);
3873 else
3874 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003875
3876 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003877 free_args(args);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003878 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003879
3880 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02003881 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003882 WILL_LJMP(lua_error(L));
3883 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003884}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003885
3886/*
3887 *
3888 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003889 * Class Converters
3890 *
3891 *
3892 */
3893
3894/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003895 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003896 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003897__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003898{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003899 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003900}
3901
3902/* This function creates and push in the stack a Converters object
3903 * according with a current TXN.
3904 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003905static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003906{
Willy Tarreau7073c472015-04-06 11:15:40 +02003907 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003908
3909 /* Check stack size. */
3910 if (!lua_checkstack(L, 3))
3911 return 0;
3912
3913 /* Create the object: obj[0] = userdata.
3914 * Note that the base of the Converters object is the
3915 * same than the TXN object.
3916 */
3917 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003918 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003919 lua_rawseti(L, -2, 0);
3920
Willy Tarreau7073c472015-04-06 11:15:40 +02003921 hsmp->s = txn->s;
3922 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003923 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003924 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003925
Willy Tarreau87b09662015-04-03 00:22:06 +02003926 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003927 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3928 lua_setmetatable(L, -2);
3929
3930 return 1;
3931}
3932
3933/* This function is an LUA binding. It is called with each converter.
3934 * It uses closure argument to store the associated converter. It
3935 * returns only one argument or throws an error. An error is thrown
3936 * only if an error is encountered during the argument parsing. If
3937 * the converter function function fails, nil is returned.
3938 */
3939__LJMP static int hlua_run_sample_conv(lua_State *L)
3940{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003941 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003942 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003943 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003944 int i;
3945 struct sample smp;
3946
3947 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003948 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003949
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003950 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003951 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003952
3953 /* Get extra arguments. */
3954 for (i = 0; i < lua_gettop(L) - 2; i++) {
3955 if (i >= ARGM_NBARGS)
3956 break;
3957 hlua_lua2arg(L, i + 3, &args[i]);
3958 }
3959 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003960 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003961
3962 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003963 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003964
3965 /* Run the special args checker. */
3966 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3967 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003968 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003969 }
3970
3971 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003972 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003973 if (!hlua_lua2smp(L, 2, &smp)) {
3974 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003975 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003976 }
3977
Willy Tarreau1777ea62016-03-10 16:15:46 +01003978 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3979
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003980 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003981 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003982 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003983 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003984 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003985 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003986 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3987 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003988 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003989 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003990 }
3991
3992 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003993 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003994 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003995 lua_pushstring(L, "");
3996 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003997 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003998 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003999 }
4000
4001 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004002 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004003 hlua_smp2lua_str(L, &smp);
4004 else
4005 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004006 end:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004007 free_args(args);
Willy Tarreaua678b432015-08-28 10:14:59 +02004008 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004009
4010 error:
Willy Tarreauee0d7272021-07-16 10:26:56 +02004011 free_args(args);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02004012 WILL_LJMP(lua_error(L));
4013 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004014}
4015
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004016/*
4017 *
4018 *
4019 * Class AppletTCP
4020 *
4021 *
4022 */
4023
4024/* Returns a struct hlua_txn if the stack entry "ud" is
4025 * a class stream, otherwise it throws an error.
4026 */
4027__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
4028{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004029 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004030}
4031
4032/* This function creates and push in the stack an Applet object
4033 * according with a current TXN.
4034 */
4035static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
4036{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004037 struct hlua_appctx *luactx;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004038 struct stream_interface *si = ctx->owner;
4039 struct stream *s = si_strm(si);
4040 struct proxy *p = s->be;
4041
4042 /* Check stack size. */
4043 if (!lua_checkstack(L, 3))
4044 return 0;
4045
4046 /* Create the object: obj[0] = userdata.
4047 * Note that the base of the Converters object is the
4048 * same than the TXN object.
4049 */
4050 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004051 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004052 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004053 luactx->appctx = ctx;
4054 luactx->htxn.s = s;
4055 luactx->htxn.p = p;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004056
4057 /* Create the "f" field that contains a list of fetches. */
4058 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004059 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004060 return 0;
4061 lua_settable(L, -3);
4062
4063 /* Create the "sf" field that contains a list of stringsafe fetches. */
4064 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004065 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004066 return 0;
4067 lua_settable(L, -3);
4068
4069 /* Create the "c" field that contains a list of converters. */
4070 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004071 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004072 return 0;
4073 lua_settable(L, -3);
4074
4075 /* Create the "sc" field that contains a list of stringsafe converters. */
4076 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004077 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004078 return 0;
4079 lua_settable(L, -3);
4080
4081 /* Pop a class stream metatable and affect it to the table. */
4082 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
4083 lua_setmetatable(L, -2);
4084
4085 return 1;
4086}
4087
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004088__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
4089{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004090 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004091 struct stream *s;
4092 const char *name;
4093 size_t len;
4094 struct sample smp;
4095
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004096 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4097 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004098
4099 /* It is useles to retrieve the stream, but this function
4100 * runs only in a stream context.
4101 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004102 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004103 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004104 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004105
4106 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004107 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004108 hlua_lua2smp(L, 3, &smp);
4109
4110 /* Store the sample in a variable. */
4111 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004112
4113 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4114 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4115 else
4116 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4117
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004118 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004119}
4120
4121__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
4122{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004123 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004124 struct stream *s;
4125 const char *name;
4126 size_t len;
4127 struct sample smp;
4128
4129 MAY_LJMP(check_args(L, 2, "unset_var"));
4130
4131 /* It is useles to retrieve the stream, but this function
4132 * runs only in a stream context.
4133 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004134 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004135 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004136 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004137
4138 /* Unset the variable. */
4139 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004140 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4141 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004142}
4143
4144__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
4145{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004146 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004147 struct stream *s;
4148 const char *name;
4149 size_t len;
4150 struct sample smp;
4151
4152 MAY_LJMP(check_args(L, 2, "get_var"));
4153
4154 /* It is useles to retrieve the stream, but this function
4155 * runs only in a stream context.
4156 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004157 luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004158 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004159 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004160
4161 smp_set_owner(&smp, s->be, s->sess, s, 0);
4162 if (!vars_get_by_name(name, len, &smp)) {
4163 lua_pushnil(L);
4164 return 1;
4165 }
4166
4167 return hlua_smp2lua(L, &smp);
4168}
4169
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004170__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
4171{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004172 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4173 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004174 struct hlua *hlua;
4175
4176 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004177 if (!s->hlua)
4178 return 0;
4179 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004180
4181 MAY_LJMP(check_args(L, 2, "set_priv"));
4182
4183 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004184 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004185
4186 /* Get and store new value. */
4187 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4188 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4189
4190 return 0;
4191}
4192
4193__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
4194{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004195 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4196 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004197 struct hlua *hlua;
4198
4199 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004200 if (!s->hlua) {
4201 lua_pushnil(L);
4202 return 1;
4203 }
4204 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004205
4206 /* Push configuration index in the stack. */
4207 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4208
4209 return 1;
4210}
4211
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004212/* If expected data not yet available, it returns a yield. This function
4213 * consumes the data in the buffer. It returns a string containing the
4214 * data. This string can be empty.
4215 */
4216__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
4217{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004218 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4219 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004220 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004221 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004222 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004223 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004224 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004225
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004226 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004227 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004228
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004229 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004230 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004231 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004232 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004233 }
4234
4235 /* End of data: commit the total strings and return. */
4236 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004237 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004238 return 1;
4239 }
4240
4241 /* Ensure that the block 2 length is usable. */
4242 if (ret == 1)
4243 len2 = 0;
4244
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004245 /* don't check the max length read and don't check. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004246 luaL_addlstring(&luactx->b, blk1, len1);
4247 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004248
4249 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004250 co_skip(si_oc(si), len1 + len2);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004251 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004252 return 1;
4253}
4254
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004255/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004256__LJMP static int hlua_applet_tcp_getline(lua_State *L)
4257{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004258 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004259
4260 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004261 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004262
4263 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
4264}
4265
4266/* If expected data not yet available, it returns a yield. This function
4267 * consumes the data in the buffer. It returns a string containing the
4268 * data. This string can be empty.
4269 */
4270__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
4271{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004272 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4273 struct stream_interface *si = luactx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004274 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004275 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004276 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004277 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004278 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004279 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004280
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004281 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004282 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004283
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004284 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004285 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004286 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004287 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004288 }
4289
4290 /* End of data: commit the total strings and return. */
4291 if (ret < 0) {
Willy Tarreau7e702d12021-04-28 17:59:21 +02004292 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004293 return 1;
4294 }
4295
4296 /* Ensure that the block 2 length is usable. */
4297 if (ret == 1)
4298 len2 = 0;
4299
4300 if (len == -1) {
4301
4302 /* If len == -1, catenate all the data avalaile and
4303 * yield because we want to get all the data until
4304 * the end of data stream.
4305 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004306 luaL_addlstring(&luactx->b, blk1, len1);
4307 luaL_addlstring(&luactx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004308 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004309 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004310 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004311
4312 } else {
4313
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004314 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004315 if (len1 > len)
4316 len1 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004317 luaL_addlstring(&luactx->b, blk1, len1);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004318 len -= len1;
4319
4320 /* Copy the second block. */
4321 if (len2 > len)
4322 len2 = len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004323 luaL_addlstring(&luactx->b, blk2, len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004324 len -= len2;
4325
4326 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004327 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004328
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004329 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004330 if (len > 0) {
4331 lua_pushinteger(L, len);
4332 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004333 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004334 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004335 }
4336
4337 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004338 luaL_pushresult(&luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004339 return 1;
4340 }
4341
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004342 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004343 hlua_pusherror(L, "Lua: internal error");
4344 WILL_LJMP(lua_error(L));
4345 return 0;
4346}
4347
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004348/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004349__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4350{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004351 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004352 int len = -1;
4353
4354 if (lua_gettop(L) > 2)
4355 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4356 if (lua_gettop(L) >= 2) {
4357 len = MAY_LJMP(luaL_checkinteger(L, 2));
4358 lua_pop(L, 1);
4359 }
4360
4361 /* Confirm or set the required length */
4362 lua_pushinteger(L, len);
4363
4364 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004365 luaL_buffinit(L, &luactx->b);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004366
4367 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4368}
4369
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004370/* Append data in the output side of the buffer. This data is immediately
4371 * sent. The function returns the amount of data written. If the buffer
4372 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004373 * if the channel is closed.
4374 */
4375__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4376{
4377 size_t len;
Willy Tarreau7e702d12021-04-28 17:59:21 +02004378 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004379 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4380 int l = MAY_LJMP(luaL_checkinteger(L, 3));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004381 struct stream_interface *si = luactx->appctx->owner;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004382 struct channel *chn = si_ic(si);
4383 int max;
4384
4385 /* Get the max amount of data which can write as input in the channel. */
4386 max = channel_recv_max(chn);
4387 if (max > (len - l))
4388 max = len - l;
4389
4390 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004391 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004392
4393 /* update counters. */
4394 l += max;
4395 lua_pop(L, 1);
4396 lua_pushinteger(L, l);
4397
4398 /* If some data is not send, declares the situation to the
4399 * applet, and returns a yield.
4400 */
4401 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004402 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004403 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004404 }
4405
4406 return 1;
4407}
4408
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004409/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004410 * yield the LUA process, and resume it without checking the
4411 * input arguments.
4412 */
4413__LJMP static int hlua_applet_tcp_send(lua_State *L)
4414{
4415 MAY_LJMP(check_args(L, 2, "send"));
4416 lua_pushinteger(L, 0);
4417
4418 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4419}
4420
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004421/*
4422 *
4423 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004424 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004425 *
4426 *
4427 */
4428
4429/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004430 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004431 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004432__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004433{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004434 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004435}
4436
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004437/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004438 * according with a current TXN.
4439 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004440static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004441{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004442 struct hlua_appctx *luactx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004443 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004444 struct stream_interface *si = ctx->owner;
4445 struct stream *s = si_strm(si);
4446 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004447 struct htx *htx;
4448 struct htx_blk *blk;
4449 struct htx_sl *sl;
4450 struct ist path;
4451 unsigned long long len = 0;
4452 int32_t pos;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004453 struct http_uri_parser parser;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004454
4455 /* Check stack size. */
4456 if (!lua_checkstack(L, 3))
4457 return 0;
4458
4459 /* Create the object: obj[0] = userdata.
4460 * Note that the base of the Converters object is the
4461 * same than the TXN object.
4462 */
4463 lua_newtable(L);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004464 luactx = lua_newuserdata(L, sizeof(*luactx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004465 lua_rawseti(L, -2, 0);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004466 luactx->appctx = ctx;
4467 luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
4468 luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
4469 luactx->htxn.s = s;
4470 luactx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004471
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004472 /* Create the "f" field that contains a list of fetches. */
4473 lua_pushstring(L, "f");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004474 if (!hlua_fetches_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004475 return 0;
4476 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004477
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004478 /* Create the "sf" field that contains a list of stringsafe fetches. */
4479 lua_pushstring(L, "sf");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004480 if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004481 return 0;
4482 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004483
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004484 /* Create the "c" field that contains a list of converters. */
4485 lua_pushstring(L, "c");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004486 if (!hlua_converters_new(L, &luactx->htxn, 0))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004487 return 0;
4488 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004489
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004490 /* Create the "sc" field that contains a list of stringsafe converters. */
4491 lua_pushstring(L, "sc");
Willy Tarreau7e702d12021-04-28 17:59:21 +02004492 if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004493 return 0;
4494 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004495
Christopher Fauleta2097962019-07-15 16:25:33 +02004496 htx = htxbuf(&s->req.buf);
4497 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004498 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004499 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004500
Christopher Fauleta2097962019-07-15 16:25:33 +02004501 /* Stores the request method. */
4502 lua_pushstring(L, "method");
4503 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4504 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004505
Christopher Fauleta2097962019-07-15 16:25:33 +02004506 /* Stores the http version. */
4507 lua_pushstring(L, "version");
4508 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4509 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004510
Christopher Fauleta2097962019-07-15 16:25:33 +02004511 /* creates an array of headers. hlua_http_get_headers() crates and push
4512 * the array on the top of the stack.
4513 */
4514 lua_pushstring(L, "headers");
4515 htxn.s = s;
4516 htxn.p = px;
4517 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004518 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004519 return 0;
4520 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004521
Amaury Denoyellec453f952021-07-06 11:40:12 +02004522 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4523 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004524 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004525 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004526
Christopher Fauleta2097962019-07-15 16:25:33 +02004527 p = path.ptr;
4528 end = path.ptr + path.len;
4529 q = p;
4530 while (q < end && *q != '?')
4531 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004532
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004533 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004534 lua_pushstring(L, "path");
4535 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004536 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004537
Christopher Fauleta2097962019-07-15 16:25:33 +02004538 /* Stores the query string. */
4539 lua_pushstring(L, "qs");
4540 if (*q == '?')
4541 q++;
4542 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004543 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004544 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004545
Christopher Fauleta2097962019-07-15 16:25:33 +02004546 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4547 struct htx_blk *blk = htx_get_blk(htx, pos);
4548 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004549
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004550 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004551 break;
4552 if (type == HTX_BLK_DATA)
4553 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004554 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004555 if (htx->extra != ULLONG_MAX)
4556 len += htx->extra;
4557
4558 /* Stores the request path. */
4559 lua_pushstring(L, "length");
4560 lua_pushinteger(L, len);
4561 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004562
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004563 /* Create an empty array of HTTP request headers. */
4564 lua_pushstring(L, "response");
4565 lua_newtable(L);
4566 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004567
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004568 /* Pop a class stream metatable and affect it to the table. */
4569 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4570 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004571
4572 return 1;
4573}
4574
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004575__LJMP static int hlua_applet_http_set_var(lua_State *L)
4576{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004577 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004578 struct stream *s;
4579 const char *name;
4580 size_t len;
4581 struct sample smp;
4582
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004583 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4584 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004585
4586 /* It is useles to retrieve the stream, but this function
4587 * runs only in a stream context.
4588 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004589 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004590 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004591 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004592
4593 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004594 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004595 hlua_lua2smp(L, 3, &smp);
4596
4597 /* Store the sample in a variable. */
4598 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004599
4600 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4601 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4602 else
4603 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4604
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004605 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004606}
4607
4608__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4609{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004610 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004611 struct stream *s;
4612 const char *name;
4613 size_t len;
4614 struct sample smp;
4615
4616 MAY_LJMP(check_args(L, 2, "unset_var"));
4617
4618 /* It is useles to retrieve the stream, but this function
4619 * runs only in a stream context.
4620 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004621 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004622 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004623 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004624
4625 /* Unset the variable. */
4626 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004627 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4628 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004629}
4630
4631__LJMP static int hlua_applet_http_get_var(lua_State *L)
4632{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004633 struct hlua_appctx *luactx;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004634 struct stream *s;
4635 const char *name;
4636 size_t len;
4637 struct sample smp;
4638
4639 MAY_LJMP(check_args(L, 2, "get_var"));
4640
4641 /* It is useles to retrieve the stream, but this function
4642 * runs only in a stream context.
4643 */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004644 luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004645 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Willy Tarreau7e702d12021-04-28 17:59:21 +02004646 s = luactx->htxn.s;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004647
4648 smp_set_owner(&smp, s->be, s->sess, s, 0);
4649 if (!vars_get_by_name(name, len, &smp)) {
4650 lua_pushnil(L);
4651 return 1;
4652 }
4653
4654 return hlua_smp2lua(L, &smp);
4655}
4656
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004657__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4658{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004659 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4660 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004661 struct hlua *hlua;
4662
4663 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004664 if (!s->hlua)
4665 return 0;
4666 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004667
4668 MAY_LJMP(check_args(L, 2, "set_priv"));
4669
4670 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004671 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004672
4673 /* Get and store new value. */
4674 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4675 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4676
4677 return 0;
4678}
4679
4680__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4681{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004682 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4683 struct stream *s = luactx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004684 struct hlua *hlua;
4685
4686 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004687 if (!s->hlua) {
4688 lua_pushnil(L);
4689 return 1;
4690 }
4691 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004692
4693 /* Push configuration index in the stack. */
4694 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4695
4696 return 1;
4697}
4698
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004699/* If expected data not yet available, it returns a yield. This function
4700 * consumes the data in the buffer. It returns a string containing the
4701 * data. This string can be empty.
4702 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004703__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004704{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004705 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4706 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004707 struct channel *req = si_oc(si);
4708 struct htx *htx;
4709 struct htx_blk *blk;
4710 size_t count;
4711 int stop = 0;
4712
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004713 htx = htx_from_buf(&req->buf);
4714 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004715 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004716
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004717 while (count && !stop && blk) {
4718 enum htx_blk_type type = htx_get_blk_type(blk);
4719 uint32_t sz = htx_get_blksz(blk);
4720 struct ist v;
4721 uint32_t vlen;
4722 char *nl;
4723
4724 vlen = sz;
4725 if (vlen > count) {
4726 if (type != HTX_BLK_DATA)
4727 break;
4728 vlen = count;
4729 }
4730
4731 switch (type) {
4732 case HTX_BLK_UNUSED:
4733 break;
4734
4735 case HTX_BLK_DATA:
4736 v = htx_get_blk_value(htx, blk);
4737 v.len = vlen;
4738 nl = istchr(v, '\n');
4739 if (nl != NULL) {
4740 stop = 1;
4741 vlen = nl - v.ptr + 1;
4742 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02004743 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004744 break;
4745
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004746 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004747 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004748 stop = 1;
4749 break;
4750
4751 default:
4752 break;
4753 }
4754
4755 co_set_data(req, co_data(req) - vlen);
4756 count -= vlen;
4757 if (sz == vlen)
4758 blk = htx_remove_blk(htx, blk);
4759 else {
4760 htx_cut_data_blk(htx, blk, vlen);
4761 break;
4762 }
4763 }
4764
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004765 /* The message was fully consumed and no more data are expected
4766 * (EOM flag set).
4767 */
4768 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4769 stop = 1;
4770
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004771 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004772 if (!stop) {
4773 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004774 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004775 }
4776
4777 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004778 luaL_pushresult(&luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004779 return 1;
4780}
4781
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004782
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004783/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004784__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004785{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004786 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004787
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004788 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004789 luaL_buffinit(L, &luactx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004790
Christopher Fauleta2097962019-07-15 16:25:33 +02004791 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004792}
4793
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004794/* If expected data not yet available, it returns a yield. This function
4795 * consumes the data in the buffer. It returns a string containing the
4796 * data. This string can be empty.
4797 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004798__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004799{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004800 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4801 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004802 struct channel *req = si_oc(si);
4803 struct htx *htx;
4804 struct htx_blk *blk;
4805 size_t count;
4806 int len;
4807
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004808 htx = htx_from_buf(&req->buf);
4809 len = MAY_LJMP(luaL_checkinteger(L, 2));
4810 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004811 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004812 while (count && len && blk) {
4813 enum htx_blk_type type = htx_get_blk_type(blk);
4814 uint32_t sz = htx_get_blksz(blk);
4815 struct ist v;
4816 uint32_t vlen;
4817
4818 vlen = sz;
4819 if (len > 0 && vlen > len)
4820 vlen = len;
4821 if (vlen > count) {
4822 if (type != HTX_BLK_DATA)
4823 break;
4824 vlen = count;
4825 }
4826
4827 switch (type) {
4828 case HTX_BLK_UNUSED:
4829 break;
4830
4831 case HTX_BLK_DATA:
4832 v = htx_get_blk_value(htx, blk);
Willy Tarreau7e702d12021-04-28 17:59:21 +02004833 luaL_addlstring(&luactx->b, v.ptr, vlen);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004834 break;
4835
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004836 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004837 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004838 len = 0;
4839 break;
4840
4841 default:
4842 break;
4843 }
4844
4845 co_set_data(req, co_data(req) - vlen);
4846 count -= vlen;
4847 if (len > 0)
4848 len -= vlen;
4849 if (sz == vlen)
4850 blk = htx_remove_blk(htx, blk);
4851 else {
4852 htx_cut_data_blk(htx, blk, vlen);
4853 break;
4854 }
4855 }
4856
Christopher Fauleteccb31c2021-04-02 14:24:56 +02004857 /* The message was fully consumed and no more data are expected
4858 * (EOM flag set).
4859 */
4860 if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM))
4861 len = 0;
4862
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004863 htx_to_buf(htx, &req->buf);
4864
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004865 /* If we are no other data available, yield waiting for new data. */
4866 if (len) {
4867 if (len > 0) {
4868 lua_pushinteger(L, len);
4869 lua_replace(L, 2);
4870 }
4871 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004872 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004873 }
4874
4875 /* return the result. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004876 luaL_pushresult(&luactx->b);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004877 return 1;
4878}
4879
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004880/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004881__LJMP static int hlua_applet_http_recv(lua_State *L)
4882{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004883 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004884 int len = -1;
4885
4886 /* Check arguments. */
4887 if (lua_gettop(L) > 2)
4888 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4889 if (lua_gettop(L) >= 2) {
4890 len = MAY_LJMP(luaL_checkinteger(L, 2));
4891 lua_pop(L, 1);
4892 }
4893
Christopher Fauleta2097962019-07-15 16:25:33 +02004894 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004895
Christopher Fauleta2097962019-07-15 16:25:33 +02004896 /* Initialise the string catenation. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004897 luaL_buffinit(L, &luactx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004898
Christopher Fauleta2097962019-07-15 16:25:33 +02004899 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004900}
4901
4902/* Append data in the output side of the buffer. This data is immediately
4903 * sent. The function returns the amount of data written. If the buffer
4904 * cannot contain the data, the function yields. The function returns -1
4905 * if the channel is closed.
4906 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004907__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004908{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004909 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4910 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004911 struct channel *res = si_ic(si);
4912 struct htx *htx = htx_from_buf(&res->buf);
4913 const char *data;
4914 size_t len;
4915 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4916 int max;
4917
Christopher Faulet9060fc02019-07-03 11:39:30 +02004918 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004919 if (!max)
4920 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004921
4922 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4923
4924 /* Get the max amount of data which can write as input in the channel. */
4925 if (max > (len - l))
4926 max = len - l;
4927
4928 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004929 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004930 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004931
4932 /* update counters. */
4933 l += max;
4934 lua_pop(L, 1);
4935 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004936
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004937 /* If some data is not send, declares the situation to the
4938 * applet, and returns a yield.
4939 */
4940 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004941 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004942 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004943 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004944 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004945 }
4946
Christopher Fauleta2097962019-07-15 16:25:33 +02004947 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004948 return 1;
4949}
4950
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004951/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004952 * yield the LUA process, and resume it without checking the
4953 * input arguments.
4954 */
4955__LJMP static int hlua_applet_http_send(lua_State *L)
4956{
Willy Tarreau7e702d12021-04-28 17:59:21 +02004957 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004958
4959 /* We want to send some data. Headers must be sent. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02004960 if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004961 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4962 WILL_LJMP(lua_error(L));
4963 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004964
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004965 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004966 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004967
Christopher Fauleta2097962019-07-15 16:25:33 +02004968 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004969}
4970
4971__LJMP static int hlua_applet_http_addheader(lua_State *L)
4972{
4973 const char *name;
4974 int ret;
4975
4976 MAY_LJMP(hlua_checkapplet_http(L, 1));
4977 name = MAY_LJMP(luaL_checkstring(L, 2));
4978 MAY_LJMP(luaL_checkstring(L, 3));
4979
4980 /* Push in the stack the "response" entry. */
4981 ret = lua_getfield(L, 1, "response");
4982 if (ret != LUA_TTABLE) {
4983 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4984 "is expected as an array. %s found", lua_typename(L, ret));
4985 WILL_LJMP(lua_error(L));
4986 }
4987
4988 /* check if the header is already registered if it is not
4989 * the case, register it.
4990 */
4991 ret = lua_getfield(L, -1, name);
4992 if (ret == LUA_TNIL) {
4993
4994 /* Entry not found. */
4995 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4996
4997 /* Insert the new header name in the array in the top of the stack.
4998 * It left the new array in the top of the stack.
4999 */
5000 lua_newtable(L);
5001 lua_pushvalue(L, 2);
5002 lua_pushvalue(L, -2);
5003 lua_settable(L, -4);
5004
5005 } else if (ret != LUA_TTABLE) {
5006
5007 /* corruption error. */
5008 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
5009 "is expected as an array. %s found", name, lua_typename(L, ret));
5010 WILL_LJMP(lua_error(L));
5011 }
5012
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07005013 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005014 * the header value as new entry.
5015 */
5016 lua_pushvalue(L, 3);
5017 ret = lua_rawlen(L, -2);
5018 lua_rawseti(L, -2, ret + 1);
5019 lua_pushboolean(L, 1);
5020 return 1;
5021}
5022
5023__LJMP static int hlua_applet_http_status(lua_State *L)
5024{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005025 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005026 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005027 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005028
5029 if (status < 100 || status > 599) {
5030 lua_pushboolean(L, 0);
5031 return 1;
5032 }
5033
Willy Tarreau7e702d12021-04-28 17:59:21 +02005034 luactx->appctx->ctx.hlua_apphttp.status = status;
5035 luactx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005036 lua_pushboolean(L, 1);
5037 return 1;
5038}
5039
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005040
Christopher Fauleta2097962019-07-15 16:25:33 +02005041__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005042{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005043 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
5044 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005045 struct channel *res = si_ic(si);
5046 struct htx *htx;
5047 struct htx_sl *sl;
5048 struct h1m h1m;
5049 const char *status, *reason;
5050 const char *name, *value;
5051 size_t nlen, vlen;
5052 unsigned int flags;
5053
5054 /* Send the message at once. */
5055 htx = htx_from_buf(&res->buf);
5056 h1m_init_res(&h1m);
5057
5058 /* Use the same http version than the request. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005059 status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
5060 reason = luactx->appctx->ctx.hlua_apphttp.reason;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005061 if (reason == NULL)
Willy Tarreau7e702d12021-04-28 17:59:21 +02005062 reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status);
5063 if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005064 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5065 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
5066 }
5067 else {
5068 flags = HTX_SL_F_IS_RESP;
5069 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
5070 }
5071 if (!sl) {
5072 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005073 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005074 WILL_LJMP(lua_error(L));
5075 }
Willy Tarreau7e702d12021-04-28 17:59:21 +02005076 sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005077
5078 /* Get the array associated to the field "response" in the object AppletHTTP. */
5079 lua_pushvalue(L, 0);
5080 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5081 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005082 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005083 WILL_LJMP(lua_error(L));
5084 }
5085
5086 /* Browse the list of headers. */
5087 lua_pushnil(L);
5088 while(lua_next(L, -2) != 0) {
5089 /* We expect a string as -2. */
5090 if (lua_type(L, -2) != LUA_TSTRING) {
5091 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005092 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005093 lua_typename(L, lua_type(L, -2)));
5094 WILL_LJMP(lua_error(L));
5095 }
5096 name = lua_tolstring(L, -2, &nlen);
5097
5098 /* We expect an array as -1. */
5099 if (lua_type(L, -1) != LUA_TTABLE) {
5100 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 +02005101 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005102 name,
5103 lua_typename(L, lua_type(L, -1)));
5104 WILL_LJMP(lua_error(L));
5105 }
5106
5107 /* Browse the table who is on the top of the stack. */
5108 lua_pushnil(L);
5109 while(lua_next(L, -2) != 0) {
5110 int id;
5111
5112 /* We expect a number as -2. */
5113 if (lua_type(L, -2) != LUA_TNUMBER) {
5114 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 +02005115 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005116 name,
5117 lua_typename(L, lua_type(L, -2)));
5118 WILL_LJMP(lua_error(L));
5119 }
5120 id = lua_tointeger(L, -2);
5121
5122 /* We expect a string as -2. */
5123 if (lua_type(L, -1) != LUA_TSTRING) {
5124 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 +02005125 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005126 name, id,
5127 lua_typename(L, lua_type(L, -1)));
5128 WILL_LJMP(lua_error(L));
5129 }
5130 value = lua_tolstring(L, -1, &vlen);
5131
5132 /* Simple Protocol checks. */
5133 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005134 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005135 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
5136 struct ist v = ist2(value, vlen);
5137 int ret;
5138
5139 ret = h1_parse_cont_len_header(&h1m, &v);
5140 if (ret < 0) {
5141 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005142 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005143 name);
5144 WILL_LJMP(lua_error(L));
5145 }
5146 else if (ret == 0)
5147 goto next; /* Skip it */
5148 }
5149
5150 /* Add a new header */
5151 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
5152 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005153 luactx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005154 name);
5155 WILL_LJMP(lua_error(L));
5156 }
5157 next:
5158 /* Remove the array from the stack, and get next element with a remaining string. */
5159 lua_pop(L, 1);
5160 }
5161
5162 /* Remove the array from the stack, and get next element with a remaining string. */
5163 lua_pop(L, 1);
5164 }
5165
5166 if (h1m.flags & H1_MF_CHNK)
5167 h1m.flags &= ~H1_MF_CLEN;
5168 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5169 h1m.flags |= H1_MF_XFER_LEN;
5170
5171 /* Uset HTX start-line flags */
5172 if (h1m.flags & H1_MF_XFER_ENC)
5173 flags |= HTX_SL_F_XFER_ENC;
5174 if (h1m.flags & H1_MF_XFER_LEN) {
5175 flags |= HTX_SL_F_XFER_LEN;
5176 if (h1m.flags & H1_MF_CHNK)
5177 flags |= HTX_SL_F_CHNK;
5178 else if (h1m.flags & H1_MF_CLEN)
5179 flags |= HTX_SL_F_CLEN;
5180 if (h1m.body_len == 0)
5181 flags |= HTX_SL_F_BODYLESS;
5182 }
5183 sl->flags |= flags;
5184
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07005185 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005186 * and the status code implies the presence of a message body, we must
5187 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005188 * for the keepalive compliance. If the applet announces a transfer-encoding
5189 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005190 */
5191 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
Willy Tarreau7e702d12021-04-28 17:59:21 +02005192 luactx->appctx->ctx.hlua_apphttp.status >= 200 &&
5193 luactx->appctx->ctx.hlua_apphttp.status != 204 &&
5194 luactx->appctx->ctx.hlua_apphttp.status != 304) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005195 /* Add a new header */
5196 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
5197 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
5198 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005199 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005200 WILL_LJMP(lua_error(L));
5201 }
5202 }
5203
5204 /* Finalize headers. */
5205 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
5206 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Willy Tarreau7e702d12021-04-28 17:59:21 +02005207 luactx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005208 WILL_LJMP(lua_error(L));
5209 }
5210
5211 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
5212 b_reset(&res->buf);
5213 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5214 WILL_LJMP(lua_error(L));
5215 }
5216
5217 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01005218 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005219
5220 /* Headers sent, set the flag. */
Willy Tarreau7e702d12021-04-28 17:59:21 +02005221 luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005222 return 0;
5223
5224}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005225/* We will build the status line and the headers of the HTTP response.
5226 * We will try send at once if its not possible, we give back the hand
5227 * waiting for more room.
5228 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005229__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005230{
Willy Tarreau7e702d12021-04-28 17:59:21 +02005231 struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1));
5232 struct stream_interface *si = luactx->appctx->owner;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005233 struct channel *res = si_ic(si);
5234
5235 if (co_data(res)) {
5236 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02005237 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005238 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005239 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005240}
5241
5242
Christopher Fauleta2097962019-07-15 16:25:33 +02005243__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005244{
Christopher Fauleta2097962019-07-15 16:25:33 +02005245 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005246}
5247
Christopher Fauleta2097962019-07-15 16:25:33 +02005248/*
5249 *
5250 *
5251 * Class HTTP
5252 *
5253 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005254 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005255
5256/* Returns a struct hlua_txn if the stack entry "ud" is
5257 * a class stream, otherwise it throws an error.
5258 */
5259__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005260{
Christopher Fauleta2097962019-07-15 16:25:33 +02005261 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
5262}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005263
Christopher Fauleta2097962019-07-15 16:25:33 +02005264/* This function creates and push in the stack a HTTP object
5265 * according with a current TXN.
5266 */
5267static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5268{
5269 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005270
Christopher Fauleta2097962019-07-15 16:25:33 +02005271 /* Check stack size. */
5272 if (!lua_checkstack(L, 3))
5273 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005274
Christopher Fauleta2097962019-07-15 16:25:33 +02005275 /* Create the object: obj[0] = userdata.
5276 * Note that the base of the Converters object is the
5277 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005278 */
Christopher Fauleta2097962019-07-15 16:25:33 +02005279 lua_newtable(L);
5280 htxn = lua_newuserdata(L, sizeof(*htxn));
5281 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005282
5283 htxn->s = txn->s;
5284 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005285 htxn->dir = txn->dir;
5286 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005287
5288 /* Pop a class stream metatable and affect it to the table. */
5289 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5290 lua_setmetatable(L, -2);
5291
5292 return 1;
5293}
5294
5295/* This function creates ans returns an array of HTTP headers.
5296 * This function does not fails. It is used as wrapper with the
5297 * 2 following functions.
5298 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005299__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005300{
Christopher Fauleta2097962019-07-15 16:25:33 +02005301 struct htx *htx;
5302 int32_t pos;
5303
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005304 /* Create the table. */
5305 lua_newtable(L);
5306
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005307
Christopher Fauleta2097962019-07-15 16:25:33 +02005308 htx = htxbuf(&msg->chn->buf);
5309 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5310 struct htx_blk *blk = htx_get_blk(htx, pos);
5311 enum htx_blk_type type = htx_get_blk_type(blk);
5312 struct ist n, v;
5313 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005314
Christopher Fauleta2097962019-07-15 16:25:33 +02005315 if (type == HTX_BLK_HDR) {
5316 n = htx_get_blk_name(htx,blk);
5317 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005318 }
Christopher Fauleta2097962019-07-15 16:25:33 +02005319 else if (type == HTX_BLK_EOH)
5320 break;
5321 else
5322 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005323
Christopher Fauleta2097962019-07-15 16:25:33 +02005324 /* Check for existing entry:
5325 * assume that the table is on the top of the stack, and
5326 * push the key in the stack, the function lua_gettable()
5327 * perform the lookup.
5328 */
5329 lua_pushlstring(L, n.ptr, n.len);
5330 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01005331
Christopher Fauleta2097962019-07-15 16:25:33 +02005332 switch (lua_type(L, -1)) {
5333 case LUA_TNIL:
5334 /* Table not found, create it. */
5335 lua_pop(L, 1); /* remove the nil value. */
5336 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5337 lua_newtable(L); /* create and push empty table. */
5338 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5339 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5340 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01005341 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005342
Christopher Fauleta2097962019-07-15 16:25:33 +02005343 case LUA_TTABLE:
5344 /* Entry found: push the value in the table. */
5345 len = lua_rawlen(L, -1);
5346 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5347 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5348 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5349 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005350
Christopher Fauleta2097962019-07-15 16:25:33 +02005351 default:
5352 /* Other cases are errors. */
5353 hlua_pusherror(L, "internal error during the parsing of headers.");
5354 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005355 }
5356 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005357 return 1;
5358}
5359
5360__LJMP static int hlua_http_req_get_headers(lua_State *L)
5361{
5362 struct hlua_txn *htxn;
5363
5364 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5365 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5366
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005367 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005368 WILL_LJMP(lua_error(L));
5369
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005370 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005371}
5372
5373__LJMP static int hlua_http_res_get_headers(lua_State *L)
5374{
5375 struct hlua_txn *htxn;
5376
5377 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5378 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5379
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005380 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005381 WILL_LJMP(lua_error(L));
5382
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005383 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005384}
5385
5386/* This function replace full header, or just a value in
5387 * the request or in the response. It is a wrapper fir the
5388 * 4 following functions.
5389 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005390__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005391{
5392 size_t name_len;
5393 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5394 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5395 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005396 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005397 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005398
Dragan Dosen26743032019-04-30 15:54:36 +02005399 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005400 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5401
Christopher Fauleta2097962019-07-15 16:25:33 +02005402 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005403 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005404 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005405 return 0;
5406}
5407
5408__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5409{
5410 struct hlua_txn *htxn;
5411
5412 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5413 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5414
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005415 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005416 WILL_LJMP(lua_error(L));
5417
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005418 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005419}
5420
5421__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5422{
5423 struct hlua_txn *htxn;
5424
5425 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5426 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5427
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005428 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005429 WILL_LJMP(lua_error(L));
5430
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005431 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005432}
5433
5434__LJMP static int hlua_http_req_rep_val(lua_State *L)
5435{
5436 struct hlua_txn *htxn;
5437
5438 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5439 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5440
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005441 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005442 WILL_LJMP(lua_error(L));
5443
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005444 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005445}
5446
5447__LJMP static int hlua_http_res_rep_val(lua_State *L)
5448{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005449 struct hlua_txn *htxn;
5450
5451 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5452 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5453
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005454 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005455 WILL_LJMP(lua_error(L));
5456
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005457 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005458}
5459
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005460/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005461 * It is a wrapper for the 2 following functions.
5462 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005463__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005464{
5465 size_t len;
5466 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005467 struct htx *htx = htxbuf(&msg->chn->buf);
5468 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005469
Christopher Fauleta2097962019-07-15 16:25:33 +02005470 ctx.blk = NULL;
5471 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5472 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005473 return 0;
5474}
5475
5476__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5477{
5478 struct hlua_txn *htxn;
5479
5480 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5481 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5482
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005483 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005484 WILL_LJMP(lua_error(L));
5485
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005486 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005487}
5488
5489__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5490{
5491 struct hlua_txn *htxn;
5492
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005493 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005494 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5495
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005496 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005497 WILL_LJMP(lua_error(L));
5498
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005499 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005500}
5501
5502/* This function adds an header. It is a wrapper used by
5503 * the 2 following functions.
5504 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005505__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005506{
5507 size_t name_len;
5508 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5509 size_t value_len;
5510 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005511 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005512
Christopher Fauleta2097962019-07-15 16:25:33 +02005513 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5514 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005515 return 0;
5516}
5517
5518__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5519{
5520 struct hlua_txn *htxn;
5521
5522 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5523 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5524
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005525 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005526 WILL_LJMP(lua_error(L));
5527
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005528 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005529}
5530
5531__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5532{
5533 struct hlua_txn *htxn;
5534
5535 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5536 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5537
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005538 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005539 WILL_LJMP(lua_error(L));
5540
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005541 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005542}
5543
5544static int hlua_http_req_set_hdr(lua_State *L)
5545{
5546 struct hlua_txn *htxn;
5547
5548 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5549 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5550
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005551 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005552 WILL_LJMP(lua_error(L));
5553
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005554 hlua_http_del_hdr(L, &htxn->s->txn->req);
5555 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005556}
5557
5558static int hlua_http_res_set_hdr(lua_State *L)
5559{
5560 struct hlua_txn *htxn;
5561
5562 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5563 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5564
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005565 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005566 WILL_LJMP(lua_error(L));
5567
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005568 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5569 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005570}
5571
5572/* This function set the method. */
5573static int hlua_http_req_set_meth(lua_State *L)
5574{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005575 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005576 size_t name_len;
5577 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005578
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005579 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005580 WILL_LJMP(lua_error(L));
5581
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005582 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005583 return 1;
5584}
5585
5586/* This function set the method. */
5587static int hlua_http_req_set_path(lua_State *L)
5588{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005589 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005590 size_t name_len;
5591 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005592
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005593 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005594 WILL_LJMP(lua_error(L));
5595
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005596 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005597 return 1;
5598}
5599
5600/* This function set the query-string. */
5601static int hlua_http_req_set_query(lua_State *L)
5602{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005603 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005604 size_t name_len;
5605 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005606
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005607 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005608 WILL_LJMP(lua_error(L));
5609
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005610 /* Check length. */
5611 if (name_len > trash.size - 1) {
5612 lua_pushboolean(L, 0);
5613 return 1;
5614 }
5615
5616 /* Add the mark question as prefix. */
5617 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005618 trash.area[trash.data++] = '?';
5619 memcpy(trash.area + trash.data, name, name_len);
5620 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005621
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005622 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005623 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005624 return 1;
5625}
5626
5627/* This function set the uri. */
5628static int hlua_http_req_set_uri(lua_State *L)
5629{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005630 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005631 size_t name_len;
5632 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005633
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005634 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005635 WILL_LJMP(lua_error(L));
5636
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005637 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005638 return 1;
5639}
5640
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005641/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005642static int hlua_http_res_set_status(lua_State *L)
5643{
5644 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5645 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005646 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5647 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005648
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005649 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005650 WILL_LJMP(lua_error(L));
5651
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005652 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005653 return 0;
5654}
5655
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005656/*
5657 *
5658 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005659 * Class TXN
5660 *
5661 *
5662 */
5663
5664/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005665 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005666 */
5667__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5668{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005669 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005670}
5671
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005672__LJMP static int hlua_set_var(lua_State *L)
5673{
5674 struct hlua_txn *htxn;
5675 const char *name;
5676 size_t len;
5677 struct sample smp;
5678
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005679 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5680 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005681
5682 /* It is useles to retrieve the stream, but this function
5683 * runs only in a stream context.
5684 */
5685 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5686 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5687
5688 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005689 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005690 hlua_lua2smp(L, 3, &smp);
5691
5692 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005693 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005694
5695 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5696 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5697 else
5698 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5699
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005700 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005701}
5702
Christopher Faulet85d79c92016-11-09 16:54:56 +01005703__LJMP static int hlua_unset_var(lua_State *L)
5704{
5705 struct hlua_txn *htxn;
5706 const char *name;
5707 size_t len;
5708 struct sample smp;
5709
5710 MAY_LJMP(check_args(L, 2, "unset_var"));
5711
5712 /* It is useles to retrieve the stream, but this function
5713 * runs only in a stream context.
5714 */
5715 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5716 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5717
5718 /* Unset the variable. */
5719 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005720 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5721 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005722}
5723
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005724__LJMP static int hlua_get_var(lua_State *L)
5725{
5726 struct hlua_txn *htxn;
5727 const char *name;
5728 size_t len;
5729 struct sample smp;
5730
5731 MAY_LJMP(check_args(L, 2, "get_var"));
5732
5733 /* It is useles to retrieve the stream, but this function
5734 * runs only in a stream context.
5735 */
5736 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5737 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5738
Willy Tarreau7560dd42016-03-10 16:28:58 +01005739 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005740 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005741 lua_pushnil(L);
5742 return 1;
5743 }
5744
5745 return hlua_smp2lua(L, &smp);
5746}
5747
Willy Tarreau59551662015-03-10 14:23:13 +01005748__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005749{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005750 struct hlua *hlua;
5751
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005752 MAY_LJMP(check_args(L, 2, "set_priv"));
5753
Willy Tarreau87b09662015-04-03 00:22:06 +02005754 /* It is useles to retrieve the stream, but this function
5755 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005756 */
5757 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005758
5759 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005760 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005761 if (!hlua)
5762 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005763
5764 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005765 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005766
5767 /* Get and store new value. */
5768 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5769 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5770
5771 return 0;
5772}
5773
Willy Tarreau59551662015-03-10 14:23:13 +01005774__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005775{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005776 struct hlua *hlua;
5777
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005778 MAY_LJMP(check_args(L, 1, "get_priv"));
5779
Willy Tarreau87b09662015-04-03 00:22:06 +02005780 /* It is useles to retrieve the stream, but this function
5781 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005782 */
5783 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005784
5785 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005786 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005787 if (!hlua) {
5788 lua_pushnil(L);
5789 return 1;
5790 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005791
5792 /* Push configuration index in the stack. */
5793 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5794
5795 return 1;
5796}
5797
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005798/* Create stack entry containing a class TXN. This function
5799 * return 0 if the stack does not contains free slots,
5800 * otherwise it returns 1.
5801 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005802static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005803{
Willy Tarreaude491382015-04-06 11:04:28 +02005804 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005805
5806 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005807 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005808 return 0;
5809
5810 /* NOTE: The allocation never fails. The failure
5811 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005812 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005813 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005814 /* Create the object: obj[0] = userdata. */
5815 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005816 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005817 lua_rawseti(L, -2, 0);
5818
Willy Tarreaude491382015-04-06 11:04:28 +02005819 htxn->s = s;
5820 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005821 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005822 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005823
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005824 /* Create the "f" field that contains a list of fetches. */
5825 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005826 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005827 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005828 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005829
5830 /* Create the "sf" field that contains a list of stringsafe fetches. */
5831 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005832 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005833 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005834 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005835
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005836 /* Create the "c" field that contains a list of converters. */
5837 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005838 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005839 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005840 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005841
5842 /* Create the "sc" field that contains a list of stringsafe converters. */
5843 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005844 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005845 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005846 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005847
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005848 /* Create the "req" field that contains the request channel object. */
5849 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005850 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005851 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005852 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005853
5854 /* Create the "res" field that contains the response channel object. */
5855 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005856 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005857 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005858 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005859
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005860 /* Creates the HTTP object is the current proxy allows http. */
5861 lua_pushstring(L, "http");
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01005862 if (IS_HTX_STRM(s)) {
Willy Tarreaude491382015-04-06 11:04:28 +02005863 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005864 return 0;
5865 }
5866 else
5867 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005868 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005869
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005870 /* Pop a class sesison metatable and affect it to the userdata. */
5871 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5872 lua_setmetatable(L, -2);
5873
5874 return 1;
5875}
5876
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005877__LJMP static int hlua_txn_deflog(lua_State *L)
5878{
5879 const char *msg;
5880 struct hlua_txn *htxn;
5881
5882 MAY_LJMP(check_args(L, 2, "deflog"));
5883 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5884 msg = MAY_LJMP(luaL_checkstring(L, 2));
5885
5886 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5887 return 0;
5888}
5889
5890__LJMP static int hlua_txn_log(lua_State *L)
5891{
5892 int level;
5893 const char *msg;
5894 struct hlua_txn *htxn;
5895
5896 MAY_LJMP(check_args(L, 3, "log"));
5897 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5898 level = MAY_LJMP(luaL_checkinteger(L, 2));
5899 msg = MAY_LJMP(luaL_checkstring(L, 3));
5900
5901 if (level < 0 || level >= NB_LOG_LEVELS)
5902 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5903
5904 hlua_sendlog(htxn->s->be, level, msg);
5905 return 0;
5906}
5907
5908__LJMP static int hlua_txn_log_debug(lua_State *L)
5909{
5910 const char *msg;
5911 struct hlua_txn *htxn;
5912
5913 MAY_LJMP(check_args(L, 2, "Debug"));
5914 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5915 msg = MAY_LJMP(luaL_checkstring(L, 2));
5916 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5917 return 0;
5918}
5919
5920__LJMP static int hlua_txn_log_info(lua_State *L)
5921{
5922 const char *msg;
5923 struct hlua_txn *htxn;
5924
5925 MAY_LJMP(check_args(L, 2, "Info"));
5926 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5927 msg = MAY_LJMP(luaL_checkstring(L, 2));
5928 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5929 return 0;
5930}
5931
5932__LJMP static int hlua_txn_log_warning(lua_State *L)
5933{
5934 const char *msg;
5935 struct hlua_txn *htxn;
5936
5937 MAY_LJMP(check_args(L, 2, "Warning"));
5938 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5939 msg = MAY_LJMP(luaL_checkstring(L, 2));
5940 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5941 return 0;
5942}
5943
5944__LJMP static int hlua_txn_log_alert(lua_State *L)
5945{
5946 const char *msg;
5947 struct hlua_txn *htxn;
5948
5949 MAY_LJMP(check_args(L, 2, "Alert"));
5950 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5951 msg = MAY_LJMP(luaL_checkstring(L, 2));
5952 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5953 return 0;
5954}
5955
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005956__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5957{
5958 struct hlua_txn *htxn;
5959 int ll;
5960
5961 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5962 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5963 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5964
5965 if (ll < 0 || ll > 7)
5966 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5967
5968 htxn->s->logs.level = ll;
5969 return 0;
5970}
5971
5972__LJMP static int hlua_txn_set_tos(lua_State *L)
5973{
5974 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005975 int tos;
5976
5977 MAY_LJMP(check_args(L, 2, "set_tos"));
5978 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5979 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5980
Willy Tarreau1a18b542018-12-11 16:37:42 +01005981 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005982 return 0;
5983}
5984
5985__LJMP static int hlua_txn_set_mark(lua_State *L)
5986{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005987 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005988 int mark;
5989
5990 MAY_LJMP(check_args(L, 2, "set_mark"));
5991 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5992 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5993
Lukas Tribus579e3e32019-08-11 18:03:45 +02005994 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005995 return 0;
5996}
5997
Patrick Hemmer268a7072018-05-11 12:52:31 -04005998__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5999{
6000 struct hlua_txn *htxn;
6001
6002 MAY_LJMP(check_args(L, 2, "set_priority_class"));
6003 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6004 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6005 return 0;
6006}
6007
6008__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6009{
6010 struct hlua_txn *htxn;
6011
6012 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6013 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6014 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6015 return 0;
6016}
6017
Christopher Faulet700d9e82020-01-31 12:21:52 +01006018/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05006019 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01006020 * message and terminate the transaction. It returns 1 on success and 0 on
6021 * error. The Reply must be on top of the stack.
6022 */
6023__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
6024{
6025 struct htx *htx;
6026 struct htx_sl *sl;
6027 struct h1m h1m;
6028 const char *status, *reason, *body;
6029 size_t status_len, reason_len, body_len;
6030 int ret, code, flags;
6031
6032 code = 200;
6033 status = "200";
6034 status_len = 3;
6035 ret = lua_getfield(L, -1, "status");
6036 if (ret == LUA_TNUMBER) {
6037 code = lua_tointeger(L, -1);
6038 status = lua_tolstring(L, -1, &status_len);
6039 }
6040 lua_pop(L, 1);
6041
6042 reason = http_get_reason(code);
6043 reason_len = strlen(reason);
6044 ret = lua_getfield(L, -1, "reason");
6045 if (ret == LUA_TSTRING)
6046 reason = lua_tolstring(L, -1, &reason_len);
6047 lua_pop(L, 1);
6048
6049 body = NULL;
6050 body_len = 0;
6051 ret = lua_getfield(L, -1, "body");
6052 if (ret == LUA_TSTRING)
6053 body = lua_tolstring(L, -1, &body_len);
6054 lua_pop(L, 1);
6055
6056 /* Prepare the response before inserting the headers */
6057 h1m_init_res(&h1m);
6058 htx = htx_from_buf(&s->res.buf);
6059 channel_htx_truncate(&s->res, htx);
6060 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
6061 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
6062 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
6063 ist2(status, status_len), ist2(reason, reason_len));
6064 }
6065 else {
6066 flags = HTX_SL_F_IS_RESP;
6067 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
6068 ist2(status, status_len), ist2(reason, reason_len));
6069 }
6070 if (!sl)
6071 goto fail;
6072 sl->info.res.status = code;
6073
6074 /* Push in the stack the "headers" entry. */
6075 ret = lua_getfield(L, -1, "headers");
6076 if (ret != LUA_TTABLE)
6077 goto skip_headers;
6078
6079 lua_pushnil(L);
6080 while (lua_next(L, -2) != 0) {
6081 struct ist name, value;
6082 const char *n, *v;
6083 size_t nlen, vlen;
6084
6085 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
6086 /* Skip element if the key is not a string or if the value is not a table */
6087 goto next_hdr;
6088 }
6089
6090 n = lua_tolstring(L, -2, &nlen);
6091 name = ist2(n, nlen);
6092 if (isteqi(name, ist("content-length"))) {
6093 /* Always skip content-length header. It will be added
6094 * later with the correct len
6095 */
6096 goto next_hdr;
6097 }
6098
6099 /* Loop on header's values */
6100 lua_pushnil(L);
6101 while (lua_next(L, -2)) {
6102 if (!lua_isstring(L, -1)) {
6103 /* Skip the value if it is not a string */
6104 goto next_value;
6105 }
6106
6107 v = lua_tolstring(L, -1, &vlen);
6108 value = ist2(v, vlen);
6109
6110 if (isteqi(name, ist("transfer-encoding")))
6111 h1_parse_xfer_enc_header(&h1m, value);
6112 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
6113 goto fail;
6114
6115 next_value:
6116 lua_pop(L, 1);
6117 }
6118
6119 next_hdr:
6120 lua_pop(L, 1);
6121 }
6122 skip_headers:
6123 lua_pop(L, 1);
6124
6125 /* Update h1m flags: CLEN is set if CHNK is not present */
6126 if (!(h1m.flags & H1_MF_CHNK)) {
6127 const char *clen = ultoa(body_len);
6128
6129 h1m.flags |= H1_MF_CLEN;
6130 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
6131 goto fail;
6132 }
6133 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
6134 h1m.flags |= H1_MF_XFER_LEN;
6135
6136 /* Update HTX start-line flags */
6137 if (h1m.flags & H1_MF_XFER_ENC)
6138 flags |= HTX_SL_F_XFER_ENC;
6139 if (h1m.flags & H1_MF_XFER_LEN) {
6140 flags |= HTX_SL_F_XFER_LEN;
6141 if (h1m.flags & H1_MF_CHNK)
6142 flags |= HTX_SL_F_CHNK;
6143 else if (h1m.flags & H1_MF_CLEN)
6144 flags |= HTX_SL_F_CLEN;
6145 if (h1m.body_len == 0)
6146 flags |= HTX_SL_F_BODYLESS;
6147 }
6148 sl->flags |= flags;
6149
6150
6151 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006152 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01006153 goto fail;
6154
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01006155 htx->flags |= HTX_FL_EOM;
6156
Christopher Faulet700d9e82020-01-31 12:21:52 +01006157 /* Now, forward the response and terminate the transaction */
6158 s->txn->status = code;
6159 htx_to_buf(htx, &s->res.buf);
6160 if (!http_forward_proxy_resp(s, 1))
6161 goto fail;
6162
6163 return 1;
6164
6165 fail:
6166 channel_htx_truncate(&s->res, htx);
6167 return 0;
6168}
6169
6170/* Terminate a transaction if called from a lua action. For TCP streams,
6171 * processing is just aborted. Nothing is returned to the client and all
6172 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
6173 * is forwarded to the client before terminating the transaction. On success,
6174 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
6175 * with ACT_RET_ERR code. If this function is not called from a lua action, it
6176 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006177 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006178__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006179{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006180 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01006181 struct stream *s;
6182 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006183
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006184 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006185
Christopher Faulet700d9e82020-01-31 12:21:52 +01006186 /* If the flags NOTERM is set, we cannot terminate the session, so we
6187 * just end the execution of the current lua code. */
6188 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006189 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006190
Christopher Faulet700d9e82020-01-31 12:21:52 +01006191 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006192 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01006193 struct channel *req = &s->req;
6194 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006195
Christopher Faulet700d9e82020-01-31 12:21:52 +01006196 channel_auto_read(req);
6197 channel_abort(req);
6198 channel_auto_close(req);
6199 channel_erase(req);
6200
6201 res->wex = tick_add_ifset(now_ms, res->wto);
6202 channel_auto_read(res);
6203 channel_auto_close(res);
6204 channel_shutr_now(res);
6205
6206 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
6207 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02006208 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006209
Christopher Faulet700d9e82020-01-31 12:21:52 +01006210 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
6211 /* No reply or invalid reply */
6212 s->txn->status = 0;
6213 http_reply_and_close(s, 0, NULL);
6214 }
6215 else {
6216 /* Remove extra args to have the reply on top of the stack */
6217 if (lua_gettop(L) > 2)
6218 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006219
Christopher Faulet700d9e82020-01-31 12:21:52 +01006220 if (!hlua_txn_forward_reply(L, s)) {
6221 if (!(s->flags & SF_ERR_MASK))
6222 s->flags |= SF_ERR_PRXCOND;
6223 lua_pushinteger(L, ACT_RET_ERR);
6224 WILL_LJMP(hlua_done(L));
6225 return 0; /* Never reached */
6226 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02006227 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006228
Christopher Faulet700d9e82020-01-31 12:21:52 +01006229 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
6230 if (htxn->dir == SMP_OPT_DIR_REQ) {
6231 /* let's log the request time */
6232 s->logs.tv_request = now;
6233 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02006234 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet700d9e82020-01-31 12:21:52 +01006235 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02006236
Christopher Faulet700d9e82020-01-31 12:21:52 +01006237 done:
6238 if (!(s->flags & SF_ERR_MASK))
6239 s->flags |= SF_ERR_LOCAL;
6240 if (!(s->flags & SF_FINST_MASK))
6241 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02006242
Christopher Faulet4ad73102020-03-05 11:07:31 +01006243 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006244 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006245 return 0;
6246}
6247
Christopher Faulet700d9e82020-01-31 12:21:52 +01006248/*
6249 *
6250 *
6251 * Class REPLY
6252 *
6253 *
6254 */
6255
6256/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
6257 * free slots, the function fails and returns 0;
6258 */
6259static int hlua_txn_reply_new(lua_State *L)
6260{
6261 struct hlua_txn *htxn;
6262 const char *reason, *body = NULL;
6263 int ret, status;
6264
6265 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006266 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01006267 hlua_pusherror(L, "txn object is not an HTTP transaction.");
6268 WILL_LJMP(lua_error(L));
6269 }
6270
6271 /* Default value */
6272 status = 200;
6273 reason = http_get_reason(status);
6274
6275 if (lua_istable(L, 2)) {
6276 /* load status and reason from the table argument at index 2 */
6277 ret = lua_getfield(L, 2, "status");
6278 if (ret == LUA_TNIL)
6279 goto reason;
6280 else if (ret != LUA_TNUMBER) {
6281 /* invalid status: ignore the reason */
6282 goto body;
6283 }
6284 status = lua_tointeger(L, -1);
6285
6286 reason:
6287 lua_pop(L, 1); /* restore the stack: remove status */
6288 ret = lua_getfield(L, 2, "reason");
6289 if (ret == LUA_TSTRING)
6290 reason = lua_tostring(L, -1);
6291
6292 body:
6293 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
6294 ret = lua_getfield(L, 2, "body");
6295 if (ret == LUA_TSTRING)
6296 body = lua_tostring(L, -1);
6297 lua_pop(L, 1); /* restore the stack: remove body */
6298 }
6299
6300 /* Create the Reply table */
6301 lua_newtable(L);
6302
6303 /* Add status element */
6304 lua_pushstring(L, "status");
6305 lua_pushinteger(L, status);
6306 lua_settable(L, -3);
6307
6308 /* Add reason element */
6309 reason = http_get_reason(status);
6310 lua_pushstring(L, "reason");
6311 lua_pushstring(L, reason);
6312 lua_settable(L, -3);
6313
6314 /* Add body element, nil if undefined */
6315 lua_pushstring(L, "body");
6316 if (body)
6317 lua_pushstring(L, body);
6318 else
6319 lua_pushnil(L);
6320 lua_settable(L, -3);
6321
6322 /* Add headers element */
6323 lua_pushstring(L, "headers");
6324 lua_newtable(L);
6325
6326 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6327 if (lua_istable(L, 2)) {
6328 /* load headers from the table argument at index 2. If it is a table, copy it. */
6329 ret = lua_getfield(L, 2, "headers");
6330 if (ret == LUA_TTABLE) {
6331 /* stack: [ ... <headers:table>, <table> ] */
6332 lua_pushnil(L);
6333 while (lua_next(L, -2) != 0) {
6334 /* stack: [ ... <headers:table>, <table>, k, v] */
6335 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
6336 /* invalid value type, skip it */
6337 lua_pop(L, 1);
6338 continue;
6339 }
6340
6341
6342 /* Duplicate the key and swap it with the value. */
6343 lua_pushvalue(L, -2);
6344 lua_insert(L, -2);
6345 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
6346
6347 lua_newtable(L);
6348 lua_insert(L, -2);
6349 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6350
6351 if (lua_isstring(L, -1)) {
6352 /* push the value in the inner table */
6353 lua_rawseti(L, -2, 1);
6354 }
6355 else { /* table */
6356 lua_pushnil(L);
6357 while (lua_next(L, -2) != 0) {
6358 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6359 if (!lua_isstring(L, -1)) {
6360 /* invalid value type, skip it*/
6361 lua_pop(L, 1);
6362 continue;
6363 }
6364 /* push the value in the inner table */
6365 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6366 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6367 }
6368 lua_pop(L, 1);
6369 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6370 }
6371
6372 /* push (k,v) on the stack in the headers table:
6373 * stack: [ ... <headers:table>, <table>, k, k, v ]
6374 */
6375 lua_settable(L, -5);
6376 /* stack: [ ... <headers:table>, <table>, k ] */
6377 }
6378 }
6379 lua_pop(L, 1);
6380 }
6381 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6382 lua_settable(L, -3);
6383 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6384
6385 /* Pop a class sesison metatable and affect it to the userdata. */
6386 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6387 lua_setmetatable(L, -2);
6388 return 1;
6389}
6390
6391/* Set the reply status code, and optionally the reason. If no reason is
6392 * provided, the default one corresponding to the status code is used.
6393 */
6394__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6395{
6396 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6397 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6398
6399 /* First argument (self) must be a table */
6400 luaL_checktype(L, 1, LUA_TTABLE);
6401
6402 if (status < 100 || status > 599) {
6403 lua_pushboolean(L, 0);
6404 return 1;
6405 }
6406 if (!reason)
6407 reason = http_get_reason(status);
6408
6409 lua_pushinteger(L, status);
6410 lua_setfield(L, 1, "status");
6411
6412 lua_pushstring(L, reason);
6413 lua_setfield(L, 1, "reason");
6414
6415 lua_pushboolean(L, 1);
6416 return 1;
6417}
6418
6419/* Add a header into the reply object. Each header name is associated to an
6420 * array of values in the "headers" table. If the header name is not found, a
6421 * new entry is created.
6422 */
6423__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6424{
6425 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6426 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6427 int ret;
6428
6429 /* First argument (self) must be a table */
6430 luaL_checktype(L, 1, LUA_TTABLE);
6431
6432 /* Push in the stack the "headers" entry. */
6433 ret = lua_getfield(L, 1, "headers");
6434 if (ret != LUA_TTABLE) {
6435 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6436 WILL_LJMP(lua_error(L));
6437 }
6438
6439 /* check if the header is already registered. If not, register it. */
6440 ret = lua_getfield(L, -1, name);
6441 if (ret == LUA_TNIL) {
6442 /* Entry not found. */
6443 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6444
6445 /* Insert the new header name in the array in the top of the stack.
6446 * It left the new array in the top of the stack.
6447 */
6448 lua_newtable(L);
6449 lua_pushstring(L, name);
6450 lua_pushvalue(L, -2);
6451 lua_settable(L, -4);
6452 }
6453 else if (ret != LUA_TTABLE) {
6454 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6455 WILL_LJMP(lua_error(L));
6456 }
6457
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006458 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006459 * the header value as new entry.
6460 */
6461 lua_pushstring(L, value);
6462 ret = lua_rawlen(L, -2);
6463 lua_rawseti(L, -2, ret + 1);
6464
6465 lua_pushboolean(L, 1);
6466 return 1;
6467}
6468
6469/* Remove all occurrences of a given header name. */
6470__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6471{
6472 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6473 int ret;
6474
6475 /* First argument (self) must be a table */
6476 luaL_checktype(L, 1, LUA_TTABLE);
6477
6478 /* Push in the stack the "headers" entry. */
6479 ret = lua_getfield(L, 1, "headers");
6480 if (ret != LUA_TTABLE) {
6481 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6482 WILL_LJMP(lua_error(L));
6483 }
6484
6485 lua_pushstring(L, name);
6486 lua_pushnil(L);
6487 lua_settable(L, -3);
6488
6489 lua_pushboolean(L, 1);
6490 return 1;
6491}
6492
6493/* Set the reply's body. Overwrite any existing entry. */
6494__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6495{
6496 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6497
6498 /* First argument (self) must be a table */
6499 luaL_checktype(L, 1, LUA_TTABLE);
6500
6501 lua_pushstring(L, payload);
6502 lua_setfield(L, 1, "body");
6503
6504 lua_pushboolean(L, 1);
6505 return 1;
6506}
6507
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006508__LJMP static int hlua_log(lua_State *L)
6509{
6510 int level;
6511 const char *msg;
6512
6513 MAY_LJMP(check_args(L, 2, "log"));
6514 level = MAY_LJMP(luaL_checkinteger(L, 1));
6515 msg = MAY_LJMP(luaL_checkstring(L, 2));
6516
6517 if (level < 0 || level >= NB_LOG_LEVELS)
6518 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6519
6520 hlua_sendlog(NULL, level, msg);
6521 return 0;
6522}
6523
6524__LJMP static int hlua_log_debug(lua_State *L)
6525{
6526 const char *msg;
6527
6528 MAY_LJMP(check_args(L, 1, "debug"));
6529 msg = MAY_LJMP(luaL_checkstring(L, 1));
6530 hlua_sendlog(NULL, LOG_DEBUG, msg);
6531 return 0;
6532}
6533
6534__LJMP static int hlua_log_info(lua_State *L)
6535{
6536 const char *msg;
6537
6538 MAY_LJMP(check_args(L, 1, "info"));
6539 msg = MAY_LJMP(luaL_checkstring(L, 1));
6540 hlua_sendlog(NULL, LOG_INFO, msg);
6541 return 0;
6542}
6543
6544__LJMP static int hlua_log_warning(lua_State *L)
6545{
6546 const char *msg;
6547
6548 MAY_LJMP(check_args(L, 1, "warning"));
6549 msg = MAY_LJMP(luaL_checkstring(L, 1));
6550 hlua_sendlog(NULL, LOG_WARNING, msg);
6551 return 0;
6552}
6553
6554__LJMP static int hlua_log_alert(lua_State *L)
6555{
6556 const char *msg;
6557
6558 MAY_LJMP(check_args(L, 1, "alert"));
6559 msg = MAY_LJMP(luaL_checkstring(L, 1));
6560 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006561 return 0;
6562}
6563
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006564__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006565{
6566 int wakeup_ms = lua_tointeger(L, -1);
6567 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006568 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006569 return 0;
6570}
6571
6572__LJMP static int hlua_sleep(lua_State *L)
6573{
6574 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006575 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006576
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006577 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006578
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006579 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006580 wakeup_ms = tick_add(now_ms, delay);
6581 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006582
Willy Tarreau9635e032018-10-16 17:52:55 +02006583 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006584 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006585}
6586
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006587__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006588{
6589 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006590 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006591
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006592 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006593
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006594 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006595 wakeup_ms = tick_add(now_ms, delay);
6596 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006597
Willy Tarreau9635e032018-10-16 17:52:55 +02006598 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006599 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006600}
6601
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006602/* This functionis an LUA binding. it permits to give back
6603 * the hand at the HAProxy scheduler. It is used when the
6604 * LUA processing consumes a lot of time.
6605 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006606__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006607{
6608 return 0;
6609}
6610
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006611__LJMP static int hlua_yield(lua_State *L)
6612{
Willy Tarreau9635e032018-10-16 17:52:55 +02006613 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006614 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006615}
6616
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006617/* This function change the nice of the currently executed
6618 * task. It is used set low or high priority at the current
6619 * task.
6620 */
Willy Tarreau59551662015-03-10 14:23:13 +01006621__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006622{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006623 struct hlua *hlua;
6624 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006625
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006626 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006627 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006628
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006629 /* Get hlua struct, or NULL if we execute from main lua state */
6630 hlua = hlua_gethlua(L);
6631
6632 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006633 if (!hlua || !hlua->task)
6634 return 0;
6635
6636 if (nice < -1024)
6637 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006638 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006639 nice = 1024;
6640
6641 hlua->task->nice = nice;
6642 return 0;
6643}
6644
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006645/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006646 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6647 * return an E_AGAIN signal, the emmiter of this signal must set a
6648 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006649 *
6650 * Task wrapper are longjmp safe because the only one Lua code
6651 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006652 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006653struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006654{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006655 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006656 enum hlua_exec status;
6657
Christopher Faulet5bc99722018-04-25 10:34:45 +02006658 if (task->thread_mask == MAX_THREADS_MASK)
6659 task_set_affinity(task, tid_bit);
6660
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006661 /* If it is the first call to the task, we must initialize the
6662 * execution timeouts.
6663 */
6664 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006665 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006666
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006667 /* Execute the Lua code. */
6668 status = hlua_ctx_resume(hlua, 1);
6669
6670 switch (status) {
6671 /* finished or yield */
6672 case HLUA_E_OK:
6673 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006674 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006675 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006676 break;
6677
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006678 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006679 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006680 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006681 break;
6682
6683 /* finished with error. */
6684 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006685 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006686 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006687 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006688 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006689 break;
6690
6691 case HLUA_E_ERR:
6692 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006693 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006694 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006695 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006696 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006697 break;
6698 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006699 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006700}
6701
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006702/* This function is an LUA binding that register LUA function to be
6703 * executed after the HAProxy configuration parsing and before the
6704 * HAProxy scheduler starts. This function expect only one LUA
6705 * argument that is a function. This function returns nothing, but
6706 * throws if an error is encountered.
6707 */
6708__LJMP static int hlua_register_init(lua_State *L)
6709{
6710 struct hlua_init_function *init;
6711 int ref;
6712
6713 MAY_LJMP(check_args(L, 1, "register_init"));
6714
6715 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6716
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006717 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006718 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006719 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006720
6721 init->function_ref = ref;
Willy Tarreau2b718102021-04-21 07:32:39 +02006722 LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006723 return 0;
6724}
6725
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006726/* This functio is an LUA binding. It permits to register a task
6727 * executed in parallel of the main HAroxy activity. The task is
6728 * created and it is set in the HAProxy scheduler. It can be called
6729 * from the "init" section, "post init" or during the runtime.
6730 *
6731 * Lua prototype:
6732 *
6733 * <none> core.register_task(<function>)
6734 */
6735static int hlua_register_task(lua_State *L)
6736{
Christopher Faulet5294ec02021-04-12 12:24:47 +02006737 struct hlua *hlua = NULL;
6738 struct task *task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006739 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006740 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006741
6742 MAY_LJMP(check_args(L, 1, "register_task"));
6743
6744 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6745
Thierry Fournier75fc0292020-11-28 13:18:56 +01006746 /* Get the reference state. If the reference is NULL, L is the master
6747 * state, otherwise hlua->T is.
6748 */
6749 hlua = hlua_gethlua(L);
6750 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006751 /* we are in runtime processing */
6752 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006753 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006754 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006755 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006756
Willy Tarreaubafbe012017-11-24 17:34:44 +01006757 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006758 if (!hlua)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006759 goto alloc_error;
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006760 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006761
Thierry Fournier59f11be2020-11-29 00:37:41 +01006762 /* We are in the common lua state, execute the task anywhere,
6763 * otherwise, inherit the current thread identifier
6764 */
6765 if (state_id == 0)
6766 task = task_new(MAX_THREADS_MASK);
6767 else
6768 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006769 if (!task)
Christopher Faulet5294ec02021-04-12 12:24:47 +02006770 goto alloc_error;
Willy Tarreaue09101e2018-10-16 17:37:12 +02006771
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006772 task->context = hlua;
6773 task->process = hlua_process_task;
6774
Thierry Fournier021d9862020-11-28 23:42:03 +01006775 if (!hlua_ctx_init(hlua, state_id, task, 1))
Christopher Faulet5294ec02021-04-12 12:24:47 +02006776 goto alloc_error;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006777
6778 /* Restore the function in the stack. */
6779 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6780 hlua->nargs = 0;
6781
6782 /* Schedule task. */
6783 task_schedule(task, now_ms);
6784
6785 return 0;
Christopher Faulet5294ec02021-04-12 12:24:47 +02006786
6787 alloc_error:
6788 task_destroy(task);
6789 hlua_ctx_destroy(hlua);
6790 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6791 return 0; /* Never reached */
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006792}
6793
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006794/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6795 * doesn't allow "yield" functions because the HAProxy engine cannot
6796 * resume converters.
6797 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006798static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006799{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006800 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006801 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006802 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006803
Willy Tarreaube508f12016-03-10 11:47:01 +01006804 if (!stream)
6805 return 0;
6806
Willy Tarreau87b09662015-04-03 00:22:06 +02006807 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006808 * Lua context can be not initialized. This behavior
6809 * permits to save performances because a systematic
6810 * Lua initialization cause 5% performances loss.
6811 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006812 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006813 struct hlua *hlua;
6814
6815 hlua = pool_alloc(pool_head_hlua);
6816 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006817 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6818 return 0;
6819 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006820 HLUA_INIT(hlua);
6821 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006822 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006823 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6824 return 0;
6825 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006826 }
6827
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006828 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006829 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006830
6831 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006832 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006833 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6834 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006835 else
6836 error = "critical error";
6837 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006838 return 0;
6839 }
6840
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006841 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006842 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006843 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006844 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006845 return 0;
6846 }
6847
6848 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006849 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006850
6851 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006852 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006853 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006854 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006855 return 0;
6856 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006857 hlua_smp2lua(stream->hlua->T, smp);
6858 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006859
6860 /* push keywords in the stack. */
6861 if (arg_p) {
6862 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006863 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006864 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006865 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006866 return 0;
6867 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006868 hlua_arg2lua(stream->hlua->T, arg_p);
6869 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006870 }
6871 }
6872
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006873 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006874 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006875
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006876 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006877 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006878 }
6879
6880 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006881 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006882 /* finished. */
6883 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006884 /* If the stack is empty, the function fails. */
6885 if (lua_gettop(stream->hlua->T) <= 0)
6886 return 0;
6887
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006888 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006889 hlua_lua2smp(stream->hlua->T, -1, smp);
6890 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006891 return 1;
6892
6893 /* yield. */
6894 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006895 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006896 return 0;
6897
6898 /* finished with error. */
6899 case HLUA_E_ERRMSG:
6900 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006901 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006902 fcn->name, lua_tostring(stream->hlua->T, -1));
6903 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006904 return 0;
6905
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006906 case HLUA_E_ETMOUT:
6907 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6908 return 0;
6909
6910 case HLUA_E_NOMEM:
6911 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6912 return 0;
6913
6914 case HLUA_E_YIELD:
6915 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6916 return 0;
6917
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006918 case HLUA_E_ERR:
6919 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006920 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006921 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006922
6923 default:
6924 return 0;
6925 }
6926}
6927
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006928/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6929 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006930 * resume sample-fetches. This function will be called by the sample
6931 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006932 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006933static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6934 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006935{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006936 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006937 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006938 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006939 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006940
Willy Tarreaube508f12016-03-10 11:47:01 +01006941 if (!stream)
6942 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006943
Willy Tarreau87b09662015-04-03 00:22:06 +02006944 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006945 * Lua context can be not initialized. This behavior
6946 * permits to save performances because a systematic
6947 * Lua initialization cause 5% performances loss.
6948 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006949 if (!stream->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006950 struct hlua *hlua;
6951
6952 hlua = pool_alloc(pool_head_hlua);
6953 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006954 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6955 return 0;
6956 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01006957 hlua->T = NULL;
6958 stream->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01006959 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006960 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6961 return 0;
6962 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006963 }
6964
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006965 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006966 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006967
6968 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006969 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006970 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6971 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006972 else
6973 error = "critical error";
6974 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006975 return 0;
6976 }
6977
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006978 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006979 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006980 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006981 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006982 return 0;
6983 }
6984
6985 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006986 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006987
6988 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006989 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006990 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006991 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006992 return 0;
6993 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006994 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006995
6996 /* push keywords in the stack. */
6997 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6998 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006999 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007000 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007001 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007002 return 0;
7003 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007004 hlua_arg2lua(stream->hlua->T, arg_p);
7005 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007006 }
7007
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007008 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007009 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007010
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007011 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007012 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007013 }
7014
7015 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007016 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007017 /* finished. */
7018 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02007019 /* If the stack is empty, the function fails. */
7020 if (lua_gettop(stream->hlua->T) <= 0)
7021 return 0;
7022
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007023 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007024 hlua_lua2smp(stream->hlua->T, -1, smp);
7025 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007026
7027 /* Set the end of execution flag. */
7028 smp->flags &= ~SMP_F_MAY_CHANGE;
7029 return 1;
7030
7031 /* yield. */
7032 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007033 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007034 return 0;
7035
7036 /* finished with error. */
7037 case HLUA_E_ERRMSG:
7038 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007039 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007040 fcn->name, lua_tostring(stream->hlua->T, -1));
7041 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007042 return 0;
7043
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007044 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007045 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
7046 return 0;
7047
7048 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007049 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
7050 return 0;
7051
7052 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007053 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
7054 return 0;
7055
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007056 case HLUA_E_ERR:
7057 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007058 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02007059 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007060
7061 default:
7062 return 0;
7063 }
7064}
7065
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007066/* This function is an LUA binding used for registering
7067 * "sample-conv" functions. It expects a converter name used
7068 * in the haproxy configuration file, and an LUA function.
7069 */
7070__LJMP static int hlua_register_converters(lua_State *L)
7071{
7072 struct sample_conv_kw_list *sck;
7073 const char *name;
7074 int ref;
7075 int len;
Christopher Fauletaa224302021-04-12 14:08:21 +02007076 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007077 struct sample_conv *sc;
7078 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007079
7080 MAY_LJMP(check_args(L, 2, "register_converters"));
7081
7082 /* First argument : converter name. */
7083 name = MAY_LJMP(luaL_checkstring(L, 1));
7084
7085 /* Second argument : lua function. */
7086 ref = MAY_LJMP(hlua_checkfunction(L, 2));
7087
Thierry Fournierf67442e2020-11-28 20:41:07 +01007088 /* Check if the converter is already registered */
7089 trash = get_trash_chunk();
7090 chunk_printf(trash, "lua.%s", name);
7091 sc = find_sample_conv(trash->area, trash->data);
7092 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007093 fcn = sc->private;
7094 if (fcn->function_ref[hlua_state_id] != -1) {
7095 ha_warning("Trying to register converter 'lua.%s' more than once. "
7096 "This will become a hard error in version 2.5.\n", name);
7097 }
7098 fcn->function_ref[hlua_state_id] = ref;
7099 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007100 }
7101
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007102 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007103 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007104 if (!sck)
Christopher Fauletaa224302021-04-12 14:08:21 +02007105 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007106 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007107 if (!fcn)
Christopher Fauletaa224302021-04-12 14:08:21 +02007108 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007109
7110 /* Fill fcn. */
7111 fcn->name = strdup(name);
7112 if (!fcn->name)
Christopher Fauletaa224302021-04-12 14:08:21 +02007113 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007114 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007115
7116 /* List head */
7117 sck->list.n = sck->list.p = NULL;
7118
7119 /* converter keyword. */
7120 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007121 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007122 if (!sck->kw[0].kw)
Christopher Fauletaa224302021-04-12 14:08:21 +02007123 goto alloc_error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007124
7125 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
7126 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01007127 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 +01007128 sck->kw[0].val_args = NULL;
7129 sck->kw[0].in_type = SMP_T_STR;
7130 sck->kw[0].out_type = SMP_T_STR;
7131 sck->kw[0].private = fcn;
7132
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007133 /* Register this new converter */
7134 sample_register_convs(sck);
7135
7136 return 0;
Christopher Fauletaa224302021-04-12 14:08:21 +02007137
7138 alloc_error:
7139 release_hlua_function(fcn);
7140 ha_free(&sck);
7141 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7142 return 0; /* Never reached */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007143}
7144
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007145/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007146 * "sample-fetch" functions. It expects a converter name used
7147 * in the haproxy configuration file, and an LUA function.
7148 */
7149__LJMP static int hlua_register_fetches(lua_State *L)
7150{
7151 const char *name;
7152 int ref;
7153 int len;
7154 struct sample_fetch_kw_list *sfk;
Christopher Faulet2567f182021-04-12 14:11:50 +02007155 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007156 struct sample_fetch *sf;
7157 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007158
7159 MAY_LJMP(check_args(L, 2, "register_fetches"));
7160
7161 /* First argument : sample-fetch name. */
7162 name = MAY_LJMP(luaL_checkstring(L, 1));
7163
7164 /* Second argument : lua function. */
7165 ref = MAY_LJMP(hlua_checkfunction(L, 2));
7166
Thierry Fournierf67442e2020-11-28 20:41:07 +01007167 /* Check if the sample-fetch is already registered */
7168 trash = get_trash_chunk();
7169 chunk_printf(trash, "lua.%s", name);
7170 sf = find_sample_fetch(trash->area, trash->data);
7171 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007172 fcn = sf->private;
7173 if (fcn->function_ref[hlua_state_id] != -1) {
7174 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
7175 "This will become a hard error in version 2.5.\n", name);
7176 }
7177 fcn->function_ref[hlua_state_id] = ref;
7178 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007179 }
7180
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007181 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007182 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007183 if (!sfk)
Christopher Faulet2567f182021-04-12 14:11:50 +02007184 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007185 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007186 if (!fcn)
Christopher Faulet2567f182021-04-12 14:11:50 +02007187 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007188
7189 /* Fill fcn. */
7190 fcn->name = strdup(name);
7191 if (!fcn->name)
Christopher Faulet2567f182021-04-12 14:11:50 +02007192 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01007193 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007194
7195 /* List head */
7196 sfk->list.n = sfk->list.p = NULL;
7197
7198 /* sample-fetch keyword. */
7199 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007200 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007201 if (!sfk->kw[0].kw)
Christopher Faulet2567f182021-04-12 14:11:50 +02007202 goto alloc_error;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007203
7204 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
7205 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01007206 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 +01007207 sfk->kw[0].val_args = NULL;
7208 sfk->kw[0].out_type = SMP_T_STR;
7209 sfk->kw[0].use = SMP_USE_HTTP_ANY;
7210 sfk->kw[0].val = 0;
7211 sfk->kw[0].private = fcn;
7212
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007213 /* Register this new fetch. */
7214 sample_register_fetches(sfk);
7215
7216 return 0;
Christopher Faulet2567f182021-04-12 14:11:50 +02007217
7218 alloc_error:
7219 release_hlua_function(fcn);
7220 ha_free(&sfk);
7221 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
7222 return 0; /* Never reached */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007223}
7224
Christopher Faulet501465d2020-02-26 14:54:16 +01007225/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007226 */
Christopher Faulet501465d2020-02-26 14:54:16 +01007227__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007228{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007229 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007230 unsigned int delay;
7231 unsigned int wakeup_ms;
7232
Thierry Fournier4234dbd2020-11-28 13:18:23 +01007233 /* Get hlua struct, or NULL if we execute from main lua state */
7234 hlua = hlua_gethlua(L);
7235 if (!hlua) {
7236 return 0;
7237 }
7238
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007239 MAY_LJMP(check_args(L, 1, "wake_time"));
7240
7241 delay = MAY_LJMP(luaL_checkinteger(L, 1));
7242 wakeup_ms = tick_add(now_ms, delay);
7243 hlua->wake_time = wakeup_ms;
7244 return 0;
7245}
7246
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007247/* This function is a wrapper to execute each LUA function declared as an action
7248 * wrapper during the initialisation period. This function may return any
7249 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
7250 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
7251 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007252 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007253static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02007254 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007255{
7256 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007257 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007258 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007259 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007260
7261 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01007262 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
7263 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
7264 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
7265 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007266 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007267 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007268 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007269 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007270
Willy Tarreau87b09662015-04-03 00:22:06 +02007271 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007272 * Lua context can be not initialized. This behavior
7273 * permits to save performances because a systematic
7274 * Lua initialization cause 5% performances loss.
7275 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007276 if (!s->hlua) {
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007277 struct hlua *hlua;
7278
7279 hlua = pool_alloc(pool_head_hlua);
7280 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007281 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007282 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007283 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007284 }
Christopher Faulet1e8433f2021-03-24 15:03:01 +01007285 HLUA_INIT(hlua);
7286 s->hlua = hlua;
Thierry Fournierc7492592020-11-28 23:57:24 +01007287 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 +01007288 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007289 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007290 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007291 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01007292 }
7293
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007294 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007295 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007296
7297 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007298 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007299 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
7300 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01007301 else
7302 error = "critical error";
7303 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007304 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007305 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007306 }
7307
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007308 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007309 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007310 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007311 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007312 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007313 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007314 }
7315
7316 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007317 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 +01007318
Willy Tarreau87b09662015-04-03 00:22:06 +02007319 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02007320 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007321 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007322 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007323 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007324 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007325 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007326 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007327
7328 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007329 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007330 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007331 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007332 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007333 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007334 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007335 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007336 lua_pushstring(s->hlua->T, *arg);
7337 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007338 }
7339
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007340 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007341 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007342
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007343 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007344 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007345 }
7346
7347 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01007348 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007349 /* finished. */
7350 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007351 /* Catch the return value */
7352 if (lua_gettop(s->hlua->T) > 0)
7353 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007354
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007355 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02007356 if (act_ret == ACT_RET_YIELD) {
7357 if (flags & ACT_OPT_FINAL)
7358 goto err_yield;
7359
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007360 if (dir == SMP_OPT_DIR_REQ)
7361 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7362 s->hlua->wake_time);
7363 else
7364 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7365 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01007366 }
7367 goto end;
7368
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007369 /* yield. */
7370 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01007371 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007372 if (dir == SMP_OPT_DIR_REQ)
7373 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
7374 s->hlua->wake_time);
7375 else
7376 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
7377 s->hlua->wake_time);
7378
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007379 /* Some actions can be wake up when a "write" event
7380 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007381 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007382 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007383 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007384 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007385 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007386 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007387 act_ret = ACT_RET_YIELD;
7388 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007389
7390 /* finished with error. */
7391 case HLUA_E_ERRMSG:
7392 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007393 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007394 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007395 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007396 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007397
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007398 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007399 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007400 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007401
7402 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007403 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007404 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007405
7406 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007407 err_yield:
7408 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007409 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007410 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007411 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007412
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007413 case HLUA_E_ERR:
7414 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007415 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007416 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007417
7418 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007419 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007420 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007421
7422 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007423 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007424 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007425 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007426}
7427
Willy Tarreau144f84a2021-03-02 16:09:26 +01007428struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007429{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007430 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007431
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007432 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007433 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007434 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007435}
7436
7437static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7438{
7439 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007440 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007441 struct task *task;
7442 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007443 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007444
Willy Tarreaubafbe012017-11-24 17:34:44 +01007445 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007446 if (!hlua) {
7447 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007448 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007449 return 0;
7450 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007451 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007452 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007453 ctx->ctx.hlua_apptcp.flags = 0;
7454
7455 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007456 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007457 if (!task) {
7458 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007459 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007460 return 0;
7461 }
7462 task->nice = 0;
7463 task->context = ctx;
7464 task->process = hlua_applet_wakeup;
7465 ctx->ctx.hlua_apptcp.task = task;
7466
7467 /* In the execution wrappers linked with a stream, the
7468 * Lua context can be not initialized. This behavior
7469 * permits to save performances because a systematic
7470 * Lua initialization cause 5% performances loss.
7471 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007472 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 +02007473 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007474 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007475 return 0;
7476 }
7477
7478 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007479 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007480
7481 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007482 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007483 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7484 error = lua_tostring(hlua->T, -1);
7485 else
7486 error = "critical error";
7487 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007488 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007489 return 0;
7490 }
7491
7492 /* Check stack available size. */
7493 if (!lua_checkstack(hlua->T, 1)) {
7494 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007495 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007496 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007497 return 0;
7498 }
7499
7500 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007501 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007502
7503 /* Create and and push object stream in the stack. */
7504 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7505 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007506 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007507 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007508 return 0;
7509 }
7510 hlua->nargs = 1;
7511
7512 /* push keywords in the stack. */
7513 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7514 if (!lua_checkstack(hlua->T, 1)) {
7515 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007516 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007517 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007518 return 0;
7519 }
7520 lua_pushstring(hlua->T, *arg);
7521 hlua->nargs++;
7522 }
7523
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007524 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007525
7526 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007527 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007528 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007529
7530 return 1;
7531}
7532
Willy Tarreau60409db2019-08-21 14:14:50 +02007533void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007534{
7535 struct stream_interface *si = ctx->owner;
7536 struct stream *strm = si_strm(si);
7537 struct channel *res = si_ic(si);
7538 struct act_rule *rule = ctx->rule;
7539 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007540 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007541
7542 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007543 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7544 /* eat the whole request */
7545 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007546 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007547 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007548
7549 /* If the stream is disconnect or closed, ldo nothing. */
7550 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7551 return;
7552
7553 /* Execute the function. */
7554 switch (hlua_ctx_resume(hlua, 1)) {
7555 /* finished. */
7556 case HLUA_E_OK:
7557 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7558
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007559 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007560 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007561 res->flags |= CF_READ_NULL;
7562 si_shutr(si);
7563 return;
7564
7565 /* yield. */
7566 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007567 if (hlua->wake_time != TICK_ETERNITY)
7568 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007569 return;
7570
7571 /* finished with error. */
7572 case HLUA_E_ERRMSG:
7573 /* Display log. */
7574 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007575 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007576 lua_pop(hlua->T, 1);
7577 goto error;
7578
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007579 case HLUA_E_ETMOUT:
7580 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007581 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007582 goto error;
7583
7584 case HLUA_E_NOMEM:
7585 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007586 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007587 goto error;
7588
7589 case HLUA_E_YIELD: /* unexpected */
7590 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007591 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007592 goto error;
7593
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007594 case HLUA_E_ERR:
7595 /* Display log. */
7596 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007597 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007598 goto error;
7599
7600 default:
7601 goto error;
7602 }
7603
7604error:
7605
7606 /* For all other cases, just close the stream. */
7607 si_shutw(si);
7608 si_shutr(si);
7609 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7610}
7611
7612static void hlua_applet_tcp_release(struct appctx *ctx)
7613{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007614 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007615 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007616 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007617 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007618}
7619
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007620/* The function returns 1 if the initialisation is complete, 0 if
7621 * an errors occurs and -1 if more data are required for initializing
7622 * the applet.
7623 */
7624static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7625{
7626 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007627 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007628 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007629 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007630 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007631 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007632
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007633 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007634 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007635 if (!hlua) {
7636 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007637 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007638 return 0;
7639 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007640 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007641 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007642 ctx->ctx.hlua_apphttp.left_bytes = -1;
7643 ctx->ctx.hlua_apphttp.flags = 0;
7644
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007645 if (txn->req.flags & HTTP_MSGF_VER_11)
7646 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7647
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007648 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007649 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007650 if (!task) {
7651 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007652 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007653 return 0;
7654 }
7655 task->nice = 0;
7656 task->context = ctx;
7657 task->process = hlua_applet_wakeup;
7658 ctx->ctx.hlua_apphttp.task = task;
7659
7660 /* In the execution wrappers linked with a stream, the
7661 * Lua context can be not initialized. This behavior
7662 * permits to save performances because a systematic
7663 * Lua initialization cause 5% performances loss.
7664 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007665 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 +02007666 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007667 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007668 return 0;
7669 }
7670
7671 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007672 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007673
7674 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007675 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007676 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7677 error = lua_tostring(hlua->T, -1);
7678 else
7679 error = "critical error";
7680 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007681 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007682 return 0;
7683 }
7684
7685 /* Check stack available size. */
7686 if (!lua_checkstack(hlua->T, 1)) {
7687 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007688 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007689 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007690 return 0;
7691 }
7692
7693 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007694 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007695
7696 /* Create and and push object stream in the stack. */
7697 if (!hlua_applet_http_new(hlua->T, ctx)) {
7698 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007699 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007700 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007701 return 0;
7702 }
7703 hlua->nargs = 1;
7704
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007705 /* push keywords in the stack. */
7706 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7707 if (!lua_checkstack(hlua->T, 1)) {
7708 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007709 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007710 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007711 return 0;
7712 }
7713 lua_pushstring(hlua->T, *arg);
7714 hlua->nargs++;
7715 }
7716
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007717 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007718
7719 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007720 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007721
7722 return 1;
7723}
7724
Willy Tarreau60409db2019-08-21 14:14:50 +02007725void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007726{
7727 struct stream_interface *si = ctx->owner;
7728 struct stream *strm = si_strm(si);
7729 struct channel *req = si_oc(si);
7730 struct channel *res = si_ic(si);
7731 struct act_rule *rule = ctx->rule;
7732 struct proxy *px = strm->be;
7733 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7734 struct htx *req_htx, *res_htx;
7735
7736 res_htx = htx_from_buf(&res->buf);
7737
7738 /* If the stream is disconnect or closed, ldo nothing. */
7739 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7740 goto out;
7741
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007742 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007743 if (!b_size(&res->buf)) {
7744 si_rx_room_blk(si);
7745 goto out;
7746 }
7747 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007748 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007749 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7750
7751 /* Set the currently running flag. */
7752 if (!HLUA_IS_RUNNING(hlua) &&
7753 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Christopher Fauletbd878d22021-04-28 10:50:21 +02007754 if (!co_data(req)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007755 si_cant_get(si);
7756 goto out;
7757 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007758 }
7759
7760 /* Executes The applet if it is not done. */
7761 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7762
7763 /* Execute the function. */
7764 switch (hlua_ctx_resume(hlua, 1)) {
7765 /* finished. */
7766 case HLUA_E_OK:
7767 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7768 break;
7769
7770 /* yield. */
7771 case HLUA_E_AGAIN:
7772 if (hlua->wake_time != TICK_ETERNITY)
7773 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007774 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007775
7776 /* finished with error. */
7777 case HLUA_E_ERRMSG:
7778 /* Display log. */
7779 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007780 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007781 lua_pop(hlua->T, 1);
7782 goto error;
7783
7784 case HLUA_E_ETMOUT:
7785 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007786 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007787 goto error;
7788
7789 case HLUA_E_NOMEM:
7790 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007791 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007792 goto error;
7793
7794 case HLUA_E_YIELD: /* unexpected */
7795 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007796 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007797 goto error;
7798
7799 case HLUA_E_ERR:
7800 /* Display log. */
7801 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007802 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007803 goto error;
7804
7805 default:
7806 goto error;
7807 }
7808 }
7809
7810 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007811 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7812 goto done;
7813
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007814 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7815 goto error;
7816
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007817 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
7818 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007819 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7820 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007821 }
7822
7823 done:
7824 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007825 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007826 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007827 si_shutr(si);
7828 }
7829
7830 /* eat the whole request */
7831 if (co_data(req)) {
7832 req_htx = htx_from_buf(&req->buf);
7833 co_htx_skip(req, req_htx, co_data(req));
7834 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007835 }
7836 }
7837
7838 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007839 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007840 return;
7841
7842 error:
7843
7844 /* If we are in HTTP mode, and we are not send any
7845 * data, return a 500 server error in best effort:
7846 * if there is no room available in the buffer,
7847 * just close the connection.
7848 */
7849 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007850 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007851
7852 channel_erase(res);
7853 res->buf.data = b_data(err);
7854 memcpy(res->buf.area, b_head(err), b_data(err));
7855 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007856 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007857 }
7858 if (!(strm->flags & SF_ERR_MASK))
7859 strm->flags |= SF_ERR_RESOURCE;
7860 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7861 goto done;
7862}
7863
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007864static void hlua_applet_http_release(struct appctx *ctx)
7865{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007866 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007867 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007868 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007869 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007870}
7871
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007872/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007873 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007874 *
7875 * This function can fail with an abort() due to an Lua critical error.
7876 * We are in the configuration parsing process of HAProxy, this abort() is
7877 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007878 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007879static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7880 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007881{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007882 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007883 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007884
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007885 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007886 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007887 if (!rule->arg.hlua_rule) {
7888 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007889 goto error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007890 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007891
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007892 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007893 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7894 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007895 if (!rule->arg.hlua_rule->args) {
7896 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007897 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007898 }
7899
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007900 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007901 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007902
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007903 /* Expect some arguments */
7904 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007905 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007906 memprintf(err, "expect %d arguments", fcn->nargs);
Christopher Faulet528526f2021-04-12 14:37:32 +02007907 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007908 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007909 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007910 if (!rule->arg.hlua_rule->args[i]) {
7911 memprintf(err, "out of memory error");
Christopher Faulet528526f2021-04-12 14:37:32 +02007912 goto error;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007913 }
7914 (*cur_arg)++;
7915 }
7916 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007917
Thierry FOURNIER42148732015-09-02 17:17:33 +02007918 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007919 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007920 return ACT_RET_PRS_OK;
Christopher Faulet528526f2021-04-12 14:37:32 +02007921
7922 error:
7923 if (rule->arg.hlua_rule) {
7924 if (rule->arg.hlua_rule->args) {
7925 for (i = 0; i < fcn->nargs; i++)
7926 ha_free(&rule->arg.hlua_rule->args[i]);
7927 ha_free(&rule->arg.hlua_rule->args);
7928 }
7929 ha_free(&rule->arg.hlua_rule);
7930 }
7931 return ACT_RET_PRS_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007932}
7933
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007934static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7935 struct act_rule *rule, char **err)
7936{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007937 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007938
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007939 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007940 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007941 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007942 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007943 * the call of this analyzer.
7944 */
7945 if (rule->from != ACT_F_HTTP_REQ) {
7946 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7947 return ACT_RET_PRS_ERR;
7948 }
7949
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007950 /* Memory for the rule. */
7951 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7952 if (!rule->arg.hlua_rule) {
7953 memprintf(err, "out of memory error");
7954 return ACT_RET_PRS_ERR;
7955 }
7956
7957 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007958 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007959
7960 /* TODO: later accept arguments. */
7961 rule->arg.hlua_rule->args = NULL;
7962
7963 /* Add applet pointer in the rule. */
7964 rule->applet.obj_type = OBJ_TYPE_APPLET;
7965 rule->applet.name = fcn->name;
7966 rule->applet.init = hlua_applet_http_init;
7967 rule->applet.fct = hlua_applet_http_fct;
7968 rule->applet.release = hlua_applet_http_release;
7969 rule->applet.timeout = hlua_timeout_applet;
7970
7971 return ACT_RET_PRS_OK;
7972}
7973
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007974/* This function is an LUA binding used for registering
7975 * "sample-conv" functions. It expects a converter name used
7976 * in the haproxy configuration file, and an LUA function.
7977 */
7978__LJMP static int hlua_register_action(lua_State *L)
7979{
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007980 struct action_kw_list *akl = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007981 const char *name;
7982 int ref;
7983 int len;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02007984 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007985 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007986 struct buffer *trash;
7987 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007988
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007989 /* Initialise the number of expected arguments at 0. */
7990 nargs = 0;
7991
7992 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7993 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007994
7995 /* First argument : converter name. */
7996 name = MAY_LJMP(luaL_checkstring(L, 1));
7997
7998 /* Second argument : environment. */
7999 if (lua_type(L, 2) != LUA_TTABLE)
8000 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
8001
8002 /* Third argument : lua function. */
8003 ref = MAY_LJMP(hlua_checkfunction(L, 3));
8004
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008005 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01008006 if (lua_gettop(L) >= 4)
8007 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
8008
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008009 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008010 lua_pushnil(L);
8011 while (lua_next(L, 2) != 0) {
8012 if (lua_type(L, -1) != LUA_TSTRING)
8013 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
8014
Thierry Fournierf67442e2020-11-28 20:41:07 +01008015 /* Check if action exists */
8016 trash = get_trash_chunk();
8017 chunk_printf(trash, "lua.%s", name);
8018 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
8019 akw = tcp_req_cont_action(trash->area);
8020 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
8021 akw = tcp_res_cont_action(trash->area);
8022 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
8023 akw = action_http_req_custom(trash->area);
8024 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
8025 akw = action_http_res_custom(trash->area);
8026 } else {
8027 akw = NULL;
8028 }
8029 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008030 fcn = akw->private;
8031 if (fcn->function_ref[hlua_state_id] != -1) {
8032 ha_warning("Trying to register action 'lua.%s' more than once. "
8033 "This will become a hard error in version 2.5.\n", name);
8034 }
8035 fcn->function_ref[hlua_state_id] = ref;
8036
8037 /* pop the environment string. */
8038 lua_pop(L, 1);
8039 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008040 }
8041
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008042 /* Check required environment. Only accepted "http" or "tcp". */
8043 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008044 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008045 if (!akl)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008046 goto alloc_error;;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008047 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008048 if (!fcn)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008049 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008050
8051 /* Fill fcn. */
8052 fcn->name = strdup(name);
8053 if (!fcn->name)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008054 goto alloc_error;
Thierry Fournierc7492592020-11-28 23:57:24 +01008055 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008056
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008057 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01008058 fcn->nargs = nargs;
8059
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008060 /* List head */
8061 akl->list.n = akl->list.p = NULL;
8062
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008063 /* action keyword. */
8064 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02008065 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008066 if (!akl->kw[0].kw)
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008067 goto alloc_error;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008068
8069 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
8070
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02008071 akl->kw[0].flags = 0;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008072 akl->kw[0].private = fcn;
8073 akl->kw[0].parse = action_register_lua;
8074
8075 /* select the action registering point. */
8076 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
8077 tcp_req_cont_keywords_register(akl);
8078 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
8079 tcp_res_cont_keywords_register(akl);
8080 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
8081 http_req_keywords_register(akl);
8082 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
8083 http_res_keywords_register(akl);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008084 else {
8085 release_hlua_function(fcn);
8086 if (akl)
8087 ha_free((char **)&(akl->kw[0].kw));
8088 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008089 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008090 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
8091 "are expected.", lua_tostring(L, -1)));
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008092 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008093
8094 /* pop the environment string. */
8095 lua_pop(L, 1);
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008096
8097 /* reset for next loop */
8098 akl = NULL;
8099 fcn = NULL;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008100 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008101 return ACT_RET_PRS_OK;
Christopher Faulet4fc9da02021-04-12 15:08:12 +02008102
8103 alloc_error:
8104 release_hlua_function(fcn);
8105 ha_free(&akl);
8106 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8107 return 0; /* Never reached */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008108}
8109
8110static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
8111 struct act_rule *rule, char **err)
8112{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02008113 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008114
Christopher Faulet280f85b2019-07-15 15:02:04 +02008115 if (px->mode == PR_MODE_HTTP) {
8116 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01008117 return ACT_RET_PRS_ERR;
8118 }
8119
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008120 /* Memory for the rule. */
8121 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
8122 if (!rule->arg.hlua_rule) {
8123 memprintf(err, "out of memory error");
8124 return ACT_RET_PRS_ERR;
8125 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008126
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008127 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01008128 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008129
8130 /* TODO: later accept arguments. */
8131 rule->arg.hlua_rule->args = NULL;
8132
8133 /* Add applet pointer in the rule. */
8134 rule->applet.obj_type = OBJ_TYPE_APPLET;
8135 rule->applet.name = fcn->name;
8136 rule->applet.init = hlua_applet_tcp_init;
8137 rule->applet.fct = hlua_applet_tcp_fct;
8138 rule->applet.release = hlua_applet_tcp_release;
8139 rule->applet.timeout = hlua_timeout_applet;
8140
8141 return 0;
8142}
8143
8144/* This function is an LUA binding used for registering
8145 * "sample-conv" functions. It expects a converter name used
8146 * in the haproxy configuration file, and an LUA function.
8147 */
8148__LJMP static int hlua_register_service(lua_State *L)
8149{
8150 struct action_kw_list *akl;
8151 const char *name;
8152 const char *env;
8153 int ref;
8154 int len;
Christopher Faulet5c028d72021-04-12 15:11:44 +02008155 struct hlua_function *fcn = NULL;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008156 struct buffer *trash;
8157 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008158
8159 MAY_LJMP(check_args(L, 3, "register_service"));
8160
8161 /* First argument : converter name. */
8162 name = MAY_LJMP(luaL_checkstring(L, 1));
8163
8164 /* Second argument : environment. */
8165 env = MAY_LJMP(luaL_checkstring(L, 2));
8166
8167 /* Third argument : lua function. */
8168 ref = MAY_LJMP(hlua_checkfunction(L, 3));
8169
Thierry Fournierf67442e2020-11-28 20:41:07 +01008170 /* Check for service already registered */
8171 trash = get_trash_chunk();
8172 chunk_printf(trash, "lua.%s", name);
8173 akw = service_find(trash->area);
8174 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008175 fcn = akw->private;
8176 if (fcn->function_ref[hlua_state_id] != -1) {
8177 ha_warning("Trying to register service 'lua.%s' more than once. "
8178 "This will become a hard error in version 2.5.\n", name);
8179 }
8180 fcn->function_ref[hlua_state_id] = ref;
8181 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008182 }
8183
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008184 /* Allocate and fill the sample fetch keyword struct. */
8185 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
8186 if (!akl)
Christopher Faulet5c028d72021-04-12 15:11:44 +02008187 goto alloc_error;
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008188 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008189 if (!fcn)
Christopher Faulet5c028d72021-04-12 15:11:44 +02008190 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008191
8192 /* Fill fcn. */
8193 len = strlen("<lua.>") + strlen(name) + 1;
8194 fcn->name = calloc(1, len);
8195 if (!fcn->name)
Christopher Faulet5c028d72021-04-12 15:11:44 +02008196 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008197 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01008198 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008199
8200 /* List head */
8201 akl->list.n = akl->list.p = NULL;
8202
8203 /* converter keyword. */
8204 len = strlen("lua.") + strlen(name) + 1;
8205 akl->kw[0].kw = calloc(1, len);
8206 if (!akl->kw[0].kw)
Christopher Faulet5c028d72021-04-12 15:11:44 +02008207 goto alloc_error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008208
8209 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
8210
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01008211 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008212 if (strcmp(env, "tcp") == 0)
8213 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008214 else if (strcmp(env, "http") == 0)
8215 akl->kw[0].parse = action_register_service_http;
Christopher Faulet5c028d72021-04-12 15:11:44 +02008216 else {
8217 release_hlua_function(fcn);
8218 if (akl)
8219 ha_free((char **)&(akl->kw[0].kw));
8220 ha_free(&akl);
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008221 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01008222 "'tcp' or 'http' are expected.", env));
Christopher Faulet5c028d72021-04-12 15:11:44 +02008223 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008224
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02008225 akl->kw[0].flags = 0;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008226 akl->kw[0].private = fcn;
8227
8228 /* End of array. */
8229 memset(&akl->kw[1], 0, sizeof(*akl->kw));
8230
8231 /* Register this new converter */
8232 service_keywords_register(akl);
8233
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008234 return 0;
Christopher Faulet5c028d72021-04-12 15:11:44 +02008235
8236 alloc_error:
8237 release_hlua_function(fcn);
8238 ha_free(&akl);
8239 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8240 return 0; /* Never reached */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008241}
8242
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008243/* This function initialises Lua cli handler. It copies the
8244 * arguments in the Lua stack and create channel IO objects.
8245 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02008246static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008247{
8248 struct hlua *hlua;
8249 struct hlua_function *fcn;
8250 int i;
8251 const char *error;
8252
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008253 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008254 appctx->ctx.hlua_cli.fcn = private;
8255
Willy Tarreaubafbe012017-11-24 17:34:44 +01008256 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008257 if (!hlua) {
8258 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008259 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008260 }
8261 HLUA_INIT(hlua);
8262 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008263
8264 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05008265 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008266 * applet_http. It is absolutely compatible.
8267 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01008268 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008269 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01008270 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008271 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008272 }
8273 appctx->ctx.hlua_cli.task->nice = 0;
8274 appctx->ctx.hlua_cli.task->context = appctx;
8275 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
8276
8277 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01008278 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 +01008279 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008280 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008281 }
8282
8283 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008284 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008285 if (lua_type(hlua->T, -1) == LUA_TSTRING)
8286 error = lua_tostring(hlua->T, -1);
8287 else
8288 error = "critical error";
8289 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
8290 goto error;
8291 }
8292
8293 /* Check stack available size. */
8294 if (!lua_checkstack(hlua->T, 2)) {
8295 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8296 goto error;
8297 }
8298
8299 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01008300 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008301
8302 /* Once the arguments parsed, the CLI is like an AppletTCP,
8303 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008304 */
8305 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
8306 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8307 goto error;
8308 }
8309 hlua->nargs = 1;
8310
8311 /* push keywords in the stack. */
8312 for (i = 0; *args[i]; i++) {
8313 /* Check stack available size. */
8314 if (!lua_checkstack(hlua->T, 1)) {
8315 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8316 goto error;
8317 }
8318 lua_pushstring(hlua->T, args[i]);
8319 hlua->nargs++;
8320 }
8321
8322 /* We must initialize the execution timeouts. */
8323 hlua->max_time = hlua_timeout_session;
8324
8325 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008326 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008327
8328 /* It's ok */
8329 return 0;
8330
8331 /* It's not ok. */
8332error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01008333 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008334 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008335 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008336 return 1;
8337}
8338
8339static int hlua_cli_io_handler_fct(struct appctx *appctx)
8340{
8341 struct hlua *hlua;
8342 struct stream_interface *si;
8343 struct hlua_function *fcn;
8344
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008345 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008346 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008347 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008348
8349 /* If the stream is disconnect or closed, ldo nothing. */
8350 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8351 return 1;
8352
8353 /* Execute the function. */
8354 switch (hlua_ctx_resume(hlua, 1)) {
8355
8356 /* finished. */
8357 case HLUA_E_OK:
8358 return 1;
8359
8360 /* yield. */
8361 case HLUA_E_AGAIN:
8362 /* We want write. */
8363 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008364 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008365 /* Set the timeout. */
8366 if (hlua->wake_time != TICK_ETERNITY)
8367 task_schedule(hlua->task, hlua->wake_time);
8368 return 0;
8369
8370 /* finished with error. */
8371 case HLUA_E_ERRMSG:
8372 /* Display log. */
8373 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8374 fcn->name, lua_tostring(hlua->T, -1));
8375 lua_pop(hlua->T, 1);
8376 return 1;
8377
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008378 case HLUA_E_ETMOUT:
8379 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8380 fcn->name);
8381 return 1;
8382
8383 case HLUA_E_NOMEM:
8384 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8385 fcn->name);
8386 return 1;
8387
8388 case HLUA_E_YIELD: /* unexpected */
8389 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8390 fcn->name);
8391 return 1;
8392
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008393 case HLUA_E_ERR:
8394 /* Display log. */
8395 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8396 fcn->name);
8397 return 1;
8398
8399 default:
8400 return 1;
8401 }
8402
8403 return 1;
8404}
8405
8406static void hlua_cli_io_release_fct(struct appctx *appctx)
8407{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008408 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008409 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008410}
8411
8412/* This function is an LUA binding used for registering
8413 * new keywords in the cli. It expects a list of keywords
8414 * which are the "path". It is limited to 5 keywords. A
8415 * description of the command, a function to be executed
8416 * for the parsing and a function for io handlers.
8417 */
8418__LJMP static int hlua_register_cli(lua_State *L)
8419{
8420 struct cli_kw_list *cli_kws;
8421 const char *message;
8422 int ref_io;
8423 int len;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008424 struct hlua_function *fcn = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008425 int index;
8426 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008427 struct buffer *trash;
8428 const char *kw[5];
8429 struct cli_kw *cli_kw;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008430 const char *errmsg;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008431
8432 MAY_LJMP(check_args(L, 3, "register_cli"));
8433
8434 /* First argument : an array of maximum 5 keywords. */
8435 if (!lua_istable(L, 1))
8436 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8437
8438 /* Second argument : string with contextual message. */
8439 message = MAY_LJMP(luaL_checkstring(L, 2));
8440
8441 /* Third and fourth argument : lua function. */
8442 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8443
Thierry Fournierf67442e2020-11-28 20:41:07 +01008444 /* Check for CLI service already registered */
8445 trash = get_trash_chunk();
8446 index = 0;
8447 lua_pushnil(L);
8448 memset(kw, 0, sizeof(kw));
8449 while (lua_next(L, 1) != 0) {
8450 if (index >= CLI_PREFIX_KW_NB)
8451 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8452 if (lua_type(L, -1) != LUA_TSTRING)
8453 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8454 kw[index] = lua_tostring(L, -1);
8455 if (index == 0)
8456 chunk_printf(trash, "%s", kw[index]);
8457 else
8458 chunk_appendf(trash, " %s", kw[index]);
8459 index++;
8460 lua_pop(L, 1);
8461 }
8462 cli_kw = cli_find_kw_exact((char **)kw);
8463 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008464 fcn = cli_kw->private;
8465 if (fcn->function_ref[hlua_state_id] != -1) {
8466 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8467 "This will become a hard error in version 2.5.\n", trash->area);
8468 }
8469 fcn->function_ref[hlua_state_id] = ref_io;
8470 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008471 }
8472
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008473 /* Allocate and fill the sample fetch keyword struct. */
8474 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008475 if (!cli_kws) {
8476 errmsg = "Lua out of memory error.";
8477 goto error;
8478 }
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008479 fcn = new_hlua_function();
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008480 if (!fcn) {
8481 errmsg = "Lua out of memory error.";
8482 goto error;
8483 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008484
8485 /* Fill path. */
8486 index = 0;
8487 lua_pushnil(L);
8488 while(lua_next(L, 1) != 0) {
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008489 if (index >= 5) {
8490 errmsg = "1st argument must be a table with a maximum of 5 entries";
8491 goto error;
8492 }
8493 if (lua_type(L, -1) != LUA_TSTRING) {
8494 errmsg = "1st argument must be a table filled with strings";
8495 goto error;
8496 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008497 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008498 if (!cli_kws->kw[0].str_kw[index]) {
8499 errmsg = "Lua out of memory error.";
8500 goto error;
8501 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008502 index++;
8503 lua_pop(L, 1);
8504 }
8505
8506 /* Copy help message. */
8507 cli_kws->kw[0].usage = strdup(message);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008508 if (!cli_kws->kw[0].usage) {
8509 errmsg = "Lua out of memory error.";
8510 goto error;
8511 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008512
8513 /* Fill fcn io handler. */
8514 len = strlen("<lua.cli>") + 1;
8515 for (i = 0; i < index; i++)
8516 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8517 fcn->name = calloc(1, len);
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008518 if (!fcn->name) {
8519 errmsg = "Lua out of memory error.";
8520 goto error;
8521 }
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008522 strncat((char *)fcn->name, "<lua.cli", len);
8523 for (i = 0; i < index; i++) {
8524 strncat((char *)fcn->name, ".", len);
8525 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8526 }
8527 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008528 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008529
8530 /* Fill last entries. */
8531 cli_kws->kw[0].private = fcn;
8532 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8533 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8534 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8535
8536 /* Register this new converter */
8537 cli_register_kw(cli_kws);
8538
8539 return 0;
Christopher Faulet3a9a12b2021-04-12 15:31:29 +02008540
8541 error:
8542 release_hlua_function(fcn);
8543 if (cli_kws) {
8544 for (i = 0; i < index; i++)
8545 ha_free((char **)&(cli_kws->kw[0].str_kw[i]));
8546 ha_free((char **)&(cli_kws->kw[0].usage));
8547 }
8548 ha_free(&cli_kws);
8549 WILL_LJMP(luaL_error(L, errmsg));
8550 return 0; /* Never reached */
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008551}
8552
Christopher Faulet69c581a2021-05-31 08:54:04 +02008553static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf)
8554{
8555 struct hlua_flt_config *conf = fconf->conf;
8556 lua_State *L;
8557 int error, pos, state_id, flt_ref;
8558
8559 state_id = reg_flt_to_stack_id(conf->reg);
8560 L = hlua_states[state_id];
8561 pos = lua_gettop(L);
8562
8563 /* The filter parsing function */
8564 lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]);
8565
8566 /* Push the filter class on the stack and resolve all callbacks */
8567 lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]);
8568
8569 /* Duplicate the filter class so each filter will have its own copy */
8570 lua_newtable(L);
8571 lua_pushnil(L);
8572
8573 while (lua_next(L, pos+2)) {
8574 lua_pushvalue(L, -2);
8575 lua_insert(L, -2);
8576 lua_settable(L, -4);
8577 }
8578 flt_ref = luaL_ref(L, LUA_REGISTRYINDEX);
8579
8580 /* Remove the original lua filter class from the stack */
8581 lua_pop(L, 1);
8582
8583 /* Push the copy on the stack */
8584 lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref);
8585
8586 /* extra args are pushed in a table */
8587 lua_newtable(L);
8588 for (pos = 0; conf->args[pos]; pos++) {
8589 /* Check stack available size. */
8590 if (!lua_checkstack(L, 1)) {
8591 ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name);
8592 goto error;
8593 }
8594 lua_pushstring(L, conf->args[pos]);
8595 lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
8596 }
8597
8598 error = lua_pcall(L, 2, LUA_MULTRET, 0);
8599 switch (error) {
8600 case LUA_OK:
8601 /* replace the filter ref */
8602 conf->ref[state_id] = flt_ref;
8603 break;
8604 case LUA_ERRRUN:
8605 ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1));
8606 goto error;
8607 case LUA_ERRMEM:
8608 ha_alert("Lua filter '%s' : out of memory error", conf->reg->name);
8609 goto error;
8610 case LUA_ERRERR:
8611 ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1));
8612 goto error;
8613#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
8614 case LUA_ERRGCMM:
8615 ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1));
8616 goto error;
8617#endif
8618 default:
8619 ha_alert("Lua filter '%s' : unknonwn error : %s", conf->reg->name, lua_tostring(L, -1));
8620 goto error;
8621 }
8622
8623 lua_settop(L, 0);
8624 return 0;
8625
8626 error:
8627 lua_settop(L, 0);
8628 return -1;
8629}
8630
8631static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
8632{
8633 struct hlua_flt_config *conf = fconf->conf;
8634 lua_State *L;
8635 int state_id;
8636
8637 if (!conf)
8638 return;
8639
8640 state_id = reg_flt_to_stack_id(conf->reg);
8641 L = hlua_states[state_id];
8642 luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]);
8643}
8644
8645static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf)
8646{
8647 struct hlua_flt_config *conf = fconf->conf;
8648 int state_id = reg_flt_to_stack_id(conf->reg);
8649
8650 /* Rely on per-thread init for global scripts */
8651 if (!state_id)
8652 return hlua_filter_init_per_thread(px, fconf);
8653 return 0;
8654}
8655
8656static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf)
8657{
8658
8659 if (fconf->conf) {
8660 struct hlua_flt_config *conf = fconf->conf;
8661 int state_id = reg_flt_to_stack_id(conf->reg);
8662 int pos;
8663
8664 /* Rely on per-thread deinit for global scripts */
8665 if (!state_id)
8666 hlua_filter_deinit_per_thread(px, fconf);
8667
8668 for (pos = 0; conf->args[pos]; pos++)
8669 free(conf->args[pos]);
8670 free(conf->args);
8671 }
8672 ha_free(&fconf->conf);
8673 ha_free((char **)&fconf->id);
8674 ha_free(&fconf->ops);
8675}
8676
8677static int hlua_filter_new(struct stream *s, struct filter *filter)
8678{
8679 struct hlua_flt_config *conf = FLT_CONF(filter);
8680 struct hlua_flt_ctx *flt_ctx = NULL;
8681 int ret = 1;
8682
8683 /* In the execution wrappers linked with a stream, the
8684 * Lua context can be not initialized. This behavior
8685 * permits to save performances because a systematic
8686 * Lua initialization cause 5% performances loss.
8687 */
8688 if (!s->hlua) {
8689 struct hlua *hlua;
8690
8691 hlua = pool_alloc(pool_head_hlua);
8692 if (!hlua) {
8693 SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n",
8694 conf->reg->name);
8695 ret = 0;
8696 goto end;
8697 }
8698 HLUA_INIT(hlua);
8699 s->hlua = hlua;
8700 if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) {
8701 SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n",
8702 conf->reg->name);
8703 ret = 0;
8704 goto end;
8705 }
8706 }
8707
8708 flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx);
8709 if (!flt_ctx) {
8710 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
8711 conf->reg->name);
8712 ret = 0;
8713 goto end;
8714 }
8715 flt_ctx->hlua[0] = pool_alloc(pool_head_hlua);
8716 flt_ctx->hlua[1] = pool_alloc(pool_head_hlua);
8717 if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) {
8718 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
8719 conf->reg->name);
8720 ret = 0;
8721 goto end;
8722 }
8723 HLUA_INIT(flt_ctx->hlua[0]);
8724 HLUA_INIT(flt_ctx->hlua[1]);
8725 if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) ||
8726 !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) {
8727 SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
8728 conf->reg->name);
8729 ret = 0;
8730 goto end;
8731 }
8732
8733 if (!HLUA_IS_RUNNING(s->hlua)) {
8734 /* The following Lua calls can fail. */
8735 if (!SET_SAFE_LJMP(s->hlua)) {
8736 const char *error;
8737
8738 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
8739 error = lua_tostring(s->hlua->T, -1);
8740 else
8741 error = "critical error";
8742 SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error);
8743 ret = 0;
8744 goto end;
8745 }
8746
8747 /* Check stack size. */
8748 if (!lua_checkstack(s->hlua->T, 1)) {
8749 SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name);
8750 ret = 0;
8751 goto end;
8752 }
8753
8754 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]);
8755 if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) {
8756 SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n",
8757 conf->reg->name);
8758 RESET_SAFE_LJMP(s->hlua);
8759 ret = 0;
8760 goto end;
8761 }
8762 lua_insert(s->hlua->T, -2);
8763
8764 /* Push the copy on the stack */
8765 s->hlua->nargs = 1;
8766
8767 /* We must initialize the execution timeouts. */
8768 s->hlua->max_time = hlua_timeout_session;
8769
8770 /* At this point the execution is safe. */
8771 RESET_SAFE_LJMP(s->hlua);
8772 }
8773
8774 switch (hlua_ctx_resume(s->hlua, 0)) {
8775 case HLUA_E_OK:
8776 /* Nothing returned or not a table, ignore the filter for current stream */
8777 if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) {
8778 ret = 0;
8779 goto end;
8780 }
8781
8782 /* Attached the filter pointer to the ctx */
8783 lua_pushstring(s->hlua->T, "__filter");
8784 lua_pushlightuserdata(s->hlua->T, filter);
8785 lua_settable(s->hlua->T, -3);
8786
8787 /* Save a ref on the filter ctx */
8788 lua_pushvalue(s->hlua->T, 1);
8789 flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX);
8790 filter->ctx = flt_ctx;
8791 break;
8792 case HLUA_E_ERRMSG:
8793 SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1));
8794 ret = -1;
8795 goto end;
8796 case HLUA_E_ETMOUT:
8797 SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name);
8798 ret = 0;
8799 goto end;
8800 case HLUA_E_NOMEM:
8801 SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name);
8802 ret = 0;
8803 goto end;
8804 case HLUA_E_AGAIN:
8805 case HLUA_E_YIELD:
8806 SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()"
8807 " are not allowed from 'new' function.\n", conf->reg->name);
8808 ret = 0;
8809 goto end;
8810 case HLUA_E_ERR:
8811 SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name);
8812 ret = 0;
8813 goto end;
8814 default:
8815 ret = 0;
8816 goto end;
8817 }
8818
8819 end:
8820 if (s->hlua)
8821 lua_settop(s->hlua->T, 0);
8822 if (ret <= 0) {
8823 if (flt_ctx) {
8824 hlua_ctx_destroy(flt_ctx->hlua[0]);
8825 hlua_ctx_destroy(flt_ctx->hlua[1]);
8826 pool_free(pool_head_hlua_flt_ctx, flt_ctx);
8827 }
8828 }
8829 return ret;
8830}
8831
8832static void hlua_filter_delete(struct stream *s, struct filter *filter)
8833{
8834 struct hlua_flt_ctx *flt_ctx = filter->ctx;
8835
8836 luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref);
8837 hlua_ctx_destroy(flt_ctx->hlua[0]);
8838 hlua_ctx_destroy(flt_ctx->hlua[1]);
8839 pool_free(pool_head_hlua_flt_ctx, flt_ctx);
8840 filter->ctx = NULL;
8841}
8842
Christopher Faulet9f55a502020-02-25 15:21:02 +01008843static int hlua_filter_from_payload(struct filter *filter)
8844{
8845 struct hlua_flt_ctx *flt_ctx = filter->ctx;
8846
8847 return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD));
8848}
8849
Christopher Faulet69c581a2021-05-31 08:54:04 +02008850static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px,
8851 struct flt_conf *fconf, char **err, void *private)
8852{
8853 struct hlua_reg_filter *reg_flt = private;
8854 lua_State *L;
8855 struct hlua_flt_config *conf = NULL;
8856 const char *flt_id = NULL;
8857 int state_id, pos, flt_flags = 0;
8858 struct flt_ops *hlua_flt_ops = NULL;
8859
8860 state_id = reg_flt_to_stack_id(reg_flt);
8861 L = hlua_states[state_id];
8862
8863 /* Initialize the filter ops with default callbacks */
8864 hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops));
8865 if (!hlua_flt_ops) {
8866 memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
8867 return -1;
8868 }
8869 hlua_flt_ops->init = hlua_filter_init;
8870 hlua_flt_ops->deinit = hlua_filter_deinit;
8871 if (state_id) {
8872 /* Set per-thread callback if script is loaded per-thread */
8873 hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread;
8874 hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread;
8875 }
8876 hlua_flt_ops->attach = hlua_filter_new;
8877 hlua_flt_ops->detach = hlua_filter_delete;
8878
8879 /* Push the filter class on the stack and resolve all callbacks */
8880 lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]);
8881
8882 /*
8883 * XXX: no callback supported for now
8884 */
8885
8886 /* Get id and flags of the filter class */
8887 if (lua_getfield(L, -1, "id") == LUA_TSTRING)
8888 flt_id = lua_tostring(L, -1);
8889 lua_pop(L, 1);
8890 if (lua_getfield(L, -1, "flags") == LUA_TNUMBER)
8891 flt_flags = lua_tointeger(L, -1);
8892 lua_pop(L, 1);
8893
8894 /* Create the filter config */
8895 conf = calloc(1, sizeof(*conf));
8896 if (!conf) {
8897 memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
8898 goto error;
8899 }
8900 conf->reg = reg_flt;
8901
8902 /* duplicate args */
8903 for (pos = 0; *args[*cur_arg + 1 + pos]; pos++);
8904 conf->args = calloc(pos + 1, sizeof(*conf->args));
8905 for (pos = 0; *args[*cur_arg + 1 + pos]; pos++)
8906 conf->args[pos] = strdup(args[*cur_arg + 1 + pos]);
8907 conf->args[pos] = NULL;
8908 *cur_arg += pos + 1;
8909
8910 fconf->id = strdup(flt_id);
8911 fconf->flags = flt_flags;
8912 fconf->conf = conf;
8913 fconf->ops = hlua_flt_ops;
8914
8915 lua_settop(L, 0);
8916 return 0;
8917
8918 error:
8919 free(hlua_flt_ops);
8920 free(conf);
8921 lua_settop(L, 0);
8922 return -1;
8923}
8924
8925/* This function is an LUA binding used for registering a filter. It expects a
8926 * fileter name used in the haproxy configuration file and a LUA function to
8927 * parse configuration arguments.
8928 */
8929__LJMP static int hlua_register_filter(lua_State *L)
8930{
8931 struct buffer *trash;
8932 struct flt_kw_list *fkl;
8933 struct flt_kw *fkw;
8934 const char *name;
8935 struct hlua_reg_filter *reg_flt= NULL;
8936 int flt_ref, fun_ref;
8937 int len;
8938
8939 MAY_LJMP(check_args(L, 3, "register_filter"));
8940
8941 /* First argument : filter name. */
8942 name = MAY_LJMP(luaL_checkstring(L, 1));
8943
8944 /* Second argument : The filter class */
8945 flt_ref = MAY_LJMP(hlua_checktable(L, 2));
8946
8947 /* Third argument : lua function. */
8948 fun_ref = MAY_LJMP(hlua_checkfunction(L, 3));
8949
8950 trash = get_trash_chunk();
8951 chunk_printf(trash, "lua.%s", name);
8952 fkw = flt_find_kw(trash->area);
8953 if (fkw != NULL) {
8954 reg_flt = fkw->private;
8955 if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) {
8956 ha_warning("Trying to register filter 'lua.%s' more than once. "
8957 "This will become a hard error in version 2.5.\n", name);
8958 }
8959 reg_flt->flt_ref[hlua_state_id] = flt_ref;
8960 reg_flt->fun_ref[hlua_state_id] = fun_ref;
8961 return 0;
8962 }
8963
8964 fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2);
8965 if (!fkl)
8966 goto alloc_error;
8967 fkl->scope = "HLUA";
8968
8969 reg_flt = new_hlua_reg_filter(name);
8970 if (!reg_flt)
8971 goto alloc_error;
8972
8973 reg_flt->flt_ref[hlua_state_id] = flt_ref;
8974 reg_flt->fun_ref[hlua_state_id] = fun_ref;
8975
8976 /* The filter keyword */
8977 len = strlen("lua.") + strlen(name) + 1;
8978 fkl->kw[0].kw = calloc(1, len);
8979 if (!fkl->kw[0].kw)
8980 goto alloc_error;
8981
8982 snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name);
8983
8984 fkl->kw[0].parse = hlua_filter_parse_fct;
8985 fkl->kw[0].private = reg_flt;
8986 memset(&fkl->kw[1], 0, sizeof(*fkl->kw));
8987
8988 /* Register this new filter */
8989 flt_register_keywords(fkl);
8990
8991 return 0;
8992
8993 alloc_error:
8994 release_hlua_reg_filter(reg_flt);
8995 ha_free(&fkl);
8996 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
8997 return 0; /* Never reached */
8998}
8999
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009000static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009001 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009002 char **err, unsigned int *timeout)
9003{
9004 const char *error;
9005
9006 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009007 if (error == PARSE_TIME_OVER) {
9008 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
9009 args[1], args[0]);
9010 return -1;
9011 }
9012 else if (error == PARSE_TIME_UNDER) {
9013 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
9014 args[1], args[0]);
9015 return -1;
9016 }
9017 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009018 memprintf(err, "%s: invalid timeout", args[0]);
9019 return -1;
9020 }
9021 return 0;
9022}
9023
9024static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009025 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009026 char **err)
9027{
9028 return hlua_read_timeout(args, section_type, curpx, defpx,
9029 file, line, err, &hlua_timeout_session);
9030}
9031
9032static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009033 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009034 char **err)
9035{
9036 return hlua_read_timeout(args, section_type, curpx, defpx,
9037 file, line, err, &hlua_timeout_task);
9038}
9039
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009040static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009041 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009042 char **err)
9043{
9044 return hlua_read_timeout(args, section_type, curpx, defpx,
9045 file, line, err, &hlua_timeout_applet);
9046}
9047
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01009048static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009049 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01009050 char **err)
9051{
9052 char *error;
9053
9054 hlua_nb_instruction = strtoll(args[1], &error, 10);
9055 if (*error != '\0') {
9056 memprintf(err, "%s: invalid number", args[0]);
9057 return -1;
9058 }
9059 return 0;
9060}
9061
Willy Tarreau32f61e22015-03-18 17:54:59 +01009062static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009063 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01009064 char **err)
9065{
9066 char *error;
9067
9068 if (*(args[1]) == 0) {
9069 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
9070 return -1;
9071 }
9072 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
9073 if (*error != '\0') {
9074 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
9075 return -1;
9076 }
9077 return 0;
9078}
9079
9080
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009081/* This function is called by the main configuration key "lua-load". It loads and
9082 * execute an lua file during the parsing of the HAProxy configuration file. It is
9083 * the main lua entry point.
9084 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009085 * This function runs with the HAProxy keywords API. It returns -1 if an error
9086 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009087 *
9088 * In some error case, LUA set an error message in top of the stack. This function
9089 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009090 *
9091 * This function can fail with an abort() due to an Lua critical error.
9092 * We are in the configuration parsing process of HAProxy, this abort() is
9093 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009094 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009095static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009096{
9097 int error;
9098
9099 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009100 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009101 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009102 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
9103 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009104 return -1;
9105 }
9106
9107 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009108 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009109 switch (error) {
9110 case LUA_OK:
9111 break;
9112 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009113 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
9114 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009115 return -1;
9116 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01009117 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009118 return -1;
9119 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009120 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
9121 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009122 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02009123#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009124 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009125 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
9126 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009127 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02009128#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009129 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009130 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
9131 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009132 return -1;
9133 }
9134
9135 return 0;
9136}
9137
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009138static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009139 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009140 char **err)
9141{
9142 if (*(args[1]) == 0) {
9143 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
9144 return -1;
9145 }
9146
Thierry Fournier59f11be2020-11-29 00:37:41 +01009147 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01009148 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01009149 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01009150 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01009151}
9152
Thierry Fournier59f11be2020-11-29 00:37:41 +01009153static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009154 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01009155 char **err)
9156{
9157 int len;
9158
9159 if (*(args[1]) == 0) {
9160 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
9161 return -1;
9162 }
9163
9164 if (per_thread_load == NULL) {
9165 /* allocate the first entry large enough to store the final NULL */
9166 per_thread_load = calloc(1, sizeof(*per_thread_load));
9167 if (per_thread_load == NULL) {
9168 memprintf(err, "out of memory error");
9169 return -1;
9170 }
9171 }
9172
9173 /* count used entries */
9174 for (len = 0; per_thread_load[len] != NULL; len++)
9175 ;
9176
9177 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
9178 if (per_thread_load == NULL) {
9179 memprintf(err, "out of memory error");
9180 return -1;
9181 }
9182
9183 per_thread_load[len] = strdup(args[1]);
9184 per_thread_load[len + 1] = NULL;
9185
9186 if (per_thread_load[len] == NULL) {
9187 memprintf(err, "out of memory error");
9188 return -1;
9189 }
9190
9191 /* loading for thread 1 only */
9192 hlua_state_id = 1;
9193 ha_set_tid(0);
9194 return hlua_load_state(args[1], hlua_states[1], err);
9195}
9196
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01009197/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
9198 * in the given <ctx>.
9199 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01009200static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01009201{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01009202 lua_getglobal(L, "package"); /* push package variable */
9203 lua_pushstring(L, path); /* push given path */
9204 lua_pushstring(L, ";"); /* push semicolon */
9205 lua_getfield(L, -3, type); /* push old path */
9206 lua_concat(L, 3); /* concatenate to new path */
9207 lua_setfield(L, -2, type); /* store new path */
9208 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01009209
9210 return 0;
9211}
9212
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01009213static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01009214 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01009215 char **err)
9216{
9217 char *path;
9218 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01009219 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01009220
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01009221 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01009222 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01009223 }
9224
9225 if (!(*args[1])) {
9226 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01009227 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01009228 }
9229 path = args[1];
9230
9231 if (*args[2]) {
9232 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
9233 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01009234 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01009235 }
9236 type = args[2];
9237 }
9238
Thierry Fournier59f11be2020-11-29 00:37:41 +01009239 p = calloc(1, sizeof(*p));
9240 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01009241 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01009242 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01009243 }
9244 p->path = strdup(path);
9245 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01009246 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01009247 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01009248 }
9249 p->type = strdup(type);
9250 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01009251 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01009252 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01009253 }
Willy Tarreau2b718102021-04-21 07:32:39 +02009254 LIST_APPEND(&prepend_path_list, &p->l);
Thierry Fournier59f11be2020-11-29 00:37:41 +01009255
9256 hlua_prepend_path(hlua_states[0], type, path);
9257 hlua_prepend_path(hlua_states[1], type, path);
9258 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01009259
9260err2:
9261 free(p->type);
9262 free(p->path);
9263err:
9264 free(p);
9265 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01009266}
9267
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009268/* configuration keywords declaration */
9269static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01009270 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009271 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01009272 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01009273 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
9274 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02009275 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01009276 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01009277 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01009278 { 0, NULL, NULL },
9279}};
9280
Willy Tarreau0108d902018-11-25 19:14:37 +01009281INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
9282
Christopher Fauletafd8f102018-11-08 11:34:21 +01009283
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009284/* This function can fail with an abort() due to an Lua critical error.
9285 * We are in the initialisation process of HAProxy, this abort() is
9286 * tolerated.
9287 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01009288int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009289{
9290 struct hlua_init_function *init;
9291 const char *msg;
9292 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01009293 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01009294 const char *kind;
9295 const char *trace;
9296 int return_status = 1;
9297#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
9298 int nres;
9299#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009300
Willy Tarreaucdb53462020-12-02 12:12:00 +01009301 /* disable memory limit checks if limit is not set */
9302 if (!hlua_global_allocator.limit)
9303 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
9304
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05009305 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01009306 if (setjmp(safe_ljmp_env) != 0) {
9307 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01009308 if (lua_type(L, -1) == LUA_TSTRING)
9309 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01009310 else
9311 error = "critical error";
9312 fprintf(stderr, "Lua post-init: %s.\n", error);
9313 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01009314 } else {
9315 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01009316 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02009317
Thierry Fournierb8cef172020-11-28 15:37:17 +01009318 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01009319
Thierry Fournierc7492592020-11-28 23:57:24 +01009320 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01009321 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01009322
9323#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01009324 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01009325#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01009326 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01009327#endif
9328 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009329 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01009330
9331 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01009332 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01009333 break;
Thierry Fournier670db242020-11-28 10:49:59 +01009334
9335 case LUA_ERRERR:
9336 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07009337 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01009338 case LUA_ERRRUN:
9339 if (!kind)
9340 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01009341 msg = lua_tostring(L, -1);
9342 lua_settop(L, 0); /* Empty the stack. */
9343 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01009344 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01009345 if (msg)
9346 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
9347 else
9348 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
9349 return_status = 0;
9350 break;
9351
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009352 default:
Thierry Fournier670db242020-11-28 10:49:59 +01009353 /* Unknown error */
9354 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07009355 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01009356 case LUA_YIELD:
9357 /* yield is not configured at this step, this state doesn't happen */
9358 if (!kind)
9359 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07009360 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01009361 case LUA_ERRMEM:
9362 if (!kind)
9363 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01009364 lua_settop(L, 0);
9365 lua_pop(L, 1);
Christopher Fauletd09cc512021-03-24 14:48:45 +01009366 trace = hlua_traceback(L, ", ");
Thierry Fournier670db242020-11-28 10:49:59 +01009367 ha_alert("Lua init: %s: %s\n", kind, trace);
9368 return_status = 0;
9369 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009370 }
Thierry Fournier670db242020-11-28 10:49:59 +01009371 if (!return_status)
9372 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009373 }
Thierry Fournier3c539322020-11-28 16:05:05 +01009374
9375 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01009376 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009377}
9378
Thierry Fournierb8cef172020-11-28 15:37:17 +01009379int hlua_post_init()
9380{
Thierry Fournier59f11be2020-11-29 00:37:41 +01009381 int ret;
9382 int i;
9383 int errors;
9384 char *err = NULL;
9385 struct hlua_function *fcn;
Christopher Faulet69c581a2021-05-31 08:54:04 +02009386 struct hlua_reg_filter *reg_flt;
Thierry Fournier59f11be2020-11-29 00:37:41 +01009387
Thierry Fournierb8cef172020-11-28 15:37:17 +01009388#if USE_OPENSSL
9389 /* Initialize SSL server. */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009390 if (socket_ssl->xprt->prepare_srv) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01009391 int saved_used_backed = global.ssl_used_backend;
9392 // don't affect maxconn automatic computation
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +01009393 socket_ssl->xprt->prepare_srv(socket_ssl);
Thierry Fournierb8cef172020-11-28 15:37:17 +01009394 global.ssl_used_backend = saved_used_backed;
9395 }
9396#endif
9397
Thierry Fournierc7492592020-11-28 23:57:24 +01009398 /* Perform post init of common thread */
9399 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01009400 ha_set_tid(0);
9401 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
9402 if (ret == 0)
9403 return 0;
9404
9405 /* init remaining lua states and load files */
9406 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
9407
9408 /* set thread context */
9409 ha_set_tid(hlua_state_id - 1);
9410
9411 /* Init lua state */
9412 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
9413
9414 /* Load lua files */
9415 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
9416 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
9417 if (ret != 0) {
9418 ha_alert("Lua init: %s\n", err);
9419 return 0;
9420 }
9421 }
9422 }
9423
9424 /* Reset thread context */
9425 ha_set_tid(0);
9426
9427 /* Execute post init for all states */
9428 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
9429
9430 /* set thread context */
9431 ha_set_tid(hlua_state_id - 1);
9432
9433 /* run post init */
9434 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
9435 if (ret == 0)
9436 return 0;
9437 }
9438
9439 /* Reset thread context */
9440 ha_set_tid(0);
9441
9442 /* control functions registering. Each function must have:
9443 * - only the function_ref[0] set positive and all other to -1
9444 * - only the function_ref[0] set to -1 and all other positive
9445 * This ensure a same reference is not used both in shared
9446 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05009447 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01009448 * complicated to found for the end user.
9449 */
9450 errors = 0;
9451 list_for_each_entry(fcn, &referenced_functions, l) {
9452 ret = 0;
9453 for (i = 1; i < global.nbthread + 1; i++) {
9454 if (fcn->function_ref[i] == -1)
9455 ret--;
9456 else
9457 ret++;
9458 }
9459 if (abs(ret) != global.nbthread) {
9460 ha_alert("Lua function '%s' is not referenced in all thread. "
9461 "Expect function in all thread or in none thread.\n", fcn->name);
9462 errors++;
9463 continue;
9464 }
9465
9466 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05009467 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
9468 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01009469 "exclusive.\n", fcn->name);
9470 errors++;
9471 }
9472 }
9473
Christopher Faulet69c581a2021-05-31 08:54:04 +02009474 /* Do the same with registered filters */
9475 list_for_each_entry(reg_flt, &referenced_filters, l) {
9476 ret = 0;
9477 for (i = 1; i < global.nbthread + 1; i++) {
9478 if (reg_flt->flt_ref[i] == -1)
9479 ret--;
9480 else
9481 ret++;
9482 }
9483 if (abs(ret) != global.nbthread) {
9484 ha_alert("Lua filter '%s' is not referenced in all thread. "
9485 "Expect function in all thread or in none thread.\n", reg_flt->name);
9486 errors++;
9487 continue;
9488 }
9489
9490 if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) {
9491 ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) "
9492 "and per-thread Lua context (through lua-load-per-thread). these two context "
9493 "exclusive.\n", fcn->name);
9494 errors++;
9495 }
9496 }
9497
9498
Thierry Fournier59f11be2020-11-29 00:37:41 +01009499 if (errors > 0)
9500 return 0;
9501
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05009502 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01009503 * -1 in order to have probably a segfault if someone use it
9504 */
9505 hlua_state_id = -1;
9506
9507 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01009508}
9509
Willy Tarreau32f61e22015-03-18 17:54:59 +01009510/* The memory allocator used by the Lua stack. <ud> is a pointer to the
9511 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
9512 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01009513 * allocation. <nsize> is the requested new size. A new allocation is
9514 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01009515 * zero. This one verifies that the limits are respected but is optimized
9516 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01009517 */
9518static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
9519{
9520 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01009521 size_t limit, old, new;
9522
Tim Duesterhus22586522021-01-08 10:35:33 +01009523 if (unlikely(!ptr && !nsize))
9524 return NULL;
9525
Willy Tarreaucdb53462020-12-02 12:12:00 +01009526 /* a limit of ~0 means unlimited and boot complete, so there's no need
9527 * for accounting anymore.
9528 */
Christopher Fauletcc2c4f82021-03-24 14:52:24 +01009529 if (likely(~zone->limit == 0))
9530 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01009531
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01009532 if (!ptr)
9533 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01009534
Willy Tarreaucdb53462020-12-02 12:12:00 +01009535 /* enforce strict limits across all threads */
9536 limit = zone->limit;
9537 old = _HA_ATOMIC_LOAD(&zone->allocated);
9538 do {
9539 new = old + nsize - osize;
9540 if (unlikely(nsize && limit && new > limit))
9541 return NULL;
9542 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01009543
9544 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01009545
9546 if (unlikely(!ptr && nsize)) // failed
9547 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
9548
9549 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01009550 return ptr;
9551}
9552
Thierry Fournierecb83c22020-11-28 15:49:44 +01009553/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009554 * We are in the initialisation process of HAProxy, this abort() is
9555 * tolerated.
9556 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01009557lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009558{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009559 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009560 int idx;
9561 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009562 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009563 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01009564 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01009565 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009566 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01009567 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009568
Thierry FOURNIER380d0932015-01-23 14:27:52 +01009569 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009570 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01009571
Thierry Fournier4234dbd2020-11-28 13:18:23 +01009572 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009573 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01009574 *context = NULL;
9575
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009576 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02009577 * the Lua function can fail with an abort. We are in the initialisation
9578 * process of HAProxy, this abort() is tolerated.
9579 */
9580
Thierry Fournier3c539322020-11-28 16:05:05 +01009581 /* Call post initialisation function in safe environment. */
9582 if (setjmp(safe_ljmp_env) != 0) {
9583 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009584 if (lua_type(L, -1) == LUA_TSTRING)
9585 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01009586 else
9587 error_msg = "critical error";
9588 fprintf(stderr, "Lua init: %s.\n", error_msg);
9589 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01009590 } else {
9591 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01009592 }
9593
Thierry FOURNIER380d0932015-01-23 14:27:52 +01009594 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009595 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01009596#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
9597#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
9598#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009599 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01009600#endif
9601#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009602 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01009603#endif
9604#undef HLUA_PREPEND_PATH_TOSTRING
9605#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009606
Thierry Fournier59f11be2020-11-29 00:37:41 +01009607 /* Apply configured prepend path */
9608 list_for_each_entry(pp, &prepend_path_list, l)
9609 hlua_prepend_path(L, pp->type, pp->path);
9610
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009611 /*
9612 *
9613 * Create "core" object.
9614 *
9615 */
9616
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01009617 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009618 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009619
Thierry Fournierecb83c22020-11-28 15:49:44 +01009620 /* set the thread id */
9621 hlua_class_const_int(L, "thread", thread_num);
9622
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009623 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01009624 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009625 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01009626
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009627 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009628 hlua_class_function(L, "register_init", hlua_register_init);
9629 hlua_class_function(L, "register_task", hlua_register_task);
9630 hlua_class_function(L, "register_fetches", hlua_register_fetches);
9631 hlua_class_function(L, "register_converters", hlua_register_converters);
9632 hlua_class_function(L, "register_action", hlua_register_action);
9633 hlua_class_function(L, "register_service", hlua_register_service);
9634 hlua_class_function(L, "register_cli", hlua_register_cli);
Christopher Faulet69c581a2021-05-31 08:54:04 +02009635 hlua_class_function(L, "register_filter", hlua_register_filter);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009636 hlua_class_function(L, "yield", hlua_yield);
9637 hlua_class_function(L, "set_nice", hlua_set_nice);
9638 hlua_class_function(L, "sleep", hlua_sleep);
9639 hlua_class_function(L, "msleep", hlua_msleep);
9640 hlua_class_function(L, "add_acl", hlua_add_acl);
9641 hlua_class_function(L, "del_acl", hlua_del_acl);
9642 hlua_class_function(L, "set_map", hlua_set_map);
9643 hlua_class_function(L, "del_map", hlua_del_map);
9644 hlua_class_function(L, "tcp", hlua_socket_new);
9645 hlua_class_function(L, "log", hlua_log);
9646 hlua_class_function(L, "Debug", hlua_log_debug);
9647 hlua_class_function(L, "Info", hlua_log_info);
9648 hlua_class_function(L, "Warning", hlua_log_warning);
9649 hlua_class_function(L, "Alert", hlua_log_alert);
9650 hlua_class_function(L, "done", hlua_done);
9651 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01009652
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009653 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009654
9655 /*
9656 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009657 * Create "act" object.
9658 *
9659 */
9660
9661 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009662 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009663
9664 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009665 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
9666 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
9667 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
9668 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
9669 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
9670 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
9671 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
9672 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009673
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009674 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01009675
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009676 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01009677
9678 /*
9679 *
Christopher Faulet69c581a2021-05-31 08:54:04 +02009680 * Create "Filter" object.
9681 *
9682 */
9683
9684 /* This table entry is the object "filter" base. */
9685 lua_newtable(L);
9686
9687 /* push flags and constants */
9688 hlua_class_const_int(L, "CONTINUE", 1);
9689 hlua_class_const_int(L, "WAIT", 0);
9690 hlua_class_const_int(L, "ERROR", -1);
9691
9692 hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX);
9693
9694 lua_setglobal(L, "filter");
9695
9696 /*
9697 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009698 * Register class Map
9699 *
9700 */
9701
9702 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009703 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009704
9705 /* register pattern types. */
9706 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009707 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009708 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009709 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009710 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01009711 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009712
9713 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009714 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009715
9716 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009717 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009718
Ilya Shipitsind4259502020-04-08 01:07:56 +05009719 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009720 lua_pushstring(L, "__index");
9721 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009722
9723 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009724 hlua_class_function(L, "lookup", hlua_map_lookup);
9725 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009726
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009727 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009728
Thierry Fournier45e78d72016-02-19 18:34:46 +01009729 /* Register previous table in the registry with reference and named entry.
9730 * The function hlua_register_metatable() pops the stack, so we
9731 * previously create a copy of the table.
9732 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009733 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
9734 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009735
9736 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009737 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009738
9739 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009740 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02009741
9742 /*
9743 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009744 * Register class Channel
9745 *
9746 */
9747
9748 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009749 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009750
Ilya Shipitsind4259502020-04-08 01:07:56 +05009751 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009752 lua_pushstring(L, "__index");
9753 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009754
9755 /* Register . */
Christopher Faulet6a79fc12021-08-06 16:02:36 +02009756 hlua_class_function(L, "data", hlua_channel_get_data);
9757 hlua_class_function(L, "line", hlua_channel_get_line);
9758 hlua_class_function(L, "set", hlua_channel_set_data);
9759 hlua_class_function(L, "remove", hlua_channel_del_data);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009760 hlua_class_function(L, "append", hlua_channel_append);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02009761 hlua_class_function(L, "prepend", hlua_channel_prepend);
9762 hlua_class_function(L, "insert", hlua_channel_insert_data);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009763 hlua_class_function(L, "send", hlua_channel_send);
9764 hlua_class_function(L, "forward", hlua_channel_forward);
Christopher Faulet6a79fc12021-08-06 16:02:36 +02009765 hlua_class_function(L, "input", hlua_channel_get_in_len);
9766 hlua_class_function(L, "output", hlua_channel_get_out_len);
9767 hlua_class_function(L, "may_recv", hlua_channel_may_recv);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009768 hlua_class_function(L, "is_full", hlua_channel_is_full);
9769 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009770
Christopher Faulet6a79fc12021-08-06 16:02:36 +02009771 /* Deprecated API */
9772 hlua_class_function(L, "get", hlua_channel_get);
9773 hlua_class_function(L, "dup", hlua_channel_dup);
9774 hlua_class_function(L, "getline", hlua_channel_getline);
9775 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
9776 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
9777
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009778 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009779
9780 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009781 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01009782
9783 /*
9784 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009785 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009786 *
9787 */
9788
9789 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009790 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009791
Ilya Shipitsind4259502020-04-08 01:07:56 +05009792 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009793 lua_pushstring(L, "__index");
9794 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009795
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009796 /* Browse existing fetches and create the associated
9797 * object method.
9798 */
9799 sf = NULL;
9800 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009801 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9802 * by an underscore.
9803 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009804 strncpy(trash.area, sf->kw, trash.size);
9805 trash.area[trash.size - 1] = '\0';
9806 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009807 if (*p == '.' || *p == '-' || *p == '+')
9808 *p = '_';
9809
9810 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009811 lua_pushstring(L, trash.area);
9812 lua_pushlightuserdata(L, sf);
9813 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
9814 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01009815 }
9816
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009817 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009818
9819 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009820 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009821
9822 /*
9823 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009824 * Register class Converters
9825 *
9826 */
9827
9828 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009829 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009830
9831 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009832 lua_pushstring(L, "__index");
9833 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009834
9835 /* Browse existing converters and create the associated
9836 * object method.
9837 */
9838 sc = NULL;
9839 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009840 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
9841 * by an underscore.
9842 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009843 strncpy(trash.area, sc->kw, trash.size);
9844 trash.area[trash.size - 1] = '\0';
9845 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009846 if (*p == '.' || *p == '-' || *p == '+')
9847 *p = '_';
9848
9849 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009850 lua_pushstring(L, trash.area);
9851 lua_pushlightuserdata(L, sc);
9852 lua_pushcclosure(L, hlua_run_sample_conv, 1);
9853 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009854 }
9855
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009856 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009857
9858 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009859 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01009860
9861 /*
9862 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009863 * Register class HTTP
9864 *
9865 */
9866
9867 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009868 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009869
Ilya Shipitsind4259502020-04-08 01:07:56 +05009870 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009871 lua_pushstring(L, "__index");
9872 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009873
9874 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009875 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
9876 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
9877 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
9878 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
9879 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
9880 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
9881 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
9882 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
9883 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
9884 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009885
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009886 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
9887 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
9888 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
9889 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
9890 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
9891 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
9892 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009893
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009894 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009895
9896 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009897 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01009898
9899 /*
9900 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009901 * Register class AppletTCP
9902 *
9903 */
9904
9905 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009906 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009907
Ilya Shipitsind4259502020-04-08 01:07:56 +05009908 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009909 lua_pushstring(L, "__index");
9910 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009911
9912 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009913 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9914 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9915 hlua_class_function(L, "send", hlua_applet_tcp_send);
9916 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9917 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9918 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9919 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9920 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009921
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009922 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009923
9924 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009925 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009926
9927 /*
9928 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009929 * Register class AppletHTTP
9930 *
9931 */
9932
9933 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009934 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009935
Ilya Shipitsind4259502020-04-08 01:07:56 +05009936 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009937 lua_pushstring(L, "__index");
9938 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009939
9940 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009941 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9942 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9943 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9944 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9945 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9946 hlua_class_function(L, "getline", hlua_applet_http_getline);
9947 hlua_class_function(L, "receive", hlua_applet_http_recv);
9948 hlua_class_function(L, "send", hlua_applet_http_send);
9949 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9950 hlua_class_function(L, "set_status", hlua_applet_http_status);
9951 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009952
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009953 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009954
9955 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009956 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009957
9958 /*
9959 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009960 * Register class TXN
9961 *
9962 */
9963
9964 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009965 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009966
Ilya Shipitsind4259502020-04-08 01:07:56 +05009967 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009968 lua_pushstring(L, "__index");
9969 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009970
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009971 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009972 hlua_class_function(L, "set_priv", hlua_set_priv);
9973 hlua_class_function(L, "get_priv", hlua_get_priv);
9974 hlua_class_function(L, "set_var", hlua_set_var);
9975 hlua_class_function(L, "unset_var", hlua_unset_var);
9976 hlua_class_function(L, "get_var", hlua_get_var);
9977 hlua_class_function(L, "done", hlua_txn_done);
9978 hlua_class_function(L, "reply", hlua_txn_reply_new);
9979 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9980 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9981 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9982 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9983 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9984 hlua_class_function(L, "deflog", hlua_txn_deflog);
9985 hlua_class_function(L, "log", hlua_txn_log);
9986 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9987 hlua_class_function(L, "Info", hlua_txn_log_info);
9988 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9989 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009990
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009991 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009992
9993 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009994 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009995
9996 /*
9997 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009998 * Register class reply
9999 *
10000 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010010001 lua_newtable(L);
10002 lua_pushstring(L, "__index");
10003 lua_newtable(L);
10004 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
10005 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
10006 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
10007 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
10008 lua_settable(L, -3); /* Sets the __index entry. */
10009 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +010010010
10011
10012 /*
10013 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010014 * Register class Socket
10015 *
10016 */
10017
10018 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010010019 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010020
Ilya Shipitsind4259502020-04-08 01:07:56 +050010021 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010010022 lua_pushstring(L, "__index");
10023 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010024
Baptiste Assmann84bb4932015-03-02 21:40:06 +010010025#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +010010026 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +010010027#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +010010028 hlua_class_function(L, "connect", hlua_socket_connect);
10029 hlua_class_function(L, "send", hlua_socket_send);
10030 hlua_class_function(L, "receive", hlua_socket_receive);
10031 hlua_class_function(L, "close", hlua_socket_close);
10032 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
10033 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
10034 hlua_class_function(L, "setoption", hlua_socket_setoption);
10035 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010036
Thierry Fournier1eac28f2020-11-28 12:26:24 +010010037 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010038
10039 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010010040 lua_pushstring(L, "__gc");
10041 lua_pushcclosure(L, hlua_socket_gc, 0);
10042 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010043
10044 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +010010045 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010046
Thierry Fournieraafc7772020-12-04 11:47:47 +010010047 lua_atpanic(L, hlua_panic_safe);
10048
10049 return L;
10050}
10051
10052void hlua_init(void) {
10053 int i;
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010010054 char *errmsg;
Thierry Fournieraafc7772020-12-04 11:47:47 +010010055#ifdef USE_OPENSSL
10056 struct srv_kw *kw;
10057 int tmp_error;
10058 char *error;
10059 char *args[] = { /* SSL client configuration. */
10060 "ssl",
10061 "verify",
10062 "none",
10063 NULL
10064 };
10065#endif
10066
10067 /* Init post init function list head */
10068 for (i = 0; i < MAX_THREADS + 1; i++)
10069 LIST_INIT(&hlua_init_functions[i]);
10070
10071 /* Init state for common/shared lua parts */
10072 hlua_state_id = 0;
10073 ha_set_tid(0);
10074 hlua_states[0] = hlua_init_state(0);
10075
10076 /* Init state 1 for thread 0. We have at least one thread. */
10077 hlua_state_id = 1;
10078 ha_set_tid(0);
10079 hlua_states[1] = hlua_init_state(1);
10080
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010081 /* Proxy and server configuration initialisation. */
William Lallemand6bb77b92021-07-28 15:48:16 +020010082 socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg);
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010010083 if (!socket_proxy) {
10084 fprintf(stderr, "Lua init: %s\n", errmsg);
10085 exit(1);
10086 }
10087 proxy_preset_defaults(socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010088
10089 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010010090 socket_tcp = new_server(socket_proxy);
10091 if (!socket_tcp) {
10092 fprintf(stderr, "Lua init: failed to allocate tcp server socket\n");
10093 exit(1);
10094 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010095
10096#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010097 /* Init TCP server: unchanged parameters */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010010098 socket_ssl = new_server(socket_proxy);
10099 if (!socket_ssl) {
10100 fprintf(stderr, "Lua init: failed to allocate ssl server socket\n");
10101 exit(1);
10102 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010103
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010010104 socket_ssl->use_ssl = 1;
10105 socket_ssl->xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010106
Bertrand Jacquin80839ff2021-01-21 19:14:46 +000010107 for (i = 0; args[i] != NULL; i++) {
10108 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010109 /*
10110 *
10111 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080010112 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010113 * features like client certificates and ssl_verify.
10114 *
10115 */
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010010116 tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010117 if (tmp_error != 0) {
10118 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
10119 abort(); /* This must be never arrives because the command line
10120 not editable by the user. */
10121 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +000010122 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010123 }
10124 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010010125#endif
Thierry Fournier75933d42016-01-21 09:30:18 +010010126
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010010127}
Willy Tarreaubb57d942016-12-21 19:04:56 +010010128
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +020010129static void hlua_deinit()
10130{
Willy Tarreau186f3762020-12-04 11:48:12 +010010131 int thr;
Christopher Faulet69c581a2021-05-31 08:54:04 +020010132 struct hlua_reg_filter *reg_flt, *reg_flt_bck;
10133
10134 list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l)
10135 release_hlua_reg_filter(reg_flt);
Willy Tarreau186f3762020-12-04 11:48:12 +010010136
10137 for (thr = 0; thr < MAX_THREADS+1; thr++) {
10138 if (hlua_states[thr])
10139 lua_close(hlua_states[thr]);
10140 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +010010141
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010010142 free_server(socket_tcp);
Willy Tarreau430bf4a2021-03-04 09:45:32 +010010143
Willy Tarreau0f143af2021-03-05 10:41:48 +010010144#ifdef USE_OPENSSL
Amaury Denoyelle704ba1d2021-03-24 17:57:47 +010010145 free_server(socket_ssl);
Willy Tarreau0f143af2021-03-05 10:41:48 +010010146#endif
Amaury Denoyelle239fdbf2021-03-24 10:22:03 +010010147
10148 free_proxy(socket_proxy);
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +020010149}
10150
10151REGISTER_POST_DEINIT(hlua_deinit);
10152
Willy Tarreau80713382018-11-26 10:19:54 +010010153static void hlua_register_build_options(void)
10154{
Willy Tarreaubb57d942016-12-21 19:04:56 +010010155 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +010010156
Willy Tarreaubb57d942016-12-21 19:04:56 +010010157 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
10158 hap_register_build_opts(ptr, 1);
10159}
Willy Tarreau80713382018-11-26 10:19:54 +010010160
10161INITCALL0(STG_REGISTER, hlua_register_build_options);