blob: ce366479aec9d20914d0c7ccc35ff49c495f34ce [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 Jacquin3dfbd272021-01-21 21:14:07 +000013#define _GNU_SOURCE
14
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020016#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010017
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010018#include <lauxlib.h>
19#include <lua.h>
20#include <lualib.h>
21
Thierry FOURNIER463119c2015-03-10 00:35:36 +010022#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
23#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010024#endif
25
Willy Tarreau8d2b7772020-05-27 10:58:19 +020026#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010027
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/api.h>
29#include <haproxy/applet.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020030#include <haproxy/arg.h>
Christopher Fauletd25d9262020-08-06 11:04:46 +020031#include <haproxy/auth.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020032#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020033#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020035#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020036#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020037#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020038#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020039#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020040#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020042#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020043#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020044#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020045#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020046#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020047#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020048#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020050#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020051#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020052#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020053#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020054#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020055#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020056#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020057#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020059#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020060#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020061#include <haproxy/xref.h>
62
Thierry FOURNIER380d0932015-01-23 14:27:52 +010063
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010064/* Lua uses longjmp to perform yield or throwing errors. This
65 * macro is used only for identifying the function that can
66 * not return because a longjmp is executed.
67 * __LJMP marks a prototype of hlua file that can use longjmp.
68 * WILL_LJMP() marks an lua function that will use longjmp.
69 * MAY_LJMP() marks an lua function that may use longjmp.
70 */
71#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020072#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010073#define MAY_LJMP(func) func
74
Thierry FOURNIERbabae282015-09-17 11:36:37 +020075/* This couple of function executes securely some Lua calls outside of
76 * the lua runtime environment. Each Lua call can return a longjmp
77 * if it encounter a memory error.
78 *
79 * Lua documentation extract:
80 *
81 * If an error happens outside any protected environment, Lua calls
82 * a panic function (see lua_atpanic) and then calls abort, thus
83 * exiting the host application. Your panic function can avoid this
84 * exit by never returning (e.g., doing a long jump to your own
85 * recovery point outside Lua).
86 *
87 * The panic function runs as if it were a message handler (see
88 * §2.3); in particular, the error message is at the top of the
89 * stack. However, there is no guarantee about stack space. To push
90 * anything on the stack, the panic function must first check the
91 * available space (see §4.2).
92 *
93 * We must check all the Lua entry point. This includes:
94 * - The include/proto/hlua.h exported functions
95 * - the task wrapper function
96 * - The action wrapper function
97 * - The converters wrapper function
98 * - The sample-fetch wrapper functions
99 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500100 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800101 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200102 *
103 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
104 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
105 * because they must be exists in the program stack when the longjmp
106 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200107 *
108 * Note that the Lua processing is not really thread safe. It provides
109 * heavy system which consists to add our own lock function in the Lua
110 * code and recompile the library. This system will probably not accepted
111 * by maintainers of various distribs.
112 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500113 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200114 * quick looking on the Lua sources displays a lua_lock() a the start
115 * of function and a lua_unlock() at the end of the function. So I
116 * conclude that the Lua thread safe mode just perform a mutex around
117 * all execution. So I prefer to do this in the HAProxy code, it will be
118 * easier for distro maintainers.
119 *
120 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
121 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
122 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100124__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200125THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100127static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128
129#define SET_SAFE_LJMP(__L) \
130 ({ \
131 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200133 if (setjmp(safe_ljmp_env) != 0) { \
134 lua_atpanic(__L, hlua_panic_safe); \
135 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100136 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200137 } else { \
138 lua_atpanic(__L, hlua_panic_ljmp); \
139 ret = 1; \
140 } \
141 ret; \
142 })
143
144/* If we are the last function catching Lua errors, we
145 * must reset the panic function.
146 */
147#define RESET_SAFE_LJMP(__L) \
148 do { \
149 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100150 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 } while(0)
152
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200153/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100155/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200157/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100158#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100159#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200160
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100161/* The main Lua execution context. */
162struct hlua gL;
163
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100164/* This is the memory pool containing struct lua for applets
165 * (including cli).
166 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100167DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100168
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100169/* Used for Socket connection. */
170static struct proxy socket_proxy;
171static struct server socket_tcp;
172#ifdef USE_OPENSSL
173static struct server socket_ssl;
174#endif
175
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100176/* List head of the function called at the initialisation time. */
177struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
178
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100179/* The following variables contains the reference of the different
180 * Lua classes. These references are useful for identify metadata
181 * associated with an object.
182 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100183static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100184static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100185static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100186static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100187static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100188static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200189static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200190static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200191static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100192static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100193
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200195 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100196 * short timeout. Lua linked with tasks doesn't have a timeout
197 * because a task may remain alive during all the haproxy execution.
198 */
199static unsigned int hlua_timeout_session = 4000; /* session timeout. */
200static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200201static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100202
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100203/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
204 * it is used for preventing infinite loops.
205 *
206 * I test the scheer with an infinite loop containing one incrementation
207 * and one test. I run this loop between 10 seconds, I raise a ceil of
208 * 710M loops from one interrupt each 9000 instructions, so I fix the value
209 * to one interrupt each 10 000 instructions.
210 *
211 * configured | Number of
212 * instructions | loops executed
213 * between two | in milions
214 * forced yields |
215 * ---------------+---------------
216 * 10 | 160
217 * 500 | 670
218 * 1000 | 680
219 * 5000 | 700
220 * 7000 | 700
221 * 8000 | 700
222 * 9000 | 710 <- ceil
223 * 10000 | 710
224 * 100000 | 710
225 * 1000000 | 710
226 *
227 */
228static unsigned int hlua_nb_instruction = 10000;
229
Willy Tarreau32f61e22015-03-18 17:54:59 +0100230/* Descriptor for the memory allocation state. If limit is not null, it will
231 * be enforced on any memory allocation.
232 */
233struct hlua_mem_allocator {
234 size_t allocated;
235 size_t limit;
236};
237
238static struct hlua_mem_allocator hlua_global_allocator;
239
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100240/* These functions converts types between HAProxy internal args or
241 * sample and LUA types. Another function permits to check if the
242 * LUA stack contains arguments according with an required ARG_T
243 * format.
244 */
245static int hlua_arg2lua(lua_State *L, const struct arg *arg);
246static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100247__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100248 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100249static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100250static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100251static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
252
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100253__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200254
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200255#define SEND_ERR(__be, __fmt, __args...) \
256 do { \
257 send_log(__be, LOG_ERR, __fmt, ## __args); \
258 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100259 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200260 } while (0)
261
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100262/* Used to check an Lua function type in the stack. It creates and
263 * returns a reference of the function. This function throws an
264 * error if the rgument is not a "function".
265 */
266__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
267{
268 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100269 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100270 WILL_LJMP(luaL_argerror(L, argno, msg));
271 }
272 lua_pushvalue(L, argno);
273 return luaL_ref(L, LUA_REGISTRYINDEX);
274}
275
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200276/* Return the string that is of the top of the stack. */
277const char *hlua_get_top_error_string(lua_State *L)
278{
279 if (lua_gettop(L) < 1)
280 return "unknown error";
281 if (lua_type(L, -1) != LUA_TSTRING)
282 return "unknown error";
283 return lua_tostring(L, -1);
284}
285
Christopher Faulet5326d6a2021-03-24 14:48:45 +0100286__LJMP const char *hlua_traceback(lua_State *L, const char* sep)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200287{
288 lua_Debug ar;
289 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200290 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200291
292 while (lua_getstack(L, level++, &ar)) {
293
294 /* Add separator */
Christopher Faulet5326d6a2021-03-24 14:48:45 +0100295 if (b_data(msg))
296 chunk_appendf(msg, "%s", sep);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200297
298 /* Fill fields:
299 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
300 * 'l': fills in the field currentline;
301 * 'n': fills in the field name and namewhat;
302 * 't': fills in the field istailcall;
303 */
304 lua_getinfo(L, "Slnt", &ar);
305
306 /* Append code localisation */
307 if (ar.currentline > 0)
Christopher Faulet5326d6a2021-03-24 14:48:45 +0100308 chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200309 else
Christopher Faulet5326d6a2021-03-24 14:48:45 +0100310 chunk_appendf(msg, "%s: ", ar.short_src);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200311
312 /*
313 * Get function name
314 *
315 * if namewhat is no empty, name is defined.
316 * what contains "Lua" for Lua function, "C" for C function,
317 * or "main" for main code.
318 */
319 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
Christopher Faulet5326d6a2021-03-24 14:48:45 +0100320 chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200321
322 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
Christopher Faulet5326d6a2021-03-24 14:48:45 +0100323 chunk_appendf(msg, "in main chunk");
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200324
325 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
Christopher Faulet5326d6a2021-03-24 14:48:45 +0100326 chunk_appendf(msg, "in function line %d", ar.linedefined);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200327
328 else /* nothing left... */
329 chunk_appendf(msg, "?");
330
331
332 /* Display tailed call */
333 if (ar.istailcall)
334 chunk_appendf(msg, " ...");
335 }
336
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200337 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200338}
339
340
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100341/* This function check the number of arguments available in the
342 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500343 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100344 */
345__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
346{
347 if (lua_gettop(L) == nb)
348 return;
349 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
350}
351
Mark Lakes22154b42018-01-29 14:38:40 -0800352/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100353 * and the line number where the error is encountered.
354 */
355static int hlua_pusherror(lua_State *L, const char *fmt, ...)
356{
357 va_list argp;
358 va_start(argp, fmt);
359 luaL_where(L, 1);
360 lua_pushvfstring(L, fmt, argp);
361 va_end(argp);
362 lua_concat(L, 2);
363 return 1;
364}
365
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100366/* This functions is used with sample fetch and converters. It
367 * converts the HAProxy configuration argument in a lua stack
368 * values.
369 *
370 * It takes an array of "arg", and each entry of the array is
371 * converted and pushed in the LUA stack.
372 */
373static int hlua_arg2lua(lua_State *L, const struct arg *arg)
374{
375 switch (arg->type) {
376 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100377 case ARGT_TIME:
378 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100379 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380 break;
381
382 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200383 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100384 break;
385
386 case ARGT_IPV4:
387 case ARGT_IPV6:
388 case ARGT_MSK4:
389 case ARGT_MSK6:
390 case ARGT_FE:
391 case ARGT_BE:
392 case ARGT_TAB:
393 case ARGT_SRV:
394 case ARGT_USR:
395 case ARGT_MAP:
396 default:
397 lua_pushnil(L);
398 break;
399 }
400 return 1;
401}
402
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500403/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100404 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500405 * with sample fetch wrappers. The input arguments are given to the
406 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100407 */
408static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
409{
410 switch (lua_type(L, ud)) {
411
412 case LUA_TNUMBER:
413 case LUA_TBOOLEAN:
414 arg->type = ARGT_SINT;
415 arg->data.sint = lua_tointeger(L, ud);
416 break;
417
418 case LUA_TSTRING:
419 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200420 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200421 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200422 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200423 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100424 break;
425
426 case LUA_TUSERDATA:
427 case LUA_TNIL:
428 case LUA_TTABLE:
429 case LUA_TFUNCTION:
430 case LUA_TTHREAD:
431 case LUA_TLIGHTUSERDATA:
432 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200433 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100434 break;
435 }
436 return 1;
437}
438
439/* the following functions are used to convert a struct sample
440 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500441 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100442 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100443static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100444{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200445 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200448 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 break;
450
451 case SMP_T_BIN:
452 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200453 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 break;
455
456 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200457 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
459 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
460 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
461 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
462 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
463 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
464 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
465 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
466 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100468 break;
469 default:
470 lua_pushnil(L);
471 break;
472 }
473 break;
474
475 case SMP_T_IPV4:
476 case SMP_T_IPV6:
477 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200478 if (sample_casts[smp->data.type][SMP_T_STR] &&
479 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100481 else
482 lua_pushnil(L);
483 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100484 default:
485 lua_pushnil(L);
486 break;
487 }
488 return 1;
489}
490
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100491/* the following functions are used to convert a struct sample
492 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500493 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100494 */
495static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
496{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200497 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100498
499 case SMP_T_BIN:
500 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100502 break;
503
504 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200505 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100506 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
507 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
508 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
509 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
510 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
511 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
512 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
513 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
514 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100516 break;
517 default:
518 lua_pushstring(L, "");
519 break;
520 }
521 break;
522
523 case SMP_T_SINT:
524 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100525 case SMP_T_IPV4:
526 case SMP_T_IPV6:
527 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200528 if (sample_casts[smp->data.type][SMP_T_STR] &&
529 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100531 else
532 lua_pushstring(L, "");
533 break;
534 default:
535 lua_pushstring(L, "");
536 break;
537 }
538 return 1;
539}
540
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100541/* the following functions are used to convert an Lua type in a
542 * struct sample. This is useful to provide data from a converter
543 * to the LUA code.
544 */
545static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
546{
547 switch (lua_type(L, ud)) {
548
549 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200551 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100552 break;
553
554
555 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200556 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100558 break;
559
560 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200561 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100562 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200563 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200564 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200565 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200566 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 break;
568
569 case LUA_TUSERDATA:
570 case LUA_TNIL:
571 case LUA_TTABLE:
572 case LUA_TFUNCTION:
573 case LUA_TTHREAD:
574 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200575 case LUA_TNONE:
576 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200577 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200578 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100579 break;
580 }
581 return 1;
582}
583
Ilya Shipitsind4259502020-04-08 01:07:56 +0500584/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800585 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100586 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100587 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500588 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100589 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100591__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100592 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593{
594 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200595 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100596 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200597 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200598 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200599 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200600 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100601
602 idx = 0;
603 min_arg = ARGM(mask);
604 mask >>= ARGM_BITS;
605
606 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200607 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100608
609 /* Check oversize. */
610 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200611 msg = "Malformed argument mask";
612 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100613 }
614
615 /* Check for mandatory arguments. */
616 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100617 if (idx < min_arg) {
618
619 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200620 if (idx > 0) {
621 msg = "Mandatory argument expected";
622 goto error;
623 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100624
625 /* If first argument have a certain type, some default values
626 * may be used. See the function smp_resolve_args().
627 */
628 switch (mask & ARGT_MASK) {
629
630 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200631 if (!(p->cap & PR_CAP_FE)) {
632 msg = "Mandatory argument expected";
633 goto error;
634 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100635 argp[idx].data.prx = p;
636 argp[idx].type = ARGT_FE;
637 argp[idx+1].type = ARGT_STOP;
638 break;
639
640 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200641 if (!(p->cap & PR_CAP_BE)) {
642 msg = "Mandatory argument expected";
643 goto error;
644 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100645 argp[idx].data.prx = p;
646 argp[idx].type = ARGT_BE;
647 argp[idx+1].type = ARGT_STOP;
648 break;
649
650 case ARGT_TAB:
651 argp[idx].data.prx = p;
652 argp[idx].type = ARGT_TAB;
653 argp[idx+1].type = ARGT_STOP;
654 break;
655
656 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200657 msg = "Mandatory argument expected";
658 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100659 break;
660 }
661 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200662 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100663 }
664
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500665 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100666 if ((mask & ARGT_MASK) == ARGT_STOP &&
667 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200668 msg = "Last argument expected";
669 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100670 }
671
672 if ((mask & ARGT_MASK) == ARGT_STOP &&
673 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200674 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100675 }
676
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200677 /* Convert some argument types. All string in argp[] are for not
678 * duplicated yet.
679 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100680 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100681 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200682 if (argp[idx].type != ARGT_SINT) {
683 msg = "integer expected";
684 goto error;
685 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100686 argp[idx].type = ARGT_SINT;
687 break;
688
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100689 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200690 if (argp[idx].type != ARGT_SINT) {
691 msg = "integer expected";
692 goto error;
693 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200694 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100695 break;
696
697 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200698 if (argp[idx].type != ARGT_SINT) {
699 msg = "integer expected";
700 goto error;
701 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200702 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100703 break;
704
705 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200706 if (argp[idx].type != ARGT_STR) {
707 msg = "string expected";
708 goto error;
709 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200710 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200711 if (!argp[idx].data.prx) {
712 msg = "frontend doesn't exist";
713 goto error;
714 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 argp[idx].type = ARGT_FE;
716 break;
717
718 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200719 if (argp[idx].type != ARGT_STR) {
720 msg = "string expected";
721 goto error;
722 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200723 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200724 if (!argp[idx].data.prx) {
725 msg = "backend doesn't exist";
726 goto error;
727 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100728 argp[idx].type = ARGT_BE;
729 break;
730
731 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200732 if (argp[idx].type != ARGT_STR) {
733 msg = "string expected";
734 goto error;
735 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200736 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200737 if (!argp[idx].data.t) {
738 msg = "table doesn't exist";
739 goto error;
740 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 argp[idx].type = ARGT_TAB;
742 break;
743
744 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200745 if (argp[idx].type != ARGT_STR) {
746 msg = "string expected";
747 goto error;
748 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200749 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 if (sname) {
751 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200752 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200753 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200754 if (!px) {
755 msg = "backend doesn't exist";
756 goto error;
757 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100758 }
759 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200760 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100762 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200764 if (!argp[idx].data.srv) {
765 msg = "server doesn't exist";
766 goto error;
767 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100768 argp[idx].type = ARGT_SRV;
769 break;
770
771 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200772 if (argp[idx].type != ARGT_STR) {
773 msg = "string expected";
774 goto error;
775 }
776 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
777 msg = "invalid IPv4 address";
778 goto error;
779 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100780 argp[idx].type = ARGT_IPV4;
781 break;
782
783 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200784 if (argp[idx].type == ARGT_SINT)
785 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
786 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200787 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
788 msg = "invalid IPv4 mask";
789 goto error;
790 }
791 }
792 else {
793 msg = "integer or string expected";
794 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200795 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100796 argp[idx].type = ARGT_MSK4;
797 break;
798
799 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200800 if (argp[idx].type != ARGT_STR) {
801 msg = "string expected";
802 goto error;
803 }
804 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
805 msg = "invalid IPv6 address";
806 goto error;
807 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100808 argp[idx].type = ARGT_IPV6;
809 break;
810
811 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200812 if (argp[idx].type == ARGT_SINT)
813 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
814 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200815 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
816 msg = "invalid IPv6 mask";
817 goto error;
818 }
819 }
820 else {
821 msg = "integer or string expected";
822 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200823 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100824 argp[idx].type = ARGT_MSK6;
825 break;
826
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200827 case ARGT_REG:
828 if (argp[idx].type != ARGT_STR) {
829 msg = "string expected";
830 goto error;
831 }
832 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
833 if (!reg) {
834 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
835 argp[idx].data.str.area, err);
836 free(err);
837 goto error;
838 }
839 argp[idx].type = ARGT_REG;
840 argp[idx].data.reg = reg;
841 break;
842
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100843 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200844 if (argp[idx].type != ARGT_STR) {
845 msg = "string expected";
846 goto error;
847 }
848 if (p->uri_auth && p->uri_auth->userlist &&
849 !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area))
850 ul = p->uri_auth->userlist;
851 else
852 ul = auth_find_userlist(argp[idx].data.str.area);
853
854 if (!ul) {
855 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
856 goto error;
857 }
858 argp[idx].type = ARGT_USR;
859 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200860 break;
861
862 case ARGT_STR:
863 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
864 msg = "unable to duplicate string arg";
865 goto error;
866 }
867 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100868 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200869
Christopher Fauletd25d9262020-08-06 11:04:46 +0200870 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200871 msg = "type not yet supported";
872 goto error;
873 break;
874
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100875 }
876
877 /* Check for type of argument. */
878 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200879 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
880 arg_type_names[(mask & ARGT_MASK)],
881 arg_type_names[argp[idx].type & ARGT_MASK]);
882 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100883 }
884
885 /* Next argument. */
886 mask >>= ARGT_BITS;
887 idx++;
888 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200889 return 0;
890
891 error:
892 for (i = 0; i < idx; i++) {
893 if (argp[i].type == ARGT_STR)
894 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200895 else if (argp[i].type == ARGT_REG)
896 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200897 }
898 WILL_LJMP(luaL_argerror(L, first + idx, msg));
899 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100900}
901
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100902/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500903 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100904 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100905 *
906 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 * - hlua_sethlua : create the association between hlua context and lua_state.
908 */
909static inline struct hlua *hlua_gethlua(lua_State *L)
910{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100911 struct hlua **hlua = lua_getextraspace(L);
912 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100913}
914static inline void hlua_sethlua(struct hlua *hlua)
915{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100916 struct hlua **hlua_store = lua_getextraspace(hlua->T);
917 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100918}
919
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100920/* This function is used to send logs. It try to send on screen (stderr)
921 * and on the default syslog server.
922 */
923static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
924{
925 struct tm tm;
926 char *p;
927
928 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200929 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100930 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200931 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200932 /* Break the message if exceed the buffer size. */
933 *(p-4) = ' ';
934 *(p-3) = '.';
935 *(p-2) = '.';
936 *(p-1) = '.';
937 break;
938 }
Willy Tarreau90807112020-02-25 08:16:33 +0100939 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100940 *p = *msg;
941 else
942 *p = '.';
943 }
944 *p = '\0';
945
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200946 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100947 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +0200948 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
949 return;
950
Willy Tarreaua678b432015-08-28 10:14:59 +0200951 get_localtime(date.tv_sec, &tm);
952 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100953 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200954 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100955 fflush(stderr);
956 }
957}
958
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100959/* This function just ensure that the yield will be always
960 * returned with a timeout and permit to set some flags
961 */
962__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100963 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100964{
965 struct hlua *hlua = hlua_gethlua(L);
966
967 /* Set the wake timeout. If timeout is required, we set
968 * the expiration time.
969 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200970 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100971
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100972 hlua->flags |= flags;
973
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100974 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200975 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100976}
977
Willy Tarreau87b09662015-04-03 00:22:06 +0200978/* This function initialises the Lua environment stored in the stream.
979 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100980 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200981 *
982 * This function is particular. it initialises a new Lua thread. If the
983 * initialisation fails (example: out of memory error), the lua function
984 * throws an error (longjmp).
985 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100986 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500987 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100988 * threads appear, the safe environment set a lock to ensure only one
989 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500990 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100991 *
992 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500993 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100994 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800995 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200996 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800997 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500998 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100999 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001000int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001001{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001002 if (!already_safe) {
1003 if (!SET_SAFE_LJMP(gL.T)) {
1004 lua->Tref = LUA_REFNIL;
1005 return 0;
1006 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001007 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001008 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001009 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001010 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001011 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001012 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001013 lua->T = lua_newthread(gL.T);
1014 if (!lua->T) {
1015 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001016 if (!already_safe)
1017 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001018 return 0;
1019 }
1020 hlua_sethlua(lua);
1021 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1022 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001023 if (!already_safe)
1024 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001025 return 1;
1026}
1027
Willy Tarreau87b09662015-04-03 00:22:06 +02001028/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001029 * is destroyed. The destroy also the memory context. The struct "lua"
1030 * is not freed.
1031 */
1032void hlua_ctx_destroy(struct hlua *lua)
1033{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001034 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001035 return;
1036
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001037 if (!lua->T)
1038 goto end;
1039
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001040 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001041 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001042
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001043 if (!SET_SAFE_LJMP(lua->T))
1044 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001045 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001046 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001047
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001048 if (!SET_SAFE_LJMP(gL.T))
1049 return;
1050 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1051 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001052 /* Forces a garbage collecting process. If the Lua program is finished
1053 * without error, we run the GC on the thread pointer. Its freed all
1054 * the unused memory.
1055 * If the thread is finnish with an error or is currently yielded,
1056 * it seems that the GC applied on the thread doesn't clean anything,
1057 * so e run the GC on the main thread.
1058 * NOTE: maybe this action locks all the Lua threads untiml the en of
1059 * the garbage collection.
1060 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001061 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001062 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001063 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001064 lua_gc(gL.T, LUA_GCCOLLECT, 0);
1065 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001066 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001067
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001068 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001069
1070end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001071 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001072}
1073
1074/* This function is used to restore the Lua context when a coroutine
1075 * fails. This function copy the common memory between old coroutine
1076 * and the new coroutine. The old coroutine is destroyed, and its
1077 * replaced by the new coroutine.
1078 * If the flag "keep_msg" is set, the last entry of the old is assumed
1079 * as string error message and it is copied in the new stack.
1080 */
1081static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1082{
1083 lua_State *T;
1084 int new_ref;
1085
1086 /* Renew the main LUA stack doesn't have sense. */
1087 if (lua == &gL)
1088 return 0;
1089
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 /* New Lua coroutine. */
1091 T = lua_newthread(gL.T);
1092 if (!T)
1093 return 0;
1094
1095 /* Copy last error message. */
1096 if (keep_msg)
1097 lua_xmove(lua->T, T, 1);
1098
1099 /* Copy data between the coroutines. */
1100 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1101 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001102 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001103
1104 /* Destroy old data. */
1105 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1106
1107 /* The thread is garbage collected by Lua. */
1108 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1109
1110 /* Fill the struct with the new coroutine values. */
1111 lua->Mref = new_ref;
1112 lua->T = T;
1113 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1114
1115 /* Set context. */
1116 hlua_sethlua(lua);
1117
1118 return 1;
1119}
1120
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001121void hlua_hook(lua_State *L, lua_Debug *ar)
1122{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001123 struct hlua *hlua = hlua_gethlua(L);
1124
1125 /* Lua cannot yield when its returning from a function,
1126 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001127 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001128 */
1129 if (lua_gethookmask(L) & LUA_MASKRET) {
1130 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1131 return;
1132 }
1133
1134 /* restore the interrupt condition. */
1135 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1136
1137 /* If we interrupt the Lua processing in yieldable state, we yield.
1138 * If the state is not yieldable, trying yield causes an error.
1139 */
1140 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001141 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001142
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001143 /* If we cannot yield, update the clock and check the timeout. */
1144 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001145 hlua->run_time += now_ms - hlua->start_time;
1146 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001147 lua_pushfstring(L, "execution timeout");
1148 WILL_LJMP(lua_error(L));
1149 }
1150
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001151 /* Update the start time. */
1152 hlua->start_time = now_ms;
1153
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001154 /* Try to interrupt the process at the end of the current
1155 * unyieldable function.
1156 */
1157 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001158}
1159
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160/* This function start or resumes the Lua stack execution. If the flag
1161 * "yield_allowed" if no set and the LUA stack execution returns a yield
1162 * The function return an error.
1163 *
1164 * The function can returns 4 values:
1165 * - HLUA_E_OK : The execution is terminated without any errors.
1166 * - HLUA_E_AGAIN : The execution must continue at the next associated
1167 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001168 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001169 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001170 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001172 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 * LUA code.
1174 */
1175static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1176{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001177#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1178 int nres;
1179#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001180 int ret;
1181 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001182 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001183
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001184 /* Initialise run time counter. */
1185 if (!HLUA_IS_RUNNING(lua))
1186 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001187
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001188 /* Lock the whole Lua execution. This lock must be before the
1189 * label "resume_execution".
1190 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001191 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001192
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001193resume_execution:
1194
1195 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1196 * instructions. it is used for preventing infinite loops.
1197 */
1198 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1199
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001200 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001201 HLUA_SET_RUN(lua);
1202 HLUA_CLR_CTRLYIELD(lua);
1203 HLUA_CLR_WAKERESWR(lua);
1204 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001205
Christopher Fauletbc275a92020-02-26 14:55:16 +01001206 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001207 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001208 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001209
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001210 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001211#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1212 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1213#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001214 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001215#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001216 switch (ret) {
1217
1218 case LUA_OK:
1219 ret = HLUA_E_OK;
1220 break;
1221
1222 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001223 /* Check if the execution timeout is expired. It it is the case, we
1224 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001225 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001226 tv_update_date(0, 1);
1227 lua->run_time += now_ms - lua->start_time;
1228 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001229 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001230 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001231 break;
1232 }
1233 /* Process the forced yield. if the general yield is not allowed or
1234 * if no task were associated this the current Lua execution
1235 * coroutine, we resume the execution. Else we want to return in the
1236 * scheduler and we want to be waked up again, to continue the
1237 * current Lua execution. So we schedule our own task.
1238 */
1239 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001240 if (!yield_allowed || !lua->task)
1241 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001242 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001243 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001244 if (!yield_allowed) {
1245 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001246 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001247 break;
1248 }
1249 ret = HLUA_E_AGAIN;
1250 break;
1251
1252 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001253
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001254 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001255 * because the errors ares the only one mean to return immediately
1256 * from and lua execution.
1257 */
1258 if (lua->flags & HLUA_EXIT) {
1259 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001260 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001261 break;
1262 }
1263
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001264 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001265 if (!lua_checkstack(lua->T, 1)) {
1266 ret = HLUA_E_ERR;
1267 break;
1268 }
1269 msg = lua_tostring(lua->T, -1);
1270 lua_settop(lua->T, 0); /* Empty the stack. */
1271 lua_pop(lua->T, 1);
Christopher Faulet5326d6a2021-03-24 14:48:45 +01001272 trace = hlua_traceback(lua->T, ", ");
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001273 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001274 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001275 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001276 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001277 ret = HLUA_E_ERRMSG;
1278 break;
1279
1280 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001281 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001282 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001283 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001284 break;
1285
1286 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001287 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001288 if (!lua_checkstack(lua->T, 1)) {
1289 ret = HLUA_E_ERR;
1290 break;
1291 }
1292 msg = lua_tostring(lua->T, -1);
1293 lua_settop(lua->T, 0); /* Empty the stack. */
1294 lua_pop(lua->T, 1);
1295 if (msg)
1296 lua_pushfstring(lua->T, "message handler error: %s", msg);
1297 else
1298 lua_pushfstring(lua->T, "message handler error");
1299 ret = HLUA_E_ERRMSG;
1300 break;
1301
1302 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001303 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001304 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001305 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001306 break;
1307 }
1308
1309 switch (ret) {
1310 case HLUA_E_AGAIN:
1311 break;
1312
1313 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001314 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001315 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001316 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001317 break;
1318
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001319 case HLUA_E_ETMOUT:
1320 case HLUA_E_NOMEM:
1321 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001322 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001323 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001324 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001325 hlua_ctx_renew(lua, 0);
1326 break;
1327
1328 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001329 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001330 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001331 break;
1332 }
1333
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001334 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001335 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001336
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001337 return ret;
1338}
1339
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001340/* This function exit the current code. */
1341__LJMP static int hlua_done(lua_State *L)
1342{
1343 struct hlua *hlua = hlua_gethlua(L);
1344
1345 hlua->flags |= HLUA_EXIT;
1346 WILL_LJMP(lua_error(L));
1347
1348 return 0;
1349}
1350
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001351/* This function is an LUA binding. It provides a function
1352 * for deleting ACL from a referenced ACL file.
1353 */
1354__LJMP static int hlua_del_acl(lua_State *L)
1355{
1356 const char *name;
1357 const char *key;
1358 struct pat_ref *ref;
1359
1360 MAY_LJMP(check_args(L, 2, "del_acl"));
1361
1362 name = MAY_LJMP(luaL_checkstring(L, 1));
1363 key = MAY_LJMP(luaL_checkstring(L, 2));
1364
1365 ref = pat_ref_lookup(name);
1366 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001367 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001368
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001369 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001370 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001371 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001372 return 0;
1373}
1374
1375/* This function is an LUA binding. It provides a function
1376 * for deleting map entry from a referenced map file.
1377 */
1378static int hlua_del_map(lua_State *L)
1379{
1380 const char *name;
1381 const char *key;
1382 struct pat_ref *ref;
1383
1384 MAY_LJMP(check_args(L, 2, "del_map"));
1385
1386 name = MAY_LJMP(luaL_checkstring(L, 1));
1387 key = MAY_LJMP(luaL_checkstring(L, 2));
1388
1389 ref = pat_ref_lookup(name);
1390 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001391 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001392
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001393 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001394 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001395 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001396 return 0;
1397}
1398
1399/* This function is an LUA binding. It provides a function
1400 * for adding ACL pattern from a referenced ACL file.
1401 */
1402static int hlua_add_acl(lua_State *L)
1403{
1404 const char *name;
1405 const char *key;
1406 struct pat_ref *ref;
1407
1408 MAY_LJMP(check_args(L, 2, "add_acl"));
1409
1410 name = MAY_LJMP(luaL_checkstring(L, 1));
1411 key = MAY_LJMP(luaL_checkstring(L, 2));
1412
1413 ref = pat_ref_lookup(name);
1414 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001415 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001416
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001417 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001418 if (pat_ref_find_elt(ref, key) == NULL)
1419 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001420 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001421 return 0;
1422}
1423
1424/* This function is an LUA binding. It provides a function
1425 * for setting map pattern and sample from a referenced map
1426 * file.
1427 */
1428static int hlua_set_map(lua_State *L)
1429{
1430 const char *name;
1431 const char *key;
1432 const char *value;
1433 struct pat_ref *ref;
1434
1435 MAY_LJMP(check_args(L, 3, "set_map"));
1436
1437 name = MAY_LJMP(luaL_checkstring(L, 1));
1438 key = MAY_LJMP(luaL_checkstring(L, 2));
1439 value = MAY_LJMP(luaL_checkstring(L, 3));
1440
1441 ref = pat_ref_lookup(name);
1442 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001443 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001444
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001445 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001446 if (pat_ref_find_elt(ref, key) != NULL)
1447 pat_ref_set(ref, key, value, NULL);
1448 else
1449 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001450 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001451 return 0;
1452}
1453
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001454/* A class is a lot of memory that contain data. This data can be a table,
1455 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001456 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001457 * the name of the object (_G[<name>] = <metable> ).
1458 *
1459 * A metable is a table that modify the standard behavior of a standard
1460 * access to the associated data. The entries of this new metatable are
1461 * defined as is:
1462 *
1463 * http://lua-users.org/wiki/MetatableEvents
1464 *
1465 * __index
1466 *
1467 * we access an absent field in a table, the result is nil. This is
1468 * true, but it is not the whole truth. Actually, such access triggers
1469 * the interpreter to look for an __index metamethod: If there is no
1470 * such method, as usually happens, then the access results in nil;
1471 * otherwise, the metamethod will provide the result.
1472 *
1473 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1474 * the key does not appear in the table, but the metatable has an __index
1475 * property:
1476 *
1477 * - if the value is a function, the function is called, passing in the
1478 * table and the key; the return value of that function is returned as
1479 * the result.
1480 *
1481 * - if the value is another table, the value of the key in that table is
1482 * asked for and returned (and if it doesn't exist in that table, but that
1483 * table's metatable has an __index property, then it continues on up)
1484 *
1485 * - Use "rawget(myTable,key)" to skip this metamethod.
1486 *
1487 * http://www.lua.org/pil/13.4.1.html
1488 *
1489 * __newindex
1490 *
1491 * Like __index, but control property assignment.
1492 *
1493 * __mode - Control weak references. A string value with one or both
1494 * of the characters 'k' and 'v' which specifies that the the
1495 * keys and/or values in the table are weak references.
1496 *
1497 * __call - Treat a table like a function. When a table is followed by
1498 * parenthesis such as "myTable( 'foo' )" and the metatable has
1499 * a __call key pointing to a function, that function is invoked
1500 * (passing any specified arguments) and the return value is
1501 * returned.
1502 *
1503 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1504 * called, if the metatable for myTable has a __metatable
1505 * key, the value of that key is returned instead of the
1506 * actual metatable.
1507 *
1508 * __tostring - Control string representation. When the builtin
1509 * "tostring( myTable )" function is called, if the metatable
1510 * for myTable has a __tostring property set to a function,
1511 * that function is invoked (passing myTable to it) and the
1512 * return value is used as the string representation.
1513 *
1514 * __len - Control table length. When the table length is requested using
1515 * the length operator ( '#' ), if the metatable for myTable has
1516 * a __len key pointing to a function, that function is invoked
1517 * (passing myTable to it) and the return value used as the value
1518 * of "#myTable".
1519 *
1520 * __gc - Userdata finalizer code. When userdata is set to be garbage
1521 * collected, if the metatable has a __gc field pointing to a
1522 * function, that function is first invoked, passing the userdata
1523 * to it. The __gc metamethod is not called for tables.
1524 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1525 *
1526 * Special metamethods for redefining standard operators:
1527 * http://www.lua.org/pil/13.1.html
1528 *
1529 * __add "+"
1530 * __sub "-"
1531 * __mul "*"
1532 * __div "/"
1533 * __unm "!"
1534 * __pow "^"
1535 * __concat ".."
1536 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001537 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001538 * http://www.lua.org/pil/13.2.html
1539 *
1540 * __eq "=="
1541 * __lt "<"
1542 * __le "<="
1543 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001544
1545/*
1546 *
1547 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001548 * Class Map
1549 *
1550 *
1551 */
1552
1553/* Returns a struct hlua_map if the stack entry "ud" is
1554 * a class session, otherwise it throws an error.
1555 */
1556__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1557{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001558 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001559}
1560
1561/* This function is the map constructor. It don't need
1562 * the class Map object. It creates and return a new Map
1563 * object. It must be called only during "body" or "init"
1564 * context because it process some filesystem accesses.
1565 */
1566__LJMP static int hlua_map_new(struct lua_State *L)
1567{
1568 const char *fn;
1569 int match = PAT_MATCH_STR;
1570 struct sample_conv conv;
1571 const char *file = "";
1572 int line = 0;
1573 lua_Debug ar;
1574 char *err = NULL;
1575 struct arg args[2];
1576
1577 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1578 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1579
1580 fn = MAY_LJMP(luaL_checkstring(L, 1));
1581
1582 if (lua_gettop(L) >= 2) {
1583 match = MAY_LJMP(luaL_checkinteger(L, 2));
1584 if (match < 0 || match >= PAT_MATCH_NUM)
1585 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1586 }
1587
1588 /* Get Lua filename and line number. */
1589 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1590 lua_getinfo(L, "Sl", &ar); /* get info about it */
1591 if (ar.currentline > 0) { /* is there info? */
1592 file = ar.short_src;
1593 line = ar.currentline;
1594 }
1595 }
1596
1597 /* fill fake sample_conv struct. */
1598 conv.kw = ""; /* unused. */
1599 conv.process = NULL; /* unused. */
1600 conv.arg_mask = 0; /* unused. */
1601 conv.val_args = NULL; /* unused. */
1602 conv.out_type = SMP_T_STR;
1603 conv.private = (void *)(long)match;
1604 switch (match) {
1605 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1606 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1607 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1608 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1609 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1610 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1611 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001612 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001613 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1614 default:
1615 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1616 }
1617
1618 /* fill fake args. */
1619 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001620 args[0].data.str.area = strdup(fn);
1621 args[0].data.str.data = strlen(fn);
1622 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001623 args[1].type = ARGT_STOP;
1624
1625 /* load the map. */
1626 if (!sample_load_map(args, &conv, file, line, &err)) {
1627 /* error case: we cant use luaL_error because we must
1628 * free the err variable.
1629 */
1630 luaL_where(L, 1);
1631 lua_pushfstring(L, "'new': %s.", err);
1632 lua_concat(L, 2);
1633 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001634 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001635 WILL_LJMP(lua_error(L));
1636 }
1637
1638 /* create the lua object. */
1639 lua_newtable(L);
1640 lua_pushlightuserdata(L, args[0].data.map);
1641 lua_rawseti(L, -2, 0);
1642
1643 /* Pop a class Map metatable and affect it to the userdata. */
1644 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1645 lua_setmetatable(L, -2);
1646
1647
1648 return 1;
1649}
1650
1651__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1652{
1653 struct map_descriptor *desc;
1654 struct pattern *pat;
1655 struct sample smp;
1656
1657 MAY_LJMP(check_args(L, 2, "lookup"));
1658 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001659 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001660 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001661 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001662 }
1663 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001664 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001665 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001666 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry Fournier66e9ff52020-11-10 20:38:20 +01001667 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001668 }
1669
1670 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001671 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001672 if (str)
1673 lua_pushstring(L, "");
1674 else
1675 lua_pushnil(L);
1676 return 1;
1677 }
1678
1679 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001680 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001681 return 1;
1682}
1683
1684__LJMP static int hlua_map_lookup(struct lua_State *L)
1685{
1686 return _hlua_map_lookup(L, 0);
1687}
1688
1689__LJMP static int hlua_map_slookup(struct lua_State *L)
1690{
1691 return _hlua_map_lookup(L, 1);
1692}
1693
1694/*
1695 *
1696 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 * Class Socket
1698 *
1699 *
1700 */
1701
1702__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1703{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001704 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705}
1706
1707/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001708 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709 * received.
1710 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001711static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001712{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001713 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001714
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001715 if (appctx->ctx.hlua_cosocket.die) {
1716 si_shutw(si);
1717 si_shutr(si);
1718 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001719 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1720 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001721 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1722 }
1723
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001724 /* If we cant write, wakeup the pending write signals. */
1725 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001726 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001727
1728 /* If we cant read, wakeup the pending read signals. */
1729 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001730 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001731
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001732 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001733 * to be notified whenever the connection completes.
1734 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001735 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001736 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001737 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001738 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001739 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001740 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001741
1742 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001743 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001744
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001745 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001746 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001747 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001748
1749 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001750 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001751 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001752
1753 /* Some data were injected in the buffer, notify the stream
1754 * interface.
1755 */
1756 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001757 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001758
1759 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001760 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001761 */
1762 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001763 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001764}
1765
Willy Tarreau87b09662015-04-03 00:22:06 +02001766/* This function is called when the "struct stream" is destroyed.
1767 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001768 * Wake all the pending signals.
1769 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001770static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001771{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001772 struct xref *peer;
1773
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001774 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001775 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1776 if (peer)
1777 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001778
1779 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001780 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1781 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782}
1783
1784/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001785 * uses this object. If the stream does not exists, just quit.
1786 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787 * pending signal can rest in the read and write lists. destroy
1788 * it.
1789 */
1790__LJMP static int hlua_socket_gc(lua_State *L)
1791{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001792 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001794 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001796 MAY_LJMP(check_args(L, 1, "__gc"));
1797
1798 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001799 peer = xref_get_peer_and_lock(&socket->xref);
1800 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001801 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001802 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001803
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001804 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001805 appctx->ctx.hlua_cosocket.die = 1;
1806 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001807
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001808 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001809 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001810 return 0;
1811}
1812
1813/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001814 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 */
sada05ed3302018-05-11 11:48:18 -07001816__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001817{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001818 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001820 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001821 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001823 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001824
1825 /* Check if we run on the same thread than the xreator thread.
1826 * We cannot access to the socket if the thread is different.
1827 */
1828 if (socket->tid != tid)
1829 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1830
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001831 peer = xref_get_peer_and_lock(&socket->xref);
1832 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001834
1835 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001836 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001838 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001839 appctx->ctx.hlua_cosocket.die = 1;
1840 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001842 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001843 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001844 return 0;
1845}
1846
sada05ed3302018-05-11 11:48:18 -07001847/* The close function calls close_helper.
1848 */
1849__LJMP static int hlua_socket_close(lua_State *L)
1850{
1851 MAY_LJMP(check_args(L, 1, "close"));
1852 return hlua_socket_close_helper(L);
1853}
1854
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855/* This Lua function assumes that the stack contain three parameters.
1856 * 1 - USERDATA containing a struct socket
1857 * 2 - INTEGER with values of the macro defined below
1858 * If the integer is -1, we must read at most one line.
1859 * If the integer is -2, we ust read all the data until the
1860 * end of the stream.
1861 * If the integer is positive value, we must read a number of
1862 * bytes corresponding to this value.
1863 */
1864#define HLSR_READ_LINE (-1)
1865#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001866__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001867{
1868 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1869 int wanted = lua_tointeger(L, 2);
1870 struct hlua *hlua = hlua_gethlua(L);
1871 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001872 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001873 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001874 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001875 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001876 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001877 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001878 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001879 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001880 struct stream_interface *si;
1881 struct stream *s;
1882 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001883 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884
1885 /* Check if this lua stack is schedulable. */
1886 if (!hlua || !hlua->task)
1887 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1888 "'frontend', 'backend' or 'task'"));
1889
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001890 /* Check if we run on the same thread than the xreator thread.
1891 * We cannot access to the socket if the thread is different.
1892 */
1893 if (socket->tid != tid)
1894 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1895
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001896 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001897 peer = xref_get_peer_and_lock(&socket->xref);
1898 if (!peer)
1899 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001900 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1901 si = appctx->owner;
1902 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001904 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001907 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 if (nblk < 0) /* Connection close. */
1909 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001910 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001912
1913 /* remove final \r\n. */
1914 if (nblk == 1) {
1915 if (blk1[len1-1] == '\n') {
1916 len1--;
1917 skip_at_end++;
1918 if (blk1[len1-1] == '\r') {
1919 len1--;
1920 skip_at_end++;
1921 }
1922 }
1923 }
1924 else {
1925 if (blk2[len2-1] == '\n') {
1926 len2--;
1927 skip_at_end++;
1928 if (blk2[len2-1] == '\r') {
1929 len2--;
1930 skip_at_end++;
1931 }
1932 }
1933 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934 }
1935
1936 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001937 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001938 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001939 if (nblk < 0) /* Connection close. */
1940 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001941 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001942 goto connection_empty;
1943 }
1944
1945 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001947 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001948 if (nblk < 0) /* Connection close. */
1949 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001950 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001951 goto connection_empty;
1952
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001953 missing_bytes = wanted - socket->b.n;
1954 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001955 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001956 len1 = missing_bytes;
1957 } if (nblk == 2 && len1 + len2 > missing_bytes)
1958 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959 }
1960
1961 len = len1;
1962
1963 luaL_addlstring(&socket->b, blk1, len1);
1964 if (nblk == 2) {
1965 len += len2;
1966 luaL_addlstring(&socket->b, blk2, len2);
1967 }
1968
1969 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001970 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971
1972 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001973 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974
1975 /* If the pattern reclaim to read all the data
1976 * in the connection, got out.
1977 */
1978 if (wanted == HLSR_READ_ALL)
1979 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001980 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001981 goto connection_empty;
1982
1983 /* Return result. */
1984 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001985 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986 return 1;
1987
1988connection_closed:
1989
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001990 xref_unlock(&socket->xref, peer);
1991
1992no_peer:
1993
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001994 /* If the buffer containds data. */
1995 if (socket->b.n > 0) {
1996 luaL_pushresult(&socket->b);
1997 return 1;
1998 }
1999 lua_pushnil(L);
2000 lua_pushstring(L, "connection closed.");
2001 return 2;
2002
2003connection_empty:
2004
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002005 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2006 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002007 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002008 }
2009 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002010 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011 return 0;
2012}
2013
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002014/* This Lua function gets two parameters. The first one can be string
2015 * or a number. If the string is "*l", the user requires one line. If
2016 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 * If the value is a number, the user require a number of bytes equal
2018 * to the value. The default value is "*l" (a line).
2019 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002020 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002021 * integer takes this values:
2022 * -1 : read a line
2023 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002024 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002025 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002026 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027 * concatenated with the read data.
2028 */
2029__LJMP static int hlua_socket_receive(struct lua_State *L)
2030{
2031 int wanted = HLSR_READ_LINE;
2032 const char *pattern;
Christopher Fauletaf3d8c12021-05-03 10:11:13 +02002033 int lastarg, type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034 char *error;
2035 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002036 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037
2038 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2039 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2040
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002041 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002043 /* Check if we run on the same thread than the xreator thread.
2044 * We cannot access to the socket if the thread is different.
2045 */
2046 if (socket->tid != tid)
2047 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2048
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049 /* check for pattern. */
2050 if (lua_gettop(L) >= 2) {
2051 type = lua_type(L, 2);
2052 if (type == LUA_TSTRING) {
2053 pattern = lua_tostring(L, 2);
2054 if (strcmp(pattern, "*a") == 0)
2055 wanted = HLSR_READ_ALL;
2056 else if (strcmp(pattern, "*l") == 0)
2057 wanted = HLSR_READ_LINE;
2058 else {
2059 wanted = strtoll(pattern, &error, 10);
2060 if (*error != '\0')
2061 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2062 }
2063 }
2064 else if (type == LUA_TNUMBER) {
2065 wanted = lua_tointeger(L, 2);
2066 if (wanted < 0)
2067 WILL_LJMP(luaL_error(L, "Unsupported size."));
2068 }
2069 }
2070
2071 /* Set pattern. */
2072 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002073
2074 /* Check if we would replace the top by itself. */
2075 if (lua_gettop(L) != 2)
2076 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077
Christopher Fauletaf3d8c12021-05-03 10:11:13 +02002078 /* Save index of the top of the stack because since buffers are used, it
2079 * may change
2080 */
2081 lastarg = lua_gettop(L);
2082
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002083 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084 luaL_buffinit(L, &socket->b);
2085
2086 /* Check prefix. */
Christopher Fauletaf3d8c12021-05-03 10:11:13 +02002087 if (lastarg >= 3) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088 if (lua_type(L, 3) != LUA_TSTRING)
2089 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2090 pattern = lua_tolstring(L, 3, &len);
2091 luaL_addlstring(&socket->b, pattern, len);
2092 }
2093
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002094 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095}
2096
2097/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002098 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002099 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002100static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101{
2102 struct hlua_socket *socket;
2103 struct hlua *hlua = hlua_gethlua(L);
2104 struct appctx *appctx;
2105 size_t buf_len;
2106 const char *buf;
2107 int len;
2108 int send_len;
2109 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002110 struct xref *peer;
2111 struct stream_interface *si;
2112 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002113
2114 /* Check if this lua stack is schedulable. */
2115 if (!hlua || !hlua->task)
2116 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2117 "'frontend', 'backend' or 'task'"));
2118
2119 /* Get object */
2120 socket = MAY_LJMP(hlua_checksocket(L, 1));
2121 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002122 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002123
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002124 /* Check if we run on the same thread than the xreator thread.
2125 * We cannot access to the socket if the thread is different.
2126 */
2127 if (socket->tid != tid)
2128 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2129
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002130 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002131 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002132 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002133 lua_pushinteger(L, -1);
2134 return 1;
2135 }
2136 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2137 si = appctx->owner;
2138 s = si_strm(si);
2139
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002140 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002141 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002142 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002143 lua_pushinteger(L, -1);
2144 return 1;
2145 }
2146
2147 /* Update the input buffer data. */
2148 buf += sent;
2149 send_len = buf_len - sent;
2150
2151 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002152 if (sent >= buf_len) {
2153 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002155 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002156
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002157 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002158 * the request buffer if its not required.
2159 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002160 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002161 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002162 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002163 }
2164
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002165 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002166 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002167 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002169 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002170
2171 /* send data */
2172 if (len < send_len)
2173 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002174 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175
2176 /* "Not enough space" (-1), "Buffer too little to contain
2177 * the data" (-2) are not expected because the available length
2178 * is tested.
2179 * Other unknown error are also not expected.
2180 */
2181 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002182 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002183 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002184
sada05ed3302018-05-11 11:48:18 -07002185 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002186 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002187 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002188 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 return 1;
2190 }
2191
2192 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002193 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002194
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002195 s->req.rex = TICK_ETERNITY;
2196 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002197
2198 /* Update length sent. */
2199 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002200 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201
2202 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002203 if (sent + len >= buf_len) {
2204 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002205 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002206 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002207
2208hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002209 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2210 xref_unlock(&socket->xref, peer);
2211 WILL_LJMP(luaL_error(L, "out of memory"));
2212 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002213 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002214 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002215 return 0;
2216}
2217
2218/* This function initiate the send of data. It just check the input
2219 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002220 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002221 * "hlua_socket_write_yield" that can yield.
2222 *
2223 * The Lua function gets between 3 and 4 parameters. The first one is
2224 * the associated object. The second is a string buffer. The third is
2225 * a facultative integer that represents where is the buffer position
2226 * of the start of the data that can send. The first byte is the
2227 * position "1". The default value is "1". The fourth argument is a
2228 * facultative integer that represents where is the buffer position
2229 * of the end of the data that can send. The default is the last byte.
2230 */
2231static int hlua_socket_send(struct lua_State *L)
2232{
2233 int i;
2234 int j;
2235 const char *buf;
2236 size_t buf_len;
2237
2238 /* Check number of arguments. */
2239 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2240 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2241
2242 /* Get the string. */
2243 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2244
2245 /* Get and check j. */
2246 if (lua_gettop(L) == 4) {
2247 j = MAY_LJMP(luaL_checkinteger(L, 4));
2248 if (j < 0)
2249 j = buf_len + j + 1;
2250 if (j > buf_len)
2251 j = buf_len + 1;
2252 lua_pop(L, 1);
2253 }
2254 else
2255 j = buf_len;
2256
2257 /* Get and check i. */
2258 if (lua_gettop(L) == 3) {
2259 i = MAY_LJMP(luaL_checkinteger(L, 3));
2260 if (i < 0)
2261 i = buf_len + i + 1;
2262 if (i > buf_len)
2263 i = buf_len + 1;
2264 lua_pop(L, 1);
2265 } else
2266 i = 1;
2267
2268 /* Check bth i and j. */
2269 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002270 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271 return 1;
2272 }
2273 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002274 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002275 return 1;
2276 }
2277 if (i == 0)
2278 i = 1;
2279 if (j == 0)
2280 j = 1;
2281
2282 /* Pop the string. */
2283 lua_pop(L, 1);
2284
2285 /* Update the buffer length. */
2286 buf += i - 1;
2287 buf_len = j - i + 1;
2288 lua_pushlstring(L, buf, buf_len);
2289
2290 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002291 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002293 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294}
2295
Willy Tarreau22b0a682015-06-17 19:43:49 +02002296#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2298{
2299 static char buffer[SOCKET_INFO_MAX_LEN];
2300 int ret;
2301 int len;
2302 char *p;
2303
2304 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2305 if (ret <= 0) {
2306 lua_pushnil(L);
2307 return 1;
2308 }
2309
2310 if (ret == AF_UNIX) {
2311 lua_pushstring(L, buffer+1);
2312 return 1;
2313 }
2314 else if (ret == AF_INET6) {
2315 buffer[0] = '[';
2316 len = strlen(buffer);
2317 buffer[len] = ']';
2318 len++;
2319 buffer[len] = ':';
2320 len++;
2321 p = buffer;
2322 }
2323 else if (ret == AF_INET) {
2324 p = buffer + 1;
2325 len = strlen(p);
2326 p[len] = ':';
2327 len++;
2328 }
2329 else {
2330 lua_pushnil(L);
2331 return 1;
2332 }
2333
2334 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2335 lua_pushnil(L);
2336 return 1;
2337 }
2338
2339 lua_pushstring(L, p);
2340 return 1;
2341}
2342
2343/* Returns information about the peer of the connection. */
2344__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2345{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002346 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002347 struct xref *peer;
2348 struct appctx *appctx;
2349 struct stream_interface *si;
2350 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002351 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002352
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002353 MAY_LJMP(check_args(L, 1, "getpeername"));
2354
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002355 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002357 /* Check if we run on the same thread than the xreator thread.
2358 * We cannot access to the socket if the thread is different.
2359 */
2360 if (socket->tid != tid)
2361 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2362
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002363 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002364 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002365 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 lua_pushnil(L);
2367 return 1;
2368 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002369 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2370 si = appctx->owner;
2371 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002372
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002373 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002374 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002375 lua_pushnil(L);
2376 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002377 }
2378
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002379 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002380 xref_unlock(&socket->xref, peer);
2381 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382}
2383
2384/* Returns information about my connection side. */
2385static int hlua_socket_getsockname(struct lua_State *L)
2386{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002387 struct hlua_socket *socket;
2388 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002389 struct appctx *appctx;
2390 struct xref *peer;
2391 struct stream_interface *si;
2392 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002393 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002394
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002395 MAY_LJMP(check_args(L, 1, "getsockname"));
2396
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002397 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002398
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002399 /* Check if we run on the same thread than the xreator thread.
2400 * We cannot access to the socket if the thread is different.
2401 */
2402 if (socket->tid != tid)
2403 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2404
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002405 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002406 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002407 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002408 lua_pushnil(L);
2409 return 1;
2410 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002411 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2412 si = appctx->owner;
2413 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002414
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002415 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002416 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002417 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418 lua_pushnil(L);
2419 return 1;
2420 }
2421
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002422 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002423 xref_unlock(&socket->xref, peer);
2424 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002425}
2426
2427/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002428static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002429 .obj_type = OBJ_TYPE_APPLET,
2430 .name = "<LUA_TCP>",
2431 .fct = hlua_socket_handler,
2432 .release = hlua_socket_release,
2433};
2434
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002435__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002436{
2437 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2438 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002439 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002440 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002441 struct stream_interface *si;
2442 struct stream *s;
2443
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002444 /* Check if we run on the same thread than the xreator thread.
2445 * We cannot access to the socket if the thread is different.
2446 */
2447 if (socket->tid != tid)
2448 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2449
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002450 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002451 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002452 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002453 lua_pushnil(L);
2454 lua_pushstring(L, "Can't connect");
2455 return 2;
2456 }
2457 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2458 si = appctx->owner;
2459 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002460
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002461 /* Check if we run on the same thread than the xreator thread.
2462 * We cannot access to the socket if the thread is different.
2463 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002464 if (socket->tid != tid) {
2465 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002466 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002467 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002468
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002469 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002470 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002471 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472 lua_pushnil(L);
2473 lua_pushstring(L, "Can't connect");
2474 return 2;
2475 }
2476
Willy Tarreaue09101e2018-10-16 17:37:12 +02002477 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002478
2479 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002480 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002481 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002482 lua_pushinteger(L, 1);
2483 return 1;
2484 }
2485
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002486 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2487 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002489 }
2490 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002491 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002492 return 0;
2493}
2494
2495/* This function fail or initite the connection. */
2496__LJMP static int hlua_socket_connect(struct lua_State *L)
2497{
2498 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002499 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002501 struct hlua *hlua;
2502 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002503 int low, high;
2504 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002505 struct xref *peer;
2506 struct stream_interface *si;
2507 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002508
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002509 if (lua_gettop(L) < 2)
2510 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002511
2512 /* Get args. */
2513 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002514
2515 /* Check if we run on the same thread than the xreator thread.
2516 * We cannot access to the socket if the thread is different.
2517 */
2518 if (socket->tid != tid)
2519 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2520
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002521 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002522 if (lua_gettop(L) >= 3) {
2523 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002524 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002525
Tim Duesterhus6edab862018-01-06 19:04:45 +01002526 /* Force the ip to end with a colon, to support IPv6 addresses
2527 * that are not enclosed within square brackets.
2528 */
2529 if (port > 0) {
2530 luaL_buffinit(L, &b);
2531 luaL_addstring(&b, ip);
2532 luaL_addchar(&b, ':');
2533 luaL_pushresult(&b);
2534 ip = lua_tolstring(L, lua_gettop(L), NULL);
2535 }
2536 }
2537
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002538 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002539 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002540 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002541 lua_pushnil(L);
2542 return 1;
2543 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002544
2545 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002546 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 +02002547 if (!addr) {
2548 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002549 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002550 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002551
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002552 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002553 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002554 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002555 if (port == -1) {
2556 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002557 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002558 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002559 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2560 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002561 if (port == -1) {
2562 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002563 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002564 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002565 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002566 }
2567 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002568
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002569 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2570 si = appctx->owner;
2571 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002572
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002573 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002574 xref_unlock(&socket->xref, peer);
2575 WILL_LJMP(luaL_error(L, "connect: internal error"));
2576 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002577 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002578
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002579 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002580
2581 /* inform the stream that we want to be notified whenever the
2582 * connection completes.
2583 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002584 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002585 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002586 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002587
Willy Tarreauf31af932020-01-14 09:59:38 +01002588 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002589
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002590 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2591 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002592 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002593 }
2594 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002595
2596 task_wakeup(s->task, TASK_WOKEN_INIT);
2597 /* Return yield waiting for connection. */
2598
Willy Tarreau9635e032018-10-16 17:52:55 +02002599 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002600
2601 return 0;
2602}
2603
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002604#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2606{
2607 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002608 struct xref *peer;
2609 struct appctx *appctx;
2610 struct stream_interface *si;
2611 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002612
2613 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2614 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002615
2616 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002617 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002618 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002619 lua_pushnil(L);
2620 return 1;
2621 }
2622 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2623 si = appctx->owner;
2624 s = si_strm(si);
2625
2626 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002627 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002628 return MAY_LJMP(hlua_socket_connect(L));
2629}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002630#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631
2632__LJMP static int hlua_socket_setoption(struct lua_State *L)
2633{
2634 return 0;
2635}
2636
2637__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2638{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002639 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002640 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002641 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002642 struct xref *peer;
2643 struct appctx *appctx;
2644 struct stream_interface *si;
2645 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002646
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647 MAY_LJMP(check_args(L, 2, "settimeout"));
2648
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002649 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002650
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002651 /* convert the timeout to millis */
2652 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653
Thierry Fournier17a921b2018-03-08 09:59:02 +01002654 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002655 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002656 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2657
Mark Lakes56cc1252018-03-27 09:48:06 +02002658 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002659 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002660
2661 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002662 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002663 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002664
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002665 /* Check if we run on the same thread than the xreator thread.
2666 * We cannot access to the socket if the thread is different.
2667 */
2668 if (socket->tid != tid)
2669 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2670
Mark Lakes56cc1252018-03-27 09:48:06 +02002671 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002672 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002673 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002674 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2675 WILL_LJMP(lua_error(L));
2676 return 0;
2677 }
2678 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2679 si = appctx->owner;
2680 s = si_strm(si);
2681
Cyril Bonté7bb63452018-08-17 23:51:02 +02002682 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002683 s->req.rto = tmout;
2684 s->req.wto = tmout;
2685 s->res.rto = tmout;
2686 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002687 s->req.rex = tick_add_ifset(now_ms, tmout);
2688 s->req.wex = tick_add_ifset(now_ms, tmout);
2689 s->res.rex = tick_add_ifset(now_ms, tmout);
2690 s->res.wex = tick_add_ifset(now_ms, tmout);
2691
2692 s->task->expire = tick_add_ifset(now_ms, tmout);
2693 task_queue(s->task);
2694
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002695 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002696
Thierry Fourniere9636f12018-03-08 09:54:32 +01002697 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002698 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002699}
2700
2701__LJMP static int hlua_socket_new(lua_State *L)
2702{
2703 struct hlua_socket *socket;
2704 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002705 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002706 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002707
2708 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002709 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710 hlua_pusherror(L, "socket: full stack");
2711 goto out_fail_conf;
2712 }
2713
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002714 /* Create the object: obj[0] = userdata. */
2715 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002716 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002717 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002718 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002719 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002720
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002721 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002722 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002723 hlua_pusherror(L, "socket: uninitialized pools.");
2724 goto out_fail_conf;
2725 }
2726
Willy Tarreau87b09662015-04-03 00:22:06 +02002727 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002728 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2729 lua_setmetatable(L, -2);
2730
Willy Tarreaud420a972015-04-06 00:39:18 +02002731 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002732 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002733 if (!appctx) {
2734 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002735 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002736 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002737
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002738 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002739 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002740 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2741 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002742
Willy Tarreaud420a972015-04-06 00:39:18 +02002743 /* Now create a session, task and stream for this applet */
2744 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2745 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002746 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002747 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002748 }
2749
Willy Tarreau87787ac2017-08-28 16:22:54 +02002750 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002751 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002752 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002753 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002754 }
2755
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002756 /* Initialise cross reference between stream and Lua socket object. */
2757 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002758
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002759 /* Configure "right" stream interface. this "si" is used to connect
2760 * and retrieve data from the server. The connection is initialized
2761 * with the "struct server".
2762 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002763 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002764
2765 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002766 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002767 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002768
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002769 return 1;
2770
Willy Tarreaud420a972015-04-06 00:39:18 +02002771 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002772 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002773 out_fail_sess:
2774 appctx_free(appctx);
2775 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002776 WILL_LJMP(lua_error(L));
2777 return 0;
2778}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002779
2780/*
2781 *
2782 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783 * Class Channel
2784 *
2785 *
2786 */
2787
2788/* Returns the struct hlua_channel join to the class channel in the
2789 * stack entry "ud" or throws an argument error.
2790 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002791__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002792{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002793 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794}
2795
Willy Tarreau47860ed2015-03-10 14:07:50 +01002796/* Pushes the channel onto the top of the stack. If the stask does not have a
2797 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002798 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002799static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002802 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803 return 0;
2804
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002805 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002806 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002807 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808
2809 /* Pop a class sesison metatable and affect it to the userdata. */
2810 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2811 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002812 return 1;
2813}
2814
2815/* Duplicate all the data present in the input channel and put it
2816 * in a string LUA variables. Returns -1 and push a nil value in
2817 * the stack if the channel is closed and all the data are consumed,
2818 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002819 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002820 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002821static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002822{
2823 char *blk1;
2824 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002825 size_t len1;
2826 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002827 int ret;
2828 luaL_Buffer b;
2829
Willy Tarreau06d80a92017-10-19 14:32:15 +02002830 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002831 if (unlikely(ret == 0))
2832 return 0;
2833
2834 if (unlikely(ret < 0)) {
2835 lua_pushnil(L);
2836 return -1;
2837 }
2838
2839 luaL_buffinit(L, &b);
2840 luaL_addlstring(&b, blk1, len1);
2841 if (unlikely(ret == 2))
2842 luaL_addlstring(&b, blk2, len2);
2843 luaL_pushresult(&b);
2844
2845 if (unlikely(ret == 2))
2846 return len1 + len2;
2847 return len1;
2848}
2849
2850/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2851 * a yield. This function keep the data in the buffer.
2852 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002853__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002854{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002855 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002856
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002857 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2858
Thierry Fournier77016da2020-08-15 14:35:51 +02002859 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2860 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002861 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002862 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002863
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002864 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002865 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002866 return 1;
2867}
2868
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002869/* Check arguments for the function "hlua_channel_dup_yield". */
2870__LJMP static int hlua_channel_dup(lua_State *L)
2871{
2872 MAY_LJMP(check_args(L, 1, "dup"));
2873 MAY_LJMP(hlua_checkchannel(L, 1));
2874 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2875}
2876
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002877/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2878 * a yield. This function consumes the data in the buffer. It returns
2879 * a string containing the data or a nil pointer if no data are available
2880 * and the channel is closed.
2881 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002882__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002883{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002884 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002885 int ret;
2886
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002887 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002888
Thierry Fournier77016da2020-08-15 14:35:51 +02002889 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2890 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002891 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002892 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002893
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002894 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002896 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002897
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002898 if (unlikely(ret == -1))
2899 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002901 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002902 return 1;
2903}
2904
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002905/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002906__LJMP static int hlua_channel_get(lua_State *L)
2907{
2908 MAY_LJMP(check_args(L, 1, "get"));
2909 MAY_LJMP(hlua_checkchannel(L, 1));
2910 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2911}
2912
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002913/* This functions consumes and returns one line. If the channel is closed,
2914 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002915 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 * value.
2917 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002918__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920 char *blk1;
2921 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002922 size_t len1;
2923 size_t len2;
2924 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002925 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002926 int ret;
2927 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002928
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002929 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2930
Thierry Fournier77016da2020-08-15 14:35:51 +02002931 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2932 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002933 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002934 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002935
Willy Tarreau06d80a92017-10-19 14:32:15 +02002936 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002938 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002939
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940 if (ret == -1) {
2941 lua_pushnil(L);
2942 return 1;
2943 }
2944
2945 luaL_buffinit(L, &b);
2946 luaL_addlstring(&b, blk1, len1);
2947 len = len1;
2948 if (unlikely(ret == 2)) {
2949 luaL_addlstring(&b, blk2, len2);
2950 len += len2;
2951 }
2952 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002953 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002954 return 1;
2955}
2956
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002957/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002958__LJMP static int hlua_channel_getline(lua_State *L)
2959{
2960 MAY_LJMP(check_args(L, 1, "getline"));
2961 MAY_LJMP(hlua_checkchannel(L, 1));
2962 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2963}
2964
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002965/* This function takes a string as input, and append it at the
2966 * input side of channel. If the data is too big, but a space
2967 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002968 * yields. If the data is bigger than the buffer, or if the
2969 * channel is closed, it returns -1. Otherwise, it returns the
2970 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002972__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002973{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002974 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002975 size_t len;
2976 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2977 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2978 int ret;
2979 int max;
2980
Thierry Fournier77016da2020-08-15 14:35:51 +02002981 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2982 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002983 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002984 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002985
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002986 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002987 * the request buffer if its not required.
2988 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002989 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002990 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002991 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002992 }
2993
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002994 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002995 if (max > len - l)
2996 max = len - l;
2997
Willy Tarreau06d80a92017-10-19 14:32:15 +02002998 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002999 if (ret == -2 || ret == -3) {
3000 lua_pushinteger(L, -1);
3001 return 1;
3002 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003003 if (ret == -1) {
3004 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003005 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003006 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003007 l += ret;
3008 lua_pop(L, 1);
3009 lua_pushinteger(L, l);
3010
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003011 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003012 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003013 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003015 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016 */
3017 return 1;
3018 }
3019 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003020 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 return 1;
3022}
3023
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003024/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3025 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 * buffer size is too little for the data.
3027 */
3028__LJMP static int hlua_channel_append(lua_State *L)
3029{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003030 size_t len;
3031
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003032 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003033 MAY_LJMP(hlua_checkchannel(L, 1));
3034 MAY_LJMP(luaL_checklstring(L, 2, &len));
3035 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036 lua_pushinteger(L, 0);
3037
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003038 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003039}
3040
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003041/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003042 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003043 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003044 * or -1 if the channel is closed or if the buffer size is too
3045 * little for the data.
3046 */
3047__LJMP static int hlua_channel_set(lua_State *L)
3048{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003049 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003050
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003052 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053 lua_pushinteger(L, 0);
3054
Thierry Fournier77016da2020-08-15 14:35:51 +02003055 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3056 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003057 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003058 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003059
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003060 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003062 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003063}
3064
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003065/* Append data in the output side of the buffer. This data is immediately
3066 * sent. The function returns the amount of data written. If the buffer
3067 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003068 * if the channel is closed.
3069 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003070__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003072 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073 size_t len;
3074 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3075 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3076 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003077 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003078
Thierry Fournier77016da2020-08-15 14:35:51 +02003079 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3080 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003081 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003082 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003083
Willy Tarreau47860ed2015-03-10 14:07:50 +01003084 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003085 lua_pushinteger(L, -1);
3086 return 1;
3087 }
3088
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003089 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003090 * the request buffer if its not required.
3091 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003092 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003093 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003094 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003095 }
3096
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003097 /* The written data will be immediately sent, so we can check
3098 * the available space without taking in account the reserve.
3099 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003100 * data, because the buffer will be flushed.
3101 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003102 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003103
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003104 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003105 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003106 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003107 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003108 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003109 return 1;
3110
3111 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003112 if (max > len - l)
3113 max = len - l;
3114
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003115 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003116 * detects a non contiguous buffer and realign it.
3117 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003118 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003119 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003120
3121 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003122 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003123
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003124 /* buffer replace considers that the input part is filled.
3125 * so, I must forward these new data in the output part.
3126 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003127 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003128
3129 l += max;
3130 lua_pop(L, 1);
3131 lua_pushinteger(L, l);
3132
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003133 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003134 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003135 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003136 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003137 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003138 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003139 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003140
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003141 if (l < len) {
3142 /* If we are waiting for space in the response buffer, we
3143 * must set the flag WAKERESWR. This flag required the task
3144 * wake up if any activity is detected on the response buffer.
3145 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003146 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003147 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003148 else
3149 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003150 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003151 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003152
3153 return 1;
3154}
3155
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003156/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003157 * yield the LUA process, and resume it without checking the
3158 * input arguments.
3159 */
3160__LJMP static int hlua_channel_send(lua_State *L)
3161{
3162 MAY_LJMP(check_args(L, 2, "send"));
3163 lua_pushinteger(L, 0);
3164
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003165 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003166}
3167
3168/* This function forward and amount of butes. The data pass from
3169 * the input side of the buffer to the output side, and can be
3170 * forwarded. This function never fails.
3171 *
3172 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003173 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003174 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003175__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003176{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003177 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003178 int len;
3179 int l;
3180 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003181 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003182
3183 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003184
Thierry Fournier77016da2020-08-15 14:35:51 +02003185 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3186 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003187 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003188 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003189
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003190 len = MAY_LJMP(luaL_checkinteger(L, 2));
3191 l = MAY_LJMP(luaL_checkinteger(L, -1));
3192
3193 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003194 if (max > ci_data(chn))
3195 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003196 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003197 l += max;
3198
3199 lua_pop(L, 1);
3200 lua_pushinteger(L, l);
3201
3202 /* Check if it miss bytes to forward. */
3203 if (l < len) {
3204 /* The the input channel or the output channel are closed, we
3205 * must return the amount of data forwarded.
3206 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003207 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003208 return 1;
3209
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003210 /* If we are waiting for space data in the response buffer, we
3211 * must set the flag WAKERESWR. This flag required the task
3212 * wake up if any activity is detected on the response buffer.
3213 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003214 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003215 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003216 else
3217 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003218
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003219 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003220 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003221 }
3222
3223 return 1;
3224}
3225
3226/* Just check the input and prepare the stack for the previous
3227 * function "hlua_channel_forward_yield"
3228 */
3229__LJMP static int hlua_channel_forward(lua_State *L)
3230{
3231 MAY_LJMP(check_args(L, 2, "forward"));
3232 MAY_LJMP(hlua_checkchannel(L, 1));
3233 MAY_LJMP(luaL_checkinteger(L, 2));
3234
3235 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003236 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003237}
3238
3239/* Just returns the number of bytes available in the input
3240 * side of the buffer. This function never fails.
3241 */
3242__LJMP static int hlua_channel_get_in_len(lua_State *L)
3243{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003244 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003245
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003246 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003247 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003248 if (IS_HTX_STRM(chn_strm(chn))) {
3249 struct htx *htx = htxbuf(&chn->buf);
3250 lua_pushinteger(L, htx->data - co_data(chn));
3251 }
3252 else
3253 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003254 return 1;
3255}
3256
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003257/* Returns true if the channel is full. */
3258__LJMP static int hlua_channel_is_full(lua_State *L)
3259{
3260 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003261
3262 MAY_LJMP(check_args(L, 1, "is_full"));
3263 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003264 /* ignore the reserve, we are not on a producer side (ie in an
3265 * applet).
3266 */
3267 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003268 return 1;
3269}
3270
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003271/* Returns true if the channel is the response channel. */
3272__LJMP static int hlua_channel_is_resp(lua_State *L)
3273{
3274 struct channel *chn;
3275
3276 MAY_LJMP(check_args(L, 1, "is_resp"));
3277 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3278
3279 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3280 return 1;
3281}
3282
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003283/* Just returns the number of bytes available in the output
3284 * side of the buffer. This function never fails.
3285 */
3286__LJMP static int hlua_channel_get_out_len(lua_State *L)
3287{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003288 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003289
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003290 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003291 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003292 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003293 return 1;
3294}
3295
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003296/*
3297 *
3298 *
3299 * Class Fetches
3300 *
3301 *
3302 */
3303
3304/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003305 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003306 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003307__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003308{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003309 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003310}
3311
3312/* This function creates and push in the stack a fetch object according
3313 * with a current TXN.
3314 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003315static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003316{
Willy Tarreau7073c472015-04-06 11:15:40 +02003317 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003318
3319 /* Check stack size. */
3320 if (!lua_checkstack(L, 3))
3321 return 0;
3322
3323 /* Create the object: obj[0] = userdata.
3324 * Note that the base of the Fetches object is the
3325 * transaction object.
3326 */
3327 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003328 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003329 lua_rawseti(L, -2, 0);
3330
Willy Tarreau7073c472015-04-06 11:15:40 +02003331 hsmp->s = txn->s;
3332 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003333 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003334 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003335
3336 /* Pop a class sesison metatable and affect it to the userdata. */
3337 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3338 lua_setmetatable(L, -2);
3339
3340 return 1;
3341}
3342
3343/* This function is an LUA binding. It is called with each sample-fetch.
3344 * It uses closure argument to store the associated sample-fetch. It
3345 * returns only one argument or throws an error. An error is thrown
3346 * only if an error is encountered during the argument parsing. If
3347 * the "sample-fetch" function fails, nil is returned.
3348 */
3349__LJMP static int hlua_run_sample_fetch(lua_State *L)
3350{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003351 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003352 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003353 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003354 int i;
3355 struct sample smp;
3356
3357 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003358 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003359
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003360 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003361 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003362
Thierry FOURNIERca988662015-12-20 18:43:03 +01003363 /* Check execution authorization. */
3364 if (f->use & SMP_USE_HTTP_ANY &&
3365 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3366 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3367 "is not available in Lua services", f->kw);
3368 WILL_LJMP(lua_error(L));
3369 }
3370
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003371 /* Get extra arguments. */
3372 for (i = 0; i < lua_gettop(L) - 1; i++) {
3373 if (i >= ARGM_NBARGS)
3374 break;
3375 hlua_lua2arg(L, i + 2, &args[i]);
3376 }
3377 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003378 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003379
3380 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003381 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003382
3383 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003384 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003385 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003386 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003387 }
3388
3389 /* Initialise the sample. */
3390 memset(&smp, 0, sizeof(smp));
3391
3392 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003393 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003394 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003395 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003396 lua_pushstring(L, "");
3397 else
3398 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003399 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003400 }
3401
3402 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003403 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003404 hlua_smp2lua_str(L, &smp);
3405 else
3406 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003407
3408 end:
3409 for (i = 0; args[i].type != ARGT_STOP; i++) {
3410 if (args[i].type == ARGT_STR)
3411 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003412 else if (args[i].type == ARGT_REG)
3413 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003414 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003415 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003416
3417 error:
3418 for (i = 0; args[i].type != ARGT_STOP; i++) {
3419 if (args[i].type == ARGT_STR)
3420 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003421 else if (args[i].type == ARGT_REG)
3422 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003423 }
3424 WILL_LJMP(lua_error(L));
3425 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003426}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003427
3428/*
3429 *
3430 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003431 * Class Converters
3432 *
3433 *
3434 */
3435
3436/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003437 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003438 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003439__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003440{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003441 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003442}
3443
3444/* This function creates and push in the stack a Converters object
3445 * according with a current TXN.
3446 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003447static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003448{
Willy Tarreau7073c472015-04-06 11:15:40 +02003449 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003450
3451 /* Check stack size. */
3452 if (!lua_checkstack(L, 3))
3453 return 0;
3454
3455 /* Create the object: obj[0] = userdata.
3456 * Note that the base of the Converters object is the
3457 * same than the TXN object.
3458 */
3459 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003460 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003461 lua_rawseti(L, -2, 0);
3462
Willy Tarreau7073c472015-04-06 11:15:40 +02003463 hsmp->s = txn->s;
3464 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003465 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003466 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003467
Willy Tarreau87b09662015-04-03 00:22:06 +02003468 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003469 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3470 lua_setmetatable(L, -2);
3471
3472 return 1;
3473}
3474
3475/* This function is an LUA binding. It is called with each converter.
3476 * It uses closure argument to store the associated converter. It
3477 * returns only one argument or throws an error. An error is thrown
3478 * only if an error is encountered during the argument parsing. If
3479 * the converter function function fails, nil is returned.
3480 */
3481__LJMP static int hlua_run_sample_conv(lua_State *L)
3482{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003483 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003484 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003485 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003486 int i;
3487 struct sample smp;
3488
3489 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003490 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003491
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003492 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003493 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003494
3495 /* Get extra arguments. */
3496 for (i = 0; i < lua_gettop(L) - 2; i++) {
3497 if (i >= ARGM_NBARGS)
3498 break;
3499 hlua_lua2arg(L, i + 3, &args[i]);
3500 }
3501 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003502 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003503
3504 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003505 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003506
3507 /* Run the special args checker. */
3508 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3509 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003510 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003511 }
3512
3513 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003514 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003515 if (!hlua_lua2smp(L, 2, &smp)) {
3516 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003517 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003518 }
3519
Willy Tarreau1777ea62016-03-10 16:15:46 +01003520 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3521
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003522 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003523 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003524 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003525 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003526 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003527 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003528 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3529 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003530 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003531 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003532 }
3533
3534 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003535 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003536 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003537 lua_pushstring(L, "");
3538 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003539 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003540 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003541 }
3542
3543 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003544 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003545 hlua_smp2lua_str(L, &smp);
3546 else
3547 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003548 end:
3549 for (i = 0; args[i].type != ARGT_STOP; i++) {
3550 if (args[i].type == ARGT_STR)
3551 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003552 else if (args[i].type == ARGT_REG)
3553 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003554 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003555 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003556
3557 error:
3558 for (i = 0; args[i].type != ARGT_STOP; i++) {
3559 if (args[i].type == ARGT_STR)
3560 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003561 else if (args[i].type == ARGT_REG)
3562 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003563 }
3564 WILL_LJMP(lua_error(L));
3565 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003566}
3567
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003568/*
3569 *
3570 *
3571 * Class AppletTCP
3572 *
3573 *
3574 */
3575
3576/* Returns a struct hlua_txn if the stack entry "ud" is
3577 * a class stream, otherwise it throws an error.
3578 */
3579__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3580{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003581 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003582}
3583
3584/* This function creates and push in the stack an Applet object
3585 * according with a current TXN.
3586 */
3587static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3588{
3589 struct hlua_appctx *appctx;
3590 struct stream_interface *si = ctx->owner;
3591 struct stream *s = si_strm(si);
3592 struct proxy *p = s->be;
3593
3594 /* Check stack size. */
3595 if (!lua_checkstack(L, 3))
3596 return 0;
3597
3598 /* Create the object: obj[0] = userdata.
3599 * Note that the base of the Converters object is the
3600 * same than the TXN object.
3601 */
3602 lua_newtable(L);
3603 appctx = lua_newuserdata(L, sizeof(*appctx));
3604 lua_rawseti(L, -2, 0);
3605 appctx->appctx = ctx;
3606 appctx->htxn.s = s;
3607 appctx->htxn.p = p;
3608
3609 /* Create the "f" field that contains a list of fetches. */
3610 lua_pushstring(L, "f");
3611 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3612 return 0;
3613 lua_settable(L, -3);
3614
3615 /* Create the "sf" field that contains a list of stringsafe fetches. */
3616 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003617 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003618 return 0;
3619 lua_settable(L, -3);
3620
3621 /* Create the "c" field that contains a list of converters. */
3622 lua_pushstring(L, "c");
3623 if (!hlua_converters_new(L, &appctx->htxn, 0))
3624 return 0;
3625 lua_settable(L, -3);
3626
3627 /* Create the "sc" field that contains a list of stringsafe converters. */
3628 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003629 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003630 return 0;
3631 lua_settable(L, -3);
3632
3633 /* Pop a class stream metatable and affect it to the table. */
3634 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3635 lua_setmetatable(L, -2);
3636
3637 return 1;
3638}
3639
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003640__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3641{
3642 struct hlua_appctx *appctx;
3643 struct stream *s;
3644 const char *name;
3645 size_t len;
3646 struct sample smp;
3647
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003648 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3649 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003650
3651 /* It is useles to retrieve the stream, but this function
3652 * runs only in a stream context.
3653 */
3654 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3655 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3656 s = appctx->htxn.s;
3657
3658 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003659 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003660 hlua_lua2smp(L, 3, &smp);
3661
3662 /* Store the sample in a variable. */
3663 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003664
3665 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3666 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3667 else
3668 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3669
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003670 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003671}
3672
3673__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3674{
3675 struct hlua_appctx *appctx;
3676 struct stream *s;
3677 const char *name;
3678 size_t len;
3679 struct sample smp;
3680
3681 MAY_LJMP(check_args(L, 2, "unset_var"));
3682
3683 /* It is useles to retrieve the stream, but this function
3684 * runs only in a stream context.
3685 */
3686 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3687 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3688 s = appctx->htxn.s;
3689
3690 /* Unset the variable. */
3691 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003692 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3693 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003694}
3695
3696__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3697{
3698 struct hlua_appctx *appctx;
3699 struct stream *s;
3700 const char *name;
3701 size_t len;
3702 struct sample smp;
3703
3704 MAY_LJMP(check_args(L, 2, "get_var"));
3705
3706 /* It is useles to retrieve the stream, but this function
3707 * runs only in a stream context.
3708 */
3709 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3710 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3711 s = appctx->htxn.s;
3712
3713 smp_set_owner(&smp, s->be, s->sess, s, 0);
3714 if (!vars_get_by_name(name, len, &smp)) {
3715 lua_pushnil(L);
3716 return 1;
3717 }
3718
3719 return hlua_smp2lua(L, &smp);
3720}
3721
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003722__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3723{
3724 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3725 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003726 struct hlua *hlua;
3727
3728 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003729 if (!s->hlua)
3730 return 0;
3731 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003732
3733 MAY_LJMP(check_args(L, 2, "set_priv"));
3734
3735 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003736 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003737
3738 /* Get and store new value. */
3739 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3740 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3741
3742 return 0;
3743}
3744
3745__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3746{
3747 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3748 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003749 struct hlua *hlua;
3750
3751 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003752 if (!s->hlua) {
3753 lua_pushnil(L);
3754 return 1;
3755 }
3756 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003757
3758 /* Push configuration index in the stack. */
3759 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3760
3761 return 1;
3762}
3763
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003764/* If expected data not yet available, it returns a yield. This function
3765 * consumes the data in the buffer. It returns a string containing the
3766 * data. This string can be empty.
3767 */
3768__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3769{
3770 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3771 struct stream_interface *si = appctx->appctx->owner;
3772 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003773 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003774 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003775 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003776 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003777
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003778 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003779 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003780
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003781 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003782 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003783 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003784 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003785 }
3786
3787 /* End of data: commit the total strings and return. */
3788 if (ret < 0) {
3789 luaL_pushresult(&appctx->b);
3790 return 1;
3791 }
3792
3793 /* Ensure that the block 2 length is usable. */
3794 if (ret == 1)
3795 len2 = 0;
3796
3797 /* dont check the max length read and dont check. */
3798 luaL_addlstring(&appctx->b, blk1, len1);
3799 luaL_addlstring(&appctx->b, blk2, len2);
3800
3801 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003802 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003803 luaL_pushresult(&appctx->b);
3804 return 1;
3805}
3806
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003807/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003808__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3809{
3810 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3811
3812 /* Initialise the string catenation. */
3813 luaL_buffinit(L, &appctx->b);
3814
3815 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3816}
3817
3818/* If expected data not yet available, it returns a yield. This function
3819 * consumes the data in the buffer. It returns a string containing the
3820 * data. This string can be empty.
3821 */
3822__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3823{
3824 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3825 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003826 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003827 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003828 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003829 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003830 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003831 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003832
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003833 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003834 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003835
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003836 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003837 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003838 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003839 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003840 }
3841
3842 /* End of data: commit the total strings and return. */
3843 if (ret < 0) {
3844 luaL_pushresult(&appctx->b);
3845 return 1;
3846 }
3847
3848 /* Ensure that the block 2 length is usable. */
3849 if (ret == 1)
3850 len2 = 0;
3851
3852 if (len == -1) {
3853
3854 /* If len == -1, catenate all the data avalaile and
3855 * yield because we want to get all the data until
3856 * the end of data stream.
3857 */
3858 luaL_addlstring(&appctx->b, blk1, len1);
3859 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003860 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003861 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003862 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003863
3864 } else {
3865
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003866 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003867 if (len1 > len)
3868 len1 = len;
3869 luaL_addlstring(&appctx->b, blk1, len1);
3870 len -= len1;
3871
3872 /* Copy the second block. */
3873 if (len2 > len)
3874 len2 = len;
3875 luaL_addlstring(&appctx->b, blk2, len2);
3876 len -= len2;
3877
3878 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003879 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003880
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003881 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003882 if (len > 0) {
3883 lua_pushinteger(L, len);
3884 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003885 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003886 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003887 }
3888
3889 /* return the result. */
3890 luaL_pushresult(&appctx->b);
3891 return 1;
3892 }
3893
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003894 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003895 hlua_pusherror(L, "Lua: internal error");
3896 WILL_LJMP(lua_error(L));
3897 return 0;
3898}
3899
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003900/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003901__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3902{
3903 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3904 int len = -1;
3905
3906 if (lua_gettop(L) > 2)
3907 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3908 if (lua_gettop(L) >= 2) {
3909 len = MAY_LJMP(luaL_checkinteger(L, 2));
3910 lua_pop(L, 1);
3911 }
3912
3913 /* Confirm or set the required length */
3914 lua_pushinteger(L, len);
3915
3916 /* Initialise the string catenation. */
3917 luaL_buffinit(L, &appctx->b);
3918
3919 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3920}
3921
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003922/* Append data in the output side of the buffer. This data is immediately
3923 * sent. The function returns the amount of data written. If the buffer
3924 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003925 * if the channel is closed.
3926 */
3927__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3928{
3929 size_t len;
3930 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3931 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3932 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3933 struct stream_interface *si = appctx->appctx->owner;
3934 struct channel *chn = si_ic(si);
3935 int max;
3936
3937 /* Get the max amount of data which can write as input in the channel. */
3938 max = channel_recv_max(chn);
3939 if (max > (len - l))
3940 max = len - l;
3941
3942 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003943 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003944
3945 /* update counters. */
3946 l += max;
3947 lua_pop(L, 1);
3948 lua_pushinteger(L, l);
3949
3950 /* If some data is not send, declares the situation to the
3951 * applet, and returns a yield.
3952 */
3953 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003954 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003955 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003956 }
3957
3958 return 1;
3959}
3960
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003961/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003962 * yield the LUA process, and resume it without checking the
3963 * input arguments.
3964 */
3965__LJMP static int hlua_applet_tcp_send(lua_State *L)
3966{
3967 MAY_LJMP(check_args(L, 2, "send"));
3968 lua_pushinteger(L, 0);
3969
3970 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3971}
3972
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003973/*
3974 *
3975 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003976 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003977 *
3978 *
3979 */
3980
3981/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003982 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003983 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003984__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003985{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003986 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003987}
3988
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003989/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003990 * according with a current TXN.
3991 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003992static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003993{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003994 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003995 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003996 struct stream_interface *si = ctx->owner;
3997 struct stream *s = si_strm(si);
3998 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003999 struct htx *htx;
4000 struct htx_blk *blk;
4001 struct htx_sl *sl;
4002 struct ist path;
4003 unsigned long long len = 0;
4004 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004005
4006 /* Check stack size. */
4007 if (!lua_checkstack(L, 3))
4008 return 0;
4009
4010 /* Create the object: obj[0] = userdata.
4011 * Note that the base of the Converters object is the
4012 * same than the TXN object.
4013 */
4014 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004015 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004016 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004017 appctx->appctx = ctx;
4018 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004019 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004020 appctx->htxn.s = s;
4021 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004022
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004023 /* Create the "f" field that contains a list of fetches. */
4024 lua_pushstring(L, "f");
4025 if (!hlua_fetches_new(L, &appctx->htxn, 0))
4026 return 0;
4027 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004028
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004029 /* Create the "sf" field that contains a list of stringsafe fetches. */
4030 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004031 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004032 return 0;
4033 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004034
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004035 /* Create the "c" field that contains a list of converters. */
4036 lua_pushstring(L, "c");
4037 if (!hlua_converters_new(L, &appctx->htxn, 0))
4038 return 0;
4039 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004040
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004041 /* Create the "sc" field that contains a list of stringsafe converters. */
4042 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004043 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004044 return 0;
4045 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004046
Christopher Fauleta2097962019-07-15 16:25:33 +02004047 htx = htxbuf(&s->req.buf);
4048 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004049 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004050 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004051
Christopher Fauleta2097962019-07-15 16:25:33 +02004052 /* Stores the request method. */
4053 lua_pushstring(L, "method");
4054 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4055 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004056
Christopher Fauleta2097962019-07-15 16:25:33 +02004057 /* Stores the http version. */
4058 lua_pushstring(L, "version");
4059 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4060 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004061
Christopher Fauleta2097962019-07-15 16:25:33 +02004062 /* creates an array of headers. hlua_http_get_headers() crates and push
4063 * the array on the top of the stack.
4064 */
4065 lua_pushstring(L, "headers");
4066 htxn.s = s;
4067 htxn.p = px;
4068 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004069 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004070 return 0;
4071 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004072
Christopher Fauleta2097962019-07-15 16:25:33 +02004073 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004074 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004075 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004076
Christopher Fauleta2097962019-07-15 16:25:33 +02004077 p = path.ptr;
4078 end = path.ptr + path.len;
4079 q = p;
4080 while (q < end && *q != '?')
4081 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004082
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004083 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004084 lua_pushstring(L, "path");
4085 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004086 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004087
Christopher Fauleta2097962019-07-15 16:25:33 +02004088 /* Stores the query string. */
4089 lua_pushstring(L, "qs");
4090 if (*q == '?')
4091 q++;
4092 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004093 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004094 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004095
Christopher Fauleta2097962019-07-15 16:25:33 +02004096 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4097 struct htx_blk *blk = htx_get_blk(htx, pos);
4098 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004099
Christopher Fauleta2097962019-07-15 16:25:33 +02004100 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
4101 break;
4102 if (type == HTX_BLK_DATA)
4103 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004104 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004105 if (htx->extra != ULLONG_MAX)
4106 len += htx->extra;
4107
4108 /* Stores the request path. */
4109 lua_pushstring(L, "length");
4110 lua_pushinteger(L, len);
4111 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004112
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004113 /* Create an empty array of HTTP request headers. */
4114 lua_pushstring(L, "response");
4115 lua_newtable(L);
4116 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004117
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004118 /* Pop a class stream metatable and affect it to the table. */
4119 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4120 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004121
4122 return 1;
4123}
4124
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004125__LJMP static int hlua_applet_http_set_var(lua_State *L)
4126{
4127 struct hlua_appctx *appctx;
4128 struct stream *s;
4129 const char *name;
4130 size_t len;
4131 struct sample smp;
4132
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004133 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4134 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004135
4136 /* It is useles to retrieve the stream, but this function
4137 * runs only in a stream context.
4138 */
4139 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4140 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4141 s = appctx->htxn.s;
4142
4143 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004144 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004145 hlua_lua2smp(L, 3, &smp);
4146
4147 /* Store the sample in a variable. */
4148 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004149
4150 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4151 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4152 else
4153 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4154
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004155 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004156}
4157
4158__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4159{
4160 struct hlua_appctx *appctx;
4161 struct stream *s;
4162 const char *name;
4163 size_t len;
4164 struct sample smp;
4165
4166 MAY_LJMP(check_args(L, 2, "unset_var"));
4167
4168 /* It is useles to retrieve the stream, but this function
4169 * runs only in a stream context.
4170 */
4171 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4172 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4173 s = appctx->htxn.s;
4174
4175 /* Unset the variable. */
4176 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004177 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4178 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004179}
4180
4181__LJMP static int hlua_applet_http_get_var(lua_State *L)
4182{
4183 struct hlua_appctx *appctx;
4184 struct stream *s;
4185 const char *name;
4186 size_t len;
4187 struct sample smp;
4188
4189 MAY_LJMP(check_args(L, 2, "get_var"));
4190
4191 /* It is useles to retrieve the stream, but this function
4192 * runs only in a stream context.
4193 */
4194 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4195 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4196 s = appctx->htxn.s;
4197
4198 smp_set_owner(&smp, s->be, s->sess, s, 0);
4199 if (!vars_get_by_name(name, len, &smp)) {
4200 lua_pushnil(L);
4201 return 1;
4202 }
4203
4204 return hlua_smp2lua(L, &smp);
4205}
4206
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004207__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4208{
4209 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4210 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004211 struct hlua *hlua;
4212
4213 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004214 if (!s->hlua)
4215 return 0;
4216 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004217
4218 MAY_LJMP(check_args(L, 2, "set_priv"));
4219
4220 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004221 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004222
4223 /* Get and store new value. */
4224 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4225 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4226
4227 return 0;
4228}
4229
4230__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4231{
4232 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4233 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004234 struct hlua *hlua;
4235
4236 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004237 if (!s->hlua) {
4238 lua_pushnil(L);
4239 return 1;
4240 }
4241 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004242
4243 /* Push configuration index in the stack. */
4244 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4245
4246 return 1;
4247}
4248
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004249/* If expected data not yet available, it returns a yield. This function
4250 * consumes the data in the buffer. It returns a string containing the
4251 * data. This string can be empty.
4252 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004253__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004254{
4255 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4256 struct stream_interface *si = appctx->appctx->owner;
4257 struct channel *req = si_oc(si);
4258 struct htx *htx;
4259 struct htx_blk *blk;
4260 size_t count;
4261 int stop = 0;
4262
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004263 htx = htx_from_buf(&req->buf);
4264 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004265 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004266
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004267 while (count && !stop && blk) {
4268 enum htx_blk_type type = htx_get_blk_type(blk);
4269 uint32_t sz = htx_get_blksz(blk);
4270 struct ist v;
4271 uint32_t vlen;
4272 char *nl;
4273
Christopher Faulete461e342018-12-18 16:43:35 +01004274 if (type == HTX_BLK_EOM) {
4275 stop = 1;
4276 break;
4277 }
4278
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004279 vlen = sz;
4280 if (vlen > count) {
4281 if (type != HTX_BLK_DATA)
4282 break;
4283 vlen = count;
4284 }
4285
4286 switch (type) {
4287 case HTX_BLK_UNUSED:
4288 break;
4289
4290 case HTX_BLK_DATA:
4291 v = htx_get_blk_value(htx, blk);
4292 v.len = vlen;
4293 nl = istchr(v, '\n');
4294 if (nl != NULL) {
4295 stop = 1;
4296 vlen = nl - v.ptr + 1;
4297 }
4298 luaL_addlstring(&appctx->b, v.ptr, vlen);
4299 break;
4300
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004301 case HTX_BLK_TLR:
4302 case HTX_BLK_EOM:
4303 stop = 1;
4304 break;
4305
4306 default:
4307 break;
4308 }
4309
4310 co_set_data(req, co_data(req) - vlen);
4311 count -= vlen;
4312 if (sz == vlen)
4313 blk = htx_remove_blk(htx, blk);
4314 else {
4315 htx_cut_data_blk(htx, blk, vlen);
4316 break;
4317 }
4318 }
4319
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004320 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004321 if (!stop) {
4322 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004323 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004324 }
4325
4326 /* return the result. */
4327 luaL_pushresult(&appctx->b);
4328 return 1;
4329}
4330
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004331
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004332/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004333__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004334{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004335 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004336
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004337 /* Initialise the string catenation. */
4338 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004339
Christopher Fauleta2097962019-07-15 16:25:33 +02004340 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004341}
4342
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004343/* If expected data not yet available, it returns a yield. This function
4344 * consumes the data in the buffer. It returns a string containing the
4345 * data. This string can be empty.
4346 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004347__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004348{
4349 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4350 struct stream_interface *si = appctx->appctx->owner;
4351 struct channel *req = si_oc(si);
4352 struct htx *htx;
4353 struct htx_blk *blk;
4354 size_t count;
4355 int len;
4356
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004357 htx = htx_from_buf(&req->buf);
4358 len = MAY_LJMP(luaL_checkinteger(L, 2));
4359 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004360 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004361 while (count && len && blk) {
4362 enum htx_blk_type type = htx_get_blk_type(blk);
4363 uint32_t sz = htx_get_blksz(blk);
4364 struct ist v;
4365 uint32_t vlen;
4366
Christopher Faulete461e342018-12-18 16:43:35 +01004367 if (type == HTX_BLK_EOM) {
4368 len = 0;
4369 break;
4370 }
4371
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004372 vlen = sz;
4373 if (len > 0 && vlen > len)
4374 vlen = len;
4375 if (vlen > count) {
4376 if (type != HTX_BLK_DATA)
4377 break;
4378 vlen = count;
4379 }
4380
4381 switch (type) {
4382 case HTX_BLK_UNUSED:
4383 break;
4384
4385 case HTX_BLK_DATA:
4386 v = htx_get_blk_value(htx, blk);
4387 luaL_addlstring(&appctx->b, v.ptr, vlen);
4388 break;
4389
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004390 case HTX_BLK_TLR:
4391 case HTX_BLK_EOM:
4392 len = 0;
4393 break;
4394
4395 default:
4396 break;
4397 }
4398
4399 co_set_data(req, co_data(req) - vlen);
4400 count -= vlen;
4401 if (len > 0)
4402 len -= vlen;
4403 if (sz == vlen)
4404 blk = htx_remove_blk(htx, blk);
4405 else {
4406 htx_cut_data_blk(htx, blk, vlen);
4407 break;
4408 }
4409 }
4410
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004411 htx_to_buf(htx, &req->buf);
4412
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004413 /* If we are no other data available, yield waiting for new data. */
4414 if (len) {
4415 if (len > 0) {
4416 lua_pushinteger(L, len);
4417 lua_replace(L, 2);
4418 }
4419 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004420 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004421 }
4422
4423 /* return the result. */
4424 luaL_pushresult(&appctx->b);
4425 return 1;
4426}
4427
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004428/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004429__LJMP static int hlua_applet_http_recv(lua_State *L)
4430{
4431 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4432 int len = -1;
4433
4434 /* Check arguments. */
4435 if (lua_gettop(L) > 2)
4436 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4437 if (lua_gettop(L) >= 2) {
4438 len = MAY_LJMP(luaL_checkinteger(L, 2));
4439 lua_pop(L, 1);
4440 }
4441
Christopher Fauleta2097962019-07-15 16:25:33 +02004442 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004443
Christopher Fauleta2097962019-07-15 16:25:33 +02004444 /* Initialise the string catenation. */
4445 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004446
Christopher Fauleta2097962019-07-15 16:25:33 +02004447 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004448}
4449
4450/* Append data in the output side of the buffer. This data is immediately
4451 * sent. The function returns the amount of data written. If the buffer
4452 * cannot contain the data, the function yields. The function returns -1
4453 * if the channel is closed.
4454 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004455__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004456{
4457 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4458 struct stream_interface *si = appctx->appctx->owner;
4459 struct channel *res = si_ic(si);
4460 struct htx *htx = htx_from_buf(&res->buf);
4461 const char *data;
4462 size_t len;
4463 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4464 int max;
4465
Christopher Faulet9060fc02019-07-03 11:39:30 +02004466 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004467 if (!max)
4468 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004469
4470 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4471
4472 /* Get the max amount of data which can write as input in the channel. */
4473 if (max > (len - l))
4474 max = len - l;
4475
4476 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004477 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004478 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004479
4480 /* update counters. */
4481 l += max;
4482 lua_pop(L, 1);
4483 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004484
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004485 /* If some data is not send, declares the situation to the
4486 * applet, and returns a yield.
4487 */
4488 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004489 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004490 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004491 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004492 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004493 }
4494
Christopher Fauleta2097962019-07-15 16:25:33 +02004495 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004496 return 1;
4497}
4498
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004499/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004500 * yield the LUA process, and resume it without checking the
4501 * input arguments.
4502 */
4503__LJMP static int hlua_applet_http_send(lua_State *L)
4504{
4505 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004506
4507 /* We want to send some data. Headers must be sent. */
4508 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4509 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4510 WILL_LJMP(lua_error(L));
4511 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004512
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004513 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004514 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004515
Christopher Fauleta2097962019-07-15 16:25:33 +02004516 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004517}
4518
4519__LJMP static int hlua_applet_http_addheader(lua_State *L)
4520{
4521 const char *name;
4522 int ret;
4523
4524 MAY_LJMP(hlua_checkapplet_http(L, 1));
4525 name = MAY_LJMP(luaL_checkstring(L, 2));
4526 MAY_LJMP(luaL_checkstring(L, 3));
4527
4528 /* Push in the stack the "response" entry. */
4529 ret = lua_getfield(L, 1, "response");
4530 if (ret != LUA_TTABLE) {
4531 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4532 "is expected as an array. %s found", lua_typename(L, ret));
4533 WILL_LJMP(lua_error(L));
4534 }
4535
4536 /* check if the header is already registered if it is not
4537 * the case, register it.
4538 */
4539 ret = lua_getfield(L, -1, name);
4540 if (ret == LUA_TNIL) {
4541
4542 /* Entry not found. */
4543 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4544
4545 /* Insert the new header name in the array in the top of the stack.
4546 * It left the new array in the top of the stack.
4547 */
4548 lua_newtable(L);
4549 lua_pushvalue(L, 2);
4550 lua_pushvalue(L, -2);
4551 lua_settable(L, -4);
4552
4553 } else if (ret != LUA_TTABLE) {
4554
4555 /* corruption error. */
4556 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4557 "is expected as an array. %s found", name, lua_typename(L, ret));
4558 WILL_LJMP(lua_error(L));
4559 }
4560
4561 /* Now the top od thestack is an array of values. We push
4562 * the header value as new entry.
4563 */
4564 lua_pushvalue(L, 3);
4565 ret = lua_rawlen(L, -2);
4566 lua_rawseti(L, -2, ret + 1);
4567 lua_pushboolean(L, 1);
4568 return 1;
4569}
4570
4571__LJMP static int hlua_applet_http_status(lua_State *L)
4572{
4573 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4574 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004575 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004576
4577 if (status < 100 || status > 599) {
4578 lua_pushboolean(L, 0);
4579 return 1;
4580 }
4581
4582 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004583 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004584 lua_pushboolean(L, 1);
4585 return 1;
4586}
4587
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004588
Christopher Fauleta2097962019-07-15 16:25:33 +02004589__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004590{
4591 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4592 struct stream_interface *si = appctx->appctx->owner;
4593 struct channel *res = si_ic(si);
4594 struct htx *htx;
4595 struct htx_sl *sl;
4596 struct h1m h1m;
4597 const char *status, *reason;
4598 const char *name, *value;
4599 size_t nlen, vlen;
4600 unsigned int flags;
4601
4602 /* Send the message at once. */
4603 htx = htx_from_buf(&res->buf);
4604 h1m_init_res(&h1m);
4605
4606 /* Use the same http version than the request. */
4607 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4608 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4609 if (reason == NULL)
4610 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4611 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4612 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4613 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4614 }
4615 else {
4616 flags = HTX_SL_F_IS_RESP;
4617 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4618 }
4619 if (!sl) {
4620 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4621 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4622 WILL_LJMP(lua_error(L));
4623 }
4624 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4625
4626 /* Get the array associated to the field "response" in the object AppletHTTP. */
4627 lua_pushvalue(L, 0);
4628 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4629 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4630 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4631 WILL_LJMP(lua_error(L));
4632 }
4633
4634 /* Browse the list of headers. */
4635 lua_pushnil(L);
4636 while(lua_next(L, -2) != 0) {
4637 /* We expect a string as -2. */
4638 if (lua_type(L, -2) != LUA_TSTRING) {
4639 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4640 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4641 lua_typename(L, lua_type(L, -2)));
4642 WILL_LJMP(lua_error(L));
4643 }
4644 name = lua_tolstring(L, -2, &nlen);
4645
4646 /* We expect an array as -1. */
4647 if (lua_type(L, -1) != LUA_TTABLE) {
4648 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4649 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4650 name,
4651 lua_typename(L, lua_type(L, -1)));
4652 WILL_LJMP(lua_error(L));
4653 }
4654
4655 /* Browse the table who is on the top of the stack. */
4656 lua_pushnil(L);
4657 while(lua_next(L, -2) != 0) {
4658 int id;
4659
4660 /* We expect a number as -2. */
4661 if (lua_type(L, -2) != LUA_TNUMBER) {
4662 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4663 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4664 name,
4665 lua_typename(L, lua_type(L, -2)));
4666 WILL_LJMP(lua_error(L));
4667 }
4668 id = lua_tointeger(L, -2);
4669
4670 /* We expect a string as -2. */
4671 if (lua_type(L, -1) != LUA_TSTRING) {
4672 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4673 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4674 name, id,
4675 lua_typename(L, lua_type(L, -1)));
4676 WILL_LJMP(lua_error(L));
4677 }
4678 value = lua_tolstring(L, -1, &vlen);
4679
4680 /* Simple Protocol checks. */
4681 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004682 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004683 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4684 struct ist v = ist2(value, vlen);
4685 int ret;
4686
4687 ret = h1_parse_cont_len_header(&h1m, &v);
4688 if (ret < 0) {
4689 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4690 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4691 name);
4692 WILL_LJMP(lua_error(L));
4693 }
4694 else if (ret == 0)
4695 goto next; /* Skip it */
4696 }
4697
4698 /* Add a new header */
4699 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4700 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4701 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4702 name);
4703 WILL_LJMP(lua_error(L));
4704 }
4705 next:
4706 /* Remove the array from the stack, and get next element with a remaining string. */
4707 lua_pop(L, 1);
4708 }
4709
4710 /* Remove the array from the stack, and get next element with a remaining string. */
4711 lua_pop(L, 1);
4712 }
4713
4714 if (h1m.flags & H1_MF_CHNK)
4715 h1m.flags &= ~H1_MF_CLEN;
4716 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4717 h1m.flags |= H1_MF_XFER_LEN;
4718
4719 /* Uset HTX start-line flags */
4720 if (h1m.flags & H1_MF_XFER_ENC)
4721 flags |= HTX_SL_F_XFER_ENC;
4722 if (h1m.flags & H1_MF_XFER_LEN) {
4723 flags |= HTX_SL_F_XFER_LEN;
4724 if (h1m.flags & H1_MF_CHNK)
4725 flags |= HTX_SL_F_CHNK;
4726 else if (h1m.flags & H1_MF_CLEN)
4727 flags |= HTX_SL_F_CLEN;
4728 if (h1m.body_len == 0)
4729 flags |= HTX_SL_F_BODYLESS;
4730 }
4731 sl->flags |= flags;
4732
4733 /* If we dont have a content-length set, and the HTTP version is 1.1
4734 * and the status code implies the presence of a message body, we must
4735 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004736 * for the keepalive compliance. If the applet announces a transfer-encoding
4737 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004738 */
4739 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4740 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4741 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4742 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4743 /* Add a new header */
4744 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4745 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4746 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4747 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4748 WILL_LJMP(lua_error(L));
4749 }
4750 }
4751
4752 /* Finalize headers. */
4753 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4754 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4755 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4756 WILL_LJMP(lua_error(L));
4757 }
4758
4759 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4760 b_reset(&res->buf);
4761 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4762 WILL_LJMP(lua_error(L));
4763 }
4764
4765 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004766 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004767
4768 /* Headers sent, set the flag. */
4769 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4770 return 0;
4771
4772}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004773/* We will build the status line and the headers of the HTTP response.
4774 * We will try send at once if its not possible, we give back the hand
4775 * waiting for more room.
4776 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004777__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004778{
4779 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4780 struct stream_interface *si = appctx->appctx->owner;
4781 struct channel *res = si_ic(si);
4782
4783 if (co_data(res)) {
4784 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004785 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004786 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004787 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004788}
4789
4790
Christopher Fauleta2097962019-07-15 16:25:33 +02004791__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004792{
Christopher Fauleta2097962019-07-15 16:25:33 +02004793 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004794}
4795
Christopher Fauleta2097962019-07-15 16:25:33 +02004796/*
4797 *
4798 *
4799 * Class HTTP
4800 *
4801 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004802 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004803
4804/* Returns a struct hlua_txn if the stack entry "ud" is
4805 * a class stream, otherwise it throws an error.
4806 */
4807__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004808{
Christopher Fauleta2097962019-07-15 16:25:33 +02004809 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4810}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004811
Christopher Fauleta2097962019-07-15 16:25:33 +02004812/* This function creates and push in the stack a HTTP object
4813 * according with a current TXN.
4814 */
4815static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4816{
4817 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004818
Christopher Fauleta2097962019-07-15 16:25:33 +02004819 /* Check stack size. */
4820 if (!lua_checkstack(L, 3))
4821 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004822
Christopher Fauleta2097962019-07-15 16:25:33 +02004823 /* Create the object: obj[0] = userdata.
4824 * Note that the base of the Converters object is the
4825 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004826 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004827 lua_newtable(L);
4828 htxn = lua_newuserdata(L, sizeof(*htxn));
4829 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004830
4831 htxn->s = txn->s;
4832 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004833 htxn->dir = txn->dir;
4834 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004835
4836 /* Pop a class stream metatable and affect it to the table. */
4837 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4838 lua_setmetatable(L, -2);
4839
4840 return 1;
4841}
4842
4843/* This function creates ans returns an array of HTTP headers.
4844 * This function does not fails. It is used as wrapper with the
4845 * 2 following functions.
4846 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004847__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004848{
Christopher Fauleta2097962019-07-15 16:25:33 +02004849 struct htx *htx;
4850 int32_t pos;
4851
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004852 /* Create the table. */
4853 lua_newtable(L);
4854
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004855
Christopher Fauleta2097962019-07-15 16:25:33 +02004856 htx = htxbuf(&msg->chn->buf);
4857 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4858 struct htx_blk *blk = htx_get_blk(htx, pos);
4859 enum htx_blk_type type = htx_get_blk_type(blk);
4860 struct ist n, v;
4861 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004862
Christopher Fauleta2097962019-07-15 16:25:33 +02004863 if (type == HTX_BLK_HDR) {
4864 n = htx_get_blk_name(htx,blk);
4865 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004866 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004867 else if (type == HTX_BLK_EOH)
4868 break;
4869 else
4870 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004871
Christopher Fauleta2097962019-07-15 16:25:33 +02004872 /* Check for existing entry:
4873 * assume that the table is on the top of the stack, and
4874 * push the key in the stack, the function lua_gettable()
4875 * perform the lookup.
4876 */
4877 lua_pushlstring(L, n.ptr, n.len);
4878 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004879
Christopher Fauleta2097962019-07-15 16:25:33 +02004880 switch (lua_type(L, -1)) {
4881 case LUA_TNIL:
4882 /* Table not found, create it. */
4883 lua_pop(L, 1); /* remove the nil value. */
4884 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4885 lua_newtable(L); /* create and push empty table. */
4886 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4887 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4888 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004889 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004890
Christopher Fauleta2097962019-07-15 16:25:33 +02004891 case LUA_TTABLE:
4892 /* Entry found: push the value in the table. */
4893 len = lua_rawlen(L, -1);
4894 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4895 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4896 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4897 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004898
Christopher Fauleta2097962019-07-15 16:25:33 +02004899 default:
4900 /* Other cases are errors. */
4901 hlua_pusherror(L, "internal error during the parsing of headers.");
4902 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004903 }
4904 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004905 return 1;
4906}
4907
4908__LJMP static int hlua_http_req_get_headers(lua_State *L)
4909{
4910 struct hlua_txn *htxn;
4911
4912 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4913 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4914
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004915 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004916 WILL_LJMP(lua_error(L));
4917
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004918 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004919}
4920
4921__LJMP static int hlua_http_res_get_headers(lua_State *L)
4922{
4923 struct hlua_txn *htxn;
4924
4925 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4926 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4927
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004928 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004929 WILL_LJMP(lua_error(L));
4930
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004931 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004932}
4933
4934/* This function replace full header, or just a value in
4935 * the request or in the response. It is a wrapper fir the
4936 * 4 following functions.
4937 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004938__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004939{
4940 size_t name_len;
4941 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4942 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4943 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004944 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004945 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004946
Dragan Dosen26743032019-04-30 15:54:36 +02004947 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004948 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4949
Christopher Fauleta2097962019-07-15 16:25:33 +02004950 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004951 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004952 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004953 return 0;
4954}
4955
4956__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4957{
4958 struct hlua_txn *htxn;
4959
4960 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4961 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4962
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004963 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004964 WILL_LJMP(lua_error(L));
4965
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004966 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004967}
4968
4969__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4970{
4971 struct hlua_txn *htxn;
4972
4973 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4974 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4975
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004976 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004977 WILL_LJMP(lua_error(L));
4978
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004979 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004980}
4981
4982__LJMP static int hlua_http_req_rep_val(lua_State *L)
4983{
4984 struct hlua_txn *htxn;
4985
4986 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4987 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4988
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004989 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004990 WILL_LJMP(lua_error(L));
4991
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004992 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004993}
4994
4995__LJMP static int hlua_http_res_rep_val(lua_State *L)
4996{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004997 struct hlua_txn *htxn;
4998
4999 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5000 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5001
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005002 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005003 WILL_LJMP(lua_error(L));
5004
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005005 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005006}
5007
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005008/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005009 * It is a wrapper for the 2 following functions.
5010 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005011__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005012{
5013 size_t len;
5014 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005015 struct htx *htx = htxbuf(&msg->chn->buf);
5016 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005017
Christopher Fauleta2097962019-07-15 16:25:33 +02005018 ctx.blk = NULL;
5019 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5020 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005021 return 0;
5022}
5023
5024__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5025{
5026 struct hlua_txn *htxn;
5027
5028 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5029 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5030
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005031 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005032 WILL_LJMP(lua_error(L));
5033
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005034 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005035}
5036
5037__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5038{
5039 struct hlua_txn *htxn;
5040
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005041 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005042 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5043
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005044 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005045 WILL_LJMP(lua_error(L));
5046
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005047 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005048}
5049
5050/* This function adds an header. It is a wrapper used by
5051 * the 2 following functions.
5052 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005053__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005054{
5055 size_t name_len;
5056 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5057 size_t value_len;
5058 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005059 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005060
Christopher Fauleta2097962019-07-15 16:25:33 +02005061 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5062 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005063 return 0;
5064}
5065
5066__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5067{
5068 struct hlua_txn *htxn;
5069
5070 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5071 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5072
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005073 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005074 WILL_LJMP(lua_error(L));
5075
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005076 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005077}
5078
5079__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5080{
5081 struct hlua_txn *htxn;
5082
5083 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5084 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5085
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005086 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005087 WILL_LJMP(lua_error(L));
5088
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005089 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005090}
5091
5092static int hlua_http_req_set_hdr(lua_State *L)
5093{
5094 struct hlua_txn *htxn;
5095
5096 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5097 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5098
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005099 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005100 WILL_LJMP(lua_error(L));
5101
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005102 hlua_http_del_hdr(L, &htxn->s->txn->req);
5103 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005104}
5105
5106static int hlua_http_res_set_hdr(lua_State *L)
5107{
5108 struct hlua_txn *htxn;
5109
5110 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5111 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5112
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005113 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005114 WILL_LJMP(lua_error(L));
5115
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005116 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5117 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005118}
5119
5120/* This function set the method. */
5121static int hlua_http_req_set_meth(lua_State *L)
5122{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005123 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005124 size_t name_len;
5125 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005126
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005127 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005128 WILL_LJMP(lua_error(L));
5129
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005130 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005131 return 1;
5132}
5133
5134/* This function set the method. */
5135static int hlua_http_req_set_path(lua_State *L)
5136{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005137 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005138 size_t name_len;
5139 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005140
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005141 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005142 WILL_LJMP(lua_error(L));
5143
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005144 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005145 return 1;
5146}
5147
5148/* This function set the query-string. */
5149static int hlua_http_req_set_query(lua_State *L)
5150{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005151 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005152 size_t name_len;
5153 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005154
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005155 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005156 WILL_LJMP(lua_error(L));
5157
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005158 /* Check length. */
5159 if (name_len > trash.size - 1) {
5160 lua_pushboolean(L, 0);
5161 return 1;
5162 }
5163
5164 /* Add the mark question as prefix. */
5165 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005166 trash.area[trash.data++] = '?';
5167 memcpy(trash.area + trash.data, name, name_len);
5168 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005169
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005170 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005171 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005172 return 1;
5173}
5174
5175/* This function set the uri. */
5176static int hlua_http_req_set_uri(lua_State *L)
5177{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005178 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005179 size_t name_len;
5180 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005181
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005182 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005183 WILL_LJMP(lua_error(L));
5184
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005185 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005186 return 1;
5187}
5188
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005189/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005190static int hlua_http_res_set_status(lua_State *L)
5191{
5192 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5193 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005194 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5195 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005196
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005197 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005198 WILL_LJMP(lua_error(L));
5199
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005200 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005201 return 0;
5202}
5203
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005204/*
5205 *
5206 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005207 * Class TXN
5208 *
5209 *
5210 */
5211
5212/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005213 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005214 */
5215__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5216{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005217 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005218}
5219
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005220__LJMP static int hlua_set_var(lua_State *L)
5221{
5222 struct hlua_txn *htxn;
5223 const char *name;
5224 size_t len;
5225 struct sample smp;
5226
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005227 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5228 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005229
5230 /* It is useles to retrieve the stream, but this function
5231 * runs only in a stream context.
5232 */
5233 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5234 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5235
5236 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005237 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005238 hlua_lua2smp(L, 3, &smp);
5239
5240 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005241 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005242
5243 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5244 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5245 else
5246 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5247
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005248 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005249}
5250
Christopher Faulet85d79c92016-11-09 16:54:56 +01005251__LJMP static int hlua_unset_var(lua_State *L)
5252{
5253 struct hlua_txn *htxn;
5254 const char *name;
5255 size_t len;
5256 struct sample smp;
5257
5258 MAY_LJMP(check_args(L, 2, "unset_var"));
5259
5260 /* It is useles to retrieve the stream, but this function
5261 * runs only in a stream context.
5262 */
5263 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5264 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5265
5266 /* Unset the variable. */
5267 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005268 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5269 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005270}
5271
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005272__LJMP static int hlua_get_var(lua_State *L)
5273{
5274 struct hlua_txn *htxn;
5275 const char *name;
5276 size_t len;
5277 struct sample smp;
5278
5279 MAY_LJMP(check_args(L, 2, "get_var"));
5280
5281 /* It is useles to retrieve the stream, but this function
5282 * runs only in a stream context.
5283 */
5284 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5285 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5286
Willy Tarreau7560dd42016-03-10 16:28:58 +01005287 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005288 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005289 lua_pushnil(L);
5290 return 1;
5291 }
5292
5293 return hlua_smp2lua(L, &smp);
5294}
5295
Willy Tarreau59551662015-03-10 14:23:13 +01005296__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005297{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005298 struct hlua *hlua;
5299
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005300 MAY_LJMP(check_args(L, 2, "set_priv"));
5301
Willy Tarreau87b09662015-04-03 00:22:06 +02005302 /* It is useles to retrieve the stream, but this function
5303 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005304 */
5305 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005306 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005307
5308 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005309 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005310
5311 /* Get and store new value. */
5312 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5313 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5314
5315 return 0;
5316}
5317
Willy Tarreau59551662015-03-10 14:23:13 +01005318__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005319{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005320 struct hlua *hlua;
5321
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005322 MAY_LJMP(check_args(L, 1, "get_priv"));
5323
Willy Tarreau87b09662015-04-03 00:22:06 +02005324 /* It is useles to retrieve the stream, but this function
5325 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005326 */
5327 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005328 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005329
5330 /* Push configuration index in the stack. */
5331 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5332
5333 return 1;
5334}
5335
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005336/* Create stack entry containing a class TXN. This function
5337 * return 0 if the stack does not contains free slots,
5338 * otherwise it returns 1.
5339 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005340static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005341{
Willy Tarreaude491382015-04-06 11:04:28 +02005342 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005343
5344 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005345 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005346 return 0;
5347
5348 /* NOTE: The allocation never fails. The failure
5349 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005350 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005351 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005352 /* Create the object: obj[0] = userdata. */
5353 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005354 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005355 lua_rawseti(L, -2, 0);
5356
Willy Tarreaude491382015-04-06 11:04:28 +02005357 htxn->s = s;
5358 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005359 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005360 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005361
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005362 /* Create the "f" field that contains a list of fetches. */
5363 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005364 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005365 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005366 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005367
5368 /* Create the "sf" field that contains a list of stringsafe fetches. */
5369 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005370 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005371 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005372 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005373
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005374 /* Create the "c" field that contains a list of converters. */
5375 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005376 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005377 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005378 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005379
5380 /* Create the "sc" field that contains a list of stringsafe converters. */
5381 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005382 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005383 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005384 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005385
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005386 /* Create the "req" field that contains the request channel object. */
5387 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005388 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005389 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005390 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005391
5392 /* Create the "res" field that contains the response channel object. */
5393 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005394 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005395 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005396 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005397
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005398 /* Creates the HTTP object is the current proxy allows http. */
5399 lua_pushstring(L, "http");
5400 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005401 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005402 return 0;
5403 }
5404 else
5405 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005406 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005407
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005408 /* Pop a class sesison metatable and affect it to the userdata. */
5409 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5410 lua_setmetatable(L, -2);
5411
5412 return 1;
5413}
5414
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005415__LJMP static int hlua_txn_deflog(lua_State *L)
5416{
5417 const char *msg;
5418 struct hlua_txn *htxn;
5419
5420 MAY_LJMP(check_args(L, 2, "deflog"));
5421 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5422 msg = MAY_LJMP(luaL_checkstring(L, 2));
5423
5424 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5425 return 0;
5426}
5427
5428__LJMP static int hlua_txn_log(lua_State *L)
5429{
5430 int level;
5431 const char *msg;
5432 struct hlua_txn *htxn;
5433
5434 MAY_LJMP(check_args(L, 3, "log"));
5435 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5436 level = MAY_LJMP(luaL_checkinteger(L, 2));
5437 msg = MAY_LJMP(luaL_checkstring(L, 3));
5438
5439 if (level < 0 || level >= NB_LOG_LEVELS)
5440 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5441
5442 hlua_sendlog(htxn->s->be, level, msg);
5443 return 0;
5444}
5445
5446__LJMP static int hlua_txn_log_debug(lua_State *L)
5447{
5448 const char *msg;
5449 struct hlua_txn *htxn;
5450
5451 MAY_LJMP(check_args(L, 2, "Debug"));
5452 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5453 msg = MAY_LJMP(luaL_checkstring(L, 2));
5454 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5455 return 0;
5456}
5457
5458__LJMP static int hlua_txn_log_info(lua_State *L)
5459{
5460 const char *msg;
5461 struct hlua_txn *htxn;
5462
5463 MAY_LJMP(check_args(L, 2, "Info"));
5464 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5465 msg = MAY_LJMP(luaL_checkstring(L, 2));
5466 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5467 return 0;
5468}
5469
5470__LJMP static int hlua_txn_log_warning(lua_State *L)
5471{
5472 const char *msg;
5473 struct hlua_txn *htxn;
5474
5475 MAY_LJMP(check_args(L, 2, "Warning"));
5476 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5477 msg = MAY_LJMP(luaL_checkstring(L, 2));
5478 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5479 return 0;
5480}
5481
5482__LJMP static int hlua_txn_log_alert(lua_State *L)
5483{
5484 const char *msg;
5485 struct hlua_txn *htxn;
5486
5487 MAY_LJMP(check_args(L, 2, "Alert"));
5488 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5489 msg = MAY_LJMP(luaL_checkstring(L, 2));
5490 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5491 return 0;
5492}
5493
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005494__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5495{
5496 struct hlua_txn *htxn;
5497 int ll;
5498
5499 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5500 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5501 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5502
5503 if (ll < 0 || ll > 7)
5504 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5505
5506 htxn->s->logs.level = ll;
5507 return 0;
5508}
5509
5510__LJMP static int hlua_txn_set_tos(lua_State *L)
5511{
5512 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005513 int tos;
5514
5515 MAY_LJMP(check_args(L, 2, "set_tos"));
5516 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5517 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5518
Willy Tarreau1a18b542018-12-11 16:37:42 +01005519 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005520 return 0;
5521}
5522
5523__LJMP static int hlua_txn_set_mark(lua_State *L)
5524{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005525 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005526 int mark;
5527
5528 MAY_LJMP(check_args(L, 2, "set_mark"));
5529 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5530 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5531
Lukas Tribus579e3e32019-08-11 18:03:45 +02005532 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005533 return 0;
5534}
5535
Patrick Hemmer268a7072018-05-11 12:52:31 -04005536__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5537{
5538 struct hlua_txn *htxn;
5539
5540 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5541 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5542 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5543 return 0;
5544}
5545
5546__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5547{
5548 struct hlua_txn *htxn;
5549
5550 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5551 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5552 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5553 return 0;
5554}
5555
Christopher Faulet700d9e82020-01-31 12:21:52 +01005556/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005557 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005558 * message and terminate the transaction. It returns 1 on success and 0 on
5559 * error. The Reply must be on top of the stack.
5560 */
5561__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5562{
5563 struct htx *htx;
5564 struct htx_sl *sl;
5565 struct h1m h1m;
5566 const char *status, *reason, *body;
5567 size_t status_len, reason_len, body_len;
5568 int ret, code, flags;
5569
5570 code = 200;
5571 status = "200";
5572 status_len = 3;
5573 ret = lua_getfield(L, -1, "status");
5574 if (ret == LUA_TNUMBER) {
5575 code = lua_tointeger(L, -1);
5576 status = lua_tolstring(L, -1, &status_len);
5577 }
5578 lua_pop(L, 1);
5579
5580 reason = http_get_reason(code);
5581 reason_len = strlen(reason);
5582 ret = lua_getfield(L, -1, "reason");
5583 if (ret == LUA_TSTRING)
5584 reason = lua_tolstring(L, -1, &reason_len);
5585 lua_pop(L, 1);
5586
5587 body = NULL;
5588 body_len = 0;
5589 ret = lua_getfield(L, -1, "body");
5590 if (ret == LUA_TSTRING)
5591 body = lua_tolstring(L, -1, &body_len);
5592 lua_pop(L, 1);
5593
5594 /* Prepare the response before inserting the headers */
5595 h1m_init_res(&h1m);
5596 htx = htx_from_buf(&s->res.buf);
5597 channel_htx_truncate(&s->res, htx);
5598 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5599 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5600 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5601 ist2(status, status_len), ist2(reason, reason_len));
5602 }
5603 else {
5604 flags = HTX_SL_F_IS_RESP;
5605 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5606 ist2(status, status_len), ist2(reason, reason_len));
5607 }
5608 if (!sl)
5609 goto fail;
5610 sl->info.res.status = code;
5611
5612 /* Push in the stack the "headers" entry. */
5613 ret = lua_getfield(L, -1, "headers");
5614 if (ret != LUA_TTABLE)
5615 goto skip_headers;
5616
5617 lua_pushnil(L);
5618 while (lua_next(L, -2) != 0) {
5619 struct ist name, value;
5620 const char *n, *v;
5621 size_t nlen, vlen;
5622
5623 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5624 /* Skip element if the key is not a string or if the value is not a table */
5625 goto next_hdr;
5626 }
5627
5628 n = lua_tolstring(L, -2, &nlen);
5629 name = ist2(n, nlen);
5630 if (isteqi(name, ist("content-length"))) {
5631 /* Always skip content-length header. It will be added
5632 * later with the correct len
5633 */
5634 goto next_hdr;
5635 }
5636
5637 /* Loop on header's values */
5638 lua_pushnil(L);
5639 while (lua_next(L, -2)) {
5640 if (!lua_isstring(L, -1)) {
5641 /* Skip the value if it is not a string */
5642 goto next_value;
5643 }
5644
5645 v = lua_tolstring(L, -1, &vlen);
5646 value = ist2(v, vlen);
5647
5648 if (isteqi(name, ist("transfer-encoding")))
5649 h1_parse_xfer_enc_header(&h1m, value);
5650 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5651 goto fail;
5652
5653 next_value:
5654 lua_pop(L, 1);
5655 }
5656
5657 next_hdr:
5658 lua_pop(L, 1);
5659 }
5660 skip_headers:
5661 lua_pop(L, 1);
5662
5663 /* Update h1m flags: CLEN is set if CHNK is not present */
5664 if (!(h1m.flags & H1_MF_CHNK)) {
5665 const char *clen = ultoa(body_len);
5666
5667 h1m.flags |= H1_MF_CLEN;
5668 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5669 goto fail;
5670 }
5671 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5672 h1m.flags |= H1_MF_XFER_LEN;
5673
5674 /* Update HTX start-line flags */
5675 if (h1m.flags & H1_MF_XFER_ENC)
5676 flags |= HTX_SL_F_XFER_ENC;
5677 if (h1m.flags & H1_MF_XFER_LEN) {
5678 flags |= HTX_SL_F_XFER_LEN;
5679 if (h1m.flags & H1_MF_CHNK)
5680 flags |= HTX_SL_F_CHNK;
5681 else if (h1m.flags & H1_MF_CLEN)
5682 flags |= HTX_SL_F_CLEN;
5683 if (h1m.body_len == 0)
5684 flags |= HTX_SL_F_BODYLESS;
5685 }
5686 sl->flags |= flags;
5687
5688
5689 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5690 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5691 !htx_add_endof(htx, HTX_BLK_EOM))
5692 goto fail;
5693
5694 /* Now, forward the response and terminate the transaction */
5695 s->txn->status = code;
5696 htx_to_buf(htx, &s->res.buf);
5697 if (!http_forward_proxy_resp(s, 1))
5698 goto fail;
5699
5700 return 1;
5701
5702 fail:
5703 channel_htx_truncate(&s->res, htx);
5704 return 0;
5705}
5706
5707/* Terminate a transaction if called from a lua action. For TCP streams,
5708 * processing is just aborted. Nothing is returned to the client and all
5709 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5710 * is forwarded to the client before terminating the transaction. On success,
5711 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5712 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5713 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005714 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005715__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005716{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005717 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005718 struct stream *s;
5719 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005720
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005721 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005722
Christopher Faulet700d9e82020-01-31 12:21:52 +01005723 /* If the flags NOTERM is set, we cannot terminate the session, so we
5724 * just end the execution of the current lua code. */
5725 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005726 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005727
Christopher Faulet700d9e82020-01-31 12:21:52 +01005728 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005729 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005730 struct channel *req = &s->req;
5731 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005732
Christopher Faulet700d9e82020-01-31 12:21:52 +01005733 channel_auto_read(req);
5734 channel_abort(req);
5735 channel_auto_close(req);
5736 channel_erase(req);
5737
5738 res->wex = tick_add_ifset(now_ms, res->wto);
5739 channel_auto_read(res);
5740 channel_auto_close(res);
5741 channel_shutr_now(res);
5742
5743 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5744 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005745 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005746
Christopher Faulet700d9e82020-01-31 12:21:52 +01005747 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5748 /* No reply or invalid reply */
5749 s->txn->status = 0;
5750 http_reply_and_close(s, 0, NULL);
5751 }
5752 else {
5753 /* Remove extra args to have the reply on top of the stack */
5754 if (lua_gettop(L) > 2)
5755 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005756
Christopher Faulet700d9e82020-01-31 12:21:52 +01005757 if (!hlua_txn_forward_reply(L, s)) {
5758 if (!(s->flags & SF_ERR_MASK))
5759 s->flags |= SF_ERR_PRXCOND;
5760 lua_pushinteger(L, ACT_RET_ERR);
5761 WILL_LJMP(hlua_done(L));
5762 return 0; /* Never reached */
5763 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005764 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005765
Christopher Faulet700d9e82020-01-31 12:21:52 +01005766 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5767 if (htxn->dir == SMP_OPT_DIR_REQ) {
5768 /* let's log the request time */
5769 s->logs.tv_request = now;
5770 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5771 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5772 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005773
Christopher Faulet700d9e82020-01-31 12:21:52 +01005774 done:
5775 if (!(s->flags & SF_ERR_MASK))
5776 s->flags |= SF_ERR_LOCAL;
5777 if (!(s->flags & SF_FINST_MASK))
5778 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005779
Christopher Faulet4ad73102020-03-05 11:07:31 +01005780 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005781 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005782 return 0;
5783}
5784
Christopher Faulet700d9e82020-01-31 12:21:52 +01005785/*
5786 *
5787 *
5788 * Class REPLY
5789 *
5790 *
5791 */
5792
5793/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5794 * free slots, the function fails and returns 0;
5795 */
5796static int hlua_txn_reply_new(lua_State *L)
5797{
5798 struct hlua_txn *htxn;
5799 const char *reason, *body = NULL;
5800 int ret, status;
5801
5802 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005803 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005804 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5805 WILL_LJMP(lua_error(L));
5806 }
5807
5808 /* Default value */
5809 status = 200;
5810 reason = http_get_reason(status);
5811
5812 if (lua_istable(L, 2)) {
5813 /* load status and reason from the table argument at index 2 */
5814 ret = lua_getfield(L, 2, "status");
5815 if (ret == LUA_TNIL)
5816 goto reason;
5817 else if (ret != LUA_TNUMBER) {
5818 /* invalid status: ignore the reason */
5819 goto body;
5820 }
5821 status = lua_tointeger(L, -1);
5822
5823 reason:
5824 lua_pop(L, 1); /* restore the stack: remove status */
5825 ret = lua_getfield(L, 2, "reason");
5826 if (ret == LUA_TSTRING)
5827 reason = lua_tostring(L, -1);
5828
5829 body:
5830 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5831 ret = lua_getfield(L, 2, "body");
5832 if (ret == LUA_TSTRING)
5833 body = lua_tostring(L, -1);
5834 lua_pop(L, 1); /* restore the stack: remove body */
5835 }
5836
5837 /* Create the Reply table */
5838 lua_newtable(L);
5839
5840 /* Add status element */
5841 lua_pushstring(L, "status");
5842 lua_pushinteger(L, status);
5843 lua_settable(L, -3);
5844
5845 /* Add reason element */
5846 reason = http_get_reason(status);
5847 lua_pushstring(L, "reason");
5848 lua_pushstring(L, reason);
5849 lua_settable(L, -3);
5850
5851 /* Add body element, nil if undefined */
5852 lua_pushstring(L, "body");
5853 if (body)
5854 lua_pushstring(L, body);
5855 else
5856 lua_pushnil(L);
5857 lua_settable(L, -3);
5858
5859 /* Add headers element */
5860 lua_pushstring(L, "headers");
5861 lua_newtable(L);
5862
5863 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5864 if (lua_istable(L, 2)) {
5865 /* load headers from the table argument at index 2. If it is a table, copy it. */
5866 ret = lua_getfield(L, 2, "headers");
5867 if (ret == LUA_TTABLE) {
5868 /* stack: [ ... <headers:table>, <table> ] */
5869 lua_pushnil(L);
5870 while (lua_next(L, -2) != 0) {
5871 /* stack: [ ... <headers:table>, <table>, k, v] */
5872 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5873 /* invalid value type, skip it */
5874 lua_pop(L, 1);
5875 continue;
5876 }
5877
5878
5879 /* Duplicate the key and swap it with the value. */
5880 lua_pushvalue(L, -2);
5881 lua_insert(L, -2);
5882 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5883
5884 lua_newtable(L);
5885 lua_insert(L, -2);
5886 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5887
5888 if (lua_isstring(L, -1)) {
5889 /* push the value in the inner table */
5890 lua_rawseti(L, -2, 1);
5891 }
5892 else { /* table */
5893 lua_pushnil(L);
5894 while (lua_next(L, -2) != 0) {
5895 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5896 if (!lua_isstring(L, -1)) {
5897 /* invalid value type, skip it*/
5898 lua_pop(L, 1);
5899 continue;
5900 }
5901 /* push the value in the inner table */
5902 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5903 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5904 }
5905 lua_pop(L, 1);
5906 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5907 }
5908
5909 /* push (k,v) on the stack in the headers table:
5910 * stack: [ ... <headers:table>, <table>, k, k, v ]
5911 */
5912 lua_settable(L, -5);
5913 /* stack: [ ... <headers:table>, <table>, k ] */
5914 }
5915 }
5916 lua_pop(L, 1);
5917 }
5918 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5919 lua_settable(L, -3);
5920 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5921
5922 /* Pop a class sesison metatable and affect it to the userdata. */
5923 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5924 lua_setmetatable(L, -2);
5925 return 1;
5926}
5927
5928/* Set the reply status code, and optionally the reason. If no reason is
5929 * provided, the default one corresponding to the status code is used.
5930 */
5931__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5932{
5933 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5934 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5935
5936 /* First argument (self) must be a table */
5937 luaL_checktype(L, 1, LUA_TTABLE);
5938
5939 if (status < 100 || status > 599) {
5940 lua_pushboolean(L, 0);
5941 return 1;
5942 }
5943 if (!reason)
5944 reason = http_get_reason(status);
5945
5946 lua_pushinteger(L, status);
5947 lua_setfield(L, 1, "status");
5948
5949 lua_pushstring(L, reason);
5950 lua_setfield(L, 1, "reason");
5951
5952 lua_pushboolean(L, 1);
5953 return 1;
5954}
5955
5956/* Add a header into the reply object. Each header name is associated to an
5957 * array of values in the "headers" table. If the header name is not found, a
5958 * new entry is created.
5959 */
5960__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5961{
5962 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5963 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5964 int ret;
5965
5966 /* First argument (self) must be a table */
5967 luaL_checktype(L, 1, LUA_TTABLE);
5968
5969 /* Push in the stack the "headers" entry. */
5970 ret = lua_getfield(L, 1, "headers");
5971 if (ret != LUA_TTABLE) {
5972 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5973 WILL_LJMP(lua_error(L));
5974 }
5975
5976 /* check if the header is already registered. If not, register it. */
5977 ret = lua_getfield(L, -1, name);
5978 if (ret == LUA_TNIL) {
5979 /* Entry not found. */
5980 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5981
5982 /* Insert the new header name in the array in the top of the stack.
5983 * It left the new array in the top of the stack.
5984 */
5985 lua_newtable(L);
5986 lua_pushstring(L, name);
5987 lua_pushvalue(L, -2);
5988 lua_settable(L, -4);
5989 }
5990 else if (ret != LUA_TTABLE) {
5991 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5992 WILL_LJMP(lua_error(L));
5993 }
5994
5995 /* Now the top od thestack is an array of values. We push
5996 * the header value as new entry.
5997 */
5998 lua_pushstring(L, value);
5999 ret = lua_rawlen(L, -2);
6000 lua_rawseti(L, -2, ret + 1);
6001
6002 lua_pushboolean(L, 1);
6003 return 1;
6004}
6005
6006/* Remove all occurrences of a given header name. */
6007__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6008{
6009 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6010 int ret;
6011
6012 /* First argument (self) must be a table */
6013 luaL_checktype(L, 1, LUA_TTABLE);
6014
6015 /* Push in the stack the "headers" entry. */
6016 ret = lua_getfield(L, 1, "headers");
6017 if (ret != LUA_TTABLE) {
6018 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6019 WILL_LJMP(lua_error(L));
6020 }
6021
6022 lua_pushstring(L, name);
6023 lua_pushnil(L);
6024 lua_settable(L, -3);
6025
6026 lua_pushboolean(L, 1);
6027 return 1;
6028}
6029
6030/* Set the reply's body. Overwrite any existing entry. */
6031__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6032{
6033 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6034
6035 /* First argument (self) must be a table */
6036 luaL_checktype(L, 1, LUA_TTABLE);
6037
6038 lua_pushstring(L, payload);
6039 lua_setfield(L, 1, "body");
6040
6041 lua_pushboolean(L, 1);
6042 return 1;
6043}
6044
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006045__LJMP static int hlua_log(lua_State *L)
6046{
6047 int level;
6048 const char *msg;
6049
6050 MAY_LJMP(check_args(L, 2, "log"));
6051 level = MAY_LJMP(luaL_checkinteger(L, 1));
6052 msg = MAY_LJMP(luaL_checkstring(L, 2));
6053
6054 if (level < 0 || level >= NB_LOG_LEVELS)
6055 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6056
6057 hlua_sendlog(NULL, level, msg);
6058 return 0;
6059}
6060
6061__LJMP static int hlua_log_debug(lua_State *L)
6062{
6063 const char *msg;
6064
6065 MAY_LJMP(check_args(L, 1, "debug"));
6066 msg = MAY_LJMP(luaL_checkstring(L, 1));
6067 hlua_sendlog(NULL, LOG_DEBUG, msg);
6068 return 0;
6069}
6070
6071__LJMP static int hlua_log_info(lua_State *L)
6072{
6073 const char *msg;
6074
6075 MAY_LJMP(check_args(L, 1, "info"));
6076 msg = MAY_LJMP(luaL_checkstring(L, 1));
6077 hlua_sendlog(NULL, LOG_INFO, msg);
6078 return 0;
6079}
6080
6081__LJMP static int hlua_log_warning(lua_State *L)
6082{
6083 const char *msg;
6084
6085 MAY_LJMP(check_args(L, 1, "warning"));
6086 msg = MAY_LJMP(luaL_checkstring(L, 1));
6087 hlua_sendlog(NULL, LOG_WARNING, msg);
6088 return 0;
6089}
6090
6091__LJMP static int hlua_log_alert(lua_State *L)
6092{
6093 const char *msg;
6094
6095 MAY_LJMP(check_args(L, 1, "alert"));
6096 msg = MAY_LJMP(luaL_checkstring(L, 1));
6097 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006098 return 0;
6099}
6100
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006101__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006102{
6103 int wakeup_ms = lua_tointeger(L, -1);
6104 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006105 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006106 return 0;
6107}
6108
6109__LJMP static int hlua_sleep(lua_State *L)
6110{
6111 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006112 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006113
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006114 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006115
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006116 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006117 wakeup_ms = tick_add(now_ms, delay);
6118 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006119
Willy Tarreau9635e032018-10-16 17:52:55 +02006120 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006121 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006122}
6123
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006124__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006125{
6126 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006127 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006128
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006129 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006130
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006131 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006132 wakeup_ms = tick_add(now_ms, delay);
6133 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006134
Willy Tarreau9635e032018-10-16 17:52:55 +02006135 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006136 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006137}
6138
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006139/* This functionis an LUA binding. it permits to give back
6140 * the hand at the HAProxy scheduler. It is used when the
6141 * LUA processing consumes a lot of time.
6142 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006143__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006144{
6145 return 0;
6146}
6147
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006148__LJMP static int hlua_yield(lua_State *L)
6149{
Willy Tarreau9635e032018-10-16 17:52:55 +02006150 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006151 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006152}
6153
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006154/* This function change the nice of the currently executed
6155 * task. It is used set low or high priority at the current
6156 * task.
6157 */
Willy Tarreau59551662015-03-10 14:23:13 +01006158__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006159{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006160 struct hlua *hlua;
6161 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006162
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006163 MAY_LJMP(check_args(L, 1, "set_nice"));
6164 hlua = hlua_gethlua(L);
6165 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006166
6167 /* If he task is not set, I'm in a start mode. */
6168 if (!hlua || !hlua->task)
6169 return 0;
6170
6171 if (nice < -1024)
6172 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006173 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006174 nice = 1024;
6175
6176 hlua->task->nice = nice;
6177 return 0;
6178}
6179
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006180/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006181 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6182 * return an E_AGAIN signal, the emmiter of this signal must set a
6183 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006184 *
6185 * Task wrapper are longjmp safe because the only one Lua code
6186 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006187 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006188struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006189{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006190 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006191 enum hlua_exec status;
6192
Christopher Faulet5bc99722018-04-25 10:34:45 +02006193 if (task->thread_mask == MAX_THREADS_MASK)
6194 task_set_affinity(task, tid_bit);
6195
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006196 /* If it is the first call to the task, we must initialize the
6197 * execution timeouts.
6198 */
6199 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006200 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006201
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006202 /* Execute the Lua code. */
6203 status = hlua_ctx_resume(hlua, 1);
6204
6205 switch (status) {
6206 /* finished or yield */
6207 case HLUA_E_OK:
6208 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006209 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006210 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006211 break;
6212
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006213 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006214 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006215 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006216 break;
6217
6218 /* finished with error. */
6219 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006220 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006221 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006222 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006223 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006224 break;
6225
6226 case HLUA_E_ERR:
6227 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006228 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006229 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006230 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006231 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006232 break;
6233 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006234 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006235}
6236
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006237/* This function is an LUA binding that register LUA function to be
6238 * executed after the HAProxy configuration parsing and before the
6239 * HAProxy scheduler starts. This function expect only one LUA
6240 * argument that is a function. This function returns nothing, but
6241 * throws if an error is encountered.
6242 */
6243__LJMP static int hlua_register_init(lua_State *L)
6244{
6245 struct hlua_init_function *init;
6246 int ref;
6247
6248 MAY_LJMP(check_args(L, 1, "register_init"));
6249
6250 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6251
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006252 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006253 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006254 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006255
6256 init->function_ref = ref;
6257 LIST_ADDQ(&hlua_init_functions, &init->l);
6258 return 0;
6259}
6260
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006261/* This functio is an LUA binding. It permits to register a task
6262 * executed in parallel of the main HAroxy activity. The task is
6263 * created and it is set in the HAProxy scheduler. It can be called
6264 * from the "init" section, "post init" or during the runtime.
6265 *
6266 * Lua prototype:
6267 *
6268 * <none> core.register_task(<function>)
6269 */
6270static int hlua_register_task(lua_State *L)
6271{
6272 struct hlua *hlua;
6273 struct task *task;
6274 int ref;
6275
6276 MAY_LJMP(check_args(L, 1, "register_task"));
6277
6278 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6279
Willy Tarreaubafbe012017-11-24 17:34:44 +01006280 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006281 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006282 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Christopher Fauletf2e73622021-03-24 15:03:01 +01006283 HLUA_INIT(hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006284
Emeric Brunc60def82017-09-27 14:59:38 +02006285 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006286 if (!task)
6287 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6288
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006289 task->context = hlua;
6290 task->process = hlua_process_task;
6291
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006292 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006293 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006294
6295 /* Restore the function in the stack. */
6296 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6297 hlua->nargs = 0;
6298
6299 /* Schedule task. */
6300 task_schedule(task, now_ms);
6301
6302 return 0;
6303}
6304
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006305/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6306 * doesn't allow "yield" functions because the HAProxy engine cannot
6307 * resume converters.
6308 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006309static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006310{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006311 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006312 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006313 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006314
Willy Tarreaube508f12016-03-10 11:47:01 +01006315 if (!stream)
6316 return 0;
6317
Willy Tarreau87b09662015-04-03 00:22:06 +02006318 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006319 * Lua context can be not initialized. This behavior
6320 * permits to save performances because a systematic
6321 * Lua initialization cause 5% performances loss.
6322 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006323 if (!stream->hlua) {
Christopher Fauletf2e73622021-03-24 15:03:01 +01006324 struct hlua *hlua;
6325
6326 hlua = pool_alloc(pool_head_hlua);
6327 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006328 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6329 return 0;
6330 }
Christopher Fauletf2e73622021-03-24 15:03:01 +01006331 HLUA_INIT(hlua);
6332 stream->hlua = hlua;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006333 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006334 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6335 return 0;
6336 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006337 }
6338
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006339 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006340 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006341
6342 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006343 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6344 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6345 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006346 else
6347 error = "critical error";
6348 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006349 return 0;
6350 }
6351
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006352 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006353 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006354 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006355 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006356 return 0;
6357 }
6358
6359 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006360 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006361
6362 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006363 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006364 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006365 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006366 return 0;
6367 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006368 hlua_smp2lua(stream->hlua->T, smp);
6369 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006370
6371 /* push keywords in the stack. */
6372 if (arg_p) {
6373 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006374 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006375 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006376 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006377 return 0;
6378 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006379 hlua_arg2lua(stream->hlua->T, arg_p);
6380 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006381 }
6382 }
6383
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006384 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006385 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006386
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006387 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006388 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006389 }
6390
6391 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006392 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006393 /* finished. */
6394 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006395 /* If the stack is empty, the function fails. */
6396 if (lua_gettop(stream->hlua->T) <= 0)
6397 return 0;
6398
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006399 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006400 hlua_lua2smp(stream->hlua->T, -1, smp);
6401 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006402 return 1;
6403
6404 /* yield. */
6405 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006406 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006407 return 0;
6408
6409 /* finished with error. */
6410 case HLUA_E_ERRMSG:
6411 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006412 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006413 fcn->name, lua_tostring(stream->hlua->T, -1));
6414 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006415 return 0;
6416
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006417 case HLUA_E_ETMOUT:
6418 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6419 return 0;
6420
6421 case HLUA_E_NOMEM:
6422 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6423 return 0;
6424
6425 case HLUA_E_YIELD:
6426 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6427 return 0;
6428
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006429 case HLUA_E_ERR:
6430 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006431 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006432 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006433
6434 default:
6435 return 0;
6436 }
6437}
6438
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006439/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6440 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006441 * resume sample-fetches. This function will be called by the sample
6442 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006443 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006444static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6445 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006446{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006447 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006448 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006449 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006450 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006451
Willy Tarreaube508f12016-03-10 11:47:01 +01006452 if (!stream)
6453 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006454
Willy Tarreau87b09662015-04-03 00:22:06 +02006455 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006456 * Lua context can be not initialized. This behavior
6457 * permits to save performances because a systematic
6458 * Lua initialization cause 5% performances loss.
6459 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006460 if (!stream->hlua) {
Christopher Fauletf2e73622021-03-24 15:03:01 +01006461 struct hlua *hlua;
6462
6463 hlua = pool_alloc(pool_head_hlua);
6464 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006465 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6466 return 0;
6467 }
Christopher Fauletf2e73622021-03-24 15:03:01 +01006468 hlua->T = NULL;
6469 stream->hlua = hlua;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006470 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006471 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6472 return 0;
6473 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006474 }
6475
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006476 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006477 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006478
6479 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006480 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6481 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6482 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006483 else
6484 error = "critical error";
6485 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006486 return 0;
6487 }
6488
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006489 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006490 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006491 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006492 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006493 return 0;
6494 }
6495
6496 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006497 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006498
6499 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006500 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006501 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006502 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006503 return 0;
6504 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006505 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006506
6507 /* push keywords in the stack. */
6508 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6509 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006510 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006511 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006512 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006513 return 0;
6514 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006515 hlua_arg2lua(stream->hlua->T, arg_p);
6516 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006517 }
6518
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006519 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006520 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006521
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006522 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006523 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006524 }
6525
6526 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006527 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006528 /* finished. */
6529 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006530 /* If the stack is empty, the function fails. */
6531 if (lua_gettop(stream->hlua->T) <= 0)
6532 return 0;
6533
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006534 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006535 hlua_lua2smp(stream->hlua->T, -1, smp);
6536 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006537
6538 /* Set the end of execution flag. */
6539 smp->flags &= ~SMP_F_MAY_CHANGE;
6540 return 1;
6541
6542 /* yield. */
6543 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006544 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006545 return 0;
6546
6547 /* finished with error. */
6548 case HLUA_E_ERRMSG:
6549 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006550 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006551 fcn->name, lua_tostring(stream->hlua->T, -1));
6552 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006553 return 0;
6554
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006555 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006556 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6557 return 0;
6558
6559 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006560 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6561 return 0;
6562
6563 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006564 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6565 return 0;
6566
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006567 case HLUA_E_ERR:
6568 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006569 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006570 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006571
6572 default:
6573 return 0;
6574 }
6575}
6576
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006577/* This function is an LUA binding used for registering
6578 * "sample-conv" functions. It expects a converter name used
6579 * in the haproxy configuration file, and an LUA function.
6580 */
6581__LJMP static int hlua_register_converters(lua_State *L)
6582{
6583 struct sample_conv_kw_list *sck;
6584 const char *name;
6585 int ref;
6586 int len;
6587 struct hlua_function *fcn;
Thierry Fournier04cbfd72020-11-28 20:41:07 +01006588 struct sample_conv *sc;
6589 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006590
6591 MAY_LJMP(check_args(L, 2, "register_converters"));
6592
6593 /* First argument : converter name. */
6594 name = MAY_LJMP(luaL_checkstring(L, 1));
6595
6596 /* Second argument : lua function. */
6597 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6598
Thierry Fournier04cbfd72020-11-28 20:41:07 +01006599 /* Check if the converter is already registered */
6600 trash = get_trash_chunk();
6601 chunk_printf(trash, "lua.%s", name);
6602 sc = find_sample_conv(trash->area, trash->data);
6603 if (sc != NULL) {
6604 ha_warning("Trying to register converter 'lua.%s' more than once. "
6605 "This will become a hard error in version 2.5.\n", name);
6606 }
6607
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006608 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006609 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006610 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006611 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006612 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006613 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006614 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006615
6616 /* Fill fcn. */
6617 fcn->name = strdup(name);
6618 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006619 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006620 fcn->function_ref = ref;
6621
6622 /* List head */
6623 sck->list.n = sck->list.p = NULL;
6624
6625 /* converter keyword. */
6626 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006627 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006628 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006629 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006630
6631 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6632 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006633 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 +01006634 sck->kw[0].val_args = NULL;
6635 sck->kw[0].in_type = SMP_T_STR;
6636 sck->kw[0].out_type = SMP_T_STR;
6637 sck->kw[0].private = fcn;
6638
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006639 /* Register this new converter */
6640 sample_register_convs(sck);
6641
6642 return 0;
6643}
6644
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006645/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006646 * "sample-fetch" functions. It expects a converter name used
6647 * in the haproxy configuration file, and an LUA function.
6648 */
6649__LJMP static int hlua_register_fetches(lua_State *L)
6650{
6651 const char *name;
6652 int ref;
6653 int len;
6654 struct sample_fetch_kw_list *sfk;
6655 struct hlua_function *fcn;
Thierry Fournier04cbfd72020-11-28 20:41:07 +01006656 struct sample_fetch *sf;
6657 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006658
6659 MAY_LJMP(check_args(L, 2, "register_fetches"));
6660
6661 /* First argument : sample-fetch name. */
6662 name = MAY_LJMP(luaL_checkstring(L, 1));
6663
6664 /* Second argument : lua function. */
6665 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6666
Thierry Fournier04cbfd72020-11-28 20:41:07 +01006667 /* Check if the sample-fetch is already registered */
6668 trash = get_trash_chunk();
6669 chunk_printf(trash, "lua.%s", name);
6670 sf = find_sample_fetch(trash->area, trash->data);
6671 if (sf != NULL) {
6672 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6673 "This will become a hard error in version 2.5.\n", name);
6674 }
6675
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006676 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006677 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006678 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006679 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006680 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006681 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006682 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006683
6684 /* Fill fcn. */
6685 fcn->name = strdup(name);
6686 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006687 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006688 fcn->function_ref = ref;
6689
6690 /* List head */
6691 sfk->list.n = sfk->list.p = NULL;
6692
6693 /* sample-fetch keyword. */
6694 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006695 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006696 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006697 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006698
6699 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6700 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006701 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 +01006702 sfk->kw[0].val_args = NULL;
6703 sfk->kw[0].out_type = SMP_T_STR;
6704 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6705 sfk->kw[0].val = 0;
6706 sfk->kw[0].private = fcn;
6707
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006708 /* Register this new fetch. */
6709 sample_register_fetches(sfk);
6710
6711 return 0;
6712}
6713
Christopher Faulet501465d2020-02-26 14:54:16 +01006714/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006715 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006716__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006717{
6718 struct hlua *hlua = hlua_gethlua(L);
6719 unsigned int delay;
6720 unsigned int wakeup_ms;
6721
6722 MAY_LJMP(check_args(L, 1, "wake_time"));
6723
6724 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6725 wakeup_ms = tick_add(now_ms, delay);
6726 hlua->wake_time = wakeup_ms;
6727 return 0;
6728}
6729
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006730/* This function is a wrapper to execute each LUA function declared as an action
6731 * wrapper during the initialisation period. This function may return any
6732 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6733 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6734 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006735 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006736static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006737 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006738{
6739 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006740 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006741 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006742 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006743
6744 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006745 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6746 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6747 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6748 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006749 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006750 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006751 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006752 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006753
Willy Tarreau87b09662015-04-03 00:22:06 +02006754 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006755 * Lua context can be not initialized. This behavior
6756 * permits to save performances because a systematic
6757 * Lua initialization cause 5% performances loss.
6758 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006759 if (!s->hlua) {
Christopher Fauletf2e73622021-03-24 15:03:01 +01006760 struct hlua *hlua;
6761
6762 hlua = pool_alloc(pool_head_hlua);
6763 if (!hlua) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006764 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6765 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006766 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006767 }
Christopher Fauletf2e73622021-03-24 15:03:01 +01006768 HLUA_INIT(hlua);
6769 s->hlua = hlua;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006770 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006771 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6772 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006773 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006774 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006775 }
6776
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006777 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006778 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006779
6780 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006781 if (!SET_SAFE_LJMP(s->hlua->T)) {
6782 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6783 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006784 else
6785 error = "critical error";
6786 SEND_ERR(px, "Lua function '%s': %s.\n",
6787 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006788 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006789 }
6790
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006791 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006792 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006793 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006794 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006795 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006796 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006797 }
6798
6799 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006800 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006801
Willy Tarreau87b09662015-04-03 00:22:06 +02006802 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006803 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006804 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006805 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006806 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006807 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006808 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006809 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006810
6811 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006812 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006813 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006814 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006815 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006816 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006817 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006818 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006819 lua_pushstring(s->hlua->T, *arg);
6820 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006821 }
6822
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006823 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006824 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006825
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006826 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006827 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006828 }
6829
6830 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006831 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006832 /* finished. */
6833 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006834 /* Catch the return value */
6835 if (lua_gettop(s->hlua->T) > 0)
6836 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006837
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006838 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006839 if (act_ret == ACT_RET_YIELD) {
6840 if (flags & ACT_OPT_FINAL)
6841 goto err_yield;
6842
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006843 if (dir == SMP_OPT_DIR_REQ)
6844 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6845 s->hlua->wake_time);
6846 else
6847 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6848 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006849 }
6850 goto end;
6851
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006852 /* yield. */
6853 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006854 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006855 if (dir == SMP_OPT_DIR_REQ)
6856 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6857 s->hlua->wake_time);
6858 else
6859 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6860 s->hlua->wake_time);
6861
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006862 /* Some actions can be wake up when a "write" event
6863 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006864 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006865 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006866 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006867 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006868 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006869 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006870 act_ret = ACT_RET_YIELD;
6871 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006872
6873 /* finished with error. */
6874 case HLUA_E_ERRMSG:
6875 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006876 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006877 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6878 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006879 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006880
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006881 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006882 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006883 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006884
6885 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006886 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006887 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006888
6889 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006890 err_yield:
6891 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006892 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6893 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006894 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006895
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006896 case HLUA_E_ERR:
6897 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006898 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006899 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006900
6901 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006902 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006903 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006904
6905 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006906 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006907 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006908 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006909}
6910
Olivier Houchard9f6af332018-05-25 14:04:04 +02006911struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006912{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006913 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006914
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006915 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006916 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006917 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006918}
6919
6920static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6921{
6922 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006923 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006924 struct task *task;
6925 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006926 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006927
Willy Tarreaubafbe012017-11-24 17:34:44 +01006928 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006929 if (!hlua) {
6930 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6931 ctx->rule->arg.hlua_rule->fcn.name);
6932 return 0;
6933 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006934 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006935 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006936 ctx->ctx.hlua_apptcp.flags = 0;
6937
6938 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006939 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006940 if (!task) {
6941 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6942 ctx->rule->arg.hlua_rule->fcn.name);
6943 return 0;
6944 }
6945 task->nice = 0;
6946 task->context = ctx;
6947 task->process = hlua_applet_wakeup;
6948 ctx->ctx.hlua_apptcp.task = task;
6949
6950 /* In the execution wrappers linked with a stream, the
6951 * Lua context can be not initialized. This behavior
6952 * permits to save performances because a systematic
6953 * Lua initialization cause 5% performances loss.
6954 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006955 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006956 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6957 ctx->rule->arg.hlua_rule->fcn.name);
6958 return 0;
6959 }
6960
6961 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006962 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006963
6964 /* The following Lua calls can fail. */
6965 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006966 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6967 error = lua_tostring(hlua->T, -1);
6968 else
6969 error = "critical error";
6970 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6971 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006972 return 0;
6973 }
6974
6975 /* Check stack available size. */
6976 if (!lua_checkstack(hlua->T, 1)) {
6977 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6978 ctx->rule->arg.hlua_rule->fcn.name);
6979 RESET_SAFE_LJMP(hlua->T);
6980 return 0;
6981 }
6982
6983 /* Restore the function in the stack. */
6984 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6985
6986 /* Create and and push object stream in the stack. */
6987 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6988 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6989 ctx->rule->arg.hlua_rule->fcn.name);
6990 RESET_SAFE_LJMP(hlua->T);
6991 return 0;
6992 }
6993 hlua->nargs = 1;
6994
6995 /* push keywords in the stack. */
6996 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6997 if (!lua_checkstack(hlua->T, 1)) {
6998 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6999 ctx->rule->arg.hlua_rule->fcn.name);
7000 RESET_SAFE_LJMP(hlua->T);
7001 return 0;
7002 }
7003 lua_pushstring(hlua->T, *arg);
7004 hlua->nargs++;
7005 }
7006
7007 RESET_SAFE_LJMP(hlua->T);
7008
7009 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007010 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007011 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007012
7013 return 1;
7014}
7015
Willy Tarreau60409db2019-08-21 14:14:50 +02007016void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007017{
7018 struct stream_interface *si = ctx->owner;
7019 struct stream *strm = si_strm(si);
7020 struct channel *res = si_ic(si);
7021 struct act_rule *rule = ctx->rule;
7022 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007023 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007024
7025 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007026 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7027 /* eat the whole request */
7028 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007029 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007030 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007031
7032 /* If the stream is disconnect or closed, ldo nothing. */
7033 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7034 return;
7035
7036 /* Execute the function. */
7037 switch (hlua_ctx_resume(hlua, 1)) {
7038 /* finished. */
7039 case HLUA_E_OK:
7040 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7041
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007042 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007043 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007044 res->flags |= CF_READ_NULL;
7045 si_shutr(si);
7046 return;
7047
7048 /* yield. */
7049 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007050 if (hlua->wake_time != TICK_ETERNITY)
7051 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007052 return;
7053
7054 /* finished with error. */
7055 case HLUA_E_ERRMSG:
7056 /* Display log. */
7057 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7058 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7059 lua_pop(hlua->T, 1);
7060 goto error;
7061
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007062 case HLUA_E_ETMOUT:
7063 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7064 rule->arg.hlua_rule->fcn.name);
7065 goto error;
7066
7067 case HLUA_E_NOMEM:
7068 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7069 rule->arg.hlua_rule->fcn.name);
7070 goto error;
7071
7072 case HLUA_E_YIELD: /* unexpected */
7073 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7074 rule->arg.hlua_rule->fcn.name);
7075 goto error;
7076
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007077 case HLUA_E_ERR:
7078 /* Display log. */
7079 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7080 rule->arg.hlua_rule->fcn.name);
7081 goto error;
7082
7083 default:
7084 goto error;
7085 }
7086
7087error:
7088
7089 /* For all other cases, just close the stream. */
7090 si_shutw(si);
7091 si_shutr(si);
7092 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7093}
7094
7095static void hlua_applet_tcp_release(struct appctx *ctx)
7096{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007097 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007098 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007099 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007100 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007101}
7102
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007103/* The function returns 1 if the initialisation is complete, 0 if
7104 * an errors occurs and -1 if more data are required for initializing
7105 * the applet.
7106 */
7107static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7108{
7109 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007110 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007111 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007112 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007113 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007114 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007115
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007116 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007117 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007118 if (!hlua) {
7119 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7120 ctx->rule->arg.hlua_rule->fcn.name);
7121 return 0;
7122 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007123 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007124 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007125 ctx->ctx.hlua_apphttp.left_bytes = -1;
7126 ctx->ctx.hlua_apphttp.flags = 0;
7127
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007128 if (txn->req.flags & HTTP_MSGF_VER_11)
7129 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7130
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007131 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007132 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007133 if (!task) {
7134 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7135 ctx->rule->arg.hlua_rule->fcn.name);
7136 return 0;
7137 }
7138 task->nice = 0;
7139 task->context = ctx;
7140 task->process = hlua_applet_wakeup;
7141 ctx->ctx.hlua_apphttp.task = task;
7142
7143 /* In the execution wrappers linked with a stream, the
7144 * Lua context can be not initialized. This behavior
7145 * permits to save performances because a systematic
7146 * Lua initialization cause 5% performances loss.
7147 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007148 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007149 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7150 ctx->rule->arg.hlua_rule->fcn.name);
7151 return 0;
7152 }
7153
7154 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007155 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007156
7157 /* The following Lua calls can fail. */
7158 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007159 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7160 error = lua_tostring(hlua->T, -1);
7161 else
7162 error = "critical error";
7163 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7164 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007165 return 0;
7166 }
7167
7168 /* Check stack available size. */
7169 if (!lua_checkstack(hlua->T, 1)) {
7170 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7171 ctx->rule->arg.hlua_rule->fcn.name);
7172 RESET_SAFE_LJMP(hlua->T);
7173 return 0;
7174 }
7175
7176 /* Restore the function in the stack. */
7177 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7178
7179 /* Create and and push object stream in the stack. */
7180 if (!hlua_applet_http_new(hlua->T, ctx)) {
7181 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7182 ctx->rule->arg.hlua_rule->fcn.name);
7183 RESET_SAFE_LJMP(hlua->T);
7184 return 0;
7185 }
7186 hlua->nargs = 1;
7187
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007188 /* push keywords in the stack. */
7189 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7190 if (!lua_checkstack(hlua->T, 1)) {
7191 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7192 ctx->rule->arg.hlua_rule->fcn.name);
7193 RESET_SAFE_LJMP(hlua->T);
7194 return 0;
7195 }
7196 lua_pushstring(hlua->T, *arg);
7197 hlua->nargs++;
7198 }
7199
7200 RESET_SAFE_LJMP(hlua->T);
7201
7202 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007203 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007204
7205 return 1;
7206}
7207
Willy Tarreau60409db2019-08-21 14:14:50 +02007208void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007209{
7210 struct stream_interface *si = ctx->owner;
7211 struct stream *strm = si_strm(si);
7212 struct channel *req = si_oc(si);
7213 struct channel *res = si_ic(si);
7214 struct act_rule *rule = ctx->rule;
7215 struct proxy *px = strm->be;
7216 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7217 struct htx *req_htx, *res_htx;
7218
7219 res_htx = htx_from_buf(&res->buf);
7220
7221 /* If the stream is disconnect or closed, ldo nothing. */
7222 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7223 goto out;
7224
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007225 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007226 if (!b_size(&res->buf)) {
7227 si_rx_room_blk(si);
7228 goto out;
7229 }
7230 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007231 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007232 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7233
7234 /* Set the currently running flag. */
7235 if (!HLUA_IS_RUNNING(hlua) &&
7236 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7237 struct htx_blk *blk;
7238 size_t count = co_data(req);
7239
7240 if (!count) {
7241 si_cant_get(si);
7242 goto out;
7243 }
7244
7245 /* We need to flush the request header. This left the body for
7246 * the Lua.
7247 */
7248 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007249 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007250 while (count && blk) {
7251 enum htx_blk_type type = htx_get_blk_type(blk);
7252 uint32_t sz = htx_get_blksz(blk);
7253
7254 if (sz > count) {
7255 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007256 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007257 goto out;
7258 }
7259
7260 count -= sz;
7261 co_set_data(req, co_data(req) - sz);
7262 blk = htx_remove_blk(req_htx, blk);
7263
7264 if (type == HTX_BLK_EOH)
7265 break;
7266 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007267 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007268 }
7269
7270 /* Executes The applet if it is not done. */
7271 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7272
7273 /* Execute the function. */
7274 switch (hlua_ctx_resume(hlua, 1)) {
7275 /* finished. */
7276 case HLUA_E_OK:
7277 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7278 break;
7279
7280 /* yield. */
7281 case HLUA_E_AGAIN:
7282 if (hlua->wake_time != TICK_ETERNITY)
7283 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007284 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007285
7286 /* finished with error. */
7287 case HLUA_E_ERRMSG:
7288 /* Display log. */
7289 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7290 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7291 lua_pop(hlua->T, 1);
7292 goto error;
7293
7294 case HLUA_E_ETMOUT:
7295 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7296 rule->arg.hlua_rule->fcn.name);
7297 goto error;
7298
7299 case HLUA_E_NOMEM:
7300 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7301 rule->arg.hlua_rule->fcn.name);
7302 goto error;
7303
7304 case HLUA_E_YIELD: /* unexpected */
7305 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7306 rule->arg.hlua_rule->fcn.name);
7307 goto error;
7308
7309 case HLUA_E_ERR:
7310 /* Display log. */
7311 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7312 rule->arg.hlua_rule->fcn.name);
7313 goto error;
7314
7315 default:
7316 goto error;
7317 }
7318 }
7319
7320 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007321 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7322 goto done;
7323
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007324 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7325 goto error;
7326
Christopher Faulet54b5e212019-06-04 10:08:28 +02007327 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007328 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007329 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7330 si_rx_room_blk(si);
7331 goto out;
7332 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007333 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007334 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7335 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007336 }
7337
7338 done:
7339 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007340 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007341 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007342 si_shutr(si);
7343 }
7344
7345 /* eat the whole request */
7346 if (co_data(req)) {
7347 req_htx = htx_from_buf(&req->buf);
7348 co_htx_skip(req, req_htx, co_data(req));
7349 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007350 }
7351 }
7352
7353 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007354 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007355 return;
7356
7357 error:
7358
7359 /* If we are in HTTP mode, and we are not send any
7360 * data, return a 500 server error in best effort:
7361 * if there is no room available in the buffer,
7362 * just close the connection.
7363 */
7364 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007365 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007366
7367 channel_erase(res);
7368 res->buf.data = b_data(err);
7369 memcpy(res->buf.area, b_head(err), b_data(err));
7370 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007371 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007372 }
7373 if (!(strm->flags & SF_ERR_MASK))
7374 strm->flags |= SF_ERR_RESOURCE;
7375 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7376 goto done;
7377}
7378
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007379static void hlua_applet_http_release(struct appctx *ctx)
7380{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007381 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007382 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007383 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007384 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007385}
7386
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007387/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007388 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007389 *
7390 * This function can fail with an abort() due to an Lua critical error.
7391 * We are in the configuration parsing process of HAProxy, this abort() is
7392 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007393 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007394static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7395 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007396{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007397 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007398 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007399
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007400 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007401 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007402 if (!rule->arg.hlua_rule) {
7403 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007404 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007405 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007406
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007407 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007408 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7409 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007410 if (!rule->arg.hlua_rule->args) {
7411 memprintf(err, "out of memory error");
7412 return ACT_RET_PRS_ERR;
7413 }
7414
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007415 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007416 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007417
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007418 /* Expect some arguments */
7419 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007420 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007421 memprintf(err, "expect %d arguments", fcn->nargs);
7422 return ACT_RET_PRS_ERR;
7423 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007424 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007425 if (!rule->arg.hlua_rule->args[i]) {
7426 memprintf(err, "out of memory error");
7427 return ACT_RET_PRS_ERR;
7428 }
7429 (*cur_arg)++;
7430 }
7431 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007432
Thierry FOURNIER42148732015-09-02 17:17:33 +02007433 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007434 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007435 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007436}
7437
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007438static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7439 struct act_rule *rule, char **err)
7440{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007441 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007442
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007443 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007444 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007445 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007446 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007447 * the call of this analyzer.
7448 */
7449 if (rule->from != ACT_F_HTTP_REQ) {
7450 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7451 return ACT_RET_PRS_ERR;
7452 }
7453
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007454 /* Memory for the rule. */
7455 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7456 if (!rule->arg.hlua_rule) {
7457 memprintf(err, "out of memory error");
7458 return ACT_RET_PRS_ERR;
7459 }
7460
7461 /* Reference the Lua function and store the reference. */
7462 rule->arg.hlua_rule->fcn = *fcn;
7463
7464 /* TODO: later accept arguments. */
7465 rule->arg.hlua_rule->args = NULL;
7466
7467 /* Add applet pointer in the rule. */
7468 rule->applet.obj_type = OBJ_TYPE_APPLET;
7469 rule->applet.name = fcn->name;
7470 rule->applet.init = hlua_applet_http_init;
7471 rule->applet.fct = hlua_applet_http_fct;
7472 rule->applet.release = hlua_applet_http_release;
7473 rule->applet.timeout = hlua_timeout_applet;
7474
7475 return ACT_RET_PRS_OK;
7476}
7477
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007478/* This function is an LUA binding used for registering
7479 * "sample-conv" functions. It expects a converter name used
7480 * in the haproxy configuration file, and an LUA function.
7481 */
7482__LJMP static int hlua_register_action(lua_State *L)
7483{
7484 struct action_kw_list *akl;
7485 const char *name;
7486 int ref;
7487 int len;
7488 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007489 int nargs;
Thierry Fournier04cbfd72020-11-28 20:41:07 +01007490 struct buffer *trash;
7491 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007492
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007493 /* Initialise the number of expected arguments at 0. */
7494 nargs = 0;
7495
7496 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7497 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007498
7499 /* First argument : converter name. */
7500 name = MAY_LJMP(luaL_checkstring(L, 1));
7501
7502 /* Second argument : environment. */
7503 if (lua_type(L, 2) != LUA_TTABLE)
7504 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7505
7506 /* Third argument : lua function. */
7507 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7508
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007509 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007510 if (lua_gettop(L) >= 4)
7511 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7512
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007513 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007514 lua_pushnil(L);
7515 while (lua_next(L, 2) != 0) {
7516 if (lua_type(L, -1) != LUA_TSTRING)
7517 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7518
Thierry Fournier04cbfd72020-11-28 20:41:07 +01007519 /* Check if action exists */
7520 trash = get_trash_chunk();
7521 chunk_printf(trash, "lua.%s", name);
7522 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7523 akw = tcp_req_cont_action(trash->area);
7524 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7525 akw = tcp_res_cont_action(trash->area);
7526 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7527 akw = action_http_req_custom(trash->area);
7528 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7529 akw = action_http_res_custom(trash->area);
7530 } else {
7531 akw = NULL;
7532 }
7533 if (akw != NULL) {
7534 ha_warning("Trying to register action 'lua.%s' more than once. "
7535 "This will become a hard error in version 2.5.\n", name);
7536 }
7537
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007538 /* Check required environment. Only accepted "http" or "tcp". */
7539 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007540 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007541 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007542 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007543 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007544 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007545 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007546
7547 /* Fill fcn. */
7548 fcn->name = strdup(name);
7549 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007550 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007551 fcn->function_ref = ref;
7552
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007553 /* Set the expected number od arguments. */
7554 fcn->nargs = nargs;
7555
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007556 /* List head */
7557 akl->list.n = akl->list.p = NULL;
7558
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007559 /* action keyword. */
7560 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007561 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007562 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007563 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007564
7565 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7566
7567 akl->kw[0].match_pfx = 0;
7568 akl->kw[0].private = fcn;
7569 akl->kw[0].parse = action_register_lua;
7570
7571 /* select the action registering point. */
7572 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7573 tcp_req_cont_keywords_register(akl);
7574 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7575 tcp_res_cont_keywords_register(akl);
7576 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7577 http_req_keywords_register(akl);
7578 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7579 http_res_keywords_register(akl);
7580 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007581 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007582 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7583 "are expected.", lua_tostring(L, -1)));
7584
7585 /* pop the environment string. */
7586 lua_pop(L, 1);
7587 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007588 return ACT_RET_PRS_OK;
7589}
7590
7591static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7592 struct act_rule *rule, char **err)
7593{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007594 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007595
Christopher Faulet280f85b2019-07-15 15:02:04 +02007596 if (px->mode == PR_MODE_HTTP) {
7597 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007598 return ACT_RET_PRS_ERR;
7599 }
7600
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007601 /* Memory for the rule. */
7602 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7603 if (!rule->arg.hlua_rule) {
7604 memprintf(err, "out of memory error");
7605 return ACT_RET_PRS_ERR;
7606 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007607
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007608 /* Reference the Lua function and store the reference. */
7609 rule->arg.hlua_rule->fcn = *fcn;
7610
7611 /* TODO: later accept arguments. */
7612 rule->arg.hlua_rule->args = NULL;
7613
7614 /* Add applet pointer in the rule. */
7615 rule->applet.obj_type = OBJ_TYPE_APPLET;
7616 rule->applet.name = fcn->name;
7617 rule->applet.init = hlua_applet_tcp_init;
7618 rule->applet.fct = hlua_applet_tcp_fct;
7619 rule->applet.release = hlua_applet_tcp_release;
7620 rule->applet.timeout = hlua_timeout_applet;
7621
7622 return 0;
7623}
7624
7625/* This function is an LUA binding used for registering
7626 * "sample-conv" functions. It expects a converter name used
7627 * in the haproxy configuration file, and an LUA function.
7628 */
7629__LJMP static int hlua_register_service(lua_State *L)
7630{
7631 struct action_kw_list *akl;
7632 const char *name;
7633 const char *env;
7634 int ref;
7635 int len;
7636 struct hlua_function *fcn;
Thierry Fournier04cbfd72020-11-28 20:41:07 +01007637 struct buffer *trash;
7638 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007639
7640 MAY_LJMP(check_args(L, 3, "register_service"));
7641
7642 /* First argument : converter name. */
7643 name = MAY_LJMP(luaL_checkstring(L, 1));
7644
7645 /* Second argument : environment. */
7646 env = MAY_LJMP(luaL_checkstring(L, 2));
7647
7648 /* Third argument : lua function. */
7649 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7650
Thierry Fournier04cbfd72020-11-28 20:41:07 +01007651 /* Check for service already registered */
7652 trash = get_trash_chunk();
7653 chunk_printf(trash, "lua.%s", name);
7654 akw = service_find(trash->area);
7655 if (akw != NULL) {
7656 ha_warning("Trying to register service 'lua.%s' more than once. "
7657 "This will become a hard error in version 2.5.\n", name);
7658 }
7659
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007660 /* Allocate and fill the sample fetch keyword struct. */
7661 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7662 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007663 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007664 fcn = calloc(1, sizeof(*fcn));
7665 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007666 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007667
7668 /* Fill fcn. */
7669 len = strlen("<lua.>") + strlen(name) + 1;
7670 fcn->name = calloc(1, len);
7671 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007672 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007673 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7674 fcn->function_ref = ref;
7675
7676 /* List head */
7677 akl->list.n = akl->list.p = NULL;
7678
7679 /* converter keyword. */
7680 len = strlen("lua.") + strlen(name) + 1;
7681 akl->kw[0].kw = calloc(1, len);
7682 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007683 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007684
7685 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7686
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007687 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007688 if (strcmp(env, "tcp") == 0)
7689 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007690 else if (strcmp(env, "http") == 0)
7691 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007692 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007693 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007694 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007695
7696 akl->kw[0].match_pfx = 0;
7697 akl->kw[0].private = fcn;
7698
7699 /* End of array. */
7700 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7701
7702 /* Register this new converter */
7703 service_keywords_register(akl);
7704
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007705 return 0;
7706}
7707
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007708/* This function initialises Lua cli handler. It copies the
7709 * arguments in the Lua stack and create channel IO objects.
7710 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007711static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007712{
7713 struct hlua *hlua;
7714 struct hlua_function *fcn;
7715 int i;
7716 const char *error;
7717
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007718 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007719 appctx->ctx.hlua_cli.fcn = private;
7720
Willy Tarreaubafbe012017-11-24 17:34:44 +01007721 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007722 if (!hlua) {
7723 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007724 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007725 }
7726 HLUA_INIT(hlua);
7727 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007728
7729 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007730 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007731 * applet_http. It is absolutely compatible.
7732 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007733 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007734 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007735 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007736 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007737 }
7738 appctx->ctx.hlua_cli.task->nice = 0;
7739 appctx->ctx.hlua_cli.task->context = appctx;
7740 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7741
7742 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007743 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007744 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007745 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007746 }
7747
7748 /* The following Lua calls can fail. */
7749 if (!SET_SAFE_LJMP(hlua->T)) {
7750 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7751 error = lua_tostring(hlua->T, -1);
7752 else
7753 error = "critical error";
7754 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7755 goto error;
7756 }
7757
7758 /* Check stack available size. */
7759 if (!lua_checkstack(hlua->T, 2)) {
7760 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7761 goto error;
7762 }
7763
7764 /* Restore the function in the stack. */
7765 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7766
7767 /* Once the arguments parsed, the CLI is like an AppletTCP,
7768 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007769 */
7770 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7771 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7772 goto error;
7773 }
7774 hlua->nargs = 1;
7775
7776 /* push keywords in the stack. */
7777 for (i = 0; *args[i]; i++) {
7778 /* Check stack available size. */
7779 if (!lua_checkstack(hlua->T, 1)) {
7780 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7781 goto error;
7782 }
7783 lua_pushstring(hlua->T, args[i]);
7784 hlua->nargs++;
7785 }
7786
7787 /* We must initialize the execution timeouts. */
7788 hlua->max_time = hlua_timeout_session;
7789
7790 /* At this point the execution is safe. */
7791 RESET_SAFE_LJMP(hlua->T);
7792
7793 /* It's ok */
7794 return 0;
7795
7796 /* It's not ok. */
7797error:
7798 RESET_SAFE_LJMP(hlua->T);
7799 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007800 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007801 return 1;
7802}
7803
7804static int hlua_cli_io_handler_fct(struct appctx *appctx)
7805{
7806 struct hlua *hlua;
7807 struct stream_interface *si;
7808 struct hlua_function *fcn;
7809
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007810 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007811 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007812 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007813
7814 /* If the stream is disconnect or closed, ldo nothing. */
7815 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7816 return 1;
7817
7818 /* Execute the function. */
7819 switch (hlua_ctx_resume(hlua, 1)) {
7820
7821 /* finished. */
7822 case HLUA_E_OK:
7823 return 1;
7824
7825 /* yield. */
7826 case HLUA_E_AGAIN:
7827 /* We want write. */
7828 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007829 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007830 /* Set the timeout. */
7831 if (hlua->wake_time != TICK_ETERNITY)
7832 task_schedule(hlua->task, hlua->wake_time);
7833 return 0;
7834
7835 /* finished with error. */
7836 case HLUA_E_ERRMSG:
7837 /* Display log. */
7838 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7839 fcn->name, lua_tostring(hlua->T, -1));
7840 lua_pop(hlua->T, 1);
7841 return 1;
7842
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007843 case HLUA_E_ETMOUT:
7844 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7845 fcn->name);
7846 return 1;
7847
7848 case HLUA_E_NOMEM:
7849 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7850 fcn->name);
7851 return 1;
7852
7853 case HLUA_E_YIELD: /* unexpected */
7854 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7855 fcn->name);
7856 return 1;
7857
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007858 case HLUA_E_ERR:
7859 /* Display log. */
7860 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7861 fcn->name);
7862 return 1;
7863
7864 default:
7865 return 1;
7866 }
7867
7868 return 1;
7869}
7870
7871static void hlua_cli_io_release_fct(struct appctx *appctx)
7872{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007873 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007874 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007875}
7876
7877/* This function is an LUA binding used for registering
7878 * new keywords in the cli. It expects a list of keywords
7879 * which are the "path". It is limited to 5 keywords. A
7880 * description of the command, a function to be executed
7881 * for the parsing and a function for io handlers.
7882 */
7883__LJMP static int hlua_register_cli(lua_State *L)
7884{
7885 struct cli_kw_list *cli_kws;
7886 const char *message;
7887 int ref_io;
7888 int len;
7889 struct hlua_function *fcn;
7890 int index;
7891 int i;
Thierry Fournier04cbfd72020-11-28 20:41:07 +01007892 struct buffer *trash;
7893 const char *kw[5];
7894 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007895
7896 MAY_LJMP(check_args(L, 3, "register_cli"));
7897
7898 /* First argument : an array of maximum 5 keywords. */
7899 if (!lua_istable(L, 1))
7900 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7901
7902 /* Second argument : string with contextual message. */
7903 message = MAY_LJMP(luaL_checkstring(L, 2));
7904
7905 /* Third and fourth argument : lua function. */
7906 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7907
Thierry Fournier04cbfd72020-11-28 20:41:07 +01007908 /* Check for CLI service already registered */
7909 trash = get_trash_chunk();
7910 index = 0;
7911 lua_pushnil(L);
7912 memset(kw, 0, sizeof(kw));
7913 while (lua_next(L, 1) != 0) {
7914 if (index >= CLI_PREFIX_KW_NB)
7915 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7916 if (lua_type(L, -1) != LUA_TSTRING)
7917 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7918 kw[index] = lua_tostring(L, -1);
7919 if (index == 0)
7920 chunk_printf(trash, "%s", kw[index]);
7921 else
7922 chunk_appendf(trash, " %s", kw[index]);
7923 index++;
7924 lua_pop(L, 1);
7925 }
7926 cli_kw = cli_find_kw_exact((char **)kw);
7927 if (cli_kw != NULL) {
7928 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
7929 "This will become a hard error in version 2.5.\n", trash->area);
7930 }
7931
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007932 /* Allocate and fill the sample fetch keyword struct. */
7933 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7934 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007935 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007936 fcn = calloc(1, sizeof(*fcn));
7937 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007938 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007939
7940 /* Fill path. */
7941 index = 0;
7942 lua_pushnil(L);
7943 while(lua_next(L, 1) != 0) {
7944 if (index >= 5)
7945 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7946 if (lua_type(L, -1) != LUA_TSTRING)
7947 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7948 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7949 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007950 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007951 index++;
7952 lua_pop(L, 1);
7953 }
7954
7955 /* Copy help message. */
7956 cli_kws->kw[0].usage = strdup(message);
7957 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007958 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007959
7960 /* Fill fcn io handler. */
7961 len = strlen("<lua.cli>") + 1;
7962 for (i = 0; i < index; i++)
7963 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7964 fcn->name = calloc(1, len);
7965 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007966 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007967 strncat((char *)fcn->name, "<lua.cli", len);
7968 for (i = 0; i < index; i++) {
7969 strncat((char *)fcn->name, ".", len);
7970 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7971 }
7972 strncat((char *)fcn->name, ">", len);
7973 fcn->function_ref = ref_io;
7974
7975 /* Fill last entries. */
7976 cli_kws->kw[0].private = fcn;
7977 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7978 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7979 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7980
7981 /* Register this new converter */
7982 cli_register_kw(cli_kws);
7983
7984 return 0;
7985}
7986
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007987static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7988 struct proxy *defpx, const char *file, int line,
7989 char **err, unsigned int *timeout)
7990{
7991 const char *error;
7992
7993 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007994 if (error == PARSE_TIME_OVER) {
7995 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7996 args[1], args[0]);
7997 return -1;
7998 }
7999 else if (error == PARSE_TIME_UNDER) {
8000 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8001 args[1], args[0]);
8002 return -1;
8003 }
8004 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008005 memprintf(err, "%s: invalid timeout", args[0]);
8006 return -1;
8007 }
8008 return 0;
8009}
8010
8011static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8012 struct proxy *defpx, const char *file, int line,
8013 char **err)
8014{
8015 return hlua_read_timeout(args, section_type, curpx, defpx,
8016 file, line, err, &hlua_timeout_session);
8017}
8018
8019static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8020 struct proxy *defpx, const char *file, int line,
8021 char **err)
8022{
8023 return hlua_read_timeout(args, section_type, curpx, defpx,
8024 file, line, err, &hlua_timeout_task);
8025}
8026
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008027static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8028 struct proxy *defpx, const char *file, int line,
8029 char **err)
8030{
8031 return hlua_read_timeout(args, section_type, curpx, defpx,
8032 file, line, err, &hlua_timeout_applet);
8033}
8034
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008035static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8036 struct proxy *defpx, const char *file, int line,
8037 char **err)
8038{
8039 char *error;
8040
8041 hlua_nb_instruction = strtoll(args[1], &error, 10);
8042 if (*error != '\0') {
8043 memprintf(err, "%s: invalid number", args[0]);
8044 return -1;
8045 }
8046 return 0;
8047}
8048
Willy Tarreau32f61e22015-03-18 17:54:59 +01008049static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8050 struct proxy *defpx, const char *file, int line,
8051 char **err)
8052{
8053 char *error;
8054
8055 if (*(args[1]) == 0) {
8056 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8057 return -1;
8058 }
8059 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8060 if (*error != '\0') {
8061 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8062 return -1;
8063 }
8064 return 0;
8065}
8066
8067
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008068/* This function is called by the main configuration key "lua-load". It loads and
8069 * execute an lua file during the parsing of the HAProxy configuration file. It is
8070 * the main lua entry point.
8071 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008072 * This function runs with the HAProxy keywords API. It returns -1 if an error
8073 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008074 *
8075 * In some error case, LUA set an error message in top of the stack. This function
8076 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008077 *
8078 * This function can fail with an abort() due to an Lua critical error.
8079 * We are in the configuration parsing process of HAProxy, this abort() is
8080 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008081 */
8082static int hlua_load(char **args, int section_type, struct proxy *curpx,
8083 struct proxy *defpx, const char *file, int line,
8084 char **err)
8085{
8086 int error;
8087
Thierry Fournierad6e9c52020-11-29 01:06:24 +01008088 if (*(args[1]) == 0) {
8089 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8090 return -1;
8091 }
8092
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008093 /* Just load and compile the file. */
8094 error = luaL_loadfile(gL.T, args[1]);
8095 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008096 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008097 lua_pop(gL.T, 1);
8098 return -1;
8099 }
8100
8101 /* If no syntax error where detected, execute the code. */
8102 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8103 switch (error) {
8104 case LUA_OK:
8105 break;
8106 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008107 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008108 lua_pop(gL.T, 1);
8109 return -1;
8110 case LUA_ERRMEM:
Thierry Fournier518017f2020-11-29 00:55:53 +01008111 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008112 return -1;
8113 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008114 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008115 lua_pop(gL.T, 1);
8116 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008117#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008118 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008119 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008120 lua_pop(gL.T, 1);
8121 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008122#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008123 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05008124 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008125 lua_pop(gL.T, 1);
8126 return -1;
8127 }
8128
8129 return 0;
8130}
8131
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008132/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8133 * in the given <ctx>.
8134 */
8135static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
8136{
8137 lua_getglobal(ctx.T, "package"); /* push package variable */
8138 lua_pushstring(ctx.T, path); /* push given path */
8139 lua_pushstring(ctx.T, ";"); /* push semicolon */
8140 lua_getfield(ctx.T, -3, type); /* push old path */
8141 lua_concat(ctx.T, 3); /* concatenate to new path */
8142 lua_setfield(ctx.T, -2, type); /* store new path */
8143 lua_pop(ctx.T, 1); /* pop package variable */
8144
8145 return 0;
8146}
8147
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008148static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
8149 struct proxy *defpx, const char *file, int line,
8150 char **err)
8151{
8152 char *path;
8153 char *type = "path";
8154 if (too_many_args(2, args, err, NULL)) {
8155 return -1;
8156 }
8157
8158 if (!(*args[1])) {
8159 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
8160 return -1;
8161 }
8162 path = args[1];
8163
8164 if (*args[2]) {
8165 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8166 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
8167 return -1;
8168 }
8169 type = args[2];
8170 }
8171
8172 return hlua_prepend_path(gL, type, path);
8173}
8174
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008175/* configuration keywords declaration */
8176static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008177 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008178 { CFG_GLOBAL, "lua-load", hlua_load },
8179 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8180 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008181 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008182 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008183 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008184 { 0, NULL, NULL },
8185}};
8186
Willy Tarreau0108d902018-11-25 19:14:37 +01008187INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8188
Christopher Fauletafd8f102018-11-08 11:34:21 +01008189
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008190/* This function can fail with an abort() due to an Lua critical error.
8191 * We are in the initialisation process of HAProxy, this abort() is
8192 * tolerated.
8193 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008194int hlua_post_init()
8195{
8196 struct hlua_init_function *init;
8197 const char *msg;
8198 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008199 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008200
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008201 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008202 if (!SET_SAFE_LJMP(gL.T)) {
8203 if (lua_type(gL.T, -1) == LUA_TSTRING)
8204 error = lua_tostring(gL.T, -1);
8205 else
8206 error = "critical error";
8207 fprintf(stderr, "Lua post-init: %s.\n", error);
8208 exit(1);
8209 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008210
8211#if USE_OPENSSL
8212 /* Initialize SSL server. */
8213 if (socket_ssl.xprt->prepare_srv) {
8214 int saved_used_backed = global.ssl_used_backend;
8215 // don't affect maxconn automatic computation
8216 socket_ssl.xprt->prepare_srv(&socket_ssl);
8217 global.ssl_used_backend = saved_used_backed;
8218 }
8219#endif
8220
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008221 hlua_fcn_post_init(gL.T);
8222 RESET_SAFE_LJMP(gL.T);
8223
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008224 list_for_each_entry(init, &hlua_init_functions, l) {
8225 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8226 ret = hlua_ctx_resume(&gL, 0);
8227 switch (ret) {
8228 case HLUA_E_OK:
8229 lua_pop(gL.T, -1);
Thierry Fournier5a964672020-11-28 11:02:58 +01008230 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008231 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008232 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008233 return 0;
8234 case HLUA_E_ERRMSG:
8235 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008236 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008237 return 0;
8238 case HLUA_E_ERR:
8239 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008240 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008241 return 0;
8242 }
8243 }
8244 return 1;
8245}
8246
Willy Tarreau32f61e22015-03-18 17:54:59 +01008247/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8248 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8249 * is the previously allocated size or the kind of object in case of a new
8250 * allocation. <nsize> is the requested new size.
8251 */
8252static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8253{
8254 struct hlua_mem_allocator *zone = ud;
8255
8256 if (nsize == 0) {
8257 /* it's a free */
8258 if (ptr)
8259 zone->allocated -= osize;
8260 free(ptr);
8261 return NULL;
8262 }
8263
8264 if (!ptr) {
8265 /* it's a new allocation */
8266 if (zone->limit && zone->allocated + nsize > zone->limit)
8267 return NULL;
8268
8269 ptr = malloc(nsize);
8270 if (ptr)
8271 zone->allocated += nsize;
8272 return ptr;
8273 }
8274
8275 /* it's a realloc */
8276 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8277 return NULL;
8278
8279 ptr = realloc(ptr, nsize);
8280 if (ptr)
8281 zone->allocated += nsize - osize;
8282 return ptr;
8283}
8284
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008285/* Ithis function can fail with an abort() due to an Lua critical error.
8286 * We are in the initialisation process of HAProxy, this abort() is
8287 * tolerated.
8288 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008289void hlua_init(void)
8290{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008291 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008292 int idx;
8293 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008294 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008295 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008296 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008297#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008298 struct srv_kw *kw;
8299 int tmp_error;
8300 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008301 char *args[] = { /* SSL client configuration. */
8302 "ssl",
8303 "verify",
8304 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008305 NULL
8306 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008307#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008308
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008309 /* Init main lua stack. */
8310 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008311 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008312 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008313 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008314 hlua_sethlua(&gL);
8315 gL.Tref = LUA_REFNIL;
8316 gL.task = NULL;
8317
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008318 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008319 * the Lua function can fail with an abort. We are in the initialisation
8320 * process of HAProxy, this abort() is tolerated.
8321 */
8322
Thierry Fournier2b7983c2020-11-28 16:08:02 +01008323 /* Set safe environment for the initialisation. */
8324 if (!SET_SAFE_LJMP(gL.T)) {
8325 if (lua_type(gL.T, -1) == LUA_TSTRING)
8326 error_msg = lua_tostring(gL.T, -1);
8327 else
8328 error_msg = "critical error";
8329 fprintf(stderr, "Lua init: %s.\n", error_msg);
8330 exit(1);
8331 }
8332
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008333 /* Initialise lua. */
8334 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008335#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8336#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8337#ifdef HLUA_PREPEND_PATH
8338 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8339#endif
8340#ifdef HLUA_PREPEND_CPATH
8341 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8342#endif
8343#undef HLUA_PREPEND_PATH_TOSTRING
8344#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008345
8346 /*
8347 *
8348 * Create "core" object.
8349 *
8350 */
8351
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008352 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008353 lua_newtable(gL.T);
8354
8355 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008356 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008357 hlua_class_const_int(gL.T, log_levels[i], i);
8358
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008359 /* Register special functions. */
8360 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008361 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008362 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008363 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008364 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008365 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008366 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008367 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008368 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008369 hlua_class_function(gL.T, "sleep", hlua_sleep);
8370 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008371 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8372 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8373 hlua_class_function(gL.T, "set_map", hlua_set_map);
8374 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008375 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008376 hlua_class_function(gL.T, "log", hlua_log);
8377 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8378 hlua_class_function(gL.T, "Info", hlua_log_info);
8379 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8380 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008381 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008382 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008383
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008384 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008385
8386 /*
8387 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008388 * Create "act" object.
8389 *
8390 */
8391
8392 /* This table entry is the object "act" base. */
8393 lua_newtable(gL.T);
8394
8395 /* push action return constants */
8396 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8397 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8398 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8399 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8400 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8401 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8402 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8403 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8404
Christopher Faulet501465d2020-02-26 14:54:16 +01008405 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008406
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008407 lua_setglobal(gL.T, "act");
8408
8409 /*
8410 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008411 * Register class Map
8412 *
8413 */
8414
8415 /* This table entry is the object "Map" base. */
8416 lua_newtable(gL.T);
8417
8418 /* register pattern types. */
8419 for (i=0; i<PAT_MATCH_NUM; i++)
8420 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008421 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008422 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8423 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008424 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008425
8426 /* register constructor. */
8427 hlua_class_function(gL.T, "new", hlua_map_new);
8428
8429 /* Create and fill the metatable. */
8430 lua_newtable(gL.T);
8431
Ilya Shipitsind4259502020-04-08 01:07:56 +05008432 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008433 lua_pushstring(gL.T, "__index");
8434 lua_newtable(gL.T);
8435
8436 /* Register . */
8437 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8438 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8439
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008440 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008441
Thierry Fournier45e78d72016-02-19 18:34:46 +01008442 /* Register previous table in the registry with reference and named entry.
8443 * The function hlua_register_metatable() pops the stack, so we
8444 * previously create a copy of the table.
8445 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008446 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008447 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008448
8449 /* Assign the metatable to the mai Map object. */
8450 lua_setmetatable(gL.T, -2);
8451
8452 /* Set a name to the table. */
8453 lua_setglobal(gL.T, "Map");
8454
8455 /*
8456 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008457 * Register class Channel
8458 *
8459 */
8460
8461 /* Create and fill the metatable. */
8462 lua_newtable(gL.T);
8463
Ilya Shipitsind4259502020-04-08 01:07:56 +05008464 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008465 lua_pushstring(gL.T, "__index");
8466 lua_newtable(gL.T);
8467
8468 /* Register . */
8469 hlua_class_function(gL.T, "get", hlua_channel_get);
8470 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8471 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8472 hlua_class_function(gL.T, "set", hlua_channel_set);
8473 hlua_class_function(gL.T, "append", hlua_channel_append);
8474 hlua_class_function(gL.T, "send", hlua_channel_send);
8475 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8476 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8477 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008478 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008479 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008480
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008481 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008482
8483 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008484 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008485
8486 /*
8487 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008488 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008489 *
8490 */
8491
8492 /* Create and fill the metatable. */
8493 lua_newtable(gL.T);
8494
Ilya Shipitsind4259502020-04-08 01:07:56 +05008495 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008496 lua_pushstring(gL.T, "__index");
8497 lua_newtable(gL.T);
8498
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008499 /* Browse existing fetches and create the associated
8500 * object method.
8501 */
8502 sf = NULL;
8503 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008504 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8505 * by an underscore.
8506 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008507 strncpy(trash.area, sf->kw, trash.size);
8508 trash.area[trash.size - 1] = '\0';
8509 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008510 if (*p == '.' || *p == '-' || *p == '+')
8511 *p = '_';
8512
8513 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008514 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008515 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008516 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008517 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008518 }
8519
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008520 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008521
8522 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008523 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008524
8525 /*
8526 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008527 * Register class Converters
8528 *
8529 */
8530
8531 /* Create and fill the metatable. */
8532 lua_newtable(gL.T);
8533
8534 /* Create and fill the __index entry. */
8535 lua_pushstring(gL.T, "__index");
8536 lua_newtable(gL.T);
8537
8538 /* Browse existing converters and create the associated
8539 * object method.
8540 */
8541 sc = NULL;
8542 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008543 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8544 * by an underscore.
8545 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008546 strncpy(trash.area, sc->kw, trash.size);
8547 trash.area[trash.size - 1] = '\0';
8548 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008549 if (*p == '.' || *p == '-' || *p == '+')
8550 *p = '_';
8551
8552 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008553 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008554 lua_pushlightuserdata(gL.T, sc);
8555 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008556 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008557 }
8558
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008559 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008560
8561 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008562 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008563
8564 /*
8565 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008566 * Register class HTTP
8567 *
8568 */
8569
8570 /* Create and fill the metatable. */
8571 lua_newtable(gL.T);
8572
Ilya Shipitsind4259502020-04-08 01:07:56 +05008573 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008574 lua_pushstring(gL.T, "__index");
8575 lua_newtable(gL.T);
8576
8577 /* Register Lua functions. */
8578 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8579 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8580 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8581 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8582 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8583 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8584 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8585 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8586 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8587 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8588
8589 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8590 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8591 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8592 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8593 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8594 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008595 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008596
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008597 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008598
8599 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008600 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008601
8602 /*
8603 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008604 * Register class AppletTCP
8605 *
8606 */
8607
8608 /* Create and fill the metatable. */
8609 lua_newtable(gL.T);
8610
Ilya Shipitsind4259502020-04-08 01:07:56 +05008611 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008612 lua_pushstring(gL.T, "__index");
8613 lua_newtable(gL.T);
8614
8615 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008616 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8617 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8618 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8619 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8620 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008621 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8622 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8623 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008624
8625 lua_settable(gL.T, -3);
8626
8627 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008628 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008629
8630 /*
8631 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008632 * Register class AppletHTTP
8633 *
8634 */
8635
8636 /* Create and fill the metatable. */
8637 lua_newtable(gL.T);
8638
Ilya Shipitsind4259502020-04-08 01:07:56 +05008639 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008640 lua_pushstring(gL.T, "__index");
8641 lua_newtable(gL.T);
8642
8643 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008644 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8645 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008646 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8647 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8648 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008649 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8650 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8651 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8652 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8653 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8654 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8655
8656 lua_settable(gL.T, -3);
8657
8658 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008659 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008660
8661 /*
8662 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008663 * Register class TXN
8664 *
8665 */
8666
8667 /* Create and fill the metatable. */
8668 lua_newtable(gL.T);
8669
Ilya Shipitsind4259502020-04-08 01:07:56 +05008670 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008671 lua_pushstring(gL.T, "__index");
8672 lua_newtable(gL.T);
8673
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008674 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008675 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8676 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8677 hlua_class_function(gL.T, "set_var", hlua_set_var);
8678 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8679 hlua_class_function(gL.T, "get_var", hlua_get_var);
8680 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008681 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008682 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8683 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8684 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8685 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8686 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8687 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8688 hlua_class_function(gL.T, "log", hlua_txn_log);
8689 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8690 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8691 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8692 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008693
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008694 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008695
8696 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008697 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008698
8699 /*
8700 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008701 * Register class reply
8702 *
8703 */
8704 lua_newtable(gL.T);
8705 lua_pushstring(gL.T, "__index");
8706 lua_newtable(gL.T);
8707 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8708 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8709 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8710 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8711 lua_settable(gL.T, -3); /* Sets the __index entry. */
8712 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8713
8714
8715 /*
8716 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008717 * Register class Socket
8718 *
8719 */
8720
8721 /* Create and fill the metatable. */
8722 lua_newtable(gL.T);
8723
Ilya Shipitsind4259502020-04-08 01:07:56 +05008724 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008725 lua_pushstring(gL.T, "__index");
8726 lua_newtable(gL.T);
8727
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008728#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008729 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008730#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008731 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8732 hlua_class_function(gL.T, "send", hlua_socket_send);
8733 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8734 hlua_class_function(gL.T, "close", hlua_socket_close);
8735 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8736 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8737 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8738 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8739
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008740 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008741
8742 /* Register the garbage collector entry. */
8743 lua_pushstring(gL.T, "__gc");
8744 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008745 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008746
8747 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008748 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008749
8750 /* Proxy and server configuration initialisation. */
8751 memset(&socket_proxy, 0, sizeof(socket_proxy));
8752 init_new_proxy(&socket_proxy);
8753 socket_proxy.parent = NULL;
8754 socket_proxy.last_change = now.tv_sec;
8755 socket_proxy.id = "LUA-SOCKET";
8756 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8757 socket_proxy.maxconn = 0;
8758 socket_proxy.accept = NULL;
8759 socket_proxy.options2 |= PR_O2_INDEPSTR;
8760 socket_proxy.srv = NULL;
8761 socket_proxy.conn_retries = 0;
8762 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8763
8764 /* Init TCP server: unchanged parameters */
8765 memset(&socket_tcp, 0, sizeof(socket_tcp));
8766 socket_tcp.next = NULL;
8767 socket_tcp.proxy = &socket_proxy;
8768 socket_tcp.obj_type = OBJ_TYPE_SERVER;
Willy Tarreau72653c52021-03-04 10:47:54 +01008769 for (i = 0; i < MAX_THREADS; i++)
8770 MT_LIST_INIT(&socket_tcp.actconns[i]);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008771 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008772 socket_tcp.idle_conns = NULL;
8773 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008774 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008775 socket_tcp.last_change = 0;
8776 socket_tcp.id = "LUA-TCP-CONN";
8777 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8778 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8779 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8780
8781 /* XXX: Copy default parameter from default server,
8782 * but the default server is not initialized.
8783 */
8784 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8785 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8786 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8787 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8788 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8789 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8790 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8791 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8792 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8793 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8794
8795 socket_tcp.check.status = HCHK_STATUS_INI;
8796 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8797 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8798 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8799 socket_tcp.check.server = &socket_tcp;
8800
8801 socket_tcp.agent.status = HCHK_STATUS_INI;
8802 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8803 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8804 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8805 socket_tcp.agent.server = &socket_tcp;
8806
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008807 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008808
8809#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008810 /* Init TCP server: unchanged parameters */
8811 memset(&socket_ssl, 0, sizeof(socket_ssl));
8812 socket_ssl.next = NULL;
8813 socket_ssl.proxy = &socket_proxy;
8814 socket_ssl.obj_type = OBJ_TYPE_SERVER;
Willy Tarreau72653c52021-03-04 10:47:54 +01008815 for (i = 0; i < MAX_THREADS; i++)
8816 MT_LIST_INIT(&socket_ssl.actconns[i]);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008817 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008818 socket_ssl.idle_conns = NULL;
8819 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008820 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008821 socket_ssl.last_change = 0;
8822 socket_ssl.id = "LUA-SSL-CONN";
8823 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8824 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8825 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8826
8827 /* XXX: Copy default parameter from default server,
8828 * but the default server is not initialized.
8829 */
8830 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8831 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8832 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8833 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8834 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8835 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8836 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8837 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8838 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8839 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8840
8841 socket_ssl.check.status = HCHK_STATUS_INI;
8842 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8843 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8844 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8845 socket_ssl.check.server = &socket_ssl;
8846
8847 socket_ssl.agent.status = HCHK_STATUS_INI;
8848 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8849 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8850 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8851 socket_ssl.agent.server = &socket_ssl;
8852
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008853 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008854 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008855
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008856 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008857 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8858 /*
8859 *
8860 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008861 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008862 * features like client certificates and ssl_verify.
8863 *
8864 */
8865 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8866 if (tmp_error != 0) {
8867 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8868 abort(); /* This must be never arrives because the command line
8869 not editable by the user. */
8870 }
8871 idx += kw->skip;
8872 }
8873 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008874#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008875
8876 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008877}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008878
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008879static void hlua_deinit()
8880{
8881 lua_close(gL.T);
8882}
8883
8884REGISTER_POST_DEINIT(hlua_deinit);
8885
Willy Tarreau80713382018-11-26 10:19:54 +01008886static void hlua_register_build_options(void)
8887{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008888 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008889
Willy Tarreaubb57d942016-12-21 19:04:56 +01008890 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8891 hap_register_build_opts(ptr, 1);
8892}
Willy Tarreau80713382018-11-26 10:19:54 +01008893
8894INITCALL0(STG_REGISTER, hlua_register_build_options);