blob: 30b7b8a4aaf7009cafe94135a3271f8163cea7fe [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>
Willy Tarreau6be78492020-06-05 00:00:29 +020029#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020030#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020031#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020032#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020033#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020034#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020035#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020036#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020037#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020039#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020040#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020041#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020042#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020043#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020044#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020045#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020047#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020048#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020049#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020050#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020051#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020052#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020053#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020054#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020056#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020057#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/xref.h>
59
Thierry FOURNIER380d0932015-01-23 14:27:52 +010060
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010061/* Lua uses longjmp to perform yield or throwing errors. This
62 * macro is used only for identifying the function that can
63 * not return because a longjmp is executed.
64 * __LJMP marks a prototype of hlua file that can use longjmp.
65 * WILL_LJMP() marks an lua function that will use longjmp.
66 * MAY_LJMP() marks an lua function that may use longjmp.
67 */
68#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020069#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010070#define MAY_LJMP(func) func
71
Thierry FOURNIERbabae282015-09-17 11:36:37 +020072/* This couple of function executes securely some Lua calls outside of
73 * the lua runtime environment. Each Lua call can return a longjmp
74 * if it encounter a memory error.
75 *
76 * Lua documentation extract:
77 *
78 * If an error happens outside any protected environment, Lua calls
79 * a panic function (see lua_atpanic) and then calls abort, thus
80 * exiting the host application. Your panic function can avoid this
81 * exit by never returning (e.g., doing a long jump to your own
82 * recovery point outside Lua).
83 *
84 * The panic function runs as if it were a message handler (see
85 * §2.3); in particular, the error message is at the top of the
86 * stack. However, there is no guarantee about stack space. To push
87 * anything on the stack, the panic function must first check the
88 * available space (see §4.2).
89 *
90 * We must check all the Lua entry point. This includes:
91 * - The include/proto/hlua.h exported functions
92 * - the task wrapper function
93 * - The action wrapper function
94 * - The converters wrapper function
95 * - The sample-fetch wrapper functions
96 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +050097 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080098 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020099 *
100 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
101 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
102 * because they must be exists in the program stack when the longjmp
103 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200104 *
105 * Note that the Lua processing is not really thread safe. It provides
106 * heavy system which consists to add our own lock function in the Lua
107 * code and recompile the library. This system will probably not accepted
108 * by maintainers of various distribs.
109 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500110 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200111 * quick looking on the Lua sources displays a lua_lock() a the start
112 * of function and a lua_unlock() at the end of the function. So I
113 * conclude that the Lua thread safe mode just perform a mutex around
114 * all execution. So I prefer to do this in the HAProxy code, it will be
115 * easier for distro maintainers.
116 *
117 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
118 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
119 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200120 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100121__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200122THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100124static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125
126#define SET_SAFE_LJMP(__L) \
127 ({ \
128 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100129 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200130 if (setjmp(safe_ljmp_env) != 0) { \
131 lua_atpanic(__L, hlua_panic_safe); \
132 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200134 } else { \
135 lua_atpanic(__L, hlua_panic_ljmp); \
136 ret = 1; \
137 } \
138 ret; \
139 })
140
141/* If we are the last function catching Lua errors, we
142 * must reset the panic function.
143 */
144#define RESET_SAFE_LJMP(__L) \
145 do { \
146 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100147 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200148 } while(0)
149
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200150/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200151#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100152/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200153#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200154/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100155#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100156#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200157
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100158/* The main Lua execution context. */
159struct hlua gL;
160
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100161/* This is the memory pool containing struct lua for applets
162 * (including cli).
163 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100164DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100166/* Used for Socket connection. */
167static struct proxy socket_proxy;
168static struct server socket_tcp;
169#ifdef USE_OPENSSL
170static struct server socket_ssl;
171#endif
172
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100173/* List head of the function called at the initialisation time. */
174struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
175
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100176/* The following variables contains the reference of the different
177 * Lua classes. These references are useful for identify metadata
178 * associated with an object.
179 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100180static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100181static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100182static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100183static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100184static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100185static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200186static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200187static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100189static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100190
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100191/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200192 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193 * short timeout. Lua linked with tasks doesn't have a timeout
194 * because a task may remain alive during all the haproxy execution.
195 */
196static unsigned int hlua_timeout_session = 4000; /* session timeout. */
197static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200198static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100199
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100200/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
201 * it is used for preventing infinite loops.
202 *
203 * I test the scheer with an infinite loop containing one incrementation
204 * and one test. I run this loop between 10 seconds, I raise a ceil of
205 * 710M loops from one interrupt each 9000 instructions, so I fix the value
206 * to one interrupt each 10 000 instructions.
207 *
208 * configured | Number of
209 * instructions | loops executed
210 * between two | in milions
211 * forced yields |
212 * ---------------+---------------
213 * 10 | 160
214 * 500 | 670
215 * 1000 | 680
216 * 5000 | 700
217 * 7000 | 700
218 * 8000 | 700
219 * 9000 | 710 <- ceil
220 * 10000 | 710
221 * 100000 | 710
222 * 1000000 | 710
223 *
224 */
225static unsigned int hlua_nb_instruction = 10000;
226
Willy Tarreau32f61e22015-03-18 17:54:59 +0100227/* Descriptor for the memory allocation state. If limit is not null, it will
228 * be enforced on any memory allocation.
229 */
230struct hlua_mem_allocator {
231 size_t allocated;
232 size_t limit;
233};
234
235static struct hlua_mem_allocator hlua_global_allocator;
236
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100237/* These functions converts types between HAProxy internal args or
238 * sample and LUA types. Another function permits to check if the
239 * LUA stack contains arguments according with an required ARG_T
240 * format.
241 */
242static int hlua_arg2lua(lua_State *L, const struct arg *arg);
243static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100244__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100245 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100246static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100247static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100248static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
249
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100250__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200251
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200252#define SEND_ERR(__be, __fmt, __args...) \
253 do { \
254 send_log(__be, LOG_ERR, __fmt, ## __args); \
255 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100256 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200257 } while (0)
258
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100259/* Used to check an Lua function type in the stack. It creates and
260 * returns a reference of the function. This function throws an
261 * error if the rgument is not a "function".
262 */
263__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
264{
265 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100266 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100267 WILL_LJMP(luaL_argerror(L, argno, msg));
268 }
269 lua_pushvalue(L, argno);
270 return luaL_ref(L, LUA_REGISTRYINDEX);
271}
272
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200273/* Return the string that is of the top of the stack. */
274const char *hlua_get_top_error_string(lua_State *L)
275{
276 if (lua_gettop(L) < 1)
277 return "unknown error";
278 if (lua_type(L, -1) != LUA_TSTRING)
279 return "unknown error";
280 return lua_tostring(L, -1);
281}
282
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200283__LJMP static const char *hlua_traceback(lua_State *L)
284{
285 lua_Debug ar;
286 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200287 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200288 int filled = 0;
289
290 while (lua_getstack(L, level++, &ar)) {
291
292 /* Add separator */
293 if (filled)
294 chunk_appendf(msg, ", ");
295 filled = 1;
296
297 /* Fill fields:
298 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
299 * 'l': fills in the field currentline;
300 * 'n': fills in the field name and namewhat;
301 * 't': fills in the field istailcall;
302 */
303 lua_getinfo(L, "Slnt", &ar);
304
305 /* Append code localisation */
306 if (ar.currentline > 0)
307 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
308 else
309 chunk_appendf(msg, "%s ", ar.short_src);
310
311 /*
312 * Get function name
313 *
314 * if namewhat is no empty, name is defined.
315 * what contains "Lua" for Lua function, "C" for C function,
316 * or "main" for main code.
317 */
318 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
319 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
320
321 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
322 chunk_appendf(msg, "main chunk");
323
324 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
325 chunk_appendf(msg, "C function line %d", ar.linedefined);
326
327 else /* nothing left... */
328 chunk_appendf(msg, "?");
329
330
331 /* Display tailed call */
332 if (ar.istailcall)
333 chunk_appendf(msg, " ...");
334 }
335
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200336 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200337}
338
339
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100340/* This function check the number of arguments available in the
341 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500342 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100343 */
344__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
345{
346 if (lua_gettop(L) == nb)
347 return;
348 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
349}
350
Mark Lakes22154b42018-01-29 14:38:40 -0800351/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100352 * and the line number where the error is encountered.
353 */
354static int hlua_pusherror(lua_State *L, const char *fmt, ...)
355{
356 va_list argp;
357 va_start(argp, fmt);
358 luaL_where(L, 1);
359 lua_pushvfstring(L, fmt, argp);
360 va_end(argp);
361 lua_concat(L, 2);
362 return 1;
363}
364
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100365/* This functions is used with sample fetch and converters. It
366 * converts the HAProxy configuration argument in a lua stack
367 * values.
368 *
369 * It takes an array of "arg", and each entry of the array is
370 * converted and pushed in the LUA stack.
371 */
372static int hlua_arg2lua(lua_State *L, const struct arg *arg)
373{
374 switch (arg->type) {
375 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100376 case ARGT_TIME:
377 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100378 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 break;
380
381 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200382 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_IPV4:
386 case ARGT_IPV6:
387 case ARGT_MSK4:
388 case ARGT_MSK6:
389 case ARGT_FE:
390 case ARGT_BE:
391 case ARGT_TAB:
392 case ARGT_SRV:
393 case ARGT_USR:
394 case ARGT_MAP:
395 default:
396 lua_pushnil(L);
397 break;
398 }
399 return 1;
400}
401
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500402/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100403 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500404 * with sample fetch wrappers. The input arguments are given to the
405 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100406 */
407static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
408{
409 switch (lua_type(L, ud)) {
410
411 case LUA_TNUMBER:
412 case LUA_TBOOLEAN:
413 arg->type = ARGT_SINT;
414 arg->data.sint = lua_tointeger(L, ud);
415 break;
416
417 case LUA_TSTRING:
418 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200419 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200420 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200421 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200422 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100423 break;
424
425 case LUA_TUSERDATA:
426 case LUA_TNIL:
427 case LUA_TTABLE:
428 case LUA_TFUNCTION:
429 case LUA_TTHREAD:
430 case LUA_TLIGHTUSERDATA:
431 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200432 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433 break;
434 }
435 return 1;
436}
437
438/* the following functions are used to convert a struct sample
439 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500440 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100441 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100442static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200444 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449
450 case SMP_T_BIN:
451 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 break;
454
455 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
458 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
459 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
460 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
461 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
462 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
463 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
464 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
465 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100467 break;
468 default:
469 lua_pushnil(L);
470 break;
471 }
472 break;
473
474 case SMP_T_IPV4:
475 case SMP_T_IPV6:
476 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200477 if (sample_casts[smp->data.type][SMP_T_STR] &&
478 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100480 else
481 lua_pushnil(L);
482 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100483 default:
484 lua_pushnil(L);
485 break;
486 }
487 return 1;
488}
489
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100490/* the following functions are used to convert a struct sample
491 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500492 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100493 */
494static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
495{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200496 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497
498 case SMP_T_BIN:
499 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501 break;
502
503 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
506 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
507 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
508 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
509 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
510 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
511 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
512 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
513 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100515 break;
516 default:
517 lua_pushstring(L, "");
518 break;
519 }
520 break;
521
522 case SMP_T_SINT:
523 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 case SMP_T_IPV4:
525 case SMP_T_IPV6:
526 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 if (sample_casts[smp->data.type][SMP_T_STR] &&
528 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 else
531 lua_pushstring(L, "");
532 break;
533 default:
534 lua_pushstring(L, "");
535 break;
536 }
537 return 1;
538}
539
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540/* the following functions are used to convert an Lua type in a
541 * struct sample. This is useful to provide data from a converter
542 * to the LUA code.
543 */
544static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
545{
546 switch (lua_type(L, ud)) {
547
548 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 break;
552
553
554 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200562 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200563 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200564 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200565 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 break;
567
568 case LUA_TUSERDATA:
569 case LUA_TNIL:
570 case LUA_TTABLE:
571 case LUA_TFUNCTION:
572 case LUA_TTHREAD:
573 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200574 case LUA_TNONE:
575 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200576 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100578 break;
579 }
580 return 1;
581}
582
Ilya Shipitsind4259502020-04-08 01:07:56 +0500583/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800584 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100585 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100586 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500587 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100588 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100590__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100591 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592{
593 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200594 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100595 struct proxy *px;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200596 const char *msg = NULL;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100597 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598
599 idx = 0;
600 min_arg = ARGM(mask);
601 mask >>= ARGM_BITS;
602
603 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200604 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100605
606 /* Check oversize. */
607 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200608 msg = "Malformed argument mask";
609 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610 }
611
612 /* Check for mandatory arguments. */
613 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100614 if (idx < min_arg) {
615
616 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200617 if (idx > 0) {
618 msg = "Mandatory argument expected";
619 goto error;
620 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100621
622 /* If first argument have a certain type, some default values
623 * may be used. See the function smp_resolve_args().
624 */
625 switch (mask & ARGT_MASK) {
626
627 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200628 if (!(p->cap & PR_CAP_FE)) {
629 msg = "Mandatory argument expected";
630 goto error;
631 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100632 argp[idx].data.prx = p;
633 argp[idx].type = ARGT_FE;
634 argp[idx+1].type = ARGT_STOP;
635 break;
636
637 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200638 if (!(p->cap & PR_CAP_BE)) {
639 msg = "Mandatory argument expected";
640 goto error;
641 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100642 argp[idx].data.prx = p;
643 argp[idx].type = ARGT_BE;
644 argp[idx+1].type = ARGT_STOP;
645 break;
646
647 case ARGT_TAB:
648 argp[idx].data.prx = p;
649 argp[idx].type = ARGT_TAB;
650 argp[idx+1].type = ARGT_STOP;
651 break;
652
653 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200654 msg = "Mandatory argument expected";
655 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100656 break;
657 }
658 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200659 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100660 }
661
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500662 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100663 if ((mask & ARGT_MASK) == ARGT_STOP &&
664 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200665 msg = "Last argument expected";
666 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100667 }
668
669 if ((mask & ARGT_MASK) == ARGT_STOP &&
670 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200671 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100672 }
673
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200674 /* Convert some argument types. All string in argp[] are for not
675 * duplicated yet.
676 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100677 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100678 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200679 if (argp[idx].type != ARGT_SINT) {
680 msg = "integer expected";
681 goto error;
682 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100683 argp[idx].type = ARGT_SINT;
684 break;
685
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100686 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200687 if (argp[idx].type != ARGT_SINT) {
688 msg = "integer expected";
689 goto error;
690 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200691 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100692 break;
693
694 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200695 if (argp[idx].type != ARGT_SINT) {
696 msg = "integer expected";
697 goto error;
698 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200699 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100700 break;
701
702 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200703 if (argp[idx].type != ARGT_STR) {
704 msg = "string expected";
705 goto error;
706 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200707 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200708 if (!argp[idx].data.prx) {
709 msg = "frontend doesn't exist";
710 goto error;
711 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100712 argp[idx].type = ARGT_FE;
713 break;
714
715 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200716 if (argp[idx].type != ARGT_STR) {
717 msg = "string expected";
718 goto error;
719 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200720 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200721 if (!argp[idx].data.prx) {
722 msg = "backend doesn't exist";
723 goto error;
724 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100725 argp[idx].type = ARGT_BE;
726 break;
727
728 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200729 if (argp[idx].type != ARGT_STR) {
730 msg = "string expected";
731 goto error;
732 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200733 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200734 if (!argp[idx].data.t) {
735 msg = "table doesn't exist";
736 goto error;
737 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100738 argp[idx].type = ARGT_TAB;
739 break;
740
741 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200742 if (argp[idx].type != ARGT_STR) {
743 msg = "string expected";
744 goto error;
745 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200746 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100747 if (sname) {
748 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200749 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200750 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200751 if (!px) {
752 msg = "backend doesn't exist";
753 goto error;
754 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100755 }
756 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200757 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100758 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100759 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100760 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200761 if (!argp[idx].data.srv) {
762 msg = "server doesn't exist";
763 goto error;
764 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100765 argp[idx].type = ARGT_SRV;
766 break;
767
768 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200769 if (argp[idx].type != ARGT_STR) {
770 msg = "string expected";
771 goto error;
772 }
773 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
774 msg = "invalid IPv4 address";
775 goto error;
776 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100777 argp[idx].type = ARGT_IPV4;
778 break;
779
780 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200781 if (argp[idx].type == ARGT_SINT)
782 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
783 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200784 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
785 msg = "invalid IPv4 mask";
786 goto error;
787 }
788 }
789 else {
790 msg = "integer or string expected";
791 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200792 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100793 argp[idx].type = ARGT_MSK4;
794 break;
795
796 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200797 if (argp[idx].type != ARGT_STR) {
798 msg = "string expected";
799 goto error;
800 }
801 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
802 msg = "invalid IPv6 address";
803 goto error;
804 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100805 argp[idx].type = ARGT_IPV6;
806 break;
807
808 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200809 if (argp[idx].type == ARGT_SINT)
810 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
811 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200812 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
813 msg = "invalid IPv6 mask";
814 goto error;
815 }
816 }
817 else {
818 msg = "integer or string expected";
819 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200820 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100821 argp[idx].type = ARGT_MSK6;
822 break;
823
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100824 case ARGT_MAP:
825 case ARGT_REG:
826 case ARGT_USR:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200827 msg = "type not yet supported";
828 goto error;
829 break;
830
831 case ARGT_STR:
832 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
833 msg = "unable to duplicate string arg";
834 goto error;
835 }
836 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100837 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200838
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100839 }
840
841 /* Check for type of argument. */
842 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200843 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
844 arg_type_names[(mask & ARGT_MASK)],
845 arg_type_names[argp[idx].type & ARGT_MASK]);
846 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100847 }
848
849 /* Next argument. */
850 mask >>= ARGT_BITS;
851 idx++;
852 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200853 return 0;
854
855 error:
856 for (i = 0; i < idx; i++) {
857 if (argp[i].type == ARGT_STR)
858 chunk_destroy(&argp[i].data.str);
859 }
860 WILL_LJMP(luaL_argerror(L, first + idx, msg));
861 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100862}
863
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100864/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500865 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100866 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100867 *
868 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100869 * - hlua_sethlua : create the association between hlua context and lua_state.
870 */
871static inline struct hlua *hlua_gethlua(lua_State *L)
872{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100873 struct hlua **hlua = lua_getextraspace(L);
874 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100875}
876static inline void hlua_sethlua(struct hlua *hlua)
877{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100878 struct hlua **hlua_store = lua_getextraspace(hlua->T);
879 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100880}
881
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100882/* This function is used to send logs. It try to send on screen (stderr)
883 * and on the default syslog server.
884 */
885static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
886{
887 struct tm tm;
888 char *p;
889
890 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200891 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100892 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200893 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200894 /* Break the message if exceed the buffer size. */
895 *(p-4) = ' ';
896 *(p-3) = '.';
897 *(p-2) = '.';
898 *(p-1) = '.';
899 break;
900 }
Willy Tarreau90807112020-02-25 08:16:33 +0100901 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100902 *p = *msg;
903 else
904 *p = '.';
905 }
906 *p = '\0';
907
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200908 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100909 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200910 get_localtime(date.tv_sec, &tm);
911 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100912 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200913 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100914 fflush(stderr);
915 }
916}
917
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100918/* This function just ensure that the yield will be always
919 * returned with a timeout and permit to set some flags
920 */
921__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100922 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100923{
924 struct hlua *hlua = hlua_gethlua(L);
925
926 /* Set the wake timeout. If timeout is required, we set
927 * the expiration time.
928 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200929 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100930
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100931 hlua->flags |= flags;
932
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100933 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200934 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100935}
936
Willy Tarreau87b09662015-04-03 00:22:06 +0200937/* This function initialises the Lua environment stored in the stream.
938 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100939 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200940 *
941 * This function is particular. it initialises a new Lua thread. If the
942 * initialisation fails (example: out of memory error), the lua function
943 * throws an error (longjmp).
944 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100945 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500946 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100947 * threads appear, the safe environment set a lock to ensure only one
948 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500949 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100950 *
951 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500952 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100953 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800954 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200955 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800956 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500957 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100958 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100959int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100960{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100961 if (!already_safe) {
962 if (!SET_SAFE_LJMP(gL.T)) {
963 lua->Tref = LUA_REFNIL;
964 return 0;
965 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200966 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100967 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100968 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100969 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100970 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100971 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100972 lua->T = lua_newthread(gL.T);
973 if (!lua->T) {
974 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100975 if (!already_safe)
976 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100977 return 0;
978 }
979 hlua_sethlua(lua);
980 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
981 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100982 if (!already_safe)
983 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100984 return 1;
985}
986
Willy Tarreau87b09662015-04-03 00:22:06 +0200987/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100988 * is destroyed. The destroy also the memory context. The struct "lua"
989 * is not freed.
990 */
991void hlua_ctx_destroy(struct hlua *lua)
992{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100993 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100994 return;
995
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100996 if (!lua->T)
997 goto end;
998
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100999 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001000 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001001
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001002 if (!SET_SAFE_LJMP(lua->T))
1003 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001004 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001005 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001006
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001007 if (!SET_SAFE_LJMP(gL.T))
1008 return;
1009 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1010 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001011 /* Forces a garbage collecting process. If the Lua program is finished
1012 * without error, we run the GC on the thread pointer. Its freed all
1013 * the unused memory.
1014 * If the thread is finnish with an error or is currently yielded,
1015 * it seems that the GC applied on the thread doesn't clean anything,
1016 * so e run the GC on the main thread.
1017 * NOTE: maybe this action locks all the Lua threads untiml the en of
1018 * the garbage collection.
1019 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001020 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001021 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001022 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001023 lua_gc(gL.T, LUA_GCCOLLECT, 0);
1024 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001025 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001026
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001027 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001028
1029end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001030 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001031}
1032
1033/* This function is used to restore the Lua context when a coroutine
1034 * fails. This function copy the common memory between old coroutine
1035 * and the new coroutine. The old coroutine is destroyed, and its
1036 * replaced by the new coroutine.
1037 * If the flag "keep_msg" is set, the last entry of the old is assumed
1038 * as string error message and it is copied in the new stack.
1039 */
1040static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1041{
1042 lua_State *T;
1043 int new_ref;
1044
1045 /* Renew the main LUA stack doesn't have sense. */
1046 if (lua == &gL)
1047 return 0;
1048
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001049 /* New Lua coroutine. */
1050 T = lua_newthread(gL.T);
1051 if (!T)
1052 return 0;
1053
1054 /* Copy last error message. */
1055 if (keep_msg)
1056 lua_xmove(lua->T, T, 1);
1057
1058 /* Copy data between the coroutines. */
1059 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1060 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001061 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001062
1063 /* Destroy old data. */
1064 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1065
1066 /* The thread is garbage collected by Lua. */
1067 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1068
1069 /* Fill the struct with the new coroutine values. */
1070 lua->Mref = new_ref;
1071 lua->T = T;
1072 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1073
1074 /* Set context. */
1075 hlua_sethlua(lua);
1076
1077 return 1;
1078}
1079
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001080void hlua_hook(lua_State *L, lua_Debug *ar)
1081{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001082 struct hlua *hlua = hlua_gethlua(L);
1083
1084 /* Lua cannot yield when its returning from a function,
1085 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001086 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001087 */
1088 if (lua_gethookmask(L) & LUA_MASKRET) {
1089 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1090 return;
1091 }
1092
1093 /* restore the interrupt condition. */
1094 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1095
1096 /* If we interrupt the Lua processing in yieldable state, we yield.
1097 * If the state is not yieldable, trying yield causes an error.
1098 */
1099 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001100 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001101
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001102 /* If we cannot yield, update the clock and check the timeout. */
1103 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001104 hlua->run_time += now_ms - hlua->start_time;
1105 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001106 lua_pushfstring(L, "execution timeout");
1107 WILL_LJMP(lua_error(L));
1108 }
1109
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001110 /* Update the start time. */
1111 hlua->start_time = now_ms;
1112
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001113 /* Try to interrupt the process at the end of the current
1114 * unyieldable function.
1115 */
1116 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001117}
1118
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001119/* This function start or resumes the Lua stack execution. If the flag
1120 * "yield_allowed" if no set and the LUA stack execution returns a yield
1121 * The function return an error.
1122 *
1123 * The function can returns 4 values:
1124 * - HLUA_E_OK : The execution is terminated without any errors.
1125 * - HLUA_E_AGAIN : The execution must continue at the next associated
1126 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001127 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001128 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001129 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001130 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001131 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001132 * LUA code.
1133 */
1134static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1135{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001136#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1137 int nres;
1138#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001139 int ret;
1140 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001141 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001143 /* Initialise run time counter. */
1144 if (!HLUA_IS_RUNNING(lua))
1145 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001146
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001147 /* Lock the whole Lua execution. This lock must be before the
1148 * label "resume_execution".
1149 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001150 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001151
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001152resume_execution:
1153
1154 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1155 * instructions. it is used for preventing infinite loops.
1156 */
1157 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1158
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001159 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001160 HLUA_SET_RUN(lua);
1161 HLUA_CLR_CTRLYIELD(lua);
1162 HLUA_CLR_WAKERESWR(lua);
1163 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001164
Christopher Fauletbc275a92020-02-26 14:55:16 +01001165 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001166 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001167 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001168
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001169 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001170#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1171 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1172#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001174#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001175 switch (ret) {
1176
1177 case LUA_OK:
1178 ret = HLUA_E_OK;
1179 break;
1180
1181 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001182 /* Check if the execution timeout is expired. It it is the case, we
1183 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001184 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001185 tv_update_date(0, 1);
1186 lua->run_time += now_ms - lua->start_time;
1187 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001188 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001189 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001190 break;
1191 }
1192 /* Process the forced yield. if the general yield is not allowed or
1193 * if no task were associated this the current Lua execution
1194 * coroutine, we resume the execution. Else we want to return in the
1195 * scheduler and we want to be waked up again, to continue the
1196 * current Lua execution. So we schedule our own task.
1197 */
1198 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001199 if (!yield_allowed || !lua->task)
1200 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001201 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001202 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 if (!yield_allowed) {
1204 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001205 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 break;
1207 }
1208 ret = HLUA_E_AGAIN;
1209 break;
1210
1211 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001212
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001213 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001214 * because the errors ares the only one mean to return immediately
1215 * from and lua execution.
1216 */
1217 if (lua->flags & HLUA_EXIT) {
1218 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001219 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001220 break;
1221 }
1222
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001223 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001224 if (!lua_checkstack(lua->T, 1)) {
1225 ret = HLUA_E_ERR;
1226 break;
1227 }
1228 msg = lua_tostring(lua->T, -1);
1229 lua_settop(lua->T, 0); /* Empty the stack. */
1230 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001231 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001232 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001233 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001234 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001235 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001236 ret = HLUA_E_ERRMSG;
1237 break;
1238
1239 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001240 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001241 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001242 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001243 break;
1244
1245 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001246 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001247 if (!lua_checkstack(lua->T, 1)) {
1248 ret = HLUA_E_ERR;
1249 break;
1250 }
1251 msg = lua_tostring(lua->T, -1);
1252 lua_settop(lua->T, 0); /* Empty the stack. */
1253 lua_pop(lua->T, 1);
1254 if (msg)
1255 lua_pushfstring(lua->T, "message handler error: %s", msg);
1256 else
1257 lua_pushfstring(lua->T, "message handler error");
1258 ret = HLUA_E_ERRMSG;
1259 break;
1260
1261 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001262 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001263 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001264 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001265 break;
1266 }
1267
1268 switch (ret) {
1269 case HLUA_E_AGAIN:
1270 break;
1271
1272 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001273 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001274 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001275 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001276 break;
1277
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001278 case HLUA_E_ETMOUT:
1279 case HLUA_E_NOMEM:
1280 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001281 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001282 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001283 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001284 hlua_ctx_renew(lua, 0);
1285 break;
1286
1287 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001288 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001289 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001290 break;
1291 }
1292
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001293 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001294 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001295
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001296 return ret;
1297}
1298
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001299/* This function exit the current code. */
1300__LJMP static int hlua_done(lua_State *L)
1301{
1302 struct hlua *hlua = hlua_gethlua(L);
1303
1304 hlua->flags |= HLUA_EXIT;
1305 WILL_LJMP(lua_error(L));
1306
1307 return 0;
1308}
1309
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001310/* This function is an LUA binding. It provides a function
1311 * for deleting ACL from a referenced ACL file.
1312 */
1313__LJMP static int hlua_del_acl(lua_State *L)
1314{
1315 const char *name;
1316 const char *key;
1317 struct pat_ref *ref;
1318
1319 MAY_LJMP(check_args(L, 2, "del_acl"));
1320
1321 name = MAY_LJMP(luaL_checkstring(L, 1));
1322 key = MAY_LJMP(luaL_checkstring(L, 2));
1323
1324 ref = pat_ref_lookup(name);
1325 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001326 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001327
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001328 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001329 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001330 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001331 return 0;
1332}
1333
1334/* This function is an LUA binding. It provides a function
1335 * for deleting map entry from a referenced map file.
1336 */
1337static int hlua_del_map(lua_State *L)
1338{
1339 const char *name;
1340 const char *key;
1341 struct pat_ref *ref;
1342
1343 MAY_LJMP(check_args(L, 2, "del_map"));
1344
1345 name = MAY_LJMP(luaL_checkstring(L, 1));
1346 key = MAY_LJMP(luaL_checkstring(L, 2));
1347
1348 ref = pat_ref_lookup(name);
1349 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001350 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001351
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001352 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001353 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001354 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001355 return 0;
1356}
1357
1358/* This function is an LUA binding. It provides a function
1359 * for adding ACL pattern from a referenced ACL file.
1360 */
1361static int hlua_add_acl(lua_State *L)
1362{
1363 const char *name;
1364 const char *key;
1365 struct pat_ref *ref;
1366
1367 MAY_LJMP(check_args(L, 2, "add_acl"));
1368
1369 name = MAY_LJMP(luaL_checkstring(L, 1));
1370 key = MAY_LJMP(luaL_checkstring(L, 2));
1371
1372 ref = pat_ref_lookup(name);
1373 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001374 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001375
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001376 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001377 if (pat_ref_find_elt(ref, key) == NULL)
1378 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001379 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001380 return 0;
1381}
1382
1383/* This function is an LUA binding. It provides a function
1384 * for setting map pattern and sample from a referenced map
1385 * file.
1386 */
1387static int hlua_set_map(lua_State *L)
1388{
1389 const char *name;
1390 const char *key;
1391 const char *value;
1392 struct pat_ref *ref;
1393
1394 MAY_LJMP(check_args(L, 3, "set_map"));
1395
1396 name = MAY_LJMP(luaL_checkstring(L, 1));
1397 key = MAY_LJMP(luaL_checkstring(L, 2));
1398 value = MAY_LJMP(luaL_checkstring(L, 3));
1399
1400 ref = pat_ref_lookup(name);
1401 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001402 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001403
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001404 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001405 if (pat_ref_find_elt(ref, key) != NULL)
1406 pat_ref_set(ref, key, value, NULL);
1407 else
1408 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001409 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001410 return 0;
1411}
1412
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001413/* A class is a lot of memory that contain data. This data can be a table,
1414 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001415 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001416 * the name of the object (_G[<name>] = <metable> ).
1417 *
1418 * A metable is a table that modify the standard behavior of a standard
1419 * access to the associated data. The entries of this new metatable are
1420 * defined as is:
1421 *
1422 * http://lua-users.org/wiki/MetatableEvents
1423 *
1424 * __index
1425 *
1426 * we access an absent field in a table, the result is nil. This is
1427 * true, but it is not the whole truth. Actually, such access triggers
1428 * the interpreter to look for an __index metamethod: If there is no
1429 * such method, as usually happens, then the access results in nil;
1430 * otherwise, the metamethod will provide the result.
1431 *
1432 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1433 * the key does not appear in the table, but the metatable has an __index
1434 * property:
1435 *
1436 * - if the value is a function, the function is called, passing in the
1437 * table and the key; the return value of that function is returned as
1438 * the result.
1439 *
1440 * - if the value is another table, the value of the key in that table is
1441 * asked for and returned (and if it doesn't exist in that table, but that
1442 * table's metatable has an __index property, then it continues on up)
1443 *
1444 * - Use "rawget(myTable,key)" to skip this metamethod.
1445 *
1446 * http://www.lua.org/pil/13.4.1.html
1447 *
1448 * __newindex
1449 *
1450 * Like __index, but control property assignment.
1451 *
1452 * __mode - Control weak references. A string value with one or both
1453 * of the characters 'k' and 'v' which specifies that the the
1454 * keys and/or values in the table are weak references.
1455 *
1456 * __call - Treat a table like a function. When a table is followed by
1457 * parenthesis such as "myTable( 'foo' )" and the metatable has
1458 * a __call key pointing to a function, that function is invoked
1459 * (passing any specified arguments) and the return value is
1460 * returned.
1461 *
1462 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1463 * called, if the metatable for myTable has a __metatable
1464 * key, the value of that key is returned instead of the
1465 * actual metatable.
1466 *
1467 * __tostring - Control string representation. When the builtin
1468 * "tostring( myTable )" function is called, if the metatable
1469 * for myTable has a __tostring property set to a function,
1470 * that function is invoked (passing myTable to it) and the
1471 * return value is used as the string representation.
1472 *
1473 * __len - Control table length. When the table length is requested using
1474 * the length operator ( '#' ), if the metatable for myTable has
1475 * a __len key pointing to a function, that function is invoked
1476 * (passing myTable to it) and the return value used as the value
1477 * of "#myTable".
1478 *
1479 * __gc - Userdata finalizer code. When userdata is set to be garbage
1480 * collected, if the metatable has a __gc field pointing to a
1481 * function, that function is first invoked, passing the userdata
1482 * to it. The __gc metamethod is not called for tables.
1483 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1484 *
1485 * Special metamethods for redefining standard operators:
1486 * http://www.lua.org/pil/13.1.html
1487 *
1488 * __add "+"
1489 * __sub "-"
1490 * __mul "*"
1491 * __div "/"
1492 * __unm "!"
1493 * __pow "^"
1494 * __concat ".."
1495 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001496 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001497 * http://www.lua.org/pil/13.2.html
1498 *
1499 * __eq "=="
1500 * __lt "<"
1501 * __le "<="
1502 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001503
1504/*
1505 *
1506 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001507 * Class Map
1508 *
1509 *
1510 */
1511
1512/* Returns a struct hlua_map if the stack entry "ud" is
1513 * a class session, otherwise it throws an error.
1514 */
1515__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1516{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001517 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001518}
1519
1520/* This function is the map constructor. It don't need
1521 * the class Map object. It creates and return a new Map
1522 * object. It must be called only during "body" or "init"
1523 * context because it process some filesystem accesses.
1524 */
1525__LJMP static int hlua_map_new(struct lua_State *L)
1526{
1527 const char *fn;
1528 int match = PAT_MATCH_STR;
1529 struct sample_conv conv;
1530 const char *file = "";
1531 int line = 0;
1532 lua_Debug ar;
1533 char *err = NULL;
1534 struct arg args[2];
1535
1536 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1537 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1538
1539 fn = MAY_LJMP(luaL_checkstring(L, 1));
1540
1541 if (lua_gettop(L) >= 2) {
1542 match = MAY_LJMP(luaL_checkinteger(L, 2));
1543 if (match < 0 || match >= PAT_MATCH_NUM)
1544 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1545 }
1546
1547 /* Get Lua filename and line number. */
1548 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1549 lua_getinfo(L, "Sl", &ar); /* get info about it */
1550 if (ar.currentline > 0) { /* is there info? */
1551 file = ar.short_src;
1552 line = ar.currentline;
1553 }
1554 }
1555
1556 /* fill fake sample_conv struct. */
1557 conv.kw = ""; /* unused. */
1558 conv.process = NULL; /* unused. */
1559 conv.arg_mask = 0; /* unused. */
1560 conv.val_args = NULL; /* unused. */
1561 conv.out_type = SMP_T_STR;
1562 conv.private = (void *)(long)match;
1563 switch (match) {
1564 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1565 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1566 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1567 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1568 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1569 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1570 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001571 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001572 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1573 default:
1574 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1575 }
1576
1577 /* fill fake args. */
1578 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001579 args[0].data.str.area = strdup(fn);
1580 args[0].data.str.data = strlen(fn);
1581 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001582 args[1].type = ARGT_STOP;
1583
1584 /* load the map. */
1585 if (!sample_load_map(args, &conv, file, line, &err)) {
1586 /* error case: we cant use luaL_error because we must
1587 * free the err variable.
1588 */
1589 luaL_where(L, 1);
1590 lua_pushfstring(L, "'new': %s.", err);
1591 lua_concat(L, 2);
1592 free(err);
Christopher Faulet73292e92020-08-06 08:40:09 +02001593 free(args[0].data.str.area);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001594 WILL_LJMP(lua_error(L));
1595 }
1596
1597 /* create the lua object. */
1598 lua_newtable(L);
1599 lua_pushlightuserdata(L, args[0].data.map);
1600 lua_rawseti(L, -2, 0);
1601
1602 /* Pop a class Map metatable and affect it to the userdata. */
1603 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1604 lua_setmetatable(L, -2);
1605
1606
1607 return 1;
1608}
1609
1610__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1611{
1612 struct map_descriptor *desc;
1613 struct pattern *pat;
1614 struct sample smp;
1615
1616 MAY_LJMP(check_args(L, 2, "lookup"));
1617 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001618 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001619 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001620 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001621 }
1622 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001623 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001624 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001625 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001626 }
1627
1628 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001629 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001630 if (str)
1631 lua_pushstring(L, "");
1632 else
1633 lua_pushnil(L);
1634 return 1;
1635 }
1636
1637 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001638 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001639 return 1;
1640}
1641
1642__LJMP static int hlua_map_lookup(struct lua_State *L)
1643{
1644 return _hlua_map_lookup(L, 0);
1645}
1646
1647__LJMP static int hlua_map_slookup(struct lua_State *L)
1648{
1649 return _hlua_map_lookup(L, 1);
1650}
1651
1652/*
1653 *
1654 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001655 * Class Socket
1656 *
1657 *
1658 */
1659
1660__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1661{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001662 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663}
1664
1665/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001666 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001667 * received.
1668 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001669static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001670{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001671 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001672
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001673 if (appctx->ctx.hlua_cosocket.die) {
1674 si_shutw(si);
1675 si_shutr(si);
1676 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001677 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1678 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001679 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1680 }
1681
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001682 /* If we cant write, wakeup the pending write signals. */
1683 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001684 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001685
1686 /* If we cant read, wakeup the pending read signals. */
1687 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001688 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001689
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001690 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001691 * to be notified whenever the connection completes.
1692 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001693 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001694 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001695 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001696 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001697 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001698 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699
1700 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001701 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001703 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001704 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001705 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706
1707 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001708 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001709 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001710
1711 /* Some data were injected in the buffer, notify the stream
1712 * interface.
1713 */
1714 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001715 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001716
1717 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001718 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001719 */
1720 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001721 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722}
1723
Willy Tarreau87b09662015-04-03 00:22:06 +02001724/* This function is called when the "struct stream" is destroyed.
1725 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001726 * Wake all the pending signals.
1727 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001728static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001729{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001730 struct xref *peer;
1731
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001732 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001733 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1734 if (peer)
1735 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001736
1737 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001738 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1739 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001740}
1741
1742/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001743 * uses this object. If the stream does not exists, just quit.
1744 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001745 * pending signal can rest in the read and write lists. destroy
1746 * it.
1747 */
1748__LJMP static int hlua_socket_gc(lua_State *L)
1749{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001750 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001751 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001752 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001754 MAY_LJMP(check_args(L, 1, "__gc"));
1755
1756 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001757 peer = xref_get_peer_and_lock(&socket->xref);
1758 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001759 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001760 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001761
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001762 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001763 appctx->ctx.hlua_cosocket.die = 1;
1764 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001765
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001766 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001767 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001768 return 0;
1769}
1770
1771/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001772 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001773 */
sada05ed3302018-05-11 11:48:18 -07001774__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001775{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001776 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001777 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001778 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001779 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001780
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001781 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001782
1783 /* Check if we run on the same thread than the xreator thread.
1784 * We cannot access to the socket if the thread is different.
1785 */
1786 if (socket->tid != tid)
1787 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1788
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001789 peer = xref_get_peer_and_lock(&socket->xref);
1790 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001792
1793 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001794 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001796 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001797 appctx->ctx.hlua_cosocket.die = 1;
1798 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001799
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001800 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001801 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001802 return 0;
1803}
1804
sada05ed3302018-05-11 11:48:18 -07001805/* The close function calls close_helper.
1806 */
1807__LJMP static int hlua_socket_close(lua_State *L)
1808{
1809 MAY_LJMP(check_args(L, 1, "close"));
1810 return hlua_socket_close_helper(L);
1811}
1812
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813/* This Lua function assumes that the stack contain three parameters.
1814 * 1 - USERDATA containing a struct socket
1815 * 2 - INTEGER with values of the macro defined below
1816 * If the integer is -1, we must read at most one line.
1817 * If the integer is -2, we ust read all the data until the
1818 * end of the stream.
1819 * If the integer is positive value, we must read a number of
1820 * bytes corresponding to this value.
1821 */
1822#define HLSR_READ_LINE (-1)
1823#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001824__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825{
1826 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1827 int wanted = lua_tointeger(L, 2);
1828 struct hlua *hlua = hlua_gethlua(L);
1829 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001830 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001831 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001832 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001833 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001834 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001835 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001836 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001837 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001838 struct stream_interface *si;
1839 struct stream *s;
1840 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001841 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001842
1843 /* Check if this lua stack is schedulable. */
1844 if (!hlua || !hlua->task)
1845 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1846 "'frontend', 'backend' or 'task'"));
1847
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001848 /* Check if we run on the same thread than the xreator thread.
1849 * We cannot access to the socket if the thread is different.
1850 */
1851 if (socket->tid != tid)
1852 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1853
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001854 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001855 peer = xref_get_peer_and_lock(&socket->xref);
1856 if (!peer)
1857 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001858 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1859 si = appctx->owner;
1860 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001862 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001863 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001864 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001865 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 if (nblk < 0) /* Connection close. */
1867 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001868 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001870
1871 /* remove final \r\n. */
1872 if (nblk == 1) {
1873 if (blk1[len1-1] == '\n') {
1874 len1--;
1875 skip_at_end++;
1876 if (blk1[len1-1] == '\r') {
1877 len1--;
1878 skip_at_end++;
1879 }
1880 }
1881 }
1882 else {
1883 if (blk2[len2-1] == '\n') {
1884 len2--;
1885 skip_at_end++;
1886 if (blk2[len2-1] == '\r') {
1887 len2--;
1888 skip_at_end++;
1889 }
1890 }
1891 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 }
1893
1894 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001896 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001897 if (nblk < 0) /* Connection close. */
1898 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001899 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001900 goto connection_empty;
1901 }
1902
1903 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001905 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 if (nblk < 0) /* Connection close. */
1907 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001908 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909 goto connection_empty;
1910
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001911 missing_bytes = wanted - socket->b.n;
1912 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001913 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001914 len1 = missing_bytes;
1915 } if (nblk == 2 && len1 + len2 > missing_bytes)
1916 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001917 }
1918
1919 len = len1;
1920
1921 luaL_addlstring(&socket->b, blk1, len1);
1922 if (nblk == 2) {
1923 len += len2;
1924 luaL_addlstring(&socket->b, blk2, len2);
1925 }
1926
1927 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001928 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001929
1930 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001931 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001932
1933 /* If the pattern reclaim to read all the data
1934 * in the connection, got out.
1935 */
1936 if (wanted == HLSR_READ_ALL)
1937 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001938 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001939 goto connection_empty;
1940
1941 /* Return result. */
1942 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001943 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001944 return 1;
1945
1946connection_closed:
1947
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001948 xref_unlock(&socket->xref, peer);
1949
1950no_peer:
1951
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001952 /* If the buffer containds data. */
1953 if (socket->b.n > 0) {
1954 luaL_pushresult(&socket->b);
1955 return 1;
1956 }
1957 lua_pushnil(L);
1958 lua_pushstring(L, "connection closed.");
1959 return 2;
1960
1961connection_empty:
1962
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001963 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1964 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001966 }
1967 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001968 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969 return 0;
1970}
1971
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001972/* This Lua function gets two parameters. The first one can be string
1973 * or a number. If the string is "*l", the user requires one line. If
1974 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975 * If the value is a number, the user require a number of bytes equal
1976 * to the value. The default value is "*l" (a line).
1977 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001978 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001979 * integer takes this values:
1980 * -1 : read a line
1981 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001982 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001983 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001984 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985 * concatenated with the read data.
1986 */
1987__LJMP static int hlua_socket_receive(struct lua_State *L)
1988{
1989 int wanted = HLSR_READ_LINE;
1990 const char *pattern;
1991 int type;
1992 char *error;
1993 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001994 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001995
1996 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1997 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1998
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001999 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002001 /* Check if we run on the same thread than the xreator thread.
2002 * We cannot access to the socket if the thread is different.
2003 */
2004 if (socket->tid != tid)
2005 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2006
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002007 /* check for pattern. */
2008 if (lua_gettop(L) >= 2) {
2009 type = lua_type(L, 2);
2010 if (type == LUA_TSTRING) {
2011 pattern = lua_tostring(L, 2);
2012 if (strcmp(pattern, "*a") == 0)
2013 wanted = HLSR_READ_ALL;
2014 else if (strcmp(pattern, "*l") == 0)
2015 wanted = HLSR_READ_LINE;
2016 else {
2017 wanted = strtoll(pattern, &error, 10);
2018 if (*error != '\0')
2019 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2020 }
2021 }
2022 else if (type == LUA_TNUMBER) {
2023 wanted = lua_tointeger(L, 2);
2024 if (wanted < 0)
2025 WILL_LJMP(luaL_error(L, "Unsupported size."));
2026 }
2027 }
2028
2029 /* Set pattern. */
2030 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002031
2032 /* Check if we would replace the top by itself. */
2033 if (lua_gettop(L) != 2)
2034 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002035
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002036 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037 luaL_buffinit(L, &socket->b);
2038
2039 /* Check prefix. */
2040 if (lua_gettop(L) >= 3) {
2041 if (lua_type(L, 3) != LUA_TSTRING)
2042 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2043 pattern = lua_tolstring(L, 3, &len);
2044 luaL_addlstring(&socket->b, pattern, len);
2045 }
2046
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002047 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048}
2049
2050/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002051 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002052 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002053static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002054{
2055 struct hlua_socket *socket;
2056 struct hlua *hlua = hlua_gethlua(L);
2057 struct appctx *appctx;
2058 size_t buf_len;
2059 const char *buf;
2060 int len;
2061 int send_len;
2062 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002063 struct xref *peer;
2064 struct stream_interface *si;
2065 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066
2067 /* Check if this lua stack is schedulable. */
2068 if (!hlua || !hlua->task)
2069 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2070 "'frontend', 'backend' or 'task'"));
2071
2072 /* Get object */
2073 socket = MAY_LJMP(hlua_checksocket(L, 1));
2074 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002075 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002076
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002077 /* Check if we run on the same thread than the xreator thread.
2078 * We cannot access to the socket if the thread is different.
2079 */
2080 if (socket->tid != tid)
2081 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2082
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002083 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002084 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002085 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002086 lua_pushinteger(L, -1);
2087 return 1;
2088 }
2089 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2090 si = appctx->owner;
2091 s = si_strm(si);
2092
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002094 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002095 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002096 lua_pushinteger(L, -1);
2097 return 1;
2098 }
2099
2100 /* Update the input buffer data. */
2101 buf += sent;
2102 send_len = buf_len - sent;
2103
2104 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002105 if (sent >= buf_len) {
2106 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002107 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002108 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002109
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002110 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002111 * the request buffer if its not required.
2112 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002113 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002114 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002115 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002116 }
2117
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002118 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002119 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002120 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002121 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002122 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002123
2124 /* send data */
2125 if (len < send_len)
2126 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002127 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002128
2129 /* "Not enough space" (-1), "Buffer too little to contain
2130 * the data" (-2) are not expected because the available length
2131 * is tested.
2132 * Other unknown error are also not expected.
2133 */
2134 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002135 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002136 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002137
sada05ed3302018-05-11 11:48:18 -07002138 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002139 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002140 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002141 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002142 return 1;
2143 }
2144
2145 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002146 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002147
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002148 s->req.rex = TICK_ETERNITY;
2149 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002150
2151 /* Update length sent. */
2152 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002153 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154
2155 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002156 if (sent + len >= buf_len) {
2157 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002158 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002159 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002160
2161hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002162 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2163 xref_unlock(&socket->xref, peer);
2164 WILL_LJMP(luaL_error(L, "out of memory"));
2165 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002166 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002167 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168 return 0;
2169}
2170
2171/* This function initiate the send of data. It just check the input
2172 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002173 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002174 * "hlua_socket_write_yield" that can yield.
2175 *
2176 * The Lua function gets between 3 and 4 parameters. The first one is
2177 * the associated object. The second is a string buffer. The third is
2178 * a facultative integer that represents where is the buffer position
2179 * of the start of the data that can send. The first byte is the
2180 * position "1". The default value is "1". The fourth argument is a
2181 * facultative integer that represents where is the buffer position
2182 * of the end of the data that can send. The default is the last byte.
2183 */
2184static int hlua_socket_send(struct lua_State *L)
2185{
2186 int i;
2187 int j;
2188 const char *buf;
2189 size_t buf_len;
2190
2191 /* Check number of arguments. */
2192 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2193 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2194
2195 /* Get the string. */
2196 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2197
2198 /* Get and check j. */
2199 if (lua_gettop(L) == 4) {
2200 j = MAY_LJMP(luaL_checkinteger(L, 4));
2201 if (j < 0)
2202 j = buf_len + j + 1;
2203 if (j > buf_len)
2204 j = buf_len + 1;
2205 lua_pop(L, 1);
2206 }
2207 else
2208 j = buf_len;
2209
2210 /* Get and check i. */
2211 if (lua_gettop(L) == 3) {
2212 i = MAY_LJMP(luaL_checkinteger(L, 3));
2213 if (i < 0)
2214 i = buf_len + i + 1;
2215 if (i > buf_len)
2216 i = buf_len + 1;
2217 lua_pop(L, 1);
2218 } else
2219 i = 1;
2220
2221 /* Check bth i and j. */
2222 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002223 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002224 return 1;
2225 }
2226 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002227 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 return 1;
2229 }
2230 if (i == 0)
2231 i = 1;
2232 if (j == 0)
2233 j = 1;
2234
2235 /* Pop the string. */
2236 lua_pop(L, 1);
2237
2238 /* Update the buffer length. */
2239 buf += i - 1;
2240 buf_len = j - i + 1;
2241 lua_pushlstring(L, buf, buf_len);
2242
2243 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002244 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002245
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002246 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247}
2248
Willy Tarreau22b0a682015-06-17 19:43:49 +02002249#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002250__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2251{
2252 static char buffer[SOCKET_INFO_MAX_LEN];
2253 int ret;
2254 int len;
2255 char *p;
2256
2257 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2258 if (ret <= 0) {
2259 lua_pushnil(L);
2260 return 1;
2261 }
2262
2263 if (ret == AF_UNIX) {
2264 lua_pushstring(L, buffer+1);
2265 return 1;
2266 }
2267 else if (ret == AF_INET6) {
2268 buffer[0] = '[';
2269 len = strlen(buffer);
2270 buffer[len] = ']';
2271 len++;
2272 buffer[len] = ':';
2273 len++;
2274 p = buffer;
2275 }
2276 else if (ret == AF_INET) {
2277 p = buffer + 1;
2278 len = strlen(p);
2279 p[len] = ':';
2280 len++;
2281 }
2282 else {
2283 lua_pushnil(L);
2284 return 1;
2285 }
2286
2287 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2288 lua_pushnil(L);
2289 return 1;
2290 }
2291
2292 lua_pushstring(L, p);
2293 return 1;
2294}
2295
2296/* Returns information about the peer of the connection. */
2297__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2298{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002299 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002300 struct xref *peer;
2301 struct appctx *appctx;
2302 struct stream_interface *si;
2303 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002304 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002305
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306 MAY_LJMP(check_args(L, 1, "getpeername"));
2307
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002308 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002309
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002310 /* Check if we run on the same thread than the xreator thread.
2311 * We cannot access to the socket if the thread is different.
2312 */
2313 if (socket->tid != tid)
2314 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2315
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002316 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002317 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002318 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319 lua_pushnil(L);
2320 return 1;
2321 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002322 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2323 si = appctx->owner;
2324 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002325
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002326 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002327 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002328 lua_pushnil(L);
2329 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002330 }
2331
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002332 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002333 xref_unlock(&socket->xref, peer);
2334 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002335}
2336
2337/* Returns information about my connection side. */
2338static int hlua_socket_getsockname(struct lua_State *L)
2339{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002340 struct hlua_socket *socket;
2341 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 struct appctx *appctx;
2343 struct xref *peer;
2344 struct stream_interface *si;
2345 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002346 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002347
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002348 MAY_LJMP(check_args(L, 1, "getsockname"));
2349
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002350 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002351
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002352 /* Check if we run on the same thread than the xreator thread.
2353 * We cannot access to the socket if the thread is different.
2354 */
2355 if (socket->tid != tid)
2356 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2357
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002358 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002359 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002360 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361 lua_pushnil(L);
2362 return 1;
2363 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002364 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2365 si = appctx->owner;
2366 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002368 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002369 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002370 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 lua_pushnil(L);
2372 return 1;
2373 }
2374
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002375 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002376 xref_unlock(&socket->xref, peer);
2377 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378}
2379
2380/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002381static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382 .obj_type = OBJ_TYPE_APPLET,
2383 .name = "<LUA_TCP>",
2384 .fct = hlua_socket_handler,
2385 .release = hlua_socket_release,
2386};
2387
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002388__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389{
2390 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2391 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002392 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002393 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002394 struct stream_interface *si;
2395 struct stream *s;
2396
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002397 /* Check if we run on the same thread than the xreator thread.
2398 * We cannot access to the socket if the thread is different.
2399 */
2400 if (socket->tid != tid)
2401 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2402
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002403 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002404 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002405 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002406 lua_pushnil(L);
2407 lua_pushstring(L, "Can't connect");
2408 return 2;
2409 }
2410 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2411 si = appctx->owner;
2412 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002413
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002414 /* Check if we run on the same thread than the xreator thread.
2415 * We cannot access to the socket if the thread is different.
2416 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002417 if (socket->tid != tid) {
2418 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002419 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002420 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002421
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002422 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002423 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002424 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002425 lua_pushnil(L);
2426 lua_pushstring(L, "Can't connect");
2427 return 2;
2428 }
2429
Willy Tarreaue09101e2018-10-16 17:37:12 +02002430 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431
2432 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002433 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002434 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002435 lua_pushinteger(L, 1);
2436 return 1;
2437 }
2438
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002439 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2440 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002441 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002442 }
2443 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002444 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445 return 0;
2446}
2447
2448/* This function fail or initite the connection. */
2449__LJMP static int hlua_socket_connect(struct lua_State *L)
2450{
2451 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002452 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002453 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002454 struct hlua *hlua;
2455 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 int low, high;
2457 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002458 struct xref *peer;
2459 struct stream_interface *si;
2460 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002461
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002462 if (lua_gettop(L) < 2)
2463 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002464
2465 /* Get args. */
2466 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002467
2468 /* Check if we run on the same thread than the xreator thread.
2469 * We cannot access to the socket if the thread is different.
2470 */
2471 if (socket->tid != tid)
2472 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2473
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002474 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002475 if (lua_gettop(L) >= 3) {
2476 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002477 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002478
Tim Duesterhus6edab862018-01-06 19:04:45 +01002479 /* Force the ip to end with a colon, to support IPv6 addresses
2480 * that are not enclosed within square brackets.
2481 */
2482 if (port > 0) {
2483 luaL_buffinit(L, &b);
2484 luaL_addstring(&b, ip);
2485 luaL_addchar(&b, ':');
2486 luaL_pushresult(&b);
2487 ip = lua_tolstring(L, lua_gettop(L), NULL);
2488 }
2489 }
2490
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002491 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002492 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002493 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002494 lua_pushnil(L);
2495 return 1;
2496 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002497
2498 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002499 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002500 if (!addr) {
2501 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002502 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002503 }
2504 if (low != high) {
2505 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002506 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002507 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002508
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002509 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002510 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002511 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002512 if (port == -1) {
2513 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002514 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002515 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002516 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2517 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002518 if (port == -1) {
2519 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002520 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002521 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002522 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002523 }
2524 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002525
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002526 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2527 si = appctx->owner;
2528 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002529
2530 if (!sockaddr_alloc(&s->target_addr)) {
2531 xref_unlock(&socket->xref, peer);
2532 WILL_LJMP(luaL_error(L, "connect: internal error"));
2533 }
2534 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002535 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002536
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002537 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002538
2539 /* inform the stream that we want to be notified whenever the
2540 * connection completes.
2541 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002542 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002543 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002544 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002545
Willy Tarreauf31af932020-01-14 09:59:38 +01002546 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002547
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002548 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2549 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002550 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002551 }
2552 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002553
2554 task_wakeup(s->task, TASK_WOKEN_INIT);
2555 /* Return yield waiting for connection. */
2556
Willy Tarreau9635e032018-10-16 17:52:55 +02002557 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002558
2559 return 0;
2560}
2561
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002562#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002563__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2564{
2565 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002566 struct xref *peer;
2567 struct appctx *appctx;
2568 struct stream_interface *si;
2569 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002570
2571 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2572 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002573
2574 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002575 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002576 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002577 lua_pushnil(L);
2578 return 1;
2579 }
2580 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2581 si = appctx->owner;
2582 s = si_strm(si);
2583
2584 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002585 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002586 return MAY_LJMP(hlua_socket_connect(L));
2587}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002588#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002589
2590__LJMP static int hlua_socket_setoption(struct lua_State *L)
2591{
2592 return 0;
2593}
2594
2595__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2596{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002597 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002598 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002599 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002600 struct xref *peer;
2601 struct appctx *appctx;
2602 struct stream_interface *si;
2603 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002604
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605 MAY_LJMP(check_args(L, 2, "settimeout"));
2606
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002607 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002608
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002609 /* convert the timeout to millis */
2610 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002611
Thierry Fournier17a921b2018-03-08 09:59:02 +01002612 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002613 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002614 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2615
Mark Lakes56cc1252018-03-27 09:48:06 +02002616 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002617 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002618
2619 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002620 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002621 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002622
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002623 /* Check if we run on the same thread than the xreator thread.
2624 * We cannot access to the socket if the thread is different.
2625 */
2626 if (socket->tid != tid)
2627 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2628
Mark Lakes56cc1252018-03-27 09:48:06 +02002629 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002630 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002631 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002632 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2633 WILL_LJMP(lua_error(L));
2634 return 0;
2635 }
2636 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2637 si = appctx->owner;
2638 s = si_strm(si);
2639
Cyril Bonté7bb63452018-08-17 23:51:02 +02002640 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002641 s->req.rto = tmout;
2642 s->req.wto = tmout;
2643 s->res.rto = tmout;
2644 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002645 s->req.rex = tick_add_ifset(now_ms, tmout);
2646 s->req.wex = tick_add_ifset(now_ms, tmout);
2647 s->res.rex = tick_add_ifset(now_ms, tmout);
2648 s->res.wex = tick_add_ifset(now_ms, tmout);
2649
2650 s->task->expire = tick_add_ifset(now_ms, tmout);
2651 task_queue(s->task);
2652
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002653 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654
Thierry Fourniere9636f12018-03-08 09:54:32 +01002655 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002656 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002657}
2658
2659__LJMP static int hlua_socket_new(lua_State *L)
2660{
2661 struct hlua_socket *socket;
2662 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002663 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002664 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002665
2666 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002667 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002668 hlua_pusherror(L, "socket: full stack");
2669 goto out_fail_conf;
2670 }
2671
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002672 /* Create the object: obj[0] = userdata. */
2673 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002674 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002675 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002677 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002678
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002679 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002680 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002681 hlua_pusherror(L, "socket: uninitialized pools.");
2682 goto out_fail_conf;
2683 }
2684
Willy Tarreau87b09662015-04-03 00:22:06 +02002685 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002686 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2687 lua_setmetatable(L, -2);
2688
Willy Tarreaud420a972015-04-06 00:39:18 +02002689 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002690 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002691 if (!appctx) {
2692 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002693 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002694 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002695
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002696 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002697 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002698 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2699 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002700
Willy Tarreaud420a972015-04-06 00:39:18 +02002701 /* Now create a session, task and stream for this applet */
2702 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2703 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002704 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002705 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002706 }
2707
Willy Tarreau87787ac2017-08-28 16:22:54 +02002708 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002709 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002711 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002712 }
2713
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002714 /* Initialise cross reference between stream and Lua socket object. */
2715 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002716
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002717 /* Configure "right" stream interface. this "si" is used to connect
2718 * and retrieve data from the server. The connection is initialized
2719 * with the "struct server".
2720 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002721 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002722
2723 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002724 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002725 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002726
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002727 return 1;
2728
Willy Tarreaud420a972015-04-06 00:39:18 +02002729 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002730 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002731 out_fail_sess:
2732 appctx_free(appctx);
2733 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002734 WILL_LJMP(lua_error(L));
2735 return 0;
2736}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002737
2738/*
2739 *
2740 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741 * Class Channel
2742 *
2743 *
2744 */
2745
2746/* Returns the struct hlua_channel join to the class channel in the
2747 * stack entry "ud" or throws an argument error.
2748 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002749__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002751 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752}
2753
Willy Tarreau47860ed2015-03-10 14:07:50 +01002754/* Pushes the channel onto the top of the stack. If the stask does not have a
2755 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002756 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002757static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002758{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002759 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002760 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002761 return 0;
2762
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002763 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002764 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002765 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002766
2767 /* Pop a class sesison metatable and affect it to the userdata. */
2768 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2769 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002770 return 1;
2771}
2772
2773/* Duplicate all the data present in the input channel and put it
2774 * in a string LUA variables. Returns -1 and push a nil value in
2775 * the stack if the channel is closed and all the data are consumed,
2776 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002777 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002779static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780{
2781 char *blk1;
2782 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002783 size_t len1;
2784 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002785 int ret;
2786 luaL_Buffer b;
2787
Willy Tarreau06d80a92017-10-19 14:32:15 +02002788 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002789 if (unlikely(ret == 0))
2790 return 0;
2791
2792 if (unlikely(ret < 0)) {
2793 lua_pushnil(L);
2794 return -1;
2795 }
2796
2797 luaL_buffinit(L, &b);
2798 luaL_addlstring(&b, blk1, len1);
2799 if (unlikely(ret == 2))
2800 luaL_addlstring(&b, blk2, len2);
2801 luaL_pushresult(&b);
2802
2803 if (unlikely(ret == 2))
2804 return len1 + len2;
2805 return len1;
2806}
2807
2808/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2809 * a yield. This function keep the data in the buffer.
2810 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002811__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002812{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002813 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002814
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002815 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2816
Christopher Faulet3f829a42018-12-13 21:56:45 +01002817 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2818 WILL_LJMP(lua_error(L));
2819
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002820 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002821 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002822 return 1;
2823}
2824
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002825/* Check arguments for the function "hlua_channel_dup_yield". */
2826__LJMP static int hlua_channel_dup(lua_State *L)
2827{
2828 MAY_LJMP(check_args(L, 1, "dup"));
2829 MAY_LJMP(hlua_checkchannel(L, 1));
2830 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2831}
2832
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002833/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2834 * a yield. This function consumes the data in the buffer. It returns
2835 * a string containing the data or a nil pointer if no data are available
2836 * and the channel is closed.
2837 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002838__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002839{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002840 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002841 int ret;
2842
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002843 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002844
Christopher Faulet3f829a42018-12-13 21:56:45 +01002845 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2846 WILL_LJMP(lua_error(L));
2847
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002848 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002849 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002850 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002851
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852 if (unlikely(ret == -1))
2853 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002854
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002855 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002856 return 1;
2857}
2858
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002859/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002860__LJMP static int hlua_channel_get(lua_State *L)
2861{
2862 MAY_LJMP(check_args(L, 1, "get"));
2863 MAY_LJMP(hlua_checkchannel(L, 1));
2864 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2865}
2866
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002867/* This functions consumes and returns one line. If the channel is closed,
2868 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002869 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002870 * value.
2871 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002872__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002873{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002874 char *blk1;
2875 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002876 size_t len1;
2877 size_t len2;
2878 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002879 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002880 int ret;
2881 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002882
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002883 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2884
Christopher Faulet3f829a42018-12-13 21:56:45 +01002885 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2886 WILL_LJMP(lua_error(L));
2887
Willy Tarreau06d80a92017-10-19 14:32:15 +02002888 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002889 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002890 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002891
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892 if (ret == -1) {
2893 lua_pushnil(L);
2894 return 1;
2895 }
2896
2897 luaL_buffinit(L, &b);
2898 luaL_addlstring(&b, blk1, len1);
2899 len = len1;
2900 if (unlikely(ret == 2)) {
2901 luaL_addlstring(&b, blk2, len2);
2902 len += len2;
2903 }
2904 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002905 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002906 return 1;
2907}
2908
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002909/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002910__LJMP static int hlua_channel_getline(lua_State *L)
2911{
2912 MAY_LJMP(check_args(L, 1, "getline"));
2913 MAY_LJMP(hlua_checkchannel(L, 1));
2914 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2915}
2916
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002917/* This function takes a string as input, and append it at the
2918 * input side of channel. If the data is too big, but a space
2919 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002920 * yields. If the data is bigger than the buffer, or if the
2921 * channel is closed, it returns -1. Otherwise, it returns the
2922 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002923 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002924__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002926 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 size_t len;
2928 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2929 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2930 int ret;
2931 int max;
2932
Christopher Faulet3f829a42018-12-13 21:56:45 +01002933 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2934 WILL_LJMP(lua_error(L));
2935
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002936 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002937 * the request buffer if its not required.
2938 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002939 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002940 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002941 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002942 }
2943
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002944 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945 if (max > len - l)
2946 max = len - l;
2947
Willy Tarreau06d80a92017-10-19 14:32:15 +02002948 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949 if (ret == -2 || ret == -3) {
2950 lua_pushinteger(L, -1);
2951 return 1;
2952 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002953 if (ret == -1) {
2954 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002955 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002956 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002957 l += ret;
2958 lua_pop(L, 1);
2959 lua_pushinteger(L, l);
2960
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002961 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002962 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002963 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002965 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002966 */
2967 return 1;
2968 }
2969 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002970 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 return 1;
2972}
2973
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002974/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2975 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002976 * buffer size is too little for the data.
2977 */
2978__LJMP static int hlua_channel_append(lua_State *L)
2979{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002980 size_t len;
2981
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002982 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002983 MAY_LJMP(hlua_checkchannel(L, 1));
2984 MAY_LJMP(luaL_checklstring(L, 2, &len));
2985 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986 lua_pushinteger(L, 0);
2987
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002988 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002989}
2990
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002991/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002992 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002993 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 * or -1 if the channel is closed or if the buffer size is too
2995 * little for the data.
2996 */
2997__LJMP static int hlua_channel_set(lua_State *L)
2998{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002999 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003000
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003002 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003003 lua_pushinteger(L, 0);
3004
Christopher Faulet3f829a42018-12-13 21:56:45 +01003005 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3006 WILL_LJMP(lua_error(L));
3007
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003008 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003010 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011}
3012
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003013/* Append data in the output side of the buffer. This data is immediately
3014 * sent. The function returns the amount of data written. If the buffer
3015 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016 * if the channel is closed.
3017 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003018__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003019{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003020 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 size_t len;
3022 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3023 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3024 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003025 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026
Christopher Faulet3f829a42018-12-13 21:56:45 +01003027 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3028 WILL_LJMP(lua_error(L));
3029
Willy Tarreau47860ed2015-03-10 14:07:50 +01003030 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 lua_pushinteger(L, -1);
3032 return 1;
3033 }
3034
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003035 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003036 * the request buffer if its not required.
3037 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003038 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003039 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003040 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003041 }
3042
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003043 /* The written data will be immediately sent, so we can check
3044 * the available space without taking in account the reserve.
3045 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003046 * data, because the buffer will be flushed.
3047 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003048 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003049
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003050 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003051 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003052 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003053 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003054 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003055 return 1;
3056
3057 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003058 if (max > len - l)
3059 max = len - l;
3060
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003061 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003062 * detects a non contiguous buffer and realign it.
3063 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003064 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003065 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003066
3067 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003068 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003069
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003070 /* buffer replace considers that the input part is filled.
3071 * so, I must forward these new data in the output part.
3072 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003073 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003074
3075 l += max;
3076 lua_pop(L, 1);
3077 lua_pushinteger(L, l);
3078
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003079 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003080 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003081 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003082 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003083 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003084 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003085 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003086
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003087 if (l < len) {
3088 /* If we are waiting for space in the response buffer, we
3089 * must set the flag WAKERESWR. This flag required the task
3090 * wake up if any activity is detected on the response buffer.
3091 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003092 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003093 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003094 else
3095 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003096 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003097 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003098
3099 return 1;
3100}
3101
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003102/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 * yield the LUA process, and resume it without checking the
3104 * input arguments.
3105 */
3106__LJMP static int hlua_channel_send(lua_State *L)
3107{
3108 MAY_LJMP(check_args(L, 2, "send"));
3109 lua_pushinteger(L, 0);
3110
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003111 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003112}
3113
3114/* This function forward and amount of butes. The data pass from
3115 * the input side of the buffer to the output side, and can be
3116 * forwarded. This function never fails.
3117 *
3118 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003119 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003120 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003121__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003122{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003123 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003124 int len;
3125 int l;
3126 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003127 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003128
3129 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003130
3131 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3132 WILL_LJMP(lua_error(L));
3133
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003134 len = MAY_LJMP(luaL_checkinteger(L, 2));
3135 l = MAY_LJMP(luaL_checkinteger(L, -1));
3136
3137 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003138 if (max > ci_data(chn))
3139 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003140 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003141 l += max;
3142
3143 lua_pop(L, 1);
3144 lua_pushinteger(L, l);
3145
3146 /* Check if it miss bytes to forward. */
3147 if (l < len) {
3148 /* The the input channel or the output channel are closed, we
3149 * must return the amount of data forwarded.
3150 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003151 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003152 return 1;
3153
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003154 /* If we are waiting for space data in the response buffer, we
3155 * must set the flag WAKERESWR. This flag required the task
3156 * wake up if any activity is detected on the response buffer.
3157 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003158 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003159 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003160 else
3161 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003162
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003163 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003164 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003165 }
3166
3167 return 1;
3168}
3169
3170/* Just check the input and prepare the stack for the previous
3171 * function "hlua_channel_forward_yield"
3172 */
3173__LJMP static int hlua_channel_forward(lua_State *L)
3174{
3175 MAY_LJMP(check_args(L, 2, "forward"));
3176 MAY_LJMP(hlua_checkchannel(L, 1));
3177 MAY_LJMP(luaL_checkinteger(L, 2));
3178
3179 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003180 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003181}
3182
3183/* Just returns the number of bytes available in the input
3184 * side of the buffer. This function never fails.
3185 */
3186__LJMP static int hlua_channel_get_in_len(lua_State *L)
3187{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003188 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003189
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003190 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003191 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003192 if (IS_HTX_STRM(chn_strm(chn))) {
3193 struct htx *htx = htxbuf(&chn->buf);
3194 lua_pushinteger(L, htx->data - co_data(chn));
3195 }
3196 else
3197 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003198 return 1;
3199}
3200
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003201/* Returns true if the channel is full. */
3202__LJMP static int hlua_channel_is_full(lua_State *L)
3203{
3204 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003205
3206 MAY_LJMP(check_args(L, 1, "is_full"));
3207 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003208 /* ignore the reserve, we are not on a producer side (ie in an
3209 * applet).
3210 */
3211 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003212 return 1;
3213}
3214
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003215/* Returns true if the channel is the response channel. */
3216__LJMP static int hlua_channel_is_resp(lua_State *L)
3217{
3218 struct channel *chn;
3219
3220 MAY_LJMP(check_args(L, 1, "is_resp"));
3221 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3222
3223 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3224 return 1;
3225}
3226
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003227/* Just returns the number of bytes available in the output
3228 * side of the buffer. This function never fails.
3229 */
3230__LJMP static int hlua_channel_get_out_len(lua_State *L)
3231{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003232 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003233
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003234 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003235 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003236 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003237 return 1;
3238}
3239
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003240/*
3241 *
3242 *
3243 * Class Fetches
3244 *
3245 *
3246 */
3247
3248/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003249 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003250 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003251__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003252{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003253 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003254}
3255
3256/* This function creates and push in the stack a fetch object according
3257 * with a current TXN.
3258 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003259static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003260{
Willy Tarreau7073c472015-04-06 11:15:40 +02003261 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003262
3263 /* Check stack size. */
3264 if (!lua_checkstack(L, 3))
3265 return 0;
3266
3267 /* Create the object: obj[0] = userdata.
3268 * Note that the base of the Fetches object is the
3269 * transaction object.
3270 */
3271 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003272 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003273 lua_rawseti(L, -2, 0);
3274
Willy Tarreau7073c472015-04-06 11:15:40 +02003275 hsmp->s = txn->s;
3276 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003277 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003278 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003279
3280 /* Pop a class sesison metatable and affect it to the userdata. */
3281 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3282 lua_setmetatable(L, -2);
3283
3284 return 1;
3285}
3286
3287/* This function is an LUA binding. It is called with each sample-fetch.
3288 * It uses closure argument to store the associated sample-fetch. It
3289 * returns only one argument or throws an error. An error is thrown
3290 * only if an error is encountered during the argument parsing. If
3291 * the "sample-fetch" function fails, nil is returned.
3292 */
3293__LJMP static int hlua_run_sample_fetch(lua_State *L)
3294{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003295 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003296 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003297 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003298 int i;
3299 struct sample smp;
3300
3301 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003302 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003303
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003304 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003305 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003306
Thierry FOURNIERca988662015-12-20 18:43:03 +01003307 /* Check execution authorization. */
3308 if (f->use & SMP_USE_HTTP_ANY &&
3309 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3310 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3311 "is not available in Lua services", f->kw);
3312 WILL_LJMP(lua_error(L));
3313 }
3314
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003315 /* Get extra arguments. */
3316 for (i = 0; i < lua_gettop(L) - 1; i++) {
3317 if (i >= ARGM_NBARGS)
3318 break;
3319 hlua_lua2arg(L, i + 2, &args[i]);
3320 }
3321 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003322 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003323
3324 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003325 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003326
3327 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003328 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003329 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003330 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003331 }
3332
3333 /* Initialise the sample. */
3334 memset(&smp, 0, sizeof(smp));
3335
3336 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003337 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003338 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003339 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003340 lua_pushstring(L, "");
3341 else
3342 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003343 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003344 }
3345
3346 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003347 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003348 hlua_smp2lua_str(L, &smp);
3349 else
3350 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003351
3352 end:
3353 for (i = 0; args[i].type != ARGT_STOP; i++) {
3354 if (args[i].type == ARGT_STR)
3355 chunk_destroy(&args[i].data.str);
3356 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003357 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003358
3359 error:
3360 for (i = 0; args[i].type != ARGT_STOP; i++) {
3361 if (args[i].type == ARGT_STR)
3362 chunk_destroy(&args[i].data.str);
3363 }
3364 WILL_LJMP(lua_error(L));
3365 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003366}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003367
3368/*
3369 *
3370 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003371 * Class Converters
3372 *
3373 *
3374 */
3375
3376/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003377 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003378 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003379__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003380{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003381 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003382}
3383
3384/* This function creates and push in the stack a Converters object
3385 * according with a current TXN.
3386 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003387static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003388{
Willy Tarreau7073c472015-04-06 11:15:40 +02003389 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003390
3391 /* Check stack size. */
3392 if (!lua_checkstack(L, 3))
3393 return 0;
3394
3395 /* Create the object: obj[0] = userdata.
3396 * Note that the base of the Converters object is the
3397 * same than the TXN object.
3398 */
3399 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003400 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003401 lua_rawseti(L, -2, 0);
3402
Willy Tarreau7073c472015-04-06 11:15:40 +02003403 hsmp->s = txn->s;
3404 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003405 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003406 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003407
Willy Tarreau87b09662015-04-03 00:22:06 +02003408 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003409 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3410 lua_setmetatable(L, -2);
3411
3412 return 1;
3413}
3414
3415/* This function is an LUA binding. It is called with each converter.
3416 * It uses closure argument to store the associated converter. It
3417 * returns only one argument or throws an error. An error is thrown
3418 * only if an error is encountered during the argument parsing. If
3419 * the converter function function fails, nil is returned.
3420 */
3421__LJMP static int hlua_run_sample_conv(lua_State *L)
3422{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003423 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003424 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003425 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003426 int i;
3427 struct sample smp;
3428
3429 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003430 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003431
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003432 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003433 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003434
3435 /* Get extra arguments. */
3436 for (i = 0; i < lua_gettop(L) - 2; i++) {
3437 if (i >= ARGM_NBARGS)
3438 break;
3439 hlua_lua2arg(L, i + 3, &args[i]);
3440 }
3441 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003442 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003443
3444 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003445 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003446
3447 /* Run the special args checker. */
3448 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3449 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003450 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003451 }
3452
3453 /* Initialise the sample. */
3454 if (!hlua_lua2smp(L, 2, &smp)) {
3455 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003456 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003457 }
3458
Willy Tarreau1777ea62016-03-10 16:15:46 +01003459 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3460
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003461 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003462 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003463 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003464 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003465 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003466 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003467 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3468 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003469 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003470 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003471 }
3472
3473 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003474 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003475 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003476 lua_pushstring(L, "");
3477 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003478 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003479 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003480 }
3481
3482 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003483 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003484 hlua_smp2lua_str(L, &smp);
3485 else
3486 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003487 end:
3488 for (i = 0; args[i].type != ARGT_STOP; i++) {
3489 if (args[i].type == ARGT_STR)
3490 chunk_destroy(&args[i].data.str);
3491 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003492 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003493
3494 error:
3495 for (i = 0; args[i].type != ARGT_STOP; i++) {
3496 if (args[i].type == ARGT_STR)
3497 chunk_destroy(&args[i].data.str);
3498 }
3499 WILL_LJMP(lua_error(L));
3500 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003501}
3502
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003503/*
3504 *
3505 *
3506 * Class AppletTCP
3507 *
3508 *
3509 */
3510
3511/* Returns a struct hlua_txn if the stack entry "ud" is
3512 * a class stream, otherwise it throws an error.
3513 */
3514__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3515{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003516 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003517}
3518
3519/* This function creates and push in the stack an Applet object
3520 * according with a current TXN.
3521 */
3522static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3523{
3524 struct hlua_appctx *appctx;
3525 struct stream_interface *si = ctx->owner;
3526 struct stream *s = si_strm(si);
3527 struct proxy *p = s->be;
3528
3529 /* Check stack size. */
3530 if (!lua_checkstack(L, 3))
3531 return 0;
3532
3533 /* Create the object: obj[0] = userdata.
3534 * Note that the base of the Converters object is the
3535 * same than the TXN object.
3536 */
3537 lua_newtable(L);
3538 appctx = lua_newuserdata(L, sizeof(*appctx));
3539 lua_rawseti(L, -2, 0);
3540 appctx->appctx = ctx;
3541 appctx->htxn.s = s;
3542 appctx->htxn.p = p;
3543
3544 /* Create the "f" field that contains a list of fetches. */
3545 lua_pushstring(L, "f");
3546 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3547 return 0;
3548 lua_settable(L, -3);
3549
3550 /* Create the "sf" field that contains a list of stringsafe fetches. */
3551 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003552 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003553 return 0;
3554 lua_settable(L, -3);
3555
3556 /* Create the "c" field that contains a list of converters. */
3557 lua_pushstring(L, "c");
3558 if (!hlua_converters_new(L, &appctx->htxn, 0))
3559 return 0;
3560 lua_settable(L, -3);
3561
3562 /* Create the "sc" field that contains a list of stringsafe converters. */
3563 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003564 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003565 return 0;
3566 lua_settable(L, -3);
3567
3568 /* Pop a class stream metatable and affect it to the table. */
3569 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3570 lua_setmetatable(L, -2);
3571
3572 return 1;
3573}
3574
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003575__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3576{
3577 struct hlua_appctx *appctx;
3578 struct stream *s;
3579 const char *name;
3580 size_t len;
3581 struct sample smp;
3582
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003583 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3584 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003585
3586 /* It is useles to retrieve the stream, but this function
3587 * runs only in a stream context.
3588 */
3589 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3590 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3591 s = appctx->htxn.s;
3592
3593 /* Converts the third argument in a sample. */
3594 hlua_lua2smp(L, 3, &smp);
3595
3596 /* Store the sample in a variable. */
3597 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003598
3599 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3600 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3601 else
3602 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3603
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003604 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003605}
3606
3607__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3608{
3609 struct hlua_appctx *appctx;
3610 struct stream *s;
3611 const char *name;
3612 size_t len;
3613 struct sample smp;
3614
3615 MAY_LJMP(check_args(L, 2, "unset_var"));
3616
3617 /* It is useles to retrieve the stream, but this function
3618 * runs only in a stream context.
3619 */
3620 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3621 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3622 s = appctx->htxn.s;
3623
3624 /* Unset the variable. */
3625 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003626 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3627 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003628}
3629
3630__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3631{
3632 struct hlua_appctx *appctx;
3633 struct stream *s;
3634 const char *name;
3635 size_t len;
3636 struct sample smp;
3637
3638 MAY_LJMP(check_args(L, 2, "get_var"));
3639
3640 /* It is useles to retrieve the stream, but this function
3641 * runs only in a stream context.
3642 */
3643 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3644 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3645 s = appctx->htxn.s;
3646
3647 smp_set_owner(&smp, s->be, s->sess, s, 0);
3648 if (!vars_get_by_name(name, len, &smp)) {
3649 lua_pushnil(L);
3650 return 1;
3651 }
3652
3653 return hlua_smp2lua(L, &smp);
3654}
3655
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003656__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3657{
3658 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3659 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003660 struct hlua *hlua;
3661
3662 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003663 if (!s->hlua)
3664 return 0;
3665 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003666
3667 MAY_LJMP(check_args(L, 2, "set_priv"));
3668
3669 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003670 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003671
3672 /* Get and store new value. */
3673 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3674 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3675
3676 return 0;
3677}
3678
3679__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3680{
3681 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3682 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003683 struct hlua *hlua;
3684
3685 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003686 if (!s->hlua) {
3687 lua_pushnil(L);
3688 return 1;
3689 }
3690 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003691
3692 /* Push configuration index in the stack. */
3693 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3694
3695 return 1;
3696}
3697
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003698/* If expected data not yet available, it returns a yield. This function
3699 * consumes the data in the buffer. It returns a string containing the
3700 * data. This string can be empty.
3701 */
3702__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3703{
3704 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3705 struct stream_interface *si = appctx->appctx->owner;
3706 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003707 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003708 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003709 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003710 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003711
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003712 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003713 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003714
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003715 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003717 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003718 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003719 }
3720
3721 /* End of data: commit the total strings and return. */
3722 if (ret < 0) {
3723 luaL_pushresult(&appctx->b);
3724 return 1;
3725 }
3726
3727 /* Ensure that the block 2 length is usable. */
3728 if (ret == 1)
3729 len2 = 0;
3730
3731 /* dont check the max length read and dont check. */
3732 luaL_addlstring(&appctx->b, blk1, len1);
3733 luaL_addlstring(&appctx->b, blk2, len2);
3734
3735 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003736 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003737 luaL_pushresult(&appctx->b);
3738 return 1;
3739}
3740
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003741/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003742__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3743{
3744 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3745
3746 /* Initialise the string catenation. */
3747 luaL_buffinit(L, &appctx->b);
3748
3749 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3750}
3751
3752/* If expected data not yet available, it returns a yield. This function
3753 * consumes the data in the buffer. It returns a string containing the
3754 * data. This string can be empty.
3755 */
3756__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3757{
3758 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3759 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003760 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003761 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003762 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003763 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003764 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003765 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003766
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003767 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003768 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003769
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003770 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003772 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003773 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003774 }
3775
3776 /* End of data: commit the total strings and return. */
3777 if (ret < 0) {
3778 luaL_pushresult(&appctx->b);
3779 return 1;
3780 }
3781
3782 /* Ensure that the block 2 length is usable. */
3783 if (ret == 1)
3784 len2 = 0;
3785
3786 if (len == -1) {
3787
3788 /* If len == -1, catenate all the data avalaile and
3789 * yield because we want to get all the data until
3790 * the end of data stream.
3791 */
3792 luaL_addlstring(&appctx->b, blk1, len1);
3793 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003794 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003795 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003796 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003797
3798 } else {
3799
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003800 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003801 if (len1 > len)
3802 len1 = len;
3803 luaL_addlstring(&appctx->b, blk1, len1);
3804 len -= len1;
3805
3806 /* Copy the second block. */
3807 if (len2 > len)
3808 len2 = len;
3809 luaL_addlstring(&appctx->b, blk2, len2);
3810 len -= len2;
3811
3812 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003813 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003814
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003815 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003816 if (len > 0) {
3817 lua_pushinteger(L, len);
3818 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003819 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003820 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003821 }
3822
3823 /* return the result. */
3824 luaL_pushresult(&appctx->b);
3825 return 1;
3826 }
3827
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003828 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003829 hlua_pusherror(L, "Lua: internal error");
3830 WILL_LJMP(lua_error(L));
3831 return 0;
3832}
3833
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003834/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003835__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3836{
3837 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3838 int len = -1;
3839
3840 if (lua_gettop(L) > 2)
3841 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3842 if (lua_gettop(L) >= 2) {
3843 len = MAY_LJMP(luaL_checkinteger(L, 2));
3844 lua_pop(L, 1);
3845 }
3846
3847 /* Confirm or set the required length */
3848 lua_pushinteger(L, len);
3849
3850 /* Initialise the string catenation. */
3851 luaL_buffinit(L, &appctx->b);
3852
3853 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3854}
3855
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003856/* Append data in the output side of the buffer. This data is immediately
3857 * sent. The function returns the amount of data written. If the buffer
3858 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003859 * if the channel is closed.
3860 */
3861__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3862{
3863 size_t len;
3864 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3865 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3866 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3867 struct stream_interface *si = appctx->appctx->owner;
3868 struct channel *chn = si_ic(si);
3869 int max;
3870
3871 /* Get the max amount of data which can write as input in the channel. */
3872 max = channel_recv_max(chn);
3873 if (max > (len - l))
3874 max = len - l;
3875
3876 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003877 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003878
3879 /* update counters. */
3880 l += max;
3881 lua_pop(L, 1);
3882 lua_pushinteger(L, l);
3883
3884 /* If some data is not send, declares the situation to the
3885 * applet, and returns a yield.
3886 */
3887 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003888 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003889 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003890 }
3891
3892 return 1;
3893}
3894
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003895/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003896 * yield the LUA process, and resume it without checking the
3897 * input arguments.
3898 */
3899__LJMP static int hlua_applet_tcp_send(lua_State *L)
3900{
3901 MAY_LJMP(check_args(L, 2, "send"));
3902 lua_pushinteger(L, 0);
3903
3904 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3905}
3906
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003907/*
3908 *
3909 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003910 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003911 *
3912 *
3913 */
3914
3915/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003916 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003917 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003918__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003919{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003920 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003921}
3922
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003924 * according with a current TXN.
3925 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003926static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003927{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003928 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003929 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003930 struct stream_interface *si = ctx->owner;
3931 struct stream *s = si_strm(si);
3932 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003933 struct htx *htx;
3934 struct htx_blk *blk;
3935 struct htx_sl *sl;
3936 struct ist path;
3937 unsigned long long len = 0;
3938 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003939
3940 /* Check stack size. */
3941 if (!lua_checkstack(L, 3))
3942 return 0;
3943
3944 /* Create the object: obj[0] = userdata.
3945 * Note that the base of the Converters object is the
3946 * same than the TXN object.
3947 */
3948 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003949 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003950 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003951 appctx->appctx = ctx;
3952 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003953 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003954 appctx->htxn.s = s;
3955 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003956
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003957 /* Create the "f" field that contains a list of fetches. */
3958 lua_pushstring(L, "f");
3959 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3960 return 0;
3961 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003962
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003963 /* Create the "sf" field that contains a list of stringsafe fetches. */
3964 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003965 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003966 return 0;
3967 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003968
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003969 /* Create the "c" field that contains a list of converters. */
3970 lua_pushstring(L, "c");
3971 if (!hlua_converters_new(L, &appctx->htxn, 0))
3972 return 0;
3973 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003974
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003975 /* Create the "sc" field that contains a list of stringsafe converters. */
3976 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003977 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003978 return 0;
3979 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003980
Christopher Fauleta2097962019-07-15 16:25:33 +02003981 htx = htxbuf(&s->req.buf);
3982 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003983 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003984 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003985
Christopher Fauleta2097962019-07-15 16:25:33 +02003986 /* Stores the request method. */
3987 lua_pushstring(L, "method");
3988 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3989 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003990
Christopher Fauleta2097962019-07-15 16:25:33 +02003991 /* Stores the http version. */
3992 lua_pushstring(L, "version");
3993 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3994 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003995
Christopher Fauleta2097962019-07-15 16:25:33 +02003996 /* creates an array of headers. hlua_http_get_headers() crates and push
3997 * the array on the top of the stack.
3998 */
3999 lua_pushstring(L, "headers");
4000 htxn.s = s;
4001 htxn.p = px;
4002 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004003 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004004 return 0;
4005 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004006
Christopher Fauleta2097962019-07-15 16:25:33 +02004007 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004008 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004009 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004010
Christopher Fauleta2097962019-07-15 16:25:33 +02004011 p = path.ptr;
4012 end = path.ptr + path.len;
4013 q = p;
4014 while (q < end && *q != '?')
4015 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004016
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004017 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004018 lua_pushstring(L, "path");
4019 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004020 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004021
Christopher Fauleta2097962019-07-15 16:25:33 +02004022 /* Stores the query string. */
4023 lua_pushstring(L, "qs");
4024 if (*q == '?')
4025 q++;
4026 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004027 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004028 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004029
Christopher Fauleta2097962019-07-15 16:25:33 +02004030 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4031 struct htx_blk *blk = htx_get_blk(htx, pos);
4032 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004033
Christopher Fauleta2097962019-07-15 16:25:33 +02004034 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
4035 break;
4036 if (type == HTX_BLK_DATA)
4037 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004038 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004039 if (htx->extra != ULLONG_MAX)
4040 len += htx->extra;
4041
4042 /* Stores the request path. */
4043 lua_pushstring(L, "length");
4044 lua_pushinteger(L, len);
4045 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004046
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004047 /* Create an empty array of HTTP request headers. */
4048 lua_pushstring(L, "response");
4049 lua_newtable(L);
4050 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004051
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004052 /* Pop a class stream metatable and affect it to the table. */
4053 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4054 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004055
4056 return 1;
4057}
4058
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004059__LJMP static int hlua_applet_http_set_var(lua_State *L)
4060{
4061 struct hlua_appctx *appctx;
4062 struct stream *s;
4063 const char *name;
4064 size_t len;
4065 struct sample smp;
4066
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004067 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4068 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004069
4070 /* It is useles to retrieve the stream, but this function
4071 * runs only in a stream context.
4072 */
4073 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4074 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4075 s = appctx->htxn.s;
4076
4077 /* Converts the third argument in a sample. */
4078 hlua_lua2smp(L, 3, &smp);
4079
4080 /* Store the sample in a variable. */
4081 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004082
4083 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4084 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4085 else
4086 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4087
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004088 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004089}
4090
4091__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4092{
4093 struct hlua_appctx *appctx;
4094 struct stream *s;
4095 const char *name;
4096 size_t len;
4097 struct sample smp;
4098
4099 MAY_LJMP(check_args(L, 2, "unset_var"));
4100
4101 /* It is useles to retrieve the stream, but this function
4102 * runs only in a stream context.
4103 */
4104 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4105 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4106 s = appctx->htxn.s;
4107
4108 /* Unset the variable. */
4109 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004110 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4111 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004112}
4113
4114__LJMP static int hlua_applet_http_get_var(lua_State *L)
4115{
4116 struct hlua_appctx *appctx;
4117 struct stream *s;
4118 const char *name;
4119 size_t len;
4120 struct sample smp;
4121
4122 MAY_LJMP(check_args(L, 2, "get_var"));
4123
4124 /* It is useles to retrieve the stream, but this function
4125 * runs only in a stream context.
4126 */
4127 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4128 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4129 s = appctx->htxn.s;
4130
4131 smp_set_owner(&smp, s->be, s->sess, s, 0);
4132 if (!vars_get_by_name(name, len, &smp)) {
4133 lua_pushnil(L);
4134 return 1;
4135 }
4136
4137 return hlua_smp2lua(L, &smp);
4138}
4139
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004140__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4141{
4142 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4143 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004144 struct hlua *hlua;
4145
4146 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004147 if (!s->hlua)
4148 return 0;
4149 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004150
4151 MAY_LJMP(check_args(L, 2, "set_priv"));
4152
4153 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004154 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004155
4156 /* Get and store new value. */
4157 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4158 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4159
4160 return 0;
4161}
4162
4163__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4164{
4165 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4166 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004167 struct hlua *hlua;
4168
4169 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004170 if (!s->hlua) {
4171 lua_pushnil(L);
4172 return 1;
4173 }
4174 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004175
4176 /* Push configuration index in the stack. */
4177 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4178
4179 return 1;
4180}
4181
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004182/* If expected data not yet available, it returns a yield. This function
4183 * consumes the data in the buffer. It returns a string containing the
4184 * data. This string can be empty.
4185 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004186__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004187{
4188 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4189 struct stream_interface *si = appctx->appctx->owner;
4190 struct channel *req = si_oc(si);
4191 struct htx *htx;
4192 struct htx_blk *blk;
4193 size_t count;
4194 int stop = 0;
4195
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004196 htx = htx_from_buf(&req->buf);
4197 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004198 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004199
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004200 while (count && !stop && blk) {
4201 enum htx_blk_type type = htx_get_blk_type(blk);
4202 uint32_t sz = htx_get_blksz(blk);
4203 struct ist v;
4204 uint32_t vlen;
4205 char *nl;
4206
Christopher Faulete461e342018-12-18 16:43:35 +01004207 if (type == HTX_BLK_EOM) {
4208 stop = 1;
4209 break;
4210 }
4211
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004212 vlen = sz;
4213 if (vlen > count) {
4214 if (type != HTX_BLK_DATA)
4215 break;
4216 vlen = count;
4217 }
4218
4219 switch (type) {
4220 case HTX_BLK_UNUSED:
4221 break;
4222
4223 case HTX_BLK_DATA:
4224 v = htx_get_blk_value(htx, blk);
4225 v.len = vlen;
4226 nl = istchr(v, '\n');
4227 if (nl != NULL) {
4228 stop = 1;
4229 vlen = nl - v.ptr + 1;
4230 }
4231 luaL_addlstring(&appctx->b, v.ptr, vlen);
4232 break;
4233
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004234 case HTX_BLK_TLR:
4235 case HTX_BLK_EOM:
4236 stop = 1;
4237 break;
4238
4239 default:
4240 break;
4241 }
4242
4243 co_set_data(req, co_data(req) - vlen);
4244 count -= vlen;
4245 if (sz == vlen)
4246 blk = htx_remove_blk(htx, blk);
4247 else {
4248 htx_cut_data_blk(htx, blk, vlen);
4249 break;
4250 }
4251 }
4252
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004253 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004254 if (!stop) {
4255 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004256 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004257 }
4258
4259 /* return the result. */
4260 luaL_pushresult(&appctx->b);
4261 return 1;
4262}
4263
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004264
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004265/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004266__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004267{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004268 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004269
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004270 /* Initialise the string catenation. */
4271 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004272
Christopher Fauleta2097962019-07-15 16:25:33 +02004273 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004274}
4275
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004276/* If expected data not yet available, it returns a yield. This function
4277 * consumes the data in the buffer. It returns a string containing the
4278 * data. This string can be empty.
4279 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004280__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004281{
4282 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4283 struct stream_interface *si = appctx->appctx->owner;
4284 struct channel *req = si_oc(si);
4285 struct htx *htx;
4286 struct htx_blk *blk;
4287 size_t count;
4288 int len;
4289
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004290 htx = htx_from_buf(&req->buf);
4291 len = MAY_LJMP(luaL_checkinteger(L, 2));
4292 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004293 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004294 while (count && len && blk) {
4295 enum htx_blk_type type = htx_get_blk_type(blk);
4296 uint32_t sz = htx_get_blksz(blk);
4297 struct ist v;
4298 uint32_t vlen;
4299
Christopher Faulete461e342018-12-18 16:43:35 +01004300 if (type == HTX_BLK_EOM) {
4301 len = 0;
4302 break;
4303 }
4304
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004305 vlen = sz;
4306 if (len > 0 && vlen > len)
4307 vlen = len;
4308 if (vlen > count) {
4309 if (type != HTX_BLK_DATA)
4310 break;
4311 vlen = count;
4312 }
4313
4314 switch (type) {
4315 case HTX_BLK_UNUSED:
4316 break;
4317
4318 case HTX_BLK_DATA:
4319 v = htx_get_blk_value(htx, blk);
4320 luaL_addlstring(&appctx->b, v.ptr, vlen);
4321 break;
4322
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004323 case HTX_BLK_TLR:
4324 case HTX_BLK_EOM:
4325 len = 0;
4326 break;
4327
4328 default:
4329 break;
4330 }
4331
4332 co_set_data(req, co_data(req) - vlen);
4333 count -= vlen;
4334 if (len > 0)
4335 len -= vlen;
4336 if (sz == vlen)
4337 blk = htx_remove_blk(htx, blk);
4338 else {
4339 htx_cut_data_blk(htx, blk, vlen);
4340 break;
4341 }
4342 }
4343
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004344 htx_to_buf(htx, &req->buf);
4345
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004346 /* If we are no other data available, yield waiting for new data. */
4347 if (len) {
4348 if (len > 0) {
4349 lua_pushinteger(L, len);
4350 lua_replace(L, 2);
4351 }
4352 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004353 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004354 }
4355
4356 /* return the result. */
4357 luaL_pushresult(&appctx->b);
4358 return 1;
4359}
4360
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004361/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004362__LJMP static int hlua_applet_http_recv(lua_State *L)
4363{
4364 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4365 int len = -1;
4366
4367 /* Check arguments. */
4368 if (lua_gettop(L) > 2)
4369 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4370 if (lua_gettop(L) >= 2) {
4371 len = MAY_LJMP(luaL_checkinteger(L, 2));
4372 lua_pop(L, 1);
4373 }
4374
Christopher Fauleta2097962019-07-15 16:25:33 +02004375 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004376
Christopher Fauleta2097962019-07-15 16:25:33 +02004377 /* Initialise the string catenation. */
4378 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004379
Christopher Fauleta2097962019-07-15 16:25:33 +02004380 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004381}
4382
4383/* Append data in the output side of the buffer. This data is immediately
4384 * sent. The function returns the amount of data written. If the buffer
4385 * cannot contain the data, the function yields. The function returns -1
4386 * if the channel is closed.
4387 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004388__LJMP static int hlua_applet_http_send_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 *res = si_ic(si);
4393 struct htx *htx = htx_from_buf(&res->buf);
4394 const char *data;
4395 size_t len;
4396 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4397 int max;
4398
Christopher Faulet9060fc02019-07-03 11:39:30 +02004399 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004400 if (!max)
4401 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004402
4403 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4404
4405 /* Get the max amount of data which can write as input in the channel. */
4406 if (max > (len - l))
4407 max = len - l;
4408
4409 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004410 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004411 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004412
4413 /* update counters. */
4414 l += max;
4415 lua_pop(L, 1);
4416 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004417
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004418 /* If some data is not send, declares the situation to the
4419 * applet, and returns a yield.
4420 */
4421 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004422 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004423 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004424 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004425 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004426 }
4427
Christopher Fauleta2097962019-07-15 16:25:33 +02004428 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004429 return 1;
4430}
4431
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004432/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004433 * yield the LUA process, and resume it without checking the
4434 * input arguments.
4435 */
4436__LJMP static int hlua_applet_http_send(lua_State *L)
4437{
4438 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004439
4440 /* We want to send some data. Headers must be sent. */
4441 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4442 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4443 WILL_LJMP(lua_error(L));
4444 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004445
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004446 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004447 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004448
Christopher Fauleta2097962019-07-15 16:25:33 +02004449 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004450}
4451
4452__LJMP static int hlua_applet_http_addheader(lua_State *L)
4453{
4454 const char *name;
4455 int ret;
4456
4457 MAY_LJMP(hlua_checkapplet_http(L, 1));
4458 name = MAY_LJMP(luaL_checkstring(L, 2));
4459 MAY_LJMP(luaL_checkstring(L, 3));
4460
4461 /* Push in the stack the "response" entry. */
4462 ret = lua_getfield(L, 1, "response");
4463 if (ret != LUA_TTABLE) {
4464 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4465 "is expected as an array. %s found", lua_typename(L, ret));
4466 WILL_LJMP(lua_error(L));
4467 }
4468
4469 /* check if the header is already registered if it is not
4470 * the case, register it.
4471 */
4472 ret = lua_getfield(L, -1, name);
4473 if (ret == LUA_TNIL) {
4474
4475 /* Entry not found. */
4476 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4477
4478 /* Insert the new header name in the array in the top of the stack.
4479 * It left the new array in the top of the stack.
4480 */
4481 lua_newtable(L);
4482 lua_pushvalue(L, 2);
4483 lua_pushvalue(L, -2);
4484 lua_settable(L, -4);
4485
4486 } else if (ret != LUA_TTABLE) {
4487
4488 /* corruption error. */
4489 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4490 "is expected as an array. %s found", name, lua_typename(L, ret));
4491 WILL_LJMP(lua_error(L));
4492 }
4493
4494 /* Now the top od thestack is an array of values. We push
4495 * the header value as new entry.
4496 */
4497 lua_pushvalue(L, 3);
4498 ret = lua_rawlen(L, -2);
4499 lua_rawseti(L, -2, ret + 1);
4500 lua_pushboolean(L, 1);
4501 return 1;
4502}
4503
4504__LJMP static int hlua_applet_http_status(lua_State *L)
4505{
4506 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4507 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004508 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004509
4510 if (status < 100 || status > 599) {
4511 lua_pushboolean(L, 0);
4512 return 1;
4513 }
4514
4515 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004516 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004517 lua_pushboolean(L, 1);
4518 return 1;
4519}
4520
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004521
Christopher Fauleta2097962019-07-15 16:25:33 +02004522__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004523{
4524 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4525 struct stream_interface *si = appctx->appctx->owner;
4526 struct channel *res = si_ic(si);
4527 struct htx *htx;
4528 struct htx_sl *sl;
4529 struct h1m h1m;
4530 const char *status, *reason;
4531 const char *name, *value;
4532 size_t nlen, vlen;
4533 unsigned int flags;
4534
4535 /* Send the message at once. */
4536 htx = htx_from_buf(&res->buf);
4537 h1m_init_res(&h1m);
4538
4539 /* Use the same http version than the request. */
4540 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4541 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4542 if (reason == NULL)
4543 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4544 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4545 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4546 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4547 }
4548 else {
4549 flags = HTX_SL_F_IS_RESP;
4550 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4551 }
4552 if (!sl) {
4553 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4554 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4555 WILL_LJMP(lua_error(L));
4556 }
4557 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4558
4559 /* Get the array associated to the field "response" in the object AppletHTTP. */
4560 lua_pushvalue(L, 0);
4561 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4562 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4563 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4564 WILL_LJMP(lua_error(L));
4565 }
4566
4567 /* Browse the list of headers. */
4568 lua_pushnil(L);
4569 while(lua_next(L, -2) != 0) {
4570 /* We expect a string as -2. */
4571 if (lua_type(L, -2) != LUA_TSTRING) {
4572 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4573 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4574 lua_typename(L, lua_type(L, -2)));
4575 WILL_LJMP(lua_error(L));
4576 }
4577 name = lua_tolstring(L, -2, &nlen);
4578
4579 /* We expect an array as -1. */
4580 if (lua_type(L, -1) != LUA_TTABLE) {
4581 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4582 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4583 name,
4584 lua_typename(L, lua_type(L, -1)));
4585 WILL_LJMP(lua_error(L));
4586 }
4587
4588 /* Browse the table who is on the top of the stack. */
4589 lua_pushnil(L);
4590 while(lua_next(L, -2) != 0) {
4591 int id;
4592
4593 /* We expect a number as -2. */
4594 if (lua_type(L, -2) != LUA_TNUMBER) {
4595 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4596 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4597 name,
4598 lua_typename(L, lua_type(L, -2)));
4599 WILL_LJMP(lua_error(L));
4600 }
4601 id = lua_tointeger(L, -2);
4602
4603 /* We expect a string as -2. */
4604 if (lua_type(L, -1) != LUA_TSTRING) {
4605 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4606 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4607 name, id,
4608 lua_typename(L, lua_type(L, -1)));
4609 WILL_LJMP(lua_error(L));
4610 }
4611 value = lua_tolstring(L, -1, &vlen);
4612
4613 /* Simple Protocol checks. */
4614 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004615 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004616 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4617 struct ist v = ist2(value, vlen);
4618 int ret;
4619
4620 ret = h1_parse_cont_len_header(&h1m, &v);
4621 if (ret < 0) {
4622 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4623 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4624 name);
4625 WILL_LJMP(lua_error(L));
4626 }
4627 else if (ret == 0)
4628 goto next; /* Skip it */
4629 }
4630
4631 /* Add a new header */
4632 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4633 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4634 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4635 name);
4636 WILL_LJMP(lua_error(L));
4637 }
4638 next:
4639 /* Remove the array from the stack, and get next element with a remaining string. */
4640 lua_pop(L, 1);
4641 }
4642
4643 /* Remove the array from the stack, and get next element with a remaining string. */
4644 lua_pop(L, 1);
4645 }
4646
4647 if (h1m.flags & H1_MF_CHNK)
4648 h1m.flags &= ~H1_MF_CLEN;
4649 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4650 h1m.flags |= H1_MF_XFER_LEN;
4651
4652 /* Uset HTX start-line flags */
4653 if (h1m.flags & H1_MF_XFER_ENC)
4654 flags |= HTX_SL_F_XFER_ENC;
4655 if (h1m.flags & H1_MF_XFER_LEN) {
4656 flags |= HTX_SL_F_XFER_LEN;
4657 if (h1m.flags & H1_MF_CHNK)
4658 flags |= HTX_SL_F_CHNK;
4659 else if (h1m.flags & H1_MF_CLEN)
4660 flags |= HTX_SL_F_CLEN;
4661 if (h1m.body_len == 0)
4662 flags |= HTX_SL_F_BODYLESS;
4663 }
4664 sl->flags |= flags;
4665
4666 /* If we dont have a content-length set, and the HTTP version is 1.1
4667 * and the status code implies the presence of a message body, we must
4668 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004669 * for the keepalive compliance. If the applet announces a transfer-encoding
4670 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004671 */
4672 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4673 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4674 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4675 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4676 /* Add a new header */
4677 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4678 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4679 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4680 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4681 WILL_LJMP(lua_error(L));
4682 }
4683 }
4684
4685 /* Finalize headers. */
4686 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4687 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4688 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4689 WILL_LJMP(lua_error(L));
4690 }
4691
4692 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4693 b_reset(&res->buf);
4694 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4695 WILL_LJMP(lua_error(L));
4696 }
4697
4698 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004699 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004700
4701 /* Headers sent, set the flag. */
4702 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4703 return 0;
4704
4705}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004706/* We will build the status line and the headers of the HTTP response.
4707 * We will try send at once if its not possible, we give back the hand
4708 * waiting for more room.
4709 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004710__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004711{
4712 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4713 struct stream_interface *si = appctx->appctx->owner;
4714 struct channel *res = si_ic(si);
4715
4716 if (co_data(res)) {
4717 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004718 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004719 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004720 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004721}
4722
4723
Christopher Fauleta2097962019-07-15 16:25:33 +02004724__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004725{
Christopher Fauleta2097962019-07-15 16:25:33 +02004726 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004727}
4728
Christopher Fauleta2097962019-07-15 16:25:33 +02004729/*
4730 *
4731 *
4732 * Class HTTP
4733 *
4734 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004735 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004736
4737/* Returns a struct hlua_txn if the stack entry "ud" is
4738 * a class stream, otherwise it throws an error.
4739 */
4740__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004741{
Christopher Fauleta2097962019-07-15 16:25:33 +02004742 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4743}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004744
Christopher Fauleta2097962019-07-15 16:25:33 +02004745/* This function creates and push in the stack a HTTP object
4746 * according with a current TXN.
4747 */
4748static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4749{
4750 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004751
Christopher Fauleta2097962019-07-15 16:25:33 +02004752 /* Check stack size. */
4753 if (!lua_checkstack(L, 3))
4754 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004755
Christopher Fauleta2097962019-07-15 16:25:33 +02004756 /* Create the object: obj[0] = userdata.
4757 * Note that the base of the Converters object is the
4758 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004759 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004760 lua_newtable(L);
4761 htxn = lua_newuserdata(L, sizeof(*htxn));
4762 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004763
4764 htxn->s = txn->s;
4765 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004766 htxn->dir = txn->dir;
4767 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004768
4769 /* Pop a class stream metatable and affect it to the table. */
4770 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4771 lua_setmetatable(L, -2);
4772
4773 return 1;
4774}
4775
4776/* This function creates ans returns an array of HTTP headers.
4777 * This function does not fails. It is used as wrapper with the
4778 * 2 following functions.
4779 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004780__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004781{
Christopher Fauleta2097962019-07-15 16:25:33 +02004782 struct htx *htx;
4783 int32_t pos;
4784
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004785 /* Create the table. */
4786 lua_newtable(L);
4787
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004788
Christopher Fauleta2097962019-07-15 16:25:33 +02004789 htx = htxbuf(&msg->chn->buf);
4790 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4791 struct htx_blk *blk = htx_get_blk(htx, pos);
4792 enum htx_blk_type type = htx_get_blk_type(blk);
4793 struct ist n, v;
4794 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004795
Christopher Fauleta2097962019-07-15 16:25:33 +02004796 if (type == HTX_BLK_HDR) {
4797 n = htx_get_blk_name(htx,blk);
4798 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004799 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004800 else if (type == HTX_BLK_EOH)
4801 break;
4802 else
4803 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004804
Christopher Fauleta2097962019-07-15 16:25:33 +02004805 /* Check for existing entry:
4806 * assume that the table is on the top of the stack, and
4807 * push the key in the stack, the function lua_gettable()
4808 * perform the lookup.
4809 */
4810 lua_pushlstring(L, n.ptr, n.len);
4811 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004812
Christopher Fauleta2097962019-07-15 16:25:33 +02004813 switch (lua_type(L, -1)) {
4814 case LUA_TNIL:
4815 /* Table not found, create it. */
4816 lua_pop(L, 1); /* remove the nil value. */
4817 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4818 lua_newtable(L); /* create and push empty table. */
4819 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4820 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4821 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004822 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004823
Christopher Fauleta2097962019-07-15 16:25:33 +02004824 case LUA_TTABLE:
4825 /* Entry found: push the value in the table. */
4826 len = lua_rawlen(L, -1);
4827 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4828 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4829 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4830 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004831
Christopher Fauleta2097962019-07-15 16:25:33 +02004832 default:
4833 /* Other cases are errors. */
4834 hlua_pusherror(L, "internal error during the parsing of headers.");
4835 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004836 }
4837 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004838 return 1;
4839}
4840
4841__LJMP static int hlua_http_req_get_headers(lua_State *L)
4842{
4843 struct hlua_txn *htxn;
4844
4845 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4846 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4847
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004848 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004849 WILL_LJMP(lua_error(L));
4850
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004851 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004852}
4853
4854__LJMP static int hlua_http_res_get_headers(lua_State *L)
4855{
4856 struct hlua_txn *htxn;
4857
4858 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4859 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4860
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004861 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004862 WILL_LJMP(lua_error(L));
4863
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004864 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004865}
4866
4867/* This function replace full header, or just a value in
4868 * the request or in the response. It is a wrapper fir the
4869 * 4 following functions.
4870 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004871__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004872{
4873 size_t name_len;
4874 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4875 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4876 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004877 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004878 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004879
Dragan Dosen26743032019-04-30 15:54:36 +02004880 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004881 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4882
Christopher Fauleta2097962019-07-15 16:25:33 +02004883 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004884 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004885 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004886 return 0;
4887}
4888
4889__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4890{
4891 struct hlua_txn *htxn;
4892
4893 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4894 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4895
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004896 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004897 WILL_LJMP(lua_error(L));
4898
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004899 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004900}
4901
4902__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4903{
4904 struct hlua_txn *htxn;
4905
4906 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4907 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4908
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004909 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004910 WILL_LJMP(lua_error(L));
4911
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004912 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004913}
4914
4915__LJMP static int hlua_http_req_rep_val(lua_State *L)
4916{
4917 struct hlua_txn *htxn;
4918
4919 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4920 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4921
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004922 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004923 WILL_LJMP(lua_error(L));
4924
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004925 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004926}
4927
4928__LJMP static int hlua_http_res_rep_val(lua_State *L)
4929{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004930 struct hlua_txn *htxn;
4931
4932 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4933 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4934
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004935 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004936 WILL_LJMP(lua_error(L));
4937
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004938 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004939}
4940
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004941/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004942 * It is a wrapper for the 2 following functions.
4943 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004944__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004945{
4946 size_t len;
4947 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004948 struct htx *htx = htxbuf(&msg->chn->buf);
4949 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004950
Christopher Fauleta2097962019-07-15 16:25:33 +02004951 ctx.blk = NULL;
4952 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4953 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004954 return 0;
4955}
4956
4957__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4958{
4959 struct hlua_txn *htxn;
4960
4961 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4962 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4963
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004964 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004965 WILL_LJMP(lua_error(L));
4966
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004967 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004968}
4969
4970__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4971{
4972 struct hlua_txn *htxn;
4973
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004974 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004975 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4976
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004977 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004978 WILL_LJMP(lua_error(L));
4979
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004980 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004981}
4982
4983/* This function adds an header. It is a wrapper used by
4984 * the 2 following functions.
4985 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004986__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004987{
4988 size_t name_len;
4989 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4990 size_t value_len;
4991 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004992 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004993
Christopher Fauleta2097962019-07-15 16:25:33 +02004994 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4995 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004996 return 0;
4997}
4998
4999__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5000{
5001 struct hlua_txn *htxn;
5002
5003 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5004 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5005
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005006 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005007 WILL_LJMP(lua_error(L));
5008
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005009 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005010}
5011
5012__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5013{
5014 struct hlua_txn *htxn;
5015
5016 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5017 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5018
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005019 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005020 WILL_LJMP(lua_error(L));
5021
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005022 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005023}
5024
5025static int hlua_http_req_set_hdr(lua_State *L)
5026{
5027 struct hlua_txn *htxn;
5028
5029 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5030 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5031
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005032 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005033 WILL_LJMP(lua_error(L));
5034
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005035 hlua_http_del_hdr(L, &htxn->s->txn->req);
5036 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005037}
5038
5039static int hlua_http_res_set_hdr(lua_State *L)
5040{
5041 struct hlua_txn *htxn;
5042
5043 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5044 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5045
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005046 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005047 WILL_LJMP(lua_error(L));
5048
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005049 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5050 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005051}
5052
5053/* This function set the method. */
5054static int hlua_http_req_set_meth(lua_State *L)
5055{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005056 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005057 size_t name_len;
5058 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005059
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005060 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005061 WILL_LJMP(lua_error(L));
5062
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005063 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005064 return 1;
5065}
5066
5067/* This function set the method. */
5068static int hlua_http_req_set_path(lua_State *L)
5069{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005070 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005071 size_t name_len;
5072 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005073
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005074 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005075 WILL_LJMP(lua_error(L));
5076
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005077 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005078 return 1;
5079}
5080
5081/* This function set the query-string. */
5082static int hlua_http_req_set_query(lua_State *L)
5083{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005084 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005085 size_t name_len;
5086 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005087
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005088 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005089 WILL_LJMP(lua_error(L));
5090
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005091 /* Check length. */
5092 if (name_len > trash.size - 1) {
5093 lua_pushboolean(L, 0);
5094 return 1;
5095 }
5096
5097 /* Add the mark question as prefix. */
5098 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005099 trash.area[trash.data++] = '?';
5100 memcpy(trash.area + trash.data, name, name_len);
5101 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005102
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005103 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005104 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005105 return 1;
5106}
5107
5108/* This function set the uri. */
5109static int hlua_http_req_set_uri(lua_State *L)
5110{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005111 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005112 size_t name_len;
5113 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005114
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005115 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005116 WILL_LJMP(lua_error(L));
5117
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005118 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005119 return 1;
5120}
5121
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005122/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005123static int hlua_http_res_set_status(lua_State *L)
5124{
5125 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5126 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005127 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5128 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005129
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005130 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005131 WILL_LJMP(lua_error(L));
5132
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005133 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005134 return 0;
5135}
5136
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005137/*
5138 *
5139 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005140 * Class TXN
5141 *
5142 *
5143 */
5144
5145/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005146 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005147 */
5148__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5149{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005150 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005151}
5152
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005153__LJMP static int hlua_set_var(lua_State *L)
5154{
5155 struct hlua_txn *htxn;
5156 const char *name;
5157 size_t len;
5158 struct sample smp;
5159
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005160 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5161 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005162
5163 /* It is useles to retrieve the stream, but this function
5164 * runs only in a stream context.
5165 */
5166 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5167 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5168
5169 /* Converts the third argument in a sample. */
5170 hlua_lua2smp(L, 3, &smp);
5171
5172 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005173 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005174
5175 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5176 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5177 else
5178 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5179
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005180 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005181}
5182
Christopher Faulet85d79c92016-11-09 16:54:56 +01005183__LJMP static int hlua_unset_var(lua_State *L)
5184{
5185 struct hlua_txn *htxn;
5186 const char *name;
5187 size_t len;
5188 struct sample smp;
5189
5190 MAY_LJMP(check_args(L, 2, "unset_var"));
5191
5192 /* It is useles to retrieve the stream, but this function
5193 * runs only in a stream context.
5194 */
5195 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5196 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5197
5198 /* Unset the variable. */
5199 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005200 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5201 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005202}
5203
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005204__LJMP static int hlua_get_var(lua_State *L)
5205{
5206 struct hlua_txn *htxn;
5207 const char *name;
5208 size_t len;
5209 struct sample smp;
5210
5211 MAY_LJMP(check_args(L, 2, "get_var"));
5212
5213 /* It is useles to retrieve the stream, but this function
5214 * runs only in a stream context.
5215 */
5216 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5217 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5218
Willy Tarreau7560dd42016-03-10 16:28:58 +01005219 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005220 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005221 lua_pushnil(L);
5222 return 1;
5223 }
5224
5225 return hlua_smp2lua(L, &smp);
5226}
5227
Willy Tarreau59551662015-03-10 14:23:13 +01005228__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005229{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005230 struct hlua *hlua;
5231
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005232 MAY_LJMP(check_args(L, 2, "set_priv"));
5233
Willy Tarreau87b09662015-04-03 00:22:06 +02005234 /* It is useles to retrieve the stream, but this function
5235 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005236 */
5237 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005238 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005239
5240 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005241 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005242
5243 /* Get and store new value. */
5244 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5245 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5246
5247 return 0;
5248}
5249
Willy Tarreau59551662015-03-10 14:23:13 +01005250__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005251{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005252 struct hlua *hlua;
5253
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005254 MAY_LJMP(check_args(L, 1, "get_priv"));
5255
Willy Tarreau87b09662015-04-03 00:22:06 +02005256 /* It is useles to retrieve the stream, but this function
5257 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005258 */
5259 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005260 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005261
5262 /* Push configuration index in the stack. */
5263 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5264
5265 return 1;
5266}
5267
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005268/* Create stack entry containing a class TXN. This function
5269 * return 0 if the stack does not contains free slots,
5270 * otherwise it returns 1.
5271 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005272static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005273{
Willy Tarreaude491382015-04-06 11:04:28 +02005274 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005275
5276 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005277 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005278 return 0;
5279
5280 /* NOTE: The allocation never fails. The failure
5281 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005282 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005283 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005284 /* Create the object: obj[0] = userdata. */
5285 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005286 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005287 lua_rawseti(L, -2, 0);
5288
Willy Tarreaude491382015-04-06 11:04:28 +02005289 htxn->s = s;
5290 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005291 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005292 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005293
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005294 /* Create the "f" field that contains a list of fetches. */
5295 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005296 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005297 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005298 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005299
5300 /* Create the "sf" field that contains a list of stringsafe fetches. */
5301 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005302 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005303 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005304 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005305
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005306 /* Create the "c" field that contains a list of converters. */
5307 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005308 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005309 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005310 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005311
5312 /* Create the "sc" field that contains a list of stringsafe converters. */
5313 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005314 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005315 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005316 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005317
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005318 /* Create the "req" field that contains the request channel object. */
5319 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005320 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005321 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005322 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005323
5324 /* Create the "res" field that contains the response channel object. */
5325 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005326 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005327 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005328 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005329
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005330 /* Creates the HTTP object is the current proxy allows http. */
5331 lua_pushstring(L, "http");
5332 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005333 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005334 return 0;
5335 }
5336 else
5337 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005338 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005339
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005340 /* Pop a class sesison metatable and affect it to the userdata. */
5341 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5342 lua_setmetatable(L, -2);
5343
5344 return 1;
5345}
5346
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005347__LJMP static int hlua_txn_deflog(lua_State *L)
5348{
5349 const char *msg;
5350 struct hlua_txn *htxn;
5351
5352 MAY_LJMP(check_args(L, 2, "deflog"));
5353 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5354 msg = MAY_LJMP(luaL_checkstring(L, 2));
5355
5356 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5357 return 0;
5358}
5359
5360__LJMP static int hlua_txn_log(lua_State *L)
5361{
5362 int level;
5363 const char *msg;
5364 struct hlua_txn *htxn;
5365
5366 MAY_LJMP(check_args(L, 3, "log"));
5367 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5368 level = MAY_LJMP(luaL_checkinteger(L, 2));
5369 msg = MAY_LJMP(luaL_checkstring(L, 3));
5370
5371 if (level < 0 || level >= NB_LOG_LEVELS)
5372 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5373
5374 hlua_sendlog(htxn->s->be, level, msg);
5375 return 0;
5376}
5377
5378__LJMP static int hlua_txn_log_debug(lua_State *L)
5379{
5380 const char *msg;
5381 struct hlua_txn *htxn;
5382
5383 MAY_LJMP(check_args(L, 2, "Debug"));
5384 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5385 msg = MAY_LJMP(luaL_checkstring(L, 2));
5386 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5387 return 0;
5388}
5389
5390__LJMP static int hlua_txn_log_info(lua_State *L)
5391{
5392 const char *msg;
5393 struct hlua_txn *htxn;
5394
5395 MAY_LJMP(check_args(L, 2, "Info"));
5396 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5397 msg = MAY_LJMP(luaL_checkstring(L, 2));
5398 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5399 return 0;
5400}
5401
5402__LJMP static int hlua_txn_log_warning(lua_State *L)
5403{
5404 const char *msg;
5405 struct hlua_txn *htxn;
5406
5407 MAY_LJMP(check_args(L, 2, "Warning"));
5408 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5409 msg = MAY_LJMP(luaL_checkstring(L, 2));
5410 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5411 return 0;
5412}
5413
5414__LJMP static int hlua_txn_log_alert(lua_State *L)
5415{
5416 const char *msg;
5417 struct hlua_txn *htxn;
5418
5419 MAY_LJMP(check_args(L, 2, "Alert"));
5420 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5421 msg = MAY_LJMP(luaL_checkstring(L, 2));
5422 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5423 return 0;
5424}
5425
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005426__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5427{
5428 struct hlua_txn *htxn;
5429 int ll;
5430
5431 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5432 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5433 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5434
5435 if (ll < 0 || ll > 7)
5436 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5437
5438 htxn->s->logs.level = ll;
5439 return 0;
5440}
5441
5442__LJMP static int hlua_txn_set_tos(lua_State *L)
5443{
5444 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005445 int tos;
5446
5447 MAY_LJMP(check_args(L, 2, "set_tos"));
5448 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5449 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5450
Willy Tarreau1a18b542018-12-11 16:37:42 +01005451 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005452 return 0;
5453}
5454
5455__LJMP static int hlua_txn_set_mark(lua_State *L)
5456{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005457 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005458 int mark;
5459
5460 MAY_LJMP(check_args(L, 2, "set_mark"));
5461 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5462 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5463
Lukas Tribus579e3e32019-08-11 18:03:45 +02005464 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005465 return 0;
5466}
5467
Patrick Hemmer268a7072018-05-11 12:52:31 -04005468__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5469{
5470 struct hlua_txn *htxn;
5471
5472 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5473 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5474 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5475 return 0;
5476}
5477
5478__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5479{
5480 struct hlua_txn *htxn;
5481
5482 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5483 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5484 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5485 return 0;
5486}
5487
Christopher Faulet700d9e82020-01-31 12:21:52 +01005488/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005489 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005490 * message and terminate the transaction. It returns 1 on success and 0 on
5491 * error. The Reply must be on top of the stack.
5492 */
5493__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5494{
5495 struct htx *htx;
5496 struct htx_sl *sl;
5497 struct h1m h1m;
5498 const char *status, *reason, *body;
5499 size_t status_len, reason_len, body_len;
5500 int ret, code, flags;
5501
5502 code = 200;
5503 status = "200";
5504 status_len = 3;
5505 ret = lua_getfield(L, -1, "status");
5506 if (ret == LUA_TNUMBER) {
5507 code = lua_tointeger(L, -1);
5508 status = lua_tolstring(L, -1, &status_len);
5509 }
5510 lua_pop(L, 1);
5511
5512 reason = http_get_reason(code);
5513 reason_len = strlen(reason);
5514 ret = lua_getfield(L, -1, "reason");
5515 if (ret == LUA_TSTRING)
5516 reason = lua_tolstring(L, -1, &reason_len);
5517 lua_pop(L, 1);
5518
5519 body = NULL;
5520 body_len = 0;
5521 ret = lua_getfield(L, -1, "body");
5522 if (ret == LUA_TSTRING)
5523 body = lua_tolstring(L, -1, &body_len);
5524 lua_pop(L, 1);
5525
5526 /* Prepare the response before inserting the headers */
5527 h1m_init_res(&h1m);
5528 htx = htx_from_buf(&s->res.buf);
5529 channel_htx_truncate(&s->res, htx);
5530 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5531 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5532 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5533 ist2(status, status_len), ist2(reason, reason_len));
5534 }
5535 else {
5536 flags = HTX_SL_F_IS_RESP;
5537 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5538 ist2(status, status_len), ist2(reason, reason_len));
5539 }
5540 if (!sl)
5541 goto fail;
5542 sl->info.res.status = code;
5543
5544 /* Push in the stack the "headers" entry. */
5545 ret = lua_getfield(L, -1, "headers");
5546 if (ret != LUA_TTABLE)
5547 goto skip_headers;
5548
5549 lua_pushnil(L);
5550 while (lua_next(L, -2) != 0) {
5551 struct ist name, value;
5552 const char *n, *v;
5553 size_t nlen, vlen;
5554
5555 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5556 /* Skip element if the key is not a string or if the value is not a table */
5557 goto next_hdr;
5558 }
5559
5560 n = lua_tolstring(L, -2, &nlen);
5561 name = ist2(n, nlen);
5562 if (isteqi(name, ist("content-length"))) {
5563 /* Always skip content-length header. It will be added
5564 * later with the correct len
5565 */
5566 goto next_hdr;
5567 }
5568
5569 /* Loop on header's values */
5570 lua_pushnil(L);
5571 while (lua_next(L, -2)) {
5572 if (!lua_isstring(L, -1)) {
5573 /* Skip the value if it is not a string */
5574 goto next_value;
5575 }
5576
5577 v = lua_tolstring(L, -1, &vlen);
5578 value = ist2(v, vlen);
5579
5580 if (isteqi(name, ist("transfer-encoding")))
5581 h1_parse_xfer_enc_header(&h1m, value);
5582 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5583 goto fail;
5584
5585 next_value:
5586 lua_pop(L, 1);
5587 }
5588
5589 next_hdr:
5590 lua_pop(L, 1);
5591 }
5592 skip_headers:
5593 lua_pop(L, 1);
5594
5595 /* Update h1m flags: CLEN is set if CHNK is not present */
5596 if (!(h1m.flags & H1_MF_CHNK)) {
5597 const char *clen = ultoa(body_len);
5598
5599 h1m.flags |= H1_MF_CLEN;
5600 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5601 goto fail;
5602 }
5603 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5604 h1m.flags |= H1_MF_XFER_LEN;
5605
5606 /* Update HTX start-line flags */
5607 if (h1m.flags & H1_MF_XFER_ENC)
5608 flags |= HTX_SL_F_XFER_ENC;
5609 if (h1m.flags & H1_MF_XFER_LEN) {
5610 flags |= HTX_SL_F_XFER_LEN;
5611 if (h1m.flags & H1_MF_CHNK)
5612 flags |= HTX_SL_F_CHNK;
5613 else if (h1m.flags & H1_MF_CLEN)
5614 flags |= HTX_SL_F_CLEN;
5615 if (h1m.body_len == 0)
5616 flags |= HTX_SL_F_BODYLESS;
5617 }
5618 sl->flags |= flags;
5619
5620
5621 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5622 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5623 !htx_add_endof(htx, HTX_BLK_EOM))
5624 goto fail;
5625
5626 /* Now, forward the response and terminate the transaction */
5627 s->txn->status = code;
5628 htx_to_buf(htx, &s->res.buf);
5629 if (!http_forward_proxy_resp(s, 1))
5630 goto fail;
5631
5632 return 1;
5633
5634 fail:
5635 channel_htx_truncate(&s->res, htx);
5636 return 0;
5637}
5638
5639/* Terminate a transaction if called from a lua action. For TCP streams,
5640 * processing is just aborted. Nothing is returned to the client and all
5641 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5642 * is forwarded to the client before terminating the transaction. On success,
5643 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5644 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5645 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005646 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005647__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005648{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005649 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005650 struct stream *s;
5651 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005652
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005653 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005654
Christopher Faulet700d9e82020-01-31 12:21:52 +01005655 /* If the flags NOTERM is set, we cannot terminate the session, so we
5656 * just end the execution of the current lua code. */
5657 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005658 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005659
Christopher Faulet700d9e82020-01-31 12:21:52 +01005660 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005661 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005662 struct channel *req = &s->req;
5663 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005664
Christopher Faulet700d9e82020-01-31 12:21:52 +01005665 channel_auto_read(req);
5666 channel_abort(req);
5667 channel_auto_close(req);
5668 channel_erase(req);
5669
5670 res->wex = tick_add_ifset(now_ms, res->wto);
5671 channel_auto_read(res);
5672 channel_auto_close(res);
5673 channel_shutr_now(res);
5674
5675 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5676 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005677 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005678
Christopher Faulet700d9e82020-01-31 12:21:52 +01005679 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5680 /* No reply or invalid reply */
5681 s->txn->status = 0;
5682 http_reply_and_close(s, 0, NULL);
5683 }
5684 else {
5685 /* Remove extra args to have the reply on top of the stack */
5686 if (lua_gettop(L) > 2)
5687 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005688
Christopher Faulet700d9e82020-01-31 12:21:52 +01005689 if (!hlua_txn_forward_reply(L, s)) {
5690 if (!(s->flags & SF_ERR_MASK))
5691 s->flags |= SF_ERR_PRXCOND;
5692 lua_pushinteger(L, ACT_RET_ERR);
5693 WILL_LJMP(hlua_done(L));
5694 return 0; /* Never reached */
5695 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005696 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005697
Christopher Faulet700d9e82020-01-31 12:21:52 +01005698 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5699 if (htxn->dir == SMP_OPT_DIR_REQ) {
5700 /* let's log the request time */
5701 s->logs.tv_request = now;
5702 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5703 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5704 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005705
Christopher Faulet700d9e82020-01-31 12:21:52 +01005706 done:
5707 if (!(s->flags & SF_ERR_MASK))
5708 s->flags |= SF_ERR_LOCAL;
5709 if (!(s->flags & SF_FINST_MASK))
5710 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005711
Christopher Faulet4ad73102020-03-05 11:07:31 +01005712 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005713 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005714 return 0;
5715}
5716
Christopher Faulet700d9e82020-01-31 12:21:52 +01005717/*
5718 *
5719 *
5720 * Class REPLY
5721 *
5722 *
5723 */
5724
5725/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5726 * free slots, the function fails and returns 0;
5727 */
5728static int hlua_txn_reply_new(lua_State *L)
5729{
5730 struct hlua_txn *htxn;
5731 const char *reason, *body = NULL;
5732 int ret, status;
5733
5734 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005735 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005736 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5737 WILL_LJMP(lua_error(L));
5738 }
5739
5740 /* Default value */
5741 status = 200;
5742 reason = http_get_reason(status);
5743
5744 if (lua_istable(L, 2)) {
5745 /* load status and reason from the table argument at index 2 */
5746 ret = lua_getfield(L, 2, "status");
5747 if (ret == LUA_TNIL)
5748 goto reason;
5749 else if (ret != LUA_TNUMBER) {
5750 /* invalid status: ignore the reason */
5751 goto body;
5752 }
5753 status = lua_tointeger(L, -1);
5754
5755 reason:
5756 lua_pop(L, 1); /* restore the stack: remove status */
5757 ret = lua_getfield(L, 2, "reason");
5758 if (ret == LUA_TSTRING)
5759 reason = lua_tostring(L, -1);
5760
5761 body:
5762 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5763 ret = lua_getfield(L, 2, "body");
5764 if (ret == LUA_TSTRING)
5765 body = lua_tostring(L, -1);
5766 lua_pop(L, 1); /* restore the stack: remove body */
5767 }
5768
5769 /* Create the Reply table */
5770 lua_newtable(L);
5771
5772 /* Add status element */
5773 lua_pushstring(L, "status");
5774 lua_pushinteger(L, status);
5775 lua_settable(L, -3);
5776
5777 /* Add reason element */
5778 reason = http_get_reason(status);
5779 lua_pushstring(L, "reason");
5780 lua_pushstring(L, reason);
5781 lua_settable(L, -3);
5782
5783 /* Add body element, nil if undefined */
5784 lua_pushstring(L, "body");
5785 if (body)
5786 lua_pushstring(L, body);
5787 else
5788 lua_pushnil(L);
5789 lua_settable(L, -3);
5790
5791 /* Add headers element */
5792 lua_pushstring(L, "headers");
5793 lua_newtable(L);
5794
5795 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5796 if (lua_istable(L, 2)) {
5797 /* load headers from the table argument at index 2. If it is a table, copy it. */
5798 ret = lua_getfield(L, 2, "headers");
5799 if (ret == LUA_TTABLE) {
5800 /* stack: [ ... <headers:table>, <table> ] */
5801 lua_pushnil(L);
5802 while (lua_next(L, -2) != 0) {
5803 /* stack: [ ... <headers:table>, <table>, k, v] */
5804 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5805 /* invalid value type, skip it */
5806 lua_pop(L, 1);
5807 continue;
5808 }
5809
5810
5811 /* Duplicate the key and swap it with the value. */
5812 lua_pushvalue(L, -2);
5813 lua_insert(L, -2);
5814 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5815
5816 lua_newtable(L);
5817 lua_insert(L, -2);
5818 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5819
5820 if (lua_isstring(L, -1)) {
5821 /* push the value in the inner table */
5822 lua_rawseti(L, -2, 1);
5823 }
5824 else { /* table */
5825 lua_pushnil(L);
5826 while (lua_next(L, -2) != 0) {
5827 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5828 if (!lua_isstring(L, -1)) {
5829 /* invalid value type, skip it*/
5830 lua_pop(L, 1);
5831 continue;
5832 }
5833 /* push the value in the inner table */
5834 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5835 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5836 }
5837 lua_pop(L, 1);
5838 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5839 }
5840
5841 /* push (k,v) on the stack in the headers table:
5842 * stack: [ ... <headers:table>, <table>, k, k, v ]
5843 */
5844 lua_settable(L, -5);
5845 /* stack: [ ... <headers:table>, <table>, k ] */
5846 }
5847 }
5848 lua_pop(L, 1);
5849 }
5850 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5851 lua_settable(L, -3);
5852 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5853
5854 /* Pop a class sesison metatable and affect it to the userdata. */
5855 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5856 lua_setmetatable(L, -2);
5857 return 1;
5858}
5859
5860/* Set the reply status code, and optionally the reason. If no reason is
5861 * provided, the default one corresponding to the status code is used.
5862 */
5863__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5864{
5865 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5866 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5867
5868 /* First argument (self) must be a table */
5869 luaL_checktype(L, 1, LUA_TTABLE);
5870
5871 if (status < 100 || status > 599) {
5872 lua_pushboolean(L, 0);
5873 return 1;
5874 }
5875 if (!reason)
5876 reason = http_get_reason(status);
5877
5878 lua_pushinteger(L, status);
5879 lua_setfield(L, 1, "status");
5880
5881 lua_pushstring(L, reason);
5882 lua_setfield(L, 1, "reason");
5883
5884 lua_pushboolean(L, 1);
5885 return 1;
5886}
5887
5888/* Add a header into the reply object. Each header name is associated to an
5889 * array of values in the "headers" table. If the header name is not found, a
5890 * new entry is created.
5891 */
5892__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5893{
5894 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5895 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5896 int ret;
5897
5898 /* First argument (self) must be a table */
5899 luaL_checktype(L, 1, LUA_TTABLE);
5900
5901 /* Push in the stack the "headers" entry. */
5902 ret = lua_getfield(L, 1, "headers");
5903 if (ret != LUA_TTABLE) {
5904 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5905 WILL_LJMP(lua_error(L));
5906 }
5907
5908 /* check if the header is already registered. If not, register it. */
5909 ret = lua_getfield(L, -1, name);
5910 if (ret == LUA_TNIL) {
5911 /* Entry not found. */
5912 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5913
5914 /* Insert the new header name in the array in the top of the stack.
5915 * It left the new array in the top of the stack.
5916 */
5917 lua_newtable(L);
5918 lua_pushstring(L, name);
5919 lua_pushvalue(L, -2);
5920 lua_settable(L, -4);
5921 }
5922 else if (ret != LUA_TTABLE) {
5923 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5924 WILL_LJMP(lua_error(L));
5925 }
5926
5927 /* Now the top od thestack is an array of values. We push
5928 * the header value as new entry.
5929 */
5930 lua_pushstring(L, value);
5931 ret = lua_rawlen(L, -2);
5932 lua_rawseti(L, -2, ret + 1);
5933
5934 lua_pushboolean(L, 1);
5935 return 1;
5936}
5937
5938/* Remove all occurrences of a given header name. */
5939__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5940{
5941 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5942 int ret;
5943
5944 /* First argument (self) must be a table */
5945 luaL_checktype(L, 1, LUA_TTABLE);
5946
5947 /* Push in the stack the "headers" entry. */
5948 ret = lua_getfield(L, 1, "headers");
5949 if (ret != LUA_TTABLE) {
5950 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5951 WILL_LJMP(lua_error(L));
5952 }
5953
5954 lua_pushstring(L, name);
5955 lua_pushnil(L);
5956 lua_settable(L, -3);
5957
5958 lua_pushboolean(L, 1);
5959 return 1;
5960}
5961
5962/* Set the reply's body. Overwrite any existing entry. */
5963__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5964{
5965 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5966
5967 /* First argument (self) must be a table */
5968 luaL_checktype(L, 1, LUA_TTABLE);
5969
5970 lua_pushstring(L, payload);
5971 lua_setfield(L, 1, "body");
5972
5973 lua_pushboolean(L, 1);
5974 return 1;
5975}
5976
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005977__LJMP static int hlua_log(lua_State *L)
5978{
5979 int level;
5980 const char *msg;
5981
5982 MAY_LJMP(check_args(L, 2, "log"));
5983 level = MAY_LJMP(luaL_checkinteger(L, 1));
5984 msg = MAY_LJMP(luaL_checkstring(L, 2));
5985
5986 if (level < 0 || level >= NB_LOG_LEVELS)
5987 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5988
5989 hlua_sendlog(NULL, level, msg);
5990 return 0;
5991}
5992
5993__LJMP static int hlua_log_debug(lua_State *L)
5994{
5995 const char *msg;
5996
5997 MAY_LJMP(check_args(L, 1, "debug"));
5998 msg = MAY_LJMP(luaL_checkstring(L, 1));
5999 hlua_sendlog(NULL, LOG_DEBUG, msg);
6000 return 0;
6001}
6002
6003__LJMP static int hlua_log_info(lua_State *L)
6004{
6005 const char *msg;
6006
6007 MAY_LJMP(check_args(L, 1, "info"));
6008 msg = MAY_LJMP(luaL_checkstring(L, 1));
6009 hlua_sendlog(NULL, LOG_INFO, msg);
6010 return 0;
6011}
6012
6013__LJMP static int hlua_log_warning(lua_State *L)
6014{
6015 const char *msg;
6016
6017 MAY_LJMP(check_args(L, 1, "warning"));
6018 msg = MAY_LJMP(luaL_checkstring(L, 1));
6019 hlua_sendlog(NULL, LOG_WARNING, msg);
6020 return 0;
6021}
6022
6023__LJMP static int hlua_log_alert(lua_State *L)
6024{
6025 const char *msg;
6026
6027 MAY_LJMP(check_args(L, 1, "alert"));
6028 msg = MAY_LJMP(luaL_checkstring(L, 1));
6029 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006030 return 0;
6031}
6032
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006033__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006034{
6035 int wakeup_ms = lua_tointeger(L, -1);
6036 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006037 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006038 return 0;
6039}
6040
6041__LJMP static int hlua_sleep(lua_State *L)
6042{
6043 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006044 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006045
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006046 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006047
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006048 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006049 wakeup_ms = tick_add(now_ms, delay);
6050 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006051
Willy Tarreau9635e032018-10-16 17:52:55 +02006052 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006053 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006054}
6055
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006056__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006057{
6058 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006059 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006060
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006061 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006062
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006063 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006064 wakeup_ms = tick_add(now_ms, delay);
6065 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006066
Willy Tarreau9635e032018-10-16 17:52:55 +02006067 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006068 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006069}
6070
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006071/* This functionis an LUA binding. it permits to give back
6072 * the hand at the HAProxy scheduler. It is used when the
6073 * LUA processing consumes a lot of time.
6074 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006075__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006076{
6077 return 0;
6078}
6079
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006080__LJMP static int hlua_yield(lua_State *L)
6081{
Willy Tarreau9635e032018-10-16 17:52:55 +02006082 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006083 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006084}
6085
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006086/* This function change the nice of the currently executed
6087 * task. It is used set low or high priority at the current
6088 * task.
6089 */
Willy Tarreau59551662015-03-10 14:23:13 +01006090__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006091{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006092 struct hlua *hlua;
6093 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006094
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006095 MAY_LJMP(check_args(L, 1, "set_nice"));
6096 hlua = hlua_gethlua(L);
6097 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006098
6099 /* If he task is not set, I'm in a start mode. */
6100 if (!hlua || !hlua->task)
6101 return 0;
6102
6103 if (nice < -1024)
6104 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006105 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006106 nice = 1024;
6107
6108 hlua->task->nice = nice;
6109 return 0;
6110}
6111
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006112/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006113 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6114 * return an E_AGAIN signal, the emmiter of this signal must set a
6115 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006116 *
6117 * Task wrapper are longjmp safe because the only one Lua code
6118 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006119 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006120struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006121{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006122 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006123 enum hlua_exec status;
6124
Christopher Faulet5bc99722018-04-25 10:34:45 +02006125 if (task->thread_mask == MAX_THREADS_MASK)
6126 task_set_affinity(task, tid_bit);
6127
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006128 /* If it is the first call to the task, we must initialize the
6129 * execution timeouts.
6130 */
6131 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006132 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006133
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006134 /* Execute the Lua code. */
6135 status = hlua_ctx_resume(hlua, 1);
6136
6137 switch (status) {
6138 /* finished or yield */
6139 case HLUA_E_OK:
6140 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006141 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006142 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006143 break;
6144
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006145 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006146 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006147 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006148 break;
6149
6150 /* finished with error. */
6151 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006152 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006153 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006154 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006155 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006156 break;
6157
6158 case HLUA_E_ERR:
6159 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006160 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006161 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006162 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006163 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006164 break;
6165 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006166 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006167}
6168
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006169/* This function is an LUA binding that register LUA function to be
6170 * executed after the HAProxy configuration parsing and before the
6171 * HAProxy scheduler starts. This function expect only one LUA
6172 * argument that is a function. This function returns nothing, but
6173 * throws if an error is encountered.
6174 */
6175__LJMP static int hlua_register_init(lua_State *L)
6176{
6177 struct hlua_init_function *init;
6178 int ref;
6179
6180 MAY_LJMP(check_args(L, 1, "register_init"));
6181
6182 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6183
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006184 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006185 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006186 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006187
6188 init->function_ref = ref;
6189 LIST_ADDQ(&hlua_init_functions, &init->l);
6190 return 0;
6191}
6192
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006193/* This functio is an LUA binding. It permits to register a task
6194 * executed in parallel of the main HAroxy activity. The task is
6195 * created and it is set in the HAProxy scheduler. It can be called
6196 * from the "init" section, "post init" or during the runtime.
6197 *
6198 * Lua prototype:
6199 *
6200 * <none> core.register_task(<function>)
6201 */
6202static int hlua_register_task(lua_State *L)
6203{
6204 struct hlua *hlua;
6205 struct task *task;
6206 int ref;
6207
6208 MAY_LJMP(check_args(L, 1, "register_task"));
6209
6210 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6211
Willy Tarreaubafbe012017-11-24 17:34:44 +01006212 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006213 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006214 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006215
Emeric Brunc60def82017-09-27 14:59:38 +02006216 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006217 if (!task)
6218 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6219
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006220 task->context = hlua;
6221 task->process = hlua_process_task;
6222
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006223 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006224 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006225
6226 /* Restore the function in the stack. */
6227 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6228 hlua->nargs = 0;
6229
6230 /* Schedule task. */
6231 task_schedule(task, now_ms);
6232
6233 return 0;
6234}
6235
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006236/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6237 * doesn't allow "yield" functions because the HAProxy engine cannot
6238 * resume converters.
6239 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006240static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006241{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006242 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006243 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006244 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006245
Willy Tarreaube508f12016-03-10 11:47:01 +01006246 if (!stream)
6247 return 0;
6248
Willy Tarreau87b09662015-04-03 00:22:06 +02006249 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006250 * Lua context can be not initialized. This behavior
6251 * permits to save performances because a systematic
6252 * Lua initialization cause 5% performances loss.
6253 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006254 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006255 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006256 if (!stream->hlua) {
6257 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6258 return 0;
6259 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006260 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006261 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6262 return 0;
6263 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006264 }
6265
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006266 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006267 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006268
6269 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006270 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6271 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6272 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006273 else
6274 error = "critical error";
6275 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006276 return 0;
6277 }
6278
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006279 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006280 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006281 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006282 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006283 return 0;
6284 }
6285
6286 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006287 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006288
6289 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006290 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006291 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006292 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006293 return 0;
6294 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006295 hlua_smp2lua(stream->hlua->T, smp);
6296 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006297
6298 /* push keywords in the stack. */
6299 if (arg_p) {
6300 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006301 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006302 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006303 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006304 return 0;
6305 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006306 hlua_arg2lua(stream->hlua->T, arg_p);
6307 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006308 }
6309 }
6310
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006311 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006312 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006313
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006314 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006315 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006316 }
6317
6318 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006319 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006320 /* finished. */
6321 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006322 /* If the stack is empty, the function fails. */
6323 if (lua_gettop(stream->hlua->T) <= 0)
6324 return 0;
6325
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006326 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006327 hlua_lua2smp(stream->hlua->T, -1, smp);
6328 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006329 return 1;
6330
6331 /* yield. */
6332 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006333 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006334 return 0;
6335
6336 /* finished with error. */
6337 case HLUA_E_ERRMSG:
6338 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006339 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006340 fcn->name, lua_tostring(stream->hlua->T, -1));
6341 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006342 return 0;
6343
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006344 case HLUA_E_ETMOUT:
6345 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6346 return 0;
6347
6348 case HLUA_E_NOMEM:
6349 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6350 return 0;
6351
6352 case HLUA_E_YIELD:
6353 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6354 return 0;
6355
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006356 case HLUA_E_ERR:
6357 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006358 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006359 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006360
6361 default:
6362 return 0;
6363 }
6364}
6365
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006366/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6367 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006368 * resume sample-fetches. This function will be called by the sample
6369 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006370 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006371static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6372 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006373{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006374 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006375 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006376 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006377 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006378
Willy Tarreaube508f12016-03-10 11:47:01 +01006379 if (!stream)
6380 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006381
Willy Tarreau87b09662015-04-03 00:22:06 +02006382 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006383 * Lua context can be not initialized. This behavior
6384 * permits to save performances because a systematic
6385 * Lua initialization cause 5% performances loss.
6386 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006387 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006388 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006389 if (!stream->hlua) {
6390 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6391 return 0;
6392 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006393 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006394 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6395 return 0;
6396 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006397 }
6398
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006399 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006400 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006401
6402 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006403 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6404 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6405 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006406 else
6407 error = "critical error";
6408 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006409 return 0;
6410 }
6411
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006412 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006413 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006414 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006415 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006416 return 0;
6417 }
6418
6419 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006420 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006421
6422 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006423 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006424 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006425 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006426 return 0;
6427 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006428 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006429
6430 /* push keywords in the stack. */
6431 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6432 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006433 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006434 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006435 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006436 return 0;
6437 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006438 hlua_arg2lua(stream->hlua->T, arg_p);
6439 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006440 }
6441
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006442 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006443 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006444
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006445 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006446 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006447 }
6448
6449 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006450 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006451 /* finished. */
6452 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006453 /* If the stack is empty, the function fails. */
6454 if (lua_gettop(stream->hlua->T) <= 0)
6455 return 0;
6456
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006457 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006458 hlua_lua2smp(stream->hlua->T, -1, smp);
6459 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006460
6461 /* Set the end of execution flag. */
6462 smp->flags &= ~SMP_F_MAY_CHANGE;
6463 return 1;
6464
6465 /* yield. */
6466 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006467 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006468 return 0;
6469
6470 /* finished with error. */
6471 case HLUA_E_ERRMSG:
6472 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006473 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006474 fcn->name, lua_tostring(stream->hlua->T, -1));
6475 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006476 return 0;
6477
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006478 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006479 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6480 return 0;
6481
6482 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006483 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6484 return 0;
6485
6486 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006487 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6488 return 0;
6489
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006490 case HLUA_E_ERR:
6491 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006492 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006493 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006494
6495 default:
6496 return 0;
6497 }
6498}
6499
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006500/* This function is an LUA binding used for registering
6501 * "sample-conv" functions. It expects a converter name used
6502 * in the haproxy configuration file, and an LUA function.
6503 */
6504__LJMP static int hlua_register_converters(lua_State *L)
6505{
6506 struct sample_conv_kw_list *sck;
6507 const char *name;
6508 int ref;
6509 int len;
6510 struct hlua_function *fcn;
6511
6512 MAY_LJMP(check_args(L, 2, "register_converters"));
6513
6514 /* First argument : converter name. */
6515 name = MAY_LJMP(luaL_checkstring(L, 1));
6516
6517 /* Second argument : lua function. */
6518 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6519
6520 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006521 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006522 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006523 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006524 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006525 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006526 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006527
6528 /* Fill fcn. */
6529 fcn->name = strdup(name);
6530 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006531 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006532 fcn->function_ref = ref;
6533
6534 /* List head */
6535 sck->list.n = sck->list.p = NULL;
6536
6537 /* converter keyword. */
6538 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006539 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006540 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006541 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006542
6543 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6544 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006545 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 +01006546 sck->kw[0].val_args = NULL;
6547 sck->kw[0].in_type = SMP_T_STR;
6548 sck->kw[0].out_type = SMP_T_STR;
6549 sck->kw[0].private = fcn;
6550
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006551 /* Register this new converter */
6552 sample_register_convs(sck);
6553
6554 return 0;
6555}
6556
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006557/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006558 * "sample-fetch" functions. It expects a converter name used
6559 * in the haproxy configuration file, and an LUA function.
6560 */
6561__LJMP static int hlua_register_fetches(lua_State *L)
6562{
6563 const char *name;
6564 int ref;
6565 int len;
6566 struct sample_fetch_kw_list *sfk;
6567 struct hlua_function *fcn;
6568
6569 MAY_LJMP(check_args(L, 2, "register_fetches"));
6570
6571 /* First argument : sample-fetch name. */
6572 name = MAY_LJMP(luaL_checkstring(L, 1));
6573
6574 /* Second argument : lua function. */
6575 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6576
6577 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006578 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006579 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006580 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006581 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006582 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006583 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006584
6585 /* Fill fcn. */
6586 fcn->name = strdup(name);
6587 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006588 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006589 fcn->function_ref = ref;
6590
6591 /* List head */
6592 sfk->list.n = sfk->list.p = NULL;
6593
6594 /* sample-fetch keyword. */
6595 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006596 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006597 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006598 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006599
6600 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6601 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006602 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 +01006603 sfk->kw[0].val_args = NULL;
6604 sfk->kw[0].out_type = SMP_T_STR;
6605 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6606 sfk->kw[0].val = 0;
6607 sfk->kw[0].private = fcn;
6608
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006609 /* Register this new fetch. */
6610 sample_register_fetches(sfk);
6611
6612 return 0;
6613}
6614
Christopher Faulet501465d2020-02-26 14:54:16 +01006615/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006616 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006617__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006618{
6619 struct hlua *hlua = hlua_gethlua(L);
6620 unsigned int delay;
6621 unsigned int wakeup_ms;
6622
6623 MAY_LJMP(check_args(L, 1, "wake_time"));
6624
6625 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6626 wakeup_ms = tick_add(now_ms, delay);
6627 hlua->wake_time = wakeup_ms;
6628 return 0;
6629}
6630
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006631/* This function is a wrapper to execute each LUA function declared as an action
6632 * wrapper during the initialisation period. This function may return any
6633 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6634 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6635 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006636 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006637static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006638 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006639{
6640 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006641 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006642 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006643 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006644
6645 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006646 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6647 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6648 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6649 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006650 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006651 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006652 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006653 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006654
Willy Tarreau87b09662015-04-03 00:22:06 +02006655 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006656 * Lua context can be not initialized. This behavior
6657 * permits to save performances because a systematic
6658 * Lua initialization cause 5% performances loss.
6659 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006660 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006661 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006662 if (!s->hlua) {
6663 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6664 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006665 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006666 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006667 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006668 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6669 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006670 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006671 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006672 }
6673
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006674 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006675 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006676
6677 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006678 if (!SET_SAFE_LJMP(s->hlua->T)) {
6679 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6680 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006681 else
6682 error = "critical error";
6683 SEND_ERR(px, "Lua function '%s': %s.\n",
6684 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006685 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006686 }
6687
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006688 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006689 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006690 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006691 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006692 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006693 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006694 }
6695
6696 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006697 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006698
Willy Tarreau87b09662015-04-03 00:22:06 +02006699 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006700 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006701 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006702 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006703 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006704 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006705 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006706 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006707
6708 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006709 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006710 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006711 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006712 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006713 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006714 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006715 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006716 lua_pushstring(s->hlua->T, *arg);
6717 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006718 }
6719
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006720 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006721 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006722
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006723 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006724 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006725 }
6726
6727 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006728 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006729 /* finished. */
6730 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006731 /* Catch the return value */
6732 if (lua_gettop(s->hlua->T) > 0)
6733 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006734
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006735 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006736 if (act_ret == ACT_RET_YIELD) {
6737 if (flags & ACT_OPT_FINAL)
6738 goto err_yield;
6739
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006740 if (dir == SMP_OPT_DIR_REQ)
6741 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6742 s->hlua->wake_time);
6743 else
6744 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6745 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006746 }
6747 goto end;
6748
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006749 /* yield. */
6750 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006751 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006752 if (dir == SMP_OPT_DIR_REQ)
6753 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6754 s->hlua->wake_time);
6755 else
6756 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6757 s->hlua->wake_time);
6758
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006759 /* Some actions can be wake up when a "write" event
6760 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006761 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006762 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006763 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006764 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006765 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006766 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006767 act_ret = ACT_RET_YIELD;
6768 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006769
6770 /* finished with error. */
6771 case HLUA_E_ERRMSG:
6772 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006773 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006774 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6775 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006776 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006777
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006778 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006779 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006780 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006781
6782 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006783 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006784 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006785
6786 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006787 err_yield:
6788 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006789 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6790 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006791 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006792
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006793 case HLUA_E_ERR:
6794 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006795 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006796 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006797
6798 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006799 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006800 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006801
6802 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006803 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006804 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006805 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006806}
6807
Olivier Houchard9f6af332018-05-25 14:04:04 +02006808struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006809{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006810 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006811
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006812 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006813 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006814 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006815}
6816
6817static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6818{
6819 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006820 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006821 struct task *task;
6822 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006823 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006824
Willy Tarreaubafbe012017-11-24 17:34:44 +01006825 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006826 if (!hlua) {
6827 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6828 ctx->rule->arg.hlua_rule->fcn.name);
6829 return 0;
6830 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006831 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006832 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006833 ctx->ctx.hlua_apptcp.flags = 0;
6834
6835 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006836 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006837 if (!task) {
6838 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6839 ctx->rule->arg.hlua_rule->fcn.name);
6840 return 0;
6841 }
6842 task->nice = 0;
6843 task->context = ctx;
6844 task->process = hlua_applet_wakeup;
6845 ctx->ctx.hlua_apptcp.task = task;
6846
6847 /* In the execution wrappers linked with a stream, the
6848 * Lua context can be not initialized. This behavior
6849 * permits to save performances because a systematic
6850 * Lua initialization cause 5% performances loss.
6851 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006852 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006853 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6854 ctx->rule->arg.hlua_rule->fcn.name);
6855 return 0;
6856 }
6857
6858 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006859 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006860
6861 /* The following Lua calls can fail. */
6862 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006863 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6864 error = lua_tostring(hlua->T, -1);
6865 else
6866 error = "critical error";
6867 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6868 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006869 return 0;
6870 }
6871
6872 /* Check stack available size. */
6873 if (!lua_checkstack(hlua->T, 1)) {
6874 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6875 ctx->rule->arg.hlua_rule->fcn.name);
6876 RESET_SAFE_LJMP(hlua->T);
6877 return 0;
6878 }
6879
6880 /* Restore the function in the stack. */
6881 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6882
6883 /* Create and and push object stream in the stack. */
6884 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6885 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6886 ctx->rule->arg.hlua_rule->fcn.name);
6887 RESET_SAFE_LJMP(hlua->T);
6888 return 0;
6889 }
6890 hlua->nargs = 1;
6891
6892 /* push keywords in the stack. */
6893 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6894 if (!lua_checkstack(hlua->T, 1)) {
6895 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6896 ctx->rule->arg.hlua_rule->fcn.name);
6897 RESET_SAFE_LJMP(hlua->T);
6898 return 0;
6899 }
6900 lua_pushstring(hlua->T, *arg);
6901 hlua->nargs++;
6902 }
6903
6904 RESET_SAFE_LJMP(hlua->T);
6905
6906 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006907 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006908 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006909
6910 return 1;
6911}
6912
Willy Tarreau60409db2019-08-21 14:14:50 +02006913void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006914{
6915 struct stream_interface *si = ctx->owner;
6916 struct stream *strm = si_strm(si);
6917 struct channel *res = si_ic(si);
6918 struct act_rule *rule = ctx->rule;
6919 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006920 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006921
6922 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006923 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6924 /* eat the whole request */
6925 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006926 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006927 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006928
6929 /* If the stream is disconnect or closed, ldo nothing. */
6930 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6931 return;
6932
6933 /* Execute the function. */
6934 switch (hlua_ctx_resume(hlua, 1)) {
6935 /* finished. */
6936 case HLUA_E_OK:
6937 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6938
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006939 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006940 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006941 res->flags |= CF_READ_NULL;
6942 si_shutr(si);
6943 return;
6944
6945 /* yield. */
6946 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006947 if (hlua->wake_time != TICK_ETERNITY)
6948 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006949 return;
6950
6951 /* finished with error. */
6952 case HLUA_E_ERRMSG:
6953 /* Display log. */
6954 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6955 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6956 lua_pop(hlua->T, 1);
6957 goto error;
6958
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006959 case HLUA_E_ETMOUT:
6960 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6961 rule->arg.hlua_rule->fcn.name);
6962 goto error;
6963
6964 case HLUA_E_NOMEM:
6965 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6966 rule->arg.hlua_rule->fcn.name);
6967 goto error;
6968
6969 case HLUA_E_YIELD: /* unexpected */
6970 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6971 rule->arg.hlua_rule->fcn.name);
6972 goto error;
6973
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006974 case HLUA_E_ERR:
6975 /* Display log. */
6976 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6977 rule->arg.hlua_rule->fcn.name);
6978 goto error;
6979
6980 default:
6981 goto error;
6982 }
6983
6984error:
6985
6986 /* For all other cases, just close the stream. */
6987 si_shutw(si);
6988 si_shutr(si);
6989 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6990}
6991
6992static void hlua_applet_tcp_release(struct appctx *ctx)
6993{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006994 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006995 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006996 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006997 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006998}
6999
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007000/* The function returns 1 if the initialisation is complete, 0 if
7001 * an errors occurs and -1 if more data are required for initializing
7002 * the applet.
7003 */
7004static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7005{
7006 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007007 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007008 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007009 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007010 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007011 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007012
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007013 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007014 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007015 if (!hlua) {
7016 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7017 ctx->rule->arg.hlua_rule->fcn.name);
7018 return 0;
7019 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007020 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007021 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007022 ctx->ctx.hlua_apphttp.left_bytes = -1;
7023 ctx->ctx.hlua_apphttp.flags = 0;
7024
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007025 if (txn->req.flags & HTTP_MSGF_VER_11)
7026 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7027
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007028 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007029 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007030 if (!task) {
7031 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7032 ctx->rule->arg.hlua_rule->fcn.name);
7033 return 0;
7034 }
7035 task->nice = 0;
7036 task->context = ctx;
7037 task->process = hlua_applet_wakeup;
7038 ctx->ctx.hlua_apphttp.task = task;
7039
7040 /* In the execution wrappers linked with a stream, the
7041 * Lua context can be not initialized. This behavior
7042 * permits to save performances because a systematic
7043 * Lua initialization cause 5% performances loss.
7044 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007045 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007046 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7047 ctx->rule->arg.hlua_rule->fcn.name);
7048 return 0;
7049 }
7050
7051 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007052 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007053
7054 /* The following Lua calls can fail. */
7055 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007056 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7057 error = lua_tostring(hlua->T, -1);
7058 else
7059 error = "critical error";
7060 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7061 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007062 return 0;
7063 }
7064
7065 /* Check stack available size. */
7066 if (!lua_checkstack(hlua->T, 1)) {
7067 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7068 ctx->rule->arg.hlua_rule->fcn.name);
7069 RESET_SAFE_LJMP(hlua->T);
7070 return 0;
7071 }
7072
7073 /* Restore the function in the stack. */
7074 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7075
7076 /* Create and and push object stream in the stack. */
7077 if (!hlua_applet_http_new(hlua->T, ctx)) {
7078 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7079 ctx->rule->arg.hlua_rule->fcn.name);
7080 RESET_SAFE_LJMP(hlua->T);
7081 return 0;
7082 }
7083 hlua->nargs = 1;
7084
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007085 /* push keywords in the stack. */
7086 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7087 if (!lua_checkstack(hlua->T, 1)) {
7088 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7089 ctx->rule->arg.hlua_rule->fcn.name);
7090 RESET_SAFE_LJMP(hlua->T);
7091 return 0;
7092 }
7093 lua_pushstring(hlua->T, *arg);
7094 hlua->nargs++;
7095 }
7096
7097 RESET_SAFE_LJMP(hlua->T);
7098
7099 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007100 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007101
7102 return 1;
7103}
7104
Willy Tarreau60409db2019-08-21 14:14:50 +02007105void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007106{
7107 struct stream_interface *si = ctx->owner;
7108 struct stream *strm = si_strm(si);
7109 struct channel *req = si_oc(si);
7110 struct channel *res = si_ic(si);
7111 struct act_rule *rule = ctx->rule;
7112 struct proxy *px = strm->be;
7113 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7114 struct htx *req_htx, *res_htx;
7115
7116 res_htx = htx_from_buf(&res->buf);
7117
7118 /* If the stream is disconnect or closed, ldo nothing. */
7119 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7120 goto out;
7121
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007122 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007123 if (!b_size(&res->buf)) {
7124 si_rx_room_blk(si);
7125 goto out;
7126 }
7127 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007128 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007129 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7130
7131 /* Set the currently running flag. */
7132 if (!HLUA_IS_RUNNING(hlua) &&
7133 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7134 struct htx_blk *blk;
7135 size_t count = co_data(req);
7136
7137 if (!count) {
7138 si_cant_get(si);
7139 goto out;
7140 }
7141
7142 /* We need to flush the request header. This left the body for
7143 * the Lua.
7144 */
7145 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007146 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007147 while (count && blk) {
7148 enum htx_blk_type type = htx_get_blk_type(blk);
7149 uint32_t sz = htx_get_blksz(blk);
7150
7151 if (sz > count) {
7152 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007153 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007154 goto out;
7155 }
7156
7157 count -= sz;
7158 co_set_data(req, co_data(req) - sz);
7159 blk = htx_remove_blk(req_htx, blk);
7160
7161 if (type == HTX_BLK_EOH)
7162 break;
7163 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007164 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007165 }
7166
7167 /* Executes The applet if it is not done. */
7168 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7169
7170 /* Execute the function. */
7171 switch (hlua_ctx_resume(hlua, 1)) {
7172 /* finished. */
7173 case HLUA_E_OK:
7174 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7175 break;
7176
7177 /* yield. */
7178 case HLUA_E_AGAIN:
7179 if (hlua->wake_time != TICK_ETERNITY)
7180 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007181 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007182
7183 /* finished with error. */
7184 case HLUA_E_ERRMSG:
7185 /* Display log. */
7186 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7187 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7188 lua_pop(hlua->T, 1);
7189 goto error;
7190
7191 case HLUA_E_ETMOUT:
7192 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7193 rule->arg.hlua_rule->fcn.name);
7194 goto error;
7195
7196 case HLUA_E_NOMEM:
7197 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7198 rule->arg.hlua_rule->fcn.name);
7199 goto error;
7200
7201 case HLUA_E_YIELD: /* unexpected */
7202 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7203 rule->arg.hlua_rule->fcn.name);
7204 goto error;
7205
7206 case HLUA_E_ERR:
7207 /* Display log. */
7208 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7209 rule->arg.hlua_rule->fcn.name);
7210 goto error;
7211
7212 default:
7213 goto error;
7214 }
7215 }
7216
7217 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007218 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7219 goto done;
7220
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007221 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7222 goto error;
7223
Christopher Faulet54b5e212019-06-04 10:08:28 +02007224 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007225 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007226 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7227 si_rx_room_blk(si);
7228 goto out;
7229 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007230 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007231 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7232 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007233 }
7234
7235 done:
7236 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007237 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007238 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007239 si_shutr(si);
7240 }
7241
7242 /* eat the whole request */
7243 if (co_data(req)) {
7244 req_htx = htx_from_buf(&req->buf);
7245 co_htx_skip(req, req_htx, co_data(req));
7246 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007247 }
7248 }
7249
7250 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007251 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007252 return;
7253
7254 error:
7255
7256 /* If we are in HTTP mode, and we are not send any
7257 * data, return a 500 server error in best effort:
7258 * if there is no room available in the buffer,
7259 * just close the connection.
7260 */
7261 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007262 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007263
7264 channel_erase(res);
7265 res->buf.data = b_data(err);
7266 memcpy(res->buf.area, b_head(err), b_data(err));
7267 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007268 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007269 }
7270 if (!(strm->flags & SF_ERR_MASK))
7271 strm->flags |= SF_ERR_RESOURCE;
7272 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7273 goto done;
7274}
7275
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007276static void hlua_applet_http_release(struct appctx *ctx)
7277{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007278 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007279 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007280 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007281 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007282}
7283
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007284/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007285 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007286 *
7287 * This function can fail with an abort() due to an Lua critical error.
7288 * We are in the configuration parsing process of HAProxy, this abort() is
7289 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007290 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007291static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7292 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007293{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007294 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007295 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007296
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007297 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007298 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007299 if (!rule->arg.hlua_rule) {
7300 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007301 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007302 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007303
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007304 /* Memory for arguments. */
7305 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7306 if (!rule->arg.hlua_rule->args) {
7307 memprintf(err, "out of memory error");
7308 return ACT_RET_PRS_ERR;
7309 }
7310
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007311 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007312 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007313
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007314 /* Expect some arguments */
7315 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007316 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007317 memprintf(err, "expect %d arguments", fcn->nargs);
7318 return ACT_RET_PRS_ERR;
7319 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007320 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007321 if (!rule->arg.hlua_rule->args[i]) {
7322 memprintf(err, "out of memory error");
7323 return ACT_RET_PRS_ERR;
7324 }
7325 (*cur_arg)++;
7326 }
7327 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007328
Thierry FOURNIER42148732015-09-02 17:17:33 +02007329 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007330 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007331 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007332}
7333
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007334static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7335 struct act_rule *rule, char **err)
7336{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007337 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007338
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007339 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007340 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007341 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007342 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007343 * the call of this analyzer.
7344 */
7345 if (rule->from != ACT_F_HTTP_REQ) {
7346 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7347 return ACT_RET_PRS_ERR;
7348 }
7349
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007350 /* Memory for the rule. */
7351 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7352 if (!rule->arg.hlua_rule) {
7353 memprintf(err, "out of memory error");
7354 return ACT_RET_PRS_ERR;
7355 }
7356
7357 /* Reference the Lua function and store the reference. */
7358 rule->arg.hlua_rule->fcn = *fcn;
7359
7360 /* TODO: later accept arguments. */
7361 rule->arg.hlua_rule->args = NULL;
7362
7363 /* Add applet pointer in the rule. */
7364 rule->applet.obj_type = OBJ_TYPE_APPLET;
7365 rule->applet.name = fcn->name;
7366 rule->applet.init = hlua_applet_http_init;
7367 rule->applet.fct = hlua_applet_http_fct;
7368 rule->applet.release = hlua_applet_http_release;
7369 rule->applet.timeout = hlua_timeout_applet;
7370
7371 return ACT_RET_PRS_OK;
7372}
7373
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007374/* This function is an LUA binding used for registering
7375 * "sample-conv" functions. It expects a converter name used
7376 * in the haproxy configuration file, and an LUA function.
7377 */
7378__LJMP static int hlua_register_action(lua_State *L)
7379{
7380 struct action_kw_list *akl;
7381 const char *name;
7382 int ref;
7383 int len;
7384 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007385 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007386
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007387 /* Initialise the number of expected arguments at 0. */
7388 nargs = 0;
7389
7390 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7391 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007392
7393 /* First argument : converter name. */
7394 name = MAY_LJMP(luaL_checkstring(L, 1));
7395
7396 /* Second argument : environment. */
7397 if (lua_type(L, 2) != LUA_TTABLE)
7398 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7399
7400 /* Third argument : lua function. */
7401 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7402
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007403 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007404 if (lua_gettop(L) >= 4)
7405 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7406
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007407 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007408 lua_pushnil(L);
7409 while (lua_next(L, 2) != 0) {
7410 if (lua_type(L, -1) != LUA_TSTRING)
7411 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7412
7413 /* Check required environment. Only accepted "http" or "tcp". */
7414 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007415 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007416 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007417 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007418 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007419 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007420 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007421
7422 /* Fill fcn. */
7423 fcn->name = strdup(name);
7424 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007425 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007426 fcn->function_ref = ref;
7427
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007428 /* Set the expected number od arguments. */
7429 fcn->nargs = nargs;
7430
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007431 /* List head */
7432 akl->list.n = akl->list.p = NULL;
7433
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007434 /* action keyword. */
7435 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007436 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007437 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007438 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007439
7440 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7441
7442 akl->kw[0].match_pfx = 0;
7443 akl->kw[0].private = fcn;
7444 akl->kw[0].parse = action_register_lua;
7445
7446 /* select the action registering point. */
7447 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7448 tcp_req_cont_keywords_register(akl);
7449 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7450 tcp_res_cont_keywords_register(akl);
7451 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7452 http_req_keywords_register(akl);
7453 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7454 http_res_keywords_register(akl);
7455 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007456 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007457 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7458 "are expected.", lua_tostring(L, -1)));
7459
7460 /* pop the environment string. */
7461 lua_pop(L, 1);
7462 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007463 return ACT_RET_PRS_OK;
7464}
7465
7466static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7467 struct act_rule *rule, char **err)
7468{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007469 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007470
Christopher Faulet280f85b2019-07-15 15:02:04 +02007471 if (px->mode == PR_MODE_HTTP) {
7472 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007473 return ACT_RET_PRS_ERR;
7474 }
7475
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007476 /* Memory for the rule. */
7477 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7478 if (!rule->arg.hlua_rule) {
7479 memprintf(err, "out of memory error");
7480 return ACT_RET_PRS_ERR;
7481 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007482
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007483 /* Reference the Lua function and store the reference. */
7484 rule->arg.hlua_rule->fcn = *fcn;
7485
7486 /* TODO: later accept arguments. */
7487 rule->arg.hlua_rule->args = NULL;
7488
7489 /* Add applet pointer in the rule. */
7490 rule->applet.obj_type = OBJ_TYPE_APPLET;
7491 rule->applet.name = fcn->name;
7492 rule->applet.init = hlua_applet_tcp_init;
7493 rule->applet.fct = hlua_applet_tcp_fct;
7494 rule->applet.release = hlua_applet_tcp_release;
7495 rule->applet.timeout = hlua_timeout_applet;
7496
7497 return 0;
7498}
7499
7500/* This function is an LUA binding used for registering
7501 * "sample-conv" functions. It expects a converter name used
7502 * in the haproxy configuration file, and an LUA function.
7503 */
7504__LJMP static int hlua_register_service(lua_State *L)
7505{
7506 struct action_kw_list *akl;
7507 const char *name;
7508 const char *env;
7509 int ref;
7510 int len;
7511 struct hlua_function *fcn;
7512
7513 MAY_LJMP(check_args(L, 3, "register_service"));
7514
7515 /* First argument : converter name. */
7516 name = MAY_LJMP(luaL_checkstring(L, 1));
7517
7518 /* Second argument : environment. */
7519 env = MAY_LJMP(luaL_checkstring(L, 2));
7520
7521 /* Third argument : lua function. */
7522 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7523
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007524 /* Allocate and fill the sample fetch keyword struct. */
7525 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7526 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007527 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007528 fcn = calloc(1, sizeof(*fcn));
7529 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007530 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007531
7532 /* Fill fcn. */
7533 len = strlen("<lua.>") + strlen(name) + 1;
7534 fcn->name = calloc(1, len);
7535 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007536 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007537 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7538 fcn->function_ref = ref;
7539
7540 /* List head */
7541 akl->list.n = akl->list.p = NULL;
7542
7543 /* converter keyword. */
7544 len = strlen("lua.") + strlen(name) + 1;
7545 akl->kw[0].kw = calloc(1, len);
7546 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007547 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007548
7549 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7550
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007551 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007552 if (strcmp(env, "tcp") == 0)
7553 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007554 else if (strcmp(env, "http") == 0)
7555 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007556 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007557 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007558 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007559
7560 akl->kw[0].match_pfx = 0;
7561 akl->kw[0].private = fcn;
7562
7563 /* End of array. */
7564 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7565
7566 /* Register this new converter */
7567 service_keywords_register(akl);
7568
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007569 return 0;
7570}
7571
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007572/* This function initialises Lua cli handler. It copies the
7573 * arguments in the Lua stack and create channel IO objects.
7574 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007575static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007576{
7577 struct hlua *hlua;
7578 struct hlua_function *fcn;
7579 int i;
7580 const char *error;
7581
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007582 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007583 appctx->ctx.hlua_cli.fcn = private;
7584
Willy Tarreaubafbe012017-11-24 17:34:44 +01007585 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007586 if (!hlua) {
7587 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007588 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007589 }
7590 HLUA_INIT(hlua);
7591 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007592
7593 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007594 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007595 * applet_http. It is absolutely compatible.
7596 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007597 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007598 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007599 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007600 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007601 }
7602 appctx->ctx.hlua_cli.task->nice = 0;
7603 appctx->ctx.hlua_cli.task->context = appctx;
7604 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7605
7606 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007607 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007608 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007609 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007610 }
7611
7612 /* The following Lua calls can fail. */
7613 if (!SET_SAFE_LJMP(hlua->T)) {
7614 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7615 error = lua_tostring(hlua->T, -1);
7616 else
7617 error = "critical error";
7618 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7619 goto error;
7620 }
7621
7622 /* Check stack available size. */
7623 if (!lua_checkstack(hlua->T, 2)) {
7624 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7625 goto error;
7626 }
7627
7628 /* Restore the function in the stack. */
7629 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7630
7631 /* Once the arguments parsed, the CLI is like an AppletTCP,
7632 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007633 */
7634 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7635 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7636 goto error;
7637 }
7638 hlua->nargs = 1;
7639
7640 /* push keywords in the stack. */
7641 for (i = 0; *args[i]; i++) {
7642 /* Check stack available size. */
7643 if (!lua_checkstack(hlua->T, 1)) {
7644 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7645 goto error;
7646 }
7647 lua_pushstring(hlua->T, args[i]);
7648 hlua->nargs++;
7649 }
7650
7651 /* We must initialize the execution timeouts. */
7652 hlua->max_time = hlua_timeout_session;
7653
7654 /* At this point the execution is safe. */
7655 RESET_SAFE_LJMP(hlua->T);
7656
7657 /* It's ok */
7658 return 0;
7659
7660 /* It's not ok. */
7661error:
7662 RESET_SAFE_LJMP(hlua->T);
7663 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007664 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007665 return 1;
7666}
7667
7668static int hlua_cli_io_handler_fct(struct appctx *appctx)
7669{
7670 struct hlua *hlua;
7671 struct stream_interface *si;
7672 struct hlua_function *fcn;
7673
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007674 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007675 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007676 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007677
7678 /* If the stream is disconnect or closed, ldo nothing. */
7679 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7680 return 1;
7681
7682 /* Execute the function. */
7683 switch (hlua_ctx_resume(hlua, 1)) {
7684
7685 /* finished. */
7686 case HLUA_E_OK:
7687 return 1;
7688
7689 /* yield. */
7690 case HLUA_E_AGAIN:
7691 /* We want write. */
7692 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007693 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007694 /* Set the timeout. */
7695 if (hlua->wake_time != TICK_ETERNITY)
7696 task_schedule(hlua->task, hlua->wake_time);
7697 return 0;
7698
7699 /* finished with error. */
7700 case HLUA_E_ERRMSG:
7701 /* Display log. */
7702 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7703 fcn->name, lua_tostring(hlua->T, -1));
7704 lua_pop(hlua->T, 1);
7705 return 1;
7706
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007707 case HLUA_E_ETMOUT:
7708 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7709 fcn->name);
7710 return 1;
7711
7712 case HLUA_E_NOMEM:
7713 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7714 fcn->name);
7715 return 1;
7716
7717 case HLUA_E_YIELD: /* unexpected */
7718 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7719 fcn->name);
7720 return 1;
7721
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007722 case HLUA_E_ERR:
7723 /* Display log. */
7724 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7725 fcn->name);
7726 return 1;
7727
7728 default:
7729 return 1;
7730 }
7731
7732 return 1;
7733}
7734
7735static void hlua_cli_io_release_fct(struct appctx *appctx)
7736{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007737 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007738 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007739}
7740
7741/* This function is an LUA binding used for registering
7742 * new keywords in the cli. It expects a list of keywords
7743 * which are the "path". It is limited to 5 keywords. A
7744 * description of the command, a function to be executed
7745 * for the parsing and a function for io handlers.
7746 */
7747__LJMP static int hlua_register_cli(lua_State *L)
7748{
7749 struct cli_kw_list *cli_kws;
7750 const char *message;
7751 int ref_io;
7752 int len;
7753 struct hlua_function *fcn;
7754 int index;
7755 int i;
7756
7757 MAY_LJMP(check_args(L, 3, "register_cli"));
7758
7759 /* First argument : an array of maximum 5 keywords. */
7760 if (!lua_istable(L, 1))
7761 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7762
7763 /* Second argument : string with contextual message. */
7764 message = MAY_LJMP(luaL_checkstring(L, 2));
7765
7766 /* Third and fourth argument : lua function. */
7767 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7768
7769 /* Allocate and fill the sample fetch keyword struct. */
7770 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7771 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007772 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007773 fcn = calloc(1, sizeof(*fcn));
7774 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007775 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007776
7777 /* Fill path. */
7778 index = 0;
7779 lua_pushnil(L);
7780 while(lua_next(L, 1) != 0) {
7781 if (index >= 5)
7782 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7783 if (lua_type(L, -1) != LUA_TSTRING)
7784 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7785 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7786 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007787 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007788 index++;
7789 lua_pop(L, 1);
7790 }
7791
7792 /* Copy help message. */
7793 cli_kws->kw[0].usage = strdup(message);
7794 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007795 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007796
7797 /* Fill fcn io handler. */
7798 len = strlen("<lua.cli>") + 1;
7799 for (i = 0; i < index; i++)
7800 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7801 fcn->name = calloc(1, len);
7802 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007803 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007804 strncat((char *)fcn->name, "<lua.cli", len);
7805 for (i = 0; i < index; i++) {
7806 strncat((char *)fcn->name, ".", len);
7807 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7808 }
7809 strncat((char *)fcn->name, ">", len);
7810 fcn->function_ref = ref_io;
7811
7812 /* Fill last entries. */
7813 cli_kws->kw[0].private = fcn;
7814 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7815 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7816 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7817
7818 /* Register this new converter */
7819 cli_register_kw(cli_kws);
7820
7821 return 0;
7822}
7823
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007824static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7825 struct proxy *defpx, const char *file, int line,
7826 char **err, unsigned int *timeout)
7827{
7828 const char *error;
7829
7830 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007831 if (error == PARSE_TIME_OVER) {
7832 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7833 args[1], args[0]);
7834 return -1;
7835 }
7836 else if (error == PARSE_TIME_UNDER) {
7837 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7838 args[1], args[0]);
7839 return -1;
7840 }
7841 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007842 memprintf(err, "%s: invalid timeout", args[0]);
7843 return -1;
7844 }
7845 return 0;
7846}
7847
7848static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7849 struct proxy *defpx, const char *file, int line,
7850 char **err)
7851{
7852 return hlua_read_timeout(args, section_type, curpx, defpx,
7853 file, line, err, &hlua_timeout_session);
7854}
7855
7856static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7857 struct proxy *defpx, const char *file, int line,
7858 char **err)
7859{
7860 return hlua_read_timeout(args, section_type, curpx, defpx,
7861 file, line, err, &hlua_timeout_task);
7862}
7863
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007864static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7865 struct proxy *defpx, const char *file, int line,
7866 char **err)
7867{
7868 return hlua_read_timeout(args, section_type, curpx, defpx,
7869 file, line, err, &hlua_timeout_applet);
7870}
7871
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007872static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7873 struct proxy *defpx, const char *file, int line,
7874 char **err)
7875{
7876 char *error;
7877
7878 hlua_nb_instruction = strtoll(args[1], &error, 10);
7879 if (*error != '\0') {
7880 memprintf(err, "%s: invalid number", args[0]);
7881 return -1;
7882 }
7883 return 0;
7884}
7885
Willy Tarreau32f61e22015-03-18 17:54:59 +01007886static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7887 struct proxy *defpx, const char *file, int line,
7888 char **err)
7889{
7890 char *error;
7891
7892 if (*(args[1]) == 0) {
7893 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7894 return -1;
7895 }
7896 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7897 if (*error != '\0') {
7898 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7899 return -1;
7900 }
7901 return 0;
7902}
7903
7904
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007905/* This function is called by the main configuration key "lua-load". It loads and
7906 * execute an lua file during the parsing of the HAProxy configuration file. It is
7907 * the main lua entry point.
7908 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007909 * This function runs with the HAProxy keywords API. It returns -1 if an error
7910 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007911 *
7912 * In some error case, LUA set an error message in top of the stack. This function
7913 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007914 *
7915 * This function can fail with an abort() due to an Lua critical error.
7916 * We are in the configuration parsing process of HAProxy, this abort() is
7917 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007918 */
7919static int hlua_load(char **args, int section_type, struct proxy *curpx,
7920 struct proxy *defpx, const char *file, int line,
7921 char **err)
7922{
7923 int error;
7924
7925 /* Just load and compile the file. */
7926 error = luaL_loadfile(gL.T, args[1]);
7927 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007928 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007929 lua_pop(gL.T, 1);
7930 return -1;
7931 }
7932
7933 /* If no syntax error where detected, execute the code. */
7934 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7935 switch (error) {
7936 case LUA_OK:
7937 break;
7938 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007939 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007940 lua_pop(gL.T, 1);
7941 return -1;
7942 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007943 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007944 return -1;
7945 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007946 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007947 lua_pop(gL.T, 1);
7948 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007949#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007950 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007951 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007952 lua_pop(gL.T, 1);
7953 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007954#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007955 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007956 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007957 lua_pop(gL.T, 1);
7958 return -1;
7959 }
7960
7961 return 0;
7962}
7963
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007964/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7965 * in the given <ctx>.
7966 */
7967static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7968{
7969 lua_getglobal(ctx.T, "package"); /* push package variable */
7970 lua_pushstring(ctx.T, path); /* push given path */
7971 lua_pushstring(ctx.T, ";"); /* push semicolon */
7972 lua_getfield(ctx.T, -3, type); /* push old path */
7973 lua_concat(ctx.T, 3); /* concatenate to new path */
7974 lua_setfield(ctx.T, -2, type); /* store new path */
7975 lua_pop(ctx.T, 1); /* pop package variable */
7976
7977 return 0;
7978}
7979
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007980static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7981 struct proxy *defpx, const char *file, int line,
7982 char **err)
7983{
7984 char *path;
7985 char *type = "path";
7986 if (too_many_args(2, args, err, NULL)) {
7987 return -1;
7988 }
7989
7990 if (!(*args[1])) {
7991 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7992 return -1;
7993 }
7994 path = args[1];
7995
7996 if (*args[2]) {
7997 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7998 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7999 return -1;
8000 }
8001 type = args[2];
8002 }
8003
8004 return hlua_prepend_path(gL, type, path);
8005}
8006
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008007/* configuration keywords declaration */
8008static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008009 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008010 { CFG_GLOBAL, "lua-load", hlua_load },
8011 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8012 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008013 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008014 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008015 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008016 { 0, NULL, NULL },
8017}};
8018
Willy Tarreau0108d902018-11-25 19:14:37 +01008019INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8020
Christopher Fauletafd8f102018-11-08 11:34:21 +01008021
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008022/* This function can fail with an abort() due to an Lua critical error.
8023 * We are in the initialisation process of HAProxy, this abort() is
8024 * tolerated.
8025 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008026int hlua_post_init()
8027{
8028 struct hlua_init_function *init;
8029 const char *msg;
8030 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008031 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008032
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008033 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008034 if (!SET_SAFE_LJMP(gL.T)) {
8035 if (lua_type(gL.T, -1) == LUA_TSTRING)
8036 error = lua_tostring(gL.T, -1);
8037 else
8038 error = "critical error";
8039 fprintf(stderr, "Lua post-init: %s.\n", error);
8040 exit(1);
8041 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008042
8043#if USE_OPENSSL
8044 /* Initialize SSL server. */
8045 if (socket_ssl.xprt->prepare_srv) {
8046 int saved_used_backed = global.ssl_used_backend;
8047 // don't affect maxconn automatic computation
8048 socket_ssl.xprt->prepare_srv(&socket_ssl);
8049 global.ssl_used_backend = saved_used_backed;
8050 }
8051#endif
8052
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008053 hlua_fcn_post_init(gL.T);
8054 RESET_SAFE_LJMP(gL.T);
8055
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008056 list_for_each_entry(init, &hlua_init_functions, l) {
8057 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8058 ret = hlua_ctx_resume(&gL, 0);
8059 switch (ret) {
8060 case HLUA_E_OK:
8061 lua_pop(gL.T, -1);
8062 return 1;
8063 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008064 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008065 return 0;
8066 case HLUA_E_ERRMSG:
8067 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008068 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008069 return 0;
8070 case HLUA_E_ERR:
8071 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008072 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008073 return 0;
8074 }
8075 }
8076 return 1;
8077}
8078
Willy Tarreau32f61e22015-03-18 17:54:59 +01008079/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8080 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8081 * is the previously allocated size or the kind of object in case of a new
8082 * allocation. <nsize> is the requested new size.
8083 */
8084static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8085{
8086 struct hlua_mem_allocator *zone = ud;
8087
8088 if (nsize == 0) {
8089 /* it's a free */
8090 if (ptr)
8091 zone->allocated -= osize;
8092 free(ptr);
8093 return NULL;
8094 }
8095
8096 if (!ptr) {
8097 /* it's a new allocation */
8098 if (zone->limit && zone->allocated + nsize > zone->limit)
8099 return NULL;
8100
8101 ptr = malloc(nsize);
8102 if (ptr)
8103 zone->allocated += nsize;
8104 return ptr;
8105 }
8106
8107 /* it's a realloc */
8108 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8109 return NULL;
8110
8111 ptr = realloc(ptr, nsize);
8112 if (ptr)
8113 zone->allocated += nsize - osize;
8114 return ptr;
8115}
8116
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008117/* Ithis function can fail with an abort() due to an Lua critical error.
8118 * We are in the initialisation process of HAProxy, this abort() is
8119 * tolerated.
8120 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008121void hlua_init(void)
8122{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008123 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008124 int idx;
8125 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008126 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008127 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008128 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008129#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008130 struct srv_kw *kw;
8131 int tmp_error;
8132 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008133 char *args[] = { /* SSL client configuration. */
8134 "ssl",
8135 "verify",
8136 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008137 NULL
8138 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008139#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008140
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008141 /* Init main lua stack. */
8142 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008143 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008144 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008145 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008146 hlua_sethlua(&gL);
8147 gL.Tref = LUA_REFNIL;
8148 gL.task = NULL;
8149
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008150 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008151 * the Lua function can fail with an abort. We are in the initialisation
8152 * process of HAProxy, this abort() is tolerated.
8153 */
8154
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008155 /* Initialise lua. */
8156 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008157#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8158#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8159#ifdef HLUA_PREPEND_PATH
8160 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8161#endif
8162#ifdef HLUA_PREPEND_CPATH
8163 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8164#endif
8165#undef HLUA_PREPEND_PATH_TOSTRING
8166#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008167
Thierry Fournier75933d42016-01-21 09:30:18 +01008168 /* Set safe environment for the initialisation. */
8169 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008170 if (lua_type(gL.T, -1) == LUA_TSTRING)
8171 error_msg = lua_tostring(gL.T, -1);
8172 else
8173 error_msg = "critical error";
8174 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008175 exit(1);
8176 }
8177
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008178 /*
8179 *
8180 * Create "core" object.
8181 *
8182 */
8183
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008184 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008185 lua_newtable(gL.T);
8186
8187 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008188 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008189 hlua_class_const_int(gL.T, log_levels[i], i);
8190
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008191 /* Register special functions. */
8192 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008193 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008194 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008195 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008196 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008197 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008198 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008199 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008200 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008201 hlua_class_function(gL.T, "sleep", hlua_sleep);
8202 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008203 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8204 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8205 hlua_class_function(gL.T, "set_map", hlua_set_map);
8206 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008207 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008208 hlua_class_function(gL.T, "log", hlua_log);
8209 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8210 hlua_class_function(gL.T, "Info", hlua_log_info);
8211 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8212 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008213 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008214 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008215
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008216 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008217
8218 /*
8219 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008220 * Create "act" object.
8221 *
8222 */
8223
8224 /* This table entry is the object "act" base. */
8225 lua_newtable(gL.T);
8226
8227 /* push action return constants */
8228 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8229 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8230 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8231 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8232 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8233 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8234 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8235 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8236
Christopher Faulet501465d2020-02-26 14:54:16 +01008237 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008238
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008239 lua_setglobal(gL.T, "act");
8240
8241 /*
8242 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008243 * Register class Map
8244 *
8245 */
8246
8247 /* This table entry is the object "Map" base. */
8248 lua_newtable(gL.T);
8249
8250 /* register pattern types. */
8251 for (i=0; i<PAT_MATCH_NUM; i++)
8252 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008253 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008254 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8255 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008256 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008257
8258 /* register constructor. */
8259 hlua_class_function(gL.T, "new", hlua_map_new);
8260
8261 /* Create and fill the metatable. */
8262 lua_newtable(gL.T);
8263
Ilya Shipitsind4259502020-04-08 01:07:56 +05008264 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008265 lua_pushstring(gL.T, "__index");
8266 lua_newtable(gL.T);
8267
8268 /* Register . */
8269 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8270 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8271
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008272 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008273
Thierry Fournier45e78d72016-02-19 18:34:46 +01008274 /* Register previous table in the registry with reference and named entry.
8275 * The function hlua_register_metatable() pops the stack, so we
8276 * previously create a copy of the table.
8277 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008278 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008279 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008280
8281 /* Assign the metatable to the mai Map object. */
8282 lua_setmetatable(gL.T, -2);
8283
8284 /* Set a name to the table. */
8285 lua_setglobal(gL.T, "Map");
8286
8287 /*
8288 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008289 * Register class Channel
8290 *
8291 */
8292
8293 /* Create and fill the metatable. */
8294 lua_newtable(gL.T);
8295
Ilya Shipitsind4259502020-04-08 01:07:56 +05008296 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008297 lua_pushstring(gL.T, "__index");
8298 lua_newtable(gL.T);
8299
8300 /* Register . */
8301 hlua_class_function(gL.T, "get", hlua_channel_get);
8302 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8303 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8304 hlua_class_function(gL.T, "set", hlua_channel_set);
8305 hlua_class_function(gL.T, "append", hlua_channel_append);
8306 hlua_class_function(gL.T, "send", hlua_channel_send);
8307 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8308 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8309 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008310 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008311 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008312
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008313 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008314
8315 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008316 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008317
8318 /*
8319 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008320 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008321 *
8322 */
8323
8324 /* Create and fill the metatable. */
8325 lua_newtable(gL.T);
8326
Ilya Shipitsind4259502020-04-08 01:07:56 +05008327 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008328 lua_pushstring(gL.T, "__index");
8329 lua_newtable(gL.T);
8330
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008331 /* Browse existing fetches and create the associated
8332 * object method.
8333 */
8334 sf = NULL;
8335 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8336
8337 /* Dont register the keywork if the arguments check function are
8338 * not safe during the runtime.
8339 */
8340 if ((sf->val_args != NULL) &&
8341 (sf->val_args != val_payload_lv) &&
8342 (sf->val_args != val_hdr))
8343 continue;
8344
8345 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8346 * by an underscore.
8347 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008348 strncpy(trash.area, sf->kw, trash.size);
8349 trash.area[trash.size - 1] = '\0';
8350 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008351 if (*p == '.' || *p == '-' || *p == '+')
8352 *p = '_';
8353
8354 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008355 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008356 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008357 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008358 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008359 }
8360
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008361 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008362
8363 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008364 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008365
8366 /*
8367 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008368 * Register class Converters
8369 *
8370 */
8371
8372 /* Create and fill the metatable. */
8373 lua_newtable(gL.T);
8374
8375 /* Create and fill the __index entry. */
8376 lua_pushstring(gL.T, "__index");
8377 lua_newtable(gL.T);
8378
8379 /* Browse existing converters and create the associated
8380 * object method.
8381 */
8382 sc = NULL;
8383 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8384 /* Dont register the keywork if the arguments check function are
8385 * not safe during the runtime.
8386 */
8387 if (sc->val_args != NULL)
8388 continue;
8389
8390 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8391 * by an underscore.
8392 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008393 strncpy(trash.area, sc->kw, trash.size);
8394 trash.area[trash.size - 1] = '\0';
8395 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008396 if (*p == '.' || *p == '-' || *p == '+')
8397 *p = '_';
8398
8399 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008400 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008401 lua_pushlightuserdata(gL.T, sc);
8402 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008403 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008404 }
8405
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008406 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008407
8408 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008409 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008410
8411 /*
8412 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008413 * Register class HTTP
8414 *
8415 */
8416
8417 /* Create and fill the metatable. */
8418 lua_newtable(gL.T);
8419
Ilya Shipitsind4259502020-04-08 01:07:56 +05008420 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008421 lua_pushstring(gL.T, "__index");
8422 lua_newtable(gL.T);
8423
8424 /* Register Lua functions. */
8425 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8426 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8427 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8428 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8429 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8430 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8431 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8432 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8433 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8434 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8435
8436 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8437 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8438 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8439 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8440 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8441 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008442 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008443
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008444 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008445
8446 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008447 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008448
8449 /*
8450 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008451 * Register class AppletTCP
8452 *
8453 */
8454
8455 /* Create and fill the metatable. */
8456 lua_newtable(gL.T);
8457
Ilya Shipitsind4259502020-04-08 01:07:56 +05008458 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008459 lua_pushstring(gL.T, "__index");
8460 lua_newtable(gL.T);
8461
8462 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008463 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8464 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8465 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8466 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8467 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008468 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8469 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8470 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008471
8472 lua_settable(gL.T, -3);
8473
8474 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008475 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008476
8477 /*
8478 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008479 * Register class AppletHTTP
8480 *
8481 */
8482
8483 /* Create and fill the metatable. */
8484 lua_newtable(gL.T);
8485
Ilya Shipitsind4259502020-04-08 01:07:56 +05008486 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008487 lua_pushstring(gL.T, "__index");
8488 lua_newtable(gL.T);
8489
8490 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008491 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8492 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008493 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8494 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8495 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008496 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8497 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8498 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8499 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8500 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8501 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8502
8503 lua_settable(gL.T, -3);
8504
8505 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008506 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008507
8508 /*
8509 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008510 * Register class TXN
8511 *
8512 */
8513
8514 /* Create and fill the metatable. */
8515 lua_newtable(gL.T);
8516
Ilya Shipitsind4259502020-04-08 01:07:56 +05008517 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008518 lua_pushstring(gL.T, "__index");
8519 lua_newtable(gL.T);
8520
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008521 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008522 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8523 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8524 hlua_class_function(gL.T, "set_var", hlua_set_var);
8525 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8526 hlua_class_function(gL.T, "get_var", hlua_get_var);
8527 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008528 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008529 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8530 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8531 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8532 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8533 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8534 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8535 hlua_class_function(gL.T, "log", hlua_txn_log);
8536 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8537 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8538 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8539 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008540
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008541 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008542
8543 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008544 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008545
8546 /*
8547 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008548 * Register class reply
8549 *
8550 */
8551 lua_newtable(gL.T);
8552 lua_pushstring(gL.T, "__index");
8553 lua_newtable(gL.T);
8554 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8555 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8556 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8557 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8558 lua_settable(gL.T, -3); /* Sets the __index entry. */
8559 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8560
8561
8562 /*
8563 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008564 * Register class Socket
8565 *
8566 */
8567
8568 /* Create and fill the metatable. */
8569 lua_newtable(gL.T);
8570
Ilya Shipitsind4259502020-04-08 01:07:56 +05008571 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008572 lua_pushstring(gL.T, "__index");
8573 lua_newtable(gL.T);
8574
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008575#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008576 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008577#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008578 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8579 hlua_class_function(gL.T, "send", hlua_socket_send);
8580 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8581 hlua_class_function(gL.T, "close", hlua_socket_close);
8582 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8583 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8584 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8585 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8586
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008587 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008588
8589 /* Register the garbage collector entry. */
8590 lua_pushstring(gL.T, "__gc");
8591 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008592 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008593
8594 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008595 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008596
8597 /* Proxy and server configuration initialisation. */
8598 memset(&socket_proxy, 0, sizeof(socket_proxy));
8599 init_new_proxy(&socket_proxy);
8600 socket_proxy.parent = NULL;
8601 socket_proxy.last_change = now.tv_sec;
8602 socket_proxy.id = "LUA-SOCKET";
8603 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8604 socket_proxy.maxconn = 0;
8605 socket_proxy.accept = NULL;
8606 socket_proxy.options2 |= PR_O2_INDEPSTR;
8607 socket_proxy.srv = NULL;
8608 socket_proxy.conn_retries = 0;
8609 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8610
8611 /* Init TCP server: unchanged parameters */
8612 memset(&socket_tcp, 0, sizeof(socket_tcp));
8613 socket_tcp.next = NULL;
8614 socket_tcp.proxy = &socket_proxy;
8615 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8616 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008617 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008618 socket_tcp.idle_conns = NULL;
8619 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008620 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008621 socket_tcp.last_change = 0;
8622 socket_tcp.id = "LUA-TCP-CONN";
8623 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8624 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8625 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8626
8627 /* XXX: Copy default parameter from default server,
8628 * but the default server is not initialized.
8629 */
8630 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8631 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8632 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8633 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8634 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8635 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8636 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8637 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8638 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8639 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8640
8641 socket_tcp.check.status = HCHK_STATUS_INI;
8642 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8643 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8644 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8645 socket_tcp.check.server = &socket_tcp;
8646
8647 socket_tcp.agent.status = HCHK_STATUS_INI;
8648 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8649 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8650 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8651 socket_tcp.agent.server = &socket_tcp;
8652
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008653 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008654
8655#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008656 /* Init TCP server: unchanged parameters */
8657 memset(&socket_ssl, 0, sizeof(socket_ssl));
8658 socket_ssl.next = NULL;
8659 socket_ssl.proxy = &socket_proxy;
8660 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8661 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008662 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008663 socket_ssl.idle_conns = NULL;
8664 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008665 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008666 socket_ssl.last_change = 0;
8667 socket_ssl.id = "LUA-SSL-CONN";
8668 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8669 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8670 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8671
8672 /* XXX: Copy default parameter from default server,
8673 * but the default server is not initialized.
8674 */
8675 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8676 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8677 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8678 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8679 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8680 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8681 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8682 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8683 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8684 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8685
8686 socket_ssl.check.status = HCHK_STATUS_INI;
8687 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8688 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8689 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8690 socket_ssl.check.server = &socket_ssl;
8691
8692 socket_ssl.agent.status = HCHK_STATUS_INI;
8693 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8694 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8695 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8696 socket_ssl.agent.server = &socket_ssl;
8697
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008698 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008699 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008700
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008701 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008702 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8703 /*
8704 *
8705 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008706 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008707 * features like client certificates and ssl_verify.
8708 *
8709 */
8710 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8711 if (tmp_error != 0) {
8712 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8713 abort(); /* This must be never arrives because the command line
8714 not editable by the user. */
8715 }
8716 idx += kw->skip;
8717 }
8718 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008719#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008720
8721 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008722}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008723
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008724static void hlua_deinit()
8725{
8726 lua_close(gL.T);
8727}
8728
8729REGISTER_POST_DEINIT(hlua_deinit);
8730
Willy Tarreau80713382018-11-26 10:19:54 +01008731static void hlua_register_build_options(void)
8732{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008733 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008734
Willy Tarreaubb57d942016-12-21 19:04:56 +01008735 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8736 hap_register_build_opts(ptr, 1);
8737}
Willy Tarreau80713382018-11-26 10:19:54 +01008738
8739INITCALL0(STG_REGISTER, hlua_register_build_options);