blob: 8844ebe6720648ad71143970c5f44426a3f08da6 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Mark Lakes56cc1252018-03-27 09:48:06 +020014#include <limits.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020015#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010016
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010017#include <lauxlib.h>
18#include <lua.h>
19#include <lualib.h>
20
Thierry FOURNIER463119c2015-03-10 00:35:36 +010021#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
22#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010023#endif
24
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025#include <ebpttree.h>
26
27#include <common/cfgparse.h>
Willy Tarreaub059b892018-10-16 17:57:36 +020028#include <common/compiler.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010030#include <common/initcall.h>
31#include <common/xref.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010032#include <common/h1.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035#include <types/hlua.h>
36#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010037#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010038
Thierry FOURNIER55da1652015-01-23 11:36:30 +010039#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010041#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010043#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/stats.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010045#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010046#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020047#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010048#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020049#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020050#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010051#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040052#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010053#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010054#include <proto/payload.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055#include <proto/http_ana.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010056#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010057#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020058#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020059#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010060#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010061#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010062#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020063#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010064
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010065/* Lua uses longjmp to perform yield or throwing errors. This
66 * macro is used only for identifying the function that can
67 * not return because a longjmp is executed.
68 * __LJMP marks a prototype of hlua file that can use longjmp.
69 * WILL_LJMP() marks an lua function that will use longjmp.
70 * MAY_LJMP() marks an lua function that may use longjmp.
71 */
72#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020073#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010074#define MAY_LJMP(func) func
75
Thierry FOURNIERbabae282015-09-17 11:36:37 +020076/* This couple of function executes securely some Lua calls outside of
77 * the lua runtime environment. Each Lua call can return a longjmp
78 * if it encounter a memory error.
79 *
80 * Lua documentation extract:
81 *
82 * If an error happens outside any protected environment, Lua calls
83 * a panic function (see lua_atpanic) and then calls abort, thus
84 * exiting the host application. Your panic function can avoid this
85 * exit by never returning (e.g., doing a long jump to your own
86 * recovery point outside Lua).
87 *
88 * The panic function runs as if it were a message handler (see
89 * §2.3); in particular, the error message is at the top of the
90 * stack. However, there is no guarantee about stack space. To push
91 * anything on the stack, the panic function must first check the
92 * available space (see §4.2).
93 *
94 * We must check all the Lua entry point. This includes:
95 * - The include/proto/hlua.h exported functions
96 * - the task wrapper function
97 * - The action wrapper function
98 * - The converters wrapper function
99 * - The sample-fetch wrapper functions
100 *
101 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800102 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200103 *
104 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
105 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
106 * because they must be exists in the program stack when the longjmp
107 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200108 *
109 * Note that the Lua processing is not really thread safe. It provides
110 * heavy system which consists to add our own lock function in the Lua
111 * code and recompile the library. This system will probably not accepted
112 * by maintainers of various distribs.
113 *
114 * Our main excution point of the Lua is the function lua_resume(). A
115 * quick looking on the Lua sources displays a lua_lock() a the start
116 * of function and a lua_unlock() at the end of the function. So I
117 * conclude that the Lua thread safe mode just perform a mutex around
118 * all execution. So I prefer to do this in the HAProxy code, it will be
119 * easier for distro maintainers.
120 *
121 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
122 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
123 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200124 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100125__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200126THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200127static int hlua_panic_safe(lua_State *L) { return 0; }
128static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
129
130#define SET_SAFE_LJMP(__L) \
131 ({ \
132 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200134 if (setjmp(safe_ljmp_env) != 0) { \
135 lua_atpanic(__L, hlua_panic_safe); \
136 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100137 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200138 } else { \
139 lua_atpanic(__L, hlua_panic_ljmp); \
140 ret = 1; \
141 } \
142 ret; \
143 })
144
145/* If we are the last function catching Lua errors, we
146 * must reset the panic function.
147 */
148#define RESET_SAFE_LJMP(__L) \
149 do { \
150 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100151 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200152 } while(0)
153
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200154/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200155#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100156/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200157#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200158/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100159#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100160#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200161
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100162/* The main Lua execution context. */
163struct hlua gL;
164
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165/* This is the memory pool containing struct lua for applets
166 * (including cli).
167 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100168DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100169
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100170/* Used for Socket connection. */
171static struct proxy socket_proxy;
172static struct server socket_tcp;
173#ifdef USE_OPENSSL
174static struct server socket_ssl;
175#endif
176
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100177/* List head of the function called at the initialisation time. */
178struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
179
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100180/* The following variables contains the reference of the different
181 * Lua classes. These references are useful for identify metadata
182 * associated with an object.
183 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100184static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100185static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100186static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100187static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100188static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100189static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200190static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200191static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200192static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100193
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200195 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100196 * short timeout. Lua linked with tasks doesn't have a timeout
197 * because a task may remain alive during all the haproxy execution.
198 */
199static unsigned int hlua_timeout_session = 4000; /* session timeout. */
200static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200201static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100202
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100203/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
204 * it is used for preventing infinite loops.
205 *
206 * I test the scheer with an infinite loop containing one incrementation
207 * and one test. I run this loop between 10 seconds, I raise a ceil of
208 * 710M loops from one interrupt each 9000 instructions, so I fix the value
209 * to one interrupt each 10 000 instructions.
210 *
211 * configured | Number of
212 * instructions | loops executed
213 * between two | in milions
214 * forced yields |
215 * ---------------+---------------
216 * 10 | 160
217 * 500 | 670
218 * 1000 | 680
219 * 5000 | 700
220 * 7000 | 700
221 * 8000 | 700
222 * 9000 | 710 <- ceil
223 * 10000 | 710
224 * 100000 | 710
225 * 1000000 | 710
226 *
227 */
228static unsigned int hlua_nb_instruction = 10000;
229
Willy Tarreau32f61e22015-03-18 17:54:59 +0100230/* Descriptor for the memory allocation state. If limit is not null, it will
231 * be enforced on any memory allocation.
232 */
233struct hlua_mem_allocator {
234 size_t allocated;
235 size_t limit;
236};
237
238static struct hlua_mem_allocator hlua_global_allocator;
239
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100240/* These functions converts types between HAProxy internal args or
241 * sample and LUA types. Another function permits to check if the
242 * LUA stack contains arguments according with an required ARG_T
243 * format.
244 */
245static int hlua_arg2lua(lua_State *L, const struct arg *arg);
246static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100247__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100248 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100249static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100250static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100251static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
252
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200253__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
254
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200255#define SEND_ERR(__be, __fmt, __args...) \
256 do { \
257 send_log(__be, LOG_ERR, __fmt, ## __args); \
258 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100259 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200260 } while (0)
261
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100262/* Used to check an Lua function type in the stack. It creates and
263 * returns a reference of the function. This function throws an
264 * error if the rgument is not a "function".
265 */
266__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
267{
268 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100269 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100270 WILL_LJMP(luaL_argerror(L, argno, msg));
271 }
272 lua_pushvalue(L, argno);
273 return luaL_ref(L, LUA_REGISTRYINDEX);
274}
275
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200276/* Return the string that is of the top of the stack. */
277const char *hlua_get_top_error_string(lua_State *L)
278{
279 if (lua_gettop(L) < 1)
280 return "unknown error";
281 if (lua_type(L, -1) != LUA_TSTRING)
282 return "unknown error";
283 return lua_tostring(L, -1);
284}
285
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200286__LJMP static const char *hlua_traceback(lua_State *L)
287{
288 lua_Debug ar;
289 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200290 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200291 int filled = 0;
292
293 while (lua_getstack(L, level++, &ar)) {
294
295 /* Add separator */
296 if (filled)
297 chunk_appendf(msg, ", ");
298 filled = 1;
299
300 /* Fill fields:
301 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
302 * 'l': fills in the field currentline;
303 * 'n': fills in the field name and namewhat;
304 * 't': fills in the field istailcall;
305 */
306 lua_getinfo(L, "Slnt", &ar);
307
308 /* Append code localisation */
309 if (ar.currentline > 0)
310 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
311 else
312 chunk_appendf(msg, "%s ", ar.short_src);
313
314 /*
315 * Get function name
316 *
317 * if namewhat is no empty, name is defined.
318 * what contains "Lua" for Lua function, "C" for C function,
319 * or "main" for main code.
320 */
321 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
322 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
323
324 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
325 chunk_appendf(msg, "main chunk");
326
327 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
328 chunk_appendf(msg, "C function line %d", ar.linedefined);
329
330 else /* nothing left... */
331 chunk_appendf(msg, "?");
332
333
334 /* Display tailed call */
335 if (ar.istailcall)
336 chunk_appendf(msg, " ...");
337 }
338
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200339 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200340}
341
342
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100343/* This function check the number of arguments available in the
344 * stack. If the number of arguments available is not the same
345 * then <nb> an error is throwed.
346 */
347__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
348{
349 if (lua_gettop(L) == nb)
350 return;
351 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
352}
353
Mark Lakes22154b42018-01-29 14:38:40 -0800354/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100355 * and the line number where the error is encountered.
356 */
357static int hlua_pusherror(lua_State *L, const char *fmt, ...)
358{
359 va_list argp;
360 va_start(argp, fmt);
361 luaL_where(L, 1);
362 lua_pushvfstring(L, fmt, argp);
363 va_end(argp);
364 lua_concat(L, 2);
365 return 1;
366}
367
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100368/* This functions is used with sample fetch and converters. It
369 * converts the HAProxy configuration argument in a lua stack
370 * values.
371 *
372 * It takes an array of "arg", and each entry of the array is
373 * converted and pushed in the LUA stack.
374 */
375static int hlua_arg2lua(lua_State *L, const struct arg *arg)
376{
377 switch (arg->type) {
378 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 case ARGT_TIME:
380 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100381 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100382 break;
383
384 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200385 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100386 break;
387
388 case ARGT_IPV4:
389 case ARGT_IPV6:
390 case ARGT_MSK4:
391 case ARGT_MSK6:
392 case ARGT_FE:
393 case ARGT_BE:
394 case ARGT_TAB:
395 case ARGT_SRV:
396 case ARGT_USR:
397 case ARGT_MAP:
398 default:
399 lua_pushnil(L);
400 break;
401 }
402 return 1;
403}
404
405/* This function take one entrie in an LUA stack at the index "ud",
406 * and try to convert it in an HAProxy argument entry. This is useful
407 * with sample fetch wrappers. The input arguments are gived to the
408 * lua wrapper and converted as arg list by thi function.
409 */
410static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
411{
412 switch (lua_type(L, ud)) {
413
414 case LUA_TNUMBER:
415 case LUA_TBOOLEAN:
416 arg->type = ARGT_SINT;
417 arg->data.sint = lua_tointeger(L, ud);
418 break;
419
420 case LUA_TSTRING:
421 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200422 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100423 break;
424
425 case LUA_TUSERDATA:
426 case LUA_TNIL:
427 case LUA_TTABLE:
428 case LUA_TFUNCTION:
429 case LUA_TTHREAD:
430 case LUA_TLIGHTUSERDATA:
431 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200432 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433 break;
434 }
435 return 1;
436}
437
438/* the following functions are used to convert a struct sample
439 * in Lua type. This useful to convert the return of the
440 * fetchs or converters.
441 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100442static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200444 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449
450 case SMP_T_BIN:
451 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 break;
454
455 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
458 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
459 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
460 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
461 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
462 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
463 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
464 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
465 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100467 break;
468 default:
469 lua_pushnil(L);
470 break;
471 }
472 break;
473
474 case SMP_T_IPV4:
475 case SMP_T_IPV6:
476 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200477 if (sample_casts[smp->data.type][SMP_T_STR] &&
478 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100480 else
481 lua_pushnil(L);
482 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100483 default:
484 lua_pushnil(L);
485 break;
486 }
487 return 1;
488}
489
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100490/* the following functions are used to convert a struct sample
491 * in Lua strings. This is useful to convert the return of the
492 * fetchs or converters.
493 */
494static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
495{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200496 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497
498 case SMP_T_BIN:
499 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501 break;
502
503 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
506 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
507 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
508 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
509 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
510 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
511 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
512 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
513 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100515 break;
516 default:
517 lua_pushstring(L, "");
518 break;
519 }
520 break;
521
522 case SMP_T_SINT:
523 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 case SMP_T_IPV4:
525 case SMP_T_IPV6:
526 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 if (sample_casts[smp->data.type][SMP_T_STR] &&
528 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 else
531 lua_pushstring(L, "");
532 break;
533 default:
534 lua_pushstring(L, "");
535 break;
536 }
537 return 1;
538}
539
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540/* the following functions are used to convert an Lua type in a
541 * struct sample. This is useful to provide data from a converter
542 * to the LUA code.
543 */
544static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
545{
546 switch (lua_type(L, ud)) {
547
548 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 break;
552
553
554 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200562 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100563 break;
564
565 case LUA_TUSERDATA:
566 case LUA_TNIL:
567 case LUA_TTABLE:
568 case LUA_TFUNCTION:
569 case LUA_TTHREAD:
570 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200571 case LUA_TNONE:
572 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200574 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100575 break;
576 }
577 return 1;
578}
579
580/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800581 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100583 *
584 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
585 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100586 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100587__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100588 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589{
590 int min_arg;
591 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100592 struct proxy *px;
593 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100594
595 idx = 0;
596 min_arg = ARGM(mask);
597 mask >>= ARGM_BITS;
598
599 while (1) {
600
601 /* Check oversize. */
602 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100603 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100604 }
605
606 /* Check for mandatory arguments. */
607 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100608 if (idx < min_arg) {
609
610 /* If miss other argument than the first one, we return an error. */
611 if (idx > 0)
612 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
613
614 /* If first argument have a certain type, some default values
615 * may be used. See the function smp_resolve_args().
616 */
617 switch (mask & ARGT_MASK) {
618
619 case ARGT_FE:
620 if (!(p->cap & PR_CAP_FE))
621 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
622 argp[idx].data.prx = p;
623 argp[idx].type = ARGT_FE;
624 argp[idx+1].type = ARGT_STOP;
625 break;
626
627 case ARGT_BE:
628 if (!(p->cap & PR_CAP_BE))
629 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
630 argp[idx].data.prx = p;
631 argp[idx].type = ARGT_BE;
632 argp[idx+1].type = ARGT_STOP;
633 break;
634
635 case ARGT_TAB:
636 argp[idx].data.prx = p;
637 argp[idx].type = ARGT_TAB;
638 argp[idx+1].type = ARGT_STOP;
639 break;
640
641 default:
642 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
643 break;
644 }
645 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100646 return 0;
647 }
648
649 /* Check for exceed the number of requiered argument. */
650 if ((mask & ARGT_MASK) == ARGT_STOP &&
651 argp[idx].type != ARGT_STOP) {
652 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
653 }
654
655 if ((mask & ARGT_MASK) == ARGT_STOP &&
656 argp[idx].type == ARGT_STOP) {
657 return 0;
658 }
659
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100660 /* Convert some argument types. */
661 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100662 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100663 if (argp[idx].type != ARGT_SINT)
664 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
665 argp[idx].type = ARGT_SINT;
666 break;
667
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100668 case ARGT_TIME:
669 if (argp[idx].type != ARGT_SINT)
670 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200671 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100672 break;
673
674 case ARGT_SIZE:
675 if (argp[idx].type != ARGT_SINT)
676 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200677 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100678 break;
679
680 case ARGT_FE:
681 if (argp[idx].type != ARGT_STR)
682 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200683 memcpy(trash.area, argp[idx].data.str.area,
684 argp[idx].data.str.data);
685 trash.area[argp[idx].data.str.data] = 0;
686 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100687 if (!argp[idx].data.prx)
688 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
689 argp[idx].type = ARGT_FE;
690 break;
691
692 case ARGT_BE:
693 if (argp[idx].type != ARGT_STR)
694 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200695 memcpy(trash.area, argp[idx].data.str.area,
696 argp[idx].data.str.data);
697 trash.area[argp[idx].data.str.data] = 0;
698 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100699 if (!argp[idx].data.prx)
700 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
701 argp[idx].type = ARGT_BE;
702 break;
703
704 case ARGT_TAB:
705 if (argp[idx].type != ARGT_STR)
706 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200707 memcpy(trash.area, argp[idx].data.str.area,
708 argp[idx].data.str.data);
709 trash.area[argp[idx].data.str.data] = 0;
710 argp[idx].data.prx = proxy_tbl_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100711 if (!argp[idx].data.prx)
712 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
713 argp[idx].type = ARGT_TAB;
714 break;
715
716 case ARGT_SRV:
717 if (argp[idx].type != ARGT_STR)
718 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200719 memcpy(trash.area, argp[idx].data.str.area,
720 argp[idx].data.str.data);
721 trash.area[argp[idx].data.str.data] = 0;
722 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100723 if (sname) {
724 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200726 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100727 if (!px)
728 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
729 }
730 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100732 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100733 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 argp[idx].data.srv = findserver(px, sname);
735 if (!argp[idx].data.srv)
736 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
737 argp[idx].type = ARGT_SRV;
738 break;
739
740 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200741 memcpy(trash.area, argp[idx].data.str.area,
742 argp[idx].data.str.data);
743 trash.area[argp[idx].data.str.data] = 0;
744 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100745 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
746 argp[idx].type = ARGT_IPV4;
747 break;
748
749 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 memcpy(trash.area, argp[idx].data.str.area,
751 argp[idx].data.str.data);
752 trash.area[argp[idx].data.str.data] = 0;
753 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100754 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
755 argp[idx].type = ARGT_MSK4;
756 break;
757
758 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 memcpy(trash.area, argp[idx].data.str.area,
760 argp[idx].data.str.data);
761 trash.area[argp[idx].data.str.data] = 0;
762 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
764 argp[idx].type = ARGT_IPV6;
765 break;
766
767 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 memcpy(trash.area, argp[idx].data.str.area,
769 argp[idx].data.str.data);
770 trash.area[argp[idx].data.str.data] = 0;
771 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100772 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
773 argp[idx].type = ARGT_MSK6;
774 break;
775
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100776 case ARGT_MAP:
777 case ARGT_REG:
778 case ARGT_USR:
779 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100780 break;
781 }
782
783 /* Check for type of argument. */
784 if ((mask & ARGT_MASK) != argp[idx].type) {
785 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
786 arg_type_names[(mask & ARGT_MASK)],
787 arg_type_names[argp[idx].type & ARGT_MASK]);
788 WILL_LJMP(luaL_argerror(L, first + idx, msg));
789 }
790
791 /* Next argument. */
792 mask >>= ARGT_BITS;
793 idx++;
794 }
795}
796
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100797/*
798 * The following functions are used to make correspondance between the the
799 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100800 *
801 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100802 * - hlua_sethlua : create the association between hlua context and lua_state.
803 */
804static inline struct hlua *hlua_gethlua(lua_State *L)
805{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100806 struct hlua **hlua = lua_getextraspace(L);
807 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100808}
809static inline void hlua_sethlua(struct hlua *hlua)
810{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100811 struct hlua **hlua_store = lua_getextraspace(hlua->T);
812 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100813}
814
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100815/* This function is used to send logs. It try to send on screen (stderr)
816 * and on the default syslog server.
817 */
818static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
819{
820 struct tm tm;
821 char *p;
822
823 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200824 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100825 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200826 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200827 /* Break the message if exceed the buffer size. */
828 *(p-4) = ' ';
829 *(p-3) = '.';
830 *(p-2) = '.';
831 *(p-1) = '.';
832 break;
833 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100834 if (isprint(*msg))
835 *p = *msg;
836 else
837 *p = '.';
838 }
839 *p = '\0';
840
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200841 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100842 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200843 get_localtime(date.tv_sec, &tm);
844 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100845 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200846 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100847 fflush(stderr);
848 }
849}
850
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100851/* This function just ensure that the yield will be always
852 * returned with a timeout and permit to set some flags
853 */
854__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100855 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100856{
857 struct hlua *hlua = hlua_gethlua(L);
858
859 /* Set the wake timeout. If timeout is required, we set
860 * the expiration time.
861 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200862 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100863
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100864 hlua->flags |= flags;
865
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100866 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200867 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100868}
869
Willy Tarreau87b09662015-04-03 00:22:06 +0200870/* This function initialises the Lua environment stored in the stream.
871 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100872 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200873 *
874 * This function is particular. it initialises a new Lua thread. If the
875 * initialisation fails (example: out of memory error), the lua function
876 * throws an error (longjmp).
877 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100878 * In some case (at least one), this function can be called from safe
879 * environement, so we must not initialise it. While the support of
880 * threads appear, the safe environment set a lock to ensure only one
881 * Lua execution at a time. If we initialize safe environment in another
882 * safe environmenet, we have a dead lock.
883 *
884 * set "already_safe" true if the context is initialized form safe
885 * Lua fonction.
886 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800887 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200888 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800889 * MUST NOT manipulate the created thread stack state, because it is not
890 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100891 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100892int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100893{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100894 if (!already_safe) {
895 if (!SET_SAFE_LJMP(gL.T)) {
896 lua->Tref = LUA_REFNIL;
897 return 0;
898 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200899 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100900 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100901 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100902 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100903 lua->T = lua_newthread(gL.T);
904 if (!lua->T) {
905 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100906 if (!already_safe)
907 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100908 return 0;
909 }
910 hlua_sethlua(lua);
911 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
912 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100913 if (!already_safe)
914 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100915 return 1;
916}
917
Willy Tarreau87b09662015-04-03 00:22:06 +0200918/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100919 * is destroyed. The destroy also the memory context. The struct "lua"
920 * is not freed.
921 */
922void hlua_ctx_destroy(struct hlua *lua)
923{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100924 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100925 return;
926
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100927 if (!lua->T)
928 goto end;
929
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100930 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200931 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100932
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200933 if (!SET_SAFE_LJMP(lua->T))
934 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100935 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200936 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200937
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200938 if (!SET_SAFE_LJMP(gL.T))
939 return;
940 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
941 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200942 /* Forces a garbage collecting process. If the Lua program is finished
943 * without error, we run the GC on the thread pointer. Its freed all
944 * the unused memory.
945 * If the thread is finnish with an error or is currently yielded,
946 * it seems that the GC applied on the thread doesn't clean anything,
947 * so e run the GC on the main thread.
948 * NOTE: maybe this action locks all the Lua threads untiml the en of
949 * the garbage collection.
950 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200951 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200952 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200953 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200954 lua_gc(gL.T, LUA_GCCOLLECT, 0);
955 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200956 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200957
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200958 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100959
960end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100961 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100962}
963
964/* This function is used to restore the Lua context when a coroutine
965 * fails. This function copy the common memory between old coroutine
966 * and the new coroutine. The old coroutine is destroyed, and its
967 * replaced by the new coroutine.
968 * If the flag "keep_msg" is set, the last entry of the old is assumed
969 * as string error message and it is copied in the new stack.
970 */
971static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
972{
973 lua_State *T;
974 int new_ref;
975
976 /* Renew the main LUA stack doesn't have sense. */
977 if (lua == &gL)
978 return 0;
979
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100980 /* New Lua coroutine. */
981 T = lua_newthread(gL.T);
982 if (!T)
983 return 0;
984
985 /* Copy last error message. */
986 if (keep_msg)
987 lua_xmove(lua->T, T, 1);
988
989 /* Copy data between the coroutines. */
990 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
991 lua_xmove(lua->T, T, 1);
992 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
993
994 /* Destroy old data. */
995 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
996
997 /* The thread is garbage collected by Lua. */
998 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
999
1000 /* Fill the struct with the new coroutine values. */
1001 lua->Mref = new_ref;
1002 lua->T = T;
1003 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1004
1005 /* Set context. */
1006 hlua_sethlua(lua);
1007
1008 return 1;
1009}
1010
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001011void hlua_hook(lua_State *L, lua_Debug *ar)
1012{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001013 struct hlua *hlua = hlua_gethlua(L);
1014
1015 /* Lua cannot yield when its returning from a function,
1016 * so, we can fix the interrupt hook to 1 instruction,
1017 * expecting that the function is finnished.
1018 */
1019 if (lua_gethookmask(L) & LUA_MASKRET) {
1020 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1021 return;
1022 }
1023
1024 /* restore the interrupt condition. */
1025 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1026
1027 /* If we interrupt the Lua processing in yieldable state, we yield.
1028 * If the state is not yieldable, trying yield causes an error.
1029 */
1030 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001031 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001032
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001033 /* If we cannot yield, update the clock and check the timeout. */
1034 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001035 hlua->run_time += now_ms - hlua->start_time;
1036 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001037 lua_pushfstring(L, "execution timeout");
1038 WILL_LJMP(lua_error(L));
1039 }
1040
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001041 /* Update the start time. */
1042 hlua->start_time = now_ms;
1043
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001044 /* Try to interrupt the process at the end of the current
1045 * unyieldable function.
1046 */
1047 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001048}
1049
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001050/* This function start or resumes the Lua stack execution. If the flag
1051 * "yield_allowed" if no set and the LUA stack execution returns a yield
1052 * The function return an error.
1053 *
1054 * The function can returns 4 values:
1055 * - HLUA_E_OK : The execution is terminated without any errors.
1056 * - HLUA_E_AGAIN : The execution must continue at the next associated
1057 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001058 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001059 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001060 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001061 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001062 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001063 * LUA code.
1064 */
1065static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1066{
1067 int ret;
1068 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001069 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001071 /* Initialise run time counter. */
1072 if (!HLUA_IS_RUNNING(lua))
1073 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001074
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001075 /* Lock the whole Lua execution. This lock must be before the
1076 * label "resume_execution".
1077 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001078 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001079
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001080resume_execution:
1081
1082 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1083 * instructions. it is used for preventing infinite loops.
1084 */
1085 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1086
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001087 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001088 HLUA_SET_RUN(lua);
1089 HLUA_CLR_CTRLYIELD(lua);
1090 HLUA_CLR_WAKERESWR(lua);
1091 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001092
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001093 /* Update the start time. */
1094 lua->start_time = now_ms;
1095
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001096 /* Call the function. */
1097 ret = lua_resume(lua->T, gL.T, lua->nargs);
1098 switch (ret) {
1099
1100 case LUA_OK:
1101 ret = HLUA_E_OK;
1102 break;
1103
1104 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001105 /* Check if the execution timeout is expired. It it is the case, we
1106 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001107 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001108 tv_update_date(0, 1);
1109 lua->run_time += now_ms - lua->start_time;
1110 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001111 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001112 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001113 break;
1114 }
1115 /* Process the forced yield. if the general yield is not allowed or
1116 * if no task were associated this the current Lua execution
1117 * coroutine, we resume the execution. Else we want to return in the
1118 * scheduler and we want to be waked up again, to continue the
1119 * current Lua execution. So we schedule our own task.
1120 */
1121 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001122 if (!yield_allowed || !lua->task)
1123 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001124 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001125 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001126 if (!yield_allowed) {
1127 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001128 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001129 break;
1130 }
1131 ret = HLUA_E_AGAIN;
1132 break;
1133
1134 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001135
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001136 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001137 * because the errors ares the only one mean to return immediately
1138 * from and lua execution.
1139 */
1140 if (lua->flags & HLUA_EXIT) {
1141 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001142 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001143 break;
1144 }
1145
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001146 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001147 if (!lua_checkstack(lua->T, 1)) {
1148 ret = HLUA_E_ERR;
1149 break;
1150 }
1151 msg = lua_tostring(lua->T, -1);
1152 lua_settop(lua->T, 0); /* Empty the stack. */
1153 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001154 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001155 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001156 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001157 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001158 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001159 ret = HLUA_E_ERRMSG;
1160 break;
1161
1162 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001163 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001165 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 break;
1167
1168 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001169 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 if (!lua_checkstack(lua->T, 1)) {
1171 ret = HLUA_E_ERR;
1172 break;
1173 }
1174 msg = lua_tostring(lua->T, -1);
1175 lua_settop(lua->T, 0); /* Empty the stack. */
1176 lua_pop(lua->T, 1);
1177 if (msg)
1178 lua_pushfstring(lua->T, "message handler error: %s", msg);
1179 else
1180 lua_pushfstring(lua->T, "message handler error");
1181 ret = HLUA_E_ERRMSG;
1182 break;
1183
1184 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001185 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001186 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001187 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001188 break;
1189 }
1190
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001191 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001192 if (lua->flags & HLUA_MUST_GC &&
1193 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001194 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1195
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196 switch (ret) {
1197 case HLUA_E_AGAIN:
1198 break;
1199
1200 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001201 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001202 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001203 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001204 break;
1205
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001206 case HLUA_E_ETMOUT:
1207 case HLUA_E_NOMEM:
1208 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001209 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001210 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001211 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 hlua_ctx_renew(lua, 0);
1213 break;
1214
1215 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001216 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001217 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001218 break;
1219 }
1220
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001221 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001222 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001223
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001224 return ret;
1225}
1226
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001227/* This function exit the current code. */
1228__LJMP static int hlua_done(lua_State *L)
1229{
1230 struct hlua *hlua = hlua_gethlua(L);
1231
1232 hlua->flags |= HLUA_EXIT;
1233 WILL_LJMP(lua_error(L));
1234
1235 return 0;
1236}
1237
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001238/* This function is an LUA binding. It provides a function
1239 * for deleting ACL from a referenced ACL file.
1240 */
1241__LJMP static int hlua_del_acl(lua_State *L)
1242{
1243 const char *name;
1244 const char *key;
1245 struct pat_ref *ref;
1246
1247 MAY_LJMP(check_args(L, 2, "del_acl"));
1248
1249 name = MAY_LJMP(luaL_checkstring(L, 1));
1250 key = MAY_LJMP(luaL_checkstring(L, 2));
1251
1252 ref = pat_ref_lookup(name);
1253 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001254 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001255
1256 pat_ref_delete(ref, key);
1257 return 0;
1258}
1259
1260/* This function is an LUA binding. It provides a function
1261 * for deleting map entry from a referenced map file.
1262 */
1263static int hlua_del_map(lua_State *L)
1264{
1265 const char *name;
1266 const char *key;
1267 struct pat_ref *ref;
1268
1269 MAY_LJMP(check_args(L, 2, "del_map"));
1270
1271 name = MAY_LJMP(luaL_checkstring(L, 1));
1272 key = MAY_LJMP(luaL_checkstring(L, 2));
1273
1274 ref = pat_ref_lookup(name);
1275 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001276 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001277
1278 pat_ref_delete(ref, key);
1279 return 0;
1280}
1281
1282/* This function is an LUA binding. It provides a function
1283 * for adding ACL pattern from a referenced ACL file.
1284 */
1285static int hlua_add_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, "add_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, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001299
1300 if (pat_ref_find_elt(ref, key) == NULL)
1301 pat_ref_add(ref, key, NULL, NULL);
1302 return 0;
1303}
1304
1305/* This function is an LUA binding. It provides a function
1306 * for setting map pattern and sample from a referenced map
1307 * file.
1308 */
1309static int hlua_set_map(lua_State *L)
1310{
1311 const char *name;
1312 const char *key;
1313 const char *value;
1314 struct pat_ref *ref;
1315
1316 MAY_LJMP(check_args(L, 3, "set_map"));
1317
1318 name = MAY_LJMP(luaL_checkstring(L, 1));
1319 key = MAY_LJMP(luaL_checkstring(L, 2));
1320 value = MAY_LJMP(luaL_checkstring(L, 3));
1321
1322 ref = pat_ref_lookup(name);
1323 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001324 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001325
1326 if (pat_ref_find_elt(ref, key) != NULL)
1327 pat_ref_set(ref, key, value, NULL);
1328 else
1329 pat_ref_add(ref, key, value, NULL);
1330 return 0;
1331}
1332
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001333/* A class is a lot of memory that contain data. This data can be a table,
1334 * an integer or user data. This data is associated with a metatable. This
1335 * metatable have an original version registred in the global context with
1336 * the name of the object (_G[<name>] = <metable> ).
1337 *
1338 * A metable is a table that modify the standard behavior of a standard
1339 * access to the associated data. The entries of this new metatable are
1340 * defined as is:
1341 *
1342 * http://lua-users.org/wiki/MetatableEvents
1343 *
1344 * __index
1345 *
1346 * we access an absent field in a table, the result is nil. This is
1347 * true, but it is not the whole truth. Actually, such access triggers
1348 * the interpreter to look for an __index metamethod: If there is no
1349 * such method, as usually happens, then the access results in nil;
1350 * otherwise, the metamethod will provide the result.
1351 *
1352 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1353 * the key does not appear in the table, but the metatable has an __index
1354 * property:
1355 *
1356 * - if the value is a function, the function is called, passing in the
1357 * table and the key; the return value of that function is returned as
1358 * the result.
1359 *
1360 * - if the value is another table, the value of the key in that table is
1361 * asked for and returned (and if it doesn't exist in that table, but that
1362 * table's metatable has an __index property, then it continues on up)
1363 *
1364 * - Use "rawget(myTable,key)" to skip this metamethod.
1365 *
1366 * http://www.lua.org/pil/13.4.1.html
1367 *
1368 * __newindex
1369 *
1370 * Like __index, but control property assignment.
1371 *
1372 * __mode - Control weak references. A string value with one or both
1373 * of the characters 'k' and 'v' which specifies that the the
1374 * keys and/or values in the table are weak references.
1375 *
1376 * __call - Treat a table like a function. When a table is followed by
1377 * parenthesis such as "myTable( 'foo' )" and the metatable has
1378 * a __call key pointing to a function, that function is invoked
1379 * (passing any specified arguments) and the return value is
1380 * returned.
1381 *
1382 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1383 * called, if the metatable for myTable has a __metatable
1384 * key, the value of that key is returned instead of the
1385 * actual metatable.
1386 *
1387 * __tostring - Control string representation. When the builtin
1388 * "tostring( myTable )" function is called, if the metatable
1389 * for myTable has a __tostring property set to a function,
1390 * that function is invoked (passing myTable to it) and the
1391 * return value is used as the string representation.
1392 *
1393 * __len - Control table length. When the table length is requested using
1394 * the length operator ( '#' ), if the metatable for myTable has
1395 * a __len key pointing to a function, that function is invoked
1396 * (passing myTable to it) and the return value used as the value
1397 * of "#myTable".
1398 *
1399 * __gc - Userdata finalizer code. When userdata is set to be garbage
1400 * collected, if the metatable has a __gc field pointing to a
1401 * function, that function is first invoked, passing the userdata
1402 * to it. The __gc metamethod is not called for tables.
1403 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1404 *
1405 * Special metamethods for redefining standard operators:
1406 * http://www.lua.org/pil/13.1.html
1407 *
1408 * __add "+"
1409 * __sub "-"
1410 * __mul "*"
1411 * __div "/"
1412 * __unm "!"
1413 * __pow "^"
1414 * __concat ".."
1415 *
1416 * Special methods for redfining standar relations
1417 * http://www.lua.org/pil/13.2.html
1418 *
1419 * __eq "=="
1420 * __lt "<"
1421 * __le "<="
1422 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001423
1424/*
1425 *
1426 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001427 * Class Map
1428 *
1429 *
1430 */
1431
1432/* Returns a struct hlua_map if the stack entry "ud" is
1433 * a class session, otherwise it throws an error.
1434 */
1435__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1436{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001437 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001438}
1439
1440/* This function is the map constructor. It don't need
1441 * the class Map object. It creates and return a new Map
1442 * object. It must be called only during "body" or "init"
1443 * context because it process some filesystem accesses.
1444 */
1445__LJMP static int hlua_map_new(struct lua_State *L)
1446{
1447 const char *fn;
1448 int match = PAT_MATCH_STR;
1449 struct sample_conv conv;
1450 const char *file = "";
1451 int line = 0;
1452 lua_Debug ar;
1453 char *err = NULL;
1454 struct arg args[2];
1455
1456 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1457 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1458
1459 fn = MAY_LJMP(luaL_checkstring(L, 1));
1460
1461 if (lua_gettop(L) >= 2) {
1462 match = MAY_LJMP(luaL_checkinteger(L, 2));
1463 if (match < 0 || match >= PAT_MATCH_NUM)
1464 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1465 }
1466
1467 /* Get Lua filename and line number. */
1468 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1469 lua_getinfo(L, "Sl", &ar); /* get info about it */
1470 if (ar.currentline > 0) { /* is there info? */
1471 file = ar.short_src;
1472 line = ar.currentline;
1473 }
1474 }
1475
1476 /* fill fake sample_conv struct. */
1477 conv.kw = ""; /* unused. */
1478 conv.process = NULL; /* unused. */
1479 conv.arg_mask = 0; /* unused. */
1480 conv.val_args = NULL; /* unused. */
1481 conv.out_type = SMP_T_STR;
1482 conv.private = (void *)(long)match;
1483 switch (match) {
1484 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1485 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1486 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1487 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1488 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1489 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1490 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001491 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001492 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1493 default:
1494 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1495 }
1496
1497 /* fill fake args. */
1498 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001499 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001500 args[1].type = ARGT_STOP;
1501
1502 /* load the map. */
1503 if (!sample_load_map(args, &conv, file, line, &err)) {
1504 /* error case: we cant use luaL_error because we must
1505 * free the err variable.
1506 */
1507 luaL_where(L, 1);
1508 lua_pushfstring(L, "'new': %s.", err);
1509 lua_concat(L, 2);
1510 free(err);
1511 WILL_LJMP(lua_error(L));
1512 }
1513
1514 /* create the lua object. */
1515 lua_newtable(L);
1516 lua_pushlightuserdata(L, args[0].data.map);
1517 lua_rawseti(L, -2, 0);
1518
1519 /* Pop a class Map metatable and affect it to the userdata. */
1520 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1521 lua_setmetatable(L, -2);
1522
1523
1524 return 1;
1525}
1526
1527__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1528{
1529 struct map_descriptor *desc;
1530 struct pattern *pat;
1531 struct sample smp;
1532
1533 MAY_LJMP(check_args(L, 2, "lookup"));
1534 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001535 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001536 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001537 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001538 }
1539 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001540 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001541 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001542 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001543 }
1544
1545 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001546 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001547 if (str)
1548 lua_pushstring(L, "");
1549 else
1550 lua_pushnil(L);
1551 return 1;
1552 }
1553
1554 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001555 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001556 return 1;
1557}
1558
1559__LJMP static int hlua_map_lookup(struct lua_State *L)
1560{
1561 return _hlua_map_lookup(L, 0);
1562}
1563
1564__LJMP static int hlua_map_slookup(struct lua_State *L)
1565{
1566 return _hlua_map_lookup(L, 1);
1567}
1568
1569/*
1570 *
1571 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001572 * Class Socket
1573 *
1574 *
1575 */
1576
1577__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1578{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001579 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001580}
1581
1582/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001583 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001584 * received.
1585 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001586static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001587{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001588 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001590 if (appctx->ctx.hlua_cosocket.die) {
1591 si_shutw(si);
1592 si_shutr(si);
1593 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001594 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1595 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001596 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1597 }
1598
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001599 /* If we cant write, wakeup the pending write signals. */
1600 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001601 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001602
1603 /* If we cant read, wakeup the pending read signals. */
1604 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001605 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001606
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001607 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001608 * to be notified whenever the connection completes.
1609 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001610 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001611 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001612 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001613 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001614 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001615 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001616
1617 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001618 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001619
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001620 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001621 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001622 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001623
1624 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001625 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001626 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001627
1628 /* Some data were injected in the buffer, notify the stream
1629 * interface.
1630 */
1631 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001632 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001633
1634 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001635 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001636 */
1637 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001638 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001639}
1640
Willy Tarreau87b09662015-04-03 00:22:06 +02001641/* This function is called when the "struct stream" is destroyed.
1642 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001643 * Wake all the pending signals.
1644 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001645static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001646{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001647 struct xref *peer;
1648
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001649 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001650 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1651 if (peer)
1652 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001653
1654 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001655 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1656 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657}
1658
1659/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001660 * uses this object. If the stream does not exists, just quit.
1661 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001662 * pending signal can rest in the read and write lists. destroy
1663 * it.
1664 */
1665__LJMP static int hlua_socket_gc(lua_State *L)
1666{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001667 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001668 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001669 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001670
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001671 MAY_LJMP(check_args(L, 1, "__gc"));
1672
1673 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001674 peer = xref_get_peer_and_lock(&socket->xref);
1675 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001676 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001677 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001678
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001679 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001680 appctx->ctx.hlua_cosocket.die = 1;
1681 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001683 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001684 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685 return 0;
1686}
1687
1688/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001689 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001690 */
sada05ed3302018-05-11 11:48:18 -07001691__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001693 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001695 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001696
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001697 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001698
1699 /* Check if we run on the same thread than the xreator thread.
1700 * We cannot access to the socket if the thread is different.
1701 */
1702 if (socket->tid != tid)
1703 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1704
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001705 peer = xref_get_peer_and_lock(&socket->xref);
1706 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001707 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001708 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001710 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001711 appctx->ctx.hlua_cosocket.die = 1;
1712 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001714 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001715 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716 return 0;
1717}
1718
sada05ed3302018-05-11 11:48:18 -07001719/* The close function calls close_helper.
1720 */
1721__LJMP static int hlua_socket_close(lua_State *L)
1722{
1723 MAY_LJMP(check_args(L, 1, "close"));
1724 return hlua_socket_close_helper(L);
1725}
1726
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727/* This Lua function assumes that the stack contain three parameters.
1728 * 1 - USERDATA containing a struct socket
1729 * 2 - INTEGER with values of the macro defined below
1730 * If the integer is -1, we must read at most one line.
1731 * If the integer is -2, we ust read all the data until the
1732 * end of the stream.
1733 * If the integer is positive value, we must read a number of
1734 * bytes corresponding to this value.
1735 */
1736#define HLSR_READ_LINE (-1)
1737#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001738__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001739{
1740 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1741 int wanted = lua_tointeger(L, 2);
1742 struct hlua *hlua = hlua_gethlua(L);
1743 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001744 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001745 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001746 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001747 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001748 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001749 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001750 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001751 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001752 struct stream_interface *si;
1753 struct stream *s;
1754 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001755 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001756
1757 /* Check if this lua stack is schedulable. */
1758 if (!hlua || !hlua->task)
1759 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1760 "'frontend', 'backend' or 'task'"));
1761
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001762 /* Check if we run on the same thread than the xreator thread.
1763 * We cannot access to the socket if the thread is different.
1764 */
1765 if (socket->tid != tid)
1766 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1767
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001768 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001769 peer = xref_get_peer_and_lock(&socket->xref);
1770 if (!peer)
1771 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001772 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1773 si = appctx->owner;
1774 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001775
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001776 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001777 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001778 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001779 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001780 if (nblk < 0) /* Connection close. */
1781 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001782 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001784
1785 /* remove final \r\n. */
1786 if (nblk == 1) {
1787 if (blk1[len1-1] == '\n') {
1788 len1--;
1789 skip_at_end++;
1790 if (blk1[len1-1] == '\r') {
1791 len1--;
1792 skip_at_end++;
1793 }
1794 }
1795 }
1796 else {
1797 if (blk2[len2-1] == '\n') {
1798 len2--;
1799 skip_at_end++;
1800 if (blk2[len2-1] == '\r') {
1801 len2--;
1802 skip_at_end++;
1803 }
1804 }
1805 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001806 }
1807
1808 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001809 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001810 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001811 if (nblk < 0) /* Connection close. */
1812 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001813 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001814 goto connection_empty;
1815 }
1816
1817 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001819 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 if (nblk < 0) /* Connection close. */
1821 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001822 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 goto connection_empty;
1824
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001825 missing_bytes = wanted - socket->b.n;
1826 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001828 len1 = missing_bytes;
1829 } if (nblk == 2 && len1 + len2 > missing_bytes)
1830 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001831 }
1832
1833 len = len1;
1834
1835 luaL_addlstring(&socket->b, blk1, len1);
1836 if (nblk == 2) {
1837 len += len2;
1838 luaL_addlstring(&socket->b, blk2, len2);
1839 }
1840
1841 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001842 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843
1844 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001845 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846
1847 /* If the pattern reclaim to read all the data
1848 * in the connection, got out.
1849 */
1850 if (wanted == HLSR_READ_ALL)
1851 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001852 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853 goto connection_empty;
1854
1855 /* Return result. */
1856 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001857 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858 return 1;
1859
1860connection_closed:
1861
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001862 xref_unlock(&socket->xref, peer);
1863
1864no_peer:
1865
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 /* If the buffer containds data. */
1867 if (socket->b.n > 0) {
1868 luaL_pushresult(&socket->b);
1869 return 1;
1870 }
1871 lua_pushnil(L);
1872 lua_pushstring(L, "connection closed.");
1873 return 2;
1874
1875connection_empty:
1876
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001877 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1878 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001879 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001880 }
1881 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001882 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883 return 0;
1884}
1885
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001886/* This Lua function gets two parameters. The first one can be string
1887 * or a number. If the string is "*l", the user requires one line. If
1888 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001889 * If the value is a number, the user require a number of bytes equal
1890 * to the value. The default value is "*l" (a line).
1891 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001892 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 * integer takes this values:
1894 * -1 : read a line
1895 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001896 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001897 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001898 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899 * concatenated with the read data.
1900 */
1901__LJMP static int hlua_socket_receive(struct lua_State *L)
1902{
1903 int wanted = HLSR_READ_LINE;
1904 const char *pattern;
1905 int type;
1906 char *error;
1907 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001908 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
1910 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1911 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1912
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001913 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001915 /* Check if we run on the same thread than the xreator thread.
1916 * We cannot access to the socket if the thread is different.
1917 */
1918 if (socket->tid != tid)
1919 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1920
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921 /* check for pattern. */
1922 if (lua_gettop(L) >= 2) {
1923 type = lua_type(L, 2);
1924 if (type == LUA_TSTRING) {
1925 pattern = lua_tostring(L, 2);
1926 if (strcmp(pattern, "*a") == 0)
1927 wanted = HLSR_READ_ALL;
1928 else if (strcmp(pattern, "*l") == 0)
1929 wanted = HLSR_READ_LINE;
1930 else {
1931 wanted = strtoll(pattern, &error, 10);
1932 if (*error != '\0')
1933 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1934 }
1935 }
1936 else if (type == LUA_TNUMBER) {
1937 wanted = lua_tointeger(L, 2);
1938 if (wanted < 0)
1939 WILL_LJMP(luaL_error(L, "Unsupported size."));
1940 }
1941 }
1942
1943 /* Set pattern. */
1944 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001945
1946 /* Check if we would replace the top by itself. */
1947 if (lua_gettop(L) != 2)
1948 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001949
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001950 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001951 luaL_buffinit(L, &socket->b);
1952
1953 /* Check prefix. */
1954 if (lua_gettop(L) >= 3) {
1955 if (lua_type(L, 3) != LUA_TSTRING)
1956 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1957 pattern = lua_tolstring(L, 3, &len);
1958 luaL_addlstring(&socket->b, pattern, len);
1959 }
1960
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001961 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962}
1963
1964/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001965 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001966 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001967static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001968{
1969 struct hlua_socket *socket;
1970 struct hlua *hlua = hlua_gethlua(L);
1971 struct appctx *appctx;
1972 size_t buf_len;
1973 const char *buf;
1974 int len;
1975 int send_len;
1976 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001977 struct xref *peer;
1978 struct stream_interface *si;
1979 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980
1981 /* Check if this lua stack is schedulable. */
1982 if (!hlua || !hlua->task)
1983 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1984 "'frontend', 'backend' or 'task'"));
1985
1986 /* Get object */
1987 socket = MAY_LJMP(hlua_checksocket(L, 1));
1988 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001989 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001990
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001991 /* Check if we run on the same thread than the xreator thread.
1992 * We cannot access to the socket if the thread is different.
1993 */
1994 if (socket->tid != tid)
1995 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1996
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001997 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001998 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001999 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002000 lua_pushinteger(L, -1);
2001 return 1;
2002 }
2003 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2004 si = appctx->owner;
2005 s = si_strm(si);
2006
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002007 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002008 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002009 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002010 lua_pushinteger(L, -1);
2011 return 1;
2012 }
2013
2014 /* Update the input buffer data. */
2015 buf += sent;
2016 send_len = buf_len - sent;
2017
2018 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002019 if (sent >= buf_len) {
2020 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002021 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002022 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002023
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002024 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002025 * the request buffer if its not required.
2026 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002027 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002028 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002029 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002030 }
2031
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002032 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002033 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002034 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002035 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002036 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037
2038 /* send data */
2039 if (len < send_len)
2040 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002041 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042
2043 /* "Not enough space" (-1), "Buffer too little to contain
2044 * the data" (-2) are not expected because the available length
2045 * is tested.
2046 * Other unknown error are also not expected.
2047 */
2048 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002049 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002050 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002051
sada05ed3302018-05-11 11:48:18 -07002052 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002053 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002054 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002055 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002056 return 1;
2057 }
2058
2059 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002060 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002061
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002062 s->req.rex = TICK_ETERNITY;
2063 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002064
2065 /* Update length sent. */
2066 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002067 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002068
2069 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002070 if (sent + len >= buf_len) {
2071 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002072 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002073 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002074
2075hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002076 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2077 xref_unlock(&socket->xref, peer);
2078 WILL_LJMP(luaL_error(L, "out of memory"));
2079 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002080 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002081 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082 return 0;
2083}
2084
2085/* This function initiate the send of data. It just check the input
2086 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002087 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088 * "hlua_socket_write_yield" that can yield.
2089 *
2090 * The Lua function gets between 3 and 4 parameters. The first one is
2091 * the associated object. The second is a string buffer. The third is
2092 * a facultative integer that represents where is the buffer position
2093 * of the start of the data that can send. The first byte is the
2094 * position "1". The default value is "1". The fourth argument is a
2095 * facultative integer that represents where is the buffer position
2096 * of the end of the data that can send. The default is the last byte.
2097 */
2098static int hlua_socket_send(struct lua_State *L)
2099{
2100 int i;
2101 int j;
2102 const char *buf;
2103 size_t buf_len;
2104
2105 /* Check number of arguments. */
2106 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2107 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2108
2109 /* Get the string. */
2110 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2111
2112 /* Get and check j. */
2113 if (lua_gettop(L) == 4) {
2114 j = MAY_LJMP(luaL_checkinteger(L, 4));
2115 if (j < 0)
2116 j = buf_len + j + 1;
2117 if (j > buf_len)
2118 j = buf_len + 1;
2119 lua_pop(L, 1);
2120 }
2121 else
2122 j = buf_len;
2123
2124 /* Get and check i. */
2125 if (lua_gettop(L) == 3) {
2126 i = MAY_LJMP(luaL_checkinteger(L, 3));
2127 if (i < 0)
2128 i = buf_len + i + 1;
2129 if (i > buf_len)
2130 i = buf_len + 1;
2131 lua_pop(L, 1);
2132 } else
2133 i = 1;
2134
2135 /* Check bth i and j. */
2136 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002137 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002138 return 1;
2139 }
2140 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002141 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002142 return 1;
2143 }
2144 if (i == 0)
2145 i = 1;
2146 if (j == 0)
2147 j = 1;
2148
2149 /* Pop the string. */
2150 lua_pop(L, 1);
2151
2152 /* Update the buffer length. */
2153 buf += i - 1;
2154 buf_len = j - i + 1;
2155 lua_pushlstring(L, buf, buf_len);
2156
2157 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002158 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002159
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002160 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002161}
2162
Willy Tarreau22b0a682015-06-17 19:43:49 +02002163#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002164__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2165{
2166 static char buffer[SOCKET_INFO_MAX_LEN];
2167 int ret;
2168 int len;
2169 char *p;
2170
2171 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2172 if (ret <= 0) {
2173 lua_pushnil(L);
2174 return 1;
2175 }
2176
2177 if (ret == AF_UNIX) {
2178 lua_pushstring(L, buffer+1);
2179 return 1;
2180 }
2181 else if (ret == AF_INET6) {
2182 buffer[0] = '[';
2183 len = strlen(buffer);
2184 buffer[len] = ']';
2185 len++;
2186 buffer[len] = ':';
2187 len++;
2188 p = buffer;
2189 }
2190 else if (ret == AF_INET) {
2191 p = buffer + 1;
2192 len = strlen(p);
2193 p[len] = ':';
2194 len++;
2195 }
2196 else {
2197 lua_pushnil(L);
2198 return 1;
2199 }
2200
2201 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2202 lua_pushnil(L);
2203 return 1;
2204 }
2205
2206 lua_pushstring(L, p);
2207 return 1;
2208}
2209
2210/* Returns information about the peer of the connection. */
2211__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2212{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002213 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002214 struct xref *peer;
2215 struct appctx *appctx;
2216 struct stream_interface *si;
2217 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002218 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002219
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002220 MAY_LJMP(check_args(L, 1, "getpeername"));
2221
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002222 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002223
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002224 /* Check if we run on the same thread than the xreator thread.
2225 * We cannot access to the socket if the thread is different.
2226 */
2227 if (socket->tid != tid)
2228 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2229
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002230 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002231 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002232 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233 lua_pushnil(L);
2234 return 1;
2235 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002236 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2237 si = appctx->owner;
2238 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002239
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002240 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002241 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002242 lua_pushnil(L);
2243 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244 }
2245
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002246 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002247 xref_unlock(&socket->xref, peer);
2248 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249}
2250
2251/* Returns information about my connection side. */
2252static int hlua_socket_getsockname(struct lua_State *L)
2253{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002254 struct hlua_socket *socket;
2255 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002256 struct appctx *appctx;
2257 struct xref *peer;
2258 struct stream_interface *si;
2259 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002260 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002261
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 MAY_LJMP(check_args(L, 1, "getsockname"));
2263
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002264 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002266 /* Check if we run on the same thread than the xreator thread.
2267 * We cannot access to the socket if the thread is different.
2268 */
2269 if (socket->tid != tid)
2270 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2271
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002272 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002273 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002274 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002275 lua_pushnil(L);
2276 return 1;
2277 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002278 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2279 si = appctx->owner;
2280 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002282 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002283 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002284 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285 lua_pushnil(L);
2286 return 1;
2287 }
2288
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002289 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002290 xref_unlock(&socket->xref, peer);
2291 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292}
2293
2294/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002295static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002296 .obj_type = OBJ_TYPE_APPLET,
2297 .name = "<LUA_TCP>",
2298 .fct = hlua_socket_handler,
2299 .release = hlua_socket_release,
2300};
2301
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002302__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303{
2304 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2305 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002306 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002308 struct stream_interface *si;
2309 struct stream *s;
2310
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002311 /* Check if we run on the same thread than the xreator thread.
2312 * We cannot access to the socket if the thread is different.
2313 */
2314 if (socket->tid != tid)
2315 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2316
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002317 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002318 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002319 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002320 lua_pushnil(L);
2321 lua_pushstring(L, "Can't connect");
2322 return 2;
2323 }
2324 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2325 si = appctx->owner;
2326 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002327
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002328 /* Check if we run on the same thread than the xreator thread.
2329 * We cannot access to the socket if the thread is different.
2330 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002331 if (socket->tid != tid) {
2332 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002333 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002334 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002335
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002336 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002337 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002338 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002339 lua_pushnil(L);
2340 lua_pushstring(L, "Can't connect");
2341 return 2;
2342 }
2343
Willy Tarreaue09101e2018-10-16 17:37:12 +02002344 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002345
2346 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002347 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002349 lua_pushinteger(L, 1);
2350 return 1;
2351 }
2352
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002353 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2354 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002355 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002356 }
2357 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002358 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359 return 0;
2360}
2361
2362/* This function fail or initite the connection. */
2363__LJMP static int hlua_socket_connect(struct lua_State *L)
2364{
2365 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002366 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002368 struct hlua *hlua;
2369 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002370 int low, high;
2371 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002372 struct xref *peer;
2373 struct stream_interface *si;
2374 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002375
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002376 if (lua_gettop(L) < 2)
2377 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378
2379 /* Get args. */
2380 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002381
2382 /* Check if we run on the same thread than the xreator thread.
2383 * We cannot access to the socket if the thread is different.
2384 */
2385 if (socket->tid != tid)
2386 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2387
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002388 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002389 if (lua_gettop(L) >= 3) {
2390 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002391 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002392
Tim Duesterhus6edab862018-01-06 19:04:45 +01002393 /* Force the ip to end with a colon, to support IPv6 addresses
2394 * that are not enclosed within square brackets.
2395 */
2396 if (port > 0) {
2397 luaL_buffinit(L, &b);
2398 luaL_addstring(&b, ip);
2399 luaL_addchar(&b, ':');
2400 luaL_pushresult(&b);
2401 ip = lua_tolstring(L, lua_gettop(L), NULL);
2402 }
2403 }
2404
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002405 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002406 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002407 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002408 lua_pushnil(L);
2409 return 1;
2410 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002411
2412 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002413 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002414 if (!addr) {
2415 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002416 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002417 }
2418 if (low != high) {
2419 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002420 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002421 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002422
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002423 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002424 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002425 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002426 if (port == -1) {
2427 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002428 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002429 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002430 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2431 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 if (port == -1) {
2433 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002434 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002435 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002436 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002437 }
2438 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002439
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002440 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2441 si = appctx->owner;
2442 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002443
2444 if (!sockaddr_alloc(&s->target_addr)) {
2445 xref_unlock(&socket->xref, peer);
2446 WILL_LJMP(luaL_error(L, "connect: internal error"));
2447 }
2448 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002449 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002450
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002451 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002452
2453 /* inform the stream that we want to be notified whenever the
2454 * connection completes.
2455 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002456 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002457 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002458 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002459
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002460 hlua->flags |= HLUA_MUST_GC;
2461
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002462 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2463 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002464 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002465 }
2466 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002467
2468 task_wakeup(s->task, TASK_WOKEN_INIT);
2469 /* Return yield waiting for connection. */
2470
Willy Tarreau9635e032018-10-16 17:52:55 +02002471 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472
2473 return 0;
2474}
2475
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002476#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002477__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2478{
2479 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002480 struct xref *peer;
2481 struct appctx *appctx;
2482 struct stream_interface *si;
2483 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002484
2485 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2486 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002487
2488 /* 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 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002499 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500 return MAY_LJMP(hlua_socket_connect(L));
2501}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002502#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002503
2504__LJMP static int hlua_socket_setoption(struct lua_State *L)
2505{
2506 return 0;
2507}
2508
2509__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2510{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002511 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002512 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002513 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002514 struct xref *peer;
2515 struct appctx *appctx;
2516 struct stream_interface *si;
2517 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002518
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519 MAY_LJMP(check_args(L, 2, "settimeout"));
2520
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002521 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002522
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002523 /* convert the timeout to millis */
2524 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002525
Thierry Fournier17a921b2018-03-08 09:59:02 +01002526 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002527 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002528 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2529
Mark Lakes56cc1252018-03-27 09:48:06 +02002530 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002531 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002532
2533 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002534 if (tmout == 0)
2535 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002536
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002537 /* Check if we run on the same thread than the xreator thread.
2538 * We cannot access to the socket if the thread is different.
2539 */
2540 if (socket->tid != tid)
2541 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2542
Mark Lakes56cc1252018-03-27 09:48:06 +02002543 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002544 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002545 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002546 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2547 WILL_LJMP(lua_error(L));
2548 return 0;
2549 }
2550 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2551 si = appctx->owner;
2552 s = si_strm(si);
2553
Cyril Bonté7bb63452018-08-17 23:51:02 +02002554 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002555 s->req.rto = tmout;
2556 s->req.wto = tmout;
2557 s->res.rto = tmout;
2558 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002559 s->req.rex = tick_add_ifset(now_ms, tmout);
2560 s->req.wex = tick_add_ifset(now_ms, tmout);
2561 s->res.rex = tick_add_ifset(now_ms, tmout);
2562 s->res.wex = tick_add_ifset(now_ms, tmout);
2563
2564 s->task->expire = tick_add_ifset(now_ms, tmout);
2565 task_queue(s->task);
2566
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002567 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002568
Thierry Fourniere9636f12018-03-08 09:54:32 +01002569 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002570 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002571}
2572
2573__LJMP static int hlua_socket_new(lua_State *L)
2574{
2575 struct hlua_socket *socket;
2576 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002577 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002578 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002579
2580 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002581 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002582 hlua_pusherror(L, "socket: full stack");
2583 goto out_fail_conf;
2584 }
2585
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002586 /* Create the object: obj[0] = userdata. */
2587 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002588 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002589 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002590 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002591 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002592
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002593 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002594 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002595 hlua_pusherror(L, "socket: uninitialized pools.");
2596 goto out_fail_conf;
2597 }
2598
Willy Tarreau87b09662015-04-03 00:22:06 +02002599 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002600 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2601 lua_setmetatable(L, -2);
2602
Willy Tarreaud420a972015-04-06 00:39:18 +02002603 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002604 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002605 if (!appctx) {
2606 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002607 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002608 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002610 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002611 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002612 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2613 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002614
Willy Tarreaud420a972015-04-06 00:39:18 +02002615 /* Now create a session, task and stream for this applet */
2616 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2617 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002619 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620 }
2621
Willy Tarreau87787ac2017-08-28 16:22:54 +02002622 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002623 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002624 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002625 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002626 }
2627
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002628 /* Initialise cross reference between stream and Lua socket object. */
2629 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002630
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631 /* Configure "right" stream interface. this "si" is used to connect
2632 * and retrieve data from the server. The connection is initialized
2633 * with the "struct server".
2634 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002635 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636
2637 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002638 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002639 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641 return 1;
2642
Willy Tarreaud420a972015-04-06 00:39:18 +02002643 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002644 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002645 out_fail_sess:
2646 appctx_free(appctx);
2647 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648 WILL_LJMP(lua_error(L));
2649 return 0;
2650}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002651
2652/*
2653 *
2654 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002655 * Class Channel
2656 *
2657 *
2658 */
2659
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002660/* This function is called before the Lua execution. It stores
2661 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002662 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002663static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002664{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002665 c->mode = stream->be->mode;
2666 switch (c->mode) {
2667 case PR_MODE_HTTP:
2668 c->data.http.dir = opt & SMP_OPT_DIR;
2669 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2670 c->data.http.state = stream->txn->req.msg_state;
2671 else
2672 c->data.http.state = stream->txn->rsp.msg_state;
2673 break;
2674 default:
2675 break;
2676 }
2677}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002678
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002679/* This function is called after the Lua execution. it
2680 * returns true if the parser state is consistent, otherwise,
2681 * it return false.
2682 *
2683 * In HTTP mode, the parser state must be in the same state
2684 * or greater when we exit the function. Even if we do a
2685 * control yield. This prevent to break the HTTP message
2686 * from the Lua code.
2687 */
2688static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2689{
2690 if (c->mode != stream->be->mode)
2691 return 0;
2692
2693 switch (c->mode) {
2694 case PR_MODE_HTTP:
2695 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002696 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002697 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2698 return stream->txn->req.msg_state >= c->data.http.state;
2699 else
2700 return stream->txn->rsp.msg_state >= c->data.http.state;
2701 default:
2702 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002703 }
2704 return 1;
2705}
2706
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002707/* Returns the struct hlua_channel join to the class channel in the
2708 * stack entry "ud" or throws an argument error.
2709 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002710__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002711{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002712 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002713}
2714
Willy Tarreau47860ed2015-03-10 14:07:50 +01002715/* Pushes the channel onto the top of the stack. If the stask does not have a
2716 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002717 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002718static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002719{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002720 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002721 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002722 return 0;
2723
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002724 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002725 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002726 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002727
2728 /* Pop a class sesison metatable and affect it to the userdata. */
2729 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2730 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002731 return 1;
2732}
2733
2734/* Duplicate all the data present in the input channel and put it
2735 * in a string LUA variables. Returns -1 and push a nil value in
2736 * the stack if the channel is closed and all the data are consumed,
2737 * returns 0 if no data are available, otherwise it returns the length
2738 * of the builded string.
2739 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002740static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741{
2742 char *blk1;
2743 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002744 size_t len1;
2745 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746 int ret;
2747 luaL_Buffer b;
2748
Willy Tarreau06d80a92017-10-19 14:32:15 +02002749 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 if (unlikely(ret == 0))
2751 return 0;
2752
2753 if (unlikely(ret < 0)) {
2754 lua_pushnil(L);
2755 return -1;
2756 }
2757
2758 luaL_buffinit(L, &b);
2759 luaL_addlstring(&b, blk1, len1);
2760 if (unlikely(ret == 2))
2761 luaL_addlstring(&b, blk2, len2);
2762 luaL_pushresult(&b);
2763
2764 if (unlikely(ret == 2))
2765 return len1 + len2;
2766 return len1;
2767}
2768
2769/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2770 * a yield. This function keep the data in the buffer.
2771 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002772__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002773{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002774 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002775
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002776 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2777
Christopher Faulet3f829a42018-12-13 21:56:45 +01002778 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2779 WILL_LJMP(lua_error(L));
2780
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002781 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002782 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783 return 1;
2784}
2785
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002786/* Check arguments for the function "hlua_channel_dup_yield". */
2787__LJMP static int hlua_channel_dup(lua_State *L)
2788{
2789 MAY_LJMP(check_args(L, 1, "dup"));
2790 MAY_LJMP(hlua_checkchannel(L, 1));
2791 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2792}
2793
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2795 * a yield. This function consumes the data in the buffer. It returns
2796 * a string containing the data or a nil pointer if no data are available
2797 * and the channel is closed.
2798 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002799__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002801 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002802 int ret;
2803
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002804 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002805
Christopher Faulet3f829a42018-12-13 21:56:45 +01002806 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2807 WILL_LJMP(lua_error(L));
2808
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002809 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002810 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002811 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002812
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002813 if (unlikely(ret == -1))
2814 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002815
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002816 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002817 return 1;
2818}
2819
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002820/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002821__LJMP static int hlua_channel_get(lua_State *L)
2822{
2823 MAY_LJMP(check_args(L, 1, "get"));
2824 MAY_LJMP(hlua_checkchannel(L, 1));
2825 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2826}
2827
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002828/* This functions consumes and returns one line. If the channel is closed,
2829 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002830 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002831 * value.
2832 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002833__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002835 char *blk1;
2836 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002837 size_t len1;
2838 size_t len2;
2839 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002840 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002841 int ret;
2842 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002843
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002844 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2845
Christopher Faulet3f829a42018-12-13 21:56:45 +01002846 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2847 WILL_LJMP(lua_error(L));
2848
Willy Tarreau06d80a92017-10-19 14:32:15 +02002849 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002850 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002851 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002852
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853 if (ret == -1) {
2854 lua_pushnil(L);
2855 return 1;
2856 }
2857
2858 luaL_buffinit(L, &b);
2859 luaL_addlstring(&b, blk1, len1);
2860 len = len1;
2861 if (unlikely(ret == 2)) {
2862 luaL_addlstring(&b, blk2, len2);
2863 len += len2;
2864 }
2865 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002866 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002867 return 1;
2868}
2869
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002870/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002871__LJMP static int hlua_channel_getline(lua_State *L)
2872{
2873 MAY_LJMP(check_args(L, 1, "getline"));
2874 MAY_LJMP(hlua_checkchannel(L, 1));
2875 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2876}
2877
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878/* This function takes a string as input, and append it at the
2879 * input side of channel. If the data is too big, but a space
2880 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002881 * yields. If the data is bigger than the buffer, or if the
2882 * channel is closed, it returns -1. Otherwise, it returns the
2883 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002884 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002885__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002886{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002887 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002888 size_t len;
2889 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2890 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2891 int ret;
2892 int max;
2893
Christopher Faulet3f829a42018-12-13 21:56:45 +01002894 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2895 WILL_LJMP(lua_error(L));
2896
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002897 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002898 * the request buffer if its not required.
2899 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002900 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002901 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002902 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002903 }
2904
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002905 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002906 if (max > len - l)
2907 max = len - l;
2908
Willy Tarreau06d80a92017-10-19 14:32:15 +02002909 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 if (ret == -2 || ret == -3) {
2911 lua_pushinteger(L, -1);
2912 return 1;
2913 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002914 if (ret == -1) {
2915 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002916 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002917 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 l += ret;
2919 lua_pop(L, 1);
2920 lua_pushinteger(L, l);
2921
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002922 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002923 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002924 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925 * in this case, we cannot add more data, so we cannot yield,
2926 * we return the amount of copyied data.
2927 */
2928 return 1;
2929 }
2930 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002931 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002932 return 1;
2933}
2934
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002935/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2936 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 * buffer size is too little for the data.
2938 */
2939__LJMP static int hlua_channel_append(lua_State *L)
2940{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002941 size_t len;
2942
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002943 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002944 MAY_LJMP(hlua_checkchannel(L, 1));
2945 MAY_LJMP(luaL_checklstring(L, 2, &len));
2946 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002947 lua_pushinteger(L, 0);
2948
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002949 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002950}
2951
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002952/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002953 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002954 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955 * or -1 if the channel is closed or if the buffer size is too
2956 * little for the data.
2957 */
2958__LJMP static int hlua_channel_set(lua_State *L)
2959{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002960 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002961
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002962 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002963 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 lua_pushinteger(L, 0);
2965
Christopher Faulet3f829a42018-12-13 21:56:45 +01002966 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2967 WILL_LJMP(lua_error(L));
2968
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002970
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002971 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002972}
2973
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002974/* Append data in the output side of the buffer. This data is immediately
2975 * sent. The function returns the amount of data written. If the buffer
2976 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977 * if the channel is closed.
2978 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002979__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002980{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002981 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002982 size_t len;
2983 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2984 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2985 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002986 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002987
Christopher Faulet3f829a42018-12-13 21:56:45 +01002988 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2989 WILL_LJMP(lua_error(L));
2990
Willy Tarreau47860ed2015-03-10 14:07:50 +01002991 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002992 lua_pushinteger(L, -1);
2993 return 1;
2994 }
2995
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002996 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002997 * the request buffer if its not required.
2998 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002999 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003000 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003001 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003002 }
3003
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003004 /* The written data will be immediately sent, so we can check
3005 * the available space without taking in account the reserve.
3006 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003007 * data, because the buffer will be flushed.
3008 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003009 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003010
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003011 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003012 * in this case, we cannot add more data, so we cannot yield,
3013 * we return the amount of copyied data.
3014 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003015 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003016 return 1;
3017
3018 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003019 if (max > len - l)
3020 max = len - l;
3021
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003022 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003023 * detects a non contiguous buffer and realign it.
3024 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003025 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003026 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003027
3028 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003029 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003030
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 /* buffer replace considers that the input part is filled.
3032 * so, I must forward these new data in the output part.
3033 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003034 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003035
3036 l += max;
3037 lua_pop(L, 1);
3038 lua_pushinteger(L, l);
3039
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003040 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003041 * in this case, we cannot add more data, so we cannot yield,
3042 * we return the amount of copyied data.
3043 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003044 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003045 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003046 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003047
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003048 if (l < len) {
3049 /* If we are waiting for space in the response buffer, we
3050 * must set the flag WAKERESWR. This flag required the task
3051 * wake up if any activity is detected on the response buffer.
3052 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003053 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003054 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003055 else
3056 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003057 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003058 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003059
3060 return 1;
3061}
3062
3063/* Just a wraper of "_hlua_channel_send". This wrapper permits
3064 * yield the LUA process, and resume it without checking the
3065 * input arguments.
3066 */
3067__LJMP static int hlua_channel_send(lua_State *L)
3068{
3069 MAY_LJMP(check_args(L, 2, "send"));
3070 lua_pushinteger(L, 0);
3071
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003072 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073}
3074
3075/* This function forward and amount of butes. The data pass from
3076 * the input side of the buffer to the output side, and can be
3077 * forwarded. This function never fails.
3078 *
3079 * The Lua function takes an amount of bytes to be forwarded in
3080 * imput. It returns the number of bytes forwarded.
3081 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003082__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003083{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003084 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003085 int len;
3086 int l;
3087 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003088 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003089
3090 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003091
3092 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3093 WILL_LJMP(lua_error(L));
3094
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003095 len = MAY_LJMP(luaL_checkinteger(L, 2));
3096 l = MAY_LJMP(luaL_checkinteger(L, -1));
3097
3098 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003099 if (max > ci_data(chn))
3100 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003101 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003102 l += max;
3103
3104 lua_pop(L, 1);
3105 lua_pushinteger(L, l);
3106
3107 /* Check if it miss bytes to forward. */
3108 if (l < len) {
3109 /* The the input channel or the output channel are closed, we
3110 * must return the amount of data forwarded.
3111 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003112 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003113 return 1;
3114
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003115 /* If we are waiting for space data in the response buffer, we
3116 * must set the flag WAKERESWR. This flag required the task
3117 * wake up if any activity is detected on the response buffer.
3118 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003119 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003120 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003121 else
3122 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003123
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003124 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003125 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003126 }
3127
3128 return 1;
3129}
3130
3131/* Just check the input and prepare the stack for the previous
3132 * function "hlua_channel_forward_yield"
3133 */
3134__LJMP static int hlua_channel_forward(lua_State *L)
3135{
3136 MAY_LJMP(check_args(L, 2, "forward"));
3137 MAY_LJMP(hlua_checkchannel(L, 1));
3138 MAY_LJMP(luaL_checkinteger(L, 2));
3139
3140 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003141 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003142}
3143
3144/* Just returns the number of bytes available in the input
3145 * side of the buffer. This function never fails.
3146 */
3147__LJMP static int hlua_channel_get_in_len(lua_State *L)
3148{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003149 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003150
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003151 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003152 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003153 if (IS_HTX_STRM(chn_strm(chn))) {
3154 struct htx *htx = htxbuf(&chn->buf);
3155 lua_pushinteger(L, htx->data - co_data(chn));
3156 }
3157 else
3158 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003159 return 1;
3160}
3161
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003162/* Returns true if the channel is full. */
3163__LJMP static int hlua_channel_is_full(lua_State *L)
3164{
3165 struct channel *chn;
3166 int rem;
3167
3168 MAY_LJMP(check_args(L, 1, "is_full"));
3169 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3170
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003171 if (IS_HTX_STRM(chn_strm(chn))) {
3172 struct htx *htx = htxbuf(&chn->buf);
3173
3174 rem = htx_free_data_space(htx);
3175 }
3176 else
3177 rem = b_room(&chn->buf);
3178
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003179 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3180
3181 lua_pushboolean(L, rem <= 0);
3182 return 1;
3183}
3184
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003185/* Just returns the number of bytes available in the output
3186 * side of the buffer. This function never fails.
3187 */
3188__LJMP static int hlua_channel_get_out_len(lua_State *L)
3189{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003190 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003191
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003192 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003193 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003194 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003195 return 1;
3196}
3197
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003198/*
3199 *
3200 *
3201 * Class Fetches
3202 *
3203 *
3204 */
3205
3206/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003207 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003208 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003209__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003210{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003211 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003212}
3213
3214/* This function creates and push in the stack a fetch object according
3215 * with a current TXN.
3216 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003217static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003218{
Willy Tarreau7073c472015-04-06 11:15:40 +02003219 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003220
3221 /* Check stack size. */
3222 if (!lua_checkstack(L, 3))
3223 return 0;
3224
3225 /* Create the object: obj[0] = userdata.
3226 * Note that the base of the Fetches object is the
3227 * transaction object.
3228 */
3229 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003230 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003231 lua_rawseti(L, -2, 0);
3232
Willy Tarreau7073c472015-04-06 11:15:40 +02003233 hsmp->s = txn->s;
3234 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003235 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003236 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003237
3238 /* Pop a class sesison metatable and affect it to the userdata. */
3239 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3240 lua_setmetatable(L, -2);
3241
3242 return 1;
3243}
3244
3245/* This function is an LUA binding. It is called with each sample-fetch.
3246 * It uses closure argument to store the associated sample-fetch. It
3247 * returns only one argument or throws an error. An error is thrown
3248 * only if an error is encountered during the argument parsing. If
3249 * the "sample-fetch" function fails, nil is returned.
3250 */
3251__LJMP static int hlua_run_sample_fetch(lua_State *L)
3252{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003253 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003254 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003255 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003256 int i;
3257 struct sample smp;
3258
3259 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003260 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003261
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003262 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003263 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003264
Thierry FOURNIERca988662015-12-20 18:43:03 +01003265 /* Check execution authorization. */
3266 if (f->use & SMP_USE_HTTP_ANY &&
3267 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3268 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3269 "is not available in Lua services", f->kw);
3270 WILL_LJMP(lua_error(L));
3271 }
3272
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003273 /* Get extra arguments. */
3274 for (i = 0; i < lua_gettop(L) - 1; i++) {
3275 if (i >= ARGM_NBARGS)
3276 break;
3277 hlua_lua2arg(L, i + 2, &args[i]);
3278 }
3279 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003280 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003281
3282 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003283 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003284
3285 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003286 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003287 lua_pushfstring(L, "error in arguments");
3288 WILL_LJMP(lua_error(L));
3289 }
3290
3291 /* Initialise the sample. */
3292 memset(&smp, 0, sizeof(smp));
3293
3294 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003295 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003296 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003297 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003298 lua_pushstring(L, "");
3299 else
3300 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003301 return 1;
3302 }
3303
3304 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003305 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003306 hlua_smp2lua_str(L, &smp);
3307 else
3308 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003309 return 1;
3310}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003311
3312/*
3313 *
3314 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003315 * Class Converters
3316 *
3317 *
3318 */
3319
3320/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003321 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003322 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003323__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003324{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003325 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003326}
3327
3328/* This function creates and push in the stack a Converters object
3329 * according with a current TXN.
3330 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003331static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003332{
Willy Tarreau7073c472015-04-06 11:15:40 +02003333 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003334
3335 /* Check stack size. */
3336 if (!lua_checkstack(L, 3))
3337 return 0;
3338
3339 /* Create the object: obj[0] = userdata.
3340 * Note that the base of the Converters object is the
3341 * same than the TXN object.
3342 */
3343 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003344 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003345 lua_rawseti(L, -2, 0);
3346
Willy Tarreau7073c472015-04-06 11:15:40 +02003347 hsmp->s = txn->s;
3348 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003349 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003350 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003351
Willy Tarreau87b09662015-04-03 00:22:06 +02003352 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003353 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3354 lua_setmetatable(L, -2);
3355
3356 return 1;
3357}
3358
3359/* This function is an LUA binding. It is called with each converter.
3360 * It uses closure argument to store the associated converter. It
3361 * returns only one argument or throws an error. An error is thrown
3362 * only if an error is encountered during the argument parsing. If
3363 * the converter function function fails, nil is returned.
3364 */
3365__LJMP static int hlua_run_sample_conv(lua_State *L)
3366{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003367 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003368 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003369 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003370 int i;
3371 struct sample smp;
3372
3373 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003374 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003376 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003377 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003378
3379 /* Get extra arguments. */
3380 for (i = 0; i < lua_gettop(L) - 2; i++) {
3381 if (i >= ARGM_NBARGS)
3382 break;
3383 hlua_lua2arg(L, i + 3, &args[i]);
3384 }
3385 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003386 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003387
3388 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003389 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003390
3391 /* Run the special args checker. */
3392 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3393 hlua_pusherror(L, "error in arguments");
3394 WILL_LJMP(lua_error(L));
3395 }
3396
3397 /* Initialise the sample. */
3398 if (!hlua_lua2smp(L, 2, &smp)) {
3399 hlua_pusherror(L, "error in the input argument");
3400 WILL_LJMP(lua_error(L));
3401 }
3402
Willy Tarreau1777ea62016-03-10 16:15:46 +01003403 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3404
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003405 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003406 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003407 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003408 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003409 WILL_LJMP(lua_error(L));
3410 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003411 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3412 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003413 hlua_pusherror(L, "error during the input argument casting");
3414 WILL_LJMP(lua_error(L));
3415 }
3416
3417 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003418 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003419 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003420 lua_pushstring(L, "");
3421 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003422 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003423 return 1;
3424 }
3425
3426 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003427 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003428 hlua_smp2lua_str(L, &smp);
3429 else
3430 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003431 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003432}
3433
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003434/*
3435 *
3436 *
3437 * Class AppletTCP
3438 *
3439 *
3440 */
3441
3442/* Returns a struct hlua_txn if the stack entry "ud" is
3443 * a class stream, otherwise it throws an error.
3444 */
3445__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3446{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003447 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003448}
3449
3450/* This function creates and push in the stack an Applet object
3451 * according with a current TXN.
3452 */
3453static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3454{
3455 struct hlua_appctx *appctx;
3456 struct stream_interface *si = ctx->owner;
3457 struct stream *s = si_strm(si);
3458 struct proxy *p = s->be;
3459
3460 /* Check stack size. */
3461 if (!lua_checkstack(L, 3))
3462 return 0;
3463
3464 /* Create the object: obj[0] = userdata.
3465 * Note that the base of the Converters object is the
3466 * same than the TXN object.
3467 */
3468 lua_newtable(L);
3469 appctx = lua_newuserdata(L, sizeof(*appctx));
3470 lua_rawseti(L, -2, 0);
3471 appctx->appctx = ctx;
3472 appctx->htxn.s = s;
3473 appctx->htxn.p = p;
3474
3475 /* Create the "f" field that contains a list of fetches. */
3476 lua_pushstring(L, "f");
3477 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3478 return 0;
3479 lua_settable(L, -3);
3480
3481 /* Create the "sf" field that contains a list of stringsafe fetches. */
3482 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003483 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003484 return 0;
3485 lua_settable(L, -3);
3486
3487 /* Create the "c" field that contains a list of converters. */
3488 lua_pushstring(L, "c");
3489 if (!hlua_converters_new(L, &appctx->htxn, 0))
3490 return 0;
3491 lua_settable(L, -3);
3492
3493 /* Create the "sc" field that contains a list of stringsafe converters. */
3494 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003495 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003496 return 0;
3497 lua_settable(L, -3);
3498
3499 /* Pop a class stream metatable and affect it to the table. */
3500 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3501 lua_setmetatable(L, -2);
3502
3503 return 1;
3504}
3505
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003506__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3507{
3508 struct hlua_appctx *appctx;
3509 struct stream *s;
3510 const char *name;
3511 size_t len;
3512 struct sample smp;
3513
3514 MAY_LJMP(check_args(L, 3, "set_var"));
3515
3516 /* It is useles to retrieve the stream, but this function
3517 * runs only in a stream context.
3518 */
3519 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3520 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3521 s = appctx->htxn.s;
3522
3523 /* Converts the third argument in a sample. */
3524 hlua_lua2smp(L, 3, &smp);
3525
3526 /* Store the sample in a variable. */
3527 smp_set_owner(&smp, s->be, s->sess, s, 0);
3528 vars_set_by_name(name, len, &smp);
3529 return 0;
3530}
3531
3532__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3533{
3534 struct hlua_appctx *appctx;
3535 struct stream *s;
3536 const char *name;
3537 size_t len;
3538 struct sample smp;
3539
3540 MAY_LJMP(check_args(L, 2, "unset_var"));
3541
3542 /* It is useles to retrieve the stream, but this function
3543 * runs only in a stream context.
3544 */
3545 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3546 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3547 s = appctx->htxn.s;
3548
3549 /* Unset the variable. */
3550 smp_set_owner(&smp, s->be, s->sess, s, 0);
3551 vars_unset_by_name(name, len, &smp);
3552 return 0;
3553}
3554
3555__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3556{
3557 struct hlua_appctx *appctx;
3558 struct stream *s;
3559 const char *name;
3560 size_t len;
3561 struct sample smp;
3562
3563 MAY_LJMP(check_args(L, 2, "get_var"));
3564
3565 /* It is useles to retrieve the stream, but this function
3566 * runs only in a stream context.
3567 */
3568 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3569 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3570 s = appctx->htxn.s;
3571
3572 smp_set_owner(&smp, s->be, s->sess, s, 0);
3573 if (!vars_get_by_name(name, len, &smp)) {
3574 lua_pushnil(L);
3575 return 1;
3576 }
3577
3578 return hlua_smp2lua(L, &smp);
3579}
3580
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003581__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3582{
3583 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3584 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003585 struct hlua *hlua;
3586
3587 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003588 if (!s->hlua)
3589 return 0;
3590 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003591
3592 MAY_LJMP(check_args(L, 2, "set_priv"));
3593
3594 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003595 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003596
3597 /* Get and store new value. */
3598 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3599 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3600
3601 return 0;
3602}
3603
3604__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3605{
3606 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3607 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003608 struct hlua *hlua;
3609
3610 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003611 if (!s->hlua) {
3612 lua_pushnil(L);
3613 return 1;
3614 }
3615 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003616
3617 /* Push configuration index in the stack. */
3618 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3619
3620 return 1;
3621}
3622
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003623/* If expected data not yet available, it returns a yield. This function
3624 * consumes the data in the buffer. It returns a string containing the
3625 * data. This string can be empty.
3626 */
3627__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3628{
3629 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3630 struct stream_interface *si = appctx->appctx->owner;
3631 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003632 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003633 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003634 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003635 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003636
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003637 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003638 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003639
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003640 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003641 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003642 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003643 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003644 }
3645
3646 /* End of data: commit the total strings and return. */
3647 if (ret < 0) {
3648 luaL_pushresult(&appctx->b);
3649 return 1;
3650 }
3651
3652 /* Ensure that the block 2 length is usable. */
3653 if (ret == 1)
3654 len2 = 0;
3655
3656 /* dont check the max length read and dont check. */
3657 luaL_addlstring(&appctx->b, blk1, len1);
3658 luaL_addlstring(&appctx->b, blk2, len2);
3659
3660 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003661 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003662 luaL_pushresult(&appctx->b);
3663 return 1;
3664}
3665
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003666/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003667__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3668{
3669 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3670
3671 /* Initialise the string catenation. */
3672 luaL_buffinit(L, &appctx->b);
3673
3674 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3675}
3676
3677/* If expected data not yet available, it returns a yield. This function
3678 * consumes the data in the buffer. It returns a string containing the
3679 * data. This string can be empty.
3680 */
3681__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3682{
3683 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3684 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003685 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003686 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003687 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003688 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003689 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003690 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003691
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003692 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003693 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003694
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003695 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003696 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003697 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003698 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003699 }
3700
3701 /* End of data: commit the total strings and return. */
3702 if (ret < 0) {
3703 luaL_pushresult(&appctx->b);
3704 return 1;
3705 }
3706
3707 /* Ensure that the block 2 length is usable. */
3708 if (ret == 1)
3709 len2 = 0;
3710
3711 if (len == -1) {
3712
3713 /* If len == -1, catenate all the data avalaile and
3714 * yield because we want to get all the data until
3715 * the end of data stream.
3716 */
3717 luaL_addlstring(&appctx->b, blk1, len1);
3718 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003719 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003720 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003721 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003722
3723 } else {
3724
3725 /* Copy the fisrt block caping to the length required. */
3726 if (len1 > len)
3727 len1 = len;
3728 luaL_addlstring(&appctx->b, blk1, len1);
3729 len -= len1;
3730
3731 /* Copy the second block. */
3732 if (len2 > len)
3733 len2 = len;
3734 luaL_addlstring(&appctx->b, blk2, len2);
3735 len -= len2;
3736
3737 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003738 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003739
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003740 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003741 if (len > 0) {
3742 lua_pushinteger(L, len);
3743 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003744 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003745 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003746 }
3747
3748 /* return the result. */
3749 luaL_pushresult(&appctx->b);
3750 return 1;
3751 }
3752
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003753 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003754 hlua_pusherror(L, "Lua: internal error");
3755 WILL_LJMP(lua_error(L));
3756 return 0;
3757}
3758
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003759/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003760__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3761{
3762 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3763 int len = -1;
3764
3765 if (lua_gettop(L) > 2)
3766 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3767 if (lua_gettop(L) >= 2) {
3768 len = MAY_LJMP(luaL_checkinteger(L, 2));
3769 lua_pop(L, 1);
3770 }
3771
3772 /* Confirm or set the required length */
3773 lua_pushinteger(L, len);
3774
3775 /* Initialise the string catenation. */
3776 luaL_buffinit(L, &appctx->b);
3777
3778 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3779}
3780
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003781/* Append data in the output side of the buffer. This data is immediately
3782 * sent. The function returns the amount of data written. If the buffer
3783 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003784 * if the channel is closed.
3785 */
3786__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3787{
3788 size_t len;
3789 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3790 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3791 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3792 struct stream_interface *si = appctx->appctx->owner;
3793 struct channel *chn = si_ic(si);
3794 int max;
3795
3796 /* Get the max amount of data which can write as input in the channel. */
3797 max = channel_recv_max(chn);
3798 if (max > (len - l))
3799 max = len - l;
3800
3801 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003802 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003803
3804 /* update counters. */
3805 l += max;
3806 lua_pop(L, 1);
3807 lua_pushinteger(L, l);
3808
3809 /* If some data is not send, declares the situation to the
3810 * applet, and returns a yield.
3811 */
3812 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003813 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003814 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003815 }
3816
3817 return 1;
3818}
3819
3820/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3821 * yield the LUA process, and resume it without checking the
3822 * input arguments.
3823 */
3824__LJMP static int hlua_applet_tcp_send(lua_State *L)
3825{
3826 MAY_LJMP(check_args(L, 2, "send"));
3827 lua_pushinteger(L, 0);
3828
3829 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3830}
3831
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003832/*
3833 *
3834 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003835 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003836 *
3837 *
3838 */
3839
3840/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003841 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003842 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003843__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003844{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003845 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003846}
3847
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003848/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003849 * according with a current TXN.
3850 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003851static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003852{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003853 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003854 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003855 struct stream_interface *si = ctx->owner;
3856 struct stream *s = si_strm(si);
3857 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003858 struct htx *htx;
3859 struct htx_blk *blk;
3860 struct htx_sl *sl;
3861 struct ist path;
3862 unsigned long long len = 0;
3863 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003864
3865 /* Check stack size. */
3866 if (!lua_checkstack(L, 3))
3867 return 0;
3868
3869 /* Create the object: obj[0] = userdata.
3870 * Note that the base of the Converters object is the
3871 * same than the TXN object.
3872 */
3873 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003874 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003875 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 appctx->appctx = ctx;
3877 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003878 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 appctx->htxn.s = s;
3880 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003881
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003882 /* Create the "f" field that contains a list of fetches. */
3883 lua_pushstring(L, "f");
3884 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3885 return 0;
3886 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003887
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003888 /* Create the "sf" field that contains a list of stringsafe fetches. */
3889 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003890 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003891 return 0;
3892 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003893
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003894 /* Create the "c" field that contains a list of converters. */
3895 lua_pushstring(L, "c");
3896 if (!hlua_converters_new(L, &appctx->htxn, 0))
3897 return 0;
3898 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003899
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003900 /* Create the "sc" field that contains a list of stringsafe converters. */
3901 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003902 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003903 return 0;
3904 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003905
Christopher Fauleta2097962019-07-15 16:25:33 +02003906 htx = htxbuf(&s->req.buf);
3907 blk = htx_get_first_blk(htx);
3908 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
3909 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003910
Christopher Fauleta2097962019-07-15 16:25:33 +02003911 /* Stores the request method. */
3912 lua_pushstring(L, "method");
3913 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3914 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003915
Christopher Fauleta2097962019-07-15 16:25:33 +02003916 /* Stores the http version. */
3917 lua_pushstring(L, "version");
3918 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3919 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003920
Christopher Fauleta2097962019-07-15 16:25:33 +02003921 /* creates an array of headers. hlua_http_get_headers() crates and push
3922 * the array on the top of the stack.
3923 */
3924 lua_pushstring(L, "headers");
3925 htxn.s = s;
3926 htxn.p = px;
3927 htxn.dir = SMP_OPT_DIR_REQ;
3928 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3929 return 0;
3930 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003931
Christopher Fauleta2097962019-07-15 16:25:33 +02003932 path = http_get_path(htx_sl_req_uri(sl));
3933 if (path.ptr) {
3934 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003935
Christopher Fauleta2097962019-07-15 16:25:33 +02003936 p = path.ptr;
3937 end = path.ptr + path.len;
3938 q = p;
3939 while (q < end && *q != '?')
3940 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003941
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003942 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003943 lua_pushstring(L, "path");
3944 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003945 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003946
Christopher Fauleta2097962019-07-15 16:25:33 +02003947 /* Stores the query string. */
3948 lua_pushstring(L, "qs");
3949 if (*q == '?')
3950 q++;
3951 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003952 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003953 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003954
Christopher Fauleta2097962019-07-15 16:25:33 +02003955 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3956 struct htx_blk *blk = htx_get_blk(htx, pos);
3957 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003958
Christopher Fauleta2097962019-07-15 16:25:33 +02003959 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3960 break;
3961 if (type == HTX_BLK_DATA)
3962 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003963 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003964 if (htx->extra != ULLONG_MAX)
3965 len += htx->extra;
3966
3967 /* Stores the request path. */
3968 lua_pushstring(L, "length");
3969 lua_pushinteger(L, len);
3970 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003971
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003972 /* Create an empty array of HTTP request headers. */
3973 lua_pushstring(L, "response");
3974 lua_newtable(L);
3975 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003976
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003977 /* Pop a class stream metatable and affect it to the table. */
3978 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3979 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003980
3981 return 1;
3982}
3983
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003984__LJMP static int hlua_applet_http_set_var(lua_State *L)
3985{
3986 struct hlua_appctx *appctx;
3987 struct stream *s;
3988 const char *name;
3989 size_t len;
3990 struct sample smp;
3991
3992 MAY_LJMP(check_args(L, 3, "set_var"));
3993
3994 /* It is useles to retrieve the stream, but this function
3995 * runs only in a stream context.
3996 */
3997 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3998 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3999 s = appctx->htxn.s;
4000
4001 /* Converts the third argument in a sample. */
4002 hlua_lua2smp(L, 3, &smp);
4003
4004 /* Store the sample in a variable. */
4005 smp_set_owner(&smp, s->be, s->sess, s, 0);
4006 vars_set_by_name(name, len, &smp);
4007 return 0;
4008}
4009
4010__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4011{
4012 struct hlua_appctx *appctx;
4013 struct stream *s;
4014 const char *name;
4015 size_t len;
4016 struct sample smp;
4017
4018 MAY_LJMP(check_args(L, 2, "unset_var"));
4019
4020 /* It is useles to retrieve the stream, but this function
4021 * runs only in a stream context.
4022 */
4023 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4024 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4025 s = appctx->htxn.s;
4026
4027 /* Unset the variable. */
4028 smp_set_owner(&smp, s->be, s->sess, s, 0);
4029 vars_unset_by_name(name, len, &smp);
4030 return 0;
4031}
4032
4033__LJMP static int hlua_applet_http_get_var(lua_State *L)
4034{
4035 struct hlua_appctx *appctx;
4036 struct stream *s;
4037 const char *name;
4038 size_t len;
4039 struct sample smp;
4040
4041 MAY_LJMP(check_args(L, 2, "get_var"));
4042
4043 /* It is useles to retrieve the stream, but this function
4044 * runs only in a stream context.
4045 */
4046 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4047 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4048 s = appctx->htxn.s;
4049
4050 smp_set_owner(&smp, s->be, s->sess, s, 0);
4051 if (!vars_get_by_name(name, len, &smp)) {
4052 lua_pushnil(L);
4053 return 1;
4054 }
4055
4056 return hlua_smp2lua(L, &smp);
4057}
4058
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004059__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4060{
4061 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4062 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004063 struct hlua *hlua;
4064
4065 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004066 if (!s->hlua)
4067 return 0;
4068 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004069
4070 MAY_LJMP(check_args(L, 2, "set_priv"));
4071
4072 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004073 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004074
4075 /* Get and store new value. */
4076 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4077 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4078
4079 return 0;
4080}
4081
4082__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4083{
4084 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4085 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004086 struct hlua *hlua;
4087
4088 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004089 if (!s->hlua) {
4090 lua_pushnil(L);
4091 return 1;
4092 }
4093 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004094
4095 /* Push configuration index in the stack. */
4096 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4097
4098 return 1;
4099}
4100
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004101/* If expected data not yet available, it returns a yield. This function
4102 * consumes the data in the buffer. It returns a string containing the
4103 * data. This string can be empty.
4104 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004105__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004106{
4107 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4108 struct stream_interface *si = appctx->appctx->owner;
4109 struct channel *req = si_oc(si);
4110 struct htx *htx;
4111 struct htx_blk *blk;
4112 size_t count;
4113 int stop = 0;
4114
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004115 htx = htx_from_buf(&req->buf);
4116 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004117 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004118
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004119 while (count && !stop && blk) {
4120 enum htx_blk_type type = htx_get_blk_type(blk);
4121 uint32_t sz = htx_get_blksz(blk);
4122 struct ist v;
4123 uint32_t vlen;
4124 char *nl;
4125
Christopher Faulete461e342018-12-18 16:43:35 +01004126 if (type == HTX_BLK_EOM) {
4127 stop = 1;
4128 break;
4129 }
4130
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004131 vlen = sz;
4132 if (vlen > count) {
4133 if (type != HTX_BLK_DATA)
4134 break;
4135 vlen = count;
4136 }
4137
4138 switch (type) {
4139 case HTX_BLK_UNUSED:
4140 break;
4141
4142 case HTX_BLK_DATA:
4143 v = htx_get_blk_value(htx, blk);
4144 v.len = vlen;
4145 nl = istchr(v, '\n');
4146 if (nl != NULL) {
4147 stop = 1;
4148 vlen = nl - v.ptr + 1;
4149 }
4150 luaL_addlstring(&appctx->b, v.ptr, vlen);
4151 break;
4152
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004153 case HTX_BLK_TLR:
4154 case HTX_BLK_EOM:
4155 stop = 1;
4156 break;
4157
4158 default:
4159 break;
4160 }
4161
4162 co_set_data(req, co_data(req) - vlen);
4163 count -= vlen;
4164 if (sz == vlen)
4165 blk = htx_remove_blk(htx, blk);
4166 else {
4167 htx_cut_data_blk(htx, blk, vlen);
4168 break;
4169 }
4170 }
4171
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004172 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004173 if (!stop) {
4174 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004175 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004176 }
4177
4178 /* return the result. */
4179 luaL_pushresult(&appctx->b);
4180 return 1;
4181}
4182
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004183
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004184/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004185__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004186{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004187 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004188
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004189 /* Initialise the string catenation. */
4190 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004191
Christopher Fauleta2097962019-07-15 16:25:33 +02004192 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004193}
4194
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004195/* If expected data not yet available, it returns a yield. This function
4196 * consumes the data in the buffer. It returns a string containing the
4197 * data. This string can be empty.
4198 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004199__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004200{
4201 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4202 struct stream_interface *si = appctx->appctx->owner;
4203 struct channel *req = si_oc(si);
4204 struct htx *htx;
4205 struct htx_blk *blk;
4206 size_t count;
4207 int len;
4208
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004209 htx = htx_from_buf(&req->buf);
4210 len = MAY_LJMP(luaL_checkinteger(L, 2));
4211 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004212 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004213 while (count && len && blk) {
4214 enum htx_blk_type type = htx_get_blk_type(blk);
4215 uint32_t sz = htx_get_blksz(blk);
4216 struct ist v;
4217 uint32_t vlen;
4218
Christopher Faulete461e342018-12-18 16:43:35 +01004219 if (type == HTX_BLK_EOM) {
4220 len = 0;
4221 break;
4222 }
4223
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004224 vlen = sz;
4225 if (len > 0 && vlen > len)
4226 vlen = len;
4227 if (vlen > count) {
4228 if (type != HTX_BLK_DATA)
4229 break;
4230 vlen = count;
4231 }
4232
4233 switch (type) {
4234 case HTX_BLK_UNUSED:
4235 break;
4236
4237 case HTX_BLK_DATA:
4238 v = htx_get_blk_value(htx, blk);
4239 luaL_addlstring(&appctx->b, v.ptr, vlen);
4240 break;
4241
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004242 case HTX_BLK_TLR:
4243 case HTX_BLK_EOM:
4244 len = 0;
4245 break;
4246
4247 default:
4248 break;
4249 }
4250
4251 co_set_data(req, co_data(req) - vlen);
4252 count -= vlen;
4253 if (len > 0)
4254 len -= vlen;
4255 if (sz == vlen)
4256 blk = htx_remove_blk(htx, blk);
4257 else {
4258 htx_cut_data_blk(htx, blk, vlen);
4259 break;
4260 }
4261 }
4262
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004263 htx_to_buf(htx, &req->buf);
4264
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004265 /* If we are no other data available, yield waiting for new data. */
4266 if (len) {
4267 if (len > 0) {
4268 lua_pushinteger(L, len);
4269 lua_replace(L, 2);
4270 }
4271 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004272 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004273 }
4274
4275 /* return the result. */
4276 luaL_pushresult(&appctx->b);
4277 return 1;
4278}
4279
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004280/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004281__LJMP static int hlua_applet_http_recv(lua_State *L)
4282{
4283 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4284 int len = -1;
4285
4286 /* Check arguments. */
4287 if (lua_gettop(L) > 2)
4288 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4289 if (lua_gettop(L) >= 2) {
4290 len = MAY_LJMP(luaL_checkinteger(L, 2));
4291 lua_pop(L, 1);
4292 }
4293
Christopher Fauleta2097962019-07-15 16:25:33 +02004294 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004295
Christopher Fauleta2097962019-07-15 16:25:33 +02004296 /* Initialise the string catenation. */
4297 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004298
Christopher Fauleta2097962019-07-15 16:25:33 +02004299 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004300}
4301
4302/* Append data in the output side of the buffer. This data is immediately
4303 * sent. The function returns the amount of data written. If the buffer
4304 * cannot contain the data, the function yields. The function returns -1
4305 * if the channel is closed.
4306 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004307__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004308{
4309 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4310 struct stream_interface *si = appctx->appctx->owner;
4311 struct channel *res = si_ic(si);
4312 struct htx *htx = htx_from_buf(&res->buf);
4313 const char *data;
4314 size_t len;
4315 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4316 int max;
4317
Christopher Faulet9060fc02019-07-03 11:39:30 +02004318 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004319 if (!max)
4320 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004321
4322 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4323
4324 /* Get the max amount of data which can write as input in the channel. */
4325 if (max > (len - l))
4326 max = len - l;
4327
4328 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004329 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004330 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004331
4332 /* update counters. */
4333 l += max;
4334 lua_pop(L, 1);
4335 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004336
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004337 /* If some data is not send, declares the situation to the
4338 * applet, and returns a yield.
4339 */
4340 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004341 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004342 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004343 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004344 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004345 }
4346
Christopher Fauleta2097962019-07-15 16:25:33 +02004347 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004348 return 1;
4349}
4350
4351/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4352 * yield the LUA process, and resume it without checking the
4353 * input arguments.
4354 */
4355__LJMP static int hlua_applet_http_send(lua_State *L)
4356{
4357 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004358
4359 /* We want to send some data. Headers must be sent. */
4360 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4361 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4362 WILL_LJMP(lua_error(L));
4363 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004364
Christopher Fauleta2097962019-07-15 16:25:33 +02004365 /* This interger is used for followinf the amount of data sent. */
4366 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004367
Christopher Fauleta2097962019-07-15 16:25:33 +02004368 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004369}
4370
4371__LJMP static int hlua_applet_http_addheader(lua_State *L)
4372{
4373 const char *name;
4374 int ret;
4375
4376 MAY_LJMP(hlua_checkapplet_http(L, 1));
4377 name = MAY_LJMP(luaL_checkstring(L, 2));
4378 MAY_LJMP(luaL_checkstring(L, 3));
4379
4380 /* Push in the stack the "response" entry. */
4381 ret = lua_getfield(L, 1, "response");
4382 if (ret != LUA_TTABLE) {
4383 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4384 "is expected as an array. %s found", lua_typename(L, ret));
4385 WILL_LJMP(lua_error(L));
4386 }
4387
4388 /* check if the header is already registered if it is not
4389 * the case, register it.
4390 */
4391 ret = lua_getfield(L, -1, name);
4392 if (ret == LUA_TNIL) {
4393
4394 /* Entry not found. */
4395 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4396
4397 /* Insert the new header name in the array in the top of the stack.
4398 * It left the new array in the top of the stack.
4399 */
4400 lua_newtable(L);
4401 lua_pushvalue(L, 2);
4402 lua_pushvalue(L, -2);
4403 lua_settable(L, -4);
4404
4405 } else if (ret != LUA_TTABLE) {
4406
4407 /* corruption error. */
4408 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4409 "is expected as an array. %s found", name, lua_typename(L, ret));
4410 WILL_LJMP(lua_error(L));
4411 }
4412
4413 /* Now the top od thestack is an array of values. We push
4414 * the header value as new entry.
4415 */
4416 lua_pushvalue(L, 3);
4417 ret = lua_rawlen(L, -2);
4418 lua_rawseti(L, -2, ret + 1);
4419 lua_pushboolean(L, 1);
4420 return 1;
4421}
4422
4423__LJMP static int hlua_applet_http_status(lua_State *L)
4424{
4425 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4426 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004427 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004428
4429 if (status < 100 || status > 599) {
4430 lua_pushboolean(L, 0);
4431 return 1;
4432 }
4433
4434 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004435 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004436 lua_pushboolean(L, 1);
4437 return 1;
4438}
4439
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004440
Christopher Fauleta2097962019-07-15 16:25:33 +02004441__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004442{
4443 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4444 struct stream_interface *si = appctx->appctx->owner;
4445 struct channel *res = si_ic(si);
4446 struct htx *htx;
4447 struct htx_sl *sl;
4448 struct h1m h1m;
4449 const char *status, *reason;
4450 const char *name, *value;
4451 size_t nlen, vlen;
4452 unsigned int flags;
4453
4454 /* Send the message at once. */
4455 htx = htx_from_buf(&res->buf);
4456 h1m_init_res(&h1m);
4457
4458 /* Use the same http version than the request. */
4459 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4460 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4461 if (reason == NULL)
4462 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4463 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4464 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4465 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4466 }
4467 else {
4468 flags = HTX_SL_F_IS_RESP;
4469 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4470 }
4471 if (!sl) {
4472 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4473 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4474 WILL_LJMP(lua_error(L));
4475 }
4476 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4477
4478 /* Get the array associated to the field "response" in the object AppletHTTP. */
4479 lua_pushvalue(L, 0);
4480 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4481 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4482 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4483 WILL_LJMP(lua_error(L));
4484 }
4485
4486 /* Browse the list of headers. */
4487 lua_pushnil(L);
4488 while(lua_next(L, -2) != 0) {
4489 /* We expect a string as -2. */
4490 if (lua_type(L, -2) != LUA_TSTRING) {
4491 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4492 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4493 lua_typename(L, lua_type(L, -2)));
4494 WILL_LJMP(lua_error(L));
4495 }
4496 name = lua_tolstring(L, -2, &nlen);
4497
4498 /* We expect an array as -1. */
4499 if (lua_type(L, -1) != LUA_TTABLE) {
4500 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4501 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4502 name,
4503 lua_typename(L, lua_type(L, -1)));
4504 WILL_LJMP(lua_error(L));
4505 }
4506
4507 /* Browse the table who is on the top of the stack. */
4508 lua_pushnil(L);
4509 while(lua_next(L, -2) != 0) {
4510 int id;
4511
4512 /* We expect a number as -2. */
4513 if (lua_type(L, -2) != LUA_TNUMBER) {
4514 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4515 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4516 name,
4517 lua_typename(L, lua_type(L, -2)));
4518 WILL_LJMP(lua_error(L));
4519 }
4520 id = lua_tointeger(L, -2);
4521
4522 /* We expect a string as -2. */
4523 if (lua_type(L, -1) != LUA_TSTRING) {
4524 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4525 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4526 name, id,
4527 lua_typename(L, lua_type(L, -1)));
4528 WILL_LJMP(lua_error(L));
4529 }
4530 value = lua_tolstring(L, -1, &vlen);
4531
4532 /* Simple Protocol checks. */
4533 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4534 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4535 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4536 struct ist v = ist2(value, vlen);
4537 int ret;
4538
4539 ret = h1_parse_cont_len_header(&h1m, &v);
4540 if (ret < 0) {
4541 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4542 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4543 name);
4544 WILL_LJMP(lua_error(L));
4545 }
4546 else if (ret == 0)
4547 goto next; /* Skip it */
4548 }
4549
4550 /* Add a new header */
4551 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4552 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4553 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4554 name);
4555 WILL_LJMP(lua_error(L));
4556 }
4557 next:
4558 /* Remove the array from the stack, and get next element with a remaining string. */
4559 lua_pop(L, 1);
4560 }
4561
4562 /* Remove the array from the stack, and get next element with a remaining string. */
4563 lua_pop(L, 1);
4564 }
4565
4566 if (h1m.flags & H1_MF_CHNK)
4567 h1m.flags &= ~H1_MF_CLEN;
4568 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4569 h1m.flags |= H1_MF_XFER_LEN;
4570
4571 /* Uset HTX start-line flags */
4572 if (h1m.flags & H1_MF_XFER_ENC)
4573 flags |= HTX_SL_F_XFER_ENC;
4574 if (h1m.flags & H1_MF_XFER_LEN) {
4575 flags |= HTX_SL_F_XFER_LEN;
4576 if (h1m.flags & H1_MF_CHNK)
4577 flags |= HTX_SL_F_CHNK;
4578 else if (h1m.flags & H1_MF_CLEN)
4579 flags |= HTX_SL_F_CLEN;
4580 if (h1m.body_len == 0)
4581 flags |= HTX_SL_F_BODYLESS;
4582 }
4583 sl->flags |= flags;
4584
4585 /* If we dont have a content-length set, and the HTTP version is 1.1
4586 * and the status code implies the presence of a message body, we must
4587 * announce a transfer encoding chunked. This is required by haproxy
4588 * for the keepalive compliance. If the applet annouces a transfer-encoding
4589 * chunked itslef, don't do anything.
4590 */
4591 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4592 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4593 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4594 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4595 /* Add a new header */
4596 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4597 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4598 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4599 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4600 WILL_LJMP(lua_error(L));
4601 }
4602 }
4603
4604 /* Finalize headers. */
4605 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4606 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4607 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4608 WILL_LJMP(lua_error(L));
4609 }
4610
4611 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4612 b_reset(&res->buf);
4613 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4614 WILL_LJMP(lua_error(L));
4615 }
4616
4617 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004618 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004619
4620 /* Headers sent, set the flag. */
4621 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4622 return 0;
4623
4624}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004625/* We will build the status line and the headers of the HTTP response.
4626 * We will try send at once if its not possible, we give back the hand
4627 * waiting for more room.
4628 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004629__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004630{
4631 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4632 struct stream_interface *si = appctx->appctx->owner;
4633 struct channel *res = si_ic(si);
4634
4635 if (co_data(res)) {
4636 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004637 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004638 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004639 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004640}
4641
4642
Christopher Fauleta2097962019-07-15 16:25:33 +02004643__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004644{
Christopher Fauleta2097962019-07-15 16:25:33 +02004645 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004646}
4647
Christopher Fauleta2097962019-07-15 16:25:33 +02004648/*
4649 *
4650 *
4651 * Class HTTP
4652 *
4653 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004654 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004655
4656/* Returns a struct hlua_txn if the stack entry "ud" is
4657 * a class stream, otherwise it throws an error.
4658 */
4659__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660{
Christopher Fauleta2097962019-07-15 16:25:33 +02004661 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4662}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004663
Christopher Fauleta2097962019-07-15 16:25:33 +02004664/* This function creates and push in the stack a HTTP object
4665 * according with a current TXN.
4666 */
4667static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4668{
4669 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004670
Christopher Fauleta2097962019-07-15 16:25:33 +02004671 /* Check stack size. */
4672 if (!lua_checkstack(L, 3))
4673 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004674
Christopher Fauleta2097962019-07-15 16:25:33 +02004675 /* Create the object: obj[0] = userdata.
4676 * Note that the base of the Converters object is the
4677 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004678 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004679 lua_newtable(L);
4680 htxn = lua_newuserdata(L, sizeof(*htxn));
4681 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004682
4683 htxn->s = txn->s;
4684 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004685 htxn->dir = txn->dir;
4686 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004687
4688 /* Pop a class stream metatable and affect it to the table. */
4689 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4690 lua_setmetatable(L, -2);
4691
4692 return 1;
4693}
4694
4695/* This function creates ans returns an array of HTTP headers.
4696 * This function does not fails. It is used as wrapper with the
4697 * 2 following functions.
4698 */
4699__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4700{
Christopher Fauleta2097962019-07-15 16:25:33 +02004701 struct htx *htx;
4702 int32_t pos;
4703
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004704 /* Create the table. */
4705 lua_newtable(L);
4706
4707 if (!htxn->s->txn)
4708 return 1;
4709
Christopher Fauleta2097962019-07-15 16:25:33 +02004710 htx = htxbuf(&msg->chn->buf);
4711 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4712 struct htx_blk *blk = htx_get_blk(htx, pos);
4713 enum htx_blk_type type = htx_get_blk_type(blk);
4714 struct ist n, v;
4715 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004716
Christopher Fauleta2097962019-07-15 16:25:33 +02004717 if (type == HTX_BLK_HDR) {
4718 n = htx_get_blk_name(htx,blk);
4719 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004720 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004721 else if (type == HTX_BLK_EOH)
4722 break;
4723 else
4724 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004725
Christopher Fauleta2097962019-07-15 16:25:33 +02004726 /* Check for existing entry:
4727 * assume that the table is on the top of the stack, and
4728 * push the key in the stack, the function lua_gettable()
4729 * perform the lookup.
4730 */
4731 lua_pushlstring(L, n.ptr, n.len);
4732 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004733
Christopher Fauleta2097962019-07-15 16:25:33 +02004734 switch (lua_type(L, -1)) {
4735 case LUA_TNIL:
4736 /* Table not found, create it. */
4737 lua_pop(L, 1); /* remove the nil value. */
4738 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4739 lua_newtable(L); /* create and push empty table. */
4740 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4741 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4742 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004743 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004744
Christopher Fauleta2097962019-07-15 16:25:33 +02004745 case LUA_TTABLE:
4746 /* Entry found: push the value in the table. */
4747 len = lua_rawlen(L, -1);
4748 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4749 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4750 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4751 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004752
Christopher Fauleta2097962019-07-15 16:25:33 +02004753 default:
4754 /* Other cases are errors. */
4755 hlua_pusherror(L, "internal error during the parsing of headers.");
4756 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004757 }
4758 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004759 return 1;
4760}
4761
4762__LJMP static int hlua_http_req_get_headers(lua_State *L)
4763{
4764 struct hlua_txn *htxn;
4765
4766 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4767 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4768
Christopher Faulet301eff82019-07-26 16:31:34 +02004769 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004770 WILL_LJMP(lua_error(L));
4771
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004772 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4773}
4774
4775__LJMP static int hlua_http_res_get_headers(lua_State *L)
4776{
4777 struct hlua_txn *htxn;
4778
4779 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4780 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4781
Christopher Faulet301eff82019-07-26 16:31:34 +02004782 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004783 WILL_LJMP(lua_error(L));
4784
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004785 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4786}
4787
4788/* This function replace full header, or just a value in
4789 * the request or in the response. It is a wrapper fir the
4790 * 4 following functions.
4791 */
4792__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4793 struct http_msg *msg, int action)
4794{
4795 size_t name_len;
4796 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4797 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4798 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004799 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004800 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004801
Dragan Dosen26743032019-04-30 15:54:36 +02004802 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004803 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4804
Christopher Fauleta2097962019-07-15 16:25:33 +02004805 htx = htxbuf(&msg->chn->buf);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004806 http_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02004807 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004808 return 0;
4809}
4810
4811__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4812{
4813 struct hlua_txn *htxn;
4814
4815 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4816 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4817
Christopher Faulet301eff82019-07-26 16:31:34 +02004818 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004819 WILL_LJMP(lua_error(L));
4820
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004821 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4822}
4823
4824__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4825{
4826 struct hlua_txn *htxn;
4827
4828 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4829 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4830
Christopher Faulet301eff82019-07-26 16:31:34 +02004831 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004832 WILL_LJMP(lua_error(L));
4833
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004834 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4835}
4836
4837__LJMP static int hlua_http_req_rep_val(lua_State *L)
4838{
4839 struct hlua_txn *htxn;
4840
4841 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4842 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4843
Christopher Faulet301eff82019-07-26 16:31:34 +02004844 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004845 WILL_LJMP(lua_error(L));
4846
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004847 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4848}
4849
4850__LJMP static int hlua_http_res_rep_val(lua_State *L)
4851{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852 struct hlua_txn *htxn;
4853
4854 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4855 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4856
Christopher Faulet301eff82019-07-26 16:31:34 +02004857 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004858 WILL_LJMP(lua_error(L));
4859
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004860 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004861}
4862
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004863/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004864 * It is a wrapper for the 2 following functions.
4865 */
4866__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4867{
4868 size_t len;
4869 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004870 struct htx *htx = htxbuf(&msg->chn->buf);
4871 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004872
Christopher Fauleta2097962019-07-15 16:25:33 +02004873 ctx.blk = NULL;
4874 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4875 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004876 return 0;
4877}
4878
4879__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4880{
4881 struct hlua_txn *htxn;
4882
4883 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4884 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4885
Christopher Faulet301eff82019-07-26 16:31:34 +02004886 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004887 WILL_LJMP(lua_error(L));
4888
Willy Tarreaueee5b512015-04-03 23:46:31 +02004889 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004890}
4891
4892__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4893{
4894 struct hlua_txn *htxn;
4895
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004896 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004897 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4898
Christopher Faulet301eff82019-07-26 16:31:34 +02004899 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004900 WILL_LJMP(lua_error(L));
4901
Willy Tarreaueee5b512015-04-03 23:46:31 +02004902 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004903}
4904
4905/* This function adds an header. It is a wrapper used by
4906 * the 2 following functions.
4907 */
4908__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4909{
4910 size_t name_len;
4911 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4912 size_t value_len;
4913 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004914 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004915
Christopher Fauleta2097962019-07-15 16:25:33 +02004916 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4917 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004918 return 0;
4919}
4920
4921__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4922{
4923 struct hlua_txn *htxn;
4924
4925 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4926 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4927
Christopher Faulet301eff82019-07-26 16:31:34 +02004928 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004929 WILL_LJMP(lua_error(L));
4930
Willy Tarreaueee5b512015-04-03 23:46:31 +02004931 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004932}
4933
4934__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4935{
4936 struct hlua_txn *htxn;
4937
4938 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4939 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4940
Christopher Faulet301eff82019-07-26 16:31:34 +02004941 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004942 WILL_LJMP(lua_error(L));
4943
Willy Tarreaueee5b512015-04-03 23:46:31 +02004944 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004945}
4946
4947static int hlua_http_req_set_hdr(lua_State *L)
4948{
4949 struct hlua_txn *htxn;
4950
4951 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4952 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4953
Christopher Faulet301eff82019-07-26 16:31:34 +02004954 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004955 WILL_LJMP(lua_error(L));
4956
Willy Tarreaueee5b512015-04-03 23:46:31 +02004957 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4958 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004959}
4960
4961static int hlua_http_res_set_hdr(lua_State *L)
4962{
4963 struct hlua_txn *htxn;
4964
4965 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4966 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4967
Christopher Faulet301eff82019-07-26 16:31:34 +02004968 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004969 WILL_LJMP(lua_error(L));
4970
Willy Tarreaueee5b512015-04-03 23:46:31 +02004971 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4972 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004973}
4974
4975/* This function set the method. */
4976static int hlua_http_req_set_meth(lua_State *L)
4977{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004978 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004979 size_t name_len;
4980 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004981
Christopher Faulet301eff82019-07-26 16:31:34 +02004982 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004983 WILL_LJMP(lua_error(L));
4984
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004985 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004986 return 1;
4987}
4988
4989/* This function set the method. */
4990static int hlua_http_req_set_path(lua_State *L)
4991{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004992 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004993 size_t name_len;
4994 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004995
Christopher Faulet301eff82019-07-26 16:31:34 +02004996 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004997 WILL_LJMP(lua_error(L));
4998
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004999 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005000 return 1;
5001}
5002
5003/* This function set the query-string. */
5004static int hlua_http_req_set_query(lua_State *L)
5005{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005006 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005007 size_t name_len;
5008 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005009
Christopher Faulet301eff82019-07-26 16:31:34 +02005010 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005011 WILL_LJMP(lua_error(L));
5012
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005013 /* Check length. */
5014 if (name_len > trash.size - 1) {
5015 lua_pushboolean(L, 0);
5016 return 1;
5017 }
5018
5019 /* Add the mark question as prefix. */
5020 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005021 trash.area[trash.data++] = '?';
5022 memcpy(trash.area + trash.data, name, name_len);
5023 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005024
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005025 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005026 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005027 return 1;
5028}
5029
5030/* This function set the uri. */
5031static int hlua_http_req_set_uri(lua_State *L)
5032{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005033 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005034 size_t name_len;
5035 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005036
Christopher Faulet301eff82019-07-26 16:31:34 +02005037 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005038 WILL_LJMP(lua_error(L));
5039
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005040 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005041 return 1;
5042}
5043
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005044/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005045static int hlua_http_res_set_status(lua_State *L)
5046{
5047 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5048 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005049 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005050
Christopher Faulet301eff82019-07-26 16:31:34 +02005051 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005052 WILL_LJMP(lua_error(L));
5053
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005054 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005055 return 0;
5056}
5057
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005058/*
5059 *
5060 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005061 * Class TXN
5062 *
5063 *
5064 */
5065
5066/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005067 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005068 */
5069__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5070{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005071 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005072}
5073
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005074__LJMP static int hlua_set_var(lua_State *L)
5075{
5076 struct hlua_txn *htxn;
5077 const char *name;
5078 size_t len;
5079 struct sample smp;
5080
5081 MAY_LJMP(check_args(L, 3, "set_var"));
5082
5083 /* It is useles to retrieve the stream, but this function
5084 * runs only in a stream context.
5085 */
5086 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5087 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5088
5089 /* Converts the third argument in a sample. */
5090 hlua_lua2smp(L, 3, &smp);
5091
5092 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005093 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005094 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005095 return 0;
5096}
5097
Christopher Faulet85d79c92016-11-09 16:54:56 +01005098__LJMP static int hlua_unset_var(lua_State *L)
5099{
5100 struct hlua_txn *htxn;
5101 const char *name;
5102 size_t len;
5103 struct sample smp;
5104
5105 MAY_LJMP(check_args(L, 2, "unset_var"));
5106
5107 /* It is useles to retrieve the stream, but this function
5108 * runs only in a stream context.
5109 */
5110 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5111 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5112
5113 /* Unset the variable. */
5114 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5115 vars_unset_by_name(name, len, &smp);
5116 return 0;
5117}
5118
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005119__LJMP static int hlua_get_var(lua_State *L)
5120{
5121 struct hlua_txn *htxn;
5122 const char *name;
5123 size_t len;
5124 struct sample smp;
5125
5126 MAY_LJMP(check_args(L, 2, "get_var"));
5127
5128 /* It is useles to retrieve the stream, but this function
5129 * runs only in a stream context.
5130 */
5131 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5132 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5133
Willy Tarreau7560dd42016-03-10 16:28:58 +01005134 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005135 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005136 lua_pushnil(L);
5137 return 1;
5138 }
5139
5140 return hlua_smp2lua(L, &smp);
5141}
5142
Willy Tarreau59551662015-03-10 14:23:13 +01005143__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005144{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005145 struct hlua *hlua;
5146
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005147 MAY_LJMP(check_args(L, 2, "set_priv"));
5148
Willy Tarreau87b09662015-04-03 00:22:06 +02005149 /* It is useles to retrieve the stream, but this function
5150 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005151 */
5152 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005153 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005154
5155 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005156 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005157
5158 /* Get and store new value. */
5159 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5160 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5161
5162 return 0;
5163}
5164
Willy Tarreau59551662015-03-10 14:23:13 +01005165__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005166{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005167 struct hlua *hlua;
5168
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005169 MAY_LJMP(check_args(L, 1, "get_priv"));
5170
Willy Tarreau87b09662015-04-03 00:22:06 +02005171 /* It is useles to retrieve the stream, but this function
5172 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005173 */
5174 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005175 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005176
5177 /* Push configuration index in the stack. */
5178 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5179
5180 return 1;
5181}
5182
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005183/* Create stack entry containing a class TXN. This function
5184 * return 0 if the stack does not contains free slots,
5185 * otherwise it returns 1.
5186 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005187static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005188{
Willy Tarreaude491382015-04-06 11:04:28 +02005189 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005190
5191 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005192 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005193 return 0;
5194
5195 /* NOTE: The allocation never fails. The failure
5196 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005197 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005198 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005199 /* Create the object: obj[0] = userdata. */
5200 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005201 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005202 lua_rawseti(L, -2, 0);
5203
Willy Tarreaude491382015-04-06 11:04:28 +02005204 htxn->s = s;
5205 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005206 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005207 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005208
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005209 /* Create the "f" field that contains a list of fetches. */
5210 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005211 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005212 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005213 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005214
5215 /* Create the "sf" field that contains a list of stringsafe fetches. */
5216 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005217 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005218 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005219 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005220
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005221 /* Create the "c" field that contains a list of converters. */
5222 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005223 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005224 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005225 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005226
5227 /* Create the "sc" field that contains a list of stringsafe converters. */
5228 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005229 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005230 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005231 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005232
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005233 /* Create the "req" field that contains the request channel object. */
5234 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005235 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005236 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005237 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005238
5239 /* Create the "res" field that contains the response channel object. */
5240 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005241 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005242 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005243 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005244
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005245 /* Creates the HTTP object is the current proxy allows http. */
5246 lua_pushstring(L, "http");
5247 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005248 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005249 return 0;
5250 }
5251 else
5252 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005253 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005254
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005255 /* Pop a class sesison metatable and affect it to the userdata. */
5256 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5257 lua_setmetatable(L, -2);
5258
5259 return 1;
5260}
5261
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005262__LJMP static int hlua_txn_deflog(lua_State *L)
5263{
5264 const char *msg;
5265 struct hlua_txn *htxn;
5266
5267 MAY_LJMP(check_args(L, 2, "deflog"));
5268 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5269 msg = MAY_LJMP(luaL_checkstring(L, 2));
5270
5271 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5272 return 0;
5273}
5274
5275__LJMP static int hlua_txn_log(lua_State *L)
5276{
5277 int level;
5278 const char *msg;
5279 struct hlua_txn *htxn;
5280
5281 MAY_LJMP(check_args(L, 3, "log"));
5282 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5283 level = MAY_LJMP(luaL_checkinteger(L, 2));
5284 msg = MAY_LJMP(luaL_checkstring(L, 3));
5285
5286 if (level < 0 || level >= NB_LOG_LEVELS)
5287 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5288
5289 hlua_sendlog(htxn->s->be, level, msg);
5290 return 0;
5291}
5292
5293__LJMP static int hlua_txn_log_debug(lua_State *L)
5294{
5295 const char *msg;
5296 struct hlua_txn *htxn;
5297
5298 MAY_LJMP(check_args(L, 2, "Debug"));
5299 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5300 msg = MAY_LJMP(luaL_checkstring(L, 2));
5301 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5302 return 0;
5303}
5304
5305__LJMP static int hlua_txn_log_info(lua_State *L)
5306{
5307 const char *msg;
5308 struct hlua_txn *htxn;
5309
5310 MAY_LJMP(check_args(L, 2, "Info"));
5311 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5312 msg = MAY_LJMP(luaL_checkstring(L, 2));
5313 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5314 return 0;
5315}
5316
5317__LJMP static int hlua_txn_log_warning(lua_State *L)
5318{
5319 const char *msg;
5320 struct hlua_txn *htxn;
5321
5322 MAY_LJMP(check_args(L, 2, "Warning"));
5323 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5324 msg = MAY_LJMP(luaL_checkstring(L, 2));
5325 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5326 return 0;
5327}
5328
5329__LJMP static int hlua_txn_log_alert(lua_State *L)
5330{
5331 const char *msg;
5332 struct hlua_txn *htxn;
5333
5334 MAY_LJMP(check_args(L, 2, "Alert"));
5335 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5336 msg = MAY_LJMP(luaL_checkstring(L, 2));
5337 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5338 return 0;
5339}
5340
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005341__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5342{
5343 struct hlua_txn *htxn;
5344 int ll;
5345
5346 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5347 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5348 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5349
5350 if (ll < 0 || ll > 7)
5351 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5352
5353 htxn->s->logs.level = ll;
5354 return 0;
5355}
5356
5357__LJMP static int hlua_txn_set_tos(lua_State *L)
5358{
5359 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005360 int tos;
5361
5362 MAY_LJMP(check_args(L, 2, "set_tos"));
5363 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5364 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5365
Willy Tarreau1a18b542018-12-11 16:37:42 +01005366 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005367 return 0;
5368}
5369
5370__LJMP static int hlua_txn_set_mark(lua_State *L)
5371{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005372 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005373 int mark;
5374
5375 MAY_LJMP(check_args(L, 2, "set_mark"));
5376 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5377 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5378
Willy Tarreau1a18b542018-12-11 16:37:42 +01005379 conn_set_tos(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005380 return 0;
5381}
5382
Patrick Hemmer268a7072018-05-11 12:52:31 -04005383__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5384{
5385 struct hlua_txn *htxn;
5386
5387 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5388 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5389 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5390 return 0;
5391}
5392
5393__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5394{
5395 struct hlua_txn *htxn;
5396
5397 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5398 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5399 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5400 return 0;
5401}
5402
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005403/* This function is an Lua binding that send pending data
5404 * to the client, and close the stream interface.
5405 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005406__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005407{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005408 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005409 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005410 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005411
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005412 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005413 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005414 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005415
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005416 /* If the flags NOTERM is set, we cannot terminate the http
5417 * session, so we just end the execution of the current
5418 * lua code.
5419 */
5420 if (htxn->flags & HLUA_TXN_NOTERM) {
5421 WILL_LJMP(hlua_done(L));
5422 return 0;
5423 }
5424
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005425 ic = &htxn->s->req;
5426 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005427
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005428 if (IS_HTX_STRM(htxn->s)) {
5429 htxn->s->txn->status = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005430 http_reply_and_close(htxn->s, 0, NULL);
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005431 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005432 else {
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005433 channel_auto_read(ic);
5434 channel_abort(ic);
5435 channel_auto_close(ic);
5436 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005437
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005438 oc->wex = tick_add_ifset(now_ms, oc->wto);
5439 channel_auto_read(oc);
5440 channel_auto_close(oc);
5441 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005442
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005443 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005444
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005445 ic->analysers &= AN_REQ_FLT_END;
5446 oc->analysers &= AN_RES_FLT_END;
5447
5448 if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is
5449 htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
5450
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005451 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005452 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005453 return 0;
5454}
5455
5456__LJMP static int hlua_log(lua_State *L)
5457{
5458 int level;
5459 const char *msg;
5460
5461 MAY_LJMP(check_args(L, 2, "log"));
5462 level = MAY_LJMP(luaL_checkinteger(L, 1));
5463 msg = MAY_LJMP(luaL_checkstring(L, 2));
5464
5465 if (level < 0 || level >= NB_LOG_LEVELS)
5466 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5467
5468 hlua_sendlog(NULL, level, msg);
5469 return 0;
5470}
5471
5472__LJMP static int hlua_log_debug(lua_State *L)
5473{
5474 const char *msg;
5475
5476 MAY_LJMP(check_args(L, 1, "debug"));
5477 msg = MAY_LJMP(luaL_checkstring(L, 1));
5478 hlua_sendlog(NULL, LOG_DEBUG, msg);
5479 return 0;
5480}
5481
5482__LJMP static int hlua_log_info(lua_State *L)
5483{
5484 const char *msg;
5485
5486 MAY_LJMP(check_args(L, 1, "info"));
5487 msg = MAY_LJMP(luaL_checkstring(L, 1));
5488 hlua_sendlog(NULL, LOG_INFO, msg);
5489 return 0;
5490}
5491
5492__LJMP static int hlua_log_warning(lua_State *L)
5493{
5494 const char *msg;
5495
5496 MAY_LJMP(check_args(L, 1, "warning"));
5497 msg = MAY_LJMP(luaL_checkstring(L, 1));
5498 hlua_sendlog(NULL, LOG_WARNING, msg);
5499 return 0;
5500}
5501
5502__LJMP static int hlua_log_alert(lua_State *L)
5503{
5504 const char *msg;
5505
5506 MAY_LJMP(check_args(L, 1, "alert"));
5507 msg = MAY_LJMP(luaL_checkstring(L, 1));
5508 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005509 return 0;
5510}
5511
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005512__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005513{
5514 int wakeup_ms = lua_tointeger(L, -1);
5515 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005516 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005517 return 0;
5518}
5519
5520__LJMP static int hlua_sleep(lua_State *L)
5521{
5522 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005523 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005524
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005525 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005526
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005527 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005528 wakeup_ms = tick_add(now_ms, delay);
5529 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005530
Willy Tarreau9635e032018-10-16 17:52:55 +02005531 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005532 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005533}
5534
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005535__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005536{
5537 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005538 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005539
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005540 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005541
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005542 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005543 wakeup_ms = tick_add(now_ms, delay);
5544 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005545
Willy Tarreau9635e032018-10-16 17:52:55 +02005546 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005547 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005548}
5549
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005550/* This functionis an LUA binding. it permits to give back
5551 * the hand at the HAProxy scheduler. It is used when the
5552 * LUA processing consumes a lot of time.
5553 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005554__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005555{
5556 return 0;
5557}
5558
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005559__LJMP static int hlua_yield(lua_State *L)
5560{
Willy Tarreau9635e032018-10-16 17:52:55 +02005561 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005562 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005563}
5564
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005565/* This function change the nice of the currently executed
5566 * task. It is used set low or high priority at the current
5567 * task.
5568 */
Willy Tarreau59551662015-03-10 14:23:13 +01005569__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005570{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005571 struct hlua *hlua;
5572 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005573
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005574 MAY_LJMP(check_args(L, 1, "set_nice"));
5575 hlua = hlua_gethlua(L);
5576 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005577
5578 /* If he task is not set, I'm in a start mode. */
5579 if (!hlua || !hlua->task)
5580 return 0;
5581
5582 if (nice < -1024)
5583 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005584 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005585 nice = 1024;
5586
5587 hlua->task->nice = nice;
5588 return 0;
5589}
5590
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005591/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005592 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5593 * return an E_AGAIN signal, the emmiter of this signal must set a
5594 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005595 *
5596 * Task wrapper are longjmp safe because the only one Lua code
5597 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005598 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005599static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005600{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005601 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005602 enum hlua_exec status;
5603
Christopher Faulet5bc99722018-04-25 10:34:45 +02005604 if (task->thread_mask == MAX_THREADS_MASK)
5605 task_set_affinity(task, tid_bit);
5606
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005607 /* If it is the first call to the task, we must initialize the
5608 * execution timeouts.
5609 */
5610 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005611 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005612
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005613 /* Execute the Lua code. */
5614 status = hlua_ctx_resume(hlua, 1);
5615
5616 switch (status) {
5617 /* finished or yield */
5618 case HLUA_E_OK:
5619 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005620 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005621 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005622 break;
5623
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005624 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005625 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005626 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005627 break;
5628
5629 /* finished with error. */
5630 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005631 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005632 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005633 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005634 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005635 break;
5636
5637 case HLUA_E_ERR:
5638 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005639 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005640 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005641 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005642 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005643 break;
5644 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005645 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005646}
5647
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005648/* This function is an LUA binding that register LUA function to be
5649 * executed after the HAProxy configuration parsing and before the
5650 * HAProxy scheduler starts. This function expect only one LUA
5651 * argument that is a function. This function returns nothing, but
5652 * throws if an error is encountered.
5653 */
5654__LJMP static int hlua_register_init(lua_State *L)
5655{
5656 struct hlua_init_function *init;
5657 int ref;
5658
5659 MAY_LJMP(check_args(L, 1, "register_init"));
5660
5661 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5662
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005663 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005664 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005665 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005666
5667 init->function_ref = ref;
5668 LIST_ADDQ(&hlua_init_functions, &init->l);
5669 return 0;
5670}
5671
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005672/* This functio is an LUA binding. It permits to register a task
5673 * executed in parallel of the main HAroxy activity. The task is
5674 * created and it is set in the HAProxy scheduler. It can be called
5675 * from the "init" section, "post init" or during the runtime.
5676 *
5677 * Lua prototype:
5678 *
5679 * <none> core.register_task(<function>)
5680 */
5681static int hlua_register_task(lua_State *L)
5682{
5683 struct hlua *hlua;
5684 struct task *task;
5685 int ref;
5686
5687 MAY_LJMP(check_args(L, 1, "register_task"));
5688
5689 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5690
Willy Tarreaubafbe012017-11-24 17:34:44 +01005691 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005692 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005693 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005694
Emeric Brunc60def82017-09-27 14:59:38 +02005695 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005696 if (!task)
5697 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5698
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005699 task->context = hlua;
5700 task->process = hlua_process_task;
5701
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005702 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005703 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005704
5705 /* Restore the function in the stack. */
5706 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5707 hlua->nargs = 0;
5708
5709 /* Schedule task. */
5710 task_schedule(task, now_ms);
5711
5712 return 0;
5713}
5714
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005715/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5716 * doesn't allow "yield" functions because the HAProxy engine cannot
5717 * resume converters.
5718 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005719static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005720{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005721 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005722 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005723 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005724
Willy Tarreaube508f12016-03-10 11:47:01 +01005725 if (!stream)
5726 return 0;
5727
Willy Tarreau87b09662015-04-03 00:22:06 +02005728 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005729 * Lua context can be not initialized. This behavior
5730 * permits to save performances because a systematic
5731 * Lua initialization cause 5% performances loss.
5732 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005733 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005734 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005735 if (!stream->hlua) {
5736 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5737 return 0;
5738 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005739 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005740 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5741 return 0;
5742 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005743 }
5744
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005745 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005746 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005747
5748 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005749 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5750 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5751 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005752 else
5753 error = "critical error";
5754 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005755 return 0;
5756 }
5757
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005758 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005759 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005760 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005761 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005762 return 0;
5763 }
5764
5765 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005766 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005767
5768 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005769 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005770 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005771 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005772 return 0;
5773 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005774 hlua_smp2lua(stream->hlua->T, smp);
5775 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005776
5777 /* push keywords in the stack. */
5778 if (arg_p) {
5779 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005780 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005781 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005782 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005783 return 0;
5784 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005785 hlua_arg2lua(stream->hlua->T, arg_p);
5786 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005787 }
5788 }
5789
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005790 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005791 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005792
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005793 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005794 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005795 }
5796
5797 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005798 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005799 /* finished. */
5800 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005801 /* If the stack is empty, the function fails. */
5802 if (lua_gettop(stream->hlua->T) <= 0)
5803 return 0;
5804
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005805 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005806 hlua_lua2smp(stream->hlua->T, -1, smp);
5807 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005808 return 1;
5809
5810 /* yield. */
5811 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005812 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005813 return 0;
5814
5815 /* finished with error. */
5816 case HLUA_E_ERRMSG:
5817 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005818 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005819 fcn->name, lua_tostring(stream->hlua->T, -1));
5820 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005821 return 0;
5822
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005823 case HLUA_E_ETMOUT:
5824 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5825 return 0;
5826
5827 case HLUA_E_NOMEM:
5828 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5829 return 0;
5830
5831 case HLUA_E_YIELD:
5832 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5833 return 0;
5834
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005835 case HLUA_E_ERR:
5836 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005837 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005838
5839 default:
5840 return 0;
5841 }
5842}
5843
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005844/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5845 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005846 * resume sample-fetches. This function will be called by the sample
5847 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005848 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005849static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5850 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005851{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005852 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005853 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005854 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005855 const struct buffer msg = { };
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02005856 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005857
Willy Tarreaube508f12016-03-10 11:47:01 +01005858 if (!stream)
5859 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005860
Willy Tarreau87b09662015-04-03 00:22:06 +02005861 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005862 * Lua context can be not initialized. This behavior
5863 * permits to save performances because a systematic
5864 * Lua initialization cause 5% performances loss.
5865 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005866 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005867 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005868 if (!stream->hlua) {
5869 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5870 return 0;
5871 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005872 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005873 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5874 return 0;
5875 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005876 }
5877
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005878 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005879
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02005880 if (stream->be->mode == PR_MODE_HTTP) {
5881 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
5882 hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
5883 else
5884 hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
5885 }
5886
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005887 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005888 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005889
5890 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005891 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5892 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5893 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005894 else
5895 error = "critical error";
5896 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005897 return 0;
5898 }
5899
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005900 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005901 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005902 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005903 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005904 return 0;
5905 }
5906
5907 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005908 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005909
5910 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02005911 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005912 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005913 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005914 return 0;
5915 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005916 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005917
5918 /* push keywords in the stack. */
5919 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5920 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005921 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005922 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005923 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005924 return 0;
5925 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005926 hlua_arg2lua(stream->hlua->T, arg_p);
5927 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005928 }
5929
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005930 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005931 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005932
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005933 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005934 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005935 }
5936
5937 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005938 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005939 /* finished. */
5940 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005941 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005942 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005943 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005944 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005945 /* If the stack is empty, the function fails. */
5946 if (lua_gettop(stream->hlua->T) <= 0)
5947 return 0;
5948
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005949 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005950 hlua_lua2smp(stream->hlua->T, -1, smp);
5951 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005952
5953 /* Set the end of execution flag. */
5954 smp->flags &= ~SMP_F_MAY_CHANGE;
5955 return 1;
5956
5957 /* yield. */
5958 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005959 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005960 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005961 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005962 return 0;
5963
5964 /* finished with error. */
5965 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005966 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005967 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005968 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005969 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005970 fcn->name, lua_tostring(stream->hlua->T, -1));
5971 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005972 return 0;
5973
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005974 case HLUA_E_ETMOUT:
5975 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005976 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005977 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5978 return 0;
5979
5980 case HLUA_E_NOMEM:
5981 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005982 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005983 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
5984 return 0;
5985
5986 case HLUA_E_YIELD:
5987 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005988 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005989 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
5990 return 0;
5991
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005992 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005993 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005994 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005995 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005996 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005997
5998 default:
5999 return 0;
6000 }
6001}
6002
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006003/* This function is an LUA binding used for registering
6004 * "sample-conv" functions. It expects a converter name used
6005 * in the haproxy configuration file, and an LUA function.
6006 */
6007__LJMP static int hlua_register_converters(lua_State *L)
6008{
6009 struct sample_conv_kw_list *sck;
6010 const char *name;
6011 int ref;
6012 int len;
6013 struct hlua_function *fcn;
6014
6015 MAY_LJMP(check_args(L, 2, "register_converters"));
6016
6017 /* First argument : converter name. */
6018 name = MAY_LJMP(luaL_checkstring(L, 1));
6019
6020 /* Second argument : lua function. */
6021 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6022
6023 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006024 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006025 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006026 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006027 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006028 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006029 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006030
6031 /* Fill fcn. */
6032 fcn->name = strdup(name);
6033 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006034 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006035 fcn->function_ref = ref;
6036
6037 /* List head */
6038 sck->list.n = sck->list.p = NULL;
6039
6040 /* converter keyword. */
6041 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006042 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006043 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006044 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006045
6046 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6047 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006048 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 +01006049 sck->kw[0].val_args = NULL;
6050 sck->kw[0].in_type = SMP_T_STR;
6051 sck->kw[0].out_type = SMP_T_STR;
6052 sck->kw[0].private = fcn;
6053
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006054 /* Register this new converter */
6055 sample_register_convs(sck);
6056
6057 return 0;
6058}
6059
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006060/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006061 * "sample-fetch" functions. It expects a converter name used
6062 * in the haproxy configuration file, and an LUA function.
6063 */
6064__LJMP static int hlua_register_fetches(lua_State *L)
6065{
6066 const char *name;
6067 int ref;
6068 int len;
6069 struct sample_fetch_kw_list *sfk;
6070 struct hlua_function *fcn;
6071
6072 MAY_LJMP(check_args(L, 2, "register_fetches"));
6073
6074 /* First argument : sample-fetch name. */
6075 name = MAY_LJMP(luaL_checkstring(L, 1));
6076
6077 /* Second argument : lua function. */
6078 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6079
6080 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006081 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006082 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006083 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006084 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006085 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006086 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006087
6088 /* Fill fcn. */
6089 fcn->name = strdup(name);
6090 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006091 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006092 fcn->function_ref = ref;
6093
6094 /* List head */
6095 sfk->list.n = sfk->list.p = NULL;
6096
6097 /* sample-fetch keyword. */
6098 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006099 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006100 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006101 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006102
6103 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6104 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006105 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 +01006106 sfk->kw[0].val_args = NULL;
6107 sfk->kw[0].out_type = SMP_T_STR;
6108 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6109 sfk->kw[0].val = 0;
6110 sfk->kw[0].private = fcn;
6111
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006112 /* Register this new fetch. */
6113 sample_register_fetches(sfk);
6114
6115 return 0;
6116}
6117
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006118/* This function is a wrapper to execute each LUA function declared
6119 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006120 * return ACT_RET_CONT if the processing is finished (with or without
6121 * error) and return ACT_RET_YIELD if the function must be called again
6122 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006123 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006124static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006125 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006126{
6127 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006128 unsigned int hflags = 0;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006129 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006130 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006131 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006132
6133 switch (rule->from) {
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006134 case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break;
6135 case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break;
6136 case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
6137 case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006138 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006139 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006140 return ACT_RET_CONT;
6141 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006142
Willy Tarreau87b09662015-04-03 00:22:06 +02006143 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006144 * Lua context can be not initialized. This behavior
6145 * permits to save performances because a systematic
6146 * Lua initialization cause 5% performances loss.
6147 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006148 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006149 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006150 if (!s->hlua) {
6151 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6152 rule->arg.hlua_rule->fcn.name);
6153 return ACT_RET_CONT;
6154 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006155 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006156 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6157 rule->arg.hlua_rule->fcn.name);
6158 return ACT_RET_CONT;
6159 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006160 }
6161
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006162 consistency_set(s, dir, &s->hlua->cons);
6163
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006164 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006165 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006166
6167 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006168 if (!SET_SAFE_LJMP(s->hlua->T)) {
6169 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6170 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006171 else
6172 error = "critical error";
6173 SEND_ERR(px, "Lua function '%s': %s.\n",
6174 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006175 return ACT_RET_CONT;
6176 }
6177
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006178 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006179 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006180 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006181 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006182 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006183 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006184 }
6185
6186 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006187 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006188
Willy Tarreau87b09662015-04-03 00:22:06 +02006189 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006190 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006191 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006192 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006193 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006194 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006195 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006196 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006197
6198 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006199 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006200 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006201 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006202 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006203 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006204 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006205 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006206 lua_pushstring(s->hlua->T, *arg);
6207 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006208 }
6209
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006210 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006211 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006212
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006213 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006214 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006215 }
6216
6217 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006218 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006219 /* finished. */
6220 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006221 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006222 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006223 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006224 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006225 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet8f1aa772019-07-04 11:27:15 +02006226 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006227 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006228
6229 /* yield. */
6230 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006231 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006232 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006233 if (dir & SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006234 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006235 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006236 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006237 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006238 /* Some actions can be wake up when a "write" event
6239 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006240 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006241 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006242 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006243 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006244 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006245 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006246 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006247 * because HAProxy is not able to manipulate data, it
6248 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006249 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006250
6251 /* finished with error. */
6252 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006253 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006254 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006255 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006256 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006257 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006258 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006259 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6260 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006261 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006262
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006263 case HLUA_E_ETMOUT:
6264 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006265 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006266 return ACT_RET_ERR;
6267 }
6268 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6269 return 0;
6270
6271 case HLUA_E_NOMEM:
6272 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006273 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006274 return ACT_RET_ERR;
6275 }
6276 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6277 return 0;
6278
6279 case HLUA_E_YIELD:
6280 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006281 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006282 return ACT_RET_ERR;
6283 }
6284 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6285 rule->arg.hlua_rule->fcn.name);
6286 return 0;
6287
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006288 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006289 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006290 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006291 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006292 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006293 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006294 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006295 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006296
6297 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006298 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006299 }
6300}
6301
Olivier Houchard9f6af332018-05-25 14:04:04 +02006302struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006303{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006304 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006305
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006306 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006307 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006308 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006309}
6310
6311static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6312{
6313 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006314 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006315 struct task *task;
6316 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006317 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006318
Willy Tarreaubafbe012017-11-24 17:34:44 +01006319 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006320 if (!hlua) {
6321 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6322 ctx->rule->arg.hlua_rule->fcn.name);
6323 return 0;
6324 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006325 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006326 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006327 ctx->ctx.hlua_apptcp.flags = 0;
6328
6329 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006330 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006331 if (!task) {
6332 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6333 ctx->rule->arg.hlua_rule->fcn.name);
6334 return 0;
6335 }
6336 task->nice = 0;
6337 task->context = ctx;
6338 task->process = hlua_applet_wakeup;
6339 ctx->ctx.hlua_apptcp.task = task;
6340
6341 /* In the execution wrappers linked with a stream, the
6342 * Lua context can be not initialized. This behavior
6343 * permits to save performances because a systematic
6344 * Lua initialization cause 5% performances loss.
6345 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006346 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006347 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6348 ctx->rule->arg.hlua_rule->fcn.name);
6349 return 0;
6350 }
6351
6352 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006353 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006354
6355 /* The following Lua calls can fail. */
6356 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006357 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6358 error = lua_tostring(hlua->T, -1);
6359 else
6360 error = "critical error";
6361 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6362 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006363 return 0;
6364 }
6365
6366 /* Check stack available size. */
6367 if (!lua_checkstack(hlua->T, 1)) {
6368 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6369 ctx->rule->arg.hlua_rule->fcn.name);
6370 RESET_SAFE_LJMP(hlua->T);
6371 return 0;
6372 }
6373
6374 /* Restore the function in the stack. */
6375 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6376
6377 /* Create and and push object stream in the stack. */
6378 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6379 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6380 ctx->rule->arg.hlua_rule->fcn.name);
6381 RESET_SAFE_LJMP(hlua->T);
6382 return 0;
6383 }
6384 hlua->nargs = 1;
6385
6386 /* push keywords in the stack. */
6387 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6388 if (!lua_checkstack(hlua->T, 1)) {
6389 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6390 ctx->rule->arg.hlua_rule->fcn.name);
6391 RESET_SAFE_LJMP(hlua->T);
6392 return 0;
6393 }
6394 lua_pushstring(hlua->T, *arg);
6395 hlua->nargs++;
6396 }
6397
6398 RESET_SAFE_LJMP(hlua->T);
6399
6400 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006401 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006402 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006403
6404 return 1;
6405}
6406
6407static void hlua_applet_tcp_fct(struct appctx *ctx)
6408{
6409 struct stream_interface *si = ctx->owner;
6410 struct stream *strm = si_strm(si);
6411 struct channel *res = si_ic(si);
6412 struct act_rule *rule = ctx->rule;
6413 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006414 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006415
6416 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006417 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6418 /* eat the whole request */
6419 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006420 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006421 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006422
6423 /* If the stream is disconnect or closed, ldo nothing. */
6424 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6425 return;
6426
6427 /* Execute the function. */
6428 switch (hlua_ctx_resume(hlua, 1)) {
6429 /* finished. */
6430 case HLUA_E_OK:
6431 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6432
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006433 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006434 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006435 res->flags |= CF_READ_NULL;
6436 si_shutr(si);
6437 return;
6438
6439 /* yield. */
6440 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006441 if (hlua->wake_time != TICK_ETERNITY)
6442 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006443 return;
6444
6445 /* finished with error. */
6446 case HLUA_E_ERRMSG:
6447 /* Display log. */
6448 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6449 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6450 lua_pop(hlua->T, 1);
6451 goto error;
6452
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006453 case HLUA_E_ETMOUT:
6454 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6455 rule->arg.hlua_rule->fcn.name);
6456 goto error;
6457
6458 case HLUA_E_NOMEM:
6459 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6460 rule->arg.hlua_rule->fcn.name);
6461 goto error;
6462
6463 case HLUA_E_YIELD: /* unexpected */
6464 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6465 rule->arg.hlua_rule->fcn.name);
6466 goto error;
6467
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006468 case HLUA_E_ERR:
6469 /* Display log. */
6470 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6471 rule->arg.hlua_rule->fcn.name);
6472 goto error;
6473
6474 default:
6475 goto error;
6476 }
6477
6478error:
6479
6480 /* For all other cases, just close the stream. */
6481 si_shutw(si);
6482 si_shutr(si);
6483 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6484}
6485
6486static void hlua_applet_tcp_release(struct appctx *ctx)
6487{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006488 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006489 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006490 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006491 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006492}
6493
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006494/* The function returns 1 if the initialisation is complete, 0 if
6495 * an errors occurs and -1 if more data are required for initializing
6496 * the applet.
6497 */
6498static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6499{
6500 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006501 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006502 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006503 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006504 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006505 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006506
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006507 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006508 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006509 if (!hlua) {
6510 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6511 ctx->rule->arg.hlua_rule->fcn.name);
6512 return 0;
6513 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006514 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006515 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006516 ctx->ctx.hlua_apphttp.left_bytes = -1;
6517 ctx->ctx.hlua_apphttp.flags = 0;
6518
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006519 if (txn->req.flags & HTTP_MSGF_VER_11)
6520 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6521
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006522 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006523 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006524 if (!task) {
6525 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6526 ctx->rule->arg.hlua_rule->fcn.name);
6527 return 0;
6528 }
6529 task->nice = 0;
6530 task->context = ctx;
6531 task->process = hlua_applet_wakeup;
6532 ctx->ctx.hlua_apphttp.task = task;
6533
6534 /* In the execution wrappers linked with a stream, the
6535 * Lua context can be not initialized. This behavior
6536 * permits to save performances because a systematic
6537 * Lua initialization cause 5% performances loss.
6538 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006539 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006540 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6541 ctx->rule->arg.hlua_rule->fcn.name);
6542 return 0;
6543 }
6544
6545 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006546 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006547
6548 /* The following Lua calls can fail. */
6549 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006550 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6551 error = lua_tostring(hlua->T, -1);
6552 else
6553 error = "critical error";
6554 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6555 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006556 return 0;
6557 }
6558
6559 /* Check stack available size. */
6560 if (!lua_checkstack(hlua->T, 1)) {
6561 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6562 ctx->rule->arg.hlua_rule->fcn.name);
6563 RESET_SAFE_LJMP(hlua->T);
6564 return 0;
6565 }
6566
6567 /* Restore the function in the stack. */
6568 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6569
6570 /* Create and and push object stream in the stack. */
6571 if (!hlua_applet_http_new(hlua->T, ctx)) {
6572 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6573 ctx->rule->arg.hlua_rule->fcn.name);
6574 RESET_SAFE_LJMP(hlua->T);
6575 return 0;
6576 }
6577 hlua->nargs = 1;
6578
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006579 /* push keywords in the stack. */
6580 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6581 if (!lua_checkstack(hlua->T, 1)) {
6582 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6583 ctx->rule->arg.hlua_rule->fcn.name);
6584 RESET_SAFE_LJMP(hlua->T);
6585 return 0;
6586 }
6587 lua_pushstring(hlua->T, *arg);
6588 hlua->nargs++;
6589 }
6590
6591 RESET_SAFE_LJMP(hlua->T);
6592
6593 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006594 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006595
6596 return 1;
6597}
6598
Christopher Fauleta2097962019-07-15 16:25:33 +02006599static void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006600{
6601 struct stream_interface *si = ctx->owner;
6602 struct stream *strm = si_strm(si);
6603 struct channel *req = si_oc(si);
6604 struct channel *res = si_ic(si);
6605 struct act_rule *rule = ctx->rule;
6606 struct proxy *px = strm->be;
6607 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
6608 struct htx *req_htx, *res_htx;
6609
6610 res_htx = htx_from_buf(&res->buf);
6611
6612 /* If the stream is disconnect or closed, ldo nothing. */
6613 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6614 goto out;
6615
6616 /* Check if the input buffer is avalaible. */
6617 if (!b_size(&res->buf)) {
6618 si_rx_room_blk(si);
6619 goto out;
6620 }
6621 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006622 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006623 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6624
6625 /* Set the currently running flag. */
6626 if (!HLUA_IS_RUNNING(hlua) &&
6627 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6628 struct htx_blk *blk;
6629 size_t count = co_data(req);
6630
6631 if (!count) {
6632 si_cant_get(si);
6633 goto out;
6634 }
6635
6636 /* We need to flush the request header. This left the body for
6637 * the Lua.
6638 */
6639 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02006640 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006641 while (count && blk) {
6642 enum htx_blk_type type = htx_get_blk_type(blk);
6643 uint32_t sz = htx_get_blksz(blk);
6644
6645 if (sz > count) {
6646 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006647 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006648 goto out;
6649 }
6650
6651 count -= sz;
6652 co_set_data(req, co_data(req) - sz);
6653 blk = htx_remove_blk(req_htx, blk);
6654
6655 if (type == HTX_BLK_EOH)
6656 break;
6657 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006658 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006659 }
6660
6661 /* Executes The applet if it is not done. */
6662 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6663
6664 /* Execute the function. */
6665 switch (hlua_ctx_resume(hlua, 1)) {
6666 /* finished. */
6667 case HLUA_E_OK:
6668 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6669 break;
6670
6671 /* yield. */
6672 case HLUA_E_AGAIN:
6673 if (hlua->wake_time != TICK_ETERNITY)
6674 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006675 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006676
6677 /* finished with error. */
6678 case HLUA_E_ERRMSG:
6679 /* Display log. */
6680 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6681 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6682 lua_pop(hlua->T, 1);
6683 goto error;
6684
6685 case HLUA_E_ETMOUT:
6686 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6687 rule->arg.hlua_rule->fcn.name);
6688 goto error;
6689
6690 case HLUA_E_NOMEM:
6691 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6692 rule->arg.hlua_rule->fcn.name);
6693 goto error;
6694
6695 case HLUA_E_YIELD: /* unexpected */
6696 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6697 rule->arg.hlua_rule->fcn.name);
6698 goto error;
6699
6700 case HLUA_E_ERR:
6701 /* Display log. */
6702 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6703 rule->arg.hlua_rule->fcn.name);
6704 goto error;
6705
6706 default:
6707 goto error;
6708 }
6709 }
6710
6711 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006712 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
6713 goto done;
6714
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006715 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
6716 goto error;
6717
Christopher Faulet54b5e212019-06-04 10:08:28 +02006718 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006719 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
6720 si_rx_room_blk(si);
6721 goto out;
6722 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006723 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006724 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6725 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006726 }
6727
6728 done:
6729 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006730 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006731 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006732 si_shutr(si);
6733 }
6734
6735 /* eat the whole request */
6736 if (co_data(req)) {
6737 req_htx = htx_from_buf(&req->buf);
6738 co_htx_skip(req, req_htx, co_data(req));
6739 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006740 }
6741 }
6742
6743 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006744 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006745 return;
6746
6747 error:
6748
6749 /* If we are in HTTP mode, and we are not send any
6750 * data, return a 500 server error in best effort:
6751 * if there is no room available in the buffer,
6752 * just close the connection.
6753 */
6754 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02006755 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006756
6757 channel_erase(res);
6758 res->buf.data = b_data(err);
6759 memcpy(res->buf.area, b_head(err), b_data(err));
6760 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006761 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006762 }
6763 if (!(strm->flags & SF_ERR_MASK))
6764 strm->flags |= SF_ERR_RESOURCE;
6765 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6766 goto done;
6767}
6768
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006769static void hlua_applet_http_release(struct appctx *ctx)
6770{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006771 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006772 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006773 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006774 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006775}
6776
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006777/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6778 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006779 *
6780 * This function can fail with an abort() due to an Lua critical error.
6781 * We are in the configuration parsing process of HAProxy, this abort() is
6782 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006783 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006784static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6785 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006786{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006787 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006788 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006789
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006790 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006791 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006792 if (!rule->arg.hlua_rule) {
6793 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006794 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006795 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006796
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006797 /* Memory for arguments. */
6798 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6799 if (!rule->arg.hlua_rule->args) {
6800 memprintf(err, "out of memory error");
6801 return ACT_RET_PRS_ERR;
6802 }
6803
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006804 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006805 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006806
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006807 /* Expect some arguments */
6808 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006809 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006810 memprintf(err, "expect %d arguments", fcn->nargs);
6811 return ACT_RET_PRS_ERR;
6812 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006813 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006814 if (!rule->arg.hlua_rule->args[i]) {
6815 memprintf(err, "out of memory error");
6816 return ACT_RET_PRS_ERR;
6817 }
6818 (*cur_arg)++;
6819 }
6820 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006821
Thierry FOURNIER42148732015-09-02 17:17:33 +02006822 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006823 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006824 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006825}
6826
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006827static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6828 struct act_rule *rule, char **err)
6829{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006830 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006831
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006832 /* HTTP applets are forbidden in tcp-request rules.
6833 * HTTP applet request requires everything initilized by
6834 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6835 * The applet will be immediately initilized, but its before
6836 * the call of this analyzer.
6837 */
6838 if (rule->from != ACT_F_HTTP_REQ) {
6839 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6840 return ACT_RET_PRS_ERR;
6841 }
6842
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006843 /* Memory for the rule. */
6844 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6845 if (!rule->arg.hlua_rule) {
6846 memprintf(err, "out of memory error");
6847 return ACT_RET_PRS_ERR;
6848 }
6849
6850 /* Reference the Lua function and store the reference. */
6851 rule->arg.hlua_rule->fcn = *fcn;
6852
6853 /* TODO: later accept arguments. */
6854 rule->arg.hlua_rule->args = NULL;
6855
6856 /* Add applet pointer in the rule. */
6857 rule->applet.obj_type = OBJ_TYPE_APPLET;
6858 rule->applet.name = fcn->name;
6859 rule->applet.init = hlua_applet_http_init;
6860 rule->applet.fct = hlua_applet_http_fct;
6861 rule->applet.release = hlua_applet_http_release;
6862 rule->applet.timeout = hlua_timeout_applet;
6863
6864 return ACT_RET_PRS_OK;
6865}
6866
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006867/* This function is an LUA binding used for registering
6868 * "sample-conv" functions. It expects a converter name used
6869 * in the haproxy configuration file, and an LUA function.
6870 */
6871__LJMP static int hlua_register_action(lua_State *L)
6872{
6873 struct action_kw_list *akl;
6874 const char *name;
6875 int ref;
6876 int len;
6877 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006878 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006879
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006880 /* Initialise the number of expected arguments at 0. */
6881 nargs = 0;
6882
6883 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6884 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006885
6886 /* First argument : converter name. */
6887 name = MAY_LJMP(luaL_checkstring(L, 1));
6888
6889 /* Second argument : environment. */
6890 if (lua_type(L, 2) != LUA_TTABLE)
6891 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6892
6893 /* Third argument : lua function. */
6894 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6895
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006896 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006897 if (lua_gettop(L) >= 4)
6898 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6899
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006900 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006901 lua_pushnil(L);
6902 while (lua_next(L, 2) != 0) {
6903 if (lua_type(L, -1) != LUA_TSTRING)
6904 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6905
6906 /* Check required environment. Only accepted "http" or "tcp". */
6907 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006908 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006909 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006910 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006911 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006912 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006913 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006914
6915 /* Fill fcn. */
6916 fcn->name = strdup(name);
6917 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006918 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006919 fcn->function_ref = ref;
6920
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006921 /* Set the expected number od arguments. */
6922 fcn->nargs = nargs;
6923
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006924 /* List head */
6925 akl->list.n = akl->list.p = NULL;
6926
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006927 /* action keyword. */
6928 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006929 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006930 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006931 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006932
6933 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6934
6935 akl->kw[0].match_pfx = 0;
6936 akl->kw[0].private = fcn;
6937 akl->kw[0].parse = action_register_lua;
6938
6939 /* select the action registering point. */
6940 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6941 tcp_req_cont_keywords_register(akl);
6942 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6943 tcp_res_cont_keywords_register(akl);
6944 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6945 http_req_keywords_register(akl);
6946 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6947 http_res_keywords_register(akl);
6948 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006949 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006950 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6951 "are expected.", lua_tostring(L, -1)));
6952
6953 /* pop the environment string. */
6954 lua_pop(L, 1);
6955 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006956 return ACT_RET_PRS_OK;
6957}
6958
6959static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6960 struct act_rule *rule, char **err)
6961{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006962 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006963
Christopher Faulet280f85b2019-07-15 15:02:04 +02006964 if (px->mode == PR_MODE_HTTP) {
6965 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01006966 return ACT_RET_PRS_ERR;
6967 }
6968
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006969 /* Memory for the rule. */
6970 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6971 if (!rule->arg.hlua_rule) {
6972 memprintf(err, "out of memory error");
6973 return ACT_RET_PRS_ERR;
6974 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006975
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006976 /* Reference the Lua function and store the reference. */
6977 rule->arg.hlua_rule->fcn = *fcn;
6978
6979 /* TODO: later accept arguments. */
6980 rule->arg.hlua_rule->args = NULL;
6981
6982 /* Add applet pointer in the rule. */
6983 rule->applet.obj_type = OBJ_TYPE_APPLET;
6984 rule->applet.name = fcn->name;
6985 rule->applet.init = hlua_applet_tcp_init;
6986 rule->applet.fct = hlua_applet_tcp_fct;
6987 rule->applet.release = hlua_applet_tcp_release;
6988 rule->applet.timeout = hlua_timeout_applet;
6989
6990 return 0;
6991}
6992
6993/* This function is an LUA binding used for registering
6994 * "sample-conv" functions. It expects a converter name used
6995 * in the haproxy configuration file, and an LUA function.
6996 */
6997__LJMP static int hlua_register_service(lua_State *L)
6998{
6999 struct action_kw_list *akl;
7000 const char *name;
7001 const char *env;
7002 int ref;
7003 int len;
7004 struct hlua_function *fcn;
7005
7006 MAY_LJMP(check_args(L, 3, "register_service"));
7007
7008 /* First argument : converter name. */
7009 name = MAY_LJMP(luaL_checkstring(L, 1));
7010
7011 /* Second argument : environment. */
7012 env = MAY_LJMP(luaL_checkstring(L, 2));
7013
7014 /* Third argument : lua function. */
7015 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7016
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007017 /* Allocate and fill the sample fetch keyword struct. */
7018 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7019 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007020 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007021 fcn = calloc(1, sizeof(*fcn));
7022 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007023 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007024
7025 /* Fill fcn. */
7026 len = strlen("<lua.>") + strlen(name) + 1;
7027 fcn->name = calloc(1, len);
7028 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007029 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007030 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7031 fcn->function_ref = ref;
7032
7033 /* List head */
7034 akl->list.n = akl->list.p = NULL;
7035
7036 /* converter keyword. */
7037 len = strlen("lua.") + strlen(name) + 1;
7038 akl->kw[0].kw = calloc(1, len);
7039 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007040 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007041
7042 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7043
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007044 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007045 if (strcmp(env, "tcp") == 0)
7046 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007047 else if (strcmp(env, "http") == 0)
7048 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007049 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007050 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007051 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007052
7053 akl->kw[0].match_pfx = 0;
7054 akl->kw[0].private = fcn;
7055
7056 /* End of array. */
7057 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7058
7059 /* Register this new converter */
7060 service_keywords_register(akl);
7061
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007062 return 0;
7063}
7064
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007065/* This function initialises Lua cli handler. It copies the
7066 * arguments in the Lua stack and create channel IO objects.
7067 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007068static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007069{
7070 struct hlua *hlua;
7071 struct hlua_function *fcn;
7072 int i;
7073 const char *error;
7074
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007075 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007076 appctx->ctx.hlua_cli.fcn = private;
7077
Willy Tarreaubafbe012017-11-24 17:34:44 +01007078 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007079 if (!hlua) {
7080 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007081 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007082 }
7083 HLUA_INIT(hlua);
7084 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007085
7086 /* Create task used by signal to wakeup applets.
7087 * We use the same wakeup fonction than the Lua applet_tcp and
7088 * applet_http. It is absolutely compatible.
7089 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007090 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007091 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007092 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007093 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007094 }
7095 appctx->ctx.hlua_cli.task->nice = 0;
7096 appctx->ctx.hlua_cli.task->context = appctx;
7097 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7098
7099 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007100 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007101 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007102 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007103 }
7104
7105 /* The following Lua calls can fail. */
7106 if (!SET_SAFE_LJMP(hlua->T)) {
7107 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7108 error = lua_tostring(hlua->T, -1);
7109 else
7110 error = "critical error";
7111 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7112 goto error;
7113 }
7114
7115 /* Check stack available size. */
7116 if (!lua_checkstack(hlua->T, 2)) {
7117 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7118 goto error;
7119 }
7120
7121 /* Restore the function in the stack. */
7122 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7123
7124 /* Once the arguments parsed, the CLI is like an AppletTCP,
7125 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007126 */
7127 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7128 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7129 goto error;
7130 }
7131 hlua->nargs = 1;
7132
7133 /* push keywords in the stack. */
7134 for (i = 0; *args[i]; i++) {
7135 /* Check stack available size. */
7136 if (!lua_checkstack(hlua->T, 1)) {
7137 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7138 goto error;
7139 }
7140 lua_pushstring(hlua->T, args[i]);
7141 hlua->nargs++;
7142 }
7143
7144 /* We must initialize the execution timeouts. */
7145 hlua->max_time = hlua_timeout_session;
7146
7147 /* At this point the execution is safe. */
7148 RESET_SAFE_LJMP(hlua->T);
7149
7150 /* It's ok */
7151 return 0;
7152
7153 /* It's not ok. */
7154error:
7155 RESET_SAFE_LJMP(hlua->T);
7156 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007157 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007158 return 1;
7159}
7160
7161static int hlua_cli_io_handler_fct(struct appctx *appctx)
7162{
7163 struct hlua *hlua;
7164 struct stream_interface *si;
7165 struct hlua_function *fcn;
7166
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007167 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007168 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007169 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007170
7171 /* If the stream is disconnect or closed, ldo nothing. */
7172 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7173 return 1;
7174
7175 /* Execute the function. */
7176 switch (hlua_ctx_resume(hlua, 1)) {
7177
7178 /* finished. */
7179 case HLUA_E_OK:
7180 return 1;
7181
7182 /* yield. */
7183 case HLUA_E_AGAIN:
7184 /* We want write. */
7185 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007186 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007187 /* Set the timeout. */
7188 if (hlua->wake_time != TICK_ETERNITY)
7189 task_schedule(hlua->task, hlua->wake_time);
7190 return 0;
7191
7192 /* finished with error. */
7193 case HLUA_E_ERRMSG:
7194 /* Display log. */
7195 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7196 fcn->name, lua_tostring(hlua->T, -1));
7197 lua_pop(hlua->T, 1);
7198 return 1;
7199
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007200 case HLUA_E_ETMOUT:
7201 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7202 fcn->name);
7203 return 1;
7204
7205 case HLUA_E_NOMEM:
7206 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7207 fcn->name);
7208 return 1;
7209
7210 case HLUA_E_YIELD: /* unexpected */
7211 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7212 fcn->name);
7213 return 1;
7214
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007215 case HLUA_E_ERR:
7216 /* Display log. */
7217 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7218 fcn->name);
7219 return 1;
7220
7221 default:
7222 return 1;
7223 }
7224
7225 return 1;
7226}
7227
7228static void hlua_cli_io_release_fct(struct appctx *appctx)
7229{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007230 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007231 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007232}
7233
7234/* This function is an LUA binding used for registering
7235 * new keywords in the cli. It expects a list of keywords
7236 * which are the "path". It is limited to 5 keywords. A
7237 * description of the command, a function to be executed
7238 * for the parsing and a function for io handlers.
7239 */
7240__LJMP static int hlua_register_cli(lua_State *L)
7241{
7242 struct cli_kw_list *cli_kws;
7243 const char *message;
7244 int ref_io;
7245 int len;
7246 struct hlua_function *fcn;
7247 int index;
7248 int i;
7249
7250 MAY_LJMP(check_args(L, 3, "register_cli"));
7251
7252 /* First argument : an array of maximum 5 keywords. */
7253 if (!lua_istable(L, 1))
7254 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7255
7256 /* Second argument : string with contextual message. */
7257 message = MAY_LJMP(luaL_checkstring(L, 2));
7258
7259 /* Third and fourth argument : lua function. */
7260 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7261
7262 /* Allocate and fill the sample fetch keyword struct. */
7263 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7264 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007265 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007266 fcn = calloc(1, sizeof(*fcn));
7267 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007268 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007269
7270 /* Fill path. */
7271 index = 0;
7272 lua_pushnil(L);
7273 while(lua_next(L, 1) != 0) {
7274 if (index >= 5)
7275 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7276 if (lua_type(L, -1) != LUA_TSTRING)
7277 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7278 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7279 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007280 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007281 index++;
7282 lua_pop(L, 1);
7283 }
7284
7285 /* Copy help message. */
7286 cli_kws->kw[0].usage = strdup(message);
7287 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007288 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007289
7290 /* Fill fcn io handler. */
7291 len = strlen("<lua.cli>") + 1;
7292 for (i = 0; i < index; i++)
7293 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7294 fcn->name = calloc(1, len);
7295 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007296 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007297 strncat((char *)fcn->name, "<lua.cli", len);
7298 for (i = 0; i < index; i++) {
7299 strncat((char *)fcn->name, ".", len);
7300 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7301 }
7302 strncat((char *)fcn->name, ">", len);
7303 fcn->function_ref = ref_io;
7304
7305 /* Fill last entries. */
7306 cli_kws->kw[0].private = fcn;
7307 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7308 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7309 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7310
7311 /* Register this new converter */
7312 cli_register_kw(cli_kws);
7313
7314 return 0;
7315}
7316
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007317static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7318 struct proxy *defpx, const char *file, int line,
7319 char **err, unsigned int *timeout)
7320{
7321 const char *error;
7322
7323 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007324 if (error == PARSE_TIME_OVER) {
7325 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7326 args[1], args[0]);
7327 return -1;
7328 }
7329 else if (error == PARSE_TIME_UNDER) {
7330 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7331 args[1], args[0]);
7332 return -1;
7333 }
7334 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007335 memprintf(err, "%s: invalid timeout", args[0]);
7336 return -1;
7337 }
7338 return 0;
7339}
7340
7341static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7342 struct proxy *defpx, const char *file, int line,
7343 char **err)
7344{
7345 return hlua_read_timeout(args, section_type, curpx, defpx,
7346 file, line, err, &hlua_timeout_session);
7347}
7348
7349static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7350 struct proxy *defpx, const char *file, int line,
7351 char **err)
7352{
7353 return hlua_read_timeout(args, section_type, curpx, defpx,
7354 file, line, err, &hlua_timeout_task);
7355}
7356
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007357static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7358 struct proxy *defpx, const char *file, int line,
7359 char **err)
7360{
7361 return hlua_read_timeout(args, section_type, curpx, defpx,
7362 file, line, err, &hlua_timeout_applet);
7363}
7364
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007365static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7366 struct proxy *defpx, const char *file, int line,
7367 char **err)
7368{
7369 char *error;
7370
7371 hlua_nb_instruction = strtoll(args[1], &error, 10);
7372 if (*error != '\0') {
7373 memprintf(err, "%s: invalid number", args[0]);
7374 return -1;
7375 }
7376 return 0;
7377}
7378
Willy Tarreau32f61e22015-03-18 17:54:59 +01007379static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7380 struct proxy *defpx, const char *file, int line,
7381 char **err)
7382{
7383 char *error;
7384
7385 if (*(args[1]) == 0) {
7386 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7387 return -1;
7388 }
7389 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7390 if (*error != '\0') {
7391 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7392 return -1;
7393 }
7394 return 0;
7395}
7396
7397
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007398/* This function is called by the main configuration key "lua-load". It loads and
7399 * execute an lua file during the parsing of the HAProxy configuration file. It is
7400 * the main lua entry point.
7401 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007402 * This function runs with the HAProxy keywords API. It returns -1 if an error
7403 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007404 *
7405 * In some error case, LUA set an error message in top of the stack. This function
7406 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007407 *
7408 * This function can fail with an abort() due to an Lua critical error.
7409 * We are in the configuration parsing process of HAProxy, this abort() is
7410 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007411 */
7412static int hlua_load(char **args, int section_type, struct proxy *curpx,
7413 struct proxy *defpx, const char *file, int line,
7414 char **err)
7415{
7416 int error;
7417
7418 /* Just load and compile the file. */
7419 error = luaL_loadfile(gL.T, args[1]);
7420 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007421 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007422 lua_pop(gL.T, 1);
7423 return -1;
7424 }
7425
7426 /* If no syntax error where detected, execute the code. */
7427 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7428 switch (error) {
7429 case LUA_OK:
7430 break;
7431 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007432 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007433 lua_pop(gL.T, 1);
7434 return -1;
7435 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007436 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007437 return -1;
7438 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007439 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007440 lua_pop(gL.T, 1);
7441 return -1;
7442 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007443 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007444 lua_pop(gL.T, 1);
7445 return -1;
7446 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007447 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007448 lua_pop(gL.T, 1);
7449 return -1;
7450 }
7451
7452 return 0;
7453}
7454
7455/* configuration keywords declaration */
7456static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007457 { CFG_GLOBAL, "lua-load", hlua_load },
7458 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7459 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007460 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007461 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007462 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007463 { 0, NULL, NULL },
7464}};
7465
Willy Tarreau0108d902018-11-25 19:14:37 +01007466INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7467
Christopher Fauletafd8f102018-11-08 11:34:21 +01007468
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007469/* This function can fail with an abort() due to an Lua critical error.
7470 * We are in the initialisation process of HAProxy, this abort() is
7471 * tolerated.
7472 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007473int hlua_post_init()
7474{
7475 struct hlua_init_function *init;
7476 const char *msg;
7477 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007478 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007479
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007480 /* Call post initialisation function in safe environement. */
7481 if (!SET_SAFE_LJMP(gL.T)) {
7482 if (lua_type(gL.T, -1) == LUA_TSTRING)
7483 error = lua_tostring(gL.T, -1);
7484 else
7485 error = "critical error";
7486 fprintf(stderr, "Lua post-init: %s.\n", error);
7487 exit(1);
7488 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007489
7490#if USE_OPENSSL
7491 /* Initialize SSL server. */
7492 if (socket_ssl.xprt->prepare_srv) {
7493 int saved_used_backed = global.ssl_used_backend;
7494 // don't affect maxconn automatic computation
7495 socket_ssl.xprt->prepare_srv(&socket_ssl);
7496 global.ssl_used_backend = saved_used_backed;
7497 }
7498#endif
7499
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007500 hlua_fcn_post_init(gL.T);
7501 RESET_SAFE_LJMP(gL.T);
7502
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007503 list_for_each_entry(init, &hlua_init_functions, l) {
7504 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7505 ret = hlua_ctx_resume(&gL, 0);
7506 switch (ret) {
7507 case HLUA_E_OK:
7508 lua_pop(gL.T, -1);
7509 return 1;
7510 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007511 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007512 return 0;
7513 case HLUA_E_ERRMSG:
7514 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007515 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007516 return 0;
7517 case HLUA_E_ERR:
7518 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007519 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007520 return 0;
7521 }
7522 }
7523 return 1;
7524}
7525
Willy Tarreau32f61e22015-03-18 17:54:59 +01007526/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7527 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7528 * is the previously allocated size or the kind of object in case of a new
7529 * allocation. <nsize> is the requested new size.
7530 */
7531static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7532{
7533 struct hlua_mem_allocator *zone = ud;
7534
7535 if (nsize == 0) {
7536 /* it's a free */
7537 if (ptr)
7538 zone->allocated -= osize;
7539 free(ptr);
7540 return NULL;
7541 }
7542
7543 if (!ptr) {
7544 /* it's a new allocation */
7545 if (zone->limit && zone->allocated + nsize > zone->limit)
7546 return NULL;
7547
7548 ptr = malloc(nsize);
7549 if (ptr)
7550 zone->allocated += nsize;
7551 return ptr;
7552 }
7553
7554 /* it's a realloc */
7555 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7556 return NULL;
7557
7558 ptr = realloc(ptr, nsize);
7559 if (ptr)
7560 zone->allocated += nsize - osize;
7561 return ptr;
7562}
7563
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007564/* Ithis function can fail with an abort() due to an Lua critical error.
7565 * We are in the initialisation process of HAProxy, this abort() is
7566 * tolerated.
7567 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007568void hlua_init(void)
7569{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007570 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007571 int idx;
7572 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007573 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007574 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007575 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007576#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007577 struct srv_kw *kw;
7578 int tmp_error;
7579 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007580 char *args[] = { /* SSL client configuration. */
7581 "ssl",
7582 "verify",
7583 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007584 NULL
7585 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007586#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007587
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007588 /* Init main lua stack. */
7589 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007590 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007591 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007592 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007593 hlua_sethlua(&gL);
7594 gL.Tref = LUA_REFNIL;
7595 gL.task = NULL;
7596
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007597 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007598 * the Lua function can fail with an abort. We are in the initialisation
7599 * process of HAProxy, this abort() is tolerated.
7600 */
7601
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007602 /* Initialise lua. */
7603 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007604
Thierry Fournier75933d42016-01-21 09:30:18 +01007605 /* Set safe environment for the initialisation. */
7606 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007607 if (lua_type(gL.T, -1) == LUA_TSTRING)
7608 error_msg = lua_tostring(gL.T, -1);
7609 else
7610 error_msg = "critical error";
7611 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007612 exit(1);
7613 }
7614
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007615 /*
7616 *
7617 * Create "core" object.
7618 *
7619 */
7620
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007621 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007622 lua_newtable(gL.T);
7623
7624 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007625 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007626 hlua_class_const_int(gL.T, log_levels[i], i);
7627
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007628 /* Register special functions. */
7629 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007630 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007631 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007632 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007633 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007634 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007635 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007636 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007637 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007638 hlua_class_function(gL.T, "sleep", hlua_sleep);
7639 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007640 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7641 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7642 hlua_class_function(gL.T, "set_map", hlua_set_map);
7643 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007644 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007645 hlua_class_function(gL.T, "log", hlua_log);
7646 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7647 hlua_class_function(gL.T, "Info", hlua_log_info);
7648 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7649 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007650 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007651 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007652
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007653 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007654
7655 /*
7656 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007657 * Register class Map
7658 *
7659 */
7660
7661 /* This table entry is the object "Map" base. */
7662 lua_newtable(gL.T);
7663
7664 /* register pattern types. */
7665 for (i=0; i<PAT_MATCH_NUM; i++)
7666 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007667 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007668 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7669 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007670 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007671
7672 /* register constructor. */
7673 hlua_class_function(gL.T, "new", hlua_map_new);
7674
7675 /* Create and fill the metatable. */
7676 lua_newtable(gL.T);
7677
7678 /* Create and fille the __index entry. */
7679 lua_pushstring(gL.T, "__index");
7680 lua_newtable(gL.T);
7681
7682 /* Register . */
7683 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7684 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7685
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007686 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007687
Thierry Fournier45e78d72016-02-19 18:34:46 +01007688 /* Register previous table in the registry with reference and named entry.
7689 * The function hlua_register_metatable() pops the stack, so we
7690 * previously create a copy of the table.
7691 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007692 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007693 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007694
7695 /* Assign the metatable to the mai Map object. */
7696 lua_setmetatable(gL.T, -2);
7697
7698 /* Set a name to the table. */
7699 lua_setglobal(gL.T, "Map");
7700
7701 /*
7702 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007703 * Register class Channel
7704 *
7705 */
7706
7707 /* Create and fill the metatable. */
7708 lua_newtable(gL.T);
7709
7710 /* Create and fille the __index entry. */
7711 lua_pushstring(gL.T, "__index");
7712 lua_newtable(gL.T);
7713
7714 /* Register . */
7715 hlua_class_function(gL.T, "get", hlua_channel_get);
7716 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7717 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7718 hlua_class_function(gL.T, "set", hlua_channel_set);
7719 hlua_class_function(gL.T, "append", hlua_channel_append);
7720 hlua_class_function(gL.T, "send", hlua_channel_send);
7721 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7722 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7723 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007724 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007725
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007726 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007727
7728 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007729 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007730
7731 /*
7732 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007733 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007734 *
7735 */
7736
7737 /* Create and fill the metatable. */
7738 lua_newtable(gL.T);
7739
7740 /* Create and fille the __index entry. */
7741 lua_pushstring(gL.T, "__index");
7742 lua_newtable(gL.T);
7743
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007744 /* Browse existing fetches and create the associated
7745 * object method.
7746 */
7747 sf = NULL;
7748 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7749
7750 /* Dont register the keywork if the arguments check function are
7751 * not safe during the runtime.
7752 */
7753 if ((sf->val_args != NULL) &&
7754 (sf->val_args != val_payload_lv) &&
7755 (sf->val_args != val_hdr))
7756 continue;
7757
7758 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7759 * by an underscore.
7760 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007761 strncpy(trash.area, sf->kw, trash.size);
7762 trash.area[trash.size - 1] = '\0';
7763 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007764 if (*p == '.' || *p == '-' || *p == '+')
7765 *p = '_';
7766
7767 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007768 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007769 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007770 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007771 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007772 }
7773
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007774 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007775
7776 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007777 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007778
7779 /*
7780 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007781 * Register class Converters
7782 *
7783 */
7784
7785 /* Create and fill the metatable. */
7786 lua_newtable(gL.T);
7787
7788 /* Create and fill the __index entry. */
7789 lua_pushstring(gL.T, "__index");
7790 lua_newtable(gL.T);
7791
7792 /* Browse existing converters and create the associated
7793 * object method.
7794 */
7795 sc = NULL;
7796 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7797 /* Dont register the keywork if the arguments check function are
7798 * not safe during the runtime.
7799 */
7800 if (sc->val_args != NULL)
7801 continue;
7802
7803 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7804 * by an underscore.
7805 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007806 strncpy(trash.area, sc->kw, trash.size);
7807 trash.area[trash.size - 1] = '\0';
7808 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007809 if (*p == '.' || *p == '-' || *p == '+')
7810 *p = '_';
7811
7812 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007813 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007814 lua_pushlightuserdata(gL.T, sc);
7815 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007816 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007817 }
7818
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007819 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007820
7821 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007822 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007823
7824 /*
7825 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007826 * Register class HTTP
7827 *
7828 */
7829
7830 /* Create and fill the metatable. */
7831 lua_newtable(gL.T);
7832
7833 /* Create and fille the __index entry. */
7834 lua_pushstring(gL.T, "__index");
7835 lua_newtable(gL.T);
7836
7837 /* Register Lua functions. */
7838 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7839 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7840 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7841 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7842 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7843 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7844 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7845 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7846 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7847 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7848
7849 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7850 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7851 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7852 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7853 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7854 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007855 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007856
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007857 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007858
7859 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007860 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007861
7862 /*
7863 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007864 * Register class AppletTCP
7865 *
7866 */
7867
7868 /* Create and fill the metatable. */
7869 lua_newtable(gL.T);
7870
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007871 /* Create and fille the __index entry. */
7872 lua_pushstring(gL.T, "__index");
7873 lua_newtable(gL.T);
7874
7875 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007876 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7877 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7878 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7879 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7880 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007881 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7882 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7883 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007884
7885 lua_settable(gL.T, -3);
7886
7887 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007888 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007889
7890 /*
7891 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007892 * Register class AppletHTTP
7893 *
7894 */
7895
7896 /* Create and fill the metatable. */
7897 lua_newtable(gL.T);
7898
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007899 /* Create and fille the __index entry. */
7900 lua_pushstring(gL.T, "__index");
7901 lua_newtable(gL.T);
7902
7903 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007904 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7905 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007906 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7907 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7908 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007909 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7910 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7911 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7912 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7913 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7914 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7915
7916 lua_settable(gL.T, -3);
7917
7918 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007919 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007920
7921 /*
7922 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007923 * Register class TXN
7924 *
7925 */
7926
7927 /* Create and fill the metatable. */
7928 lua_newtable(gL.T);
7929
7930 /* Create and fille the __index entry. */
7931 lua_pushstring(gL.T, "__index");
7932 lua_newtable(gL.T);
7933
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007934 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04007935 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7936 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
7937 hlua_class_function(gL.T, "set_var", hlua_set_var);
7938 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
7939 hlua_class_function(gL.T, "get_var", hlua_get_var);
7940 hlua_class_function(gL.T, "done", hlua_txn_done);
7941 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
7942 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7943 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
7944 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
7945 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
7946 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7947 hlua_class_function(gL.T, "log", hlua_txn_log);
7948 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7949 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7950 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7951 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007952
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007953 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007954
7955 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007956 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007957
7958 /*
7959 *
7960 * Register class Socket
7961 *
7962 */
7963
7964 /* Create and fill the metatable. */
7965 lua_newtable(gL.T);
7966
7967 /* Create and fille the __index entry. */
7968 lua_pushstring(gL.T, "__index");
7969 lua_newtable(gL.T);
7970
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007971#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007972 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007973#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007974 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7975 hlua_class_function(gL.T, "send", hlua_socket_send);
7976 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7977 hlua_class_function(gL.T, "close", hlua_socket_close);
7978 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7979 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7980 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7981 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7982
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007983 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007984
7985 /* Register the garbage collector entry. */
7986 lua_pushstring(gL.T, "__gc");
7987 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007988 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007989
7990 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007991 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007992
7993 /* Proxy and server configuration initialisation. */
7994 memset(&socket_proxy, 0, sizeof(socket_proxy));
7995 init_new_proxy(&socket_proxy);
7996 socket_proxy.parent = NULL;
7997 socket_proxy.last_change = now.tv_sec;
7998 socket_proxy.id = "LUA-SOCKET";
7999 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8000 socket_proxy.maxconn = 0;
8001 socket_proxy.accept = NULL;
8002 socket_proxy.options2 |= PR_O2_INDEPSTR;
8003 socket_proxy.srv = NULL;
8004 socket_proxy.conn_retries = 0;
8005 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8006
8007 /* Init TCP server: unchanged parameters */
8008 memset(&socket_tcp, 0, sizeof(socket_tcp));
8009 socket_tcp.next = NULL;
8010 socket_tcp.proxy = &socket_proxy;
8011 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8012 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008013 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008014 socket_tcp.priv_conns = NULL;
8015 socket_tcp.idle_conns = NULL;
8016 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008017 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008018 socket_tcp.last_change = 0;
8019 socket_tcp.id = "LUA-TCP-CONN";
8020 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8021 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8022 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8023
8024 /* XXX: Copy default parameter from default server,
8025 * but the default server is not initialized.
8026 */
8027 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8028 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8029 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8030 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8031 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8032 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8033 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8034 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8035 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8036 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8037
8038 socket_tcp.check.status = HCHK_STATUS_INI;
8039 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8040 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8041 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8042 socket_tcp.check.server = &socket_tcp;
8043
8044 socket_tcp.agent.status = HCHK_STATUS_INI;
8045 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8046 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8047 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8048 socket_tcp.agent.server = &socket_tcp;
8049
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008050 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008051
8052#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008053 /* Init TCP server: unchanged parameters */
8054 memset(&socket_ssl, 0, sizeof(socket_ssl));
8055 socket_ssl.next = NULL;
8056 socket_ssl.proxy = &socket_proxy;
8057 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8058 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008059 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008060 socket_ssl.priv_conns = NULL;
8061 socket_ssl.idle_conns = NULL;
8062 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008063 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008064 socket_ssl.last_change = 0;
8065 socket_ssl.id = "LUA-SSL-CONN";
8066 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8067 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8068 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8069
8070 /* XXX: Copy default parameter from default server,
8071 * but the default server is not initialized.
8072 */
8073 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8074 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8075 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8076 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8077 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8078 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8079 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8080 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8081 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8082 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8083
8084 socket_ssl.check.status = HCHK_STATUS_INI;
8085 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8086 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8087 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8088 socket_ssl.check.server = &socket_ssl;
8089
8090 socket_ssl.agent.status = HCHK_STATUS_INI;
8091 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8092 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8093 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8094 socket_ssl.agent.server = &socket_ssl;
8095
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008096 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008097 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008098
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008099 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008100 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8101 /*
8102 *
8103 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008104 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008105 * features like client certificates and ssl_verify.
8106 *
8107 */
8108 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8109 if (tmp_error != 0) {
8110 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8111 abort(); /* This must be never arrives because the command line
8112 not editable by the user. */
8113 }
8114 idx += kw->skip;
8115 }
8116 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008117#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008118
8119 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008120}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008121
Willy Tarreau80713382018-11-26 10:19:54 +01008122static void hlua_register_build_options(void)
8123{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008124 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008125
Willy Tarreaubb57d942016-12-21 19:04:56 +01008126 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8127 hap_register_build_opts(ptr, 1);
8128}
Willy Tarreau80713382018-11-26 10:19:54 +01008129
8130INITCALL0(STG_REGISTER, hlua_register_build_options);