blob: de03ca383e4ebf43808091293556b2001c59b519 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Bertrand Jacquin892e4fd2021-01-21 21:14:07 +000013#define _GNU_SOURCE
14
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015#include <ctype.h>
Mark Lakes56cc1252018-03-27 09:48:06 +020016#include <limits.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020017#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010018
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010019#include <lauxlib.h>
20#include <lua.h>
21#include <lualib.h>
22
Thierry FOURNIER463119c2015-03-10 00:35:36 +010023#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
24#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010025#endif
26
Thierry FOURNIER380d0932015-01-23 14:27:52 +010027#include <ebpttree.h>
28
29#include <common/cfgparse.h>
Willy Tarreaub059b892018-10-16 17:57:36 +020030#include <common/compiler.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020031#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010032#include <common/initcall.h>
33#include <common/xref.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010034#include <common/h1.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035
William Lallemand9ed62032016-11-21 17:49:11 +010036#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010037#include <types/hlua.h>
38#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010039#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010040
Thierry FOURNIER55da1652015-01-23 11:36:30 +010041#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020042#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010043#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010045#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010047#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010048#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010049#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020050#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010051#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020052#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020053#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010054#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040055#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010056#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010057#include <proto/payload.h>
58#include <proto/proto_http.h>
59#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010060#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020061#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020062#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010063#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010064#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010065#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020066#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010067
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010068/* Lua uses longjmp to perform yield or throwing errors. This
69 * macro is used only for identifying the function that can
70 * not return because a longjmp is executed.
71 * __LJMP marks a prototype of hlua file that can use longjmp.
72 * WILL_LJMP() marks an lua function that will use longjmp.
73 * MAY_LJMP() marks an lua function that may use longjmp.
74 */
75#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020076#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010077#define MAY_LJMP(func) func
78
Thierry FOURNIERbabae282015-09-17 11:36:37 +020079/* This couple of function executes securely some Lua calls outside of
80 * the lua runtime environment. Each Lua call can return a longjmp
81 * if it encounter a memory error.
82 *
83 * Lua documentation extract:
84 *
85 * If an error happens outside any protected environment, Lua calls
86 * a panic function (see lua_atpanic) and then calls abort, thus
87 * exiting the host application. Your panic function can avoid this
88 * exit by never returning (e.g., doing a long jump to your own
89 * recovery point outside Lua).
90 *
91 * The panic function runs as if it were a message handler (see
92 * §2.3); in particular, the error message is at the top of the
93 * stack. However, there is no guarantee about stack space. To push
94 * anything on the stack, the panic function must first check the
95 * available space (see §4.2).
96 *
97 * We must check all the Lua entry point. This includes:
98 * - The include/proto/hlua.h exported functions
99 * - the task wrapper function
100 * - The action wrapper function
101 * - The converters wrapper function
102 * - The sample-fetch wrapper functions
103 *
104 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800105 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200106 *
107 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
108 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
109 * because they must be exists in the program stack when the longjmp
110 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200111 *
112 * Note that the Lua processing is not really thread safe. It provides
113 * heavy system which consists to add our own lock function in the Lua
114 * code and recompile the library. This system will probably not accepted
115 * by maintainers of various distribs.
116 *
117 * Our main excution point of the Lua is the function lua_resume(). A
118 * quick looking on the Lua sources displays a lua_lock() a the start
119 * of function and a lua_unlock() at the end of the function. So I
120 * conclude that the Lua thread safe mode just perform a mutex around
121 * all execution. So I prefer to do this in the HAProxy code, it will be
122 * easier for distro maintainers.
123 *
124 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
125 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
126 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200127 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100128__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200129THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200130static int hlua_panic_safe(lua_State *L) { return 0; }
131static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
132
133#define SET_SAFE_LJMP(__L) \
134 ({ \
135 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100136 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200137 if (setjmp(safe_ljmp_env) != 0) { \
138 lua_atpanic(__L, hlua_panic_safe); \
139 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100140 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200141 } else { \
142 lua_atpanic(__L, hlua_panic_ljmp); \
143 ret = 1; \
144 } \
145 ret; \
146 })
147
148/* If we are the last function catching Lua errors, we
149 * must reset the panic function.
150 */
151#define RESET_SAFE_LJMP(__L) \
152 do { \
153 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100154 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200155 } while(0)
156
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200157/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100159/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200160#define APPLET_HDR_SENT 0x04 /* Response header sent. */
161#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
162#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100163#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100164#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200165
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100166/* The main Lua execution context. */
167struct hlua gL;
168
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100169/* This is the memory pool containing struct lua for applets
170 * (including cli).
171 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100172DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100173
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100174/* Used for Socket connection. */
175static struct proxy socket_proxy;
176static struct server socket_tcp;
177#ifdef USE_OPENSSL
178static struct server socket_ssl;
179#endif
180
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100181/* List head of the function called at the initialisation time. */
182struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
183
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100184/* The following variables contains the reference of the different
185 * Lua classes. These references are useful for identify metadata
186 * associated with an object.
187 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100188static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100189static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100190static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100191static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100192static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100193static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200194static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200195static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200196static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100197
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100198/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200199 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100200 * short timeout. Lua linked with tasks doesn't have a timeout
201 * because a task may remain alive during all the haproxy execution.
202 */
203static unsigned int hlua_timeout_session = 4000; /* session timeout. */
204static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200205static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100206
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100207/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
208 * it is used for preventing infinite loops.
209 *
210 * I test the scheer with an infinite loop containing one incrementation
211 * and one test. I run this loop between 10 seconds, I raise a ceil of
212 * 710M loops from one interrupt each 9000 instructions, so I fix the value
213 * to one interrupt each 10 000 instructions.
214 *
215 * configured | Number of
216 * instructions | loops executed
217 * between two | in milions
218 * forced yields |
219 * ---------------+---------------
220 * 10 | 160
221 * 500 | 670
222 * 1000 | 680
223 * 5000 | 700
224 * 7000 | 700
225 * 8000 | 700
226 * 9000 | 710 <- ceil
227 * 10000 | 710
228 * 100000 | 710
229 * 1000000 | 710
230 *
231 */
232static unsigned int hlua_nb_instruction = 10000;
233
Willy Tarreau32f61e22015-03-18 17:54:59 +0100234/* Descriptor for the memory allocation state. If limit is not null, it will
235 * be enforced on any memory allocation.
236 */
237struct hlua_mem_allocator {
238 size_t allocated;
239 size_t limit;
240};
241
242static struct hlua_mem_allocator hlua_global_allocator;
243
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200244static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200245 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200246 "Cache-Control: no-cache\r\n"
247 "Connection: close\r\n"
248 "Content-Type: text/html\r\n"
249 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800250 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200251
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100252/* These functions converts types between HAProxy internal args or
253 * sample and LUA types. Another function permits to check if the
254 * LUA stack contains arguments according with an required ARG_T
255 * format.
256 */
257static int hlua_arg2lua(lua_State *L, const struct arg *arg);
258static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100259__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100260 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100261static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100262static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100263static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
264
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200265__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
266
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200267#define SEND_ERR(__be, __fmt, __args...) \
268 do { \
269 send_log(__be, LOG_ERR, __fmt, ## __args); \
270 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100271 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200272 } while (0)
273
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100274/* Used to check an Lua function type in the stack. It creates and
275 * returns a reference of the function. This function throws an
276 * error if the rgument is not a "function".
277 */
278__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
279{
280 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100281 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100282 WILL_LJMP(luaL_argerror(L, argno, msg));
283 }
284 lua_pushvalue(L, argno);
285 return luaL_ref(L, LUA_REGISTRYINDEX);
286}
287
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200288/* Return the string that is of the top of the stack. */
289const char *hlua_get_top_error_string(lua_State *L)
290{
291 if (lua_gettop(L) < 1)
292 return "unknown error";
293 if (lua_type(L, -1) != LUA_TSTRING)
294 return "unknown error";
295 return lua_tostring(L, -1);
296}
297
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200298__LJMP static const char *hlua_traceback(lua_State *L)
299{
300 lua_Debug ar;
301 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200302 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200303 int filled = 0;
304
305 while (lua_getstack(L, level++, &ar)) {
306
307 /* Add separator */
308 if (filled)
309 chunk_appendf(msg, ", ");
310 filled = 1;
311
312 /* Fill fields:
313 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
314 * 'l': fills in the field currentline;
315 * 'n': fills in the field name and namewhat;
316 * 't': fills in the field istailcall;
317 */
318 lua_getinfo(L, "Slnt", &ar);
319
320 /* Append code localisation */
321 if (ar.currentline > 0)
322 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
323 else
324 chunk_appendf(msg, "%s ", ar.short_src);
325
326 /*
327 * Get function name
328 *
329 * if namewhat is no empty, name is defined.
330 * what contains "Lua" for Lua function, "C" for C function,
331 * or "main" for main code.
332 */
333 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
334 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
335
336 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
337 chunk_appendf(msg, "main chunk");
338
339 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
340 chunk_appendf(msg, "C function line %d", ar.linedefined);
341
342 else /* nothing left... */
343 chunk_appendf(msg, "?");
344
345
346 /* Display tailed call */
347 if (ar.istailcall)
348 chunk_appendf(msg, " ...");
349 }
350
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200351 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200352}
353
354
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100355/* This function check the number of arguments available in the
356 * stack. If the number of arguments available is not the same
357 * then <nb> an error is throwed.
358 */
359__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
360{
361 if (lua_gettop(L) == nb)
362 return;
363 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
364}
365
Mark Lakes22154b42018-01-29 14:38:40 -0800366/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100367 * and the line number where the error is encountered.
368 */
369static int hlua_pusherror(lua_State *L, const char *fmt, ...)
370{
371 va_list argp;
372 va_start(argp, fmt);
373 luaL_where(L, 1);
374 lua_pushvfstring(L, fmt, argp);
375 va_end(argp);
376 lua_concat(L, 2);
377 return 1;
378}
379
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380/* This functions is used with sample fetch and converters. It
381 * converts the HAProxy configuration argument in a lua stack
382 * values.
383 *
384 * It takes an array of "arg", and each entry of the array is
385 * converted and pushed in the LUA stack.
386 */
387static int hlua_arg2lua(lua_State *L, const struct arg *arg)
388{
389 switch (arg->type) {
390 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100391 case ARGT_TIME:
392 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100393 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100394 break;
395
396 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200397 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100398 break;
399
400 case ARGT_IPV4:
401 case ARGT_IPV6:
402 case ARGT_MSK4:
403 case ARGT_MSK6:
404 case ARGT_FE:
405 case ARGT_BE:
406 case ARGT_TAB:
407 case ARGT_SRV:
408 case ARGT_USR:
409 case ARGT_MAP:
410 default:
411 lua_pushnil(L);
412 break;
413 }
414 return 1;
415}
416
417/* This function take one entrie in an LUA stack at the index "ud",
418 * and try to convert it in an HAProxy argument entry. This is useful
419 * with sample fetch wrappers. The input arguments are gived to the
420 * lua wrapper and converted as arg list by thi function.
421 */
422static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
423{
424 switch (lua_type(L, ud)) {
425
426 case LUA_TNUMBER:
427 case LUA_TBOOLEAN:
428 arg->type = ARGT_SINT;
429 arg->data.sint = lua_tointeger(L, ud);
430 break;
431
432 case LUA_TSTRING:
433 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200434 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Tim Duesterhusf371b832019-09-29 23:03:07 +0200435 /* We don't know the actual size of the underlying allocation, so be conservative. */
436 arg->data.str.size = arg->data.str.data;
437 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100438 break;
439
440 case LUA_TUSERDATA:
441 case LUA_TNIL:
442 case LUA_TTABLE:
443 case LUA_TFUNCTION:
444 case LUA_TTHREAD:
445 case LUA_TLIGHTUSERDATA:
446 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200447 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449 }
450 return 1;
451}
452
453/* the following functions are used to convert a struct sample
454 * in Lua type. This useful to convert the return of the
455 * fetchs or converters.
456 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100457static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200459 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100460 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200462 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100463 break;
464
465 case SMP_T_BIN:
466 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100468 break;
469
470 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200471 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100472 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
473 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
474 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
475 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
476 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
477 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
478 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
479 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
480 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200481 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100482 break;
483 default:
484 lua_pushnil(L);
485 break;
486 }
487 break;
488
489 case SMP_T_IPV4:
490 case SMP_T_IPV6:
491 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200492 if (sample_casts[smp->data.type][SMP_T_STR] &&
493 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200494 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100495 else
496 lua_pushnil(L);
497 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100498 default:
499 lua_pushnil(L);
500 break;
501 }
502 return 1;
503}
504
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505/* the following functions are used to convert a struct sample
506 * in Lua strings. This is useful to convert the return of the
507 * fetchs or converters.
508 */
509static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
510{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200511 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100512
513 case SMP_T_BIN:
514 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100516 break;
517
518 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200519 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100520 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
521 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
522 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
523 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
524 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
525 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
526 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
527 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
528 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 break;
531 default:
532 lua_pushstring(L, "");
533 break;
534 }
535 break;
536
537 case SMP_T_SINT:
538 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100539 case SMP_T_IPV4:
540 case SMP_T_IPV6:
541 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200542 if (sample_casts[smp->data.type][SMP_T_STR] &&
543 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200544 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100545 else
546 lua_pushstring(L, "");
547 break;
548 default:
549 lua_pushstring(L, "");
550 break;
551 }
552 return 1;
553}
554
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100555/* the following functions are used to convert an Lua type in a
556 * struct sample. This is useful to provide data from a converter
557 * to the LUA code.
558 */
559static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
560{
561 switch (lua_type(L, ud)) {
562
563 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200565 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 break;
567
568
569 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200570 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200571 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100572 break;
573
574 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200575 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100576 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200577 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Tim Duesterhusf371b832019-09-29 23:03:07 +0200578 /* We don't know the actual size of the underlying allocation, so be conservative. */
579 smp->data.u.str.size = smp->data.u.str.data;
580 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100581 break;
582
583 case LUA_TUSERDATA:
584 case LUA_TNIL:
585 case LUA_TTABLE:
586 case LUA_TFUNCTION:
587 case LUA_TTHREAD:
588 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200589 case LUA_TNONE:
590 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200591 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200592 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593 break;
594 }
595 return 1;
596}
597
598/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800599 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100600 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100601 *
602 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
603 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100604 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100605__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100606 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100607{
608 int min_arg;
609 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100610 struct proxy *px;
611 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100612
613 idx = 0;
614 min_arg = ARGM(mask);
615 mask >>= ARGM_BITS;
616
617 while (1) {
618
619 /* Check oversize. */
620 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100621 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100622 }
623
624 /* Check for mandatory arguments. */
625 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100626 if (idx < min_arg) {
627
628 /* If miss other argument than the first one, we return an error. */
629 if (idx > 0)
630 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
631
632 /* If first argument have a certain type, some default values
633 * may be used. See the function smp_resolve_args().
634 */
635 switch (mask & ARGT_MASK) {
636
637 case ARGT_FE:
638 if (!(p->cap & PR_CAP_FE))
639 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
640 argp[idx].data.prx = p;
641 argp[idx].type = ARGT_FE;
642 argp[idx+1].type = ARGT_STOP;
643 break;
644
645 case ARGT_BE:
646 if (!(p->cap & PR_CAP_BE))
647 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
648 argp[idx].data.prx = p;
649 argp[idx].type = ARGT_BE;
650 argp[idx+1].type = ARGT_STOP;
651 break;
652
653 case ARGT_TAB:
654 argp[idx].data.prx = p;
655 argp[idx].type = ARGT_TAB;
656 argp[idx+1].type = ARGT_STOP;
657 break;
658
659 default:
660 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
661 break;
662 }
663 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100664 return 0;
665 }
666
667 /* Check for exceed the number of requiered argument. */
668 if ((mask & ARGT_MASK) == ARGT_STOP &&
669 argp[idx].type != ARGT_STOP) {
670 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
671 }
672
673 if ((mask & ARGT_MASK) == ARGT_STOP &&
674 argp[idx].type == ARGT_STOP) {
675 return 0;
676 }
677
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100678 /* Convert some argument types. */
679 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100680 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 if (argp[idx].type != ARGT_SINT)
682 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
683 argp[idx].type = ARGT_SINT;
684 break;
685
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100686 case ARGT_TIME:
687 if (argp[idx].type != ARGT_SINT)
688 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200689 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100690 break;
691
692 case ARGT_SIZE:
693 if (argp[idx].type != ARGT_SINT)
694 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200695 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100696 break;
697
698 case ARGT_FE:
699 if (argp[idx].type != ARGT_STR)
700 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200701 memcpy(trash.area, argp[idx].data.str.area,
702 argp[idx].data.str.data);
703 trash.area[argp[idx].data.str.data] = 0;
704 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100705 if (!argp[idx].data.prx)
706 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
707 argp[idx].type = ARGT_FE;
708 break;
709
710 case ARGT_BE:
711 if (argp[idx].type != ARGT_STR)
712 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 memcpy(trash.area, argp[idx].data.str.area,
714 argp[idx].data.str.data);
715 trash.area[argp[idx].data.str.data] = 0;
716 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100717 if (!argp[idx].data.prx)
718 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
719 argp[idx].type = ARGT_BE;
720 break;
721
722 case ARGT_TAB:
723 if (argp[idx].type != ARGT_STR)
724 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 memcpy(trash.area, argp[idx].data.str.area,
726 argp[idx].data.str.data);
727 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhuse351e762019-09-29 23:03:09 +0200728 argp[idx].data.t = stktable_find_by_name(trash.area);
729 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100730 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
731 argp[idx].type = ARGT_TAB;
732 break;
733
734 case ARGT_SRV:
735 if (argp[idx].type != ARGT_STR)
736 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200737 memcpy(trash.area, argp[idx].data.str.area,
738 argp[idx].data.str.data);
739 trash.area[argp[idx].data.str.data] = 0;
740 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 if (sname) {
742 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200743 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200744 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100745 if (!px)
746 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
747 }
748 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200749 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100751 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100752 argp[idx].data.srv = findserver(px, sname);
753 if (!argp[idx].data.srv)
754 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
755 argp[idx].type = ARGT_SRV;
756 break;
757
758 case ARGT_IPV4:
Christopher Faulete7f1fcf2020-08-07 09:07:26 +0200759 if (argp[idx].type != ARGT_STR)
760 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200761 memcpy(trash.area, argp[idx].data.str.area,
762 argp[idx].data.str.data);
763 trash.area[argp[idx].data.str.data] = 0;
764 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100765 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
766 argp[idx].type = ARGT_IPV4;
767 break;
768
769 case ARGT_MSK4:
Christopher Fauletff8de2c2020-08-07 09:11:22 +0200770 if (argp[idx].type == ARGT_SINT)
771 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
772 else if (argp[idx].type == ARGT_STR) {
773 memcpy(trash.area, argp[idx].data.str.area,
774 argp[idx].data.str.data);
775 trash.area[argp[idx].data.str.data] = 0;
776 if (!str2mask(trash.area, &argp[idx].data.ipv4))
777 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
778 }
779 else
780 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100781 argp[idx].type = ARGT_MSK4;
782 break;
783
784 case ARGT_IPV6:
Christopher Faulete7f1fcf2020-08-07 09:07:26 +0200785 if (argp[idx].type != ARGT_STR)
786 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200787 memcpy(trash.area, argp[idx].data.str.area,
788 argp[idx].data.str.data);
789 trash.area[argp[idx].data.str.data] = 0;
790 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100791 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
792 argp[idx].type = ARGT_IPV6;
793 break;
794
795 case ARGT_MSK6:
Christopher Fauletff8de2c2020-08-07 09:11:22 +0200796 if (argp[idx].type == ARGT_SINT)
797 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
798 else if (argp[idx].type == ARGT_STR) {
799 memcpy(trash.area, argp[idx].data.str.area,
800 argp[idx].data.str.data);
801 trash.area[argp[idx].data.str.data] = 0;
802 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
803 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
804 }
805 else
806 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Tim Duesterhusb814da62018-01-25 16:24:50 +0100807 argp[idx].type = ARGT_MSK6;
808 break;
809
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100810 case ARGT_MAP:
811 case ARGT_REG:
812 case ARGT_USR:
813 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100814 break;
815 }
816
817 /* Check for type of argument. */
818 if ((mask & ARGT_MASK) != argp[idx].type) {
819 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
820 arg_type_names[(mask & ARGT_MASK)],
821 arg_type_names[argp[idx].type & ARGT_MASK]);
822 WILL_LJMP(luaL_argerror(L, first + idx, msg));
823 }
824
825 /* Next argument. */
826 mask >>= ARGT_BITS;
827 idx++;
828 }
829}
830
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100831/*
832 * The following functions are used to make correspondance between the the
833 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100834 *
835 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100836 * - hlua_sethlua : create the association between hlua context and lua_state.
837 */
838static inline struct hlua *hlua_gethlua(lua_State *L)
839{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100840 struct hlua **hlua = lua_getextraspace(L);
841 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100842}
843static inline void hlua_sethlua(struct hlua *hlua)
844{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100845 struct hlua **hlua_store = lua_getextraspace(hlua->T);
846 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100847}
848
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100849/* This function is used to send logs. It try to send on screen (stderr)
850 * and on the default syslog server.
851 */
852static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
853{
854 struct tm tm;
855 char *p;
856
857 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200858 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100859 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200860 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200861 /* Break the message if exceed the buffer size. */
862 *(p-4) = ' ';
863 *(p-3) = '.';
864 *(p-2) = '.';
865 *(p-1) = '.';
866 break;
867 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100868 if (isprint(*msg))
869 *p = *msg;
870 else
871 *p = '.';
872 }
873 *p = '\0';
874
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200875 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100876 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletb290edf2020-10-02 18:13:52 +0200877 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
878 return;
879
Willy Tarreaua678b432015-08-28 10:14:59 +0200880 get_localtime(date.tv_sec, &tm);
881 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100882 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200883 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100884 fflush(stderr);
885 }
886}
887
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100888/* This function just ensure that the yield will be always
889 * returned with a timeout and permit to set some flags
890 */
891__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100892 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100893{
894 struct hlua *hlua = hlua_gethlua(L);
895
896 /* Set the wake timeout. If timeout is required, we set
897 * the expiration time.
898 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200899 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100900
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100901 hlua->flags |= flags;
902
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100903 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200904 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100905}
906
Willy Tarreau87b09662015-04-03 00:22:06 +0200907/* This function initialises the Lua environment stored in the stream.
908 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100909 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200910 *
911 * This function is particular. it initialises a new Lua thread. If the
912 * initialisation fails (example: out of memory error), the lua function
913 * throws an error (longjmp).
914 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100915 * In some case (at least one), this function can be called from safe
916 * environement, so we must not initialise it. While the support of
917 * threads appear, the safe environment set a lock to ensure only one
918 * Lua execution at a time. If we initialize safe environment in another
919 * safe environmenet, we have a dead lock.
920 *
921 * set "already_safe" true if the context is initialized form safe
922 * Lua fonction.
923 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800924 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200925 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800926 * MUST NOT manipulate the created thread stack state, because it is not
927 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100928 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100929int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100930{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100931 if (!already_safe) {
932 if (!SET_SAFE_LJMP(gL.T)) {
933 lua->Tref = LUA_REFNIL;
934 return 0;
935 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200936 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100937 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100938 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100939 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100940 lua->T = lua_newthread(gL.T);
941 if (!lua->T) {
942 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100943 if (!already_safe)
944 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100945 return 0;
946 }
947 hlua_sethlua(lua);
948 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
949 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100950 if (!already_safe)
951 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100952 return 1;
953}
954
Willy Tarreau87b09662015-04-03 00:22:06 +0200955/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100956 * is destroyed. The destroy also the memory context. The struct "lua"
957 * is not freed.
958 */
959void hlua_ctx_destroy(struct hlua *lua)
960{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100961 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100962 return;
963
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100964 if (!lua->T)
965 goto end;
966
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100967 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200968 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100969
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200970 if (!SET_SAFE_LJMP(lua->T))
971 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100972 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200973 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200974
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200975 if (!SET_SAFE_LJMP(gL.T))
976 return;
977 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
978 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200979 /* Forces a garbage collecting process. If the Lua program is finished
980 * without error, we run the GC on the thread pointer. Its freed all
981 * the unused memory.
982 * If the thread is finnish with an error or is currently yielded,
983 * it seems that the GC applied on the thread doesn't clean anything,
984 * so e run the GC on the main thread.
985 * NOTE: maybe this action locks all the Lua threads untiml the en of
986 * the garbage collection.
987 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200988 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200989 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200990 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200991 lua_gc(gL.T, LUA_GCCOLLECT, 0);
992 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200993 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200994
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200995 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100996
997end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100998 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100999}
1000
1001/* This function is used to restore the Lua context when a coroutine
1002 * fails. This function copy the common memory between old coroutine
1003 * and the new coroutine. The old coroutine is destroyed, and its
1004 * replaced by the new coroutine.
1005 * If the flag "keep_msg" is set, the last entry of the old is assumed
1006 * as string error message and it is copied in the new stack.
1007 */
1008static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1009{
1010 lua_State *T;
1011 int new_ref;
1012
1013 /* Renew the main LUA stack doesn't have sense. */
1014 if (lua == &gL)
1015 return 0;
1016
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001017 /* New Lua coroutine. */
1018 T = lua_newthread(gL.T);
1019 if (!T)
1020 return 0;
1021
1022 /* Copy last error message. */
1023 if (keep_msg)
1024 lua_xmove(lua->T, T, 1);
1025
1026 /* Copy data between the coroutines. */
1027 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1028 lua_xmove(lua->T, T, 1);
1029 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1030
1031 /* Destroy old data. */
1032 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1033
1034 /* The thread is garbage collected by Lua. */
1035 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1036
1037 /* Fill the struct with the new coroutine values. */
1038 lua->Mref = new_ref;
1039 lua->T = T;
1040 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1041
1042 /* Set context. */
1043 hlua_sethlua(lua);
1044
1045 return 1;
1046}
1047
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001048void hlua_hook(lua_State *L, lua_Debug *ar)
1049{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001050 struct hlua *hlua = hlua_gethlua(L);
1051
1052 /* Lua cannot yield when its returning from a function,
1053 * so, we can fix the interrupt hook to 1 instruction,
1054 * expecting that the function is finnished.
1055 */
1056 if (lua_gethookmask(L) & LUA_MASKRET) {
1057 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1058 return;
1059 }
1060
1061 /* restore the interrupt condition. */
1062 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1063
1064 /* If we interrupt the Lua processing in yieldable state, we yield.
1065 * If the state is not yieldable, trying yield causes an error.
1066 */
1067 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001068 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001069
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001070 /* If we cannot yield, update the clock and check the timeout. */
1071 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001072 hlua->run_time += now_ms - hlua->start_time;
1073 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001074 lua_pushfstring(L, "execution timeout");
1075 WILL_LJMP(lua_error(L));
1076 }
1077
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001078 /* Update the start time. */
1079 hlua->start_time = now_ms;
1080
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001081 /* Try to interrupt the process at the end of the current
1082 * unyieldable function.
1083 */
1084 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001085}
1086
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001087/* This function start or resumes the Lua stack execution. If the flag
1088 * "yield_allowed" if no set and the LUA stack execution returns a yield
1089 * The function return an error.
1090 *
1091 * The function can returns 4 values:
1092 * - HLUA_E_OK : The execution is terminated without any errors.
1093 * - HLUA_E_AGAIN : The execution must continue at the next associated
1094 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001095 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001096 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001097 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001098 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001099 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001100 * LUA code.
1101 */
1102static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1103{
Christopher Faulet20699902020-07-28 10:33:25 +02001104#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1105 int nres;
1106#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001107 int ret;
1108 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001109 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001110
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001111 /* Initialise run time counter. */
1112 if (!HLUA_IS_RUNNING(lua))
1113 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001114
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001115 /* Lock the whole Lua execution. This lock must be before the
1116 * label "resume_execution".
1117 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001118 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001119
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001120resume_execution:
1121
1122 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1123 * instructions. it is used for preventing infinite loops.
1124 */
1125 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1126
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001127 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001128 HLUA_SET_RUN(lua);
1129 HLUA_CLR_CTRLYIELD(lua);
1130 HLUA_CLR_WAKERESWR(lua);
1131 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001132
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001133 /* Update the start time. */
1134 lua->start_time = now_ms;
1135
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001136 /* Call the function. */
Christopher Faulet20699902020-07-28 10:33:25 +02001137#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1138 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1139#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001140 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet20699902020-07-28 10:33:25 +02001141#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142 switch (ret) {
1143
1144 case LUA_OK:
1145 ret = HLUA_E_OK;
1146 break;
1147
1148 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001149 /* Check if the execution timeout is expired. It it is the case, we
1150 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001151 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001152 tv_update_date(0, 1);
1153 lua->run_time += now_ms - lua->start_time;
1154 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001155 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001156 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001157 break;
1158 }
1159 /* Process the forced yield. if the general yield is not allowed or
1160 * if no task were associated this the current Lua execution
1161 * coroutine, we resume the execution. Else we want to return in the
1162 * scheduler and we want to be waked up again, to continue the
1163 * current Lua execution. So we schedule our own task.
1164 */
1165 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001166 if (!yield_allowed || !lua->task)
1167 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001168 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001169 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 if (!yield_allowed) {
1171 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001172 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 break;
1174 }
1175 ret = HLUA_E_AGAIN;
1176 break;
1177
1178 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001179
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001180 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001181 * because the errors ares the only one mean to return immediately
1182 * from and lua execution.
1183 */
1184 if (lua->flags & HLUA_EXIT) {
1185 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001186 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001187 break;
1188 }
1189
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001190 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001191 if (!lua_checkstack(lua->T, 1)) {
1192 ret = HLUA_E_ERR;
1193 break;
1194 }
1195 msg = lua_tostring(lua->T, -1);
1196 lua_settop(lua->T, 0); /* Empty the stack. */
1197 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001198 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001199 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001200 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001201 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001202 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 ret = HLUA_E_ERRMSG;
1204 break;
1205
1206 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001207 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001208 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001209 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001210 break;
1211
1212 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001213 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001214 if (!lua_checkstack(lua->T, 1)) {
1215 ret = HLUA_E_ERR;
1216 break;
1217 }
1218 msg = lua_tostring(lua->T, -1);
1219 lua_settop(lua->T, 0); /* Empty the stack. */
1220 lua_pop(lua->T, 1);
1221 if (msg)
1222 lua_pushfstring(lua->T, "message handler error: %s", msg);
1223 else
1224 lua_pushfstring(lua->T, "message handler error");
1225 ret = HLUA_E_ERRMSG;
1226 break;
1227
1228 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001229 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001230 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001231 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001232 break;
1233 }
1234
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001235 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001236 if (lua->flags & HLUA_MUST_GC &&
1237 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001238 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1239
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001240 switch (ret) {
1241 case HLUA_E_AGAIN:
1242 break;
1243
1244 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001245 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001246 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001247 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001248 break;
1249
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001250 case HLUA_E_ETMOUT:
1251 case HLUA_E_NOMEM:
1252 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001253 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001254 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001255 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001256 hlua_ctx_renew(lua, 0);
1257 break;
1258
1259 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001260 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001261 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001262 break;
1263 }
1264
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001265 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001266 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001267
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001268 return ret;
1269}
1270
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001271/* This function exit the current code. */
1272__LJMP static int hlua_done(lua_State *L)
1273{
1274 struct hlua *hlua = hlua_gethlua(L);
1275
1276 hlua->flags |= HLUA_EXIT;
1277 WILL_LJMP(lua_error(L));
1278
1279 return 0;
1280}
1281
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001282/* This function is an LUA binding. It provides a function
1283 * for deleting ACL from a referenced ACL file.
1284 */
1285__LJMP static int hlua_del_acl(lua_State *L)
1286{
1287 const char *name;
1288 const char *key;
1289 struct pat_ref *ref;
1290
1291 MAY_LJMP(check_args(L, 2, "del_acl"));
1292
1293 name = MAY_LJMP(luaL_checkstring(L, 1));
1294 key = MAY_LJMP(luaL_checkstring(L, 2));
1295
1296 ref = pat_ref_lookup(name);
1297 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001298 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001299
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001300 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001301 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001302 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001303 return 0;
1304}
1305
1306/* This function is an LUA binding. It provides a function
1307 * for deleting map entry from a referenced map file.
1308 */
1309static int hlua_del_map(lua_State *L)
1310{
1311 const char *name;
1312 const char *key;
1313 struct pat_ref *ref;
1314
1315 MAY_LJMP(check_args(L, 2, "del_map"));
1316
1317 name = MAY_LJMP(luaL_checkstring(L, 1));
1318 key = MAY_LJMP(luaL_checkstring(L, 2));
1319
1320 ref = pat_ref_lookup(name);
1321 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001322 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001323
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001324 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001325 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001326 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001327 return 0;
1328}
1329
1330/* This function is an LUA binding. It provides a function
1331 * for adding ACL pattern from a referenced ACL file.
1332 */
1333static int hlua_add_acl(lua_State *L)
1334{
1335 const char *name;
1336 const char *key;
1337 struct pat_ref *ref;
1338
1339 MAY_LJMP(check_args(L, 2, "add_acl"));
1340
1341 name = MAY_LJMP(luaL_checkstring(L, 1));
1342 key = MAY_LJMP(luaL_checkstring(L, 2));
1343
1344 ref = pat_ref_lookup(name);
1345 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001346 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001347
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001348 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001349 if (pat_ref_find_elt(ref, key) == NULL)
1350 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001351 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001352 return 0;
1353}
1354
1355/* This function is an LUA binding. It provides a function
1356 * for setting map pattern and sample from a referenced map
1357 * file.
1358 */
1359static int hlua_set_map(lua_State *L)
1360{
1361 const char *name;
1362 const char *key;
1363 const char *value;
1364 struct pat_ref *ref;
1365
1366 MAY_LJMP(check_args(L, 3, "set_map"));
1367
1368 name = MAY_LJMP(luaL_checkstring(L, 1));
1369 key = MAY_LJMP(luaL_checkstring(L, 2));
1370 value = MAY_LJMP(luaL_checkstring(L, 3));
1371
1372 ref = pat_ref_lookup(name);
1373 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001374 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001375
Christopher Faulet9fa1f4e2020-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_set(ref, key, value, NULL);
1379 else
1380 pat_ref_add(ref, key, value, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001381 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001382 return 0;
1383}
1384
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001385/* A class is a lot of memory that contain data. This data can be a table,
1386 * an integer or user data. This data is associated with a metatable. This
1387 * metatable have an original version registred in the global context with
1388 * the name of the object (_G[<name>] = <metable> ).
1389 *
1390 * A metable is a table that modify the standard behavior of a standard
1391 * access to the associated data. The entries of this new metatable are
1392 * defined as is:
1393 *
1394 * http://lua-users.org/wiki/MetatableEvents
1395 *
1396 * __index
1397 *
1398 * we access an absent field in a table, the result is nil. This is
1399 * true, but it is not the whole truth. Actually, such access triggers
1400 * the interpreter to look for an __index metamethod: If there is no
1401 * such method, as usually happens, then the access results in nil;
1402 * otherwise, the metamethod will provide the result.
1403 *
1404 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1405 * the key does not appear in the table, but the metatable has an __index
1406 * property:
1407 *
1408 * - if the value is a function, the function is called, passing in the
1409 * table and the key; the return value of that function is returned as
1410 * the result.
1411 *
1412 * - if the value is another table, the value of the key in that table is
1413 * asked for and returned (and if it doesn't exist in that table, but that
1414 * table's metatable has an __index property, then it continues on up)
1415 *
1416 * - Use "rawget(myTable,key)" to skip this metamethod.
1417 *
1418 * http://www.lua.org/pil/13.4.1.html
1419 *
1420 * __newindex
1421 *
1422 * Like __index, but control property assignment.
1423 *
1424 * __mode - Control weak references. A string value with one or both
1425 * of the characters 'k' and 'v' which specifies that the the
1426 * keys and/or values in the table are weak references.
1427 *
1428 * __call - Treat a table like a function. When a table is followed by
1429 * parenthesis such as "myTable( 'foo' )" and the metatable has
1430 * a __call key pointing to a function, that function is invoked
1431 * (passing any specified arguments) and the return value is
1432 * returned.
1433 *
1434 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1435 * called, if the metatable for myTable has a __metatable
1436 * key, the value of that key is returned instead of the
1437 * actual metatable.
1438 *
1439 * __tostring - Control string representation. When the builtin
1440 * "tostring( myTable )" function is called, if the metatable
1441 * for myTable has a __tostring property set to a function,
1442 * that function is invoked (passing myTable to it) and the
1443 * return value is used as the string representation.
1444 *
1445 * __len - Control table length. When the table length is requested using
1446 * the length operator ( '#' ), if the metatable for myTable has
1447 * a __len key pointing to a function, that function is invoked
1448 * (passing myTable to it) and the return value used as the value
1449 * of "#myTable".
1450 *
1451 * __gc - Userdata finalizer code. When userdata is set to be garbage
1452 * collected, if the metatable has a __gc field pointing to a
1453 * function, that function is first invoked, passing the userdata
1454 * to it. The __gc metamethod is not called for tables.
1455 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1456 *
1457 * Special metamethods for redefining standard operators:
1458 * http://www.lua.org/pil/13.1.html
1459 *
1460 * __add "+"
1461 * __sub "-"
1462 * __mul "*"
1463 * __div "/"
1464 * __unm "!"
1465 * __pow "^"
1466 * __concat ".."
1467 *
1468 * Special methods for redfining standar relations
1469 * http://www.lua.org/pil/13.2.html
1470 *
1471 * __eq "=="
1472 * __lt "<"
1473 * __le "<="
1474 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001475
1476/*
1477 *
1478 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001479 * Class Map
1480 *
1481 *
1482 */
1483
1484/* Returns a struct hlua_map if the stack entry "ud" is
1485 * a class session, otherwise it throws an error.
1486 */
1487__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1488{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001489 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001490}
1491
1492/* This function is the map constructor. It don't need
1493 * the class Map object. It creates and return a new Map
1494 * object. It must be called only during "body" or "init"
1495 * context because it process some filesystem accesses.
1496 */
1497__LJMP static int hlua_map_new(struct lua_State *L)
1498{
1499 const char *fn;
1500 int match = PAT_MATCH_STR;
1501 struct sample_conv conv;
1502 const char *file = "";
1503 int line = 0;
1504 lua_Debug ar;
1505 char *err = NULL;
1506 struct arg args[2];
1507
1508 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1509 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1510
1511 fn = MAY_LJMP(luaL_checkstring(L, 1));
1512
1513 if (lua_gettop(L) >= 2) {
1514 match = MAY_LJMP(luaL_checkinteger(L, 2));
1515 if (match < 0 || match >= PAT_MATCH_NUM)
1516 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1517 }
1518
1519 /* Get Lua filename and line number. */
1520 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1521 lua_getinfo(L, "Sl", &ar); /* get info about it */
1522 if (ar.currentline > 0) { /* is there info? */
1523 file = ar.short_src;
1524 line = ar.currentline;
1525 }
1526 }
1527
1528 /* fill fake sample_conv struct. */
1529 conv.kw = ""; /* unused. */
1530 conv.process = NULL; /* unused. */
1531 conv.arg_mask = 0; /* unused. */
1532 conv.val_args = NULL; /* unused. */
1533 conv.out_type = SMP_T_STR;
1534 conv.private = (void *)(long)match;
1535 switch (match) {
1536 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1537 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1538 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1539 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1540 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1541 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1542 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001543 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001544 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1545 default:
1546 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1547 }
1548
1549 /* fill fake args. */
1550 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001551 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001552 args[1].type = ARGT_STOP;
1553
1554 /* load the map. */
1555 if (!sample_load_map(args, &conv, file, line, &err)) {
1556 /* error case: we cant use luaL_error because we must
1557 * free the err variable.
1558 */
1559 luaL_where(L, 1);
1560 lua_pushfstring(L, "'new': %s.", err);
1561 lua_concat(L, 2);
1562 free(err);
1563 WILL_LJMP(lua_error(L));
1564 }
1565
1566 /* create the lua object. */
1567 lua_newtable(L);
1568 lua_pushlightuserdata(L, args[0].data.map);
1569 lua_rawseti(L, -2, 0);
1570
1571 /* Pop a class Map metatable and affect it to the userdata. */
1572 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1573 lua_setmetatable(L, -2);
1574
1575
1576 return 1;
1577}
1578
1579__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1580{
1581 struct map_descriptor *desc;
1582 struct pattern *pat;
1583 struct sample smp;
1584
1585 MAY_LJMP(check_args(L, 2, "lookup"));
1586 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001587 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001588 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001589 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001590 }
1591 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001592 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001593 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001594 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry Fournier8e553842020-11-10 20:38:20 +01001595 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001596 }
1597
1598 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001599 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001600 if (str)
1601 lua_pushstring(L, "");
1602 else
1603 lua_pushnil(L);
1604 return 1;
1605 }
1606
1607 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001608 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001609 return 1;
1610}
1611
1612__LJMP static int hlua_map_lookup(struct lua_State *L)
1613{
1614 return _hlua_map_lookup(L, 0);
1615}
1616
1617__LJMP static int hlua_map_slookup(struct lua_State *L)
1618{
1619 return _hlua_map_lookup(L, 1);
1620}
1621
1622/*
1623 *
1624 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001625 * Class Socket
1626 *
1627 *
1628 */
1629
1630__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1631{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001632 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001633}
1634
1635/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001636 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001637 * received.
1638 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001639static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001640{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001641 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001642 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001643
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001644 if (appctx->ctx.hlua_cosocket.die) {
1645 si_shutw(si);
1646 si_shutr(si);
1647 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001648 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1649 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001650 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1651 }
1652
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001653 /* If the connection object is not available, close all the
1654 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001655 */
1656 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657 si_shutw(si);
1658 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001659 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001660 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1661 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001662 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663 }
1664
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001665 /* If we cant write, wakeup the pending write signals. */
1666 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001667 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001668
1669 /* If we cant read, wakeup the pending read signals. */
1670 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001671 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001672
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001673 /* if the connection is not estabkished, inform the stream that we want
1674 * to be notified whenever the connection completes.
1675 */
1676 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001677 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001678 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001679 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001680 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001681 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682
1683 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001684 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001686 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001687 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001688 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689
1690 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001691 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001692 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001693
1694 /* Some data were injected in the buffer, notify the stream
1695 * interface.
1696 */
1697 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001698 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001699
1700 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001701 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001702 */
1703 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001704 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705}
1706
Willy Tarreau87b09662015-04-03 00:22:06 +02001707/* This function is called when the "struct stream" is destroyed.
1708 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709 * Wake all the pending signals.
1710 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001711static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001712{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001713 struct xref *peer;
1714
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001715 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001716 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1717 if (peer)
1718 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001719
1720 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001721 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1722 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001723}
1724
1725/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001726 * uses this object. If the stream does not exists, just quit.
1727 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001728 * pending signal can rest in the read and write lists. destroy
1729 * it.
1730 */
1731__LJMP static int hlua_socket_gc(lua_State *L)
1732{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001733 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001734 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001735 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001736
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001737 MAY_LJMP(check_args(L, 1, "__gc"));
1738
1739 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001740 peer = xref_get_peer_and_lock(&socket->xref);
1741 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001742 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001743 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001744
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001745 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001746 appctx->ctx.hlua_cosocket.die = 1;
1747 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001748
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001749 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001750 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001751 return 0;
1752}
1753
1754/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001755 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001756 */
sada05ed3302018-05-11 11:48:18 -07001757__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001758{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001759 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001760 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001761 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001762
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001763 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001764
1765 /* Check if we run on the same thread than the xreator thread.
1766 * We cannot access to the socket if the thread is different.
1767 */
1768 if (socket->tid != tid)
1769 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1770
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001771 peer = xref_get_peer_and_lock(&socket->xref);
1772 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001773 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001774 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001775
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001776 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001777 appctx->ctx.hlua_cosocket.die = 1;
1778 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001779
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001780 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001781 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782 return 0;
1783}
1784
sada05ed3302018-05-11 11:48:18 -07001785/* The close function calls close_helper.
1786 */
1787__LJMP static int hlua_socket_close(lua_State *L)
1788{
1789 MAY_LJMP(check_args(L, 1, "close"));
1790 return hlua_socket_close_helper(L);
1791}
1792
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793/* This Lua function assumes that the stack contain three parameters.
1794 * 1 - USERDATA containing a struct socket
1795 * 2 - INTEGER with values of the macro defined below
1796 * If the integer is -1, we must read at most one line.
1797 * If the integer is -2, we ust read all the data until the
1798 * end of the stream.
1799 * If the integer is positive value, we must read a number of
1800 * bytes corresponding to this value.
1801 */
1802#define HLSR_READ_LINE (-1)
1803#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001804__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001805{
1806 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1807 int wanted = lua_tointeger(L, 2);
1808 struct hlua *hlua = hlua_gethlua(L);
1809 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001810 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001811 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001812 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001813 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001814 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001815 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001816 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001817 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001818 struct stream_interface *si;
1819 struct stream *s;
1820 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001821 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822
1823 /* Check if this lua stack is schedulable. */
1824 if (!hlua || !hlua->task)
1825 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1826 "'frontend', 'backend' or 'task'"));
1827
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001828 /* Check if we run on the same thread than the xreator thread.
1829 * We cannot access to the socket if the thread is different.
1830 */
1831 if (socket->tid != tid)
1832 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1833
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001834 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001835 peer = xref_get_peer_and_lock(&socket->xref);
1836 if (!peer)
1837 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001838 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1839 si = appctx->owner;
1840 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001842 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001844 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001845 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846 if (nblk < 0) /* Connection close. */
1847 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001848 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001849 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001850
1851 /* remove final \r\n. */
1852 if (nblk == 1) {
1853 if (blk1[len1-1] == '\n') {
1854 len1--;
1855 skip_at_end++;
1856 if (blk1[len1-1] == '\r') {
1857 len1--;
1858 skip_at_end++;
1859 }
1860 }
1861 }
1862 else {
1863 if (blk2[len2-1] == '\n') {
1864 len2--;
1865 skip_at_end++;
1866 if (blk2[len2-1] == '\r') {
1867 len2--;
1868 skip_at_end++;
1869 }
1870 }
1871 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 }
1873
1874 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001875 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001876 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877 if (nblk < 0) /* Connection close. */
1878 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001879 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 goto connection_empty;
1881 }
1882
1883 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001885 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001886 if (nblk < 0) /* Connection close. */
1887 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001888 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001889 goto connection_empty;
1890
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001891 missing_bytes = wanted - socket->b.n;
1892 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001894 len1 = missing_bytes;
1895 } if (nblk == 2 && len1 + len2 > missing_bytes)
1896 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001897 }
1898
1899 len = len1;
1900
1901 luaL_addlstring(&socket->b, blk1, len1);
1902 if (nblk == 2) {
1903 len += len2;
1904 luaL_addlstring(&socket->b, blk2, len2);
1905 }
1906
1907 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001908 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
1910 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001911 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001912
1913 /* If the pattern reclaim to read all the data
1914 * in the connection, got out.
1915 */
1916 if (wanted == HLSR_READ_ALL)
1917 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001918 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001919 goto connection_empty;
1920
1921 /* Return result. */
1922 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001923 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001924 return 1;
1925
1926connection_closed:
1927
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001928 xref_unlock(&socket->xref, peer);
1929
1930no_peer:
1931
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001932 /* If the buffer containds data. */
1933 if (socket->b.n > 0) {
1934 luaL_pushresult(&socket->b);
1935 return 1;
1936 }
1937 lua_pushnil(L);
1938 lua_pushstring(L, "connection closed.");
1939 return 2;
1940
1941connection_empty:
1942
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001943 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1944 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001945 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001946 }
1947 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001948 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001949 return 0;
1950}
1951
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001952/* This Lua function gets two parameters. The first one can be string
1953 * or a number. If the string is "*l", the user requires one line. If
1954 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001955 * If the value is a number, the user require a number of bytes equal
1956 * to the value. The default value is "*l" (a line).
1957 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001958 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959 * integer takes this values:
1960 * -1 : read a line
1961 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001962 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001964 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 * concatenated with the read data.
1966 */
1967__LJMP static int hlua_socket_receive(struct lua_State *L)
1968{
1969 int wanted = HLSR_READ_LINE;
1970 const char *pattern;
1971 int type;
1972 char *error;
1973 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001974 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975
1976 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1977 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1978
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001979 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001981 /* Check if we run on the same thread than the xreator thread.
1982 * We cannot access to the socket if the thread is different.
1983 */
1984 if (socket->tid != tid)
1985 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1986
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001987 /* check for pattern. */
1988 if (lua_gettop(L) >= 2) {
1989 type = lua_type(L, 2);
1990 if (type == LUA_TSTRING) {
1991 pattern = lua_tostring(L, 2);
1992 if (strcmp(pattern, "*a") == 0)
1993 wanted = HLSR_READ_ALL;
1994 else if (strcmp(pattern, "*l") == 0)
1995 wanted = HLSR_READ_LINE;
1996 else {
1997 wanted = strtoll(pattern, &error, 10);
1998 if (*error != '\0')
1999 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2000 }
2001 }
2002 else if (type == LUA_TNUMBER) {
2003 wanted = lua_tointeger(L, 2);
2004 if (wanted < 0)
2005 WILL_LJMP(luaL_error(L, "Unsupported size."));
2006 }
2007 }
2008
2009 /* Set pattern. */
2010 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002011
2012 /* Check if we would replace the top by itself. */
2013 if (lua_gettop(L) != 2)
2014 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002015
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002016 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 luaL_buffinit(L, &socket->b);
2018
2019 /* Check prefix. */
2020 if (lua_gettop(L) >= 3) {
2021 if (lua_type(L, 3) != LUA_TSTRING)
2022 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2023 pattern = lua_tolstring(L, 3, &len);
2024 luaL_addlstring(&socket->b, pattern, len);
2025 }
2026
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002027 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028}
2029
2030/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002031 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002033static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034{
2035 struct hlua_socket *socket;
2036 struct hlua *hlua = hlua_gethlua(L);
2037 struct appctx *appctx;
2038 size_t buf_len;
2039 const char *buf;
2040 int len;
2041 int send_len;
2042 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002043 struct xref *peer;
2044 struct stream_interface *si;
2045 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002046
2047 /* Check if this lua stack is schedulable. */
2048 if (!hlua || !hlua->task)
2049 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2050 "'frontend', 'backend' or 'task'"));
2051
2052 /* Get object */
2053 socket = MAY_LJMP(hlua_checksocket(L, 1));
2054 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002055 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002056
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002057 /* Check if we run on the same thread than the xreator thread.
2058 * We cannot access to the socket if the thread is different.
2059 */
2060 if (socket->tid != tid)
2061 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2062
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002063 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002064 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002065 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002066 lua_pushinteger(L, -1);
2067 return 1;
2068 }
2069 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2070 si = appctx->owner;
2071 s = si_strm(si);
2072
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002074 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002075 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002076 lua_pushinteger(L, -1);
2077 return 1;
2078 }
2079
2080 /* Update the input buffer data. */
2081 buf += sent;
2082 send_len = buf_len - sent;
2083
2084 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002085 if (sent >= buf_len) {
2086 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002088 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002089
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002090 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002091 * the request buffer if its not required.
2092 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002093 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002094 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002095 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002096 }
2097
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002098 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002099 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002100 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002102 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002103
2104 /* send data */
2105 if (len < send_len)
2106 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002107 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002108
2109 /* "Not enough space" (-1), "Buffer too little to contain
2110 * the data" (-2) are not expected because the available length
2111 * is tested.
2112 * Other unknown error are also not expected.
2113 */
2114 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002115 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002116 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002117
sada05ed3302018-05-11 11:48:18 -07002118 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002120 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002121 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002122 return 1;
2123 }
2124
2125 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002126 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002127
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002128 s->req.rex = TICK_ETERNITY;
2129 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002130
2131 /* Update length sent. */
2132 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002133 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002134
2135 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002136 if (sent + len >= buf_len) {
2137 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002138 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002139 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002140
2141hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002142 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2143 xref_unlock(&socket->xref, peer);
2144 WILL_LJMP(luaL_error(L, "out of memory"));
2145 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002146 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002147 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 return 0;
2149}
2150
2151/* This function initiate the send of data. It just check the input
2152 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002153 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154 * "hlua_socket_write_yield" that can yield.
2155 *
2156 * The Lua function gets between 3 and 4 parameters. The first one is
2157 * the associated object. The second is a string buffer. The third is
2158 * a facultative integer that represents where is the buffer position
2159 * of the start of the data that can send. The first byte is the
2160 * position "1". The default value is "1". The fourth argument is a
2161 * facultative integer that represents where is the buffer position
2162 * of the end of the data that can send. The default is the last byte.
2163 */
2164static int hlua_socket_send(struct lua_State *L)
2165{
2166 int i;
2167 int j;
2168 const char *buf;
2169 size_t buf_len;
2170
2171 /* Check number of arguments. */
2172 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2173 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2174
2175 /* Get the string. */
2176 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2177
2178 /* Get and check j. */
2179 if (lua_gettop(L) == 4) {
2180 j = MAY_LJMP(luaL_checkinteger(L, 4));
2181 if (j < 0)
2182 j = buf_len + j + 1;
2183 if (j > buf_len)
2184 j = buf_len + 1;
2185 lua_pop(L, 1);
2186 }
2187 else
2188 j = buf_len;
2189
2190 /* Get and check i. */
2191 if (lua_gettop(L) == 3) {
2192 i = MAY_LJMP(luaL_checkinteger(L, 3));
2193 if (i < 0)
2194 i = buf_len + i + 1;
2195 if (i > buf_len)
2196 i = buf_len + 1;
2197 lua_pop(L, 1);
2198 } else
2199 i = 1;
2200
2201 /* Check bth i and j. */
2202 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002203 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204 return 1;
2205 }
2206 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002207 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002208 return 1;
2209 }
2210 if (i == 0)
2211 i = 1;
2212 if (j == 0)
2213 j = 1;
2214
2215 /* Pop the string. */
2216 lua_pop(L, 1);
2217
2218 /* Update the buffer length. */
2219 buf += i - 1;
2220 buf_len = j - i + 1;
2221 lua_pushlstring(L, buf, buf_len);
2222
2223 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002224 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002225
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002226 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002227}
2228
Willy Tarreau22b0a682015-06-17 19:43:49 +02002229#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2231{
2232 static char buffer[SOCKET_INFO_MAX_LEN];
2233 int ret;
2234 int len;
2235 char *p;
2236
2237 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2238 if (ret <= 0) {
2239 lua_pushnil(L);
2240 return 1;
2241 }
2242
2243 if (ret == AF_UNIX) {
2244 lua_pushstring(L, buffer+1);
2245 return 1;
2246 }
2247 else if (ret == AF_INET6) {
2248 buffer[0] = '[';
2249 len = strlen(buffer);
2250 buffer[len] = ']';
2251 len++;
2252 buffer[len] = ':';
2253 len++;
2254 p = buffer;
2255 }
2256 else if (ret == AF_INET) {
2257 p = buffer + 1;
2258 len = strlen(p);
2259 p[len] = ':';
2260 len++;
2261 }
2262 else {
2263 lua_pushnil(L);
2264 return 1;
2265 }
2266
2267 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2268 lua_pushnil(L);
2269 return 1;
2270 }
2271
2272 lua_pushstring(L, p);
2273 return 1;
2274}
2275
2276/* Returns information about the peer of the connection. */
2277__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2278{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002279 struct hlua_socket *socket;
2280 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002281 struct xref *peer;
2282 struct appctx *appctx;
2283 struct stream_interface *si;
2284 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002285 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002286
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287 MAY_LJMP(check_args(L, 1, "getpeername"));
2288
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002289 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002291 /* Check if we run on the same thread than the xreator thread.
2292 * We cannot access to the socket if the thread is different.
2293 */
2294 if (socket->tid != tid)
2295 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2296
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002297 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002298 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002299 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300 lua_pushnil(L);
2301 return 1;
2302 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002303 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2304 si = appctx->owner;
2305 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002307 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002309 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 lua_pushnil(L);
2311 return 1;
2312 }
2313
Willy Tarreaua71f6422016-11-16 17:00:14 +01002314 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002315 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002316 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002317 lua_pushnil(L);
2318 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319 }
2320
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002321 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2322 xref_unlock(&socket->xref, peer);
2323 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002324}
2325
2326/* Returns information about my connection side. */
2327static int hlua_socket_getsockname(struct lua_State *L)
2328{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002329 struct hlua_socket *socket;
2330 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 struct appctx *appctx;
2332 struct xref *peer;
2333 struct stream_interface *si;
2334 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002335 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002336
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002337 MAY_LJMP(check_args(L, 1, "getsockname"));
2338
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002339 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002341 /* Check if we run on the same thread than the xreator thread.
2342 * We cannot access to the socket if the thread is different.
2343 */
2344 if (socket->tid != tid)
2345 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2346
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002347 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002349 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350 lua_pushnil(L);
2351 return 1;
2352 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002353 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2354 si = appctx->owner;
2355 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002357 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002358 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002359 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 lua_pushnil(L);
2361 return 1;
2362 }
2363
Willy Tarreaua71f6422016-11-16 17:00:14 +01002364 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002365 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002366 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002367 lua_pushnil(L);
2368 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002369 }
2370
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002371 ret = hlua_socket_info(L, &conn->addr.from);
2372 xref_unlock(&socket->xref, peer);
2373 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374}
2375
2376/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002377static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 .obj_type = OBJ_TYPE_APPLET,
2379 .name = "<LUA_TCP>",
2380 .fct = hlua_socket_handler,
2381 .release = hlua_socket_release,
2382};
2383
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002384__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385{
2386 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2387 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002388 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002390 struct stream_interface *si;
2391 struct stream *s;
2392
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002393 /* Check if we run on the same thread than the xreator thread.
2394 * We cannot access to the socket if the thread is different.
2395 */
2396 if (socket->tid != tid)
2397 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2398
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002399 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002400 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002401 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002402 lua_pushnil(L);
2403 lua_pushstring(L, "Can't connect");
2404 return 2;
2405 }
2406 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2407 si = appctx->owner;
2408 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002409
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002410 /* Check if we run on the same thread than the xreator thread.
2411 * We cannot access to the socket if the thread is different.
2412 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002413 if (socket->tid != tid) {
2414 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002415 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002416 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002417
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002419 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002420 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002421 lua_pushnil(L);
2422 lua_pushstring(L, "Can't connect");
2423 return 2;
2424 }
2425
Willy Tarreaue09101e2018-10-16 17:37:12 +02002426 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002427
2428 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002429 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002430 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431 lua_pushinteger(L, 1);
2432 return 1;
2433 }
2434
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002435 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2436 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002437 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002438 }
2439 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002440 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002441 return 0;
2442}
2443
2444/* This function fail or initite the connection. */
2445__LJMP static int hlua_socket_connect(struct lua_State *L)
2446{
2447 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002448 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449 const char *ip;
2450 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002451 struct hlua *hlua;
2452 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002453 int low, high;
2454 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002455 struct xref *peer;
2456 struct stream_interface *si;
2457 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002458
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002459 if (lua_gettop(L) < 2)
2460 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002461
2462 /* Get args. */
2463 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002464
2465 /* Check if we run on the same thread than the xreator thread.
2466 * We cannot access to the socket if the thread is different.
2467 */
2468 if (socket->tid != tid)
2469 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2470
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002471 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002472 if (lua_gettop(L) >= 3) {
2473 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002474 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002475
Tim Duesterhus6edab862018-01-06 19:04:45 +01002476 /* Force the ip to end with a colon, to support IPv6 addresses
2477 * that are not enclosed within square brackets.
2478 */
2479 if (port > 0) {
2480 luaL_buffinit(L, &b);
2481 luaL_addstring(&b, ip);
2482 luaL_addchar(&b, ':');
2483 luaL_pushresult(&b);
2484 ip = lua_tolstring(L, lua_gettop(L), NULL);
2485 }
2486 }
2487
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002488 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002489 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002490 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002491 lua_pushnil(L);
2492 return 1;
2493 }
2494 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2495 si = appctx->owner;
2496 s = si_strm(si);
2497
2498 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002499 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002500 if (!conn) {
2501 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002502 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002503 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002504
Willy Tarreau3adac082015-09-26 17:51:09 +02002505 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002506 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002507
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002508 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002509 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002510 if (!addr) {
2511 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002512 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002513 }
2514 if (low != high) {
2515 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002516 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002517 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002518 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519
2520 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002521 if (low == 0) {
2522 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002523 if (port == -1) {
2524 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002525 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002526 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002527 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2528 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002529 if (port == -1) {
2530 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002531 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002532 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002533 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2534 }
2535 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002536
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002537 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002538 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002539
2540 /* inform the stream that we want to be notified whenever the
2541 * connection completes.
2542 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002543 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002544 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002545 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002546
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002547 hlua->flags |= HLUA_MUST_GC;
2548
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002549 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2550 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002551 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002552 }
2553 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002554
2555 task_wakeup(s->task, TASK_WOKEN_INIT);
2556 /* Return yield waiting for connection. */
2557
Willy Tarreau9635e032018-10-16 17:52:55 +02002558 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002559
2560 return 0;
2561}
2562
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002563#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002564__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2565{
2566 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002567 struct xref *peer;
2568 struct appctx *appctx;
2569 struct stream_interface *si;
2570 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002571
2572 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2573 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574
2575 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002576 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002577 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002578 lua_pushnil(L);
2579 return 1;
2580 }
2581 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2582 si = appctx->owner;
2583 s = si_strm(si);
2584
2585 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002586 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587 return MAY_LJMP(hlua_socket_connect(L));
2588}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002589#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002590
2591__LJMP static int hlua_socket_setoption(struct lua_State *L)
2592{
2593 return 0;
2594}
2595
2596__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2597{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002598 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002599 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002600 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002601 struct xref *peer;
2602 struct appctx *appctx;
2603 struct stream_interface *si;
2604 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002605
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606 MAY_LJMP(check_args(L, 2, "settimeout"));
2607
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002608 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002609
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002610 /* convert the timeout to millis */
2611 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002612
Thierry Fournier17a921b2018-03-08 09:59:02 +01002613 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002614 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002615 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2616
Mark Lakes56cc1252018-03-27 09:48:06 +02002617 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002618 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002619
2620 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002621 if (tmout == 0)
2622 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002623
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002624 /* Check if we run on the same thread than the xreator thread.
2625 * We cannot access to the socket if the thread is different.
2626 */
2627 if (socket->tid != tid)
2628 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2629
Mark Lakes56cc1252018-03-27 09:48:06 +02002630 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002631 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002632 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002633 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2634 WILL_LJMP(lua_error(L));
2635 return 0;
2636 }
2637 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2638 si = appctx->owner;
2639 s = si_strm(si);
2640
Cyril Bonté7bb63452018-08-17 23:51:02 +02002641 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002642 s->req.rto = tmout;
2643 s->req.wto = tmout;
2644 s->res.rto = tmout;
2645 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002646 s->req.rex = tick_add_ifset(now_ms, tmout);
2647 s->req.wex = tick_add_ifset(now_ms, tmout);
2648 s->res.rex = tick_add_ifset(now_ms, tmout);
2649 s->res.wex = tick_add_ifset(now_ms, tmout);
2650
2651 s->task->expire = tick_add_ifset(now_ms, tmout);
2652 task_queue(s->task);
2653
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002654 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002655
Thierry Fourniere9636f12018-03-08 09:54:32 +01002656 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002657 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658}
2659
2660__LJMP static int hlua_socket_new(lua_State *L)
2661{
2662 struct hlua_socket *socket;
2663 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002664 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002665 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002666
2667 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002668 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002669 hlua_pusherror(L, "socket: full stack");
2670 goto out_fail_conf;
2671 }
2672
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002673 /* Create the object: obj[0] = userdata. */
2674 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002675 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002676 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002677 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002678 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002679
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002680 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002681 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002682 hlua_pusherror(L, "socket: uninitialized pools.");
2683 goto out_fail_conf;
2684 }
2685
Willy Tarreau87b09662015-04-03 00:22:06 +02002686 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002687 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2688 lua_setmetatable(L, -2);
2689
Willy Tarreaud420a972015-04-06 00:39:18 +02002690 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002691 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002692 if (!appctx) {
2693 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002694 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002695 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002696
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002697 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002698 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002699 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2700 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002701
Willy Tarreaud420a972015-04-06 00:39:18 +02002702 /* Now create a session, task and stream for this applet */
2703 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2704 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002705 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002706 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002707 }
2708
Willy Tarreau87787ac2017-08-28 16:22:54 +02002709 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002710 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002711 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002712 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002713 }
2714
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002715 /* Initialise cross reference between stream and Lua socket object. */
2716 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002717
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002718 /* Configure "right" stream interface. this "si" is used to connect
2719 * and retrieve data from the server. The connection is initialized
2720 * with the "struct server".
2721 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002722 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002723
2724 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002725 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2726 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002727
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002728 return 1;
2729
Willy Tarreaud420a972015-04-06 00:39:18 +02002730 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002731 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002732 out_fail_sess:
2733 appctx_free(appctx);
2734 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002735 WILL_LJMP(lua_error(L));
2736 return 0;
2737}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002738
2739/*
2740 *
2741 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002742 * Class Channel
2743 *
2744 *
2745 */
2746
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002747/* This function is called before the Lua execution. It stores
2748 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002749 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002750static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002751{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002752 c->mode = stream->be->mode;
2753 switch (c->mode) {
2754 case PR_MODE_HTTP:
2755 c->data.http.dir = opt & SMP_OPT_DIR;
2756 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2757 c->data.http.state = stream->txn->req.msg_state;
2758 else
2759 c->data.http.state = stream->txn->rsp.msg_state;
2760 break;
2761 default:
2762 break;
2763 }
2764}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002765
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002766/* This function is called after the Lua execution. it
2767 * returns true if the parser state is consistent, otherwise,
2768 * it return false.
2769 *
2770 * In HTTP mode, the parser state must be in the same state
2771 * or greater when we exit the function. Even if we do a
2772 * control yield. This prevent to break the HTTP message
2773 * from the Lua code.
2774 */
2775static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2776{
2777 if (c->mode != stream->be->mode)
2778 return 0;
2779
2780 switch (c->mode) {
2781 case PR_MODE_HTTP:
2782 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002783 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002784 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2785 return stream->txn->req.msg_state >= c->data.http.state;
2786 else
2787 return stream->txn->rsp.msg_state >= c->data.http.state;
2788 default:
2789 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002790 }
2791 return 1;
2792}
2793
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794/* Returns the struct hlua_channel join to the class channel in the
2795 * stack entry "ud" or throws an argument error.
2796 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002797__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002798{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002799 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800}
2801
Willy Tarreau47860ed2015-03-10 14:07:50 +01002802/* Pushes the channel onto the top of the stack. If the stask does not have a
2803 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002805static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002807 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002808 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002809 return 0;
2810
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002811 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002812 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002813 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002814
2815 /* Pop a class sesison metatable and affect it to the userdata. */
2816 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2817 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002818 return 1;
2819}
2820
2821/* Duplicate all the data present in the input channel and put it
2822 * in a string LUA variables. Returns -1 and push a nil value in
2823 * the stack if the channel is closed and all the data are consumed,
2824 * returns 0 if no data are available, otherwise it returns the length
2825 * of the builded string.
2826 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002827static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002828{
2829 char *blk1;
2830 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002831 size_t len1;
2832 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002833 int ret;
2834 luaL_Buffer b;
2835
Willy Tarreau06d80a92017-10-19 14:32:15 +02002836 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002837 if (unlikely(ret == 0))
2838 return 0;
2839
2840 if (unlikely(ret < 0)) {
2841 lua_pushnil(L);
2842 return -1;
2843 }
2844
2845 luaL_buffinit(L, &b);
2846 luaL_addlstring(&b, blk1, len1);
2847 if (unlikely(ret == 2))
2848 luaL_addlstring(&b, blk2, len2);
2849 luaL_pushresult(&b);
2850
2851 if (unlikely(ret == 2))
2852 return len1 + len2;
2853 return len1;
2854}
2855
2856/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2857 * a yield. This function keep the data in the buffer.
2858 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002859__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002860{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002861 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002862
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002863 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2864
Christopher Faulet3f829a42018-12-13 21:56:45 +01002865 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2866 WILL_LJMP(lua_error(L));
2867
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002868 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002869 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002870 return 1;
2871}
2872
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002873/* Check arguments for the function "hlua_channel_dup_yield". */
2874__LJMP static int hlua_channel_dup(lua_State *L)
2875{
2876 MAY_LJMP(check_args(L, 1, "dup"));
2877 MAY_LJMP(hlua_checkchannel(L, 1));
2878 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2879}
2880
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2882 * a yield. This function consumes the data in the buffer. It returns
2883 * a string containing the data or a nil pointer if no data are available
2884 * and the channel is closed.
2885 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002886__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002887{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002888 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002889 int ret;
2890
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002891 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002892
Christopher Faulet3f829a42018-12-13 21:56:45 +01002893 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2894 WILL_LJMP(lua_error(L));
2895
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002896 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002898 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002899
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900 if (unlikely(ret == -1))
2901 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002902
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002903 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904 return 1;
2905}
2906
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002907/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002908__LJMP static int hlua_channel_get(lua_State *L)
2909{
2910 MAY_LJMP(check_args(L, 1, "get"));
2911 MAY_LJMP(hlua_checkchannel(L, 1));
2912 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2913}
2914
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915/* This functions consumes and returns one line. If the channel is closed,
2916 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002917 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 * value.
2919 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002920__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002921{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 char *blk1;
2923 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002924 size_t len1;
2925 size_t len2;
2926 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002927 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002928 int ret;
2929 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002930
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002931 chn = MAY_LJMP(hlua_checkchannel(L, 1));
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
Willy Tarreau06d80a92017-10-19 14:32:15 +02002936 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002938 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002939
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940 if (ret == -1) {
2941 lua_pushnil(L);
2942 return 1;
2943 }
2944
2945 luaL_buffinit(L, &b);
2946 luaL_addlstring(&b, blk1, len1);
2947 len = len1;
2948 if (unlikely(ret == 2)) {
2949 luaL_addlstring(&b, blk2, len2);
2950 len += len2;
2951 }
2952 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002953 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002954 return 1;
2955}
2956
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002957/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002958__LJMP static int hlua_channel_getline(lua_State *L)
2959{
2960 MAY_LJMP(check_args(L, 1, "getline"));
2961 MAY_LJMP(hlua_checkchannel(L, 1));
2962 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2963}
2964
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002965/* This function takes a string as input, and append it at the
2966 * input side of channel. If the data is too big, but a space
2967 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002968 * yields. If the data is bigger than the buffer, or if the
2969 * channel is closed, it returns -1. Otherwise, it returns the
2970 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002972__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002973{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002974 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002975 size_t len;
2976 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2977 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2978 int ret;
2979 int max;
2980
Christopher Faulet3f829a42018-12-13 21:56:45 +01002981 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2982 WILL_LJMP(lua_error(L));
2983
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002984 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002985 * the request buffer if its not required.
2986 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002987 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002988 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002989 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002990 }
2991
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002992 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993 if (max > len - l)
2994 max = len - l;
2995
Willy Tarreau06d80a92017-10-19 14:32:15 +02002996 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002997 if (ret == -2 || ret == -3) {
2998 lua_pushinteger(L, -1);
2999 return 1;
3000 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003001 if (ret == -1) {
3002 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003003 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003004 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003005 l += ret;
3006 lua_pop(L, 1);
3007 lua_pushinteger(L, l);
3008
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003009 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003010 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003011 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003012 * in this case, we cannot add more data, so we cannot yield,
3013 * we return the amount of copyied data.
3014 */
3015 return 1;
3016 }
3017 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003018 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003019 return 1;
3020}
3021
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003022/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3023 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 * buffer size is too little for the data.
3025 */
3026__LJMP static int hlua_channel_append(lua_State *L)
3027{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003028 size_t len;
3029
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003031 MAY_LJMP(hlua_checkchannel(L, 1));
3032 MAY_LJMP(luaL_checklstring(L, 2, &len));
3033 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003034 lua_pushinteger(L, 0);
3035
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003036 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037}
3038
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003039/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003040 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003041 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003042 * or -1 if the channel is closed or if the buffer size is too
3043 * little for the data.
3044 */
3045__LJMP static int hlua_channel_set(lua_State *L)
3046{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003047 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003048
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003050 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051 lua_pushinteger(L, 0);
3052
Christopher Faulet3f829a42018-12-13 21:56:45 +01003053 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3054 WILL_LJMP(lua_error(L));
3055
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003056 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003057
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003058 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003059}
3060
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003061/* Append data in the output side of the buffer. This data is immediately
3062 * sent. The function returns the amount of data written. If the buffer
3063 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064 * if the channel is closed.
3065 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003066__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003067{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003068 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069 size_t len;
3070 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3071 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3072 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003073 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003074
Christopher Faulet3f829a42018-12-13 21:56:45 +01003075 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3076 WILL_LJMP(lua_error(L));
3077
Willy Tarreau47860ed2015-03-10 14:07:50 +01003078 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003079 lua_pushinteger(L, -1);
3080 return 1;
3081 }
3082
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003083 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003084 * the request buffer if its not required.
3085 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003086 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003087 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003088 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003089 }
3090
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003091 /* The written data will be immediately sent, so we can check
3092 * the available space without taking in account the reserve.
3093 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003094 * data, because the buffer will be flushed.
3095 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003096 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003097
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003098 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003099 * in this case, we cannot add more data, so we cannot yield,
3100 * we return the amount of copyied data.
3101 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003102 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003103 return 1;
3104
3105 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003106 if (max > len - l)
3107 max = len - l;
3108
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003109 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003110 * detects a non contiguous buffer and realign it.
3111 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003112 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003113 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003114
3115 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003116 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003117
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003118 /* buffer replace considers that the input part is filled.
3119 * so, I must forward these new data in the output part.
3120 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003121 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003122
3123 l += max;
3124 lua_pop(L, 1);
3125 lua_pushinteger(L, l);
3126
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003127 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003128 * in this case, we cannot add more data, so we cannot yield,
3129 * we return the amount of copyied data.
3130 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003131 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003132 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003133 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003134
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003135 if (l < len) {
3136 /* If we are waiting for space in the response buffer, we
3137 * must set the flag WAKERESWR. This flag required the task
3138 * wake up if any activity is detected on the response buffer.
3139 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003140 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003141 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003142 else
3143 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003144 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003145 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003146
3147 return 1;
3148}
3149
3150/* Just a wraper of "_hlua_channel_send". This wrapper permits
3151 * yield the LUA process, and resume it without checking the
3152 * input arguments.
3153 */
3154__LJMP static int hlua_channel_send(lua_State *L)
3155{
3156 MAY_LJMP(check_args(L, 2, "send"));
3157 lua_pushinteger(L, 0);
3158
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003159 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160}
3161
3162/* This function forward and amount of butes. The data pass from
3163 * the input side of the buffer to the output side, and can be
3164 * forwarded. This function never fails.
3165 *
3166 * The Lua function takes an amount of bytes to be forwarded in
3167 * imput. It returns the number of bytes forwarded.
3168 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003169__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003170{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003171 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172 int len;
3173 int l;
3174 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003175 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003176
3177 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003178
3179 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3180 WILL_LJMP(lua_error(L));
3181
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003182 len = MAY_LJMP(luaL_checkinteger(L, 2));
3183 l = MAY_LJMP(luaL_checkinteger(L, -1));
3184
3185 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003186 if (max > ci_data(chn))
3187 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003188 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003189 l += max;
3190
3191 lua_pop(L, 1);
3192 lua_pushinteger(L, l);
3193
3194 /* Check if it miss bytes to forward. */
3195 if (l < len) {
3196 /* The the input channel or the output channel are closed, we
3197 * must return the amount of data forwarded.
3198 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003199 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003200 return 1;
3201
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003202 /* If we are waiting for space data in the response buffer, we
3203 * must set the flag WAKERESWR. This flag required the task
3204 * wake up if any activity is detected on the response buffer.
3205 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003206 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003207 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003208 else
3209 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003210
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003211 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003212 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003213 }
3214
3215 return 1;
3216}
3217
3218/* Just check the input and prepare the stack for the previous
3219 * function "hlua_channel_forward_yield"
3220 */
3221__LJMP static int hlua_channel_forward(lua_State *L)
3222{
3223 MAY_LJMP(check_args(L, 2, "forward"));
3224 MAY_LJMP(hlua_checkchannel(L, 1));
3225 MAY_LJMP(luaL_checkinteger(L, 2));
3226
3227 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003228 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003229}
3230
3231/* Just returns the number of bytes available in the input
3232 * side of the buffer. This function never fails.
3233 */
3234__LJMP static int hlua_channel_get_in_len(lua_State *L)
3235{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003236 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003237
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003238 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003239 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003240 if (IS_HTX_STRM(chn_strm(chn))) {
3241 struct htx *htx = htxbuf(&chn->buf);
3242 lua_pushinteger(L, htx->data - co_data(chn));
3243 }
3244 else
3245 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003246 return 1;
3247}
3248
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003249/* Returns true if the channel is full. */
3250__LJMP static int hlua_channel_is_full(lua_State *L)
3251{
3252 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003253
3254 MAY_LJMP(check_args(L, 1, "is_full"));
3255 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0136ebb2020-02-26 11:59:19 +01003256 /* ignore the reserve, we are not on a producer side (ie in an
3257 * applet).
3258 */
3259 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003260 return 1;
3261}
3262
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003263/* Just returns the number of bytes available in the output
3264 * side of the buffer. This function never fails.
3265 */
3266__LJMP static int hlua_channel_get_out_len(lua_State *L)
3267{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003268 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003269
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003270 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003271 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003272 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003273 return 1;
3274}
3275
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003276/*
3277 *
3278 *
3279 * Class Fetches
3280 *
3281 *
3282 */
3283
3284/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003285 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003286 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003287__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003288{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003289 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003290}
3291
3292/* This function creates and push in the stack a fetch object according
3293 * with a current TXN.
3294 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003295static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003296{
Willy Tarreau7073c472015-04-06 11:15:40 +02003297 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003298
3299 /* Check stack size. */
3300 if (!lua_checkstack(L, 3))
3301 return 0;
3302
3303 /* Create the object: obj[0] = userdata.
3304 * Note that the base of the Fetches object is the
3305 * transaction object.
3306 */
3307 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003308 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003309 lua_rawseti(L, -2, 0);
3310
Willy Tarreau7073c472015-04-06 11:15:40 +02003311 hsmp->s = txn->s;
3312 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003313 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003314 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003315
3316 /* Pop a class sesison metatable and affect it to the userdata. */
3317 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3318 lua_setmetatable(L, -2);
3319
3320 return 1;
3321}
3322
3323/* This function is an LUA binding. It is called with each sample-fetch.
3324 * It uses closure argument to store the associated sample-fetch. It
3325 * returns only one argument or throws an error. An error is thrown
3326 * only if an error is encountered during the argument parsing. If
3327 * the "sample-fetch" function fails, nil is returned.
3328 */
3329__LJMP static int hlua_run_sample_fetch(lua_State *L)
3330{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003331 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003332 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003333 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003334 int i;
3335 struct sample smp;
3336
3337 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003338 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003339
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003340 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003341 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003342
Thierry FOURNIERca988662015-12-20 18:43:03 +01003343 /* Check execution authorization. */
3344 if (f->use & SMP_USE_HTTP_ANY &&
3345 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3346 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3347 "is not available in Lua services", f->kw);
3348 WILL_LJMP(lua_error(L));
3349 }
3350
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003351 /* Get extra arguments. */
3352 for (i = 0; i < lua_gettop(L) - 1; i++) {
3353 if (i >= ARGM_NBARGS)
3354 break;
3355 hlua_lua2arg(L, i + 2, &args[i]);
3356 }
3357 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003358 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003359
3360 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003361 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003362
3363 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003364 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003365 lua_pushfstring(L, "error in arguments");
3366 WILL_LJMP(lua_error(L));
3367 }
3368
3369 /* Initialise the sample. */
3370 memset(&smp, 0, sizeof(smp));
3371
3372 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003373 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003374 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003375 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003376 lua_pushstring(L, "");
3377 else
3378 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003379 return 1;
3380 }
3381
3382 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003383 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003384 hlua_smp2lua_str(L, &smp);
3385 else
3386 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003387 return 1;
3388}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003389
3390/*
3391 *
3392 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003393 * Class Converters
3394 *
3395 *
3396 */
3397
3398/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003399 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003400 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003401__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003402{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003403 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003404}
3405
3406/* This function creates and push in the stack a Converters object
3407 * according with a current TXN.
3408 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003409static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003410{
Willy Tarreau7073c472015-04-06 11:15:40 +02003411 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003412
3413 /* Check stack size. */
3414 if (!lua_checkstack(L, 3))
3415 return 0;
3416
3417 /* Create the object: obj[0] = userdata.
3418 * Note that the base of the Converters object is the
3419 * same than the TXN object.
3420 */
3421 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003422 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003423 lua_rawseti(L, -2, 0);
3424
Willy Tarreau7073c472015-04-06 11:15:40 +02003425 hsmp->s = txn->s;
3426 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003427 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003428 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003429
Willy Tarreau87b09662015-04-03 00:22:06 +02003430 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003431 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3432 lua_setmetatable(L, -2);
3433
3434 return 1;
3435}
3436
3437/* This function is an LUA binding. It is called with each converter.
3438 * It uses closure argument to store the associated converter. It
3439 * returns only one argument or throws an error. An error is thrown
3440 * only if an error is encountered during the argument parsing. If
3441 * the converter function function fails, nil is returned.
3442 */
3443__LJMP static int hlua_run_sample_conv(lua_State *L)
3444{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003445 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003446 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003447 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003448 int i;
3449 struct sample smp;
3450
3451 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003452 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003453
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003454 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003455 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003456
3457 /* Get extra arguments. */
3458 for (i = 0; i < lua_gettop(L) - 2; i++) {
3459 if (i >= ARGM_NBARGS)
3460 break;
3461 hlua_lua2arg(L, i + 3, &args[i]);
3462 }
3463 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003464 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003465
3466 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003467 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003468
3469 /* Run the special args checker. */
3470 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3471 hlua_pusherror(L, "error in arguments");
3472 WILL_LJMP(lua_error(L));
3473 }
3474
3475 /* Initialise the sample. */
Amaury Denoyelleca492f32020-10-29 17:21:20 +01003476 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003477 if (!hlua_lua2smp(L, 2, &smp)) {
3478 hlua_pusherror(L, "error in the input argument");
3479 WILL_LJMP(lua_error(L));
3480 }
3481
Willy Tarreau1777ea62016-03-10 16:15:46 +01003482 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3483
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003484 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003485 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003486 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003487 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003488 WILL_LJMP(lua_error(L));
3489 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003490 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3491 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003492 hlua_pusherror(L, "error during the input argument casting");
3493 WILL_LJMP(lua_error(L));
3494 }
3495
3496 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003497 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003498 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003499 lua_pushstring(L, "");
3500 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003501 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003502 return 1;
3503 }
3504
3505 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003506 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003507 hlua_smp2lua_str(L, &smp);
3508 else
3509 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003510 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003511}
3512
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003513/*
3514 *
3515 *
3516 * Class AppletTCP
3517 *
3518 *
3519 */
3520
3521/* Returns a struct hlua_txn if the stack entry "ud" is
3522 * a class stream, otherwise it throws an error.
3523 */
3524__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3525{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003526 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003527}
3528
3529/* This function creates and push in the stack an Applet object
3530 * according with a current TXN.
3531 */
3532static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3533{
3534 struct hlua_appctx *appctx;
3535 struct stream_interface *si = ctx->owner;
3536 struct stream *s = si_strm(si);
3537 struct proxy *p = s->be;
3538
3539 /* Check stack size. */
3540 if (!lua_checkstack(L, 3))
3541 return 0;
3542
3543 /* Create the object: obj[0] = userdata.
3544 * Note that the base of the Converters object is the
3545 * same than the TXN object.
3546 */
3547 lua_newtable(L);
3548 appctx = lua_newuserdata(L, sizeof(*appctx));
3549 lua_rawseti(L, -2, 0);
3550 appctx->appctx = ctx;
3551 appctx->htxn.s = s;
3552 appctx->htxn.p = p;
3553
3554 /* Create the "f" field that contains a list of fetches. */
3555 lua_pushstring(L, "f");
3556 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3557 return 0;
3558 lua_settable(L, -3);
3559
3560 /* Create the "sf" field that contains a list of stringsafe fetches. */
3561 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003562 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003563 return 0;
3564 lua_settable(L, -3);
3565
3566 /* Create the "c" field that contains a list of converters. */
3567 lua_pushstring(L, "c");
3568 if (!hlua_converters_new(L, &appctx->htxn, 0))
3569 return 0;
3570 lua_settable(L, -3);
3571
3572 /* Create the "sc" field that contains a list of stringsafe converters. */
3573 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003574 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003575 return 0;
3576 lua_settable(L, -3);
3577
3578 /* Pop a class stream metatable and affect it to the table. */
3579 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3580 lua_setmetatable(L, -2);
3581
3582 return 1;
3583}
3584
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003585__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3586{
3587 struct hlua_appctx *appctx;
3588 struct stream *s;
3589 const char *name;
3590 size_t len;
3591 struct sample smp;
3592
3593 MAY_LJMP(check_args(L, 3, "set_var"));
3594
3595 /* It is useles to retrieve the stream, but this function
3596 * runs only in a stream context.
3597 */
3598 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3599 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3600 s = appctx->htxn.s;
3601
3602 /* Converts the third argument in a sample. */
Amaury Denoyelleca492f32020-10-29 17:21:20 +01003603 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003604 hlua_lua2smp(L, 3, &smp);
3605
3606 /* Store the sample in a variable. */
3607 smp_set_owner(&smp, s->be, s->sess, s, 0);
3608 vars_set_by_name(name, len, &smp);
3609 return 0;
3610}
3611
3612__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3613{
3614 struct hlua_appctx *appctx;
3615 struct stream *s;
3616 const char *name;
3617 size_t len;
3618 struct sample smp;
3619
3620 MAY_LJMP(check_args(L, 2, "unset_var"));
3621
3622 /* It is useles to retrieve the stream, but this function
3623 * runs only in a stream context.
3624 */
3625 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3626 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3627 s = appctx->htxn.s;
3628
3629 /* Unset the variable. */
3630 smp_set_owner(&smp, s->be, s->sess, s, 0);
3631 vars_unset_by_name(name, len, &smp);
3632 return 0;
3633}
3634
3635__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3636{
3637 struct hlua_appctx *appctx;
3638 struct stream *s;
3639 const char *name;
3640 size_t len;
3641 struct sample smp;
3642
3643 MAY_LJMP(check_args(L, 2, "get_var"));
3644
3645 /* It is useles to retrieve the stream, but this function
3646 * runs only in a stream context.
3647 */
3648 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3649 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3650 s = appctx->htxn.s;
3651
3652 smp_set_owner(&smp, s->be, s->sess, s, 0);
3653 if (!vars_get_by_name(name, len, &smp)) {
3654 lua_pushnil(L);
3655 return 1;
3656 }
3657
3658 return hlua_smp2lua(L, &smp);
3659}
3660
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003661__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3662{
3663 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3664 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003665 struct hlua *hlua;
3666
3667 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003668 if (!s->hlua)
3669 return 0;
3670 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003671
3672 MAY_LJMP(check_args(L, 2, "set_priv"));
3673
3674 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003675 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003676
3677 /* Get and store new value. */
3678 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3679 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3680
3681 return 0;
3682}
3683
3684__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3685{
3686 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3687 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003688 struct hlua *hlua;
3689
3690 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003691 if (!s->hlua) {
3692 lua_pushnil(L);
3693 return 1;
3694 }
3695 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003696
3697 /* Push configuration index in the stack. */
3698 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3699
3700 return 1;
3701}
3702
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003703/* If expected data not yet available, it returns a yield. This function
3704 * consumes the data in the buffer. It returns a string containing the
3705 * data. This string can be empty.
3706 */
3707__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3708{
3709 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3710 struct stream_interface *si = appctx->appctx->owner;
3711 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003712 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003713 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003714 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003715 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003717 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003718 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003719
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003720 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003721 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003722 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003723 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003724 }
3725
3726 /* End of data: commit the total strings and return. */
3727 if (ret < 0) {
3728 luaL_pushresult(&appctx->b);
3729 return 1;
3730 }
3731
3732 /* Ensure that the block 2 length is usable. */
3733 if (ret == 1)
3734 len2 = 0;
3735
3736 /* dont check the max length read and dont check. */
3737 luaL_addlstring(&appctx->b, blk1, len1);
3738 luaL_addlstring(&appctx->b, blk2, len2);
3739
3740 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003741 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003742 luaL_pushresult(&appctx->b);
3743 return 1;
3744}
3745
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003746/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003747__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3748{
3749 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3750
3751 /* Initialise the string catenation. */
3752 luaL_buffinit(L, &appctx->b);
3753
3754 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3755}
3756
3757/* If expected data not yet available, it returns a yield. This function
3758 * consumes the data in the buffer. It returns a string containing the
3759 * data. This string can be empty.
3760 */
3761__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3762{
3763 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3764 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003765 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003766 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003767 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003768 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003769 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003770 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003772 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003773 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003774
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003775 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003776 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003777 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003778 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003779 }
3780
3781 /* End of data: commit the total strings and return. */
3782 if (ret < 0) {
3783 luaL_pushresult(&appctx->b);
3784 return 1;
3785 }
3786
3787 /* Ensure that the block 2 length is usable. */
3788 if (ret == 1)
3789 len2 = 0;
3790
3791 if (len == -1) {
3792
3793 /* If len == -1, catenate all the data avalaile and
3794 * yield because we want to get all the data until
3795 * the end of data stream.
3796 */
3797 luaL_addlstring(&appctx->b, blk1, len1);
3798 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003799 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003800 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003801 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003802
3803 } else {
3804
3805 /* Copy the fisrt block caping to the length required. */
3806 if (len1 > len)
3807 len1 = len;
3808 luaL_addlstring(&appctx->b, blk1, len1);
3809 len -= len1;
3810
3811 /* Copy the second block. */
3812 if (len2 > len)
3813 len2 = len;
3814 luaL_addlstring(&appctx->b, blk2, len2);
3815 len -= len2;
3816
3817 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003818 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003819
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003820 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003821 if (len > 0) {
3822 lua_pushinteger(L, len);
3823 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003824 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003825 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003826 }
3827
3828 /* return the result. */
3829 luaL_pushresult(&appctx->b);
3830 return 1;
3831 }
3832
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003833 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003834 hlua_pusherror(L, "Lua: internal error");
3835 WILL_LJMP(lua_error(L));
3836 return 0;
3837}
3838
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003839/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003840__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3841{
3842 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3843 int len = -1;
3844
3845 if (lua_gettop(L) > 2)
3846 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3847 if (lua_gettop(L) >= 2) {
3848 len = MAY_LJMP(luaL_checkinteger(L, 2));
3849 lua_pop(L, 1);
3850 }
3851
3852 /* Confirm or set the required length */
3853 lua_pushinteger(L, len);
3854
3855 /* Initialise the string catenation. */
3856 luaL_buffinit(L, &appctx->b);
3857
3858 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3859}
3860
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003861/* Append data in the output side of the buffer. This data is immediately
3862 * sent. The function returns the amount of data written. If the buffer
3863 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003864 * if the channel is closed.
3865 */
3866__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3867{
3868 size_t len;
3869 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3870 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3871 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3872 struct stream_interface *si = appctx->appctx->owner;
3873 struct channel *chn = si_ic(si);
3874 int max;
3875
3876 /* Get the max amount of data which can write as input in the channel. */
3877 max = channel_recv_max(chn);
3878 if (max > (len - l))
3879 max = len - l;
3880
3881 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003882 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003883
3884 /* update counters. */
3885 l += max;
3886 lua_pop(L, 1);
3887 lua_pushinteger(L, l);
3888
3889 /* If some data is not send, declares the situation to the
3890 * applet, and returns a yield.
3891 */
3892 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003893 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003894 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003895 }
3896
3897 return 1;
3898}
3899
3900/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3901 * yield the LUA process, and resume it without checking the
3902 * input arguments.
3903 */
3904__LJMP static int hlua_applet_tcp_send(lua_State *L)
3905{
3906 MAY_LJMP(check_args(L, 2, "send"));
3907 lua_pushinteger(L, 0);
3908
3909 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3910}
3911
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003912/*
3913 *
3914 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003915 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003916 *
3917 *
3918 */
3919
3920/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003921 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003922 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003924{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003925 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003926}
3927
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003928/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003929 * according with a current TXN.
3930 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003931static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003932{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003933 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003934 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003935 struct stream_interface *si = ctx->owner;
3936 struct stream *s = si_strm(si);
3937 struct proxy *px = s->be;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003938
3939 /* Check stack size. */
3940 if (!lua_checkstack(L, 3))
3941 return 0;
3942
3943 /* Create the object: obj[0] = userdata.
3944 * Note that the base of the Converters object is the
3945 * same than the TXN object.
3946 */
3947 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003948 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003949 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003950 appctx->appctx = ctx;
3951 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003952 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003953 appctx->htxn.s = s;
3954 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003955
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003956 /* Create the "f" field that contains a list of fetches. */
3957 lua_pushstring(L, "f");
3958 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3959 return 0;
3960 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003961
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003962 /* Create the "sf" field that contains a list of stringsafe fetches. */
3963 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003964 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003965 return 0;
3966 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003967
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003968 /* Create the "c" field that contains a list of converters. */
3969 lua_pushstring(L, "c");
3970 if (!hlua_converters_new(L, &appctx->htxn, 0))
3971 return 0;
3972 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003973
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003974 /* Create the "sc" field that contains a list of stringsafe converters. */
3975 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003976 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003977 return 0;
3978 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003979
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003980 if (IS_HTX_STRM(s)) {
3981 /* HTX version */
3982 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet29f17582019-05-23 11:03:26 +02003983 struct htx_blk *blk;
3984 struct htx_sl *sl;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003985 struct ist path;
3986 unsigned long long len = 0;
3987 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003988
Christopher Faulet29f17582019-05-23 11:03:26 +02003989 blk = htx_get_first_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01003990 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Faulet29f17582019-05-23 11:03:26 +02003991 sl = htx_get_blk_ptr(htx, blk);
3992
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003993 /* Stores the request method. */
3994 lua_pushstring(L, "method");
3995 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3996 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003997
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003998 /* Stores the http version. */
3999 lua_pushstring(L, "version");
4000 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4001 lua_settable(L, -3);
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004002
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004003 /* creates an array of headers. hlua_http_get_headers() crates and push
4004 * the array on the top of the stack.
4005 */
4006 lua_pushstring(L, "headers");
4007 htxn.s = s;
4008 htxn.p = px;
4009 htxn.dir = SMP_OPT_DIR_REQ;
4010 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4011 return 0;
4012 lua_settable(L, -3);
4013
4014 path = http_get_path(htx_sl_req_uri(sl));
4015 if (path.ptr) {
4016 char *p, *q, *end;
4017
4018 p = path.ptr;
4019 end = path.ptr + path.len;
4020 q = p;
4021 while (q < end && *q != '?')
4022 q++;
4023
4024 /* Stores the request path. */
4025 lua_pushstring(L, "path");
4026 lua_pushlstring(L, p, q - p);
4027 lua_settable(L, -3);
4028
4029 /* Stores the query string. */
4030 lua_pushstring(L, "qs");
4031 if (*q == '?')
4032 q++;
4033 lua_pushlstring(L, q, end - q);
4034 lua_settable(L, -3);
4035 }
4036
Christopher Fauleta3f15502019-05-13 15:27:23 +02004037 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004038 struct htx_blk *blk = htx_get_blk(htx, pos);
4039 enum htx_blk_type type = htx_get_blk_type(blk);
4040
Christopher Faulet54b5e212019-06-04 10:08:28 +02004041 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004042 break;
4043 if (type == HTX_BLK_DATA)
4044 len += htx_get_blksz(blk);
4045 }
4046 if (htx->extra != ULLONG_MAX)
4047 len += htx->extra;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004048
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004049 /* Stores the request path. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004050 lua_pushstring(L, "length");
4051 lua_pushinteger(L, len);
4052 lua_settable(L, -3);
4053 }
4054 else {
4055 /* Legacy HTTP version */
4056 struct http_txn *txn = s->txn;
4057 const char *path;
4058 const char *end;
4059 const char *p;
4060
4061 /* Stores the request method. */
4062 lua_pushstring(L, "method");
4063 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004064 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004065
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004066 /* Stores the http version. */
4067 lua_pushstring(L, "version");
4068 lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004069 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004070
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004071 /* creates an array of headers. hlua_http_get_headers() crates and push
4072 * the array on the top of the stack.
4073 */
4074 lua_pushstring(L, "headers");
4075 htxn.s = s;
4076 htxn.p = px;
4077 htxn.dir = SMP_OPT_DIR_REQ;
4078 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4079 return 0;
4080 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004081
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004082 /* Get path and qs */
4083 path = http_txn_get_path(txn);
4084 if (path) {
4085 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4086 p = path;
4087 while (p < end && *p != '?')
4088 p++;
4089
4090 /* Stores the request path. */
4091 lua_pushstring(L, "path");
4092 lua_pushlstring(L, path, p - path);
4093 lua_settable(L, -3);
4094
4095 /* Stores the query string. */
4096 lua_pushstring(L, "qs");
4097 if (*p == '?')
4098 p++;
4099 lua_pushlstring(L, p, end - p);
4100 lua_settable(L, -3);
4101 }
4102
4103 /* Stores the request path. */
4104 lua_pushstring(L, "length");
4105 lua_pushinteger(L, txn->req.body_len);
4106 lua_settable(L, -3);
4107 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004108
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004109 /* Create an empty array of HTTP request headers. */
4110 lua_pushstring(L, "response");
4111 lua_newtable(L);
4112 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004113
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004114 /* Pop a class stream metatable and affect it to the table. */
4115 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4116 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004117
4118 return 1;
4119}
4120
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004121__LJMP static int hlua_applet_http_set_var(lua_State *L)
4122{
4123 struct hlua_appctx *appctx;
4124 struct stream *s;
4125 const char *name;
4126 size_t len;
4127 struct sample smp;
4128
4129 MAY_LJMP(check_args(L, 3, "set_var"));
4130
4131 /* It is useles to retrieve the stream, but this function
4132 * runs only in a stream context.
4133 */
4134 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4135 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4136 s = appctx->htxn.s;
4137
4138 /* Converts the third argument in a sample. */
Amaury Denoyelleca492f32020-10-29 17:21:20 +01004139 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004140 hlua_lua2smp(L, 3, &smp);
4141
4142 /* Store the sample in a variable. */
4143 smp_set_owner(&smp, s->be, s->sess, s, 0);
4144 vars_set_by_name(name, len, &smp);
4145 return 0;
4146}
4147
4148__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4149{
4150 struct hlua_appctx *appctx;
4151 struct stream *s;
4152 const char *name;
4153 size_t len;
4154 struct sample smp;
4155
4156 MAY_LJMP(check_args(L, 2, "unset_var"));
4157
4158 /* It is useles to retrieve the stream, but this function
4159 * runs only in a stream context.
4160 */
4161 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4162 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4163 s = appctx->htxn.s;
4164
4165 /* Unset the variable. */
4166 smp_set_owner(&smp, s->be, s->sess, s, 0);
4167 vars_unset_by_name(name, len, &smp);
4168 return 0;
4169}
4170
4171__LJMP static int hlua_applet_http_get_var(lua_State *L)
4172{
4173 struct hlua_appctx *appctx;
4174 struct stream *s;
4175 const char *name;
4176 size_t len;
4177 struct sample smp;
4178
4179 MAY_LJMP(check_args(L, 2, "get_var"));
4180
4181 /* It is useles to retrieve the stream, but this function
4182 * runs only in a stream context.
4183 */
4184 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4185 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4186 s = appctx->htxn.s;
4187
4188 smp_set_owner(&smp, s->be, s->sess, s, 0);
4189 if (!vars_get_by_name(name, len, &smp)) {
4190 lua_pushnil(L);
4191 return 1;
4192 }
4193
4194 return hlua_smp2lua(L, &smp);
4195}
4196
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004197__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4198{
4199 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4200 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004201 struct hlua *hlua;
4202
4203 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004204 if (!s->hlua)
4205 return 0;
4206 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004207
4208 MAY_LJMP(check_args(L, 2, "set_priv"));
4209
4210 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004211 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004212
4213 /* Get and store new value. */
4214 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4215 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4216
4217 return 0;
4218}
4219
4220__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4221{
4222 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4223 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004224 struct hlua *hlua;
4225
4226 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004227 if (!s->hlua) {
4228 lua_pushnil(L);
4229 return 1;
4230 }
4231 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004232
4233 /* Push configuration index in the stack. */
4234 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4235
4236 return 1;
4237}
4238
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004239/* If expected data not yet available, it returns a yield. This function
4240 * consumes the data in the buffer. It returns a string containing the
4241 * data. This string can be empty.
4242 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004243__LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx)
4244{
4245 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4246 struct stream_interface *si = appctx->appctx->owner;
4247 struct channel *req = si_oc(si);
4248 struct htx *htx;
4249 struct htx_blk *blk;
4250 size_t count;
4251 int stop = 0;
4252
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004253 htx = htx_from_buf(&req->buf);
4254 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004255 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004256
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004257 while (count && !stop && blk) {
4258 enum htx_blk_type type = htx_get_blk_type(blk);
4259 uint32_t sz = htx_get_blksz(blk);
4260 struct ist v;
4261 uint32_t vlen;
4262 char *nl;
4263
Christopher Faulete461e342018-12-18 16:43:35 +01004264 if (type == HTX_BLK_EOM) {
4265 stop = 1;
4266 break;
4267 }
4268
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004269 vlen = sz;
4270 if (vlen > count) {
4271 if (type != HTX_BLK_DATA)
4272 break;
4273 vlen = count;
4274 }
4275
4276 switch (type) {
4277 case HTX_BLK_UNUSED:
4278 break;
4279
4280 case HTX_BLK_DATA:
4281 v = htx_get_blk_value(htx, blk);
4282 v.len = vlen;
4283 nl = istchr(v, '\n');
4284 if (nl != NULL) {
4285 stop = 1;
4286 vlen = nl - v.ptr + 1;
4287 }
4288 luaL_addlstring(&appctx->b, v.ptr, vlen);
4289 break;
4290
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004291 case HTX_BLK_TLR:
4292 case HTX_BLK_EOM:
4293 stop = 1;
4294 break;
4295
4296 default:
4297 break;
4298 }
4299
4300 co_set_data(req, co_data(req) - vlen);
4301 count -= vlen;
4302 if (sz == vlen)
4303 blk = htx_remove_blk(htx, blk);
4304 else {
4305 htx_cut_data_blk(htx, blk, vlen);
4306 break;
4307 }
4308 }
4309
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004310 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004311 if (!stop) {
4312 si_cant_get(si);
4313 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0));
4314 }
4315
4316 /* return the result. */
4317 luaL_pushresult(&appctx->b);
4318 return 1;
4319}
4320
4321
4322/* If expected data not yet available, it returns a yield. This function
4323 * consumes the data in the buffer. It returns a string containing the
4324 * data. This string can be empty.
4325 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004326__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004327{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004328 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4329 struct stream_interface *si = appctx->appctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004330 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004331 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004332 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004333 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004334 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004335
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004336 /* Check for the end of the data. */
4337 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4338 luaL_pushresult(&appctx->b);
4339 return 1;
4340 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004341
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004342 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004343 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004344
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004345 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004346 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004347 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004348 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004349 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004350
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004351 /* End of data: commit the total strings and return. */
4352 if (ret < 0) {
4353 luaL_pushresult(&appctx->b);
4354 return 1;
4355 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004356
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004357 /* Ensure that the block 2 length is usable. */
4358 if (ret == 1)
4359 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004360
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004361 /* Copy the fisrt block caping to the length required. */
4362 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4363 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4364 luaL_addlstring(&appctx->b, blk1, len1);
4365 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004366
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004367 /* Copy the second block. */
4368 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4369 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4370 luaL_addlstring(&appctx->b, blk2, len2);
4371 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004372
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004373 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004374 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004375 luaL_pushresult(&appctx->b);
4376 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004377}
4378
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004379/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004380__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004381{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004382 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004383
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004384 /* Initialise the string catenation. */
4385 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004386
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004387 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
4388 return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0));
4389 else
4390 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004391}
4392
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004393/* If expected data not yet available, it returns a yield. This function
4394 * consumes the data in the buffer. It returns a string containing the
4395 * data. This string can be empty.
4396 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004397__LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx)
4398{
4399 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4400 struct stream_interface *si = appctx->appctx->owner;
4401 struct channel *req = si_oc(si);
4402 struct htx *htx;
4403 struct htx_blk *blk;
4404 size_t count;
4405 int len;
4406
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004407 htx = htx_from_buf(&req->buf);
4408 len = MAY_LJMP(luaL_checkinteger(L, 2));
4409 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004410 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004411 while (count && len && blk) {
4412 enum htx_blk_type type = htx_get_blk_type(blk);
4413 uint32_t sz = htx_get_blksz(blk);
4414 struct ist v;
4415 uint32_t vlen;
4416
Christopher Faulete461e342018-12-18 16:43:35 +01004417 if (type == HTX_BLK_EOM) {
4418 len = 0;
4419 break;
4420 }
4421
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004422 vlen = sz;
4423 if (len > 0 && vlen > len)
4424 vlen = len;
4425 if (vlen > count) {
4426 if (type != HTX_BLK_DATA)
4427 break;
4428 vlen = count;
4429 }
4430
4431 switch (type) {
4432 case HTX_BLK_UNUSED:
4433 break;
4434
4435 case HTX_BLK_DATA:
4436 v = htx_get_blk_value(htx, blk);
4437 luaL_addlstring(&appctx->b, v.ptr, vlen);
4438 break;
4439
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004440 case HTX_BLK_TLR:
4441 case HTX_BLK_EOM:
4442 len = 0;
4443 break;
4444
4445 default:
4446 break;
4447 }
4448
4449 co_set_data(req, co_data(req) - vlen);
4450 count -= vlen;
4451 if (len > 0)
4452 len -= vlen;
4453 if (sz == vlen)
4454 blk = htx_remove_blk(htx, blk);
4455 else {
4456 htx_cut_data_blk(htx, blk, vlen);
4457 break;
4458 }
4459 }
4460
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004461 htx_to_buf(htx, &req->buf);
4462
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004463 /* If we are no other data available, yield waiting for new data. */
4464 if (len) {
4465 if (len > 0) {
4466 lua_pushinteger(L, len);
4467 lua_replace(L, 2);
4468 }
4469 si_cant_get(si);
4470 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0));
4471 }
4472
4473 /* return the result. */
4474 luaL_pushresult(&appctx->b);
4475 return 1;
4476}
4477
4478/* If expected data not yet available, it returns a yield. This function
4479 * consumes the data in the buffer. It returns a string containing the
4480 * data. This string can be empty.
4481 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004482__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004483{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004484 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4485 struct stream_interface *si = appctx->appctx->owner;
4486 int len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004487 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004488 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004489 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004490 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004491 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004492
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004493 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004494 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004495
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004496 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004497 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004498 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004499 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004500 }
4501
4502 /* End of data: commit the total strings and return. */
4503 if (ret < 0) {
4504 luaL_pushresult(&appctx->b);
4505 return 1;
4506 }
4507
4508 /* Ensure that the block 2 length is usable. */
4509 if (ret == 1)
4510 len2 = 0;
4511
4512 /* Copy the fisrt block caping to the length required. */
4513 if (len1 > len)
4514 len1 = len;
4515 luaL_addlstring(&appctx->b, blk1, len1);
4516 len -= len1;
4517
4518 /* Copy the second block. */
4519 if (len2 > len)
4520 len2 = len;
4521 luaL_addlstring(&appctx->b, blk2, len2);
4522 len -= len2;
4523
4524 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004525 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004526 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4527 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4528
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004529 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004530 if (len > 0) {
4531 lua_pushinteger(L, len);
4532 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004533 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004534 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004535 }
4536
4537 /* return the result. */
4538 luaL_pushresult(&appctx->b);
4539 return 1;
4540}
4541
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004542/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004543__LJMP static int hlua_applet_http_recv(lua_State *L)
4544{
4545 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4546 int len = -1;
4547
4548 /* Check arguments. */
4549 if (lua_gettop(L) > 2)
4550 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4551 if (lua_gettop(L) >= 2) {
4552 len = MAY_LJMP(luaL_checkinteger(L, 2));
4553 lua_pop(L, 1);
4554 }
4555
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004556 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4557 /* HTX version */
4558 lua_pushinteger(L, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004559
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004560 /* Initialise the string catenation. */
4561 luaL_buffinit(L, &appctx->b);
4562
4563 return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0));
4564 }
4565 else {
4566 /* Legacy HTTP version */
4567 /* Check the required length */
4568 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4569 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4570 lua_pushinteger(L, len);
4571
4572 /* Initialise the string catenation. */
4573 luaL_buffinit(L, &appctx->b);
4574
4575 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4576 }
4577}
4578
4579/* Append data in the output side of the buffer. This data is immediately
4580 * sent. The function returns the amount of data written. If the buffer
4581 * cannot contain the data, the function yields. The function returns -1
4582 * if the channel is closed.
4583 */
4584__LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx)
4585{
4586 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4587 struct stream_interface *si = appctx->appctx->owner;
4588 struct channel *res = si_ic(si);
4589 struct htx *htx = htx_from_buf(&res->buf);
4590 const char *data;
4591 size_t len;
4592 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4593 int max;
4594
Christopher Faulet4e963012019-07-03 11:39:30 +02004595 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004596 if (!max)
4597 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004598
4599 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4600
4601 /* Get the max amount of data which can write as input in the channel. */
4602 if (max > (len - l))
4603 max = len - l;
4604
4605 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004606 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004607 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004608
4609 /* update counters. */
4610 l += max;
4611 lua_pop(L, 1);
4612 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004613
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004614 /* If some data is not send, declares the situation to the
4615 * applet, and returns a yield.
4616 */
4617 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004618 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004619 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004620 si_rx_room_blk(si);
4621 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4622 }
4623
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004624 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004625 return 1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004626}
4627
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004628/* Append data in the output side of the buffer. This data is immediately
4629 * sent. The function returns the amount of data written. If the buffer
4630 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004631 * if the channel is closed.
4632 */
4633__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4634{
4635 size_t len;
4636 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4637 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4638 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4639 struct stream_interface *si = appctx->appctx->owner;
4640 struct channel *chn = si_ic(si);
4641 int max;
4642
4643 /* Get the max amount of data which can write as input in the channel. */
4644 max = channel_recv_max(chn);
4645 if (max > (len - l))
4646 max = len - l;
4647
4648 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004649 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004650
4651 /* update counters. */
4652 l += max;
4653 lua_pop(L, 1);
4654 lua_pushinteger(L, l);
4655
4656 /* If some data is not send, declares the situation to the
4657 * applet, and returns a yield.
4658 */
4659 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004660 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004661 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004662 }
4663
4664 return 1;
4665}
4666
4667/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4668 * yield the LUA process, and resume it without checking the
4669 * input arguments.
4670 */
4671__LJMP static int hlua_applet_http_send(lua_State *L)
4672{
4673 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004674
4675 /* We want to send some data. Headers must be sent. */
4676 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4677 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4678 WILL_LJMP(lua_error(L));
4679 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004680
4681 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4682 /* HTX version */
4683 /* This interger is used for followinf the amount of data sent. */
4684 lua_pushinteger(L, 0);
4685
4686 return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0));
4687 }
4688 else {
4689 /* Legacy HTTP version */
4690 size_t len;
4691 char hex[10];
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004692
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004693 MAY_LJMP(luaL_checklstring(L, 2, &len));
4694
4695 /* If transfer encoding chunked is selected, we surround the data
4696 * by chunk data.
4697 */
4698 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4699 snprintf(hex, 9, "%x", (unsigned int)len);
4700 lua_pushfstring(L, "%s\r\n", hex);
4701 lua_insert(L, 2); /* swap the last 2 entries. */
4702 lua_pushstring(L, "\r\n");
4703 lua_concat(L, 3);
4704 }
4705
4706 /* This interger is used for followinf the amount of data sent. */
4707 lua_pushinteger(L, 0);
4708
4709 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4710 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004711}
4712
4713__LJMP static int hlua_applet_http_addheader(lua_State *L)
4714{
4715 const char *name;
4716 int ret;
4717
4718 MAY_LJMP(hlua_checkapplet_http(L, 1));
4719 name = MAY_LJMP(luaL_checkstring(L, 2));
4720 MAY_LJMP(luaL_checkstring(L, 3));
4721
4722 /* Push in the stack the "response" entry. */
4723 ret = lua_getfield(L, 1, "response");
4724 if (ret != LUA_TTABLE) {
4725 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4726 "is expected as an array. %s found", lua_typename(L, ret));
4727 WILL_LJMP(lua_error(L));
4728 }
4729
4730 /* check if the header is already registered if it is not
4731 * the case, register it.
4732 */
4733 ret = lua_getfield(L, -1, name);
4734 if (ret == LUA_TNIL) {
4735
4736 /* Entry not found. */
4737 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4738
4739 /* Insert the new header name in the array in the top of the stack.
4740 * It left the new array in the top of the stack.
4741 */
4742 lua_newtable(L);
4743 lua_pushvalue(L, 2);
4744 lua_pushvalue(L, -2);
4745 lua_settable(L, -4);
4746
4747 } else if (ret != LUA_TTABLE) {
4748
4749 /* corruption error. */
4750 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4751 "is expected as an array. %s found", name, lua_typename(L, ret));
4752 WILL_LJMP(lua_error(L));
4753 }
4754
4755 /* Now the top od thestack is an array of values. We push
4756 * the header value as new entry.
4757 */
4758 lua_pushvalue(L, 3);
4759 ret = lua_rawlen(L, -2);
4760 lua_rawseti(L, -2, ret + 1);
4761 lua_pushboolean(L, 1);
4762 return 1;
4763}
4764
4765__LJMP static int hlua_applet_http_status(lua_State *L)
4766{
4767 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4768 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004769 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004770
4771 if (status < 100 || status > 599) {
4772 lua_pushboolean(L, 0);
4773 return 1;
4774 }
4775
4776 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004777 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004778 lua_pushboolean(L, 1);
4779 return 1;
4780}
4781
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004782
4783__LJMP static int hlua_applet_htx_send_response(lua_State *L)
4784{
4785 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4786 struct stream_interface *si = appctx->appctx->owner;
4787 struct channel *res = si_ic(si);
4788 struct htx *htx;
4789 struct htx_sl *sl;
4790 struct h1m h1m;
4791 const char *status, *reason;
4792 const char *name, *value;
4793 size_t nlen, vlen;
4794 unsigned int flags;
4795
4796 /* Send the message at once. */
4797 htx = htx_from_buf(&res->buf);
4798 h1m_init_res(&h1m);
4799
4800 /* Use the same http version than the request. */
4801 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4802 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4803 if (reason == NULL)
4804 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4805 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4806 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4807 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4808 }
4809 else {
4810 flags = HTX_SL_F_IS_RESP;
4811 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4812 }
4813 if (!sl) {
4814 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4815 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4816 WILL_LJMP(lua_error(L));
4817 }
4818 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4819
4820 /* Get the array associated to the field "response" in the object AppletHTTP. */
4821 lua_pushvalue(L, 0);
4822 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4823 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4824 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4825 WILL_LJMP(lua_error(L));
4826 }
4827
4828 /* Browse the list of headers. */
4829 lua_pushnil(L);
4830 while(lua_next(L, -2) != 0) {
4831 /* We expect a string as -2. */
4832 if (lua_type(L, -2) != LUA_TSTRING) {
4833 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4834 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4835 lua_typename(L, lua_type(L, -2)));
4836 WILL_LJMP(lua_error(L));
4837 }
4838 name = lua_tolstring(L, -2, &nlen);
4839
4840 /* We expect an array as -1. */
4841 if (lua_type(L, -1) != LUA_TTABLE) {
4842 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4843 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4844 name,
4845 lua_typename(L, lua_type(L, -1)));
4846 WILL_LJMP(lua_error(L));
4847 }
4848
4849 /* Browse the table who is on the top of the stack. */
4850 lua_pushnil(L);
4851 while(lua_next(L, -2) != 0) {
4852 int id;
4853
4854 /* We expect a number as -2. */
4855 if (lua_type(L, -2) != LUA_TNUMBER) {
4856 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4857 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4858 name,
4859 lua_typename(L, lua_type(L, -2)));
4860 WILL_LJMP(lua_error(L));
4861 }
4862 id = lua_tointeger(L, -2);
4863
4864 /* We expect a string as -2. */
4865 if (lua_type(L, -1) != LUA_TSTRING) {
4866 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4867 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4868 name, id,
4869 lua_typename(L, lua_type(L, -1)));
4870 WILL_LJMP(lua_error(L));
4871 }
4872 value = lua_tolstring(L, -1, &vlen);
4873
4874 /* Simple Protocol checks. */
4875 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4876 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4877 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4878 struct ist v = ist2(value, vlen);
4879 int ret;
4880
4881 ret = h1_parse_cont_len_header(&h1m, &v);
4882 if (ret < 0) {
4883 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4884 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4885 name);
4886 WILL_LJMP(lua_error(L));
4887 }
4888 else if (ret == 0)
4889 goto next; /* Skip it */
4890 }
4891
4892 /* Add a new header */
4893 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4894 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4895 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4896 name);
4897 WILL_LJMP(lua_error(L));
4898 }
4899 next:
4900 /* Remove the array from the stack, and get next element with a remaining string. */
4901 lua_pop(L, 1);
4902 }
4903
4904 /* Remove the array from the stack, and get next element with a remaining string. */
4905 lua_pop(L, 1);
4906 }
4907
4908 if (h1m.flags & H1_MF_CHNK)
4909 h1m.flags &= ~H1_MF_CLEN;
4910 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4911 h1m.flags |= H1_MF_XFER_LEN;
4912
4913 /* Uset HTX start-line flags */
4914 if (h1m.flags & H1_MF_XFER_ENC)
4915 flags |= HTX_SL_F_XFER_ENC;
4916 if (h1m.flags & H1_MF_XFER_LEN) {
4917 flags |= HTX_SL_F_XFER_LEN;
4918 if (h1m.flags & H1_MF_CHNK)
4919 flags |= HTX_SL_F_CHNK;
4920 else if (h1m.flags & H1_MF_CLEN)
4921 flags |= HTX_SL_F_CLEN;
4922 if (h1m.body_len == 0)
4923 flags |= HTX_SL_F_BODYLESS;
4924 }
4925 sl->flags |= flags;
4926
4927 /* If we dont have a content-length set, and the HTTP version is 1.1
4928 * and the status code implies the presence of a message body, we must
4929 * announce a transfer encoding chunked. This is required by haproxy
4930 * for the keepalive compliance. If the applet annouces a transfer-encoding
4931 * chunked itslef, don't do anything.
4932 */
4933 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4934 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4935 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4936 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4937 /* Add a new header */
4938 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4939 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4940 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4941 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4942 WILL_LJMP(lua_error(L));
4943 }
4944 }
4945
4946 /* Finalize headers. */
4947 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4948 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4949 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4950 WILL_LJMP(lua_error(L));
4951 }
4952
4953 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4954 b_reset(&res->buf);
4955 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4956 WILL_LJMP(lua_error(L));
4957 }
4958
4959 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004960 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004961
4962 /* Headers sent, set the flag. */
4963 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4964 return 0;
4965
4966}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004967/* We will build the status line and the headers of the HTTP response.
4968 * We will try send at once if its not possible, we give back the hand
4969 * waiting for more room.
4970 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004971__LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4972{
4973 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4974 struct stream_interface *si = appctx->appctx->owner;
4975 struct channel *res = si_ic(si);
4976
4977 if (co_data(res)) {
4978 si_rx_room_blk(si);
4979 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0));
4980 }
4981 return MAY_LJMP(hlua_applet_htx_send_response(L));
4982}
4983
4984
4985__LJMP static int hlua_applet_htx_start_response(lua_State *L)
4986{
4987 return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0));
4988}
4989
4990/* We will build the status line and the headers of the HTTP response.
4991 * We will try send at once if its not possible, we give back the hand
4992 * waiting for more room.
4993 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004994__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4995{
4996 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4997 struct stream_interface *si = appctx->appctx->owner;
4998 struct channel *chn = si_ic(si);
4999 int ret;
5000 size_t len;
5001 const char *msg;
5002
5003 /* Get the message as the first argument on the stack. */
5004 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
5005
5006 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02005007 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005008
5009 /* if ret == -2 or -3 the channel closed or the message si too
5010 * big for the buffers.
5011 */
5012 if (ret == -2 || ret == -3) {
5013 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5014 WILL_LJMP(lua_error(L));
5015 }
5016
5017 /* If ret is -1, we dont have room in the buffer, so we yield. */
5018 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01005019 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005020 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005021 }
5022
5023 /* Headers sent, set the flag. */
5024 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
5025 return 0;
5026}
5027
5028__LJMP static int hlua_applet_http_start_response(lua_State *L)
5029{
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005030 struct buffer *tmp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005031 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005032 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02005033 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005034 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02005035 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005036 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005037 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005038 int hdr_chunked = 0;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005039 const char *reason;
5040
5041 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
5042 return MAY_LJMP(hlua_applet_htx_start_response(L));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005043
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005044 reason = appctx->appctx->ctx.hlua_apphttp.reason;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005045 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02005046 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005047
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005048 tmp = get_trash_chunk();
5049
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005050 /* Use the same http version than the request. */
5051 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01005052 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005053 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005054 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005055
5056 /* Get the array associated to the field "response" in the object AppletHTTP. */
5057 lua_pushvalue(L, 0);
5058 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5059 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
5060 appctx->appctx->rule->arg.hlua_rule->fcn.name);
5061 WILL_LJMP(lua_error(L));
5062 }
5063
5064 /* Browse the list of headers. */
5065 lua_pushnil(L);
5066 while(lua_next(L, -2) != 0) {
5067
5068 /* We expect a string as -2. */
5069 if (lua_type(L, -2) != LUA_TSTRING) {
5070 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
5071 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5072 lua_typename(L, lua_type(L, -2)));
5073 WILL_LJMP(lua_error(L));
5074 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005075 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005076
5077 /* We expect an array as -1. */
5078 if (lua_type(L, -1) != LUA_TTABLE) {
5079 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
5080 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5081 name,
5082 lua_typename(L, lua_type(L, -1)));
5083 WILL_LJMP(lua_error(L));
5084 }
5085
5086 /* Browse the table who is on the top of the stack. */
5087 lua_pushnil(L);
5088 while(lua_next(L, -2) != 0) {
5089
5090 /* We expect a number as -2. */
5091 if (lua_type(L, -2) != LUA_TNUMBER) {
5092 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
5093 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5094 name,
5095 lua_typename(L, lua_type(L, -2)));
5096 WILL_LJMP(lua_error(L));
5097 }
5098 id = lua_tointeger(L, -2);
5099
5100 /* We expect a string as -2. */
5101 if (lua_type(L, -1) != LUA_TSTRING) {
5102 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
5103 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5104 name, id,
5105 lua_typename(L, lua_type(L, -1)));
5106 WILL_LJMP(lua_error(L));
5107 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005108 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005109
5110 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005111 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
5112 memcpy(tmp->area + tmp->data, name, name_len);
5113 tmp->data += name_len;
5114 tmp->area[tmp->data++] = ':';
5115 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02005116
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005117 memcpy(tmp->area + tmp->data, value,
5118 value_len);
5119 tmp->data += value_len;
5120 tmp->area[tmp->data++] = '\r';
5121 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02005122 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005123
5124 /* Protocol checks. */
5125
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005126 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005127 * is done without control. If it contains a bad value,
5128 * the content-length remains negative so that we can
5129 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005130 */
Willy Tarreaua3294632017-08-23 11:24:47 +02005131 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005132 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005133
5134 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02005135 if (name_len == 17 && value_len == 7 &&
5136 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005137 strcasecmp("chunked", value) == 0)
5138 hdr_chunked = 1;
5139
5140 /* Remove the array from the stack, and get next element with a remaining string. */
5141 lua_pop(L, 1);
5142 }
5143
5144 /* Remove the array from the stack, and get next element with a remaining string. */
5145 lua_pop(L, 1);
5146 }
5147
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005148 /* If we dont have a content-length set, and the HTTP version is 1.1
5149 * and the status code implies the presence of a message body, we must
5150 * announce a transfer encoding chunked. This is required by haproxy
5151 * for the keepalive compliance. If the applet annouces a transfer-encoding
5152 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005153 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005154 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005155 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
5156 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
5157 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
5158 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005159 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
5160 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
5161 }
5162
5163 /* Finalize headers. */
5164 chunk_appendf(tmp, "\r\n");
5165
5166 /* Remove the last entry and the array of headers */
5167 lua_pop(L, 2);
5168
5169 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005170 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005171
5172 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
5173}
5174
5175/*
5176 *
5177 *
5178 * Class HTTP
5179 *
5180 *
5181 */
5182
5183/* Returns a struct hlua_txn if the stack entry "ud" is
5184 * a class stream, otherwise it throws an error.
5185 */
5186__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
5187{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005188 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005189}
5190
5191/* This function creates and push in the stack a HTTP object
5192 * according with a current TXN.
5193 */
5194static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5195{
5196 struct hlua_txn *htxn;
5197
5198 /* Check stack size. */
5199 if (!lua_checkstack(L, 3))
5200 return 0;
5201
5202 /* Create the object: obj[0] = userdata.
5203 * Note that the base of the Converters object is the
5204 * same than the TXN object.
5205 */
5206 lua_newtable(L);
5207 htxn = lua_newuserdata(L, sizeof(*htxn));
5208 lua_rawseti(L, -2, 0);
5209
5210 htxn->s = txn->s;
5211 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005212 htxn->dir = txn->dir;
5213 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005214
5215 /* Pop a class stream metatable and affect it to the table. */
5216 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5217 lua_setmetatable(L, -2);
5218
5219 return 1;
5220}
5221
5222/* This function creates ans returns an array of HTTP headers.
5223 * This function does not fails. It is used as wrapper with the
5224 * 2 following functions.
5225 */
5226__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5227{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005228 /* Create the table. */
5229 lua_newtable(L);
5230
5231 if (!htxn->s->txn)
5232 return 1;
5233
Christopher Faulet724a12c2018-12-13 22:12:15 +01005234 if (IS_HTX_STRM(htxn->s)) {
5235 /* HTX version */
5236 struct htx *htx = htxbuf(&msg->chn->buf);
5237 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005238
Christopher Fauleta3f15502019-05-13 15:27:23 +02005239 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet724a12c2018-12-13 22:12:15 +01005240 struct htx_blk *blk = htx_get_blk(htx, pos);
5241 enum htx_blk_type type = htx_get_blk_type(blk);
5242 struct ist n, v;
5243 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005244
Christopher Faulet724a12c2018-12-13 22:12:15 +01005245 if (type == HTX_BLK_HDR) {
5246 n = htx_get_blk_name(htx,blk);
5247 v = htx_get_blk_value(htx, blk);
5248 }
5249 else if (type == HTX_BLK_EOH)
5250 break;
5251 else
5252 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005253
Christopher Faulet724a12c2018-12-13 22:12:15 +01005254 /* Check for existing entry:
5255 * assume that the table is on the top of the stack, and
5256 * push the key in the stack, the function lua_gettable()
5257 * perform the lookup.
5258 */
5259 lua_pushlstring(L, n.ptr, n.len);
5260 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005261
Christopher Faulet724a12c2018-12-13 22:12:15 +01005262 switch (lua_type(L, -1)) {
5263 case LUA_TNIL:
5264 /* Table not found, create it. */
5265 lua_pop(L, 1); /* remove the nil value. */
5266 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5267 lua_newtable(L); /* create and push empty table. */
5268 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5269 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5270 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5271 break;
5272
5273 case LUA_TTABLE:
5274 /* Entry found: push the value in the table. */
5275 len = lua_rawlen(L, -1);
5276 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5277 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5278 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5279 break;
5280
5281 default:
5282 /* Other cases are errors. */
5283 hlua_pusherror(L, "internal error during the parsing of headers.");
5284 WILL_LJMP(lua_error(L));
5285 }
5286 }
5287 }
5288 else {
5289 /* Legacy HTTP version */
5290 const char *cur_ptr, *cur_next, *p;
5291 int old_idx, cur_idx;
5292 struct hdr_idx_elem *cur_hdr;
5293 const char *hn, *hv;
5294 int hnl, hvl;
5295 const char *in;
5296 char *out;
5297 int len;
5298
5299 /* Build array of headers. */
5300 old_idx = 0;
5301 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
5302
5303 while (1) {
5304 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
5305 if (!cur_idx)
5306 break;
5307 old_idx = cur_idx;
5308
5309 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
5310 cur_ptr = cur_next;
5311 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
5312
5313 /* Now we have one full header at cur_ptr of len cur_hdr->len,
5314 * and the next header starts at cur_next. We'll check
5315 * this header in the list as well as against the default
5316 * rule.
5317 */
5318
5319 /* look for ': *'. */
5320 hn = cur_ptr;
5321 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
5322 if (p >= cur_ptr+cur_hdr->len)
5323 continue;
5324 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005325 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005326 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
5327 p++;
5328 if (p >= cur_ptr+cur_hdr->len)
5329 continue;
5330 hv = p;
5331 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005332
Christopher Faulet724a12c2018-12-13 22:12:15 +01005333 /* Lowercase the key. Don't check the size of trash, it have
5334 * the size of one buffer and the input data contains in one
5335 * buffer.
5336 */
5337 out = trash.area;
5338 for (in=hn; in<hn+hnl; in++, out++)
5339 *out = tolower(*in);
5340 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005341
Christopher Faulet724a12c2018-12-13 22:12:15 +01005342 /* Check for existing entry:
5343 * assume that the table is on the top of the stack, and
5344 * push the key in the stack, the function lua_gettable()
5345 * perform the lookup.
5346 */
5347 lua_pushlstring(L, trash.area, hnl);
5348 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005349
Christopher Faulet724a12c2018-12-13 22:12:15 +01005350 switch (lua_type(L, -1)) {
5351 case LUA_TNIL:
5352 /* Table not found, create it. */
5353 lua_pop(L, 1); /* remove the nil value. */
5354 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
5355 lua_newtable(L); /* create and push empty table. */
5356 lua_pushlstring(L, hv, hvl); /* push header value. */
5357 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5358 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5359 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005360
Christopher Faulet724a12c2018-12-13 22:12:15 +01005361 case LUA_TTABLE:
5362 /* Entry found: push the value in the table. */
5363 len = lua_rawlen(L, -1);
5364 lua_pushlstring(L, hv, hvl); /* push header value. */
5365 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5366 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5367 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005368
Christopher Faulet724a12c2018-12-13 22:12:15 +01005369 default:
5370 /* Other cases are errors. */
5371 hlua_pusherror(L, "internal error during the parsing of headers.");
5372 WILL_LJMP(lua_error(L));
5373 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005374 }
5375 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005376 return 1;
5377}
5378
5379__LJMP static int hlua_http_req_get_headers(lua_State *L)
5380{
5381 struct hlua_txn *htxn;
5382
5383 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5384 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5385
Christopher Faulet2351ca22019-07-26 16:31:34 +02005386 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005387 WILL_LJMP(lua_error(L));
5388
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005389 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
5390}
5391
5392__LJMP static int hlua_http_res_get_headers(lua_State *L)
5393{
5394 struct hlua_txn *htxn;
5395
5396 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5397 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5398
Christopher Faulet2351ca22019-07-26 16:31:34 +02005399 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005400 WILL_LJMP(lua_error(L));
5401
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005402 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
5403}
5404
5405/* This function replace full header, or just a value in
5406 * the request or in the response. It is a wrapper fir the
5407 * 4 following functions.
5408 */
5409__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
5410 struct http_msg *msg, int action)
5411{
5412 size_t name_len;
5413 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5414 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5415 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Dragan Dosen26743032019-04-30 15:54:36 +02005416 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005417
Dragan Dosen26743032019-04-30 15:54:36 +02005418 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005419 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5420
Christopher Faulet5a8549e2019-06-17 13:36:06 +02005421 if (IS_HTX_STRM(htxn->s)) {
5422 struct htx *htx = htxbuf(&msg->chn->buf);
5423
5424 htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
5425 }
5426 else
5427 http_transform_header_str(htxn->s, msg, name, name_len, value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02005428 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005429 return 0;
5430}
5431
5432__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5433{
5434 struct hlua_txn *htxn;
5435
5436 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5437 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5438
Christopher Faulet2351ca22019-07-26 16:31:34 +02005439 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005440 WILL_LJMP(lua_error(L));
5441
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005442 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
5443}
5444
5445__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5446{
5447 struct hlua_txn *htxn;
5448
5449 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5450 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5451
Christopher Faulet2351ca22019-07-26 16:31:34 +02005452 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005453 WILL_LJMP(lua_error(L));
5454
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005455 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
5456}
5457
5458__LJMP static int hlua_http_req_rep_val(lua_State *L)
5459{
5460 struct hlua_txn *htxn;
5461
5462 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5463 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5464
Christopher Faulet2351ca22019-07-26 16:31:34 +02005465 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005466 WILL_LJMP(lua_error(L));
5467
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005468 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
5469}
5470
5471__LJMP static int hlua_http_res_rep_val(lua_State *L)
5472{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005473 struct hlua_txn *htxn;
5474
5475 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5476 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5477
Christopher Faulet2351ca22019-07-26 16:31:34 +02005478 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005479 WILL_LJMP(lua_error(L));
5480
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02005481 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005482}
5483
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005484/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005485 * It is a wrapper for the 2 following functions.
5486 */
5487__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5488{
5489 size_t len;
5490 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005491
Christopher Faulet724a12c2018-12-13 22:12:15 +01005492 if (IS_HTX_STRM(htxn->s)) {
5493 /* HTX version */
5494 struct htx *htx = htxbuf(&msg->chn->buf);
5495 struct http_hdr_ctx ctx;
5496
5497 ctx.blk = NULL;
5498 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5499 http_remove_header(htx, &ctx);
5500 }
5501 else {
5502 /* Legacy HTTP version */
5503 struct hdr_ctx ctx;
5504 struct http_txn *txn = htxn->s->txn;
5505
5506 ctx.idx = 0;
5507 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
5508 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5509 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005510 return 0;
5511}
5512
5513__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5514{
5515 struct hlua_txn *htxn;
5516
5517 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5518 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5519
Christopher Faulet2351ca22019-07-26 16:31:34 +02005520 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005521 WILL_LJMP(lua_error(L));
5522
Willy Tarreaueee5b512015-04-03 23:46:31 +02005523 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005524}
5525
5526__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5527{
5528 struct hlua_txn *htxn;
5529
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005530 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005531 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5532
Christopher Faulet2351ca22019-07-26 16:31:34 +02005533 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005534 WILL_LJMP(lua_error(L));
5535
Willy Tarreaueee5b512015-04-03 23:46:31 +02005536 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005537}
5538
5539/* This function adds an header. It is a wrapper used by
5540 * the 2 following functions.
5541 */
5542__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5543{
5544 size_t name_len;
5545 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5546 size_t value_len;
5547 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
5548 char *p;
5549
Christopher Faulet724a12c2018-12-13 22:12:15 +01005550 if (IS_HTX_STRM(htxn->s)) {
5551 /* HTX version */
5552 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005553
Christopher Faulet724a12c2018-12-13 22:12:15 +01005554 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5555 ist2(value, value_len)));
5556 }
5557 else {
5558 /* Legacy HTTP version */
5559 /* Check length. */
5560 trash.data = value_len + name_len + 2;
5561 if (trash.data > trash.size)
5562 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005563
Christopher Faulet724a12c2018-12-13 22:12:15 +01005564 /* Creates the header string. */
5565 p = trash.area;
5566 memcpy(p, name, name_len);
5567 p += name_len;
5568 *p = ':';
5569 p++;
5570 *p = ' ';
5571 p++;
5572 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005573
Christopher Faulet724a12c2018-12-13 22:12:15 +01005574 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5575 trash.area, trash.data) != 0);
5576 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005577 return 0;
5578}
5579
5580__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5581{
5582 struct hlua_txn *htxn;
5583
5584 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5585 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5586
Christopher Faulet2351ca22019-07-26 16:31:34 +02005587 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005588 WILL_LJMP(lua_error(L));
5589
Willy Tarreaueee5b512015-04-03 23:46:31 +02005590 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005591}
5592
5593__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5594{
5595 struct hlua_txn *htxn;
5596
5597 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5598 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5599
Christopher Faulet2351ca22019-07-26 16:31:34 +02005600 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005601 WILL_LJMP(lua_error(L));
5602
Willy Tarreaueee5b512015-04-03 23:46:31 +02005603 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005604}
5605
5606static int hlua_http_req_set_hdr(lua_State *L)
5607{
5608 struct hlua_txn *htxn;
5609
5610 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5611 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5612
Christopher Faulet2351ca22019-07-26 16:31:34 +02005613 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005614 WILL_LJMP(lua_error(L));
5615
Willy Tarreaueee5b512015-04-03 23:46:31 +02005616 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
5617 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005618}
5619
5620static int hlua_http_res_set_hdr(lua_State *L)
5621{
5622 struct hlua_txn *htxn;
5623
5624 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5625 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5626
Christopher Faulet2351ca22019-07-26 16:31:34 +02005627 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005628 WILL_LJMP(lua_error(L));
5629
Willy Tarreaueee5b512015-04-03 23:46:31 +02005630 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5631 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005632}
5633
5634/* This function set the method. */
5635static int hlua_http_req_set_meth(lua_State *L)
5636{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005637 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005638 size_t name_len;
5639 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005640
Christopher Faulet2351ca22019-07-26 16:31:34 +02005641 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005642 WILL_LJMP(lua_error(L));
5643
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005644 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005645 return 1;
5646}
5647
5648/* This function set the method. */
5649static int hlua_http_req_set_path(lua_State *L)
5650{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005651 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005652 size_t name_len;
5653 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005654
Christopher Faulet2351ca22019-07-26 16:31:34 +02005655 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005656 WILL_LJMP(lua_error(L));
5657
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005658 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005659 return 1;
5660}
5661
5662/* This function set the query-string. */
5663static int hlua_http_req_set_query(lua_State *L)
5664{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005665 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005666 size_t name_len;
5667 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005668
Christopher Faulet2351ca22019-07-26 16:31:34 +02005669 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005670 WILL_LJMP(lua_error(L));
5671
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005672 /* Check length. */
5673 if (name_len > trash.size - 1) {
5674 lua_pushboolean(L, 0);
5675 return 1;
5676 }
5677
5678 /* Add the mark question as prefix. */
5679 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005680 trash.area[trash.data++] = '?';
5681 memcpy(trash.area + trash.data, name, name_len);
5682 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005683
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005684 lua_pushboolean(L,
5685 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005686 return 1;
5687}
5688
5689/* This function set the uri. */
5690static int hlua_http_req_set_uri(lua_State *L)
5691{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005692 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005693 size_t name_len;
5694 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005695
Christopher Faulet2351ca22019-07-26 16:31:34 +02005696 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005697 WILL_LJMP(lua_error(L));
5698
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005699 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005700 return 1;
5701}
5702
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005703/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005704static int hlua_http_res_set_status(lua_State *L)
5705{
5706 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5707 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005708 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005709
Christopher Faulet2351ca22019-07-26 16:31:34 +02005710 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005711 WILL_LJMP(lua_error(L));
5712
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005713 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005714 return 0;
5715}
5716
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005717/*
5718 *
5719 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005720 * Class TXN
5721 *
5722 *
5723 */
5724
5725/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005726 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005727 */
5728__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5729{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005730 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005731}
5732
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005733__LJMP static int hlua_set_var(lua_State *L)
5734{
5735 struct hlua_txn *htxn;
5736 const char *name;
5737 size_t len;
5738 struct sample smp;
5739
5740 MAY_LJMP(check_args(L, 3, "set_var"));
5741
5742 /* It is useles to retrieve the stream, but this function
5743 * runs only in a stream context.
5744 */
5745 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5746 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5747
5748 /* Converts the third argument in a sample. */
Amaury Denoyelleca492f32020-10-29 17:21:20 +01005749 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005750 hlua_lua2smp(L, 3, &smp);
5751
5752 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005753 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005754 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005755 return 0;
5756}
5757
Christopher Faulet85d79c92016-11-09 16:54:56 +01005758__LJMP static int hlua_unset_var(lua_State *L)
5759{
5760 struct hlua_txn *htxn;
5761 const char *name;
5762 size_t len;
5763 struct sample smp;
5764
5765 MAY_LJMP(check_args(L, 2, "unset_var"));
5766
5767 /* It is useles to retrieve the stream, but this function
5768 * runs only in a stream context.
5769 */
5770 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5771 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5772
5773 /* Unset the variable. */
5774 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5775 vars_unset_by_name(name, len, &smp);
5776 return 0;
5777}
5778
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005779__LJMP static int hlua_get_var(lua_State *L)
5780{
5781 struct hlua_txn *htxn;
5782 const char *name;
5783 size_t len;
5784 struct sample smp;
5785
5786 MAY_LJMP(check_args(L, 2, "get_var"));
5787
5788 /* It is useles to retrieve the stream, but this function
5789 * runs only in a stream context.
5790 */
5791 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5792 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5793
Willy Tarreau7560dd42016-03-10 16:28:58 +01005794 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005795 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005796 lua_pushnil(L);
5797 return 1;
5798 }
5799
5800 return hlua_smp2lua(L, &smp);
5801}
5802
Willy Tarreau59551662015-03-10 14:23:13 +01005803__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005804{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005805 struct hlua *hlua;
5806
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005807 MAY_LJMP(check_args(L, 2, "set_priv"));
5808
Willy Tarreau87b09662015-04-03 00:22:06 +02005809 /* It is useles to retrieve the stream, but this function
5810 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005811 */
5812 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005813 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005814
5815 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005816 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005817
5818 /* Get and store new value. */
5819 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5820 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5821
5822 return 0;
5823}
5824
Willy Tarreau59551662015-03-10 14:23:13 +01005825__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005826{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005827 struct hlua *hlua;
5828
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005829 MAY_LJMP(check_args(L, 1, "get_priv"));
5830
Willy Tarreau87b09662015-04-03 00:22:06 +02005831 /* It is useles to retrieve the stream, but this function
5832 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005833 */
5834 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005835 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005836
5837 /* Push configuration index in the stack. */
5838 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5839
5840 return 1;
5841}
5842
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005843/* Create stack entry containing a class TXN. This function
5844 * return 0 if the stack does not contains free slots,
5845 * otherwise it returns 1.
5846 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005847static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005848{
Willy Tarreaude491382015-04-06 11:04:28 +02005849 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005850
5851 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005852 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005853 return 0;
5854
5855 /* NOTE: The allocation never fails. The failure
5856 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005857 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005858 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005859 /* Create the object: obj[0] = userdata. */
5860 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005861 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005862 lua_rawseti(L, -2, 0);
5863
Willy Tarreaude491382015-04-06 11:04:28 +02005864 htxn->s = s;
5865 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005866 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005867 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005868
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005869 /* Create the "f" field that contains a list of fetches. */
5870 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005871 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005872 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005873 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005874
5875 /* Create the "sf" field that contains a list of stringsafe fetches. */
5876 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005877 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005878 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005879 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005880
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005881 /* Create the "c" field that contains a list of converters. */
5882 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005883 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005884 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005885 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005886
5887 /* Create the "sc" field that contains a list of stringsafe converters. */
5888 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005889 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005890 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005891 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005892
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005893 /* Create the "req" field that contains the request channel object. */
5894 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005895 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005896 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005897 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005898
5899 /* Create the "res" field that contains the response channel object. */
5900 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005901 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005902 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005903 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005904
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005905 /* Creates the HTTP object is the current proxy allows http. */
5906 lua_pushstring(L, "http");
5907 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005908 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005909 return 0;
5910 }
5911 else
5912 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005913 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005914
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005915 /* Pop a class sesison metatable and affect it to the userdata. */
5916 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5917 lua_setmetatable(L, -2);
5918
5919 return 1;
5920}
5921
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005922__LJMP static int hlua_txn_deflog(lua_State *L)
5923{
5924 const char *msg;
5925 struct hlua_txn *htxn;
5926
5927 MAY_LJMP(check_args(L, 2, "deflog"));
5928 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5929 msg = MAY_LJMP(luaL_checkstring(L, 2));
5930
5931 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5932 return 0;
5933}
5934
5935__LJMP static int hlua_txn_log(lua_State *L)
5936{
5937 int level;
5938 const char *msg;
5939 struct hlua_txn *htxn;
5940
5941 MAY_LJMP(check_args(L, 3, "log"));
5942 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5943 level = MAY_LJMP(luaL_checkinteger(L, 2));
5944 msg = MAY_LJMP(luaL_checkstring(L, 3));
5945
5946 if (level < 0 || level >= NB_LOG_LEVELS)
5947 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5948
5949 hlua_sendlog(htxn->s->be, level, msg);
5950 return 0;
5951}
5952
5953__LJMP static int hlua_txn_log_debug(lua_State *L)
5954{
5955 const char *msg;
5956 struct hlua_txn *htxn;
5957
5958 MAY_LJMP(check_args(L, 2, "Debug"));
5959 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5960 msg = MAY_LJMP(luaL_checkstring(L, 2));
5961 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5962 return 0;
5963}
5964
5965__LJMP static int hlua_txn_log_info(lua_State *L)
5966{
5967 const char *msg;
5968 struct hlua_txn *htxn;
5969
5970 MAY_LJMP(check_args(L, 2, "Info"));
5971 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5972 msg = MAY_LJMP(luaL_checkstring(L, 2));
5973 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5974 return 0;
5975}
5976
5977__LJMP static int hlua_txn_log_warning(lua_State *L)
5978{
5979 const char *msg;
5980 struct hlua_txn *htxn;
5981
5982 MAY_LJMP(check_args(L, 2, "Warning"));
5983 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5984 msg = MAY_LJMP(luaL_checkstring(L, 2));
5985 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5986 return 0;
5987}
5988
5989__LJMP static int hlua_txn_log_alert(lua_State *L)
5990{
5991 const char *msg;
5992 struct hlua_txn *htxn;
5993
5994 MAY_LJMP(check_args(L, 2, "Alert"));
5995 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5996 msg = MAY_LJMP(luaL_checkstring(L, 2));
5997 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5998 return 0;
5999}
6000
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006001__LJMP static int hlua_txn_set_loglevel(lua_State *L)
6002{
6003 struct hlua_txn *htxn;
6004 int ll;
6005
6006 MAY_LJMP(check_args(L, 2, "set_loglevel"));
6007 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6008 ll = MAY_LJMP(luaL_checkinteger(L, 2));
6009
6010 if (ll < 0 || ll > 7)
6011 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
6012
6013 htxn->s->logs.level = ll;
6014 return 0;
6015}
6016
6017__LJMP static int hlua_txn_set_tos(lua_State *L)
6018{
6019 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006020 int tos;
6021
6022 MAY_LJMP(check_args(L, 2, "set_tos"));
6023 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6024 tos = MAY_LJMP(luaL_checkinteger(L, 2));
6025
Willy Tarreau1a18b542018-12-11 16:37:42 +01006026 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006027 return 0;
6028}
6029
6030__LJMP static int hlua_txn_set_mark(lua_State *L)
6031{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006032 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006033 int mark;
6034
6035 MAY_LJMP(check_args(L, 2, "set_mark"));
6036 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6037 mark = MAY_LJMP(luaL_checkinteger(L, 2));
6038
Lukas Tribusb59291a2019-08-11 18:03:45 +02006039 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006040 return 0;
6041}
6042
Patrick Hemmer268a7072018-05-11 12:52:31 -04006043__LJMP static int hlua_txn_set_priority_class(lua_State *L)
6044{
6045 struct hlua_txn *htxn;
6046
6047 MAY_LJMP(check_args(L, 2, "set_priority_class"));
6048 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6049 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6050 return 0;
6051}
6052
6053__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6054{
6055 struct hlua_txn *htxn;
6056
6057 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6058 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6059 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6060 return 0;
6061}
6062
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006063/* This function is an Lua binding that send pending data
6064 * to the client, and close the stream interface.
6065 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006066__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006067{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006068 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006069 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01006070 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006071
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006072 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006073 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006074 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006075
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006076 /* If the flags NOTERM is set, we cannot terminate the http
6077 * session, so we just end the execution of the current
6078 * lua code.
6079 */
6080 if (htxn->flags & HLUA_TXN_NOTERM) {
6081 WILL_LJMP(hlua_done(L));
6082 return 0;
6083 }
6084
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006085 ic = &htxn->s->req;
6086 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006087
Christopher Faulet72c69272019-07-26 16:40:24 +02006088 if (IS_HTX_STRM(htxn->s)) {
6089 htxn->s->txn->status = 0;
6090 http_reply_and_close(htxn->s, 0, NULL);
6091 ic->analysers &= AN_REQ_FLT_END;
6092 oc->analysers &= AN_RES_FLT_END;
6093 }
Christopher Fauletb58fb792019-07-16 10:52:40 +02006094 else {
6095 if (htxn->s->txn) {
6096 /* HTTP mode, let's stay in sync with the stream */
6097 b_del(&ic->buf, htxn->s->txn->req.sov);
6098 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
6099 htxn->s->txn->req.sov = 0;
6100
6101 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
6102 oc->analysers = AN_RES_HTTP_XFER_BODY;
6103 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
6104 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreau630ef452015-08-28 10:06:15 +02006105
Willy Tarreau630ef452015-08-28 10:06:15 +02006106 /* Note that if we want to support keep-alive, we need
6107 * to bypass the close/shutr_now calls below, but that
6108 * may only be done if the HTTP request was already
6109 * processed and the connection header is known (ie
6110 * not during TCP rules).
6111 */
Christopher Fauletb58fb792019-07-16 10:52:40 +02006112 }
Willy Tarreau630ef452015-08-28 10:06:15 +02006113
Christopher Fauletb58fb792019-07-16 10:52:40 +02006114 channel_auto_read(ic);
6115 channel_abort(ic);
6116 channel_auto_close(ic);
6117 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006118
Christopher Fauletb58fb792019-07-16 10:52:40 +02006119 oc->wex = tick_add_ifset(now_ms, oc->wto);
6120 channel_auto_read(oc);
6121 channel_auto_close(oc);
6122 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006123
Christopher Fauletb58fb792019-07-16 10:52:40 +02006124 ic->analysers = 0;
6125 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006126
Christopher Faulet72c69272019-07-26 16:40:24 +02006127 if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is
6128 htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
6129
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006130 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006131 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006132 return 0;
6133}
6134
6135__LJMP static int hlua_log(lua_State *L)
6136{
6137 int level;
6138 const char *msg;
6139
6140 MAY_LJMP(check_args(L, 2, "log"));
6141 level = MAY_LJMP(luaL_checkinteger(L, 1));
6142 msg = MAY_LJMP(luaL_checkstring(L, 2));
6143
6144 if (level < 0 || level >= NB_LOG_LEVELS)
6145 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6146
6147 hlua_sendlog(NULL, level, msg);
6148 return 0;
6149}
6150
6151__LJMP static int hlua_log_debug(lua_State *L)
6152{
6153 const char *msg;
6154
6155 MAY_LJMP(check_args(L, 1, "debug"));
6156 msg = MAY_LJMP(luaL_checkstring(L, 1));
6157 hlua_sendlog(NULL, LOG_DEBUG, msg);
6158 return 0;
6159}
6160
6161__LJMP static int hlua_log_info(lua_State *L)
6162{
6163 const char *msg;
6164
6165 MAY_LJMP(check_args(L, 1, "info"));
6166 msg = MAY_LJMP(luaL_checkstring(L, 1));
6167 hlua_sendlog(NULL, LOG_INFO, msg);
6168 return 0;
6169}
6170
6171__LJMP static int hlua_log_warning(lua_State *L)
6172{
6173 const char *msg;
6174
6175 MAY_LJMP(check_args(L, 1, "warning"));
6176 msg = MAY_LJMP(luaL_checkstring(L, 1));
6177 hlua_sendlog(NULL, LOG_WARNING, msg);
6178 return 0;
6179}
6180
6181__LJMP static int hlua_log_alert(lua_State *L)
6182{
6183 const char *msg;
6184
6185 MAY_LJMP(check_args(L, 1, "alert"));
6186 msg = MAY_LJMP(luaL_checkstring(L, 1));
6187 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006188 return 0;
6189}
6190
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006191__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006192{
6193 int wakeup_ms = lua_tointeger(L, -1);
6194 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006195 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006196 return 0;
6197}
6198
6199__LJMP static int hlua_sleep(lua_State *L)
6200{
6201 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006202 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006203
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006204 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006205
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006206 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006207 wakeup_ms = tick_add(now_ms, delay);
6208 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006209
Willy Tarreau9635e032018-10-16 17:52:55 +02006210 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006211 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006212}
6213
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006214__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006215{
6216 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006217 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006218
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006219 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006220
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006221 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006222 wakeup_ms = tick_add(now_ms, delay);
6223 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006224
Willy Tarreau9635e032018-10-16 17:52:55 +02006225 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006226 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006227}
6228
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006229/* This functionis an LUA binding. it permits to give back
6230 * the hand at the HAProxy scheduler. It is used when the
6231 * LUA processing consumes a lot of time.
6232 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006233__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006234{
6235 return 0;
6236}
6237
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006238__LJMP static int hlua_yield(lua_State *L)
6239{
Willy Tarreau9635e032018-10-16 17:52:55 +02006240 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006241 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006242}
6243
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006244/* This function change the nice of the currently executed
6245 * task. It is used set low or high priority at the current
6246 * task.
6247 */
Willy Tarreau59551662015-03-10 14:23:13 +01006248__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006249{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006250 struct hlua *hlua;
6251 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006252
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006253 MAY_LJMP(check_args(L, 1, "set_nice"));
6254 hlua = hlua_gethlua(L);
6255 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006256
6257 /* If he task is not set, I'm in a start mode. */
6258 if (!hlua || !hlua->task)
6259 return 0;
6260
6261 if (nice < -1024)
6262 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006263 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006264 nice = 1024;
6265
6266 hlua->task->nice = nice;
6267 return 0;
6268}
6269
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006270/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006271 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6272 * return an E_AGAIN signal, the emmiter of this signal must set a
6273 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006274 *
6275 * Task wrapper are longjmp safe because the only one Lua code
6276 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006277 */
Willy Tarreau83a5ff42019-08-21 14:14:50 +02006278struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006279{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006280 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006281 enum hlua_exec status;
6282
Christopher Faulet5bc99722018-04-25 10:34:45 +02006283 if (task->thread_mask == MAX_THREADS_MASK)
6284 task_set_affinity(task, tid_bit);
6285
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006286 /* If it is the first call to the task, we must initialize the
6287 * execution timeouts.
6288 */
6289 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006290 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006291
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006292 /* Execute the Lua code. */
6293 status = hlua_ctx_resume(hlua, 1);
6294
6295 switch (status) {
6296 /* finished or yield */
6297 case HLUA_E_OK:
6298 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006299 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006300 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006301 break;
6302
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006303 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006304 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006305 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006306 break;
6307
6308 /* finished with error. */
6309 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006310 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006311 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006312 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006313 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006314 break;
6315
6316 case HLUA_E_ERR:
6317 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006318 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006319 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006320 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006321 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006322 break;
6323 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006324 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006325}
6326
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006327/* This function is an LUA binding that register LUA function to be
6328 * executed after the HAProxy configuration parsing and before the
6329 * HAProxy scheduler starts. This function expect only one LUA
6330 * argument that is a function. This function returns nothing, but
6331 * throws if an error is encountered.
6332 */
6333__LJMP static int hlua_register_init(lua_State *L)
6334{
6335 struct hlua_init_function *init;
6336 int ref;
6337
6338 MAY_LJMP(check_args(L, 1, "register_init"));
6339
6340 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6341
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006342 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006343 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006344 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006345
6346 init->function_ref = ref;
6347 LIST_ADDQ(&hlua_init_functions, &init->l);
6348 return 0;
6349}
6350
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006351/* This functio is an LUA binding. It permits to register a task
6352 * executed in parallel of the main HAroxy activity. The task is
6353 * created and it is set in the HAProxy scheduler. It can be called
6354 * from the "init" section, "post init" or during the runtime.
6355 *
6356 * Lua prototype:
6357 *
6358 * <none> core.register_task(<function>)
6359 */
6360static int hlua_register_task(lua_State *L)
6361{
6362 struct hlua *hlua;
6363 struct task *task;
6364 int ref;
6365
6366 MAY_LJMP(check_args(L, 1, "register_task"));
6367
6368 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6369
Willy Tarreaubafbe012017-11-24 17:34:44 +01006370 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006371 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006372 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006373
Emeric Brunc60def82017-09-27 14:59:38 +02006374 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006375 if (!task)
6376 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6377
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006378 task->context = hlua;
6379 task->process = hlua_process_task;
6380
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006381 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006382 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006383
6384 /* Restore the function in the stack. */
6385 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6386 hlua->nargs = 0;
6387
6388 /* Schedule task. */
6389 task_schedule(task, now_ms);
6390
6391 return 0;
6392}
6393
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006394/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6395 * doesn't allow "yield" functions because the HAProxy engine cannot
6396 * resume converters.
6397 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006398static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006399{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006400 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006401 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006402 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006403
Willy Tarreaube508f12016-03-10 11:47:01 +01006404 if (!stream)
6405 return 0;
6406
Willy Tarreau87b09662015-04-03 00:22:06 +02006407 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006408 * Lua context can be not initialized. This behavior
6409 * permits to save performances because a systematic
6410 * Lua initialization cause 5% performances loss.
6411 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006412 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006413 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006414 if (!stream->hlua) {
6415 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6416 return 0;
6417 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006418 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006419 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6420 return 0;
6421 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006422 }
6423
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006424 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006425 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006426
6427 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006428 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6429 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6430 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006431 else
6432 error = "critical error";
6433 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006434 return 0;
6435 }
6436
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006437 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006438 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006439 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006440 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006441 return 0;
6442 }
6443
6444 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006445 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006446
6447 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006448 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006449 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006450 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006451 return 0;
6452 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006453 hlua_smp2lua(stream->hlua->T, smp);
6454 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006455
6456 /* push keywords in the stack. */
6457 if (arg_p) {
6458 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006459 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006460 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006461 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006462 return 0;
6463 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006464 hlua_arg2lua(stream->hlua->T, arg_p);
6465 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006466 }
6467 }
6468
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006469 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006470 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006471
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006472 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006473 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006474 }
6475
6476 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006477 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006478 /* finished. */
6479 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006480 /* If the stack is empty, the function fails. */
6481 if (lua_gettop(stream->hlua->T) <= 0)
6482 return 0;
6483
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006484 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006485 hlua_lua2smp(stream->hlua->T, -1, smp);
6486 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006487 return 1;
6488
6489 /* yield. */
6490 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006491 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006492 return 0;
6493
6494 /* finished with error. */
6495 case HLUA_E_ERRMSG:
6496 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006497 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006498 fcn->name, lua_tostring(stream->hlua->T, -1));
6499 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006500 return 0;
6501
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006502 case HLUA_E_ETMOUT:
6503 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6504 return 0;
6505
6506 case HLUA_E_NOMEM:
6507 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6508 return 0;
6509
6510 case HLUA_E_YIELD:
6511 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6512 return 0;
6513
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006514 case HLUA_E_ERR:
6515 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006516 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006517
6518 default:
6519 return 0;
6520 }
6521}
6522
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006523/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6524 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006525 * resume sample-fetches. This function will be called by the sample
6526 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006527 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006528static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6529 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006530{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006531 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006532 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006533 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006534 const struct buffer msg = { };
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006535 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006536
Willy Tarreaube508f12016-03-10 11:47:01 +01006537 if (!stream)
6538 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006539
Willy Tarreau87b09662015-04-03 00:22:06 +02006540 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006541 * Lua context can be not initialized. This behavior
6542 * permits to save performances because a systematic
6543 * Lua initialization cause 5% performances loss.
6544 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006545 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006546 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006547 if (!stream->hlua) {
6548 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6549 return 0;
6550 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006551 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006552 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6553 return 0;
6554 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006555 }
6556
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006557 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006558
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006559 if (stream->be->mode == PR_MODE_HTTP) {
6560 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
6561 hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6562 else
6563 hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6564 }
6565
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006566 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006568
6569 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006570 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6571 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6572 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006573 else
6574 error = "critical error";
6575 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006576 return 0;
6577 }
6578
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006579 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006580 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006581 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006582 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006583 return 0;
6584 }
6585
6586 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006587 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006588
6589 /* push arguments in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006590 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006591 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006592 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006593 return 0;
6594 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006595 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006596
6597 /* push keywords in the stack. */
6598 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6599 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006600 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006601 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006602 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006603 return 0;
6604 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006605 hlua_arg2lua(stream->hlua->T, arg_p);
6606 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006607 }
6608
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006609 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006611
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006612 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006613 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006614 }
6615
6616 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006617 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006618 /* finished. */
6619 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006620 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006621 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006622 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006623 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006624 /* If the stack is empty, the function fails. */
6625 if (lua_gettop(stream->hlua->T) <= 0)
6626 return 0;
6627
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006628 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006629 hlua_lua2smp(stream->hlua->T, -1, smp);
6630 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006631
6632 /* Set the end of execution flag. */
6633 smp->flags &= ~SMP_F_MAY_CHANGE;
6634 return 1;
6635
6636 /* yield. */
6637 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006638 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006639 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006640 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006641 return 0;
6642
6643 /* finished with error. */
6644 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006645 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006646 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006647 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006648 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006649 fcn->name, lua_tostring(stream->hlua->T, -1));
6650 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006651 return 0;
6652
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006653 case HLUA_E_ETMOUT:
6654 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006655 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006656 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6657 return 0;
6658
6659 case HLUA_E_NOMEM:
6660 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006661 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006662 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6663 return 0;
6664
6665 case HLUA_E_YIELD:
6666 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006667 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006668 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6669 return 0;
6670
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006671 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006672 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006673 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006674 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006675 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006676
6677 default:
6678 return 0;
6679 }
6680}
6681
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006682/* This function is an LUA binding used for registering
6683 * "sample-conv" functions. It expects a converter name used
6684 * in the haproxy configuration file, and an LUA function.
6685 */
6686__LJMP static int hlua_register_converters(lua_State *L)
6687{
6688 struct sample_conv_kw_list *sck;
6689 const char *name;
6690 int ref;
6691 int len;
6692 struct hlua_function *fcn;
Thierry Fourniera9230f82020-11-28 20:41:07 +01006693 struct sample_conv *sc;
6694 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006695
6696 MAY_LJMP(check_args(L, 2, "register_converters"));
6697
6698 /* First argument : converter name. */
6699 name = MAY_LJMP(luaL_checkstring(L, 1));
6700
6701 /* Second argument : lua function. */
6702 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6703
Thierry Fourniera9230f82020-11-28 20:41:07 +01006704 /* Check if the converter is already registered */
6705 trash = get_trash_chunk();
6706 chunk_printf(trash, "lua.%s", name);
6707 sc = find_sample_conv(trash->area, trash->data);
6708 if (sc != NULL) {
6709 ha_warning("Trying to register converter 'lua.%s' more than once. "
6710 "This will become a hard error in version 2.5.\n", name);
6711 }
6712
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006713 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006714 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006715 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006716 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006717 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006718 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006719 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006720
6721 /* Fill fcn. */
6722 fcn->name = strdup(name);
6723 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006724 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006725 fcn->function_ref = ref;
6726
6727 /* List head */
6728 sck->list.n = sck->list.p = NULL;
6729
6730 /* converter keyword. */
6731 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006732 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006733 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006734 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006735
6736 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6737 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006738 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 +01006739 sck->kw[0].val_args = NULL;
6740 sck->kw[0].in_type = SMP_T_STR;
6741 sck->kw[0].out_type = SMP_T_STR;
6742 sck->kw[0].private = fcn;
6743
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006744 /* Register this new converter */
6745 sample_register_convs(sck);
6746
6747 return 0;
6748}
6749
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006750/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006751 * "sample-fetch" functions. It expects a converter name used
6752 * in the haproxy configuration file, and an LUA function.
6753 */
6754__LJMP static int hlua_register_fetches(lua_State *L)
6755{
6756 const char *name;
6757 int ref;
6758 int len;
6759 struct sample_fetch_kw_list *sfk;
6760 struct hlua_function *fcn;
Thierry Fourniera9230f82020-11-28 20:41:07 +01006761 struct sample_fetch *sf;
6762 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006763
6764 MAY_LJMP(check_args(L, 2, "register_fetches"));
6765
6766 /* First argument : sample-fetch name. */
6767 name = MAY_LJMP(luaL_checkstring(L, 1));
6768
6769 /* Second argument : lua function. */
6770 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6771
Thierry Fourniera9230f82020-11-28 20:41:07 +01006772 /* Check if the sample-fetch is already registered */
6773 trash = get_trash_chunk();
6774 chunk_printf(trash, "lua.%s", name);
6775 sf = find_sample_fetch(trash->area, trash->data);
6776 if (sf != NULL) {
6777 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6778 "This will become a hard error in version 2.5.\n", name);
6779 }
6780
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006781 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006782 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006783 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006784 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006785 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006786 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006787 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006788
6789 /* Fill fcn. */
6790 fcn->name = strdup(name);
6791 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006792 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006793 fcn->function_ref = ref;
6794
6795 /* List head */
6796 sfk->list.n = sfk->list.p = NULL;
6797
6798 /* sample-fetch keyword. */
6799 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006800 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006801 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006802 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006803
6804 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6805 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006806 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 +01006807 sfk->kw[0].val_args = NULL;
6808 sfk->kw[0].out_type = SMP_T_STR;
6809 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6810 sfk->kw[0].val = 0;
6811 sfk->kw[0].private = fcn;
6812
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006813 /* Register this new fetch. */
6814 sample_register_fetches(sfk);
6815
6816 return 0;
6817}
6818
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006819/* This function is a wrapper to execute each LUA function declared
6820 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006821 * return ACT_RET_CONT if the processing is finished (with or without
6822 * error) and return ACT_RET_YIELD if the function must be called again
6823 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006824 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006825static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006826 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006827{
6828 char **arg;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006829 unsigned int hflags = 0;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006830 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006831 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006832 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006833
6834 switch (rule->from) {
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006835 case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break;
6836 case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break;
6837 case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
6838 case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006839 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006840 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006841 return ACT_RET_CONT;
6842 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006843
Willy Tarreau87b09662015-04-03 00:22:06 +02006844 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006845 * Lua context can be not initialized. This behavior
6846 * permits to save performances because a systematic
6847 * Lua initialization cause 5% performances loss.
6848 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006849 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006850 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006851 if (!s->hlua) {
6852 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6853 rule->arg.hlua_rule->fcn.name);
6854 return ACT_RET_CONT;
6855 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006856 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006857 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6858 rule->arg.hlua_rule->fcn.name);
6859 return ACT_RET_CONT;
6860 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006861 }
6862
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006863 consistency_set(s, dir, &s->hlua->cons);
6864
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006865 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006866 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006867
6868 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006869 if (!SET_SAFE_LJMP(s->hlua->T)) {
6870 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6871 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006872 else
6873 error = "critical error";
6874 SEND_ERR(px, "Lua function '%s': %s.\n",
6875 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006876 return ACT_RET_CONT;
6877 }
6878
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006879 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006880 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006881 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006882 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006883 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006884 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006885 }
6886
6887 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006888 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006889
Willy Tarreau87b09662015-04-03 00:22:06 +02006890 /* Create and and push object stream in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006891 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006892 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006893 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006894 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006895 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006896 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006897 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006898
6899 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006900 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006901 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006902 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006903 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006904 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006905 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006906 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006907 lua_pushstring(s->hlua->T, *arg);
6908 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006909 }
6910
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006911 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006912 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006913
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006914 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006915 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006916 }
6917
Christopher Faulet719ddc82020-06-02 18:46:07 +02006918 /* Always reset the analyse expiration timeout for the corresponding
6919 * channel in case the lua script yield, to be sure to not keep an
6920 * expired timeout.
6921 */
6922 if (dir == SMP_OPT_DIR_REQ)
6923 s->req.analyse_exp = TICK_ETERNITY;
6924 else
6925 s->res.analyse_exp = TICK_ETERNITY;
6926
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006927 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006928 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006929 /* finished. */
6930 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006931 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006932 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006933 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006934 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006935 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet4629d082019-07-04 11:27:15 +02006936 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006937 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006938
6939 /* yield. */
6940 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006941 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006942 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Fauletea0b0e22019-08-14 23:19:45 +02006943 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006944 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006945 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006946 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006947 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006948 /* Some actions can be wake up when a "write" event
6949 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006950 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006951 */
Christopher Fauletb22f6502019-07-26 14:54:52 +02006952 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006953 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006954 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006955 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006956 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006957 * because HAProxy is not able to manipulate data, it
6958 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006959 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006960
6961 /* finished with error. */
6962 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006963 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006964 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006965 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006966 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006967 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006968 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006969 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6970 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006971 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006972
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006973 case HLUA_E_ETMOUT:
6974 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006975 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006976 return ACT_RET_ERR;
6977 }
6978 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6979 return 0;
6980
6981 case HLUA_E_NOMEM:
6982 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006983 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006984 return ACT_RET_ERR;
6985 }
6986 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6987 return 0;
6988
6989 case HLUA_E_YIELD:
6990 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006991 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006992 return ACT_RET_ERR;
6993 }
6994 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6995 rule->arg.hlua_rule->fcn.name);
6996 return 0;
6997
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006998 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006999 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01007000 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02007001 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01007002 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007003 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007004 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007005 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007006
7007 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02007008 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007009 }
7010}
7011
Olivier Houchard9f6af332018-05-25 14:04:04 +02007012struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007013{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007014 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007015
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007016 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007017 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007018 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007019}
7020
7021static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7022{
7023 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007024 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007025 struct task *task;
7026 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007027 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007028
Willy Tarreaubafbe012017-11-24 17:34:44 +01007029 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007030 if (!hlua) {
7031 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
7032 ctx->rule->arg.hlua_rule->fcn.name);
7033 return 0;
7034 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007035 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007036 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007037 ctx->ctx.hlua_apptcp.flags = 0;
7038
7039 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007040 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007041 if (!task) {
7042 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
7043 ctx->rule->arg.hlua_rule->fcn.name);
7044 return 0;
7045 }
7046 task->nice = 0;
7047 task->context = ctx;
7048 task->process = hlua_applet_wakeup;
7049 ctx->ctx.hlua_apptcp.task = task;
7050
7051 /* In the execution wrappers linked with a stream, the
7052 * Lua context can be not initialized. This behavior
7053 * permits to save performances because a systematic
7054 * Lua initialization cause 5% performances loss.
7055 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007056 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007057 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
7058 ctx->rule->arg.hlua_rule->fcn.name);
7059 return 0;
7060 }
7061
7062 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007063 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007064
7065 /* The following Lua calls can fail. */
7066 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007067 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7068 error = lua_tostring(hlua->T, -1);
7069 else
7070 error = "critical error";
7071 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7072 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007073 return 0;
7074 }
7075
7076 /* Check stack available size. */
7077 if (!lua_checkstack(hlua->T, 1)) {
7078 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7079 ctx->rule->arg.hlua_rule->fcn.name);
7080 RESET_SAFE_LJMP(hlua->T);
7081 return 0;
7082 }
7083
7084 /* Restore the function in the stack. */
7085 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7086
7087 /* Create and and push object stream in the stack. */
7088 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7089 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7090 ctx->rule->arg.hlua_rule->fcn.name);
7091 RESET_SAFE_LJMP(hlua->T);
7092 return 0;
7093 }
7094 hlua->nargs = 1;
7095
7096 /* push keywords in the stack. */
7097 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7098 if (!lua_checkstack(hlua->T, 1)) {
7099 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7100 ctx->rule->arg.hlua_rule->fcn.name);
7101 RESET_SAFE_LJMP(hlua->T);
7102 return 0;
7103 }
7104 lua_pushstring(hlua->T, *arg);
7105 hlua->nargs++;
7106 }
7107
7108 RESET_SAFE_LJMP(hlua->T);
7109
7110 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007111 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007112 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007113
7114 return 1;
7115}
7116
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007117void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007118{
7119 struct stream_interface *si = ctx->owner;
7120 struct stream *strm = si_strm(si);
7121 struct channel *res = si_ic(si);
7122 struct act_rule *rule = ctx->rule;
7123 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007124 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007125
7126 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007127 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7128 /* eat the whole request */
7129 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007130 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007131 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007132
7133 /* If the stream is disconnect or closed, ldo nothing. */
7134 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7135 return;
7136
7137 /* Execute the function. */
7138 switch (hlua_ctx_resume(hlua, 1)) {
7139 /* finished. */
7140 case HLUA_E_OK:
7141 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7142
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007143 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007144 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007145 res->flags |= CF_READ_NULL;
7146 si_shutr(si);
7147 return;
7148
7149 /* yield. */
7150 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007151 if (hlua->wake_time != TICK_ETERNITY)
7152 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007153 return;
7154
7155 /* finished with error. */
7156 case HLUA_E_ERRMSG:
7157 /* Display log. */
7158 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7159 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7160 lua_pop(hlua->T, 1);
7161 goto error;
7162
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007163 case HLUA_E_ETMOUT:
7164 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7165 rule->arg.hlua_rule->fcn.name);
7166 goto error;
7167
7168 case HLUA_E_NOMEM:
7169 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7170 rule->arg.hlua_rule->fcn.name);
7171 goto error;
7172
7173 case HLUA_E_YIELD: /* unexpected */
7174 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7175 rule->arg.hlua_rule->fcn.name);
7176 goto error;
7177
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007178 case HLUA_E_ERR:
7179 /* Display log. */
7180 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7181 rule->arg.hlua_rule->fcn.name);
7182 goto error;
7183
7184 default:
7185 goto error;
7186 }
7187
7188error:
7189
7190 /* For all other cases, just close the stream. */
7191 si_shutw(si);
7192 si_shutr(si);
7193 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7194}
7195
7196static void hlua_applet_tcp_release(struct appctx *ctx)
7197{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007198 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007199 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007200 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007201 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007202}
7203
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007204/* The function returns 1 if the initialisation is complete, 0 if
7205 * an errors occurs and -1 if more data are required for initializing
7206 * the applet.
7207 */
7208static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7209{
7210 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007211 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007212 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007213 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007214 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007215 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007216
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007217 txn = strm->txn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007218
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007219 /* We want two things in HTTP mode :
7220 * - enforce server-close mode if we were in keep-alive, so that the
7221 * applet is released after each response ;
7222 * - enable request body transfer to the applet in order to resync
7223 * with the response body.
7224 */
7225 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
7226 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007227
Willy Tarreaubafbe012017-11-24 17:34:44 +01007228 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007229 if (!hlua) {
7230 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7231 ctx->rule->arg.hlua_rule->fcn.name);
7232 return 0;
7233 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007234 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007235 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007236 ctx->ctx.hlua_apphttp.left_bytes = -1;
7237 ctx->ctx.hlua_apphttp.flags = 0;
7238
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007239 if (txn->req.flags & HTTP_MSGF_VER_11)
7240 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7241
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007242 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007243 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007244 if (!task) {
7245 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7246 ctx->rule->arg.hlua_rule->fcn.name);
7247 return 0;
7248 }
7249 task->nice = 0;
7250 task->context = ctx;
7251 task->process = hlua_applet_wakeup;
7252 ctx->ctx.hlua_apphttp.task = task;
7253
7254 /* In the execution wrappers linked with a stream, the
7255 * Lua context can be not initialized. This behavior
7256 * permits to save performances because a systematic
7257 * Lua initialization cause 5% performances loss.
7258 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007259 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007260 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7261 ctx->rule->arg.hlua_rule->fcn.name);
7262 return 0;
7263 }
7264
7265 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007266 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007267
7268 /* The following Lua calls can fail. */
7269 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007270 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7271 error = lua_tostring(hlua->T, -1);
7272 else
7273 error = "critical error";
7274 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7275 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007276 return 0;
7277 }
7278
7279 /* Check stack available size. */
7280 if (!lua_checkstack(hlua->T, 1)) {
7281 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7282 ctx->rule->arg.hlua_rule->fcn.name);
7283 RESET_SAFE_LJMP(hlua->T);
7284 return 0;
7285 }
7286
7287 /* Restore the function in the stack. */
7288 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7289
7290 /* Create and and push object stream in the stack. */
7291 if (!hlua_applet_http_new(hlua->T, ctx)) {
7292 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7293 ctx->rule->arg.hlua_rule->fcn.name);
7294 RESET_SAFE_LJMP(hlua->T);
7295 return 0;
7296 }
7297 hlua->nargs = 1;
7298
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007299 /* push keywords in the stack. */
7300 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7301 if (!lua_checkstack(hlua->T, 1)) {
7302 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7303 ctx->rule->arg.hlua_rule->fcn.name);
7304 RESET_SAFE_LJMP(hlua->T);
7305 return 0;
7306 }
7307 lua_pushstring(hlua->T, *arg);
7308 hlua->nargs++;
7309 }
7310
7311 RESET_SAFE_LJMP(hlua->T);
7312
7313 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007314 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007315
7316 return 1;
7317}
7318
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007319static void hlua_applet_htx_fct(struct appctx *ctx)
7320{
7321 struct stream_interface *si = ctx->owner;
7322 struct stream *strm = si_strm(si);
7323 struct channel *req = si_oc(si);
7324 struct channel *res = si_ic(si);
7325 struct act_rule *rule = ctx->rule;
7326 struct proxy *px = strm->be;
7327 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7328 struct htx *req_htx, *res_htx;
7329
7330 res_htx = htx_from_buf(&res->buf);
7331
7332 /* If the stream is disconnect or closed, ldo nothing. */
7333 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7334 goto out;
7335
7336 /* Check if the input buffer is avalaible. */
7337 if (!b_size(&res->buf)) {
7338 si_rx_room_blk(si);
7339 goto out;
7340 }
7341 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007342 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007343 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7344
7345 /* Set the currently running flag. */
7346 if (!HLUA_IS_RUNNING(hlua) &&
7347 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7348 struct htx_blk *blk;
7349 size_t count = co_data(req);
7350
7351 if (!count) {
7352 si_cant_get(si);
7353 goto out;
7354 }
7355
7356 /* We need to flush the request header. This left the body for
7357 * the Lua.
7358 */
7359 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007360 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007361 while (count && blk) {
7362 enum htx_blk_type type = htx_get_blk_type(blk);
7363 uint32_t sz = htx_get_blksz(blk);
7364
7365 if (sz > count) {
7366 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007367 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007368 goto out;
7369 }
7370
7371 count -= sz;
7372 co_set_data(req, co_data(req) - sz);
7373 blk = htx_remove_blk(req_htx, blk);
7374
7375 if (type == HTX_BLK_EOH)
7376 break;
7377 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007378 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007379 }
7380
7381 /* Executes The applet if it is not done. */
7382 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7383
7384 /* Execute the function. */
7385 switch (hlua_ctx_resume(hlua, 1)) {
7386 /* finished. */
7387 case HLUA_E_OK:
7388 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7389 break;
7390
7391 /* yield. */
7392 case HLUA_E_AGAIN:
7393 if (hlua->wake_time != TICK_ETERNITY)
7394 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007395 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007396
7397 /* finished with error. */
7398 case HLUA_E_ERRMSG:
7399 /* Display log. */
7400 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7401 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7402 lua_pop(hlua->T, 1);
7403 goto error;
7404
7405 case HLUA_E_ETMOUT:
7406 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7407 rule->arg.hlua_rule->fcn.name);
7408 goto error;
7409
7410 case HLUA_E_NOMEM:
7411 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7412 rule->arg.hlua_rule->fcn.name);
7413 goto error;
7414
7415 case HLUA_E_YIELD: /* unexpected */
7416 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7417 rule->arg.hlua_rule->fcn.name);
7418 goto error;
7419
7420 case HLUA_E_ERR:
7421 /* Display log. */
7422 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7423 rule->arg.hlua_rule->fcn.name);
7424 goto error;
7425
7426 default:
7427 goto error;
7428 }
7429 }
7430
7431 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007432 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7433 goto done;
7434
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007435 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7436 goto error;
7437
Christopher Faulet54b5e212019-06-04 10:08:28 +02007438 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007439 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7440 si_rx_room_blk(si);
7441 goto out;
7442 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007443 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007444 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7445 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007446 }
7447
7448 done:
7449 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007450 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007451 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007452 si_shutr(si);
7453 }
7454
7455 /* eat the whole request */
7456 if (co_data(req)) {
7457 req_htx = htx_from_buf(&req->buf);
7458 co_htx_skip(req, req_htx, co_data(req));
7459 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007460 }
7461 }
7462
7463 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007464 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007465 return;
7466
7467 error:
7468
7469 /* If we are in HTTP mode, and we are not send any
7470 * data, return a 500 server error in best effort:
7471 * if there is no room available in the buffer,
7472 * just close the connection.
7473 */
7474 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7475 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
7476
7477 channel_erase(res);
7478 res->buf.data = b_data(err);
7479 memcpy(res->buf.area, b_head(err), b_data(err));
7480 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007481 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007482 }
7483 if (!(strm->flags & SF_ERR_MASK))
7484 strm->flags |= SF_ERR_RESOURCE;
7485 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7486 goto done;
7487}
7488
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007489void hlua_applet_http_fct(struct appctx *ctx)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007490{
7491 struct stream_interface *si = ctx->owner;
7492 struct stream *strm = si_strm(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007493 struct channel *req = si_oc(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007494 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007495 struct act_rule *rule = ctx->rule;
7496 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007497 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02007498 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007499 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02007500 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007501 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007502 int ret;
7503
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007504 if (IS_HTX_STRM(strm))
7505 return hlua_applet_htx_fct(ctx);
7506
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007507 /* If the stream is disconnect or closed, ldo nothing. */
7508 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007509 goto out;
7510
7511 /* Check if the input buffer is avalaible. */
7512 if (!b_size(&res->buf)) {
7513 si_rx_room_blk(si);
7514 goto out;
7515 }
7516 /* check that the output is not closed */
7517 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
7518 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007519
7520 /* Set the currently running flag. */
7521 if (!HLUA_IS_RUNNING(hlua) &&
7522 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007523 /* Store the max amount of bytes that we can read. */
7524 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
7525
7526 /* We need to flush the request header. This left the body
7527 * for the Lua.
7528 */
7529
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007530 /* Read the maximum amount of data available. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007531 ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007532 if (ret == -1)
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007533 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007534
7535 /* No data available, ask for more data. */
7536 if (ret == 1)
7537 len2 = 0;
7538 if (ret == 0)
7539 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007540 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007541 si_cant_get(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007542 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007543 }
7544
7545 /* skip the requests bytes. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007546 co_skip(req, strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007547 }
7548
7549 /* Executes The applet if it is not done. */
7550 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7551
7552 /* Execute the function. */
7553 switch (hlua_ctx_resume(hlua, 1)) {
7554 /* finished. */
7555 case HLUA_E_OK:
7556 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7557 break;
7558
7559 /* yield. */
7560 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007561 if (hlua->wake_time != TICK_ETERNITY)
7562 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007563 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007564
7565 /* finished with error. */
7566 case HLUA_E_ERRMSG:
7567 /* Display log. */
7568 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7569 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7570 lua_pop(hlua->T, 1);
7571 goto error;
7572
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007573 case HLUA_E_ETMOUT:
7574 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7575 rule->arg.hlua_rule->fcn.name);
7576 goto error;
7577
7578 case HLUA_E_NOMEM:
7579 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7580 rule->arg.hlua_rule->fcn.name);
7581 goto error;
7582
7583 case HLUA_E_YIELD: /* unexpected */
7584 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7585 rule->arg.hlua_rule->fcn.name);
7586 goto error;
7587
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007588 case HLUA_E_ERR:
7589 /* Display log. */
7590 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7591 rule->arg.hlua_rule->fcn.name);
7592 goto error;
7593
7594 default:
7595 goto error;
7596 }
7597 }
7598
7599 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007600 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7601 goto done;
7602
Christopher Fauletcc26b132018-12-18 21:20:57 +01007603 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7604 goto error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007605
7606 /* We must send the final chunk. */
7607 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
7608 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
7609
7610 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007611 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007612
7613 /* critical error. */
7614 if (ret == -2 || ret == -3) {
7615 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
7616 rule->arg.hlua_rule->fcn.name);
7617 goto error;
7618 }
7619
7620 /* no enough space error. */
7621 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01007622 si_rx_room_blk(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007623 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007624 }
7625
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007626 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7627 ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007628 }
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007629 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007630
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007631 done:
7632 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7633 if (!(res->flags & CF_SHUTR)) {
7634 res->flags |= CF_READ_NULL;
7635 si_shutr(si);
7636 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007637
7638 /* eat the whole request */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007639 if (co_data(req))
7640 co_skip(req, co_data(req));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007641 }
7642
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007643 out:
7644 return;
7645
7646 error:
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007647
7648 /* If we are in HTTP mode, and we are not send any
7649 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007650 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007651 * just close the connection.
7652 */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007653 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7654 channel_erase(res);
7655 ci_putblk(res, error_500, strlen(error_500));
7656 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007657 if (!(strm->flags & SF_ERR_MASK))
7658 strm->flags |= SF_ERR_RESOURCE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007659 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007660 goto done;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007661}
7662
7663static void hlua_applet_http_release(struct appctx *ctx)
7664{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007665 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007666 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007667 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007668 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007669}
7670
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007671/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
7672 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007673 *
7674 * This function can fail with an abort() due to an Lua critical error.
7675 * We are in the configuration parsing process of HAProxy, this abort() is
7676 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007677 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007678static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7679 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007680{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007681 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007682 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007683
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007684 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007685 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007686 if (!rule->arg.hlua_rule) {
7687 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007688 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007689 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007690
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007691 /* Memory for arguments. */
7692 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7693 if (!rule->arg.hlua_rule->args) {
7694 memprintf(err, "out of memory error");
7695 return ACT_RET_PRS_ERR;
7696 }
7697
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007698 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007699 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007700
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007701 /* Expect some arguments */
7702 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007703 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007704 memprintf(err, "expect %d arguments", fcn->nargs);
7705 return ACT_RET_PRS_ERR;
7706 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007707 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007708 if (!rule->arg.hlua_rule->args[i]) {
7709 memprintf(err, "out of memory error");
7710 return ACT_RET_PRS_ERR;
7711 }
7712 (*cur_arg)++;
7713 }
7714 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007715
Thierry FOURNIER42148732015-09-02 17:17:33 +02007716 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007717 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007718 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007719}
7720
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007721static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7722 struct act_rule *rule, char **err)
7723{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007724 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007725
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007726 /* HTTP applets are forbidden in tcp-request rules.
7727 * HTTP applet request requires everything initilized by
7728 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
7729 * The applet will be immediately initilized, but its before
7730 * the call of this analyzer.
7731 */
7732 if (rule->from != ACT_F_HTTP_REQ) {
7733 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7734 return ACT_RET_PRS_ERR;
7735 }
7736
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007737 /* Memory for the rule. */
7738 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7739 if (!rule->arg.hlua_rule) {
7740 memprintf(err, "out of memory error");
7741 return ACT_RET_PRS_ERR;
7742 }
7743
7744 /* Reference the Lua function and store the reference. */
7745 rule->arg.hlua_rule->fcn = *fcn;
7746
7747 /* TODO: later accept arguments. */
7748 rule->arg.hlua_rule->args = NULL;
7749
7750 /* Add applet pointer in the rule. */
7751 rule->applet.obj_type = OBJ_TYPE_APPLET;
7752 rule->applet.name = fcn->name;
7753 rule->applet.init = hlua_applet_http_init;
7754 rule->applet.fct = hlua_applet_http_fct;
7755 rule->applet.release = hlua_applet_http_release;
7756 rule->applet.timeout = hlua_timeout_applet;
7757
7758 return ACT_RET_PRS_OK;
7759}
7760
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007761/* This function is an LUA binding used for registering
7762 * "sample-conv" functions. It expects a converter name used
7763 * in the haproxy configuration file, and an LUA function.
7764 */
7765__LJMP static int hlua_register_action(lua_State *L)
7766{
7767 struct action_kw_list *akl;
7768 const char *name;
7769 int ref;
7770 int len;
7771 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007772 int nargs;
Thierry Fourniera9230f82020-11-28 20:41:07 +01007773 struct buffer *trash;
7774 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007775
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007776 /* Initialise the number of expected arguments at 0. */
7777 nargs = 0;
7778
7779 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7780 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007781
7782 /* First argument : converter name. */
7783 name = MAY_LJMP(luaL_checkstring(L, 1));
7784
7785 /* Second argument : environment. */
7786 if (lua_type(L, 2) != LUA_TTABLE)
7787 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7788
7789 /* Third argument : lua function. */
7790 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7791
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007792 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007793 if (lua_gettop(L) >= 4)
7794 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7795
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007796 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007797 lua_pushnil(L);
7798 while (lua_next(L, 2) != 0) {
7799 if (lua_type(L, -1) != LUA_TSTRING)
7800 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7801
Thierry Fourniera9230f82020-11-28 20:41:07 +01007802 /* Check if action exists */
7803 trash = get_trash_chunk();
7804 chunk_printf(trash, "lua.%s", name);
7805 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7806 akw = tcp_req_cont_action(trash->area);
7807 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7808 akw = tcp_res_cont_action(trash->area);
7809 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7810 akw = action_http_req_custom(trash->area);
7811 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7812 akw = action_http_res_custom(trash->area);
7813 } else {
7814 akw = NULL;
7815 }
7816 if (akw != NULL) {
7817 ha_warning("Trying to register action 'lua.%s' more than once. "
7818 "This will become a hard error in version 2.5.\n", name);
7819 }
7820
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007821 /* Check required environment. Only accepted "http" or "tcp". */
7822 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007823 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007824 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007825 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007826 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007827 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007828 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007829
7830 /* Fill fcn. */
7831 fcn->name = strdup(name);
7832 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007833 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007834 fcn->function_ref = ref;
7835
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007836 /* Set the expected number od arguments. */
7837 fcn->nargs = nargs;
7838
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007839 /* List head */
7840 akl->list.n = akl->list.p = NULL;
7841
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007842 /* action keyword. */
7843 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007844 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007845 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007846 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007847
7848 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7849
7850 akl->kw[0].match_pfx = 0;
7851 akl->kw[0].private = fcn;
7852 akl->kw[0].parse = action_register_lua;
7853
7854 /* select the action registering point. */
7855 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7856 tcp_req_cont_keywords_register(akl);
7857 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7858 tcp_res_cont_keywords_register(akl);
7859 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7860 http_req_keywords_register(akl);
7861 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7862 http_res_keywords_register(akl);
7863 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007864 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007865 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7866 "are expected.", lua_tostring(L, -1)));
7867
7868 /* pop the environment string. */
7869 lua_pop(L, 1);
7870 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007871 return ACT_RET_PRS_OK;
7872}
7873
7874static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7875 struct act_rule *rule, char **err)
7876{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007877 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007878
Christopher Fauleteb709802019-04-11 22:04:08 +02007879 if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) {
Christopher Fauletafd8f102018-11-08 11:34:21 +01007880 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7881 return ACT_RET_PRS_ERR;
7882 }
7883
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007884 /* Memory for the rule. */
7885 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7886 if (!rule->arg.hlua_rule) {
7887 memprintf(err, "out of memory error");
7888 return ACT_RET_PRS_ERR;
7889 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007890
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007891 /* Reference the Lua function and store the reference. */
7892 rule->arg.hlua_rule->fcn = *fcn;
7893
7894 /* TODO: later accept arguments. */
7895 rule->arg.hlua_rule->args = NULL;
7896
7897 /* Add applet pointer in the rule. */
7898 rule->applet.obj_type = OBJ_TYPE_APPLET;
7899 rule->applet.name = fcn->name;
7900 rule->applet.init = hlua_applet_tcp_init;
7901 rule->applet.fct = hlua_applet_tcp_fct;
7902 rule->applet.release = hlua_applet_tcp_release;
7903 rule->applet.timeout = hlua_timeout_applet;
7904
7905 return 0;
7906}
7907
7908/* This function is an LUA binding used for registering
7909 * "sample-conv" functions. It expects a converter name used
7910 * in the haproxy configuration file, and an LUA function.
7911 */
7912__LJMP static int hlua_register_service(lua_State *L)
7913{
7914 struct action_kw_list *akl;
7915 const char *name;
7916 const char *env;
7917 int ref;
7918 int len;
7919 struct hlua_function *fcn;
Thierry Fourniera9230f82020-11-28 20:41:07 +01007920 struct buffer *trash;
7921 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007922
7923 MAY_LJMP(check_args(L, 3, "register_service"));
7924
7925 /* First argument : converter name. */
7926 name = MAY_LJMP(luaL_checkstring(L, 1));
7927
7928 /* Second argument : environment. */
7929 env = MAY_LJMP(luaL_checkstring(L, 2));
7930
7931 /* Third argument : lua function. */
7932 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7933
Thierry Fourniera9230f82020-11-28 20:41:07 +01007934 /* Check for service already registered */
7935 trash = get_trash_chunk();
7936 chunk_printf(trash, "lua.%s", name);
7937 akw = service_find(trash->area);
7938 if (akw != NULL) {
7939 ha_warning("Trying to register service 'lua.%s' more than once. "
7940 "This will become a hard error in version 2.5.\n", name);
7941 }
7942
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007943 /* Allocate and fill the sample fetch keyword struct. */
7944 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7945 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007946 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007947 fcn = calloc(1, sizeof(*fcn));
7948 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007949 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007950
7951 /* Fill fcn. */
7952 len = strlen("<lua.>") + strlen(name) + 1;
7953 fcn->name = calloc(1, len);
7954 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007955 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007956 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7957 fcn->function_ref = ref;
7958
7959 /* List head */
7960 akl->list.n = akl->list.p = NULL;
7961
7962 /* converter keyword. */
7963 len = strlen("lua.") + strlen(name) + 1;
7964 akl->kw[0].kw = calloc(1, len);
7965 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007966 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007967
7968 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7969
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007970 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007971 if (strcmp(env, "tcp") == 0)
7972 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007973 else if (strcmp(env, "http") == 0)
7974 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007975 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007976 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007977 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007978
7979 akl->kw[0].match_pfx = 0;
7980 akl->kw[0].private = fcn;
7981
7982 /* End of array. */
7983 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7984
7985 /* Register this new converter */
7986 service_keywords_register(akl);
7987
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007988 return 0;
7989}
7990
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007991/* This function initialises Lua cli handler. It copies the
7992 * arguments in the Lua stack and create channel IO objects.
7993 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007994static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007995{
7996 struct hlua *hlua;
7997 struct hlua_function *fcn;
7998 int i;
7999 const char *error;
8000
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008001 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008002 appctx->ctx.hlua_cli.fcn = private;
8003
Willy Tarreaubafbe012017-11-24 17:34:44 +01008004 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008005 if (!hlua) {
8006 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008007 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008008 }
8009 HLUA_INIT(hlua);
8010 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008011
8012 /* Create task used by signal to wakeup applets.
8013 * We use the same wakeup fonction than the Lua applet_tcp and
8014 * applet_http. It is absolutely compatible.
8015 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01008016 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008017 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01008018 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008019 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008020 }
8021 appctx->ctx.hlua_cli.task->nice = 0;
8022 appctx->ctx.hlua_cli.task->context = appctx;
8023 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
8024
8025 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01008026 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008027 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008028 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008029 }
8030
8031 /* The following Lua calls can fail. */
8032 if (!SET_SAFE_LJMP(hlua->T)) {
8033 if (lua_type(hlua->T, -1) == LUA_TSTRING)
8034 error = lua_tostring(hlua->T, -1);
8035 else
8036 error = "critical error";
8037 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
8038 goto error;
8039 }
8040
8041 /* Check stack available size. */
8042 if (!lua_checkstack(hlua->T, 2)) {
8043 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8044 goto error;
8045 }
8046
8047 /* Restore the function in the stack. */
8048 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
8049
8050 /* Once the arguments parsed, the CLI is like an AppletTCP,
8051 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008052 */
8053 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
8054 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8055 goto error;
8056 }
8057 hlua->nargs = 1;
8058
8059 /* push keywords in the stack. */
8060 for (i = 0; *args[i]; i++) {
8061 /* Check stack available size. */
8062 if (!lua_checkstack(hlua->T, 1)) {
8063 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8064 goto error;
8065 }
8066 lua_pushstring(hlua->T, args[i]);
8067 hlua->nargs++;
8068 }
8069
8070 /* We must initialize the execution timeouts. */
8071 hlua->max_time = hlua_timeout_session;
8072
8073 /* At this point the execution is safe. */
8074 RESET_SAFE_LJMP(hlua->T);
8075
8076 /* It's ok */
8077 return 0;
8078
8079 /* It's not ok. */
8080error:
8081 RESET_SAFE_LJMP(hlua->T);
8082 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008083 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008084 return 1;
8085}
8086
8087static int hlua_cli_io_handler_fct(struct appctx *appctx)
8088{
8089 struct hlua *hlua;
8090 struct stream_interface *si;
8091 struct hlua_function *fcn;
8092
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008093 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008094 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008095 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008096
8097 /* If the stream is disconnect or closed, ldo nothing. */
8098 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8099 return 1;
8100
8101 /* Execute the function. */
8102 switch (hlua_ctx_resume(hlua, 1)) {
8103
8104 /* finished. */
8105 case HLUA_E_OK:
8106 return 1;
8107
8108 /* yield. */
8109 case HLUA_E_AGAIN:
8110 /* We want write. */
8111 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008112 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008113 /* Set the timeout. */
8114 if (hlua->wake_time != TICK_ETERNITY)
8115 task_schedule(hlua->task, hlua->wake_time);
8116 return 0;
8117
8118 /* finished with error. */
8119 case HLUA_E_ERRMSG:
8120 /* Display log. */
8121 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8122 fcn->name, lua_tostring(hlua->T, -1));
8123 lua_pop(hlua->T, 1);
8124 return 1;
8125
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008126 case HLUA_E_ETMOUT:
8127 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8128 fcn->name);
8129 return 1;
8130
8131 case HLUA_E_NOMEM:
8132 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8133 fcn->name);
8134 return 1;
8135
8136 case HLUA_E_YIELD: /* unexpected */
8137 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8138 fcn->name);
8139 return 1;
8140
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008141 case HLUA_E_ERR:
8142 /* Display log. */
8143 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8144 fcn->name);
8145 return 1;
8146
8147 default:
8148 return 1;
8149 }
8150
8151 return 1;
8152}
8153
8154static void hlua_cli_io_release_fct(struct appctx *appctx)
8155{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008156 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008157 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008158}
8159
8160/* This function is an LUA binding used for registering
8161 * new keywords in the cli. It expects a list of keywords
8162 * which are the "path". It is limited to 5 keywords. A
8163 * description of the command, a function to be executed
8164 * for the parsing and a function for io handlers.
8165 */
8166__LJMP static int hlua_register_cli(lua_State *L)
8167{
8168 struct cli_kw_list *cli_kws;
8169 const char *message;
8170 int ref_io;
8171 int len;
8172 struct hlua_function *fcn;
8173 int index;
8174 int i;
Thierry Fourniera9230f82020-11-28 20:41:07 +01008175 struct buffer *trash;
8176 const char *kw[5];
8177 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008178
8179 MAY_LJMP(check_args(L, 3, "register_cli"));
8180
8181 /* First argument : an array of maximum 5 keywords. */
8182 if (!lua_istable(L, 1))
8183 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8184
8185 /* Second argument : string with contextual message. */
8186 message = MAY_LJMP(luaL_checkstring(L, 2));
8187
8188 /* Third and fourth argument : lua function. */
8189 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8190
Thierry Fourniera9230f82020-11-28 20:41:07 +01008191 /* Check for CLI service already registered */
8192 trash = get_trash_chunk();
8193 index = 0;
8194 lua_pushnil(L);
8195 memset(kw, 0, sizeof(kw));
8196 while (lua_next(L, 1) != 0) {
8197 if (index >= CLI_PREFIX_KW_NB)
8198 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8199 if (lua_type(L, -1) != LUA_TSTRING)
8200 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8201 kw[index] = lua_tostring(L, -1);
8202 if (index == 0)
8203 chunk_printf(trash, "%s", kw[index]);
8204 else
8205 chunk_appendf(trash, " %s", kw[index]);
8206 index++;
8207 lua_pop(L, 1);
8208 }
8209 cli_kw = cli_find_kw_exact((char **)kw);
8210 if (cli_kw != NULL) {
8211 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8212 "This will become a hard error in version 2.5.\n", trash->area);
8213 }
8214
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008215 /* Allocate and fill the sample fetch keyword struct. */
8216 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8217 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008218 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008219 fcn = calloc(1, sizeof(*fcn));
8220 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008221 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008222
8223 /* Fill path. */
8224 index = 0;
8225 lua_pushnil(L);
8226 while(lua_next(L, 1) != 0) {
8227 if (index >= 5)
8228 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8229 if (lua_type(L, -1) != LUA_TSTRING)
8230 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8231 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8232 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008233 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008234 index++;
8235 lua_pop(L, 1);
8236 }
8237
8238 /* Copy help message. */
8239 cli_kws->kw[0].usage = strdup(message);
8240 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008241 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008242
8243 /* Fill fcn io handler. */
8244 len = strlen("<lua.cli>") + 1;
8245 for (i = 0; i < index; i++)
8246 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8247 fcn->name = calloc(1, len);
8248 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008249 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008250 strncat((char *)fcn->name, "<lua.cli", len);
8251 for (i = 0; i < index; i++) {
8252 strncat((char *)fcn->name, ".", len);
8253 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8254 }
8255 strncat((char *)fcn->name, ">", len);
8256 fcn->function_ref = ref_io;
8257
8258 /* Fill last entries. */
8259 cli_kws->kw[0].private = fcn;
8260 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8261 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8262 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8263
8264 /* Register this new converter */
8265 cli_register_kw(cli_kws);
8266
8267 return 0;
8268}
8269
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008270static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8271 struct proxy *defpx, const char *file, int line,
8272 char **err, unsigned int *timeout)
8273{
8274 const char *error;
8275
8276 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008277 if (error == PARSE_TIME_OVER) {
8278 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8279 args[1], args[0]);
8280 return -1;
8281 }
8282 else if (error == PARSE_TIME_UNDER) {
8283 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8284 args[1], args[0]);
8285 return -1;
8286 }
8287 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008288 memprintf(err, "%s: invalid timeout", args[0]);
8289 return -1;
8290 }
8291 return 0;
8292}
8293
8294static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8295 struct proxy *defpx, const char *file, int line,
8296 char **err)
8297{
8298 return hlua_read_timeout(args, section_type, curpx, defpx,
8299 file, line, err, &hlua_timeout_session);
8300}
8301
8302static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8303 struct proxy *defpx, const char *file, int line,
8304 char **err)
8305{
8306 return hlua_read_timeout(args, section_type, curpx, defpx,
8307 file, line, err, &hlua_timeout_task);
8308}
8309
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008310static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8311 struct proxy *defpx, const char *file, int line,
8312 char **err)
8313{
8314 return hlua_read_timeout(args, section_type, curpx, defpx,
8315 file, line, err, &hlua_timeout_applet);
8316}
8317
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008318static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8319 struct proxy *defpx, const char *file, int line,
8320 char **err)
8321{
8322 char *error;
8323
8324 hlua_nb_instruction = strtoll(args[1], &error, 10);
8325 if (*error != '\0') {
8326 memprintf(err, "%s: invalid number", args[0]);
8327 return -1;
8328 }
8329 return 0;
8330}
8331
Willy Tarreau32f61e22015-03-18 17:54:59 +01008332static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8333 struct proxy *defpx, const char *file, int line,
8334 char **err)
8335{
8336 char *error;
8337
8338 if (*(args[1]) == 0) {
8339 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8340 return -1;
8341 }
8342 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8343 if (*error != '\0') {
8344 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8345 return -1;
8346 }
8347 return 0;
8348}
8349
8350
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008351/* This function is called by the main configuration key "lua-load". It loads and
8352 * execute an lua file during the parsing of the HAProxy configuration file. It is
8353 * the main lua entry point.
8354 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008355 * This function runs with the HAProxy keywords API. It returns -1 if an error
8356 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008357 *
8358 * In some error case, LUA set an error message in top of the stack. This function
8359 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008360 *
8361 * This function can fail with an abort() due to an Lua critical error.
8362 * We are in the configuration parsing process of HAProxy, this abort() is
8363 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008364 */
8365static int hlua_load(char **args, int section_type, struct proxy *curpx,
8366 struct proxy *defpx, const char *file, int line,
8367 char **err)
8368{
8369 int error;
8370
Thierry Fourniere6679502020-11-29 01:06:24 +01008371 if (*(args[1]) == 0) {
8372 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8373 return -1;
8374 }
8375
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008376 /* Just load and compile the file. */
8377 error = luaL_loadfile(gL.T, args[1]);
8378 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008379 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008380 lua_pop(gL.T, 1);
8381 return -1;
8382 }
8383
8384 /* If no syntax error where detected, execute the code. */
8385 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8386 switch (error) {
8387 case LUA_OK:
8388 break;
8389 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008390 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008391 lua_pop(gL.T, 1);
8392 return -1;
8393 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008394 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008395 return -1;
8396 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008397 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008398 lua_pop(gL.T, 1);
8399 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008400#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008401 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008402 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008403 lua_pop(gL.T, 1);
8404 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008405#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008406 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008407 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008408 lua_pop(gL.T, 1);
8409 return -1;
8410 }
8411
8412 return 0;
8413}
8414
8415/* configuration keywords declaration */
8416static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008417 { CFG_GLOBAL, "lua-load", hlua_load },
8418 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8419 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008420 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008421 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008422 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008423 { 0, NULL, NULL },
8424}};
8425
Willy Tarreau0108d902018-11-25 19:14:37 +01008426INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8427
Christopher Fauletafd8f102018-11-08 11:34:21 +01008428
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008429/* This function can fail with an abort() due to an Lua critical error.
8430 * We are in the initialisation process of HAProxy, this abort() is
8431 * tolerated.
8432 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008433int hlua_post_init()
8434{
8435 struct hlua_init_function *init;
8436 const char *msg;
8437 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008438 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008439
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008440 /* Call post initialisation function in safe environement. */
8441 if (!SET_SAFE_LJMP(gL.T)) {
8442 if (lua_type(gL.T, -1) == LUA_TSTRING)
8443 error = lua_tostring(gL.T, -1);
8444 else
8445 error = "critical error";
8446 fprintf(stderr, "Lua post-init: %s.\n", error);
8447 exit(1);
8448 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008449
8450#if USE_OPENSSL
8451 /* Initialize SSL server. */
8452 if (socket_ssl.xprt->prepare_srv) {
8453 int saved_used_backed = global.ssl_used_backend;
8454 // don't affect maxconn automatic computation
8455 socket_ssl.xprt->prepare_srv(&socket_ssl);
8456 global.ssl_used_backend = saved_used_backed;
8457 }
8458#endif
8459
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008460 hlua_fcn_post_init(gL.T);
8461 RESET_SAFE_LJMP(gL.T);
8462
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008463 list_for_each_entry(init, &hlua_init_functions, l) {
8464 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8465 ret = hlua_ctx_resume(&gL, 0);
8466 switch (ret) {
8467 case HLUA_E_OK:
8468 lua_pop(gL.T, -1);
Thierry Fournier65588c92020-11-28 11:02:58 +01008469 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008470 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008471 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008472 return 0;
8473 case HLUA_E_ERRMSG:
8474 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008475 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008476 return 0;
8477 case HLUA_E_ERR:
8478 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008479 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008480 return 0;
8481 }
8482 }
8483 return 1;
8484}
8485
Willy Tarreau32f61e22015-03-18 17:54:59 +01008486/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8487 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8488 * is the previously allocated size or the kind of object in case of a new
8489 * allocation. <nsize> is the requested new size.
8490 */
8491static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8492{
8493 struct hlua_mem_allocator *zone = ud;
8494
8495 if (nsize == 0) {
8496 /* it's a free */
8497 if (ptr)
8498 zone->allocated -= osize;
8499 free(ptr);
8500 return NULL;
8501 }
8502
8503 if (!ptr) {
8504 /* it's a new allocation */
8505 if (zone->limit && zone->allocated + nsize > zone->limit)
8506 return NULL;
8507
8508 ptr = malloc(nsize);
8509 if (ptr)
8510 zone->allocated += nsize;
8511 return ptr;
8512 }
8513
8514 /* it's a realloc */
8515 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8516 return NULL;
8517
8518 ptr = realloc(ptr, nsize);
8519 if (ptr)
8520 zone->allocated += nsize - osize;
8521 return ptr;
8522}
8523
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008524/* Ithis function can fail with an abort() due to an Lua critical error.
8525 * We are in the initialisation process of HAProxy, this abort() is
8526 * tolerated.
8527 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008528void hlua_init(void)
8529{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008530 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008531 int idx;
8532 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008533 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008534 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008535 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008536#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008537 struct srv_kw *kw;
8538 int tmp_error;
8539 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008540 char *args[] = { /* SSL client configuration. */
8541 "ssl",
8542 "verify",
8543 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008544 NULL
8545 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008546#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008547
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008548 /* Init main lua stack. */
8549 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008550 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008551 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008552 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008553 hlua_sethlua(&gL);
8554 gL.Tref = LUA_REFNIL;
8555 gL.task = NULL;
8556
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008557 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008558 * the Lua function can fail with an abort. We are in the initialisation
8559 * process of HAProxy, this abort() is tolerated.
8560 */
8561
Thierry Fournier75933d42016-01-21 09:30:18 +01008562 /* Set safe environment for the initialisation. */
8563 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008564 if (lua_type(gL.T, -1) == LUA_TSTRING)
8565 error_msg = lua_tostring(gL.T, -1);
8566 else
8567 error_msg = "critical error";
8568 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008569 exit(1);
8570 }
8571
Thierry Fourniera5d62582020-11-28 16:08:02 +01008572 /* Initialise lua. */
8573 luaL_openlibs(gL.T);
8574
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008575 /*
8576 *
8577 * Create "core" object.
8578 *
8579 */
8580
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008581 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008582 lua_newtable(gL.T);
8583
8584 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008585 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008586 hlua_class_const_int(gL.T, log_levels[i], i);
8587
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008588 /* Register special functions. */
8589 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008590 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008591 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008592 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008593 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008594 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008595 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008596 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008597 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008598 hlua_class_function(gL.T, "sleep", hlua_sleep);
8599 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008600 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8601 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8602 hlua_class_function(gL.T, "set_map", hlua_set_map);
8603 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008604 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008605 hlua_class_function(gL.T, "log", hlua_log);
8606 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8607 hlua_class_function(gL.T, "Info", hlua_log_info);
8608 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8609 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008610 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008611 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008612
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008613 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008614
8615 /*
8616 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008617 * Register class Map
8618 *
8619 */
8620
8621 /* This table entry is the object "Map" base. */
8622 lua_newtable(gL.T);
8623
8624 /* register pattern types. */
8625 for (i=0; i<PAT_MATCH_NUM; i++)
8626 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008627 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008628 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8629 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008630 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008631
8632 /* register constructor. */
8633 hlua_class_function(gL.T, "new", hlua_map_new);
8634
8635 /* Create and fill the metatable. */
8636 lua_newtable(gL.T);
8637
8638 /* Create and fille the __index entry. */
8639 lua_pushstring(gL.T, "__index");
8640 lua_newtable(gL.T);
8641
8642 /* Register . */
8643 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8644 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8645
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008646 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008647
Thierry Fournier45e78d72016-02-19 18:34:46 +01008648 /* Register previous table in the registry with reference and named entry.
8649 * The function hlua_register_metatable() pops the stack, so we
8650 * previously create a copy of the table.
8651 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008652 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008653 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008654
8655 /* Assign the metatable to the mai Map object. */
8656 lua_setmetatable(gL.T, -2);
8657
8658 /* Set a name to the table. */
8659 lua_setglobal(gL.T, "Map");
8660
8661 /*
8662 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008663 * Register class Channel
8664 *
8665 */
8666
8667 /* Create and fill the metatable. */
8668 lua_newtable(gL.T);
8669
8670 /* Create and fille the __index entry. */
8671 lua_pushstring(gL.T, "__index");
8672 lua_newtable(gL.T);
8673
8674 /* Register . */
8675 hlua_class_function(gL.T, "get", hlua_channel_get);
8676 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8677 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8678 hlua_class_function(gL.T, "set", hlua_channel_set);
8679 hlua_class_function(gL.T, "append", hlua_channel_append);
8680 hlua_class_function(gL.T, "send", hlua_channel_send);
8681 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8682 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8683 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008684 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008685
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008686 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008687
8688 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008689 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008690
8691 /*
8692 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008693 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008694 *
8695 */
8696
8697 /* Create and fill the metatable. */
8698 lua_newtable(gL.T);
8699
8700 /* Create and fille the __index entry. */
8701 lua_pushstring(gL.T, "__index");
8702 lua_newtable(gL.T);
8703
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008704 /* Browse existing fetches and create the associated
8705 * object method.
8706 */
8707 sf = NULL;
8708 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8709
8710 /* Dont register the keywork if the arguments check function are
8711 * not safe during the runtime.
8712 */
8713 if ((sf->val_args != NULL) &&
8714 (sf->val_args != val_payload_lv) &&
8715 (sf->val_args != val_hdr))
8716 continue;
8717
8718 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8719 * by an underscore.
8720 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008721 strncpy(trash.area, sf->kw, trash.size);
8722 trash.area[trash.size - 1] = '\0';
8723 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008724 if (*p == '.' || *p == '-' || *p == '+')
8725 *p = '_';
8726
8727 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008728 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008729 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008730 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008731 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008732 }
8733
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008734 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008735
8736 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008737 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008738
8739 /*
8740 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008741 * Register class Converters
8742 *
8743 */
8744
8745 /* Create and fill the metatable. */
8746 lua_newtable(gL.T);
8747
8748 /* Create and fill the __index entry. */
8749 lua_pushstring(gL.T, "__index");
8750 lua_newtable(gL.T);
8751
8752 /* Browse existing converters and create the associated
8753 * object method.
8754 */
8755 sc = NULL;
8756 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8757 /* Dont register the keywork if the arguments check function are
8758 * not safe during the runtime.
8759 */
8760 if (sc->val_args != NULL)
8761 continue;
8762
8763 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8764 * by an underscore.
8765 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008766 strncpy(trash.area, sc->kw, trash.size);
8767 trash.area[trash.size - 1] = '\0';
8768 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008769 if (*p == '.' || *p == '-' || *p == '+')
8770 *p = '_';
8771
8772 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008773 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008774 lua_pushlightuserdata(gL.T, sc);
8775 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008776 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008777 }
8778
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008779 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008780
8781 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008782 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008783
8784 /*
8785 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008786 * Register class HTTP
8787 *
8788 */
8789
8790 /* Create and fill the metatable. */
8791 lua_newtable(gL.T);
8792
8793 /* Create and fille the __index entry. */
8794 lua_pushstring(gL.T, "__index");
8795 lua_newtable(gL.T);
8796
8797 /* Register Lua functions. */
8798 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8799 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8800 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8801 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8802 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8803 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8804 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8805 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8806 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8807 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8808
8809 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8810 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8811 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8812 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8813 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8814 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008815 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008816
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008817 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008818
8819 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008820 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008821
8822 /*
8823 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008824 * Register class AppletTCP
8825 *
8826 */
8827
8828 /* Create and fill the metatable. */
8829 lua_newtable(gL.T);
8830
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008831 /* Create and fille the __index entry. */
8832 lua_pushstring(gL.T, "__index");
8833 lua_newtable(gL.T);
8834
8835 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008836 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8837 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8838 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8839 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8840 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008841 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8842 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8843 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008844
8845 lua_settable(gL.T, -3);
8846
8847 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008848 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008849
8850 /*
8851 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008852 * Register class AppletHTTP
8853 *
8854 */
8855
8856 /* Create and fill the metatable. */
8857 lua_newtable(gL.T);
8858
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008859 /* Create and fille the __index entry. */
8860 lua_pushstring(gL.T, "__index");
8861 lua_newtable(gL.T);
8862
8863 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008864 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8865 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008866 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8867 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8868 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008869 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8870 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8871 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8872 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8873 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8874 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8875
8876 lua_settable(gL.T, -3);
8877
8878 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008879 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008880
8881 /*
8882 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008883 * Register class TXN
8884 *
8885 */
8886
8887 /* Create and fill the metatable. */
8888 lua_newtable(gL.T);
8889
8890 /* Create and fille the __index entry. */
8891 lua_pushstring(gL.T, "__index");
8892 lua_newtable(gL.T);
8893
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008894 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008895 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8896 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8897 hlua_class_function(gL.T, "set_var", hlua_set_var);
8898 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8899 hlua_class_function(gL.T, "get_var", hlua_get_var);
8900 hlua_class_function(gL.T, "done", hlua_txn_done);
8901 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8902 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8903 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8904 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8905 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8906 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8907 hlua_class_function(gL.T, "log", hlua_txn_log);
8908 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8909 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8910 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8911 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008912
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008913 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008914
8915 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008916 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008917
8918 /*
8919 *
8920 * Register class Socket
8921 *
8922 */
8923
8924 /* Create and fill the metatable. */
8925 lua_newtable(gL.T);
8926
8927 /* Create and fille the __index entry. */
8928 lua_pushstring(gL.T, "__index");
8929 lua_newtable(gL.T);
8930
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008931#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008932 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008933#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008934 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8935 hlua_class_function(gL.T, "send", hlua_socket_send);
8936 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8937 hlua_class_function(gL.T, "close", hlua_socket_close);
8938 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8939 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8940 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8941 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8942
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008943 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008944
8945 /* Register the garbage collector entry. */
8946 lua_pushstring(gL.T, "__gc");
8947 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008948 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008949
8950 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008951 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008952
8953 /* Proxy and server configuration initialisation. */
8954 memset(&socket_proxy, 0, sizeof(socket_proxy));
8955 init_new_proxy(&socket_proxy);
8956 socket_proxy.parent = NULL;
8957 socket_proxy.last_change = now.tv_sec;
8958 socket_proxy.id = "LUA-SOCKET";
8959 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8960 socket_proxy.maxconn = 0;
8961 socket_proxy.accept = NULL;
8962 socket_proxy.options2 |= PR_O2_INDEPSTR;
8963 socket_proxy.srv = NULL;
8964 socket_proxy.conn_retries = 0;
8965 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8966
8967 /* Init TCP server: unchanged parameters */
8968 memset(&socket_tcp, 0, sizeof(socket_tcp));
8969 socket_tcp.next = NULL;
8970 socket_tcp.proxy = &socket_proxy;
8971 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8972 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008973 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008974 socket_tcp.priv_conns = NULL;
8975 socket_tcp.idle_conns = NULL;
8976 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008977 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008978 socket_tcp.last_change = 0;
8979 socket_tcp.id = "LUA-TCP-CONN";
8980 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8981 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8982 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8983
8984 /* XXX: Copy default parameter from default server,
8985 * but the default server is not initialized.
8986 */
8987 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8988 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8989 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8990 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8991 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8992 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8993 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8994 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8995 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8996 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8997
8998 socket_tcp.check.status = HCHK_STATUS_INI;
8999 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
9000 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
9001 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
9002 socket_tcp.check.server = &socket_tcp;
9003
9004 socket_tcp.agent.status = HCHK_STATUS_INI;
9005 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
9006 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
9007 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
9008 socket_tcp.agent.server = &socket_tcp;
9009
Willy Tarreaua261e9b2016-12-22 20:44:00 +01009010 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009011
9012#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009013 /* Init TCP server: unchanged parameters */
9014 memset(&socket_ssl, 0, sizeof(socket_ssl));
9015 socket_ssl.next = NULL;
9016 socket_ssl.proxy = &socket_proxy;
9017 socket_ssl.obj_type = OBJ_TYPE_SERVER;
9018 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04009019 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01009020 socket_ssl.priv_conns = NULL;
9021 socket_ssl.idle_conns = NULL;
9022 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02009023 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009024 socket_ssl.last_change = 0;
9025 socket_ssl.id = "LUA-SSL-CONN";
9026 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9027 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9028 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
9029
9030 /* XXX: Copy default parameter from default server,
9031 * but the default server is not initialized.
9032 */
9033 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
9034 socket_ssl.minconn = socket_proxy.defsrv.minconn;
9035 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
9036 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
9037 socket_ssl.onerror = socket_proxy.defsrv.onerror;
9038 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
9039 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
9040 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
9041 socket_ssl.uweight = socket_proxy.defsrv.iweight;
9042 socket_ssl.iweight = socket_proxy.defsrv.iweight;
9043
9044 socket_ssl.check.status = HCHK_STATUS_INI;
9045 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
9046 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
9047 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
9048 socket_ssl.check.server = &socket_ssl;
9049
9050 socket_ssl.agent.status = HCHK_STATUS_INI;
9051 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
9052 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
9053 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
9054 socket_ssl.agent.server = &socket_ssl;
9055
Thierry FOURNIER36d13742015-03-17 16:48:53 +01009056 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01009057 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009058
Thierry FOURNIER36d13742015-03-17 16:48:53 +01009059 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009060 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
9061 /*
9062 *
9063 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009064 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009065 * features like client certificates and ssl_verify.
9066 *
9067 */
9068 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
9069 if (tmp_error != 0) {
9070 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9071 abort(); /* This must be never arrives because the command line
9072 not editable by the user. */
9073 }
9074 idx += kw->skip;
9075 }
9076 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009077#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009078
9079 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009080}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009081
Willy Tarreau80713382018-11-26 10:19:54 +01009082static void hlua_register_build_options(void)
9083{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009084 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009085
Willy Tarreaubb57d942016-12-21 19:04:56 +01009086 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9087 hap_register_build_opts(ptr, 1);
9088}
Willy Tarreau80713382018-11-26 10:19:54 +01009089
9090INITCALL0(STG_REGISTER, hlua_register_build_options);