blob: 3d92c7d08b09174e3034ce89fface445098aafb7 [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
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020014#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016#include <lauxlib.h>
17#include <lua.h>
18#include <lualib.h>
19
Thierry FOURNIER463119c2015-03-10 00:35:36 +010020#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
21#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010022#endif
23
Willy Tarreau8d2b7772020-05-27 10:58:19 +020024#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
27#include <haproxy/applet.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/arg.h>
Christopher Fauletd25d9262020-08-06 11:04:46 +020029#include <haproxy/auth.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020030#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020031#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020032#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020033#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020034#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020035#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020036#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020037#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020038#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020040#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020041#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020042#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020043#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020044#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020045#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020046#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020048#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020049#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020050#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020051#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020052#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020053#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020054#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020055#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020056#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020057#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020058#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020059#include <haproxy/xref.h>
60
Thierry FOURNIER380d0932015-01-23 14:27:52 +010061
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010062/* Lua uses longjmp to perform yield or throwing errors. This
63 * macro is used only for identifying the function that can
64 * not return because a longjmp is executed.
65 * __LJMP marks a prototype of hlua file that can use longjmp.
66 * WILL_LJMP() marks an lua function that will use longjmp.
67 * MAY_LJMP() marks an lua function that may use longjmp.
68 */
69#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020070#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010071#define MAY_LJMP(func) func
72
Thierry FOURNIERbabae282015-09-17 11:36:37 +020073/* This couple of function executes securely some Lua calls outside of
74 * the lua runtime environment. Each Lua call can return a longjmp
75 * if it encounter a memory error.
76 *
77 * Lua documentation extract:
78 *
79 * If an error happens outside any protected environment, Lua calls
80 * a panic function (see lua_atpanic) and then calls abort, thus
81 * exiting the host application. Your panic function can avoid this
82 * exit by never returning (e.g., doing a long jump to your own
83 * recovery point outside Lua).
84 *
85 * The panic function runs as if it were a message handler (see
86 * §2.3); in particular, the error message is at the top of the
87 * stack. However, there is no guarantee about stack space. To push
88 * anything on the stack, the panic function must first check the
89 * available space (see §4.2).
90 *
91 * We must check all the Lua entry point. This includes:
92 * - The include/proto/hlua.h exported functions
93 * - the task wrapper function
94 * - The action wrapper function
95 * - The converters wrapper function
96 * - The sample-fetch wrapper functions
97 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +050098 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080099 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200100 *
101 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
102 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
103 * because they must be exists in the program stack when the longjmp
104 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200105 *
106 * Note that the Lua processing is not really thread safe. It provides
107 * heavy system which consists to add our own lock function in the Lua
108 * code and recompile the library. This system will probably not accepted
109 * by maintainers of various distribs.
110 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500111 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200112 * quick looking on the Lua sources displays a lua_lock() a the start
113 * of function and a lua_unlock() at the end of the function. So I
114 * conclude that the Lua thread safe mode just perform a mutex around
115 * all execution. So I prefer to do this in the HAProxy code, it will be
116 * easier for distro maintainers.
117 *
118 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
119 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
120 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200121 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100122__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200123THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200124static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100125static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126
127#define SET_SAFE_LJMP(__L) \
128 ({ \
129 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200131 if (setjmp(safe_ljmp_env) != 0) { \
132 lua_atpanic(__L, hlua_panic_safe); \
133 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 } else { \
136 lua_atpanic(__L, hlua_panic_ljmp); \
137 ret = 1; \
138 } \
139 ret; \
140 })
141
142/* If we are the last function catching Lua errors, we
143 * must reset the panic function.
144 */
145#define RESET_SAFE_LJMP(__L) \
146 do { \
147 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100148 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200149 } while(0)
150
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200151/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200152#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100153/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200155/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100156#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100157#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100159/* The main Lua execution context. */
160struct hlua gL;
161
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100162/* This is the memory pool containing struct lua for applets
163 * (including cli).
164 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100165DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100166
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100167/* Used for Socket connection. */
168static struct proxy socket_proxy;
169static struct server socket_tcp;
170#ifdef USE_OPENSSL
171static struct server socket_ssl;
172#endif
173
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100174/* List head of the function called at the initialisation time. */
175struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
176
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100177/* The following variables contains the reference of the different
178 * Lua classes. These references are useful for identify metadata
179 * associated with an object.
180 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100181static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100182static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100183static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100184static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100185static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100186static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200187static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200188static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200189static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100190static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100191
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100192/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200193 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194 * short timeout. Lua linked with tasks doesn't have a timeout
195 * because a task may remain alive during all the haproxy execution.
196 */
197static unsigned int hlua_timeout_session = 4000; /* session timeout. */
198static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200199static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100200
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100201/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
202 * it is used for preventing infinite loops.
203 *
204 * I test the scheer with an infinite loop containing one incrementation
205 * and one test. I run this loop between 10 seconds, I raise a ceil of
206 * 710M loops from one interrupt each 9000 instructions, so I fix the value
207 * to one interrupt each 10 000 instructions.
208 *
209 * configured | Number of
210 * instructions | loops executed
211 * between two | in milions
212 * forced yields |
213 * ---------------+---------------
214 * 10 | 160
215 * 500 | 670
216 * 1000 | 680
217 * 5000 | 700
218 * 7000 | 700
219 * 8000 | 700
220 * 9000 | 710 <- ceil
221 * 10000 | 710
222 * 100000 | 710
223 * 1000000 | 710
224 *
225 */
226static unsigned int hlua_nb_instruction = 10000;
227
Willy Tarreaucdb53462020-12-02 12:12:00 +0100228/* Descriptor for the memory allocation state. The limit is pre-initialised to
229 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
230 * is replaced with ~0 during post_init after everything was loaded. This way
231 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
232 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100233 */
234struct hlua_mem_allocator {
235 size_t allocated;
236 size_t limit;
237};
238
Willy Tarreaucdb53462020-12-02 12:12:00 +0100239static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100240
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100241/* These functions converts types between HAProxy internal args or
242 * sample and LUA types. Another function permits to check if the
243 * LUA stack contains arguments according with an required ARG_T
244 * format.
245 */
246static int hlua_arg2lua(lua_State *L, const struct arg *arg);
247static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100248__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100249 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100250static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100251static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100252static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
253
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100254__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200255
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200256#define SEND_ERR(__be, __fmt, __args...) \
257 do { \
258 send_log(__be, LOG_ERR, __fmt, ## __args); \
259 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100260 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200261 } while (0)
262
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100263/* Used to check an Lua function type in the stack. It creates and
264 * returns a reference of the function. This function throws an
265 * error if the rgument is not a "function".
266 */
267__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
268{
269 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100270 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100271 WILL_LJMP(luaL_argerror(L, argno, msg));
272 }
273 lua_pushvalue(L, argno);
274 return luaL_ref(L, LUA_REGISTRYINDEX);
275}
276
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200277/* Return the string that is of the top of the stack. */
278const char *hlua_get_top_error_string(lua_State *L)
279{
280 if (lua_gettop(L) < 1)
281 return "unknown error";
282 if (lua_type(L, -1) != LUA_TSTRING)
283 return "unknown error";
284 return lua_tostring(L, -1);
285}
286
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200287__LJMP static const char *hlua_traceback(lua_State *L)
288{
289 lua_Debug ar;
290 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200291 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200292 int filled = 0;
293
294 while (lua_getstack(L, level++, &ar)) {
295
296 /* Add separator */
297 if (filled)
298 chunk_appendf(msg, ", ");
299 filled = 1;
300
301 /* Fill fields:
302 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
303 * 'l': fills in the field currentline;
304 * 'n': fills in the field name and namewhat;
305 * 't': fills in the field istailcall;
306 */
307 lua_getinfo(L, "Slnt", &ar);
308
309 /* Append code localisation */
310 if (ar.currentline > 0)
311 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
312 else
313 chunk_appendf(msg, "%s ", ar.short_src);
314
315 /*
316 * Get function name
317 *
318 * if namewhat is no empty, name is defined.
319 * what contains "Lua" for Lua function, "C" for C function,
320 * or "main" for main code.
321 */
322 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
323 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
324
325 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
326 chunk_appendf(msg, "main chunk");
327
328 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
329 chunk_appendf(msg, "C function line %d", ar.linedefined);
330
331 else /* nothing left... */
332 chunk_appendf(msg, "?");
333
334
335 /* Display tailed call */
336 if (ar.istailcall)
337 chunk_appendf(msg, " ...");
338 }
339
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200340 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200341}
342
343
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100344/* This function check the number of arguments available in the
345 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500346 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100347 */
348__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
349{
350 if (lua_gettop(L) == nb)
351 return;
352 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
353}
354
Mark Lakes22154b42018-01-29 14:38:40 -0800355/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100356 * and the line number where the error is encountered.
357 */
358static int hlua_pusherror(lua_State *L, const char *fmt, ...)
359{
360 va_list argp;
361 va_start(argp, fmt);
362 luaL_where(L, 1);
363 lua_pushvfstring(L, fmt, argp);
364 va_end(argp);
365 lua_concat(L, 2);
366 return 1;
367}
368
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100369/* This functions is used with sample fetch and converters. It
370 * converts the HAProxy configuration argument in a lua stack
371 * values.
372 *
373 * It takes an array of "arg", and each entry of the array is
374 * converted and pushed in the LUA stack.
375 */
376static int hlua_arg2lua(lua_State *L, const struct arg *arg)
377{
378 switch (arg->type) {
379 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380 case ARGT_TIME:
381 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100382 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200386 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100387 break;
388
389 case ARGT_IPV4:
390 case ARGT_IPV6:
391 case ARGT_MSK4:
392 case ARGT_MSK6:
393 case ARGT_FE:
394 case ARGT_BE:
395 case ARGT_TAB:
396 case ARGT_SRV:
397 case ARGT_USR:
398 case ARGT_MAP:
399 default:
400 lua_pushnil(L);
401 break;
402 }
403 return 1;
404}
405
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500406/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100407 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500408 * with sample fetch wrappers. The input arguments are given to the
409 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100410 */
411static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
412{
413 switch (lua_type(L, ud)) {
414
415 case LUA_TNUMBER:
416 case LUA_TBOOLEAN:
417 arg->type = ARGT_SINT;
418 arg->data.sint = lua_tointeger(L, ud);
419 break;
420
421 case LUA_TSTRING:
422 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200423 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200424 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200425 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200426 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100427 break;
428
429 case LUA_TUSERDATA:
430 case LUA_TNIL:
431 case LUA_TTABLE:
432 case LUA_TFUNCTION:
433 case LUA_TTHREAD:
434 case LUA_TLIGHTUSERDATA:
435 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200436 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100437 break;
438 }
439 return 1;
440}
441
442/* the following functions are used to convert a struct sample
443 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500444 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100446static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200448 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200451 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 break;
453
454 case SMP_T_BIN:
455 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200456 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
462 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
463 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
464 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
465 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
466 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
467 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
468 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
469 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200470 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100471 break;
472 default:
473 lua_pushnil(L);
474 break;
475 }
476 break;
477
478 case SMP_T_IPV4:
479 case SMP_T_IPV6:
480 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200481 if (sample_casts[smp->data.type][SMP_T_STR] &&
482 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200483 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100484 else
485 lua_pushnil(L);
486 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100487 default:
488 lua_pushnil(L);
489 break;
490 }
491 return 1;
492}
493
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100494/* the following functions are used to convert a struct sample
495 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500496 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497 */
498static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
499{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200500 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501
502 case SMP_T_BIN:
503 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 break;
506
507 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200508 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100509 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
510 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
511 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
512 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
513 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
514 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
515 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
516 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
517 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100519 break;
520 default:
521 lua_pushstring(L, "");
522 break;
523 }
524 break;
525
526 case SMP_T_SINT:
527 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 case SMP_T_IPV4:
529 case SMP_T_IPV6:
530 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200531 if (sample_casts[smp->data.type][SMP_T_STR] &&
532 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200533 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100534 else
535 lua_pushstring(L, "");
536 break;
537 default:
538 lua_pushstring(L, "");
539 break;
540 }
541 return 1;
542}
543
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100544/* the following functions are used to convert an Lua type in a
545 * struct sample. This is useful to provide data from a converter
546 * to the LUA code.
547 */
548static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
549{
550 switch (lua_type(L, ud)) {
551
552 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200554 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100555 break;
556
557
558 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200559 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200560 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 break;
562
563 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100565 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200566 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200567 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200568 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200569 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TUSERDATA:
573 case LUA_TNIL:
574 case LUA_TTABLE:
575 case LUA_TFUNCTION:
576 case LUA_TTHREAD:
577 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200578 case LUA_TNONE:
579 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200580 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200581 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 break;
583 }
584 return 1;
585}
586
Ilya Shipitsind4259502020-04-08 01:07:56 +0500587/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800588 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100590 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500591 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100592 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100594__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100595 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100596{
597 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200598 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100599 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200600 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200601 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200602 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200603 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100604
605 idx = 0;
606 min_arg = ARGM(mask);
607 mask >>= ARGM_BITS;
608
609 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200610 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100611
612 /* Check oversize. */
613 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200614 msg = "Malformed argument mask";
615 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100616 }
617
618 /* Check for mandatory arguments. */
619 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100620 if (idx < min_arg) {
621
622 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200623 if (idx > 0) {
624 msg = "Mandatory argument expected";
625 goto error;
626 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100627
628 /* If first argument have a certain type, some default values
629 * may be used. See the function smp_resolve_args().
630 */
631 switch (mask & ARGT_MASK) {
632
633 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200634 if (!(p->cap & PR_CAP_FE)) {
635 msg = "Mandatory argument expected";
636 goto error;
637 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100638 argp[idx].data.prx = p;
639 argp[idx].type = ARGT_FE;
640 argp[idx+1].type = ARGT_STOP;
641 break;
642
643 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200644 if (!(p->cap & PR_CAP_BE)) {
645 msg = "Mandatory argument expected";
646 goto error;
647 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100648 argp[idx].data.prx = p;
649 argp[idx].type = ARGT_BE;
650 argp[idx+1].type = ARGT_STOP;
651 break;
652
653 case ARGT_TAB:
654 argp[idx].data.prx = p;
655 argp[idx].type = ARGT_TAB;
656 argp[idx+1].type = ARGT_STOP;
657 break;
658
659 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200660 msg = "Mandatory argument expected";
661 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100662 break;
663 }
664 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200665 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100666 }
667
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500668 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100669 if ((mask & ARGT_MASK) == ARGT_STOP &&
670 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200671 msg = "Last argument expected";
672 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100673 }
674
675 if ((mask & ARGT_MASK) == ARGT_STOP &&
676 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200677 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100678 }
679
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200680 /* Convert some argument types. All string in argp[] are for not
681 * duplicated yet.
682 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100683 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100684 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200685 if (argp[idx].type != ARGT_SINT) {
686 msg = "integer expected";
687 goto error;
688 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100689 argp[idx].type = ARGT_SINT;
690 break;
691
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100692 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200693 if (argp[idx].type != ARGT_SINT) {
694 msg = "integer expected";
695 goto error;
696 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200697 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100698 break;
699
700 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200701 if (argp[idx].type != ARGT_SINT) {
702 msg = "integer expected";
703 goto error;
704 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200705 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100706 break;
707
708 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200709 if (argp[idx].type != ARGT_STR) {
710 msg = "string expected";
711 goto error;
712 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200713 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200714 if (!argp[idx].data.prx) {
715 msg = "frontend doesn't exist";
716 goto error;
717 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100718 argp[idx].type = ARGT_FE;
719 break;
720
721 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200722 if (argp[idx].type != ARGT_STR) {
723 msg = "string expected";
724 goto error;
725 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200726 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200727 if (!argp[idx].data.prx) {
728 msg = "backend doesn't exist";
729 goto error;
730 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100731 argp[idx].type = ARGT_BE;
732 break;
733
734 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200735 if (argp[idx].type != ARGT_STR) {
736 msg = "string expected";
737 goto error;
738 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200739 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200740 if (!argp[idx].data.t) {
741 msg = "table doesn't exist";
742 goto error;
743 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100744 argp[idx].type = ARGT_TAB;
745 break;
746
747 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200748 if (argp[idx].type != ARGT_STR) {
749 msg = "string expected";
750 goto error;
751 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200752 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100753 if (sname) {
754 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200755 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200756 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200757 if (!px) {
758 msg = "backend doesn't exist";
759 goto error;
760 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 }
762 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200763 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100764 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100765 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100766 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200767 if (!argp[idx].data.srv) {
768 msg = "server doesn't exist";
769 goto error;
770 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100771 argp[idx].type = ARGT_SRV;
772 break;
773
774 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200775 if (argp[idx].type != ARGT_STR) {
776 msg = "string expected";
777 goto error;
778 }
779 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
780 msg = "invalid IPv4 address";
781 goto error;
782 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 argp[idx].type = ARGT_IPV4;
784 break;
785
786 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200787 if (argp[idx].type == ARGT_SINT)
788 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
789 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200790 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
791 msg = "invalid IPv4 mask";
792 goto error;
793 }
794 }
795 else {
796 msg = "integer or string expected";
797 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200798 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100799 argp[idx].type = ARGT_MSK4;
800 break;
801
802 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200803 if (argp[idx].type != ARGT_STR) {
804 msg = "string expected";
805 goto error;
806 }
807 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
808 msg = "invalid IPv6 address";
809 goto error;
810 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100811 argp[idx].type = ARGT_IPV6;
812 break;
813
814 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200815 if (argp[idx].type == ARGT_SINT)
816 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
817 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200818 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
819 msg = "invalid IPv6 mask";
820 goto error;
821 }
822 }
823 else {
824 msg = "integer or string expected";
825 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200826 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100827 argp[idx].type = ARGT_MSK6;
828 break;
829
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200830 case ARGT_REG:
831 if (argp[idx].type != ARGT_STR) {
832 msg = "string expected";
833 goto error;
834 }
835 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
836 if (!reg) {
837 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
838 argp[idx].data.str.area, err);
839 free(err);
840 goto error;
841 }
842 argp[idx].type = ARGT_REG;
843 argp[idx].data.reg = reg;
844 break;
845
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100846 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200847 if (argp[idx].type != ARGT_STR) {
848 msg = "string expected";
849 goto error;
850 }
851 if (p->uri_auth && p->uri_auth->userlist &&
852 !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area))
853 ul = p->uri_auth->userlist;
854 else
855 ul = auth_find_userlist(argp[idx].data.str.area);
856
857 if (!ul) {
858 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
859 goto error;
860 }
861 argp[idx].type = ARGT_USR;
862 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200863 break;
864
865 case ARGT_STR:
866 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
867 msg = "unable to duplicate string arg";
868 goto error;
869 }
870 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100871 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200872
Christopher Fauletd25d9262020-08-06 11:04:46 +0200873 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200874 msg = "type not yet supported";
875 goto error;
876 break;
877
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100878 }
879
880 /* Check for type of argument. */
881 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200882 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
883 arg_type_names[(mask & ARGT_MASK)],
884 arg_type_names[argp[idx].type & ARGT_MASK]);
885 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100886 }
887
888 /* Next argument. */
889 mask >>= ARGT_BITS;
890 idx++;
891 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200892 return 0;
893
894 error:
895 for (i = 0; i < idx; i++) {
896 if (argp[i].type == ARGT_STR)
897 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200898 else if (argp[i].type == ARGT_REG)
899 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200900 }
901 WILL_LJMP(luaL_argerror(L, first + idx, msg));
902 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100903}
904
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100905/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500906 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100908 *
909 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100910 * - hlua_sethlua : create the association between hlua context and lua_state.
911 */
912static inline struct hlua *hlua_gethlua(lua_State *L)
913{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100914 struct hlua **hlua = lua_getextraspace(L);
915 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100916}
917static inline void hlua_sethlua(struct hlua *hlua)
918{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100919 struct hlua **hlua_store = lua_getextraspace(hlua->T);
920 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100921}
922
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100923/* This function is used to send logs. It try to send on screen (stderr)
924 * and on the default syslog server.
925 */
926static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
927{
928 struct tm tm;
929 char *p;
930
931 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200932 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100933 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200934 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200935 /* Break the message if exceed the buffer size. */
936 *(p-4) = ' ';
937 *(p-3) = '.';
938 *(p-2) = '.';
939 *(p-1) = '.';
940 break;
941 }
Willy Tarreau90807112020-02-25 08:16:33 +0100942 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100943 *p = *msg;
944 else
945 *p = '.';
946 }
947 *p = '\0';
948
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200949 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100950 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +0200951 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
952 return;
953
Willy Tarreaua678b432015-08-28 10:14:59 +0200954 get_localtime(date.tv_sec, &tm);
955 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100956 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200957 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100958 fflush(stderr);
959 }
960}
961
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100962/* This function just ensure that the yield will be always
963 * returned with a timeout and permit to set some flags
964 */
965__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100966 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100967{
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100968 struct hlua *hlua;
969
970 /* Get hlua struct, or NULL if we execute from main lua state */
971 hlua = hlua_gethlua(L);
972 if (!hlua) {
973 return;
974 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100975
976 /* Set the wake timeout. If timeout is required, we set
977 * the expiration time.
978 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200979 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100980
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100981 hlua->flags |= flags;
982
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100983 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200984 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100985}
986
Willy Tarreau87b09662015-04-03 00:22:06 +0200987/* This function initialises the Lua environment stored in the stream.
988 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100989 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200990 *
991 * This function is particular. it initialises a new Lua thread. If the
992 * initialisation fails (example: out of memory error), the lua function
993 * throws an error (longjmp).
994 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100995 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500996 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100997 * threads appear, the safe environment set a lock to ensure only one
998 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500999 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001000 *
1001 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001002 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001003 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001004 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001005 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001006 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001007 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001008 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001009int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001010{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001011 if (!already_safe) {
1012 if (!SET_SAFE_LJMP(gL.T)) {
1013 lua->Tref = LUA_REFNIL;
1014 return 0;
1015 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001016 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001017 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001018 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001019 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001020 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001021 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001022 lua->T = lua_newthread(gL.T);
1023 if (!lua->T) {
1024 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001025 if (!already_safe)
1026 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001027 return 0;
1028 }
1029 hlua_sethlua(lua);
1030 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1031 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001032 if (!already_safe)
1033 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001034 return 1;
1035}
1036
Willy Tarreau87b09662015-04-03 00:22:06 +02001037/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001038 * is destroyed. The destroy also the memory context. The struct "lua"
1039 * is not freed.
1040 */
1041void hlua_ctx_destroy(struct hlua *lua)
1042{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001043 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001044 return;
1045
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001046 if (!lua->T)
1047 goto end;
1048
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001049 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001050 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001051
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001052 if (!SET_SAFE_LJMP(lua->T))
1053 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001054 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001055 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001056
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001057 if (!SET_SAFE_LJMP(gL.T))
1058 return;
1059 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1060 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001061 /* Forces a garbage collecting process. If the Lua program is finished
1062 * without error, we run the GC on the thread pointer. Its freed all
1063 * the unused memory.
1064 * If the thread is finnish with an error or is currently yielded,
1065 * it seems that the GC applied on the thread doesn't clean anything,
1066 * so e run the GC on the main thread.
1067 * NOTE: maybe this action locks all the Lua threads untiml the en of
1068 * the garbage collection.
1069 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001070 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001071 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001072 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001073 lua_gc(gL.T, LUA_GCCOLLECT, 0);
1074 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001075 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001076
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001077 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001078
1079end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001080 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001081}
1082
1083/* This function is used to restore the Lua context when a coroutine
1084 * fails. This function copy the common memory between old coroutine
1085 * and the new coroutine. The old coroutine is destroyed, and its
1086 * replaced by the new coroutine.
1087 * If the flag "keep_msg" is set, the last entry of the old is assumed
1088 * as string error message and it is copied in the new stack.
1089 */
1090static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1091{
1092 lua_State *T;
1093 int new_ref;
1094
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001095 /* New Lua coroutine. */
1096 T = lua_newthread(gL.T);
1097 if (!T)
1098 return 0;
1099
1100 /* Copy last error message. */
1101 if (keep_msg)
1102 lua_xmove(lua->T, T, 1);
1103
1104 /* Copy data between the coroutines. */
1105 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1106 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001107 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001108
1109 /* Destroy old data. */
1110 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1111
1112 /* The thread is garbage collected by Lua. */
1113 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1114
1115 /* Fill the struct with the new coroutine values. */
1116 lua->Mref = new_ref;
1117 lua->T = T;
1118 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1119
1120 /* Set context. */
1121 hlua_sethlua(lua);
1122
1123 return 1;
1124}
1125
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001126void hlua_hook(lua_State *L, lua_Debug *ar)
1127{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001128 struct hlua *hlua;
1129
1130 /* Get hlua struct, or NULL if we execute from main lua state */
1131 hlua = hlua_gethlua(L);
1132 if (!hlua)
1133 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001134
1135 /* Lua cannot yield when its returning from a function,
1136 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001137 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001138 */
1139 if (lua_gethookmask(L) & LUA_MASKRET) {
1140 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1141 return;
1142 }
1143
1144 /* restore the interrupt condition. */
1145 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1146
1147 /* If we interrupt the Lua processing in yieldable state, we yield.
1148 * If the state is not yieldable, trying yield causes an error.
1149 */
1150 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001151 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001152
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001153 /* If we cannot yield, update the clock and check the timeout. */
1154 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001155 hlua->run_time += now_ms - hlua->start_time;
1156 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001157 lua_pushfstring(L, "execution timeout");
1158 WILL_LJMP(lua_error(L));
1159 }
1160
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001161 /* Update the start time. */
1162 hlua->start_time = now_ms;
1163
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001164 /* Try to interrupt the process at the end of the current
1165 * unyieldable function.
1166 */
1167 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001168}
1169
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170/* This function start or resumes the Lua stack execution. If the flag
1171 * "yield_allowed" if no set and the LUA stack execution returns a yield
1172 * The function return an error.
1173 *
1174 * The function can returns 4 values:
1175 * - HLUA_E_OK : The execution is terminated without any errors.
1176 * - HLUA_E_AGAIN : The execution must continue at the next associated
1177 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001178 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001179 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001180 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001181 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001182 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001183 * LUA code.
1184 */
1185static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1186{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001187#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1188 int nres;
1189#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001190 int ret;
1191 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001192 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001194 /* Initialise run time counter. */
1195 if (!HLUA_IS_RUNNING(lua))
1196 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001197
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001198 /* Lock the whole Lua execution. This lock must be before the
1199 * label "resume_execution".
1200 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001201 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001202
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001203resume_execution:
1204
1205 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1206 * instructions. it is used for preventing infinite loops.
1207 */
1208 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1209
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001210 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001211 HLUA_SET_RUN(lua);
1212 HLUA_CLR_CTRLYIELD(lua);
1213 HLUA_CLR_WAKERESWR(lua);
1214 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001215
Christopher Fauletbc275a92020-02-26 14:55:16 +01001216 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001217 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001218 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001219
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001220 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001221#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1222 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1223#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001224 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001225#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001226 switch (ret) {
1227
1228 case LUA_OK:
1229 ret = HLUA_E_OK;
1230 break;
1231
1232 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001233 /* Check if the execution timeout is expired. It it is the case, we
1234 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001235 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001236 tv_update_date(0, 1);
1237 lua->run_time += now_ms - lua->start_time;
1238 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001239 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001240 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001241 break;
1242 }
1243 /* Process the forced yield. if the general yield is not allowed or
1244 * if no task were associated this the current Lua execution
1245 * coroutine, we resume the execution. Else we want to return in the
1246 * scheduler and we want to be waked up again, to continue the
1247 * current Lua execution. So we schedule our own task.
1248 */
1249 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001250 if (!yield_allowed || !lua->task)
1251 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001252 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001253 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001254 if (!yield_allowed) {
1255 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001256 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001257 break;
1258 }
1259 ret = HLUA_E_AGAIN;
1260 break;
1261
1262 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001263
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001264 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001265 * because the errors ares the only one mean to return immediately
1266 * from and lua execution.
1267 */
1268 if (lua->flags & HLUA_EXIT) {
1269 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001270 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001271 break;
1272 }
1273
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001274 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001275 if (!lua_checkstack(lua->T, 1)) {
1276 ret = HLUA_E_ERR;
1277 break;
1278 }
1279 msg = lua_tostring(lua->T, -1);
1280 lua_settop(lua->T, 0); /* Empty the stack. */
1281 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001282 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001283 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001284 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001285 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001286 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001287 ret = HLUA_E_ERRMSG;
1288 break;
1289
1290 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001291 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001292 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001293 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001294 break;
1295
1296 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001297 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001298 if (!lua_checkstack(lua->T, 1)) {
1299 ret = HLUA_E_ERR;
1300 break;
1301 }
1302 msg = lua_tostring(lua->T, -1);
1303 lua_settop(lua->T, 0); /* Empty the stack. */
1304 lua_pop(lua->T, 1);
1305 if (msg)
1306 lua_pushfstring(lua->T, "message handler error: %s", msg);
1307 else
1308 lua_pushfstring(lua->T, "message handler error");
1309 ret = HLUA_E_ERRMSG;
1310 break;
1311
1312 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001313 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001314 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001315 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001316 break;
1317 }
1318
1319 switch (ret) {
1320 case HLUA_E_AGAIN:
1321 break;
1322
1323 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001324 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001325 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001326 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001327 break;
1328
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001329 case HLUA_E_ETMOUT:
1330 case HLUA_E_NOMEM:
1331 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001332 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001333 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001334 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001335 hlua_ctx_renew(lua, 0);
1336 break;
1337
1338 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001339 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001340 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001341 break;
1342 }
1343
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001344 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001345 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001346
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001347 return ret;
1348}
1349
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001350/* This function exit the current code. */
1351__LJMP static int hlua_done(lua_State *L)
1352{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001353 struct hlua *hlua;
1354
1355 /* Get hlua struct, or NULL if we execute from main lua state */
1356 hlua = hlua_gethlua(L);
1357 if (!hlua)
1358 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001359
1360 hlua->flags |= HLUA_EXIT;
1361 WILL_LJMP(lua_error(L));
1362
1363 return 0;
1364}
1365
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001366/* This function is an LUA binding. It provides a function
1367 * for deleting ACL from a referenced ACL file.
1368 */
1369__LJMP static int hlua_del_acl(lua_State *L)
1370{
1371 const char *name;
1372 const char *key;
1373 struct pat_ref *ref;
1374
1375 MAY_LJMP(check_args(L, 2, "del_acl"));
1376
1377 name = MAY_LJMP(luaL_checkstring(L, 1));
1378 key = MAY_LJMP(luaL_checkstring(L, 2));
1379
1380 ref = pat_ref_lookup(name);
1381 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001382 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001383
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001384 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001385 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001386 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001387 return 0;
1388}
1389
1390/* This function is an LUA binding. It provides a function
1391 * for deleting map entry from a referenced map file.
1392 */
1393static int hlua_del_map(lua_State *L)
1394{
1395 const char *name;
1396 const char *key;
1397 struct pat_ref *ref;
1398
1399 MAY_LJMP(check_args(L, 2, "del_map"));
1400
1401 name = MAY_LJMP(luaL_checkstring(L, 1));
1402 key = MAY_LJMP(luaL_checkstring(L, 2));
1403
1404 ref = pat_ref_lookup(name);
1405 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001406 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001407
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001408 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001409 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001410 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001411 return 0;
1412}
1413
1414/* This function is an LUA binding. It provides a function
1415 * for adding ACL pattern from a referenced ACL file.
1416 */
1417static int hlua_add_acl(lua_State *L)
1418{
1419 const char *name;
1420 const char *key;
1421 struct pat_ref *ref;
1422
1423 MAY_LJMP(check_args(L, 2, "add_acl"));
1424
1425 name = MAY_LJMP(luaL_checkstring(L, 1));
1426 key = MAY_LJMP(luaL_checkstring(L, 2));
1427
1428 ref = pat_ref_lookup(name);
1429 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001430 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001431
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001432 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001433 if (pat_ref_find_elt(ref, key) == NULL)
1434 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001435 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001436 return 0;
1437}
1438
1439/* This function is an LUA binding. It provides a function
1440 * for setting map pattern and sample from a referenced map
1441 * file.
1442 */
1443static int hlua_set_map(lua_State *L)
1444{
1445 const char *name;
1446 const char *key;
1447 const char *value;
1448 struct pat_ref *ref;
1449
1450 MAY_LJMP(check_args(L, 3, "set_map"));
1451
1452 name = MAY_LJMP(luaL_checkstring(L, 1));
1453 key = MAY_LJMP(luaL_checkstring(L, 2));
1454 value = MAY_LJMP(luaL_checkstring(L, 3));
1455
1456 ref = pat_ref_lookup(name);
1457 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001458 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001459
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001460 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001461 if (pat_ref_find_elt(ref, key) != NULL)
1462 pat_ref_set(ref, key, value, NULL);
1463 else
1464 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001465 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001466 return 0;
1467}
1468
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001469/* A class is a lot of memory that contain data. This data can be a table,
1470 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001471 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001472 * the name of the object (_G[<name>] = <metable> ).
1473 *
1474 * A metable is a table that modify the standard behavior of a standard
1475 * access to the associated data. The entries of this new metatable are
1476 * defined as is:
1477 *
1478 * http://lua-users.org/wiki/MetatableEvents
1479 *
1480 * __index
1481 *
1482 * we access an absent field in a table, the result is nil. This is
1483 * true, but it is not the whole truth. Actually, such access triggers
1484 * the interpreter to look for an __index metamethod: If there is no
1485 * such method, as usually happens, then the access results in nil;
1486 * otherwise, the metamethod will provide the result.
1487 *
1488 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1489 * the key does not appear in the table, but the metatable has an __index
1490 * property:
1491 *
1492 * - if the value is a function, the function is called, passing in the
1493 * table and the key; the return value of that function is returned as
1494 * the result.
1495 *
1496 * - if the value is another table, the value of the key in that table is
1497 * asked for and returned (and if it doesn't exist in that table, but that
1498 * table's metatable has an __index property, then it continues on up)
1499 *
1500 * - Use "rawget(myTable,key)" to skip this metamethod.
1501 *
1502 * http://www.lua.org/pil/13.4.1.html
1503 *
1504 * __newindex
1505 *
1506 * Like __index, but control property assignment.
1507 *
1508 * __mode - Control weak references. A string value with one or both
1509 * of the characters 'k' and 'v' which specifies that the the
1510 * keys and/or values in the table are weak references.
1511 *
1512 * __call - Treat a table like a function. When a table is followed by
1513 * parenthesis such as "myTable( 'foo' )" and the metatable has
1514 * a __call key pointing to a function, that function is invoked
1515 * (passing any specified arguments) and the return value is
1516 * returned.
1517 *
1518 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1519 * called, if the metatable for myTable has a __metatable
1520 * key, the value of that key is returned instead of the
1521 * actual metatable.
1522 *
1523 * __tostring - Control string representation. When the builtin
1524 * "tostring( myTable )" function is called, if the metatable
1525 * for myTable has a __tostring property set to a function,
1526 * that function is invoked (passing myTable to it) and the
1527 * return value is used as the string representation.
1528 *
1529 * __len - Control table length. When the table length is requested using
1530 * the length operator ( '#' ), if the metatable for myTable has
1531 * a __len key pointing to a function, that function is invoked
1532 * (passing myTable to it) and the return value used as the value
1533 * of "#myTable".
1534 *
1535 * __gc - Userdata finalizer code. When userdata is set to be garbage
1536 * collected, if the metatable has a __gc field pointing to a
1537 * function, that function is first invoked, passing the userdata
1538 * to it. The __gc metamethod is not called for tables.
1539 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1540 *
1541 * Special metamethods for redefining standard operators:
1542 * http://www.lua.org/pil/13.1.html
1543 *
1544 * __add "+"
1545 * __sub "-"
1546 * __mul "*"
1547 * __div "/"
1548 * __unm "!"
1549 * __pow "^"
1550 * __concat ".."
1551 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001552 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001553 * http://www.lua.org/pil/13.2.html
1554 *
1555 * __eq "=="
1556 * __lt "<"
1557 * __le "<="
1558 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001559
1560/*
1561 *
1562 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001563 * Class Map
1564 *
1565 *
1566 */
1567
1568/* Returns a struct hlua_map if the stack entry "ud" is
1569 * a class session, otherwise it throws an error.
1570 */
1571__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1572{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001573 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001574}
1575
1576/* This function is the map constructor. It don't need
1577 * the class Map object. It creates and return a new Map
1578 * object. It must be called only during "body" or "init"
1579 * context because it process some filesystem accesses.
1580 */
1581__LJMP static int hlua_map_new(struct lua_State *L)
1582{
1583 const char *fn;
1584 int match = PAT_MATCH_STR;
1585 struct sample_conv conv;
1586 const char *file = "";
1587 int line = 0;
1588 lua_Debug ar;
1589 char *err = NULL;
1590 struct arg args[2];
1591
1592 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1593 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1594
1595 fn = MAY_LJMP(luaL_checkstring(L, 1));
1596
1597 if (lua_gettop(L) >= 2) {
1598 match = MAY_LJMP(luaL_checkinteger(L, 2));
1599 if (match < 0 || match >= PAT_MATCH_NUM)
1600 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1601 }
1602
1603 /* Get Lua filename and line number. */
1604 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1605 lua_getinfo(L, "Sl", &ar); /* get info about it */
1606 if (ar.currentline > 0) { /* is there info? */
1607 file = ar.short_src;
1608 line = ar.currentline;
1609 }
1610 }
1611
1612 /* fill fake sample_conv struct. */
1613 conv.kw = ""; /* unused. */
1614 conv.process = NULL; /* unused. */
1615 conv.arg_mask = 0; /* unused. */
1616 conv.val_args = NULL; /* unused. */
1617 conv.out_type = SMP_T_STR;
1618 conv.private = (void *)(long)match;
1619 switch (match) {
1620 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1621 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1622 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1623 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1624 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1625 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1626 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001627 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001628 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1629 default:
1630 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1631 }
1632
1633 /* fill fake args. */
1634 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001635 args[0].data.str.area = strdup(fn);
1636 args[0].data.str.data = strlen(fn);
1637 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001638 args[1].type = ARGT_STOP;
1639
1640 /* load the map. */
1641 if (!sample_load_map(args, &conv, file, line, &err)) {
1642 /* error case: we cant use luaL_error because we must
1643 * free the err variable.
1644 */
1645 luaL_where(L, 1);
1646 lua_pushfstring(L, "'new': %s.", err);
1647 lua_concat(L, 2);
1648 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001649 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001650 WILL_LJMP(lua_error(L));
1651 }
1652
1653 /* create the lua object. */
1654 lua_newtable(L);
1655 lua_pushlightuserdata(L, args[0].data.map);
1656 lua_rawseti(L, -2, 0);
1657
1658 /* Pop a class Map metatable and affect it to the userdata. */
1659 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1660 lua_setmetatable(L, -2);
1661
1662
1663 return 1;
1664}
1665
1666__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1667{
1668 struct map_descriptor *desc;
1669 struct pattern *pat;
1670 struct sample smp;
1671
1672 MAY_LJMP(check_args(L, 2, "lookup"));
1673 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001674 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001675 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001676 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001677 }
1678 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001679 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001680 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001681 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry Fournier91dc0c02020-11-10 20:38:20 +01001682 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001683 }
1684
1685 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001686 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001687 if (str)
1688 lua_pushstring(L, "");
1689 else
1690 lua_pushnil(L);
1691 return 1;
1692 }
1693
1694 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001695 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001696 return 1;
1697}
1698
1699__LJMP static int hlua_map_lookup(struct lua_State *L)
1700{
1701 return _hlua_map_lookup(L, 0);
1702}
1703
1704__LJMP static int hlua_map_slookup(struct lua_State *L)
1705{
1706 return _hlua_map_lookup(L, 1);
1707}
1708
1709/*
1710 *
1711 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001712 * Class Socket
1713 *
1714 *
1715 */
1716
1717__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1718{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001719 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720}
1721
1722/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001723 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001724 * received.
1725 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001726static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001728 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001729
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001730 if (appctx->ctx.hlua_cosocket.die) {
1731 si_shutw(si);
1732 si_shutr(si);
1733 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001734 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1735 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001736 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1737 }
1738
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001739 /* If we cant write, wakeup the pending write signals. */
1740 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001741 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001742
1743 /* If we cant read, wakeup the pending read signals. */
1744 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001745 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001746
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001747 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001748 * to be notified whenever the connection completes.
1749 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001750 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001751 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001752 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001753 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001754 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001755 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001756
1757 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001758 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001759
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001760 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001761 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001762 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001763
1764 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001765 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001766 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001767
1768 /* Some data were injected in the buffer, notify the stream
1769 * interface.
1770 */
1771 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001772 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001773
1774 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001775 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001776 */
1777 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001778 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001779}
1780
Willy Tarreau87b09662015-04-03 00:22:06 +02001781/* This function is called when the "struct stream" is destroyed.
1782 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783 * Wake all the pending signals.
1784 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001785static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001786{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001787 struct xref *peer;
1788
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001790 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1791 if (peer)
1792 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793
1794 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001795 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1796 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001797}
1798
1799/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001800 * uses this object. If the stream does not exists, just quit.
1801 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001802 * pending signal can rest in the read and write lists. destroy
1803 * it.
1804 */
1805__LJMP static int hlua_socket_gc(lua_State *L)
1806{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001807 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001808 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001809 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001810
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001811 MAY_LJMP(check_args(L, 1, "__gc"));
1812
1813 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001814 peer = xref_get_peer_and_lock(&socket->xref);
1815 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001817 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001819 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001820 appctx->ctx.hlua_cosocket.die = 1;
1821 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001823 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001824 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 return 0;
1826}
1827
1828/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001829 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830 */
sada05ed3302018-05-11 11:48:18 -07001831__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001833 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001835 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001836 struct hlua *hlua;
1837
1838 /* Get hlua struct, or NULL if we execute from main lua state */
1839 hlua = hlua_gethlua(L);
1840 if (!hlua)
1841 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001842
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001843 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001844
1845 /* Check if we run on the same thread than the xreator thread.
1846 * We cannot access to the socket if the thread is different.
1847 */
1848 if (socket->tid != tid)
1849 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1850
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001851 peer = xref_get_peer_and_lock(&socket->xref);
1852 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001854
1855 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001856 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001858 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001859 appctx->ctx.hlua_cosocket.die = 1;
1860 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001862 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001863 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001864 return 0;
1865}
1866
sada05ed3302018-05-11 11:48:18 -07001867/* The close function calls close_helper.
1868 */
1869__LJMP static int hlua_socket_close(lua_State *L)
1870{
1871 MAY_LJMP(check_args(L, 1, "close"));
1872 return hlua_socket_close_helper(L);
1873}
1874
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001875/* This Lua function assumes that the stack contain three parameters.
1876 * 1 - USERDATA containing a struct socket
1877 * 2 - INTEGER with values of the macro defined below
1878 * If the integer is -1, we must read at most one line.
1879 * If the integer is -2, we ust read all the data until the
1880 * end of the stream.
1881 * If the integer is positive value, we must read a number of
1882 * bytes corresponding to this value.
1883 */
1884#define HLSR_READ_LINE (-1)
1885#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001886__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001887{
1888 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1889 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001890 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001892 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001894 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001895 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001896 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001897 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001898 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001899 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001900 struct stream_interface *si;
1901 struct stream *s;
1902 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001903 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001905 /* Get hlua struct, or NULL if we execute from main lua state */
1906 hlua = hlua_gethlua(L);
1907
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 /* Check if this lua stack is schedulable. */
1909 if (!hlua || !hlua->task)
1910 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1911 "'frontend', 'backend' or 'task'"));
1912
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001913 /* Check if we run on the same thread than the xreator thread.
1914 * We cannot access to the socket if the thread is different.
1915 */
1916 if (socket->tid != tid)
1917 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1918
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001919 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001920 peer = xref_get_peer_and_lock(&socket->xref);
1921 if (!peer)
1922 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001923 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1924 si = appctx->owner;
1925 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001926
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001927 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001929 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001930 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931 if (nblk < 0) /* Connection close. */
1932 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001933 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001935
1936 /* remove final \r\n. */
1937 if (nblk == 1) {
1938 if (blk1[len1-1] == '\n') {
1939 len1--;
1940 skip_at_end++;
1941 if (blk1[len1-1] == '\r') {
1942 len1--;
1943 skip_at_end++;
1944 }
1945 }
1946 }
1947 else {
1948 if (blk2[len2-1] == '\n') {
1949 len2--;
1950 skip_at_end++;
1951 if (blk2[len2-1] == '\r') {
1952 len2--;
1953 skip_at_end++;
1954 }
1955 }
1956 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957 }
1958
1959 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001961 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 if (nblk < 0) /* Connection close. */
1963 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001964 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 goto connection_empty;
1966 }
1967
1968 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001970 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971 if (nblk < 0) /* Connection close. */
1972 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001973 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974 goto connection_empty;
1975
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001976 missing_bytes = wanted - socket->b.n;
1977 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001978 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001979 len1 = missing_bytes;
1980 } if (nblk == 2 && len1 + len2 > missing_bytes)
1981 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982 }
1983
1984 len = len1;
1985
1986 luaL_addlstring(&socket->b, blk1, len1);
1987 if (nblk == 2) {
1988 len += len2;
1989 luaL_addlstring(&socket->b, blk2, len2);
1990 }
1991
1992 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001993 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001994
1995 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001996 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997
1998 /* If the pattern reclaim to read all the data
1999 * in the connection, got out.
2000 */
2001 if (wanted == HLSR_READ_ALL)
2002 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002003 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002004 goto connection_empty;
2005
2006 /* Return result. */
2007 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002008 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002009 return 1;
2010
2011connection_closed:
2012
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002013 xref_unlock(&socket->xref, peer);
2014
2015no_peer:
2016
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 /* If the buffer containds data. */
2018 if (socket->b.n > 0) {
2019 luaL_pushresult(&socket->b);
2020 return 1;
2021 }
2022 lua_pushnil(L);
2023 lua_pushstring(L, "connection closed.");
2024 return 2;
2025
2026connection_empty:
2027
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002028 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2029 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002031 }
2032 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002033 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034 return 0;
2035}
2036
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002037/* This Lua function gets two parameters. The first one can be string
2038 * or a number. If the string is "*l", the user requires one line. If
2039 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002040 * If the value is a number, the user require a number of bytes equal
2041 * to the value. The default value is "*l" (a line).
2042 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002043 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002044 * integer takes this values:
2045 * -1 : read a line
2046 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002047 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002049 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002050 * concatenated with the read data.
2051 */
2052__LJMP static int hlua_socket_receive(struct lua_State *L)
2053{
2054 int wanted = HLSR_READ_LINE;
2055 const char *pattern;
2056 int type;
2057 char *error;
2058 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002059 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060
2061 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2062 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2063
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002064 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002065
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002066 /* Check if we run on the same thread than the xreator thread.
2067 * We cannot access to the socket if the thread is different.
2068 */
2069 if (socket->tid != tid)
2070 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2071
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002072 /* check for pattern. */
2073 if (lua_gettop(L) >= 2) {
2074 type = lua_type(L, 2);
2075 if (type == LUA_TSTRING) {
2076 pattern = lua_tostring(L, 2);
2077 if (strcmp(pattern, "*a") == 0)
2078 wanted = HLSR_READ_ALL;
2079 else if (strcmp(pattern, "*l") == 0)
2080 wanted = HLSR_READ_LINE;
2081 else {
2082 wanted = strtoll(pattern, &error, 10);
2083 if (*error != '\0')
2084 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2085 }
2086 }
2087 else if (type == LUA_TNUMBER) {
2088 wanted = lua_tointeger(L, 2);
2089 if (wanted < 0)
2090 WILL_LJMP(luaL_error(L, "Unsupported size."));
2091 }
2092 }
2093
2094 /* Set pattern. */
2095 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002096
2097 /* Check if we would replace the top by itself. */
2098 if (lua_gettop(L) != 2)
2099 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002101 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 luaL_buffinit(L, &socket->b);
2103
2104 /* Check prefix. */
2105 if (lua_gettop(L) >= 3) {
2106 if (lua_type(L, 3) != LUA_TSTRING)
2107 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2108 pattern = lua_tolstring(L, 3, &len);
2109 luaL_addlstring(&socket->b, pattern, len);
2110 }
2111
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002112 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002113}
2114
2115/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002116 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002117 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002118static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119{
2120 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002121 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002122 struct appctx *appctx;
2123 size_t buf_len;
2124 const char *buf;
2125 int len;
2126 int send_len;
2127 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002128 struct xref *peer;
2129 struct stream_interface *si;
2130 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002131
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002132 /* Get hlua struct, or NULL if we execute from main lua state */
2133 hlua = hlua_gethlua(L);
2134
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002135 /* Check if this lua stack is schedulable. */
2136 if (!hlua || !hlua->task)
2137 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2138 "'frontend', 'backend' or 'task'"));
2139
2140 /* Get object */
2141 socket = MAY_LJMP(hlua_checksocket(L, 1));
2142 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002143 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002144
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002145 /* Check if we run on the same thread than the xreator thread.
2146 * We cannot access to the socket if the thread is different.
2147 */
2148 if (socket->tid != tid)
2149 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2150
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002151 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002152 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002153 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002154 lua_pushinteger(L, -1);
2155 return 1;
2156 }
2157 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2158 si = appctx->owner;
2159 s = si_strm(si);
2160
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002161 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002162 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002163 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002164 lua_pushinteger(L, -1);
2165 return 1;
2166 }
2167
2168 /* Update the input buffer data. */
2169 buf += sent;
2170 send_len = buf_len - sent;
2171
2172 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002173 if (sent >= buf_len) {
2174 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002176 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002178 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002179 * the request buffer if its not required.
2180 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002181 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002182 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002183 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002184 }
2185
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002186 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002187 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002188 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002190 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002191
2192 /* send data */
2193 if (len < send_len)
2194 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002195 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002196
2197 /* "Not enough space" (-1), "Buffer too little to contain
2198 * the data" (-2) are not expected because the available length
2199 * is tested.
2200 * Other unknown error are also not expected.
2201 */
2202 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002203 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002204 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002205
sada05ed3302018-05-11 11:48:18 -07002206 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002207 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002208 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002209 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002210 return 1;
2211 }
2212
2213 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002214 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002215
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002216 s->req.rex = TICK_ETERNITY;
2217 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002218
2219 /* Update length sent. */
2220 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002221 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002222
2223 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002224 if (sent + len >= buf_len) {
2225 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002226 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002227 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228
2229hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002230 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2231 xref_unlock(&socket->xref, peer);
2232 WILL_LJMP(luaL_error(L, "out of memory"));
2233 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002234 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002235 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002236 return 0;
2237}
2238
2239/* This function initiate the send of data. It just check the input
2240 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002241 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002242 * "hlua_socket_write_yield" that can yield.
2243 *
2244 * The Lua function gets between 3 and 4 parameters. The first one is
2245 * the associated object. The second is a string buffer. The third is
2246 * a facultative integer that represents where is the buffer position
2247 * of the start of the data that can send. The first byte is the
2248 * position "1". The default value is "1". The fourth argument is a
2249 * facultative integer that represents where is the buffer position
2250 * of the end of the data that can send. The default is the last byte.
2251 */
2252static int hlua_socket_send(struct lua_State *L)
2253{
2254 int i;
2255 int j;
2256 const char *buf;
2257 size_t buf_len;
2258
2259 /* Check number of arguments. */
2260 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2261 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2262
2263 /* Get the string. */
2264 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2265
2266 /* Get and check j. */
2267 if (lua_gettop(L) == 4) {
2268 j = MAY_LJMP(luaL_checkinteger(L, 4));
2269 if (j < 0)
2270 j = buf_len + j + 1;
2271 if (j > buf_len)
2272 j = buf_len + 1;
2273 lua_pop(L, 1);
2274 }
2275 else
2276 j = buf_len;
2277
2278 /* Get and check i. */
2279 if (lua_gettop(L) == 3) {
2280 i = MAY_LJMP(luaL_checkinteger(L, 3));
2281 if (i < 0)
2282 i = buf_len + i + 1;
2283 if (i > buf_len)
2284 i = buf_len + 1;
2285 lua_pop(L, 1);
2286 } else
2287 i = 1;
2288
2289 /* Check bth i and j. */
2290 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002291 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292 return 1;
2293 }
2294 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002295 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002296 return 1;
2297 }
2298 if (i == 0)
2299 i = 1;
2300 if (j == 0)
2301 j = 1;
2302
2303 /* Pop the string. */
2304 lua_pop(L, 1);
2305
2306 /* Update the buffer length. */
2307 buf += i - 1;
2308 buf_len = j - i + 1;
2309 lua_pushlstring(L, buf, buf_len);
2310
2311 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002312 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002313
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002314 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002315}
2316
Willy Tarreau22b0a682015-06-17 19:43:49 +02002317#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2319{
2320 static char buffer[SOCKET_INFO_MAX_LEN];
2321 int ret;
2322 int len;
2323 char *p;
2324
2325 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2326 if (ret <= 0) {
2327 lua_pushnil(L);
2328 return 1;
2329 }
2330
2331 if (ret == AF_UNIX) {
2332 lua_pushstring(L, buffer+1);
2333 return 1;
2334 }
2335 else if (ret == AF_INET6) {
2336 buffer[0] = '[';
2337 len = strlen(buffer);
2338 buffer[len] = ']';
2339 len++;
2340 buffer[len] = ':';
2341 len++;
2342 p = buffer;
2343 }
2344 else if (ret == AF_INET) {
2345 p = buffer + 1;
2346 len = strlen(p);
2347 p[len] = ':';
2348 len++;
2349 }
2350 else {
2351 lua_pushnil(L);
2352 return 1;
2353 }
2354
2355 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2356 lua_pushnil(L);
2357 return 1;
2358 }
2359
2360 lua_pushstring(L, p);
2361 return 1;
2362}
2363
2364/* Returns information about the peer of the connection. */
2365__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2366{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002367 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002368 struct xref *peer;
2369 struct appctx *appctx;
2370 struct stream_interface *si;
2371 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002372 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002373
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374 MAY_LJMP(check_args(L, 1, "getpeername"));
2375
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002376 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002377
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002378 /* Check if we run on the same thread than the xreator thread.
2379 * We cannot access to the socket if the thread is different.
2380 */
2381 if (socket->tid != tid)
2382 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2383
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002384 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002385 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002386 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387 lua_pushnil(L);
2388 return 1;
2389 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002390 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2391 si = appctx->owner;
2392 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002393
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002394 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002395 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002396 lua_pushnil(L);
2397 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002398 }
2399
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002400 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002401 xref_unlock(&socket->xref, peer);
2402 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403}
2404
2405/* Returns information about my connection side. */
2406static int hlua_socket_getsockname(struct lua_State *L)
2407{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002408 struct hlua_socket *socket;
2409 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002410 struct appctx *appctx;
2411 struct xref *peer;
2412 struct stream_interface *si;
2413 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002414 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002415
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416 MAY_LJMP(check_args(L, 1, "getsockname"));
2417
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002418 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002419
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002420 /* Check if we run on the same thread than the xreator thread.
2421 * We cannot access to the socket if the thread is different.
2422 */
2423 if (socket->tid != tid)
2424 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2425
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002426 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002427 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002428 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002429 lua_pushnil(L);
2430 return 1;
2431 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002432 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2433 si = appctx->owner;
2434 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002435
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002436 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002437 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002438 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002439 lua_pushnil(L);
2440 return 1;
2441 }
2442
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002443 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002444 xref_unlock(&socket->xref, peer);
2445 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002446}
2447
2448/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002449static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002450 .obj_type = OBJ_TYPE_APPLET,
2451 .name = "<LUA_TCP>",
2452 .fct = hlua_socket_handler,
2453 .release = hlua_socket_release,
2454};
2455
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002456__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002457{
2458 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002459 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002460 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002461 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002462 struct stream_interface *si;
2463 struct stream *s;
2464
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002465 /* Get hlua struct, or NULL if we execute from main lua state */
2466 hlua = hlua_gethlua(L);
2467 if (!hlua)
2468 return 0;
2469
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002470 /* Check if we run on the same thread than the xreator thread.
2471 * We cannot access to the socket if the thread is different.
2472 */
2473 if (socket->tid != tid)
2474 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2475
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002476 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002477 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002478 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002479 lua_pushnil(L);
2480 lua_pushstring(L, "Can't connect");
2481 return 2;
2482 }
2483 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2484 si = appctx->owner;
2485 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002486
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002487 /* Check if we run on the same thread than the xreator thread.
2488 * We cannot access to the socket if the thread is different.
2489 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002490 if (socket->tid != tid) {
2491 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002492 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002493 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002494
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002495 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002496 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002497 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002498 lua_pushnil(L);
2499 lua_pushstring(L, "Can't connect");
2500 return 2;
2501 }
2502
Willy Tarreaue09101e2018-10-16 17:37:12 +02002503 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002504
2505 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002506 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002507 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002508 lua_pushinteger(L, 1);
2509 return 1;
2510 }
2511
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002512 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2513 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002514 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002515 }
2516 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002517 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002518 return 0;
2519}
2520
2521/* This function fail or initite the connection. */
2522__LJMP static int hlua_socket_connect(struct lua_State *L)
2523{
2524 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002525 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002526 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002527 struct hlua *hlua;
2528 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002529 int low, high;
2530 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002531 struct xref *peer;
2532 struct stream_interface *si;
2533 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002534
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002535 if (lua_gettop(L) < 2)
2536 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002537
2538 /* Get args. */
2539 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002540
2541 /* Check if we run on the same thread than the xreator thread.
2542 * We cannot access to the socket if the thread is different.
2543 */
2544 if (socket->tid != tid)
2545 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2546
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002547 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002548 if (lua_gettop(L) >= 3) {
2549 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002550 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002551
Tim Duesterhus6edab862018-01-06 19:04:45 +01002552 /* Force the ip to end with a colon, to support IPv6 addresses
2553 * that are not enclosed within square brackets.
2554 */
2555 if (port > 0) {
2556 luaL_buffinit(L, &b);
2557 luaL_addstring(&b, ip);
2558 luaL_addchar(&b, ':');
2559 luaL_pushresult(&b);
2560 ip = lua_tolstring(L, lua_gettop(L), NULL);
2561 }
2562 }
2563
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002564 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002565 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002566 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002567 lua_pushnil(L);
2568 return 1;
2569 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002570
2571 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002572 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 +02002573 if (!addr) {
2574 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002575 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002576 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002577
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002578 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002579 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002580 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002581 if (port == -1) {
2582 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002583 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002584 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002585 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2586 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002587 if (port == -1) {
2588 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002589 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002590 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002591 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002592 }
2593 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002594
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002595 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2596 si = appctx->owner;
2597 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002598
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002599 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002600 xref_unlock(&socket->xref, peer);
2601 WILL_LJMP(luaL_error(L, "connect: internal error"));
2602 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002603 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002604
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002605 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002606 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002607 if (!hlua)
2608 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002609
2610 /* inform the stream that we want to be notified whenever the
2611 * connection completes.
2612 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002613 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002614 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002615 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002616
Willy Tarreauf31af932020-01-14 09:59:38 +01002617 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002618
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002619 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2620 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002621 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002622 }
2623 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002624
2625 task_wakeup(s->task, TASK_WOKEN_INIT);
2626 /* Return yield waiting for connection. */
2627
Willy Tarreau9635e032018-10-16 17:52:55 +02002628 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002629
2630 return 0;
2631}
2632
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002633#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002634__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2635{
2636 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002637 struct xref *peer;
2638 struct appctx *appctx;
2639 struct stream_interface *si;
2640 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641
2642 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2643 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002644
2645 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002646 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002647 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002648 lua_pushnil(L);
2649 return 1;
2650 }
2651 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2652 si = appctx->owner;
2653 s = si_strm(si);
2654
2655 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002656 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002657 return MAY_LJMP(hlua_socket_connect(L));
2658}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002659#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002660
2661__LJMP static int hlua_socket_setoption(struct lua_State *L)
2662{
2663 return 0;
2664}
2665
2666__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2667{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002668 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002669 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002670 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002671 struct xref *peer;
2672 struct appctx *appctx;
2673 struct stream_interface *si;
2674 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002675
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676 MAY_LJMP(check_args(L, 2, "settimeout"));
2677
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002678 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002679
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002680 /* convert the timeout to millis */
2681 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002682
Thierry Fournier17a921b2018-03-08 09:59:02 +01002683 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002684 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002685 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2686
Mark Lakes56cc1252018-03-27 09:48:06 +02002687 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002688 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002689
2690 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002691 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002692 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002693
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002694 /* Check if we run on the same thread than the xreator thread.
2695 * We cannot access to the socket if the thread is different.
2696 */
2697 if (socket->tid != tid)
2698 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2699
Mark Lakes56cc1252018-03-27 09:48:06 +02002700 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002701 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002702 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002703 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2704 WILL_LJMP(lua_error(L));
2705 return 0;
2706 }
2707 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2708 si = appctx->owner;
2709 s = si_strm(si);
2710
Cyril Bonté7bb63452018-08-17 23:51:02 +02002711 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002712 s->req.rto = tmout;
2713 s->req.wto = tmout;
2714 s->res.rto = tmout;
2715 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002716 s->req.rex = tick_add_ifset(now_ms, tmout);
2717 s->req.wex = tick_add_ifset(now_ms, tmout);
2718 s->res.rex = tick_add_ifset(now_ms, tmout);
2719 s->res.wex = tick_add_ifset(now_ms, tmout);
2720
2721 s->task->expire = tick_add_ifset(now_ms, tmout);
2722 task_queue(s->task);
2723
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002724 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002725
Thierry Fourniere9636f12018-03-08 09:54:32 +01002726 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002727 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002728}
2729
2730__LJMP static int hlua_socket_new(lua_State *L)
2731{
2732 struct hlua_socket *socket;
2733 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002734 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002735 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002736
2737 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002738 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002739 hlua_pusherror(L, "socket: full stack");
2740 goto out_fail_conf;
2741 }
2742
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002743 /* Create the object: obj[0] = userdata. */
2744 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002745 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002746 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002747 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002748 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002749
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002750 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002751 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002752 hlua_pusherror(L, "socket: uninitialized pools.");
2753 goto out_fail_conf;
2754 }
2755
Willy Tarreau87b09662015-04-03 00:22:06 +02002756 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002757 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2758 lua_setmetatable(L, -2);
2759
Willy Tarreaud420a972015-04-06 00:39:18 +02002760 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002761 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002762 if (!appctx) {
2763 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002764 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002765 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002766
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002767 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002768 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002769 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2770 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002771
Willy Tarreaud420a972015-04-06 00:39:18 +02002772 /* Now create a session, task and stream for this applet */
2773 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2774 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002775 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002776 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002777 }
2778
Willy Tarreau87787ac2017-08-28 16:22:54 +02002779 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002780 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002781 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002782 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002783 }
2784
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002785 /* Initialise cross reference between stream and Lua socket object. */
2786 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002787
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002788 /* Configure "right" stream interface. this "si" is used to connect
2789 * and retrieve data from the server. The connection is initialized
2790 * with the "struct server".
2791 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002792 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002793
2794 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002795 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002796 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002797
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002798 return 1;
2799
Willy Tarreaud420a972015-04-06 00:39:18 +02002800 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002801 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002802 out_fail_sess:
2803 appctx_free(appctx);
2804 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002805 WILL_LJMP(lua_error(L));
2806 return 0;
2807}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002808
2809/*
2810 *
2811 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002812 * Class Channel
2813 *
2814 *
2815 */
2816
2817/* Returns the struct hlua_channel join to the class channel in the
2818 * stack entry "ud" or throws an argument error.
2819 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002820__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002821{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002822 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002823}
2824
Willy Tarreau47860ed2015-03-10 14:07:50 +01002825/* Pushes the channel onto the top of the stack. If the stask does not have a
2826 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002827 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002828static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002829{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002830 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002831 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002832 return 0;
2833
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002834 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002835 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002836 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002837
2838 /* Pop a class sesison metatable and affect it to the userdata. */
2839 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2840 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002841 return 1;
2842}
2843
2844/* Duplicate all the data present in the input channel and put it
2845 * in a string LUA variables. Returns -1 and push a nil value in
2846 * the stack if the channel is closed and all the data are consumed,
2847 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002848 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002849 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002850static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002851{
2852 char *blk1;
2853 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002854 size_t len1;
2855 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002856 int ret;
2857 luaL_Buffer b;
2858
Willy Tarreau06d80a92017-10-19 14:32:15 +02002859 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002860 if (unlikely(ret == 0))
2861 return 0;
2862
2863 if (unlikely(ret < 0)) {
2864 lua_pushnil(L);
2865 return -1;
2866 }
2867
2868 luaL_buffinit(L, &b);
2869 luaL_addlstring(&b, blk1, len1);
2870 if (unlikely(ret == 2))
2871 luaL_addlstring(&b, blk2, len2);
2872 luaL_pushresult(&b);
2873
2874 if (unlikely(ret == 2))
2875 return len1 + len2;
2876 return len1;
2877}
2878
2879/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2880 * a yield. This function keep the data in the buffer.
2881 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002882__LJMP static int hlua_channel_dup_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
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002886 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2887
Thierry Fournier77016da2020-08-15 14:35:51 +02002888 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2889 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002890 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002891 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002892
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002894 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895 return 1;
2896}
2897
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002898/* Check arguments for the function "hlua_channel_dup_yield". */
2899__LJMP static int hlua_channel_dup(lua_State *L)
2900{
2901 MAY_LJMP(check_args(L, 1, "dup"));
2902 MAY_LJMP(hlua_checkchannel(L, 1));
2903 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2904}
2905
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002906/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2907 * a yield. This function consumes the data in the buffer. It returns
2908 * a string containing the data or a nil pointer if no data are available
2909 * and the channel is closed.
2910 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002911__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002913 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002914 int ret;
2915
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002916 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002917
Thierry Fournier77016da2020-08-15 14:35:51 +02002918 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2919 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002920 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002921 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002922
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002923 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002924 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002925 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002926
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 if (unlikely(ret == -1))
2928 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002930 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931 return 1;
2932}
2933
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002934/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002935__LJMP static int hlua_channel_get(lua_State *L)
2936{
2937 MAY_LJMP(check_args(L, 1, "get"));
2938 MAY_LJMP(hlua_checkchannel(L, 1));
2939 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2940}
2941
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002942/* This functions consumes and returns one line. If the channel is closed,
2943 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002944 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945 * value.
2946 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002947__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949 char *blk1;
2950 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002951 size_t len1;
2952 size_t len2;
2953 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002954 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955 int ret;
2956 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002957
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002958 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2959
Thierry Fournier77016da2020-08-15 14:35:51 +02002960 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2961 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002962 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002963 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002964
Willy Tarreau06d80a92017-10-19 14:32:15 +02002965 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002966 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002967 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002968
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002969 if (ret == -1) {
2970 lua_pushnil(L);
2971 return 1;
2972 }
2973
2974 luaL_buffinit(L, &b);
2975 luaL_addlstring(&b, blk1, len1);
2976 len = len1;
2977 if (unlikely(ret == 2)) {
2978 luaL_addlstring(&b, blk2, len2);
2979 len += len2;
2980 }
2981 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002982 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 return 1;
2984}
2985
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002986/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002987__LJMP static int hlua_channel_getline(lua_State *L)
2988{
2989 MAY_LJMP(check_args(L, 1, "getline"));
2990 MAY_LJMP(hlua_checkchannel(L, 1));
2991 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2992}
2993
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994/* This function takes a string as input, and append it at the
2995 * input side of channel. If the data is too big, but a space
2996 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002997 * yields. If the data is bigger than the buffer, or if the
2998 * channel is closed, it returns -1. Otherwise, it returns the
2999 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003001__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003003 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003004 size_t len;
3005 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3006 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3007 int ret;
3008 int max;
3009
Thierry Fournier77016da2020-08-15 14:35:51 +02003010 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3011 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003012 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003013 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003014
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003015 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003016 * the request buffer if its not required.
3017 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003018 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003019 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003020 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003021 }
3022
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003023 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 if (max > len - l)
3025 max = len - l;
3026
Willy Tarreau06d80a92017-10-19 14:32:15 +02003027 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003028 if (ret == -2 || ret == -3) {
3029 lua_pushinteger(L, -1);
3030 return 1;
3031 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003032 if (ret == -1) {
3033 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003034 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003035 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036 l += ret;
3037 lua_pop(L, 1);
3038 lua_pushinteger(L, l);
3039
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003040 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003041 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003042 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003043 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003044 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003045 */
3046 return 1;
3047 }
3048 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003049 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050 return 1;
3051}
3052
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003053/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3054 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003055 * buffer size is too little for the data.
3056 */
3057__LJMP static int hlua_channel_append(lua_State *L)
3058{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003059 size_t len;
3060
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003062 MAY_LJMP(hlua_checkchannel(L, 1));
3063 MAY_LJMP(luaL_checklstring(L, 2, &len));
3064 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065 lua_pushinteger(L, 0);
3066
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003067 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003068}
3069
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003070/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003072 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073 * or -1 if the channel is closed or if the buffer size is too
3074 * little for the data.
3075 */
3076__LJMP static int hlua_channel_set(lua_State *L)
3077{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003078 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003079
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003080 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003081 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003082 lua_pushinteger(L, 0);
3083
Thierry Fournier77016da2020-08-15 14:35:51 +02003084 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3085 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003086 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003087 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003088
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003089 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003090
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003091 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003092}
3093
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003094/* Append data in the output side of the buffer. This data is immediately
3095 * sent. The function returns the amount of data written. If the buffer
3096 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003097 * if the channel is closed.
3098 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003099__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003100{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003101 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003102 size_t len;
3103 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3104 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3105 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003106 struct hlua *hlua;
3107
3108 /* Get hlua struct, or NULL if we execute from main lua state */
3109 hlua = hlua_gethlua(L);
3110 if (!hlua) {
3111 lua_pushnil(L);
3112 return 1;
3113 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003114
Thierry Fournier77016da2020-08-15 14:35:51 +02003115 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3116 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003117 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003118 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003119
Willy Tarreau47860ed2015-03-10 14:07:50 +01003120 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003121 lua_pushinteger(L, -1);
3122 return 1;
3123 }
3124
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003125 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003126 * the request buffer if its not required.
3127 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003128 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003129 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003130 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003131 }
3132
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003133 /* The written data will be immediately sent, so we can check
3134 * the available space without taking in account the reserve.
3135 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003136 * data, because the buffer will be flushed.
3137 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003138 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003139
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003140 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003141 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003142 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003143 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003144 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003145 return 1;
3146
3147 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003148 if (max > len - l)
3149 max = len - l;
3150
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003151 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003152 * detects a non contiguous buffer and realign it.
3153 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003154 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003155 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003156
3157 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003158 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003159
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160 /* buffer replace considers that the input part is filled.
3161 * so, I must forward these new data in the output part.
3162 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003163 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164
3165 l += max;
3166 lua_pop(L, 1);
3167 lua_pushinteger(L, l);
3168
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003169 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003170 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003171 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003172 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003173 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003174 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003175 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003176
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003177 if (l < len) {
3178 /* If we are waiting for space in the response buffer, we
3179 * must set the flag WAKERESWR. This flag required the task
3180 * wake up if any activity is detected on the response buffer.
3181 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003182 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003183 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003184 else
3185 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003186 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003187 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003188
3189 return 1;
3190}
3191
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003192/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003193 * yield the LUA process, and resume it without checking the
3194 * input arguments.
3195 */
3196__LJMP static int hlua_channel_send(lua_State *L)
3197{
3198 MAY_LJMP(check_args(L, 2, "send"));
3199 lua_pushinteger(L, 0);
3200
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003201 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003202}
3203
3204/* This function forward and amount of butes. The data pass from
3205 * the input side of the buffer to the output side, and can be
3206 * forwarded. This function never fails.
3207 *
3208 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003209 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003210 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003211__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003212{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003213 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003214 int len;
3215 int l;
3216 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003217 struct hlua *hlua;
3218
3219 /* Get hlua struct, or NULL if we execute from main lua state */
3220 hlua = hlua_gethlua(L);
3221 if (!hlua)
3222 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003223
3224 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003225
Thierry Fournier77016da2020-08-15 14:35:51 +02003226 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3227 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003228 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003229 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003230
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003231 len = MAY_LJMP(luaL_checkinteger(L, 2));
3232 l = MAY_LJMP(luaL_checkinteger(L, -1));
3233
3234 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003235 if (max > ci_data(chn))
3236 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003237 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003238 l += max;
3239
3240 lua_pop(L, 1);
3241 lua_pushinteger(L, l);
3242
3243 /* Check if it miss bytes to forward. */
3244 if (l < len) {
3245 /* The the input channel or the output channel are closed, we
3246 * must return the amount of data forwarded.
3247 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003248 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003249 return 1;
3250
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003251 /* If we are waiting for space data in the response buffer, we
3252 * must set the flag WAKERESWR. This flag required the task
3253 * wake up if any activity is detected on the response buffer.
3254 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003255 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003256 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003257 else
3258 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003259
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003260 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003261 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003262 }
3263
3264 return 1;
3265}
3266
3267/* Just check the input and prepare the stack for the previous
3268 * function "hlua_channel_forward_yield"
3269 */
3270__LJMP static int hlua_channel_forward(lua_State *L)
3271{
3272 MAY_LJMP(check_args(L, 2, "forward"));
3273 MAY_LJMP(hlua_checkchannel(L, 1));
3274 MAY_LJMP(luaL_checkinteger(L, 2));
3275
3276 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003277 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003278}
3279
3280/* Just returns the number of bytes available in the input
3281 * side of the buffer. This function never fails.
3282 */
3283__LJMP static int hlua_channel_get_in_len(lua_State *L)
3284{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003285 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003286
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003287 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003288 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003289 if (IS_HTX_STRM(chn_strm(chn))) {
3290 struct htx *htx = htxbuf(&chn->buf);
3291 lua_pushinteger(L, htx->data - co_data(chn));
3292 }
3293 else
3294 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003295 return 1;
3296}
3297
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003298/* Returns true if the channel is full. */
3299__LJMP static int hlua_channel_is_full(lua_State *L)
3300{
3301 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003302
3303 MAY_LJMP(check_args(L, 1, "is_full"));
3304 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003305 /* ignore the reserve, we are not on a producer side (ie in an
3306 * applet).
3307 */
3308 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003309 return 1;
3310}
3311
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003312/* Returns true if the channel is the response channel. */
3313__LJMP static int hlua_channel_is_resp(lua_State *L)
3314{
3315 struct channel *chn;
3316
3317 MAY_LJMP(check_args(L, 1, "is_resp"));
3318 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3319
3320 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3321 return 1;
3322}
3323
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003324/* Just returns the number of bytes available in the output
3325 * side of the buffer. This function never fails.
3326 */
3327__LJMP static int hlua_channel_get_out_len(lua_State *L)
3328{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003329 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003330
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003331 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003332 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003333 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003334 return 1;
3335}
3336
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003337/*
3338 *
3339 *
3340 * Class Fetches
3341 *
3342 *
3343 */
3344
3345/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003346 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003347 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003348__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003349{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003350 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003351}
3352
3353/* This function creates and push in the stack a fetch object according
3354 * with a current TXN.
3355 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003356static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003357{
Willy Tarreau7073c472015-04-06 11:15:40 +02003358 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003359
3360 /* Check stack size. */
3361 if (!lua_checkstack(L, 3))
3362 return 0;
3363
3364 /* Create the object: obj[0] = userdata.
3365 * Note that the base of the Fetches object is the
3366 * transaction object.
3367 */
3368 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003369 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003370 lua_rawseti(L, -2, 0);
3371
Willy Tarreau7073c472015-04-06 11:15:40 +02003372 hsmp->s = txn->s;
3373 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003374 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003375 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003376
3377 /* Pop a class sesison metatable and affect it to the userdata. */
3378 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3379 lua_setmetatable(L, -2);
3380
3381 return 1;
3382}
3383
3384/* This function is an LUA binding. It is called with each sample-fetch.
3385 * It uses closure argument to store the associated sample-fetch. It
3386 * returns only one argument or throws an error. An error is thrown
3387 * only if an error is encountered during the argument parsing. If
3388 * the "sample-fetch" function fails, nil is returned.
3389 */
3390__LJMP static int hlua_run_sample_fetch(lua_State *L)
3391{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003392 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003393 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003394 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003395 int i;
3396 struct sample smp;
3397
3398 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003399 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003400
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003401 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003402 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003403
Thierry FOURNIERca988662015-12-20 18:43:03 +01003404 /* Check execution authorization. */
3405 if (f->use & SMP_USE_HTTP_ANY &&
3406 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3407 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3408 "is not available in Lua services", f->kw);
3409 WILL_LJMP(lua_error(L));
3410 }
3411
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003412 /* Get extra arguments. */
3413 for (i = 0; i < lua_gettop(L) - 1; i++) {
3414 if (i >= ARGM_NBARGS)
3415 break;
3416 hlua_lua2arg(L, i + 2, &args[i]);
3417 }
3418 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003419 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003420
3421 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003422 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003423
3424 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003425 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003426 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003427 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003428 }
3429
3430 /* Initialise the sample. */
3431 memset(&smp, 0, sizeof(smp));
3432
3433 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003434 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003435 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003436 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003437 lua_pushstring(L, "");
3438 else
3439 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003440 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003441 }
3442
3443 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003444 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003445 hlua_smp2lua_str(L, &smp);
3446 else
3447 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003448
3449 end:
3450 for (i = 0; args[i].type != ARGT_STOP; i++) {
3451 if (args[i].type == ARGT_STR)
3452 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003453 else if (args[i].type == ARGT_REG)
3454 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003455 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003456 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003457
3458 error:
3459 for (i = 0; args[i].type != ARGT_STOP; i++) {
3460 if (args[i].type == ARGT_STR)
3461 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003462 else if (args[i].type == ARGT_REG)
3463 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003464 }
3465 WILL_LJMP(lua_error(L));
3466 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003467}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003468
3469/*
3470 *
3471 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003472 * Class Converters
3473 *
3474 *
3475 */
3476
3477/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003478 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003479 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003480__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003481{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003482 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003483}
3484
3485/* This function creates and push in the stack a Converters object
3486 * according with a current TXN.
3487 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003488static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003489{
Willy Tarreau7073c472015-04-06 11:15:40 +02003490 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003491
3492 /* Check stack size. */
3493 if (!lua_checkstack(L, 3))
3494 return 0;
3495
3496 /* Create the object: obj[0] = userdata.
3497 * Note that the base of the Converters object is the
3498 * same than the TXN object.
3499 */
3500 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003501 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003502 lua_rawseti(L, -2, 0);
3503
Willy Tarreau7073c472015-04-06 11:15:40 +02003504 hsmp->s = txn->s;
3505 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003506 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003507 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003508
Willy Tarreau87b09662015-04-03 00:22:06 +02003509 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003510 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3511 lua_setmetatable(L, -2);
3512
3513 return 1;
3514}
3515
3516/* This function is an LUA binding. It is called with each converter.
3517 * It uses closure argument to store the associated converter. It
3518 * returns only one argument or throws an error. An error is thrown
3519 * only if an error is encountered during the argument parsing. If
3520 * the converter function function fails, nil is returned.
3521 */
3522__LJMP static int hlua_run_sample_conv(lua_State *L)
3523{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003524 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003525 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003526 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003527 int i;
3528 struct sample smp;
3529
3530 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003531 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003532
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003533 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003534 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003535
3536 /* Get extra arguments. */
3537 for (i = 0; i < lua_gettop(L) - 2; i++) {
3538 if (i >= ARGM_NBARGS)
3539 break;
3540 hlua_lua2arg(L, i + 3, &args[i]);
3541 }
3542 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003543 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003544
3545 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003546 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003547
3548 /* Run the special args checker. */
3549 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3550 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003551 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003552 }
3553
3554 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003555 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003556 if (!hlua_lua2smp(L, 2, &smp)) {
3557 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003558 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003559 }
3560
Willy Tarreau1777ea62016-03-10 16:15:46 +01003561 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3562
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003563 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003564 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003565 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003566 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003567 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003568 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003569 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3570 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003571 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003572 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003573 }
3574
3575 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003576 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003577 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003578 lua_pushstring(L, "");
3579 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003580 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003581 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003582 }
3583
3584 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003585 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003586 hlua_smp2lua_str(L, &smp);
3587 else
3588 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003589 end:
3590 for (i = 0; args[i].type != ARGT_STOP; i++) {
3591 if (args[i].type == ARGT_STR)
3592 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003593 else if (args[i].type == ARGT_REG)
3594 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003595 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003596 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003597
3598 error:
3599 for (i = 0; args[i].type != ARGT_STOP; i++) {
3600 if (args[i].type == ARGT_STR)
3601 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003602 else if (args[i].type == ARGT_REG)
3603 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003604 }
3605 WILL_LJMP(lua_error(L));
3606 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003607}
3608
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003609/*
3610 *
3611 *
3612 * Class AppletTCP
3613 *
3614 *
3615 */
3616
3617/* Returns a struct hlua_txn if the stack entry "ud" is
3618 * a class stream, otherwise it throws an error.
3619 */
3620__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3621{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003622 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003623}
3624
3625/* This function creates and push in the stack an Applet object
3626 * according with a current TXN.
3627 */
3628static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3629{
3630 struct hlua_appctx *appctx;
3631 struct stream_interface *si = ctx->owner;
3632 struct stream *s = si_strm(si);
3633 struct proxy *p = s->be;
3634
3635 /* Check stack size. */
3636 if (!lua_checkstack(L, 3))
3637 return 0;
3638
3639 /* Create the object: obj[0] = userdata.
3640 * Note that the base of the Converters object is the
3641 * same than the TXN object.
3642 */
3643 lua_newtable(L);
3644 appctx = lua_newuserdata(L, sizeof(*appctx));
3645 lua_rawseti(L, -2, 0);
3646 appctx->appctx = ctx;
3647 appctx->htxn.s = s;
3648 appctx->htxn.p = p;
3649
3650 /* Create the "f" field that contains a list of fetches. */
3651 lua_pushstring(L, "f");
3652 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3653 return 0;
3654 lua_settable(L, -3);
3655
3656 /* Create the "sf" field that contains a list of stringsafe fetches. */
3657 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003658 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003659 return 0;
3660 lua_settable(L, -3);
3661
3662 /* Create the "c" field that contains a list of converters. */
3663 lua_pushstring(L, "c");
3664 if (!hlua_converters_new(L, &appctx->htxn, 0))
3665 return 0;
3666 lua_settable(L, -3);
3667
3668 /* Create the "sc" field that contains a list of stringsafe converters. */
3669 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003670 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003671 return 0;
3672 lua_settable(L, -3);
3673
3674 /* Pop a class stream metatable and affect it to the table. */
3675 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3676 lua_setmetatable(L, -2);
3677
3678 return 1;
3679}
3680
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003681__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3682{
3683 struct hlua_appctx *appctx;
3684 struct stream *s;
3685 const char *name;
3686 size_t len;
3687 struct sample smp;
3688
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003689 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3690 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003691
3692 /* It is useles to retrieve the stream, but this function
3693 * runs only in a stream context.
3694 */
3695 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3696 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3697 s = appctx->htxn.s;
3698
3699 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003700 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003701 hlua_lua2smp(L, 3, &smp);
3702
3703 /* Store the sample in a variable. */
3704 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003705
3706 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3707 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3708 else
3709 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3710
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003711 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003712}
3713
3714__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3715{
3716 struct hlua_appctx *appctx;
3717 struct stream *s;
3718 const char *name;
3719 size_t len;
3720 struct sample smp;
3721
3722 MAY_LJMP(check_args(L, 2, "unset_var"));
3723
3724 /* It is useles to retrieve the stream, but this function
3725 * runs only in a stream context.
3726 */
3727 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3728 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3729 s = appctx->htxn.s;
3730
3731 /* Unset the variable. */
3732 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003733 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3734 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003735}
3736
3737__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3738{
3739 struct hlua_appctx *appctx;
3740 struct stream *s;
3741 const char *name;
3742 size_t len;
3743 struct sample smp;
3744
3745 MAY_LJMP(check_args(L, 2, "get_var"));
3746
3747 /* It is useles to retrieve the stream, but this function
3748 * runs only in a stream context.
3749 */
3750 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3751 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3752 s = appctx->htxn.s;
3753
3754 smp_set_owner(&smp, s->be, s->sess, s, 0);
3755 if (!vars_get_by_name(name, len, &smp)) {
3756 lua_pushnil(L);
3757 return 1;
3758 }
3759
3760 return hlua_smp2lua(L, &smp);
3761}
3762
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003763__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3764{
3765 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3766 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003767 struct hlua *hlua;
3768
3769 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003770 if (!s->hlua)
3771 return 0;
3772 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003773
3774 MAY_LJMP(check_args(L, 2, "set_priv"));
3775
3776 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003777 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003778
3779 /* Get and store new value. */
3780 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3781 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3782
3783 return 0;
3784}
3785
3786__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3787{
3788 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3789 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003790 struct hlua *hlua;
3791
3792 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003793 if (!s->hlua) {
3794 lua_pushnil(L);
3795 return 1;
3796 }
3797 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003798
3799 /* Push configuration index in the stack. */
3800 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3801
3802 return 1;
3803}
3804
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003805/* If expected data not yet available, it returns a yield. This function
3806 * consumes the data in the buffer. It returns a string containing the
3807 * data. This string can be empty.
3808 */
3809__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3810{
3811 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3812 struct stream_interface *si = appctx->appctx->owner;
3813 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003814 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003815 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003816 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003817 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003818
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003819 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003820 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003821
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003822 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003823 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003824 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003825 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003826 }
3827
3828 /* End of data: commit the total strings and return. */
3829 if (ret < 0) {
3830 luaL_pushresult(&appctx->b);
3831 return 1;
3832 }
3833
3834 /* Ensure that the block 2 length is usable. */
3835 if (ret == 1)
3836 len2 = 0;
3837
3838 /* dont check the max length read and dont check. */
3839 luaL_addlstring(&appctx->b, blk1, len1);
3840 luaL_addlstring(&appctx->b, blk2, len2);
3841
3842 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003843 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003844 luaL_pushresult(&appctx->b);
3845 return 1;
3846}
3847
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003848/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003849__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3850{
3851 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3852
3853 /* Initialise the string catenation. */
3854 luaL_buffinit(L, &appctx->b);
3855
3856 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3857}
3858
3859/* If expected data not yet available, it returns a yield. This function
3860 * consumes the data in the buffer. It returns a string containing the
3861 * data. This string can be empty.
3862 */
3863__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3864{
3865 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3866 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003867 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003868 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003869 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003870 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003871 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003872 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003873
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003874 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003875 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003876
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003877 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003878 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003879 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003880 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003881 }
3882
3883 /* End of data: commit the total strings and return. */
3884 if (ret < 0) {
3885 luaL_pushresult(&appctx->b);
3886 return 1;
3887 }
3888
3889 /* Ensure that the block 2 length is usable. */
3890 if (ret == 1)
3891 len2 = 0;
3892
3893 if (len == -1) {
3894
3895 /* If len == -1, catenate all the data avalaile and
3896 * yield because we want to get all the data until
3897 * the end of data stream.
3898 */
3899 luaL_addlstring(&appctx->b, blk1, len1);
3900 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003901 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003902 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003903 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003904
3905 } else {
3906
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003907 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003908 if (len1 > len)
3909 len1 = len;
3910 luaL_addlstring(&appctx->b, blk1, len1);
3911 len -= len1;
3912
3913 /* Copy the second block. */
3914 if (len2 > len)
3915 len2 = len;
3916 luaL_addlstring(&appctx->b, blk2, len2);
3917 len -= len2;
3918
3919 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003920 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003921
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003922 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003923 if (len > 0) {
3924 lua_pushinteger(L, len);
3925 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003926 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003927 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003928 }
3929
3930 /* return the result. */
3931 luaL_pushresult(&appctx->b);
3932 return 1;
3933 }
3934
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003935 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003936 hlua_pusherror(L, "Lua: internal error");
3937 WILL_LJMP(lua_error(L));
3938 return 0;
3939}
3940
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003941/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003942__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3943{
3944 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3945 int len = -1;
3946
3947 if (lua_gettop(L) > 2)
3948 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3949 if (lua_gettop(L) >= 2) {
3950 len = MAY_LJMP(luaL_checkinteger(L, 2));
3951 lua_pop(L, 1);
3952 }
3953
3954 /* Confirm or set the required length */
3955 lua_pushinteger(L, len);
3956
3957 /* Initialise the string catenation. */
3958 luaL_buffinit(L, &appctx->b);
3959
3960 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3961}
3962
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003963/* Append data in the output side of the buffer. This data is immediately
3964 * sent. The function returns the amount of data written. If the buffer
3965 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003966 * if the channel is closed.
3967 */
3968__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3969{
3970 size_t len;
3971 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3972 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3973 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3974 struct stream_interface *si = appctx->appctx->owner;
3975 struct channel *chn = si_ic(si);
3976 int max;
3977
3978 /* Get the max amount of data which can write as input in the channel. */
3979 max = channel_recv_max(chn);
3980 if (max > (len - l))
3981 max = len - l;
3982
3983 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003984 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003985
3986 /* update counters. */
3987 l += max;
3988 lua_pop(L, 1);
3989 lua_pushinteger(L, l);
3990
3991 /* If some data is not send, declares the situation to the
3992 * applet, and returns a yield.
3993 */
3994 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003995 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003996 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003997 }
3998
3999 return 1;
4000}
4001
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004002/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004003 * yield the LUA process, and resume it without checking the
4004 * input arguments.
4005 */
4006__LJMP static int hlua_applet_tcp_send(lua_State *L)
4007{
4008 MAY_LJMP(check_args(L, 2, "send"));
4009 lua_pushinteger(L, 0);
4010
4011 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4012}
4013
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004014/*
4015 *
4016 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004017 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004018 *
4019 *
4020 */
4021
4022/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004023 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004024 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004025__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004026{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004027 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004028}
4029
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004030/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004031 * according with a current TXN.
4032 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004033static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004034{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004035 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004036 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004037 struct stream_interface *si = ctx->owner;
4038 struct stream *s = si_strm(si);
4039 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004040 struct htx *htx;
4041 struct htx_blk *blk;
4042 struct htx_sl *sl;
4043 struct ist path;
4044 unsigned long long len = 0;
4045 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004046
4047 /* Check stack size. */
4048 if (!lua_checkstack(L, 3))
4049 return 0;
4050
4051 /* Create the object: obj[0] = userdata.
4052 * Note that the base of the Converters object is the
4053 * same than the TXN object.
4054 */
4055 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004056 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004057 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004058 appctx->appctx = ctx;
4059 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004060 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004061 appctx->htxn.s = s;
4062 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004063
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004064 /* Create the "f" field that contains a list of fetches. */
4065 lua_pushstring(L, "f");
4066 if (!hlua_fetches_new(L, &appctx->htxn, 0))
4067 return 0;
4068 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004069
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004070 /* Create the "sf" field that contains a list of stringsafe fetches. */
4071 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004072 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004073 return 0;
4074 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004075
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004076 /* Create the "c" field that contains a list of converters. */
4077 lua_pushstring(L, "c");
4078 if (!hlua_converters_new(L, &appctx->htxn, 0))
4079 return 0;
4080 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004081
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004082 /* Create the "sc" field that contains a list of stringsafe converters. */
4083 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004084 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004085 return 0;
4086 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004087
Christopher Fauleta2097962019-07-15 16:25:33 +02004088 htx = htxbuf(&s->req.buf);
4089 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004090 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004091 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004092
Christopher Fauleta2097962019-07-15 16:25:33 +02004093 /* Stores the request method. */
4094 lua_pushstring(L, "method");
4095 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4096 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004097
Christopher Fauleta2097962019-07-15 16:25:33 +02004098 /* Stores the http version. */
4099 lua_pushstring(L, "version");
4100 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4101 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004102
Christopher Fauleta2097962019-07-15 16:25:33 +02004103 /* creates an array of headers. hlua_http_get_headers() crates and push
4104 * the array on the top of the stack.
4105 */
4106 lua_pushstring(L, "headers");
4107 htxn.s = s;
4108 htxn.p = px;
4109 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004110 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004111 return 0;
4112 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004113
Christopher Fauleta2097962019-07-15 16:25:33 +02004114 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004115 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004116 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004117
Christopher Fauleta2097962019-07-15 16:25:33 +02004118 p = path.ptr;
4119 end = path.ptr + path.len;
4120 q = p;
4121 while (q < end && *q != '?')
4122 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004123
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004124 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004125 lua_pushstring(L, "path");
4126 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004127 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004128
Christopher Fauleta2097962019-07-15 16:25:33 +02004129 /* Stores the query string. */
4130 lua_pushstring(L, "qs");
4131 if (*q == '?')
4132 q++;
4133 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004134 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004135 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004136
Christopher Fauleta2097962019-07-15 16:25:33 +02004137 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4138 struct htx_blk *blk = htx_get_blk(htx, pos);
4139 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004140
Christopher Fauleta2097962019-07-15 16:25:33 +02004141 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
4142 break;
4143 if (type == HTX_BLK_DATA)
4144 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004145 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004146 if (htx->extra != ULLONG_MAX)
4147 len += htx->extra;
4148
4149 /* Stores the request path. */
4150 lua_pushstring(L, "length");
4151 lua_pushinteger(L, len);
4152 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004153
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004154 /* Create an empty array of HTTP request headers. */
4155 lua_pushstring(L, "response");
4156 lua_newtable(L);
4157 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004158
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004159 /* Pop a class stream metatable and affect it to the table. */
4160 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4161 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004162
4163 return 1;
4164}
4165
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004166__LJMP static int hlua_applet_http_set_var(lua_State *L)
4167{
4168 struct hlua_appctx *appctx;
4169 struct stream *s;
4170 const char *name;
4171 size_t len;
4172 struct sample smp;
4173
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004174 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4175 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004176
4177 /* It is useles to retrieve the stream, but this function
4178 * runs only in a stream context.
4179 */
4180 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4181 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4182 s = appctx->htxn.s;
4183
4184 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004185 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004186 hlua_lua2smp(L, 3, &smp);
4187
4188 /* Store the sample in a variable. */
4189 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004190
4191 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4192 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4193 else
4194 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4195
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004196 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004197}
4198
4199__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4200{
4201 struct hlua_appctx *appctx;
4202 struct stream *s;
4203 const char *name;
4204 size_t len;
4205 struct sample smp;
4206
4207 MAY_LJMP(check_args(L, 2, "unset_var"));
4208
4209 /* It is useles to retrieve the stream, but this function
4210 * runs only in a stream context.
4211 */
4212 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4213 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4214 s = appctx->htxn.s;
4215
4216 /* Unset the variable. */
4217 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004218 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4219 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004220}
4221
4222__LJMP static int hlua_applet_http_get_var(lua_State *L)
4223{
4224 struct hlua_appctx *appctx;
4225 struct stream *s;
4226 const char *name;
4227 size_t len;
4228 struct sample smp;
4229
4230 MAY_LJMP(check_args(L, 2, "get_var"));
4231
4232 /* It is useles to retrieve the stream, but this function
4233 * runs only in a stream context.
4234 */
4235 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4236 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4237 s = appctx->htxn.s;
4238
4239 smp_set_owner(&smp, s->be, s->sess, s, 0);
4240 if (!vars_get_by_name(name, len, &smp)) {
4241 lua_pushnil(L);
4242 return 1;
4243 }
4244
4245 return hlua_smp2lua(L, &smp);
4246}
4247
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004248__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4249{
4250 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4251 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004252 struct hlua *hlua;
4253
4254 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004255 if (!s->hlua)
4256 return 0;
4257 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004258
4259 MAY_LJMP(check_args(L, 2, "set_priv"));
4260
4261 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004262 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004263
4264 /* Get and store new value. */
4265 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4266 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4267
4268 return 0;
4269}
4270
4271__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4272{
4273 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4274 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004275 struct hlua *hlua;
4276
4277 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004278 if (!s->hlua) {
4279 lua_pushnil(L);
4280 return 1;
4281 }
4282 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004283
4284 /* Push configuration index in the stack. */
4285 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4286
4287 return 1;
4288}
4289
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004290/* If expected data not yet available, it returns a yield. This function
4291 * consumes the data in the buffer. It returns a string containing the
4292 * data. This string can be empty.
4293 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004294__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004295{
4296 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4297 struct stream_interface *si = appctx->appctx->owner;
4298 struct channel *req = si_oc(si);
4299 struct htx *htx;
4300 struct htx_blk *blk;
4301 size_t count;
4302 int stop = 0;
4303
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004304 htx = htx_from_buf(&req->buf);
4305 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004306 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004307
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004308 while (count && !stop && blk) {
4309 enum htx_blk_type type = htx_get_blk_type(blk);
4310 uint32_t sz = htx_get_blksz(blk);
4311 struct ist v;
4312 uint32_t vlen;
4313 char *nl;
4314
Christopher Faulete461e342018-12-18 16:43:35 +01004315 if (type == HTX_BLK_EOM) {
4316 stop = 1;
4317 break;
4318 }
4319
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004320 vlen = sz;
4321 if (vlen > count) {
4322 if (type != HTX_BLK_DATA)
4323 break;
4324 vlen = count;
4325 }
4326
4327 switch (type) {
4328 case HTX_BLK_UNUSED:
4329 break;
4330
4331 case HTX_BLK_DATA:
4332 v = htx_get_blk_value(htx, blk);
4333 v.len = vlen;
4334 nl = istchr(v, '\n');
4335 if (nl != NULL) {
4336 stop = 1;
4337 vlen = nl - v.ptr + 1;
4338 }
4339 luaL_addlstring(&appctx->b, v.ptr, vlen);
4340 break;
4341
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004342 case HTX_BLK_TLR:
4343 case HTX_BLK_EOM:
4344 stop = 1;
4345 break;
4346
4347 default:
4348 break;
4349 }
4350
4351 co_set_data(req, co_data(req) - vlen);
4352 count -= vlen;
4353 if (sz == vlen)
4354 blk = htx_remove_blk(htx, blk);
4355 else {
4356 htx_cut_data_blk(htx, blk, vlen);
4357 break;
4358 }
4359 }
4360
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004361 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004362 if (!stop) {
4363 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004364 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004365 }
4366
4367 /* return the result. */
4368 luaL_pushresult(&appctx->b);
4369 return 1;
4370}
4371
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004372
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004373/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004374__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004375{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004376 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004377
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004378 /* Initialise the string catenation. */
4379 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004380
Christopher Fauleta2097962019-07-15 16:25:33 +02004381 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004382}
4383
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004384/* If expected data not yet available, it returns a yield. This function
4385 * consumes the data in the buffer. It returns a string containing the
4386 * data. This string can be empty.
4387 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004388__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004389{
4390 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4391 struct stream_interface *si = appctx->appctx->owner;
4392 struct channel *req = si_oc(si);
4393 struct htx *htx;
4394 struct htx_blk *blk;
4395 size_t count;
4396 int len;
4397
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004398 htx = htx_from_buf(&req->buf);
4399 len = MAY_LJMP(luaL_checkinteger(L, 2));
4400 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004401 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004402 while (count && len && blk) {
4403 enum htx_blk_type type = htx_get_blk_type(blk);
4404 uint32_t sz = htx_get_blksz(blk);
4405 struct ist v;
4406 uint32_t vlen;
4407
Christopher Faulete461e342018-12-18 16:43:35 +01004408 if (type == HTX_BLK_EOM) {
4409 len = 0;
4410 break;
4411 }
4412
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004413 vlen = sz;
4414 if (len > 0 && vlen > len)
4415 vlen = len;
4416 if (vlen > count) {
4417 if (type != HTX_BLK_DATA)
4418 break;
4419 vlen = count;
4420 }
4421
4422 switch (type) {
4423 case HTX_BLK_UNUSED:
4424 break;
4425
4426 case HTX_BLK_DATA:
4427 v = htx_get_blk_value(htx, blk);
4428 luaL_addlstring(&appctx->b, v.ptr, vlen);
4429 break;
4430
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004431 case HTX_BLK_TLR:
4432 case HTX_BLK_EOM:
4433 len = 0;
4434 break;
4435
4436 default:
4437 break;
4438 }
4439
4440 co_set_data(req, co_data(req) - vlen);
4441 count -= vlen;
4442 if (len > 0)
4443 len -= vlen;
4444 if (sz == vlen)
4445 blk = htx_remove_blk(htx, blk);
4446 else {
4447 htx_cut_data_blk(htx, blk, vlen);
4448 break;
4449 }
4450 }
4451
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004452 htx_to_buf(htx, &req->buf);
4453
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004454 /* If we are no other data available, yield waiting for new data. */
4455 if (len) {
4456 if (len > 0) {
4457 lua_pushinteger(L, len);
4458 lua_replace(L, 2);
4459 }
4460 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004461 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004462 }
4463
4464 /* return the result. */
4465 luaL_pushresult(&appctx->b);
4466 return 1;
4467}
4468
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004469/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004470__LJMP static int hlua_applet_http_recv(lua_State *L)
4471{
4472 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4473 int len = -1;
4474
4475 /* Check arguments. */
4476 if (lua_gettop(L) > 2)
4477 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4478 if (lua_gettop(L) >= 2) {
4479 len = MAY_LJMP(luaL_checkinteger(L, 2));
4480 lua_pop(L, 1);
4481 }
4482
Christopher Fauleta2097962019-07-15 16:25:33 +02004483 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004484
Christopher Fauleta2097962019-07-15 16:25:33 +02004485 /* Initialise the string catenation. */
4486 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004487
Christopher Fauleta2097962019-07-15 16:25:33 +02004488 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004489}
4490
4491/* Append data in the output side of the buffer. This data is immediately
4492 * sent. The function returns the amount of data written. If the buffer
4493 * cannot contain the data, the function yields. The function returns -1
4494 * if the channel is closed.
4495 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004496__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004497{
4498 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4499 struct stream_interface *si = appctx->appctx->owner;
4500 struct channel *res = si_ic(si);
4501 struct htx *htx = htx_from_buf(&res->buf);
4502 const char *data;
4503 size_t len;
4504 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4505 int max;
4506
Christopher Faulet9060fc02019-07-03 11:39:30 +02004507 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004508 if (!max)
4509 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004510
4511 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4512
4513 /* Get the max amount of data which can write as input in the channel. */
4514 if (max > (len - l))
4515 max = len - l;
4516
4517 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004518 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004519 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004520
4521 /* update counters. */
4522 l += max;
4523 lua_pop(L, 1);
4524 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004525
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004526 /* If some data is not send, declares the situation to the
4527 * applet, and returns a yield.
4528 */
4529 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004530 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004531 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004532 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004533 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004534 }
4535
Christopher Fauleta2097962019-07-15 16:25:33 +02004536 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004537 return 1;
4538}
4539
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004540/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004541 * yield the LUA process, and resume it without checking the
4542 * input arguments.
4543 */
4544__LJMP static int hlua_applet_http_send(lua_State *L)
4545{
4546 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004547
4548 /* We want to send some data. Headers must be sent. */
4549 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4550 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4551 WILL_LJMP(lua_error(L));
4552 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004553
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004554 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004555 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004556
Christopher Fauleta2097962019-07-15 16:25:33 +02004557 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004558}
4559
4560__LJMP static int hlua_applet_http_addheader(lua_State *L)
4561{
4562 const char *name;
4563 int ret;
4564
4565 MAY_LJMP(hlua_checkapplet_http(L, 1));
4566 name = MAY_LJMP(luaL_checkstring(L, 2));
4567 MAY_LJMP(luaL_checkstring(L, 3));
4568
4569 /* Push in the stack the "response" entry. */
4570 ret = lua_getfield(L, 1, "response");
4571 if (ret != LUA_TTABLE) {
4572 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4573 "is expected as an array. %s found", lua_typename(L, ret));
4574 WILL_LJMP(lua_error(L));
4575 }
4576
4577 /* check if the header is already registered if it is not
4578 * the case, register it.
4579 */
4580 ret = lua_getfield(L, -1, name);
4581 if (ret == LUA_TNIL) {
4582
4583 /* Entry not found. */
4584 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4585
4586 /* Insert the new header name in the array in the top of the stack.
4587 * It left the new array in the top of the stack.
4588 */
4589 lua_newtable(L);
4590 lua_pushvalue(L, 2);
4591 lua_pushvalue(L, -2);
4592 lua_settable(L, -4);
4593
4594 } else if (ret != LUA_TTABLE) {
4595
4596 /* corruption error. */
4597 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4598 "is expected as an array. %s found", name, lua_typename(L, ret));
4599 WILL_LJMP(lua_error(L));
4600 }
4601
4602 /* Now the top od thestack is an array of values. We push
4603 * the header value as new entry.
4604 */
4605 lua_pushvalue(L, 3);
4606 ret = lua_rawlen(L, -2);
4607 lua_rawseti(L, -2, ret + 1);
4608 lua_pushboolean(L, 1);
4609 return 1;
4610}
4611
4612__LJMP static int hlua_applet_http_status(lua_State *L)
4613{
4614 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4615 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004616 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004617
4618 if (status < 100 || status > 599) {
4619 lua_pushboolean(L, 0);
4620 return 1;
4621 }
4622
4623 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004624 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004625 lua_pushboolean(L, 1);
4626 return 1;
4627}
4628
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004629
Christopher Fauleta2097962019-07-15 16:25:33 +02004630__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004631{
4632 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4633 struct stream_interface *si = appctx->appctx->owner;
4634 struct channel *res = si_ic(si);
4635 struct htx *htx;
4636 struct htx_sl *sl;
4637 struct h1m h1m;
4638 const char *status, *reason;
4639 const char *name, *value;
4640 size_t nlen, vlen;
4641 unsigned int flags;
4642
4643 /* Send the message at once. */
4644 htx = htx_from_buf(&res->buf);
4645 h1m_init_res(&h1m);
4646
4647 /* Use the same http version than the request. */
4648 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4649 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4650 if (reason == NULL)
4651 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4652 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4653 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4654 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4655 }
4656 else {
4657 flags = HTX_SL_F_IS_RESP;
4658 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4659 }
4660 if (!sl) {
4661 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4662 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4663 WILL_LJMP(lua_error(L));
4664 }
4665 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4666
4667 /* Get the array associated to the field "response" in the object AppletHTTP. */
4668 lua_pushvalue(L, 0);
4669 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4670 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4671 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4672 WILL_LJMP(lua_error(L));
4673 }
4674
4675 /* Browse the list of headers. */
4676 lua_pushnil(L);
4677 while(lua_next(L, -2) != 0) {
4678 /* We expect a string as -2. */
4679 if (lua_type(L, -2) != LUA_TSTRING) {
4680 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4681 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4682 lua_typename(L, lua_type(L, -2)));
4683 WILL_LJMP(lua_error(L));
4684 }
4685 name = lua_tolstring(L, -2, &nlen);
4686
4687 /* We expect an array as -1. */
4688 if (lua_type(L, -1) != LUA_TTABLE) {
4689 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4690 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4691 name,
4692 lua_typename(L, lua_type(L, -1)));
4693 WILL_LJMP(lua_error(L));
4694 }
4695
4696 /* Browse the table who is on the top of the stack. */
4697 lua_pushnil(L);
4698 while(lua_next(L, -2) != 0) {
4699 int id;
4700
4701 /* We expect a number as -2. */
4702 if (lua_type(L, -2) != LUA_TNUMBER) {
4703 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4704 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4705 name,
4706 lua_typename(L, lua_type(L, -2)));
4707 WILL_LJMP(lua_error(L));
4708 }
4709 id = lua_tointeger(L, -2);
4710
4711 /* We expect a string as -2. */
4712 if (lua_type(L, -1) != LUA_TSTRING) {
4713 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4714 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4715 name, id,
4716 lua_typename(L, lua_type(L, -1)));
4717 WILL_LJMP(lua_error(L));
4718 }
4719 value = lua_tolstring(L, -1, &vlen);
4720
4721 /* Simple Protocol checks. */
4722 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004723 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004724 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4725 struct ist v = ist2(value, vlen);
4726 int ret;
4727
4728 ret = h1_parse_cont_len_header(&h1m, &v);
4729 if (ret < 0) {
4730 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4731 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4732 name);
4733 WILL_LJMP(lua_error(L));
4734 }
4735 else if (ret == 0)
4736 goto next; /* Skip it */
4737 }
4738
4739 /* Add a new header */
4740 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4741 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4742 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4743 name);
4744 WILL_LJMP(lua_error(L));
4745 }
4746 next:
4747 /* Remove the array from the stack, and get next element with a remaining string. */
4748 lua_pop(L, 1);
4749 }
4750
4751 /* Remove the array from the stack, and get next element with a remaining string. */
4752 lua_pop(L, 1);
4753 }
4754
4755 if (h1m.flags & H1_MF_CHNK)
4756 h1m.flags &= ~H1_MF_CLEN;
4757 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4758 h1m.flags |= H1_MF_XFER_LEN;
4759
4760 /* Uset HTX start-line flags */
4761 if (h1m.flags & H1_MF_XFER_ENC)
4762 flags |= HTX_SL_F_XFER_ENC;
4763 if (h1m.flags & H1_MF_XFER_LEN) {
4764 flags |= HTX_SL_F_XFER_LEN;
4765 if (h1m.flags & H1_MF_CHNK)
4766 flags |= HTX_SL_F_CHNK;
4767 else if (h1m.flags & H1_MF_CLEN)
4768 flags |= HTX_SL_F_CLEN;
4769 if (h1m.body_len == 0)
4770 flags |= HTX_SL_F_BODYLESS;
4771 }
4772 sl->flags |= flags;
4773
4774 /* If we dont have a content-length set, and the HTTP version is 1.1
4775 * and the status code implies the presence of a message body, we must
4776 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004777 * for the keepalive compliance. If the applet announces a transfer-encoding
4778 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004779 */
4780 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4781 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4782 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4783 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4784 /* Add a new header */
4785 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4786 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4787 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4788 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4789 WILL_LJMP(lua_error(L));
4790 }
4791 }
4792
4793 /* Finalize headers. */
4794 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4795 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4796 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4797 WILL_LJMP(lua_error(L));
4798 }
4799
4800 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4801 b_reset(&res->buf);
4802 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4803 WILL_LJMP(lua_error(L));
4804 }
4805
4806 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004807 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004808
4809 /* Headers sent, set the flag. */
4810 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4811 return 0;
4812
4813}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004814/* We will build the status line and the headers of the HTTP response.
4815 * We will try send at once if its not possible, we give back the hand
4816 * waiting for more room.
4817 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004818__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004819{
4820 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4821 struct stream_interface *si = appctx->appctx->owner;
4822 struct channel *res = si_ic(si);
4823
4824 if (co_data(res)) {
4825 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004826 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004827 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004828 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004829}
4830
4831
Christopher Fauleta2097962019-07-15 16:25:33 +02004832__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004833{
Christopher Fauleta2097962019-07-15 16:25:33 +02004834 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004835}
4836
Christopher Fauleta2097962019-07-15 16:25:33 +02004837/*
4838 *
4839 *
4840 * Class HTTP
4841 *
4842 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004843 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004844
4845/* Returns a struct hlua_txn if the stack entry "ud" is
4846 * a class stream, otherwise it throws an error.
4847 */
4848__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004849{
Christopher Fauleta2097962019-07-15 16:25:33 +02004850 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4851}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004852
Christopher Fauleta2097962019-07-15 16:25:33 +02004853/* This function creates and push in the stack a HTTP object
4854 * according with a current TXN.
4855 */
4856static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4857{
4858 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004859
Christopher Fauleta2097962019-07-15 16:25:33 +02004860 /* Check stack size. */
4861 if (!lua_checkstack(L, 3))
4862 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004863
Christopher Fauleta2097962019-07-15 16:25:33 +02004864 /* Create the object: obj[0] = userdata.
4865 * Note that the base of the Converters object is the
4866 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004867 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004868 lua_newtable(L);
4869 htxn = lua_newuserdata(L, sizeof(*htxn));
4870 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004871
4872 htxn->s = txn->s;
4873 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004874 htxn->dir = txn->dir;
4875 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004876
4877 /* Pop a class stream metatable and affect it to the table. */
4878 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4879 lua_setmetatable(L, -2);
4880
4881 return 1;
4882}
4883
4884/* This function creates ans returns an array of HTTP headers.
4885 * This function does not fails. It is used as wrapper with the
4886 * 2 following functions.
4887 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004888__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004889{
Christopher Fauleta2097962019-07-15 16:25:33 +02004890 struct htx *htx;
4891 int32_t pos;
4892
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004893 /* Create the table. */
4894 lua_newtable(L);
4895
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004896
Christopher Fauleta2097962019-07-15 16:25:33 +02004897 htx = htxbuf(&msg->chn->buf);
4898 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4899 struct htx_blk *blk = htx_get_blk(htx, pos);
4900 enum htx_blk_type type = htx_get_blk_type(blk);
4901 struct ist n, v;
4902 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004903
Christopher Fauleta2097962019-07-15 16:25:33 +02004904 if (type == HTX_BLK_HDR) {
4905 n = htx_get_blk_name(htx,blk);
4906 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004907 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004908 else if (type == HTX_BLK_EOH)
4909 break;
4910 else
4911 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004912
Christopher Fauleta2097962019-07-15 16:25:33 +02004913 /* Check for existing entry:
4914 * assume that the table is on the top of the stack, and
4915 * push the key in the stack, the function lua_gettable()
4916 * perform the lookup.
4917 */
4918 lua_pushlstring(L, n.ptr, n.len);
4919 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004920
Christopher Fauleta2097962019-07-15 16:25:33 +02004921 switch (lua_type(L, -1)) {
4922 case LUA_TNIL:
4923 /* Table not found, create it. */
4924 lua_pop(L, 1); /* remove the nil value. */
4925 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4926 lua_newtable(L); /* create and push empty table. */
4927 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4928 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4929 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004930 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004931
Christopher Fauleta2097962019-07-15 16:25:33 +02004932 case LUA_TTABLE:
4933 /* Entry found: push the value in the table. */
4934 len = lua_rawlen(L, -1);
4935 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4936 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4937 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4938 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004939
Christopher Fauleta2097962019-07-15 16:25:33 +02004940 default:
4941 /* Other cases are errors. */
4942 hlua_pusherror(L, "internal error during the parsing of headers.");
4943 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004944 }
4945 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004946 return 1;
4947}
4948
4949__LJMP static int hlua_http_req_get_headers(lua_State *L)
4950{
4951 struct hlua_txn *htxn;
4952
4953 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4954 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4955
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004956 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004957 WILL_LJMP(lua_error(L));
4958
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004959 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004960}
4961
4962__LJMP static int hlua_http_res_get_headers(lua_State *L)
4963{
4964 struct hlua_txn *htxn;
4965
4966 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4967 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4968
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004969 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004970 WILL_LJMP(lua_error(L));
4971
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004972 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004973}
4974
4975/* This function replace full header, or just a value in
4976 * the request or in the response. It is a wrapper fir the
4977 * 4 following functions.
4978 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004979__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004980{
4981 size_t name_len;
4982 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4983 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4984 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004985 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004986 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004987
Dragan Dosen26743032019-04-30 15:54:36 +02004988 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004989 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4990
Christopher Fauleta2097962019-07-15 16:25:33 +02004991 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004992 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004993 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004994 return 0;
4995}
4996
4997__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4998{
4999 struct hlua_txn *htxn;
5000
5001 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5002 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5003
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005004 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005005 WILL_LJMP(lua_error(L));
5006
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005007 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005008}
5009
5010__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5011{
5012 struct hlua_txn *htxn;
5013
5014 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5015 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5016
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005017 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005018 WILL_LJMP(lua_error(L));
5019
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005020 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005021}
5022
5023__LJMP static int hlua_http_req_rep_val(lua_State *L)
5024{
5025 struct hlua_txn *htxn;
5026
5027 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5028 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5029
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005030 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005031 WILL_LJMP(lua_error(L));
5032
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005033 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005034}
5035
5036__LJMP static int hlua_http_res_rep_val(lua_State *L)
5037{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005038 struct hlua_txn *htxn;
5039
5040 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5041 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5042
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005043 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005044 WILL_LJMP(lua_error(L));
5045
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005046 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005047}
5048
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005049/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005050 * It is a wrapper for the 2 following functions.
5051 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005052__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005053{
5054 size_t len;
5055 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005056 struct htx *htx = htxbuf(&msg->chn->buf);
5057 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005058
Christopher Fauleta2097962019-07-15 16:25:33 +02005059 ctx.blk = NULL;
5060 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5061 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005062 return 0;
5063}
5064
5065__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5066{
5067 struct hlua_txn *htxn;
5068
5069 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5070 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5071
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005072 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005073 WILL_LJMP(lua_error(L));
5074
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005075 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005076}
5077
5078__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5079{
5080 struct hlua_txn *htxn;
5081
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005082 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005083 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5084
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005085 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005086 WILL_LJMP(lua_error(L));
5087
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005088 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005089}
5090
5091/* This function adds an header. It is a wrapper used by
5092 * the 2 following functions.
5093 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005094__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005095{
5096 size_t name_len;
5097 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5098 size_t value_len;
5099 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005100 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005101
Christopher Fauleta2097962019-07-15 16:25:33 +02005102 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5103 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005104 return 0;
5105}
5106
5107__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5108{
5109 struct hlua_txn *htxn;
5110
5111 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5112 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5113
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005114 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005115 WILL_LJMP(lua_error(L));
5116
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005117 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005118}
5119
5120__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5121{
5122 struct hlua_txn *htxn;
5123
5124 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5125 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5126
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005127 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005128 WILL_LJMP(lua_error(L));
5129
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005130 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005131}
5132
5133static int hlua_http_req_set_hdr(lua_State *L)
5134{
5135 struct hlua_txn *htxn;
5136
5137 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5138 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5139
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005140 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005141 WILL_LJMP(lua_error(L));
5142
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005143 hlua_http_del_hdr(L, &htxn->s->txn->req);
5144 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005145}
5146
5147static int hlua_http_res_set_hdr(lua_State *L)
5148{
5149 struct hlua_txn *htxn;
5150
5151 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5152 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5153
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005154 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005155 WILL_LJMP(lua_error(L));
5156
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005157 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5158 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005159}
5160
5161/* This function set the method. */
5162static int hlua_http_req_set_meth(lua_State *L)
5163{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005164 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005165 size_t name_len;
5166 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005167
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005168 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005169 WILL_LJMP(lua_error(L));
5170
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005171 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005172 return 1;
5173}
5174
5175/* This function set the method. */
5176static int hlua_http_req_set_path(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 FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005181
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(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005186 return 1;
5187}
5188
5189/* This function set the query-string. */
5190static int hlua_http_req_set_query(lua_State *L)
5191{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005192 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005193 size_t name_len;
5194 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005195
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005196 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005197 WILL_LJMP(lua_error(L));
5198
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005199 /* Check length. */
5200 if (name_len > trash.size - 1) {
5201 lua_pushboolean(L, 0);
5202 return 1;
5203 }
5204
5205 /* Add the mark question as prefix. */
5206 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005207 trash.area[trash.data++] = '?';
5208 memcpy(trash.area + trash.data, name, name_len);
5209 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005210
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005211 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005212 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005213 return 1;
5214}
5215
5216/* This function set the uri. */
5217static int hlua_http_req_set_uri(lua_State *L)
5218{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005219 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005220 size_t name_len;
5221 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005222
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005223 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005224 WILL_LJMP(lua_error(L));
5225
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005226 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005227 return 1;
5228}
5229
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005230/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005231static int hlua_http_res_set_status(lua_State *L)
5232{
5233 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5234 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005235 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5236 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005237
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005238 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005239 WILL_LJMP(lua_error(L));
5240
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005241 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005242 return 0;
5243}
5244
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005245/*
5246 *
5247 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005248 * Class TXN
5249 *
5250 *
5251 */
5252
5253/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005254 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005255 */
5256__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5257{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005258 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005259}
5260
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005261__LJMP static int hlua_set_var(lua_State *L)
5262{
5263 struct hlua_txn *htxn;
5264 const char *name;
5265 size_t len;
5266 struct sample smp;
5267
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005268 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5269 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005270
5271 /* It is useles to retrieve the stream, but this function
5272 * runs only in a stream context.
5273 */
5274 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5275 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5276
5277 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005278 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005279 hlua_lua2smp(L, 3, &smp);
5280
5281 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005282 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005283
5284 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5285 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5286 else
5287 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5288
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005289 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005290}
5291
Christopher Faulet85d79c92016-11-09 16:54:56 +01005292__LJMP static int hlua_unset_var(lua_State *L)
5293{
5294 struct hlua_txn *htxn;
5295 const char *name;
5296 size_t len;
5297 struct sample smp;
5298
5299 MAY_LJMP(check_args(L, 2, "unset_var"));
5300
5301 /* It is useles to retrieve the stream, but this function
5302 * runs only in a stream context.
5303 */
5304 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5305 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5306
5307 /* Unset the variable. */
5308 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005309 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5310 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005311}
5312
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005313__LJMP static int hlua_get_var(lua_State *L)
5314{
5315 struct hlua_txn *htxn;
5316 const char *name;
5317 size_t len;
5318 struct sample smp;
5319
5320 MAY_LJMP(check_args(L, 2, "get_var"));
5321
5322 /* It is useles to retrieve the stream, but this function
5323 * runs only in a stream context.
5324 */
5325 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5326 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5327
Willy Tarreau7560dd42016-03-10 16:28:58 +01005328 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005329 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005330 lua_pushnil(L);
5331 return 1;
5332 }
5333
5334 return hlua_smp2lua(L, &smp);
5335}
5336
Willy Tarreau59551662015-03-10 14:23:13 +01005337__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005338{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005339 struct hlua *hlua;
5340
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005341 MAY_LJMP(check_args(L, 2, "set_priv"));
5342
Willy Tarreau87b09662015-04-03 00:22:06 +02005343 /* It is useles to retrieve the stream, but this function
5344 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005345 */
5346 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005347
5348 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005349 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005350 if (!hlua)
5351 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005352
5353 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005354 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005355
5356 /* Get and store new value. */
5357 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5358 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5359
5360 return 0;
5361}
5362
Willy Tarreau59551662015-03-10 14:23:13 +01005363__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005364{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005365 struct hlua *hlua;
5366
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005367 MAY_LJMP(check_args(L, 1, "get_priv"));
5368
Willy Tarreau87b09662015-04-03 00:22:06 +02005369 /* It is useles to retrieve the stream, but this function
5370 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005371 */
5372 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005373
5374 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005375 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005376 if (!hlua) {
5377 lua_pushnil(L);
5378 return 1;
5379 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005380
5381 /* Push configuration index in the stack. */
5382 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5383
5384 return 1;
5385}
5386
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005387/* Create stack entry containing a class TXN. This function
5388 * return 0 if the stack does not contains free slots,
5389 * otherwise it returns 1.
5390 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005391static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005392{
Willy Tarreaude491382015-04-06 11:04:28 +02005393 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005394
5395 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005396 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005397 return 0;
5398
5399 /* NOTE: The allocation never fails. The failure
5400 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005401 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005402 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005403 /* Create the object: obj[0] = userdata. */
5404 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005405 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005406 lua_rawseti(L, -2, 0);
5407
Willy Tarreaude491382015-04-06 11:04:28 +02005408 htxn->s = s;
5409 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005410 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005411 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005412
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005413 /* Create the "f" field that contains a list of fetches. */
5414 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005415 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005416 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005417 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005418
5419 /* Create the "sf" field that contains a list of stringsafe fetches. */
5420 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005421 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005422 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005423 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005424
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005425 /* Create the "c" field that contains a list of converters. */
5426 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005427 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005428 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005429 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005430
5431 /* Create the "sc" field that contains a list of stringsafe converters. */
5432 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005433 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005434 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005435 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005436
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005437 /* Create the "req" field that contains the request channel object. */
5438 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005439 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005440 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005441 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005442
5443 /* Create the "res" field that contains the response channel object. */
5444 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005445 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005446 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005447 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005448
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005449 /* Creates the HTTP object is the current proxy allows http. */
5450 lua_pushstring(L, "http");
5451 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005452 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005453 return 0;
5454 }
5455 else
5456 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005457 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005458
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005459 /* Pop a class sesison metatable and affect it to the userdata. */
5460 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5461 lua_setmetatable(L, -2);
5462
5463 return 1;
5464}
5465
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005466__LJMP static int hlua_txn_deflog(lua_State *L)
5467{
5468 const char *msg;
5469 struct hlua_txn *htxn;
5470
5471 MAY_LJMP(check_args(L, 2, "deflog"));
5472 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5473 msg = MAY_LJMP(luaL_checkstring(L, 2));
5474
5475 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5476 return 0;
5477}
5478
5479__LJMP static int hlua_txn_log(lua_State *L)
5480{
5481 int level;
5482 const char *msg;
5483 struct hlua_txn *htxn;
5484
5485 MAY_LJMP(check_args(L, 3, "log"));
5486 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5487 level = MAY_LJMP(luaL_checkinteger(L, 2));
5488 msg = MAY_LJMP(luaL_checkstring(L, 3));
5489
5490 if (level < 0 || level >= NB_LOG_LEVELS)
5491 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5492
5493 hlua_sendlog(htxn->s->be, level, msg);
5494 return 0;
5495}
5496
5497__LJMP static int hlua_txn_log_debug(lua_State *L)
5498{
5499 const char *msg;
5500 struct hlua_txn *htxn;
5501
5502 MAY_LJMP(check_args(L, 2, "Debug"));
5503 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5504 msg = MAY_LJMP(luaL_checkstring(L, 2));
5505 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5506 return 0;
5507}
5508
5509__LJMP static int hlua_txn_log_info(lua_State *L)
5510{
5511 const char *msg;
5512 struct hlua_txn *htxn;
5513
5514 MAY_LJMP(check_args(L, 2, "Info"));
5515 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5516 msg = MAY_LJMP(luaL_checkstring(L, 2));
5517 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5518 return 0;
5519}
5520
5521__LJMP static int hlua_txn_log_warning(lua_State *L)
5522{
5523 const char *msg;
5524 struct hlua_txn *htxn;
5525
5526 MAY_LJMP(check_args(L, 2, "Warning"));
5527 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5528 msg = MAY_LJMP(luaL_checkstring(L, 2));
5529 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5530 return 0;
5531}
5532
5533__LJMP static int hlua_txn_log_alert(lua_State *L)
5534{
5535 const char *msg;
5536 struct hlua_txn *htxn;
5537
5538 MAY_LJMP(check_args(L, 2, "Alert"));
5539 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5540 msg = MAY_LJMP(luaL_checkstring(L, 2));
5541 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5542 return 0;
5543}
5544
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005545__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5546{
5547 struct hlua_txn *htxn;
5548 int ll;
5549
5550 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5551 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5552 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5553
5554 if (ll < 0 || ll > 7)
5555 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5556
5557 htxn->s->logs.level = ll;
5558 return 0;
5559}
5560
5561__LJMP static int hlua_txn_set_tos(lua_State *L)
5562{
5563 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005564 int tos;
5565
5566 MAY_LJMP(check_args(L, 2, "set_tos"));
5567 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5568 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5569
Willy Tarreau1a18b542018-12-11 16:37:42 +01005570 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005571 return 0;
5572}
5573
5574__LJMP static int hlua_txn_set_mark(lua_State *L)
5575{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005576 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005577 int mark;
5578
5579 MAY_LJMP(check_args(L, 2, "set_mark"));
5580 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5581 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5582
Lukas Tribus579e3e32019-08-11 18:03:45 +02005583 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005584 return 0;
5585}
5586
Patrick Hemmer268a7072018-05-11 12:52:31 -04005587__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5588{
5589 struct hlua_txn *htxn;
5590
5591 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5592 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5593 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5594 return 0;
5595}
5596
5597__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5598{
5599 struct hlua_txn *htxn;
5600
5601 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5602 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5603 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5604 return 0;
5605}
5606
Christopher Faulet700d9e82020-01-31 12:21:52 +01005607/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005608 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005609 * message and terminate the transaction. It returns 1 on success and 0 on
5610 * error. The Reply must be on top of the stack.
5611 */
5612__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5613{
5614 struct htx *htx;
5615 struct htx_sl *sl;
5616 struct h1m h1m;
5617 const char *status, *reason, *body;
5618 size_t status_len, reason_len, body_len;
5619 int ret, code, flags;
5620
5621 code = 200;
5622 status = "200";
5623 status_len = 3;
5624 ret = lua_getfield(L, -1, "status");
5625 if (ret == LUA_TNUMBER) {
5626 code = lua_tointeger(L, -1);
5627 status = lua_tolstring(L, -1, &status_len);
5628 }
5629 lua_pop(L, 1);
5630
5631 reason = http_get_reason(code);
5632 reason_len = strlen(reason);
5633 ret = lua_getfield(L, -1, "reason");
5634 if (ret == LUA_TSTRING)
5635 reason = lua_tolstring(L, -1, &reason_len);
5636 lua_pop(L, 1);
5637
5638 body = NULL;
5639 body_len = 0;
5640 ret = lua_getfield(L, -1, "body");
5641 if (ret == LUA_TSTRING)
5642 body = lua_tolstring(L, -1, &body_len);
5643 lua_pop(L, 1);
5644
5645 /* Prepare the response before inserting the headers */
5646 h1m_init_res(&h1m);
5647 htx = htx_from_buf(&s->res.buf);
5648 channel_htx_truncate(&s->res, htx);
5649 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5650 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5651 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5652 ist2(status, status_len), ist2(reason, reason_len));
5653 }
5654 else {
5655 flags = HTX_SL_F_IS_RESP;
5656 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5657 ist2(status, status_len), ist2(reason, reason_len));
5658 }
5659 if (!sl)
5660 goto fail;
5661 sl->info.res.status = code;
5662
5663 /* Push in the stack the "headers" entry. */
5664 ret = lua_getfield(L, -1, "headers");
5665 if (ret != LUA_TTABLE)
5666 goto skip_headers;
5667
5668 lua_pushnil(L);
5669 while (lua_next(L, -2) != 0) {
5670 struct ist name, value;
5671 const char *n, *v;
5672 size_t nlen, vlen;
5673
5674 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5675 /* Skip element if the key is not a string or if the value is not a table */
5676 goto next_hdr;
5677 }
5678
5679 n = lua_tolstring(L, -2, &nlen);
5680 name = ist2(n, nlen);
5681 if (isteqi(name, ist("content-length"))) {
5682 /* Always skip content-length header. It will be added
5683 * later with the correct len
5684 */
5685 goto next_hdr;
5686 }
5687
5688 /* Loop on header's values */
5689 lua_pushnil(L);
5690 while (lua_next(L, -2)) {
5691 if (!lua_isstring(L, -1)) {
5692 /* Skip the value if it is not a string */
5693 goto next_value;
5694 }
5695
5696 v = lua_tolstring(L, -1, &vlen);
5697 value = ist2(v, vlen);
5698
5699 if (isteqi(name, ist("transfer-encoding")))
5700 h1_parse_xfer_enc_header(&h1m, value);
5701 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5702 goto fail;
5703
5704 next_value:
5705 lua_pop(L, 1);
5706 }
5707
5708 next_hdr:
5709 lua_pop(L, 1);
5710 }
5711 skip_headers:
5712 lua_pop(L, 1);
5713
5714 /* Update h1m flags: CLEN is set if CHNK is not present */
5715 if (!(h1m.flags & H1_MF_CHNK)) {
5716 const char *clen = ultoa(body_len);
5717
5718 h1m.flags |= H1_MF_CLEN;
5719 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5720 goto fail;
5721 }
5722 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5723 h1m.flags |= H1_MF_XFER_LEN;
5724
5725 /* Update HTX start-line flags */
5726 if (h1m.flags & H1_MF_XFER_ENC)
5727 flags |= HTX_SL_F_XFER_ENC;
5728 if (h1m.flags & H1_MF_XFER_LEN) {
5729 flags |= HTX_SL_F_XFER_LEN;
5730 if (h1m.flags & H1_MF_CHNK)
5731 flags |= HTX_SL_F_CHNK;
5732 else if (h1m.flags & H1_MF_CLEN)
5733 flags |= HTX_SL_F_CLEN;
5734 if (h1m.body_len == 0)
5735 flags |= HTX_SL_F_BODYLESS;
5736 }
5737 sl->flags |= flags;
5738
5739
5740 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5741 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5742 !htx_add_endof(htx, HTX_BLK_EOM))
5743 goto fail;
5744
5745 /* Now, forward the response and terminate the transaction */
5746 s->txn->status = code;
5747 htx_to_buf(htx, &s->res.buf);
5748 if (!http_forward_proxy_resp(s, 1))
5749 goto fail;
5750
5751 return 1;
5752
5753 fail:
5754 channel_htx_truncate(&s->res, htx);
5755 return 0;
5756}
5757
5758/* Terminate a transaction if called from a lua action. For TCP streams,
5759 * processing is just aborted. Nothing is returned to the client and all
5760 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5761 * is forwarded to the client before terminating the transaction. On success,
5762 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5763 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5764 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005765 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005766__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005767{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005768 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005769 struct stream *s;
5770 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005771
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005772 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005773
Christopher Faulet700d9e82020-01-31 12:21:52 +01005774 /* If the flags NOTERM is set, we cannot terminate the session, so we
5775 * just end the execution of the current lua code. */
5776 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005777 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005778
Christopher Faulet700d9e82020-01-31 12:21:52 +01005779 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005780 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005781 struct channel *req = &s->req;
5782 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005783
Christopher Faulet700d9e82020-01-31 12:21:52 +01005784 channel_auto_read(req);
5785 channel_abort(req);
5786 channel_auto_close(req);
5787 channel_erase(req);
5788
5789 res->wex = tick_add_ifset(now_ms, res->wto);
5790 channel_auto_read(res);
5791 channel_auto_close(res);
5792 channel_shutr_now(res);
5793
5794 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5795 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005796 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005797
Christopher Faulet700d9e82020-01-31 12:21:52 +01005798 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5799 /* No reply or invalid reply */
5800 s->txn->status = 0;
5801 http_reply_and_close(s, 0, NULL);
5802 }
5803 else {
5804 /* Remove extra args to have the reply on top of the stack */
5805 if (lua_gettop(L) > 2)
5806 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005807
Christopher Faulet700d9e82020-01-31 12:21:52 +01005808 if (!hlua_txn_forward_reply(L, s)) {
5809 if (!(s->flags & SF_ERR_MASK))
5810 s->flags |= SF_ERR_PRXCOND;
5811 lua_pushinteger(L, ACT_RET_ERR);
5812 WILL_LJMP(hlua_done(L));
5813 return 0; /* Never reached */
5814 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005815 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005816
Christopher Faulet700d9e82020-01-31 12:21:52 +01005817 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5818 if (htxn->dir == SMP_OPT_DIR_REQ) {
5819 /* let's log the request time */
5820 s->logs.tv_request = now;
5821 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5822 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5823 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005824
Christopher Faulet700d9e82020-01-31 12:21:52 +01005825 done:
5826 if (!(s->flags & SF_ERR_MASK))
5827 s->flags |= SF_ERR_LOCAL;
5828 if (!(s->flags & SF_FINST_MASK))
5829 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005830
Christopher Faulet4ad73102020-03-05 11:07:31 +01005831 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005832 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005833 return 0;
5834}
5835
Christopher Faulet700d9e82020-01-31 12:21:52 +01005836/*
5837 *
5838 *
5839 * Class REPLY
5840 *
5841 *
5842 */
5843
5844/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5845 * free slots, the function fails and returns 0;
5846 */
5847static int hlua_txn_reply_new(lua_State *L)
5848{
5849 struct hlua_txn *htxn;
5850 const char *reason, *body = NULL;
5851 int ret, status;
5852
5853 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005854 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005855 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5856 WILL_LJMP(lua_error(L));
5857 }
5858
5859 /* Default value */
5860 status = 200;
5861 reason = http_get_reason(status);
5862
5863 if (lua_istable(L, 2)) {
5864 /* load status and reason from the table argument at index 2 */
5865 ret = lua_getfield(L, 2, "status");
5866 if (ret == LUA_TNIL)
5867 goto reason;
5868 else if (ret != LUA_TNUMBER) {
5869 /* invalid status: ignore the reason */
5870 goto body;
5871 }
5872 status = lua_tointeger(L, -1);
5873
5874 reason:
5875 lua_pop(L, 1); /* restore the stack: remove status */
5876 ret = lua_getfield(L, 2, "reason");
5877 if (ret == LUA_TSTRING)
5878 reason = lua_tostring(L, -1);
5879
5880 body:
5881 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5882 ret = lua_getfield(L, 2, "body");
5883 if (ret == LUA_TSTRING)
5884 body = lua_tostring(L, -1);
5885 lua_pop(L, 1); /* restore the stack: remove body */
5886 }
5887
5888 /* Create the Reply table */
5889 lua_newtable(L);
5890
5891 /* Add status element */
5892 lua_pushstring(L, "status");
5893 lua_pushinteger(L, status);
5894 lua_settable(L, -3);
5895
5896 /* Add reason element */
5897 reason = http_get_reason(status);
5898 lua_pushstring(L, "reason");
5899 lua_pushstring(L, reason);
5900 lua_settable(L, -3);
5901
5902 /* Add body element, nil if undefined */
5903 lua_pushstring(L, "body");
5904 if (body)
5905 lua_pushstring(L, body);
5906 else
5907 lua_pushnil(L);
5908 lua_settable(L, -3);
5909
5910 /* Add headers element */
5911 lua_pushstring(L, "headers");
5912 lua_newtable(L);
5913
5914 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5915 if (lua_istable(L, 2)) {
5916 /* load headers from the table argument at index 2. If it is a table, copy it. */
5917 ret = lua_getfield(L, 2, "headers");
5918 if (ret == LUA_TTABLE) {
5919 /* stack: [ ... <headers:table>, <table> ] */
5920 lua_pushnil(L);
5921 while (lua_next(L, -2) != 0) {
5922 /* stack: [ ... <headers:table>, <table>, k, v] */
5923 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5924 /* invalid value type, skip it */
5925 lua_pop(L, 1);
5926 continue;
5927 }
5928
5929
5930 /* Duplicate the key and swap it with the value. */
5931 lua_pushvalue(L, -2);
5932 lua_insert(L, -2);
5933 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5934
5935 lua_newtable(L);
5936 lua_insert(L, -2);
5937 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5938
5939 if (lua_isstring(L, -1)) {
5940 /* push the value in the inner table */
5941 lua_rawseti(L, -2, 1);
5942 }
5943 else { /* table */
5944 lua_pushnil(L);
5945 while (lua_next(L, -2) != 0) {
5946 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5947 if (!lua_isstring(L, -1)) {
5948 /* invalid value type, skip it*/
5949 lua_pop(L, 1);
5950 continue;
5951 }
5952 /* push the value in the inner table */
5953 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5954 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5955 }
5956 lua_pop(L, 1);
5957 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5958 }
5959
5960 /* push (k,v) on the stack in the headers table:
5961 * stack: [ ... <headers:table>, <table>, k, k, v ]
5962 */
5963 lua_settable(L, -5);
5964 /* stack: [ ... <headers:table>, <table>, k ] */
5965 }
5966 }
5967 lua_pop(L, 1);
5968 }
5969 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5970 lua_settable(L, -3);
5971 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5972
5973 /* Pop a class sesison metatable and affect it to the userdata. */
5974 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5975 lua_setmetatable(L, -2);
5976 return 1;
5977}
5978
5979/* Set the reply status code, and optionally the reason. If no reason is
5980 * provided, the default one corresponding to the status code is used.
5981 */
5982__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5983{
5984 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5985 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5986
5987 /* First argument (self) must be a table */
5988 luaL_checktype(L, 1, LUA_TTABLE);
5989
5990 if (status < 100 || status > 599) {
5991 lua_pushboolean(L, 0);
5992 return 1;
5993 }
5994 if (!reason)
5995 reason = http_get_reason(status);
5996
5997 lua_pushinteger(L, status);
5998 lua_setfield(L, 1, "status");
5999
6000 lua_pushstring(L, reason);
6001 lua_setfield(L, 1, "reason");
6002
6003 lua_pushboolean(L, 1);
6004 return 1;
6005}
6006
6007/* Add a header into the reply object. Each header name is associated to an
6008 * array of values in the "headers" table. If the header name is not found, a
6009 * new entry is created.
6010 */
6011__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6012{
6013 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6014 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6015 int ret;
6016
6017 /* First argument (self) must be a table */
6018 luaL_checktype(L, 1, LUA_TTABLE);
6019
6020 /* Push in the stack the "headers" entry. */
6021 ret = lua_getfield(L, 1, "headers");
6022 if (ret != LUA_TTABLE) {
6023 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6024 WILL_LJMP(lua_error(L));
6025 }
6026
6027 /* check if the header is already registered. If not, register it. */
6028 ret = lua_getfield(L, -1, name);
6029 if (ret == LUA_TNIL) {
6030 /* Entry not found. */
6031 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6032
6033 /* Insert the new header name in the array in the top of the stack.
6034 * It left the new array in the top of the stack.
6035 */
6036 lua_newtable(L);
6037 lua_pushstring(L, name);
6038 lua_pushvalue(L, -2);
6039 lua_settable(L, -4);
6040 }
6041 else if (ret != LUA_TTABLE) {
6042 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6043 WILL_LJMP(lua_error(L));
6044 }
6045
6046 /* Now the top od thestack is an array of values. We push
6047 * the header value as new entry.
6048 */
6049 lua_pushstring(L, value);
6050 ret = lua_rawlen(L, -2);
6051 lua_rawseti(L, -2, ret + 1);
6052
6053 lua_pushboolean(L, 1);
6054 return 1;
6055}
6056
6057/* Remove all occurrences of a given header name. */
6058__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6059{
6060 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6061 int ret;
6062
6063 /* First argument (self) must be a table */
6064 luaL_checktype(L, 1, LUA_TTABLE);
6065
6066 /* Push in the stack the "headers" entry. */
6067 ret = lua_getfield(L, 1, "headers");
6068 if (ret != LUA_TTABLE) {
6069 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6070 WILL_LJMP(lua_error(L));
6071 }
6072
6073 lua_pushstring(L, name);
6074 lua_pushnil(L);
6075 lua_settable(L, -3);
6076
6077 lua_pushboolean(L, 1);
6078 return 1;
6079}
6080
6081/* Set the reply's body. Overwrite any existing entry. */
6082__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6083{
6084 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6085
6086 /* First argument (self) must be a table */
6087 luaL_checktype(L, 1, LUA_TTABLE);
6088
6089 lua_pushstring(L, payload);
6090 lua_setfield(L, 1, "body");
6091
6092 lua_pushboolean(L, 1);
6093 return 1;
6094}
6095
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006096__LJMP static int hlua_log(lua_State *L)
6097{
6098 int level;
6099 const char *msg;
6100
6101 MAY_LJMP(check_args(L, 2, "log"));
6102 level = MAY_LJMP(luaL_checkinteger(L, 1));
6103 msg = MAY_LJMP(luaL_checkstring(L, 2));
6104
6105 if (level < 0 || level >= NB_LOG_LEVELS)
6106 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6107
6108 hlua_sendlog(NULL, level, msg);
6109 return 0;
6110}
6111
6112__LJMP static int hlua_log_debug(lua_State *L)
6113{
6114 const char *msg;
6115
6116 MAY_LJMP(check_args(L, 1, "debug"));
6117 msg = MAY_LJMP(luaL_checkstring(L, 1));
6118 hlua_sendlog(NULL, LOG_DEBUG, msg);
6119 return 0;
6120}
6121
6122__LJMP static int hlua_log_info(lua_State *L)
6123{
6124 const char *msg;
6125
6126 MAY_LJMP(check_args(L, 1, "info"));
6127 msg = MAY_LJMP(luaL_checkstring(L, 1));
6128 hlua_sendlog(NULL, LOG_INFO, msg);
6129 return 0;
6130}
6131
6132__LJMP static int hlua_log_warning(lua_State *L)
6133{
6134 const char *msg;
6135
6136 MAY_LJMP(check_args(L, 1, "warning"));
6137 msg = MAY_LJMP(luaL_checkstring(L, 1));
6138 hlua_sendlog(NULL, LOG_WARNING, msg);
6139 return 0;
6140}
6141
6142__LJMP static int hlua_log_alert(lua_State *L)
6143{
6144 const char *msg;
6145
6146 MAY_LJMP(check_args(L, 1, "alert"));
6147 msg = MAY_LJMP(luaL_checkstring(L, 1));
6148 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006149 return 0;
6150}
6151
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006152__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006153{
6154 int wakeup_ms = lua_tointeger(L, -1);
6155 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006156 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006157 return 0;
6158}
6159
6160__LJMP static int hlua_sleep(lua_State *L)
6161{
6162 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006163 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006164
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006165 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006166
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006167 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006168 wakeup_ms = tick_add(now_ms, delay);
6169 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006170
Willy Tarreau9635e032018-10-16 17:52:55 +02006171 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006172 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006173}
6174
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006175__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006176{
6177 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006178 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006179
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006180 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006181
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006182 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006183 wakeup_ms = tick_add(now_ms, delay);
6184 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006185
Willy Tarreau9635e032018-10-16 17:52:55 +02006186 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006187 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006188}
6189
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006190/* This functionis an LUA binding. it permits to give back
6191 * the hand at the HAProxy scheduler. It is used when the
6192 * LUA processing consumes a lot of time.
6193 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006194__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006195{
6196 return 0;
6197}
6198
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006199__LJMP static int hlua_yield(lua_State *L)
6200{
Willy Tarreau9635e032018-10-16 17:52:55 +02006201 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006202 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006203}
6204
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006205/* This function change the nice of the currently executed
6206 * task. It is used set low or high priority at the current
6207 * task.
6208 */
Willy Tarreau59551662015-03-10 14:23:13 +01006209__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006210{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006211 struct hlua *hlua;
6212 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006213
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006214 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006215 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006216
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006217 /* Get hlua struct, or NULL if we execute from main lua state */
6218 hlua = hlua_gethlua(L);
6219
6220 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006221 if (!hlua || !hlua->task)
6222 return 0;
6223
6224 if (nice < -1024)
6225 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006226 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006227 nice = 1024;
6228
6229 hlua->task->nice = nice;
6230 return 0;
6231}
6232
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006233/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006234 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6235 * return an E_AGAIN signal, the emmiter of this signal must set a
6236 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006237 *
6238 * Task wrapper are longjmp safe because the only one Lua code
6239 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006240 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006241struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006242{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006243 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006244 enum hlua_exec status;
6245
Christopher Faulet5bc99722018-04-25 10:34:45 +02006246 if (task->thread_mask == MAX_THREADS_MASK)
6247 task_set_affinity(task, tid_bit);
6248
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006249 /* If it is the first call to the task, we must initialize the
6250 * execution timeouts.
6251 */
6252 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006253 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006254
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006255 /* Execute the Lua code. */
6256 status = hlua_ctx_resume(hlua, 1);
6257
6258 switch (status) {
6259 /* finished or yield */
6260 case HLUA_E_OK:
6261 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006262 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006263 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006264 break;
6265
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006266 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006267 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006268 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006269 break;
6270
6271 /* finished with error. */
6272 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006273 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006274 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006275 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006276 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006277 break;
6278
6279 case HLUA_E_ERR:
6280 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006281 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006282 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006283 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006284 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006285 break;
6286 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006287 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006288}
6289
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006290/* This function is an LUA binding that register LUA function to be
6291 * executed after the HAProxy configuration parsing and before the
6292 * HAProxy scheduler starts. This function expect only one LUA
6293 * argument that is a function. This function returns nothing, but
6294 * throws if an error is encountered.
6295 */
6296__LJMP static int hlua_register_init(lua_State *L)
6297{
6298 struct hlua_init_function *init;
6299 int ref;
6300
6301 MAY_LJMP(check_args(L, 1, "register_init"));
6302
6303 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6304
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006305 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006306 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006307 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006308
6309 init->function_ref = ref;
6310 LIST_ADDQ(&hlua_init_functions, &init->l);
6311 return 0;
6312}
6313
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006314/* This functio is an LUA binding. It permits to register a task
6315 * executed in parallel of the main HAroxy activity. The task is
6316 * created and it is set in the HAProxy scheduler. It can be called
6317 * from the "init" section, "post init" or during the runtime.
6318 *
6319 * Lua prototype:
6320 *
6321 * <none> core.register_task(<function>)
6322 */
6323static int hlua_register_task(lua_State *L)
6324{
6325 struct hlua *hlua;
6326 struct task *task;
6327 int ref;
6328
6329 MAY_LJMP(check_args(L, 1, "register_task"));
6330
6331 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6332
Willy Tarreaubafbe012017-11-24 17:34:44 +01006333 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006334 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006335 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006336
Emeric Brunc60def82017-09-27 14:59:38 +02006337 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006338 if (!task)
6339 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6340
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006341 task->context = hlua;
6342 task->process = hlua_process_task;
6343
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006344 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006345 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006346
6347 /* Restore the function in the stack. */
6348 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6349 hlua->nargs = 0;
6350
6351 /* Schedule task. */
6352 task_schedule(task, now_ms);
6353
6354 return 0;
6355}
6356
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006357/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6358 * doesn't allow "yield" functions because the HAProxy engine cannot
6359 * resume converters.
6360 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006361static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006362{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006363 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006364 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006365 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006366
Willy Tarreaube508f12016-03-10 11:47:01 +01006367 if (!stream)
6368 return 0;
6369
Willy Tarreau87b09662015-04-03 00:22:06 +02006370 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006371 * Lua context can be not initialized. This behavior
6372 * permits to save performances because a systematic
6373 * Lua initialization cause 5% performances loss.
6374 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006375 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006376 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006377 if (!stream->hlua) {
6378 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6379 return 0;
6380 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006381 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006382 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6383 return 0;
6384 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006385 }
6386
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006387 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006388 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006389
6390 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006391 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6392 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6393 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006394 else
6395 error = "critical error";
6396 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006397 return 0;
6398 }
6399
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006400 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006401 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006402 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006403 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006404 return 0;
6405 }
6406
6407 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006408 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006409
6410 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006411 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006412 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006413 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006414 return 0;
6415 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006416 hlua_smp2lua(stream->hlua->T, smp);
6417 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006418
6419 /* push keywords in the stack. */
6420 if (arg_p) {
6421 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006422 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006423 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006424 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006425 return 0;
6426 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006427 hlua_arg2lua(stream->hlua->T, arg_p);
6428 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006429 }
6430 }
6431
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006432 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006433 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006434
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006435 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006436 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006437 }
6438
6439 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006440 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006441 /* finished. */
6442 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006443 /* If the stack is empty, the function fails. */
6444 if (lua_gettop(stream->hlua->T) <= 0)
6445 return 0;
6446
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006447 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006448 hlua_lua2smp(stream->hlua->T, -1, smp);
6449 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006450 return 1;
6451
6452 /* yield. */
6453 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006454 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006455 return 0;
6456
6457 /* finished with error. */
6458 case HLUA_E_ERRMSG:
6459 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006460 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006461 fcn->name, lua_tostring(stream->hlua->T, -1));
6462 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006463 return 0;
6464
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006465 case HLUA_E_ETMOUT:
6466 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6467 return 0;
6468
6469 case HLUA_E_NOMEM:
6470 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6471 return 0;
6472
6473 case HLUA_E_YIELD:
6474 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6475 return 0;
6476
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006477 case HLUA_E_ERR:
6478 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006479 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006480 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006481
6482 default:
6483 return 0;
6484 }
6485}
6486
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006487/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6488 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006489 * resume sample-fetches. This function will be called by the sample
6490 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006491 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006492static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6493 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006494{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006495 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006496 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006497 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006498 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006499
Willy Tarreaube508f12016-03-10 11:47:01 +01006500 if (!stream)
6501 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006502
Willy Tarreau87b09662015-04-03 00:22:06 +02006503 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006504 * Lua context can be not initialized. This behavior
6505 * permits to save performances because a systematic
6506 * Lua initialization cause 5% performances loss.
6507 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006508 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006509 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006510 if (!stream->hlua) {
6511 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6512 return 0;
6513 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006514 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006515 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6516 return 0;
6517 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006518 }
6519
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006520 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006521 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006522
6523 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006524 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6525 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6526 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006527 else
6528 error = "critical error";
6529 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006530 return 0;
6531 }
6532
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006533 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006534 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006535 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006536 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006537 return 0;
6538 }
6539
6540 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006541 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006542
6543 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006544 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006545 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006546 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006547 return 0;
6548 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006549 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006550
6551 /* push keywords in the stack. */
6552 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6553 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006554 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006555 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006556 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006557 return 0;
6558 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006559 hlua_arg2lua(stream->hlua->T, arg_p);
6560 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006561 }
6562
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006563 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006564 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006565
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006566 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006568 }
6569
6570 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006571 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006572 /* finished. */
6573 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006574 /* If the stack is empty, the function fails. */
6575 if (lua_gettop(stream->hlua->T) <= 0)
6576 return 0;
6577
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006578 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006579 hlua_lua2smp(stream->hlua->T, -1, smp);
6580 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006581
6582 /* Set the end of execution flag. */
6583 smp->flags &= ~SMP_F_MAY_CHANGE;
6584 return 1;
6585
6586 /* yield. */
6587 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006588 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006589 return 0;
6590
6591 /* finished with error. */
6592 case HLUA_E_ERRMSG:
6593 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006594 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006595 fcn->name, lua_tostring(stream->hlua->T, -1));
6596 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006597 return 0;
6598
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006599 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006600 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6601 return 0;
6602
6603 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006604 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6605 return 0;
6606
6607 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006608 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6609 return 0;
6610
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006611 case HLUA_E_ERR:
6612 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006613 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006614 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006615
6616 default:
6617 return 0;
6618 }
6619}
6620
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006621/* This function is an LUA binding used for registering
6622 * "sample-conv" functions. It expects a converter name used
6623 * in the haproxy configuration file, and an LUA function.
6624 */
6625__LJMP static int hlua_register_converters(lua_State *L)
6626{
6627 struct sample_conv_kw_list *sck;
6628 const char *name;
6629 int ref;
6630 int len;
6631 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006632 struct sample_conv *sc;
6633 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006634
6635 MAY_LJMP(check_args(L, 2, "register_converters"));
6636
6637 /* First argument : converter name. */
6638 name = MAY_LJMP(luaL_checkstring(L, 1));
6639
6640 /* Second argument : lua function. */
6641 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6642
Thierry Fournierf67442e2020-11-28 20:41:07 +01006643 /* Check if the converter is already registered */
6644 trash = get_trash_chunk();
6645 chunk_printf(trash, "lua.%s", name);
6646 sc = find_sample_conv(trash->area, trash->data);
6647 if (sc != NULL) {
6648 ha_warning("Trying to register converter 'lua.%s' more than once. "
6649 "This will become a hard error in version 2.5.\n", name);
6650 }
6651
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006652 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006653 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006654 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006655 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006656 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006657 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006658 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006659
6660 /* Fill fcn. */
6661 fcn->name = strdup(name);
6662 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006663 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006664 fcn->function_ref = ref;
6665
6666 /* List head */
6667 sck->list.n = sck->list.p = NULL;
6668
6669 /* converter keyword. */
6670 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006671 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006672 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006673 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006674
6675 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6676 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006677 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 +01006678 sck->kw[0].val_args = NULL;
6679 sck->kw[0].in_type = SMP_T_STR;
6680 sck->kw[0].out_type = SMP_T_STR;
6681 sck->kw[0].private = fcn;
6682
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006683 /* Register this new converter */
6684 sample_register_convs(sck);
6685
6686 return 0;
6687}
6688
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006689/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006690 * "sample-fetch" functions. It expects a converter name used
6691 * in the haproxy configuration file, and an LUA function.
6692 */
6693__LJMP static int hlua_register_fetches(lua_State *L)
6694{
6695 const char *name;
6696 int ref;
6697 int len;
6698 struct sample_fetch_kw_list *sfk;
6699 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006700 struct sample_fetch *sf;
6701 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006702
6703 MAY_LJMP(check_args(L, 2, "register_fetches"));
6704
6705 /* First argument : sample-fetch name. */
6706 name = MAY_LJMP(luaL_checkstring(L, 1));
6707
6708 /* Second argument : lua function. */
6709 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6710
Thierry Fournierf67442e2020-11-28 20:41:07 +01006711 /* Check if the sample-fetch is already registered */
6712 trash = get_trash_chunk();
6713 chunk_printf(trash, "lua.%s", name);
6714 sf = find_sample_fetch(trash->area, trash->data);
6715 if (sf != NULL) {
6716 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6717 "This will become a hard error in version 2.5.\n", name);
6718 }
6719
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006720 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006721 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006722 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006723 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006724 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006725 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006726 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006727
6728 /* Fill fcn. */
6729 fcn->name = strdup(name);
6730 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006731 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006732 fcn->function_ref = ref;
6733
6734 /* List head */
6735 sfk->list.n = sfk->list.p = NULL;
6736
6737 /* sample-fetch keyword. */
6738 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006739 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006740 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006741 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006742
6743 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6744 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006745 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 +01006746 sfk->kw[0].val_args = NULL;
6747 sfk->kw[0].out_type = SMP_T_STR;
6748 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6749 sfk->kw[0].val = 0;
6750 sfk->kw[0].private = fcn;
6751
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006752 /* Register this new fetch. */
6753 sample_register_fetches(sfk);
6754
6755 return 0;
6756}
6757
Christopher Faulet501465d2020-02-26 14:54:16 +01006758/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006759 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006760__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006761{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006762 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006763 unsigned int delay;
6764 unsigned int wakeup_ms;
6765
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006766 /* Get hlua struct, or NULL if we execute from main lua state */
6767 hlua = hlua_gethlua(L);
6768 if (!hlua) {
6769 return 0;
6770 }
6771
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006772 MAY_LJMP(check_args(L, 1, "wake_time"));
6773
6774 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6775 wakeup_ms = tick_add(now_ms, delay);
6776 hlua->wake_time = wakeup_ms;
6777 return 0;
6778}
6779
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006780/* This function is a wrapper to execute each LUA function declared as an action
6781 * wrapper during the initialisation period. This function may return any
6782 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6783 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6784 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006785 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006786static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006787 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006788{
6789 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006790 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006791 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006792 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006793
6794 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006795 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6796 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6797 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6798 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006799 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006800 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006801 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006802 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006803
Willy Tarreau87b09662015-04-03 00:22:06 +02006804 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006805 * Lua context can be not initialized. This behavior
6806 * permits to save performances because a systematic
6807 * Lua initialization cause 5% performances loss.
6808 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006809 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006810 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006811 if (!s->hlua) {
6812 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6813 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006814 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006815 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006816 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006817 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6818 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006819 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006820 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006821 }
6822
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006823 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006824 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006825
6826 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006827 if (!SET_SAFE_LJMP(s->hlua->T)) {
6828 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6829 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006830 else
6831 error = "critical error";
6832 SEND_ERR(px, "Lua function '%s': %s.\n",
6833 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006834 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006835 }
6836
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006837 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006838 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006839 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006840 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006841 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006842 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006843 }
6844
6845 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006846 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006847
Willy Tarreau87b09662015-04-03 00:22:06 +02006848 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006849 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006850 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006851 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006852 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006853 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006854 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006855 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006856
6857 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006858 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006859 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006860 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006861 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006862 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006863 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006864 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006865 lua_pushstring(s->hlua->T, *arg);
6866 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006867 }
6868
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006869 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006870 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006871
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006872 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006873 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006874 }
6875
6876 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006877 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006878 /* finished. */
6879 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006880 /* Catch the return value */
6881 if (lua_gettop(s->hlua->T) > 0)
6882 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006883
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006884 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006885 if (act_ret == ACT_RET_YIELD) {
6886 if (flags & ACT_OPT_FINAL)
6887 goto err_yield;
6888
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006889 if (dir == SMP_OPT_DIR_REQ)
6890 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6891 s->hlua->wake_time);
6892 else
6893 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6894 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006895 }
6896 goto end;
6897
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006898 /* yield. */
6899 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006900 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006901 if (dir == SMP_OPT_DIR_REQ)
6902 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6903 s->hlua->wake_time);
6904 else
6905 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6906 s->hlua->wake_time);
6907
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006908 /* Some actions can be wake up when a "write" event
6909 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006910 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006911 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006912 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006913 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006914 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006915 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006916 act_ret = ACT_RET_YIELD;
6917 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006918
6919 /* finished with error. */
6920 case HLUA_E_ERRMSG:
6921 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006922 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006923 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6924 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006925 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006926
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006927 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006928 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006929 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006930
6931 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006932 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006933 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006934
6935 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006936 err_yield:
6937 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006938 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6939 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006940 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006941
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006942 case HLUA_E_ERR:
6943 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006944 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006945 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006946
6947 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006948 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006949 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006950
6951 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006952 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006953 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006954 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006955}
6956
Olivier Houchard9f6af332018-05-25 14:04:04 +02006957struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006958{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006959 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006960
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006961 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006962 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006963 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006964}
6965
6966static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6967{
6968 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006969 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006970 struct task *task;
6971 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006972 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006973
Willy Tarreaubafbe012017-11-24 17:34:44 +01006974 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006975 if (!hlua) {
6976 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6977 ctx->rule->arg.hlua_rule->fcn.name);
6978 return 0;
6979 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006980 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006981 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006982 ctx->ctx.hlua_apptcp.flags = 0;
6983
6984 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006985 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006986 if (!task) {
6987 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6988 ctx->rule->arg.hlua_rule->fcn.name);
6989 return 0;
6990 }
6991 task->nice = 0;
6992 task->context = ctx;
6993 task->process = hlua_applet_wakeup;
6994 ctx->ctx.hlua_apptcp.task = task;
6995
6996 /* In the execution wrappers linked with a stream, the
6997 * Lua context can be not initialized. This behavior
6998 * permits to save performances because a systematic
6999 * Lua initialization cause 5% performances loss.
7000 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007001 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007002 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
7003 ctx->rule->arg.hlua_rule->fcn.name);
7004 return 0;
7005 }
7006
7007 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007008 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007009
7010 /* The following Lua calls can fail. */
7011 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007012 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7013 error = lua_tostring(hlua->T, -1);
7014 else
7015 error = "critical error";
7016 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7017 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007018 return 0;
7019 }
7020
7021 /* Check stack available size. */
7022 if (!lua_checkstack(hlua->T, 1)) {
7023 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7024 ctx->rule->arg.hlua_rule->fcn.name);
7025 RESET_SAFE_LJMP(hlua->T);
7026 return 0;
7027 }
7028
7029 /* Restore the function in the stack. */
7030 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7031
7032 /* Create and and push object stream in the stack. */
7033 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7034 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7035 ctx->rule->arg.hlua_rule->fcn.name);
7036 RESET_SAFE_LJMP(hlua->T);
7037 return 0;
7038 }
7039 hlua->nargs = 1;
7040
7041 /* push keywords in the stack. */
7042 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7043 if (!lua_checkstack(hlua->T, 1)) {
7044 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7045 ctx->rule->arg.hlua_rule->fcn.name);
7046 RESET_SAFE_LJMP(hlua->T);
7047 return 0;
7048 }
7049 lua_pushstring(hlua->T, *arg);
7050 hlua->nargs++;
7051 }
7052
7053 RESET_SAFE_LJMP(hlua->T);
7054
7055 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007056 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007057 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007058
7059 return 1;
7060}
7061
Willy Tarreau60409db2019-08-21 14:14:50 +02007062void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007063{
7064 struct stream_interface *si = ctx->owner;
7065 struct stream *strm = si_strm(si);
7066 struct channel *res = si_ic(si);
7067 struct act_rule *rule = ctx->rule;
7068 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007069 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007070
7071 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007072 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7073 /* eat the whole request */
7074 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007075 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007076 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007077
7078 /* If the stream is disconnect or closed, ldo nothing. */
7079 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7080 return;
7081
7082 /* Execute the function. */
7083 switch (hlua_ctx_resume(hlua, 1)) {
7084 /* finished. */
7085 case HLUA_E_OK:
7086 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7087
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007088 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007089 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007090 res->flags |= CF_READ_NULL;
7091 si_shutr(si);
7092 return;
7093
7094 /* yield. */
7095 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007096 if (hlua->wake_time != TICK_ETERNITY)
7097 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007098 return;
7099
7100 /* finished with error. */
7101 case HLUA_E_ERRMSG:
7102 /* Display log. */
7103 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7104 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7105 lua_pop(hlua->T, 1);
7106 goto error;
7107
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007108 case HLUA_E_ETMOUT:
7109 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7110 rule->arg.hlua_rule->fcn.name);
7111 goto error;
7112
7113 case HLUA_E_NOMEM:
7114 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7115 rule->arg.hlua_rule->fcn.name);
7116 goto error;
7117
7118 case HLUA_E_YIELD: /* unexpected */
7119 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7120 rule->arg.hlua_rule->fcn.name);
7121 goto error;
7122
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007123 case HLUA_E_ERR:
7124 /* Display log. */
7125 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7126 rule->arg.hlua_rule->fcn.name);
7127 goto error;
7128
7129 default:
7130 goto error;
7131 }
7132
7133error:
7134
7135 /* For all other cases, just close the stream. */
7136 si_shutw(si);
7137 si_shutr(si);
7138 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7139}
7140
7141static void hlua_applet_tcp_release(struct appctx *ctx)
7142{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007143 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007144 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007145 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007146 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007147}
7148
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007149/* The function returns 1 if the initialisation is complete, 0 if
7150 * an errors occurs and -1 if more data are required for initializing
7151 * the applet.
7152 */
7153static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7154{
7155 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007156 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007157 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007158 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007159 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007160 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007161
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007162 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007163 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007164 if (!hlua) {
7165 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7166 ctx->rule->arg.hlua_rule->fcn.name);
7167 return 0;
7168 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007169 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007170 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007171 ctx->ctx.hlua_apphttp.left_bytes = -1;
7172 ctx->ctx.hlua_apphttp.flags = 0;
7173
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007174 if (txn->req.flags & HTTP_MSGF_VER_11)
7175 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7176
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007177 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007178 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007179 if (!task) {
7180 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7181 ctx->rule->arg.hlua_rule->fcn.name);
7182 return 0;
7183 }
7184 task->nice = 0;
7185 task->context = ctx;
7186 task->process = hlua_applet_wakeup;
7187 ctx->ctx.hlua_apphttp.task = task;
7188
7189 /* In the execution wrappers linked with a stream, the
7190 * Lua context can be not initialized. This behavior
7191 * permits to save performances because a systematic
7192 * Lua initialization cause 5% performances loss.
7193 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007194 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007195 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7196 ctx->rule->arg.hlua_rule->fcn.name);
7197 return 0;
7198 }
7199
7200 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007201 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007202
7203 /* The following Lua calls can fail. */
7204 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007205 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7206 error = lua_tostring(hlua->T, -1);
7207 else
7208 error = "critical error";
7209 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7210 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007211 return 0;
7212 }
7213
7214 /* Check stack available size. */
7215 if (!lua_checkstack(hlua->T, 1)) {
7216 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7217 ctx->rule->arg.hlua_rule->fcn.name);
7218 RESET_SAFE_LJMP(hlua->T);
7219 return 0;
7220 }
7221
7222 /* Restore the function in the stack. */
7223 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7224
7225 /* Create and and push object stream in the stack. */
7226 if (!hlua_applet_http_new(hlua->T, ctx)) {
7227 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7228 ctx->rule->arg.hlua_rule->fcn.name);
7229 RESET_SAFE_LJMP(hlua->T);
7230 return 0;
7231 }
7232 hlua->nargs = 1;
7233
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007234 /* push keywords in the stack. */
7235 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7236 if (!lua_checkstack(hlua->T, 1)) {
7237 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7238 ctx->rule->arg.hlua_rule->fcn.name);
7239 RESET_SAFE_LJMP(hlua->T);
7240 return 0;
7241 }
7242 lua_pushstring(hlua->T, *arg);
7243 hlua->nargs++;
7244 }
7245
7246 RESET_SAFE_LJMP(hlua->T);
7247
7248 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007249 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007250
7251 return 1;
7252}
7253
Willy Tarreau60409db2019-08-21 14:14:50 +02007254void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007255{
7256 struct stream_interface *si = ctx->owner;
7257 struct stream *strm = si_strm(si);
7258 struct channel *req = si_oc(si);
7259 struct channel *res = si_ic(si);
7260 struct act_rule *rule = ctx->rule;
7261 struct proxy *px = strm->be;
7262 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7263 struct htx *req_htx, *res_htx;
7264
7265 res_htx = htx_from_buf(&res->buf);
7266
7267 /* If the stream is disconnect or closed, ldo nothing. */
7268 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7269 goto out;
7270
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007271 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007272 if (!b_size(&res->buf)) {
7273 si_rx_room_blk(si);
7274 goto out;
7275 }
7276 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007277 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007278 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7279
7280 /* Set the currently running flag. */
7281 if (!HLUA_IS_RUNNING(hlua) &&
7282 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7283 struct htx_blk *blk;
7284 size_t count = co_data(req);
7285
7286 if (!count) {
7287 si_cant_get(si);
7288 goto out;
7289 }
7290
7291 /* We need to flush the request header. This left the body for
7292 * the Lua.
7293 */
7294 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007295 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007296 while (count && blk) {
7297 enum htx_blk_type type = htx_get_blk_type(blk);
7298 uint32_t sz = htx_get_blksz(blk);
7299
7300 if (sz > count) {
7301 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007302 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007303 goto out;
7304 }
7305
7306 count -= sz;
7307 co_set_data(req, co_data(req) - sz);
7308 blk = htx_remove_blk(req_htx, blk);
7309
7310 if (type == HTX_BLK_EOH)
7311 break;
7312 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007313 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007314 }
7315
7316 /* Executes The applet if it is not done. */
7317 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7318
7319 /* Execute the function. */
7320 switch (hlua_ctx_resume(hlua, 1)) {
7321 /* finished. */
7322 case HLUA_E_OK:
7323 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7324 break;
7325
7326 /* yield. */
7327 case HLUA_E_AGAIN:
7328 if (hlua->wake_time != TICK_ETERNITY)
7329 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007330 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007331
7332 /* finished with error. */
7333 case HLUA_E_ERRMSG:
7334 /* Display log. */
7335 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7336 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7337 lua_pop(hlua->T, 1);
7338 goto error;
7339
7340 case HLUA_E_ETMOUT:
7341 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7342 rule->arg.hlua_rule->fcn.name);
7343 goto error;
7344
7345 case HLUA_E_NOMEM:
7346 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7347 rule->arg.hlua_rule->fcn.name);
7348 goto error;
7349
7350 case HLUA_E_YIELD: /* unexpected */
7351 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7352 rule->arg.hlua_rule->fcn.name);
7353 goto error;
7354
7355 case HLUA_E_ERR:
7356 /* Display log. */
7357 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7358 rule->arg.hlua_rule->fcn.name);
7359 goto error;
7360
7361 default:
7362 goto error;
7363 }
7364 }
7365
7366 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007367 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7368 goto done;
7369
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007370 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7371 goto error;
7372
Christopher Faulet54b5e212019-06-04 10:08:28 +02007373 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007374 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007375 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7376 si_rx_room_blk(si);
7377 goto out;
7378 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007379 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007380 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7381 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007382 }
7383
7384 done:
7385 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007386 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007387 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007388 si_shutr(si);
7389 }
7390
7391 /* eat the whole request */
7392 if (co_data(req)) {
7393 req_htx = htx_from_buf(&req->buf);
7394 co_htx_skip(req, req_htx, co_data(req));
7395 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007396 }
7397 }
7398
7399 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007400 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007401 return;
7402
7403 error:
7404
7405 /* If we are in HTTP mode, and we are not send any
7406 * data, return a 500 server error in best effort:
7407 * if there is no room available in the buffer,
7408 * just close the connection.
7409 */
7410 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007411 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007412
7413 channel_erase(res);
7414 res->buf.data = b_data(err);
7415 memcpy(res->buf.area, b_head(err), b_data(err));
7416 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007417 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007418 }
7419 if (!(strm->flags & SF_ERR_MASK))
7420 strm->flags |= SF_ERR_RESOURCE;
7421 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7422 goto done;
7423}
7424
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007425static void hlua_applet_http_release(struct appctx *ctx)
7426{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007427 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007428 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007429 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007430 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007431}
7432
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007433/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007434 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007435 *
7436 * This function can fail with an abort() due to an Lua critical error.
7437 * We are in the configuration parsing process of HAProxy, this abort() is
7438 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007439 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007440static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7441 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007442{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007443 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007444 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007445
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007446 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007447 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007448 if (!rule->arg.hlua_rule) {
7449 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007450 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007451 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007452
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007453 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007454 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7455 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007456 if (!rule->arg.hlua_rule->args) {
7457 memprintf(err, "out of memory error");
7458 return ACT_RET_PRS_ERR;
7459 }
7460
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007461 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007462 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007463
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007464 /* Expect some arguments */
7465 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007466 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007467 memprintf(err, "expect %d arguments", fcn->nargs);
7468 return ACT_RET_PRS_ERR;
7469 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007470 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007471 if (!rule->arg.hlua_rule->args[i]) {
7472 memprintf(err, "out of memory error");
7473 return ACT_RET_PRS_ERR;
7474 }
7475 (*cur_arg)++;
7476 }
7477 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007478
Thierry FOURNIER42148732015-09-02 17:17:33 +02007479 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007480 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007481 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007482}
7483
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007484static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7485 struct act_rule *rule, char **err)
7486{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007487 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007488
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007489 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007490 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007491 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007492 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007493 * the call of this analyzer.
7494 */
7495 if (rule->from != ACT_F_HTTP_REQ) {
7496 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7497 return ACT_RET_PRS_ERR;
7498 }
7499
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007500 /* Memory for the rule. */
7501 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7502 if (!rule->arg.hlua_rule) {
7503 memprintf(err, "out of memory error");
7504 return ACT_RET_PRS_ERR;
7505 }
7506
7507 /* Reference the Lua function and store the reference. */
7508 rule->arg.hlua_rule->fcn = *fcn;
7509
7510 /* TODO: later accept arguments. */
7511 rule->arg.hlua_rule->args = NULL;
7512
7513 /* Add applet pointer in the rule. */
7514 rule->applet.obj_type = OBJ_TYPE_APPLET;
7515 rule->applet.name = fcn->name;
7516 rule->applet.init = hlua_applet_http_init;
7517 rule->applet.fct = hlua_applet_http_fct;
7518 rule->applet.release = hlua_applet_http_release;
7519 rule->applet.timeout = hlua_timeout_applet;
7520
7521 return ACT_RET_PRS_OK;
7522}
7523
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007524/* This function is an LUA binding used for registering
7525 * "sample-conv" functions. It expects a converter name used
7526 * in the haproxy configuration file, and an LUA function.
7527 */
7528__LJMP static int hlua_register_action(lua_State *L)
7529{
7530 struct action_kw_list *akl;
7531 const char *name;
7532 int ref;
7533 int len;
7534 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007535 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007536 struct buffer *trash;
7537 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007538
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007539 /* Initialise the number of expected arguments at 0. */
7540 nargs = 0;
7541
7542 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7543 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007544
7545 /* First argument : converter name. */
7546 name = MAY_LJMP(luaL_checkstring(L, 1));
7547
7548 /* Second argument : environment. */
7549 if (lua_type(L, 2) != LUA_TTABLE)
7550 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7551
7552 /* Third argument : lua function. */
7553 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7554
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007555 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007556 if (lua_gettop(L) >= 4)
7557 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7558
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007559 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007560 lua_pushnil(L);
7561 while (lua_next(L, 2) != 0) {
7562 if (lua_type(L, -1) != LUA_TSTRING)
7563 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7564
Thierry Fournierf67442e2020-11-28 20:41:07 +01007565 /* Check if action exists */
7566 trash = get_trash_chunk();
7567 chunk_printf(trash, "lua.%s", name);
7568 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7569 akw = tcp_req_cont_action(trash->area);
7570 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7571 akw = tcp_res_cont_action(trash->area);
7572 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7573 akw = action_http_req_custom(trash->area);
7574 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7575 akw = action_http_res_custom(trash->area);
7576 } else {
7577 akw = NULL;
7578 }
7579 if (akw != NULL) {
7580 ha_warning("Trying to register action 'lua.%s' more than once. "
7581 "This will become a hard error in version 2.5.\n", name);
7582 }
7583
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007584 /* Check required environment. Only accepted "http" or "tcp". */
7585 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007586 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007587 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007588 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007589 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007590 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007591 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007592
7593 /* Fill fcn. */
7594 fcn->name = strdup(name);
7595 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007596 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007597 fcn->function_ref = ref;
7598
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007599 /* Set the expected number od arguments. */
7600 fcn->nargs = nargs;
7601
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007602 /* List head */
7603 akl->list.n = akl->list.p = NULL;
7604
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007605 /* action keyword. */
7606 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007607 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007608 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007609 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007610
7611 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7612
7613 akl->kw[0].match_pfx = 0;
7614 akl->kw[0].private = fcn;
7615 akl->kw[0].parse = action_register_lua;
7616
7617 /* select the action registering point. */
7618 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7619 tcp_req_cont_keywords_register(akl);
7620 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7621 tcp_res_cont_keywords_register(akl);
7622 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7623 http_req_keywords_register(akl);
7624 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7625 http_res_keywords_register(akl);
7626 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007627 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007628 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7629 "are expected.", lua_tostring(L, -1)));
7630
7631 /* pop the environment string. */
7632 lua_pop(L, 1);
7633 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007634 return ACT_RET_PRS_OK;
7635}
7636
7637static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7638 struct act_rule *rule, char **err)
7639{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007640 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007641
Christopher Faulet280f85b2019-07-15 15:02:04 +02007642 if (px->mode == PR_MODE_HTTP) {
7643 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007644 return ACT_RET_PRS_ERR;
7645 }
7646
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007647 /* Memory for the rule. */
7648 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7649 if (!rule->arg.hlua_rule) {
7650 memprintf(err, "out of memory error");
7651 return ACT_RET_PRS_ERR;
7652 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007653
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007654 /* Reference the Lua function and store the reference. */
7655 rule->arg.hlua_rule->fcn = *fcn;
7656
7657 /* TODO: later accept arguments. */
7658 rule->arg.hlua_rule->args = NULL;
7659
7660 /* Add applet pointer in the rule. */
7661 rule->applet.obj_type = OBJ_TYPE_APPLET;
7662 rule->applet.name = fcn->name;
7663 rule->applet.init = hlua_applet_tcp_init;
7664 rule->applet.fct = hlua_applet_tcp_fct;
7665 rule->applet.release = hlua_applet_tcp_release;
7666 rule->applet.timeout = hlua_timeout_applet;
7667
7668 return 0;
7669}
7670
7671/* This function is an LUA binding used for registering
7672 * "sample-conv" functions. It expects a converter name used
7673 * in the haproxy configuration file, and an LUA function.
7674 */
7675__LJMP static int hlua_register_service(lua_State *L)
7676{
7677 struct action_kw_list *akl;
7678 const char *name;
7679 const char *env;
7680 int ref;
7681 int len;
7682 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007683 struct buffer *trash;
7684 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007685
7686 MAY_LJMP(check_args(L, 3, "register_service"));
7687
7688 /* First argument : converter name. */
7689 name = MAY_LJMP(luaL_checkstring(L, 1));
7690
7691 /* Second argument : environment. */
7692 env = MAY_LJMP(luaL_checkstring(L, 2));
7693
7694 /* Third argument : lua function. */
7695 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7696
Thierry Fournierf67442e2020-11-28 20:41:07 +01007697 /* Check for service already registered */
7698 trash = get_trash_chunk();
7699 chunk_printf(trash, "lua.%s", name);
7700 akw = service_find(trash->area);
7701 if (akw != NULL) {
7702 ha_warning("Trying to register service 'lua.%s' more than once. "
7703 "This will become a hard error in version 2.5.\n", name);
7704 }
7705
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007706 /* Allocate and fill the sample fetch keyword struct. */
7707 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7708 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007709 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007710 fcn = calloc(1, sizeof(*fcn));
7711 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007712 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007713
7714 /* Fill fcn. */
7715 len = strlen("<lua.>") + strlen(name) + 1;
7716 fcn->name = calloc(1, len);
7717 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007718 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007719 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7720 fcn->function_ref = ref;
7721
7722 /* List head */
7723 akl->list.n = akl->list.p = NULL;
7724
7725 /* converter keyword. */
7726 len = strlen("lua.") + strlen(name) + 1;
7727 akl->kw[0].kw = calloc(1, len);
7728 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007729 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007730
7731 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7732
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007733 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007734 if (strcmp(env, "tcp") == 0)
7735 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007736 else if (strcmp(env, "http") == 0)
7737 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007738 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007739 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007740 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007741
7742 akl->kw[0].match_pfx = 0;
7743 akl->kw[0].private = fcn;
7744
7745 /* End of array. */
7746 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7747
7748 /* Register this new converter */
7749 service_keywords_register(akl);
7750
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007751 return 0;
7752}
7753
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007754/* This function initialises Lua cli handler. It copies the
7755 * arguments in the Lua stack and create channel IO objects.
7756 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007757static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007758{
7759 struct hlua *hlua;
7760 struct hlua_function *fcn;
7761 int i;
7762 const char *error;
7763
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007764 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007765 appctx->ctx.hlua_cli.fcn = private;
7766
Willy Tarreaubafbe012017-11-24 17:34:44 +01007767 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007768 if (!hlua) {
7769 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007770 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007771 }
7772 HLUA_INIT(hlua);
7773 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007774
7775 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007776 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007777 * applet_http. It is absolutely compatible.
7778 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007779 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007780 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007781 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007782 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007783 }
7784 appctx->ctx.hlua_cli.task->nice = 0;
7785 appctx->ctx.hlua_cli.task->context = appctx;
7786 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7787
7788 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007789 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007790 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007791 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007792 }
7793
7794 /* The following Lua calls can fail. */
7795 if (!SET_SAFE_LJMP(hlua->T)) {
7796 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7797 error = lua_tostring(hlua->T, -1);
7798 else
7799 error = "critical error";
7800 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7801 goto error;
7802 }
7803
7804 /* Check stack available size. */
7805 if (!lua_checkstack(hlua->T, 2)) {
7806 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7807 goto error;
7808 }
7809
7810 /* Restore the function in the stack. */
7811 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7812
7813 /* Once the arguments parsed, the CLI is like an AppletTCP,
7814 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007815 */
7816 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7817 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7818 goto error;
7819 }
7820 hlua->nargs = 1;
7821
7822 /* push keywords in the stack. */
7823 for (i = 0; *args[i]; i++) {
7824 /* Check stack available size. */
7825 if (!lua_checkstack(hlua->T, 1)) {
7826 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7827 goto error;
7828 }
7829 lua_pushstring(hlua->T, args[i]);
7830 hlua->nargs++;
7831 }
7832
7833 /* We must initialize the execution timeouts. */
7834 hlua->max_time = hlua_timeout_session;
7835
7836 /* At this point the execution is safe. */
7837 RESET_SAFE_LJMP(hlua->T);
7838
7839 /* It's ok */
7840 return 0;
7841
7842 /* It's not ok. */
7843error:
7844 RESET_SAFE_LJMP(hlua->T);
7845 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007846 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007847 return 1;
7848}
7849
7850static int hlua_cli_io_handler_fct(struct appctx *appctx)
7851{
7852 struct hlua *hlua;
7853 struct stream_interface *si;
7854 struct hlua_function *fcn;
7855
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007856 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007857 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007858 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007859
7860 /* If the stream is disconnect or closed, ldo nothing. */
7861 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7862 return 1;
7863
7864 /* Execute the function. */
7865 switch (hlua_ctx_resume(hlua, 1)) {
7866
7867 /* finished. */
7868 case HLUA_E_OK:
7869 return 1;
7870
7871 /* yield. */
7872 case HLUA_E_AGAIN:
7873 /* We want write. */
7874 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007875 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007876 /* Set the timeout. */
7877 if (hlua->wake_time != TICK_ETERNITY)
7878 task_schedule(hlua->task, hlua->wake_time);
7879 return 0;
7880
7881 /* finished with error. */
7882 case HLUA_E_ERRMSG:
7883 /* Display log. */
7884 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7885 fcn->name, lua_tostring(hlua->T, -1));
7886 lua_pop(hlua->T, 1);
7887 return 1;
7888
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007889 case HLUA_E_ETMOUT:
7890 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7891 fcn->name);
7892 return 1;
7893
7894 case HLUA_E_NOMEM:
7895 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7896 fcn->name);
7897 return 1;
7898
7899 case HLUA_E_YIELD: /* unexpected */
7900 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7901 fcn->name);
7902 return 1;
7903
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007904 case HLUA_E_ERR:
7905 /* Display log. */
7906 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7907 fcn->name);
7908 return 1;
7909
7910 default:
7911 return 1;
7912 }
7913
7914 return 1;
7915}
7916
7917static void hlua_cli_io_release_fct(struct appctx *appctx)
7918{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007919 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007920 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007921}
7922
7923/* This function is an LUA binding used for registering
7924 * new keywords in the cli. It expects a list of keywords
7925 * which are the "path". It is limited to 5 keywords. A
7926 * description of the command, a function to be executed
7927 * for the parsing and a function for io handlers.
7928 */
7929__LJMP static int hlua_register_cli(lua_State *L)
7930{
7931 struct cli_kw_list *cli_kws;
7932 const char *message;
7933 int ref_io;
7934 int len;
7935 struct hlua_function *fcn;
7936 int index;
7937 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007938 struct buffer *trash;
7939 const char *kw[5];
7940 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007941
7942 MAY_LJMP(check_args(L, 3, "register_cli"));
7943
7944 /* First argument : an array of maximum 5 keywords. */
7945 if (!lua_istable(L, 1))
7946 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7947
7948 /* Second argument : string with contextual message. */
7949 message = MAY_LJMP(luaL_checkstring(L, 2));
7950
7951 /* Third and fourth argument : lua function. */
7952 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7953
Thierry Fournierf67442e2020-11-28 20:41:07 +01007954 /* Check for CLI service already registered */
7955 trash = get_trash_chunk();
7956 index = 0;
7957 lua_pushnil(L);
7958 memset(kw, 0, sizeof(kw));
7959 while (lua_next(L, 1) != 0) {
7960 if (index >= CLI_PREFIX_KW_NB)
7961 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7962 if (lua_type(L, -1) != LUA_TSTRING)
7963 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7964 kw[index] = lua_tostring(L, -1);
7965 if (index == 0)
7966 chunk_printf(trash, "%s", kw[index]);
7967 else
7968 chunk_appendf(trash, " %s", kw[index]);
7969 index++;
7970 lua_pop(L, 1);
7971 }
7972 cli_kw = cli_find_kw_exact((char **)kw);
7973 if (cli_kw != NULL) {
7974 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
7975 "This will become a hard error in version 2.5.\n", trash->area);
7976 }
7977
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007978 /* Allocate and fill the sample fetch keyword struct. */
7979 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7980 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007981 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007982 fcn = calloc(1, sizeof(*fcn));
7983 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007984 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007985
7986 /* Fill path. */
7987 index = 0;
7988 lua_pushnil(L);
7989 while(lua_next(L, 1) != 0) {
7990 if (index >= 5)
7991 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7992 if (lua_type(L, -1) != LUA_TSTRING)
7993 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7994 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7995 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007996 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007997 index++;
7998 lua_pop(L, 1);
7999 }
8000
8001 /* Copy help message. */
8002 cli_kws->kw[0].usage = strdup(message);
8003 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008004 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008005
8006 /* Fill fcn io handler. */
8007 len = strlen("<lua.cli>") + 1;
8008 for (i = 0; i < index; i++)
8009 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8010 fcn->name = calloc(1, len);
8011 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008012 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008013 strncat((char *)fcn->name, "<lua.cli", len);
8014 for (i = 0; i < index; i++) {
8015 strncat((char *)fcn->name, ".", len);
8016 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8017 }
8018 strncat((char *)fcn->name, ">", len);
8019 fcn->function_ref = ref_io;
8020
8021 /* Fill last entries. */
8022 cli_kws->kw[0].private = fcn;
8023 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8024 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8025 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8026
8027 /* Register this new converter */
8028 cli_register_kw(cli_kws);
8029
8030 return 0;
8031}
8032
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008033static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8034 struct proxy *defpx, const char *file, int line,
8035 char **err, unsigned int *timeout)
8036{
8037 const char *error;
8038
8039 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008040 if (error == PARSE_TIME_OVER) {
8041 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8042 args[1], args[0]);
8043 return -1;
8044 }
8045 else if (error == PARSE_TIME_UNDER) {
8046 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8047 args[1], args[0]);
8048 return -1;
8049 }
8050 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008051 memprintf(err, "%s: invalid timeout", args[0]);
8052 return -1;
8053 }
8054 return 0;
8055}
8056
8057static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8058 struct proxy *defpx, const char *file, int line,
8059 char **err)
8060{
8061 return hlua_read_timeout(args, section_type, curpx, defpx,
8062 file, line, err, &hlua_timeout_session);
8063}
8064
8065static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8066 struct proxy *defpx, const char *file, int line,
8067 char **err)
8068{
8069 return hlua_read_timeout(args, section_type, curpx, defpx,
8070 file, line, err, &hlua_timeout_task);
8071}
8072
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008073static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8074 struct proxy *defpx, const char *file, int line,
8075 char **err)
8076{
8077 return hlua_read_timeout(args, section_type, curpx, defpx,
8078 file, line, err, &hlua_timeout_applet);
8079}
8080
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008081static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8082 struct proxy *defpx, const char *file, int line,
8083 char **err)
8084{
8085 char *error;
8086
8087 hlua_nb_instruction = strtoll(args[1], &error, 10);
8088 if (*error != '\0') {
8089 memprintf(err, "%s: invalid number", args[0]);
8090 return -1;
8091 }
8092 return 0;
8093}
8094
Willy Tarreau32f61e22015-03-18 17:54:59 +01008095static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8096 struct proxy *defpx, const char *file, int line,
8097 char **err)
8098{
8099 char *error;
8100
8101 if (*(args[1]) == 0) {
8102 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8103 return -1;
8104 }
8105 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8106 if (*error != '\0') {
8107 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8108 return -1;
8109 }
8110 return 0;
8111}
8112
8113
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008114/* This function is called by the main configuration key "lua-load". It loads and
8115 * execute an lua file during the parsing of the HAProxy configuration file. It is
8116 * the main lua entry point.
8117 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008118 * This function runs with the HAProxy keywords API. It returns -1 if an error
8119 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008120 *
8121 * In some error case, LUA set an error message in top of the stack. This function
8122 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008123 *
8124 * This function can fail with an abort() due to an Lua critical error.
8125 * We are in the configuration parsing process of HAProxy, this abort() is
8126 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008127 */
8128static int hlua_load(char **args, int section_type, struct proxy *curpx,
8129 struct proxy *defpx, const char *file, int line,
8130 char **err)
8131{
8132 int error;
8133
Thierry Fournier77a88942020-11-29 01:06:24 +01008134 if (*(args[1]) == 0) {
8135 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8136 return -1;
8137 }
8138
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008139 /* Just load and compile the file. */
8140 error = luaL_loadfile(gL.T, args[1]);
8141 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008142 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008143 lua_pop(gL.T, 1);
8144 return -1;
8145 }
8146
8147 /* If no syntax error where detected, execute the code. */
8148 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8149 switch (error) {
8150 case LUA_OK:
8151 break;
8152 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008153 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008154 lua_pop(gL.T, 1);
8155 return -1;
8156 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008157 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008158 return -1;
8159 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008160 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008161 lua_pop(gL.T, 1);
8162 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008163#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008164 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008165 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008166 lua_pop(gL.T, 1);
8167 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008168#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008169 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05008170 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008171 lua_pop(gL.T, 1);
8172 return -1;
8173 }
8174
8175 return 0;
8176}
8177
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008178/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8179 * in the given <ctx>.
8180 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008181static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008182{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008183 lua_getglobal(L, "package"); /* push package variable */
8184 lua_pushstring(L, path); /* push given path */
8185 lua_pushstring(L, ";"); /* push semicolon */
8186 lua_getfield(L, -3, type); /* push old path */
8187 lua_concat(L, 3); /* concatenate to new path */
8188 lua_setfield(L, -2, type); /* store new path */
8189 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008190
8191 return 0;
8192}
8193
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008194static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
8195 struct proxy *defpx, const char *file, int line,
8196 char **err)
8197{
8198 char *path;
8199 char *type = "path";
8200 if (too_many_args(2, args, err, NULL)) {
8201 return -1;
8202 }
8203
8204 if (!(*args[1])) {
8205 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
8206 return -1;
8207 }
8208 path = args[1];
8209
8210 if (*args[2]) {
8211 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8212 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
8213 return -1;
8214 }
8215 type = args[2];
8216 }
8217
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008218 return hlua_prepend_path(gL.T, type, path);
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008219}
8220
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008221/* configuration keywords declaration */
8222static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008223 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008224 { CFG_GLOBAL, "lua-load", hlua_load },
8225 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8226 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008227 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008228 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008229 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008230 { 0, NULL, NULL },
8231}};
8232
Willy Tarreau0108d902018-11-25 19:14:37 +01008233INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8234
Christopher Fauletafd8f102018-11-08 11:34:21 +01008235
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008236/* This function can fail with an abort() due to an Lua critical error.
8237 * We are in the initialisation process of HAProxy, this abort() is
8238 * tolerated.
8239 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008240int hlua_post_init()
8241{
8242 struct hlua_init_function *init;
8243 const char *msg;
8244 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008245 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008246 const char *kind;
8247 const char *trace;
8248 int return_status = 1;
8249#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8250 int nres;
8251#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008252
Willy Tarreaucdb53462020-12-02 12:12:00 +01008253 /* disable memory limit checks if limit is not set */
8254 if (!hlua_global_allocator.limit)
8255 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8256
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008257 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008258 if (!SET_SAFE_LJMP(gL.T)) {
8259 if (lua_type(gL.T, -1) == LUA_TSTRING)
8260 error = lua_tostring(gL.T, -1);
8261 else
8262 error = "critical error";
8263 fprintf(stderr, "Lua post-init: %s.\n", error);
8264 exit(1);
8265 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008266
8267#if USE_OPENSSL
8268 /* Initialize SSL server. */
8269 if (socket_ssl.xprt->prepare_srv) {
8270 int saved_used_backed = global.ssl_used_backend;
8271 // don't affect maxconn automatic computation
8272 socket_ssl.xprt->prepare_srv(&socket_ssl);
8273 global.ssl_used_backend = saved_used_backed;
8274 }
8275#endif
8276
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008277 hlua_fcn_post_init(gL.T);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008278
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008279 list_for_each_entry(init, &hlua_init_functions, l) {
8280 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008281
8282#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8283 ret = lua_resume(gL.T, gL.T, 0, &nres);
8284#else
8285 ret = lua_resume(gL.T, gL.T, 0);
8286#endif
8287 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008288 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008289
8290 case LUA_OK:
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008291 lua_pop(gL.T, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008292 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008293
8294 case LUA_ERRERR:
8295 kind = "message handler error";
8296 /* Fall thru */
8297 case LUA_ERRRUN:
8298 if (!kind)
8299 kind = "runtime error";
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008300 msg = lua_tostring(gL.T, -1);
Thierry Fournier670db242020-11-28 10:49:59 +01008301 lua_settop(gL.T, 0); /* Empty the stack. */
8302 lua_pop(gL.T, 1);
8303 trace = hlua_traceback(gL.T);
8304 if (msg)
8305 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8306 else
8307 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8308 return_status = 0;
8309 break;
8310
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008311 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008312 /* Unknown error */
8313 kind = "Unknown error";
8314 /* Fall thru */
8315 case LUA_YIELD:
8316 /* yield is not configured at this step, this state doesn't happen */
8317 if (!kind)
8318 kind = "yield not allowed";
8319 /* Fall thru */
8320 case LUA_ERRMEM:
8321 if (!kind)
8322 kind = "out of memory error";
8323 lua_settop(gL.T, 0);
8324 lua_pop(gL.T, 1);
8325 trace = hlua_traceback(gL.T);
8326 ha_alert("Lua init: %s: %s\n", kind, trace);
8327 return_status = 0;
8328 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008329 }
Thierry Fournier670db242020-11-28 10:49:59 +01008330 if (!return_status)
8331 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008332 }
Thierry Fournier670db242020-11-28 10:49:59 +01008333 RESET_SAFE_LJMP(gL.T);
8334 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008335}
8336
Willy Tarreau32f61e22015-03-18 17:54:59 +01008337/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8338 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8339 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008340 * allocation. <nsize> is the requested new size. A new allocation is
8341 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008342 * zero. This one verifies that the limits are respected but is optimized
8343 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008344 */
8345static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8346{
8347 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008348 size_t limit, old, new;
8349
8350 /* a limit of ~0 means unlimited and boot complete, so there's no need
8351 * for accounting anymore.
8352 */
8353 if (likely(~zone->limit == 0))
8354 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008355
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008356 if (!ptr)
8357 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008358
Willy Tarreaucdb53462020-12-02 12:12:00 +01008359 /* enforce strict limits across all threads */
8360 limit = zone->limit;
8361 old = _HA_ATOMIC_LOAD(&zone->allocated);
8362 do {
8363 new = old + nsize - osize;
8364 if (unlikely(nsize && limit && new > limit))
8365 return NULL;
8366 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008367
8368 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008369
8370 if (unlikely(!ptr && nsize)) // failed
8371 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8372
8373 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008374 return ptr;
8375}
8376
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008377/* Ithis function can fail with an abort() due to an Lua critical error.
8378 * We are in the initialisation process of HAProxy, this abort() is
8379 * tolerated.
8380 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008381void hlua_init(void)
8382{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008383 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008384 int idx;
8385 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008386 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008387 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008388 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008389 void **context;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008390#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008391 struct srv_kw *kw;
8392 int tmp_error;
8393 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008394 char *args[] = { /* SSL client configuration. */
8395 "ssl",
8396 "verify",
8397 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008398 NULL
8399 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008400#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008401
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008402 /* Init main lua stack. */
8403 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008404 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008405 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008406 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008407 gL.Tref = LUA_REFNIL;
8408 gL.task = NULL;
8409
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008410 /* Initialise Lua context to NULL */
8411 context = lua_getextraspace(gL.T);
8412 *context = NULL;
8413
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008414 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008415 * the Lua function can fail with an abort. We are in the initialisation
8416 * process of HAProxy, this abort() is tolerated.
8417 */
8418
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008419 /* Set safe environment for the initialisation. */
8420 if (!SET_SAFE_LJMP(gL.T)) {
8421 if (lua_type(gL.T, -1) == LUA_TSTRING)
8422 error_msg = lua_tostring(gL.T, -1);
8423 else
8424 error_msg = "critical error";
8425 fprintf(stderr, "Lua init: %s.\n", error_msg);
8426 exit(1);
8427 }
8428
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008429 /* Initialise lua. */
8430 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008431#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8432#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8433#ifdef HLUA_PREPEND_PATH
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008434 hlua_prepend_path(gL.T, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008435#endif
8436#ifdef HLUA_PREPEND_CPATH
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008437 hlua_prepend_path(gL.T, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008438#endif
8439#undef HLUA_PREPEND_PATH_TOSTRING
8440#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008441
8442 /*
8443 *
8444 * Create "core" object.
8445 *
8446 */
8447
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008448 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008449 lua_newtable(gL.T);
8450
8451 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008452 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008453 hlua_class_const_int(gL.T, log_levels[i], i);
8454
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008455 /* Register special functions. */
8456 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008457 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008458 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008459 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008460 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008461 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008462 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008463 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008464 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008465 hlua_class_function(gL.T, "sleep", hlua_sleep);
8466 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008467 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8468 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8469 hlua_class_function(gL.T, "set_map", hlua_set_map);
8470 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008471 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008472 hlua_class_function(gL.T, "log", hlua_log);
8473 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8474 hlua_class_function(gL.T, "Info", hlua_log_info);
8475 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8476 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008477 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008478 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008479
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008480 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008481
8482 /*
8483 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008484 * Create "act" object.
8485 *
8486 */
8487
8488 /* This table entry is the object "act" base. */
8489 lua_newtable(gL.T);
8490
8491 /* push action return constants */
8492 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8493 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8494 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8495 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8496 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8497 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8498 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8499 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8500
Christopher Faulet501465d2020-02-26 14:54:16 +01008501 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008502
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008503 lua_setglobal(gL.T, "act");
8504
8505 /*
8506 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008507 * Register class Map
8508 *
8509 */
8510
8511 /* This table entry is the object "Map" base. */
8512 lua_newtable(gL.T);
8513
8514 /* register pattern types. */
8515 for (i=0; i<PAT_MATCH_NUM; i++)
8516 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008517 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008518 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8519 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008520 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008521
8522 /* register constructor. */
8523 hlua_class_function(gL.T, "new", hlua_map_new);
8524
8525 /* Create and fill the metatable. */
8526 lua_newtable(gL.T);
8527
Ilya Shipitsind4259502020-04-08 01:07:56 +05008528 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008529 lua_pushstring(gL.T, "__index");
8530 lua_newtable(gL.T);
8531
8532 /* Register . */
8533 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8534 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8535
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008536 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008537
Thierry Fournier45e78d72016-02-19 18:34:46 +01008538 /* Register previous table in the registry with reference and named entry.
8539 * The function hlua_register_metatable() pops the stack, so we
8540 * previously create a copy of the table.
8541 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008542 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008543 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008544
8545 /* Assign the metatable to the mai Map object. */
8546 lua_setmetatable(gL.T, -2);
8547
8548 /* Set a name to the table. */
8549 lua_setglobal(gL.T, "Map");
8550
8551 /*
8552 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008553 * Register class Channel
8554 *
8555 */
8556
8557 /* Create and fill the metatable. */
8558 lua_newtable(gL.T);
8559
Ilya Shipitsind4259502020-04-08 01:07:56 +05008560 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008561 lua_pushstring(gL.T, "__index");
8562 lua_newtable(gL.T);
8563
8564 /* Register . */
8565 hlua_class_function(gL.T, "get", hlua_channel_get);
8566 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8567 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8568 hlua_class_function(gL.T, "set", hlua_channel_set);
8569 hlua_class_function(gL.T, "append", hlua_channel_append);
8570 hlua_class_function(gL.T, "send", hlua_channel_send);
8571 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8572 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8573 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008574 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008575 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008576
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008577 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008578
8579 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008580 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008581
8582 /*
8583 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008584 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008585 *
8586 */
8587
8588 /* Create and fill the metatable. */
8589 lua_newtable(gL.T);
8590
Ilya Shipitsind4259502020-04-08 01:07:56 +05008591 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008592 lua_pushstring(gL.T, "__index");
8593 lua_newtable(gL.T);
8594
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008595 /* Browse existing fetches and create the associated
8596 * object method.
8597 */
8598 sf = NULL;
8599 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008600 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8601 * by an underscore.
8602 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008603 strncpy(trash.area, sf->kw, trash.size);
8604 trash.area[trash.size - 1] = '\0';
8605 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008606 if (*p == '.' || *p == '-' || *p == '+')
8607 *p = '_';
8608
8609 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008610 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008611 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008612 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008613 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008614 }
8615
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008616 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008617
8618 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008619 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008620
8621 /*
8622 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008623 * Register class Converters
8624 *
8625 */
8626
8627 /* Create and fill the metatable. */
8628 lua_newtable(gL.T);
8629
8630 /* Create and fill the __index entry. */
8631 lua_pushstring(gL.T, "__index");
8632 lua_newtable(gL.T);
8633
8634 /* Browse existing converters and create the associated
8635 * object method.
8636 */
8637 sc = NULL;
8638 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008639 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8640 * by an underscore.
8641 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008642 strncpy(trash.area, sc->kw, trash.size);
8643 trash.area[trash.size - 1] = '\0';
8644 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008645 if (*p == '.' || *p == '-' || *p == '+')
8646 *p = '_';
8647
8648 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008649 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008650 lua_pushlightuserdata(gL.T, sc);
8651 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008652 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008653 }
8654
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008655 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008656
8657 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008658 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008659
8660 /*
8661 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008662 * Register class HTTP
8663 *
8664 */
8665
8666 /* Create and fill the metatable. */
8667 lua_newtable(gL.T);
8668
Ilya Shipitsind4259502020-04-08 01:07:56 +05008669 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008670 lua_pushstring(gL.T, "__index");
8671 lua_newtable(gL.T);
8672
8673 /* Register Lua functions. */
8674 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8675 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8676 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8677 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8678 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8679 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8680 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8681 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8682 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8683 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8684
8685 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8686 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8687 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8688 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8689 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8690 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008691 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008692
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008693 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008694
8695 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008696 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008697
8698 /*
8699 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008700 * Register class AppletTCP
8701 *
8702 */
8703
8704 /* Create and fill the metatable. */
8705 lua_newtable(gL.T);
8706
Ilya Shipitsind4259502020-04-08 01:07:56 +05008707 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008708 lua_pushstring(gL.T, "__index");
8709 lua_newtable(gL.T);
8710
8711 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008712 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8713 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8714 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8715 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8716 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008717 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8718 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8719 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008720
8721 lua_settable(gL.T, -3);
8722
8723 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008724 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008725
8726 /*
8727 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008728 * Register class AppletHTTP
8729 *
8730 */
8731
8732 /* Create and fill the metatable. */
8733 lua_newtable(gL.T);
8734
Ilya Shipitsind4259502020-04-08 01:07:56 +05008735 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008736 lua_pushstring(gL.T, "__index");
8737 lua_newtable(gL.T);
8738
8739 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008740 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8741 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008742 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8743 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8744 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008745 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8746 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8747 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8748 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8749 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8750 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8751
8752 lua_settable(gL.T, -3);
8753
8754 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008755 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008756
8757 /*
8758 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008759 * Register class TXN
8760 *
8761 */
8762
8763 /* Create and fill the metatable. */
8764 lua_newtable(gL.T);
8765
Ilya Shipitsind4259502020-04-08 01:07:56 +05008766 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008767 lua_pushstring(gL.T, "__index");
8768 lua_newtable(gL.T);
8769
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008770 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008771 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8772 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8773 hlua_class_function(gL.T, "set_var", hlua_set_var);
8774 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8775 hlua_class_function(gL.T, "get_var", hlua_get_var);
8776 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008777 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008778 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8779 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8780 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8781 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8782 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8783 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8784 hlua_class_function(gL.T, "log", hlua_txn_log);
8785 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8786 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8787 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8788 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008789
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008790 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008791
8792 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008793 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008794
8795 /*
8796 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008797 * Register class reply
8798 *
8799 */
8800 lua_newtable(gL.T);
8801 lua_pushstring(gL.T, "__index");
8802 lua_newtable(gL.T);
8803 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8804 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8805 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8806 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8807 lua_settable(gL.T, -3); /* Sets the __index entry. */
8808 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8809
8810
8811 /*
8812 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008813 * Register class Socket
8814 *
8815 */
8816
8817 /* Create and fill the metatable. */
8818 lua_newtable(gL.T);
8819
Ilya Shipitsind4259502020-04-08 01:07:56 +05008820 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008821 lua_pushstring(gL.T, "__index");
8822 lua_newtable(gL.T);
8823
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008824#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008825 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008826#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008827 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8828 hlua_class_function(gL.T, "send", hlua_socket_send);
8829 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8830 hlua_class_function(gL.T, "close", hlua_socket_close);
8831 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8832 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8833 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8834 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8835
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008836 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008837
8838 /* Register the garbage collector entry. */
8839 lua_pushstring(gL.T, "__gc");
8840 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008841 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008842
8843 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008844 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008845
8846 /* Proxy and server configuration initialisation. */
8847 memset(&socket_proxy, 0, sizeof(socket_proxy));
8848 init_new_proxy(&socket_proxy);
8849 socket_proxy.parent = NULL;
8850 socket_proxy.last_change = now.tv_sec;
8851 socket_proxy.id = "LUA-SOCKET";
8852 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8853 socket_proxy.maxconn = 0;
8854 socket_proxy.accept = NULL;
8855 socket_proxy.options2 |= PR_O2_INDEPSTR;
8856 socket_proxy.srv = NULL;
8857 socket_proxy.conn_retries = 0;
8858 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8859
8860 /* Init TCP server: unchanged parameters */
8861 memset(&socket_tcp, 0, sizeof(socket_tcp));
8862 socket_tcp.next = NULL;
8863 socket_tcp.proxy = &socket_proxy;
8864 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8865 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008866 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008867 socket_tcp.idle_conns = NULL;
8868 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008869 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008870 socket_tcp.last_change = 0;
8871 socket_tcp.id = "LUA-TCP-CONN";
8872 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8873 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8874 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8875
8876 /* XXX: Copy default parameter from default server,
8877 * but the default server is not initialized.
8878 */
8879 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8880 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8881 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8882 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8883 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8884 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8885 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8886 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8887 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8888 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8889
8890 socket_tcp.check.status = HCHK_STATUS_INI;
8891 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8892 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8893 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8894 socket_tcp.check.server = &socket_tcp;
8895
8896 socket_tcp.agent.status = HCHK_STATUS_INI;
8897 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8898 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8899 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8900 socket_tcp.agent.server = &socket_tcp;
8901
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008902 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008903
8904#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008905 /* Init TCP server: unchanged parameters */
8906 memset(&socket_ssl, 0, sizeof(socket_ssl));
8907 socket_ssl.next = NULL;
8908 socket_ssl.proxy = &socket_proxy;
8909 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8910 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008911 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008912 socket_ssl.idle_conns = NULL;
8913 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008914 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008915 socket_ssl.last_change = 0;
8916 socket_ssl.id = "LUA-SSL-CONN";
8917 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8918 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8919 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8920
8921 /* XXX: Copy default parameter from default server,
8922 * but the default server is not initialized.
8923 */
8924 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8925 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8926 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8927 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8928 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8929 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8930 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8931 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8932 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8933 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8934
8935 socket_ssl.check.status = HCHK_STATUS_INI;
8936 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8937 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8938 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8939 socket_ssl.check.server = &socket_ssl;
8940
8941 socket_ssl.agent.status = HCHK_STATUS_INI;
8942 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8943 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8944 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8945 socket_ssl.agent.server = &socket_ssl;
8946
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008947 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008948 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008949
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008950 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008951 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8952 /*
8953 *
8954 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008955 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008956 * features like client certificates and ssl_verify.
8957 *
8958 */
8959 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8960 if (tmp_error != 0) {
8961 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8962 abort(); /* This must be never arrives because the command line
8963 not editable by the user. */
8964 }
8965 idx += kw->skip;
8966 }
8967 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008968#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008969
8970 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008971}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008972
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008973static void hlua_deinit()
8974{
8975 lua_close(gL.T);
8976}
8977
8978REGISTER_POST_DEINIT(hlua_deinit);
8979
Willy Tarreau80713382018-11-26 10:19:54 +01008980static void hlua_register_build_options(void)
8981{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008982 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008983
Willy Tarreaubb57d942016-12-21 19:04:56 +01008984 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8985 hap_register_build_opts(ptr, 1);
8986}
Willy Tarreau80713382018-11-26 10:19:54 +01008987
8988INITCALL0(STG_REGISTER, hlua_register_build_options);