blob: 742acc7062b77df62cdef5851c14c7ed2c9b6bdf [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;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200422 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200423 /* We don't know the actual size of the underlying allocation, so be conservative. */
424 arg->data.str.size = arg->data.str.data;
425 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100426 break;
427
428 case LUA_TUSERDATA:
429 case LUA_TNIL:
430 case LUA_TTABLE:
431 case LUA_TFUNCTION:
432 case LUA_TTHREAD:
433 case LUA_TLIGHTUSERDATA:
434 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200435 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100436 break;
437 }
438 return 1;
439}
440
441/* the following functions are used to convert a struct sample
442 * in Lua type. This useful to convert the return of the
443 * fetchs or converters.
444 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100445static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200447 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200450 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100451 break;
452
453 case SMP_T_BIN:
454 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200455 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456 break;
457
458 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200459 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100460 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
461 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
462 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
463 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
464 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
465 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
466 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
467 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
468 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200469 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100470 break;
471 default:
472 lua_pushnil(L);
473 break;
474 }
475 break;
476
477 case SMP_T_IPV4:
478 case SMP_T_IPV6:
479 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200480 if (sample_casts[smp->data.type][SMP_T_STR] &&
481 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200482 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100483 else
484 lua_pushnil(L);
485 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100486 default:
487 lua_pushnil(L);
488 break;
489 }
490 return 1;
491}
492
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100493/* the following functions are used to convert a struct sample
494 * in Lua strings. This is useful to convert the return of the
495 * fetchs or converters.
496 */
497static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
498{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200499 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100500
501 case SMP_T_BIN:
502 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100504 break;
505
506 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200507 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100508 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
509 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
510 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
511 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
512 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
513 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
514 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
515 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
516 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200517 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100518 break;
519 default:
520 lua_pushstring(L, "");
521 break;
522 }
523 break;
524
525 case SMP_T_SINT:
526 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100527 case SMP_T_IPV4:
528 case SMP_T_IPV6:
529 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200530 if (sample_casts[smp->data.type][SMP_T_STR] &&
531 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200532 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100533 else
534 lua_pushstring(L, "");
535 break;
536 default:
537 lua_pushstring(L, "");
538 break;
539 }
540 return 1;
541}
542
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100543/* the following functions are used to convert an Lua type in a
544 * struct sample. This is useful to provide data from a converter
545 * to the LUA code.
546 */
547static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
548{
549 switch (lua_type(L, ud)) {
550
551 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200552 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200553 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100554 break;
555
556
557 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200558 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100560 break;
561
562 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200563 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200565 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200566 /* We don't know the actual size of the underlying allocation, so be conservative. */
567 smp->data.u.str.size = smp->data.u.str.data;
568 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100569 break;
570
571 case LUA_TUSERDATA:
572 case LUA_TNIL:
573 case LUA_TTABLE:
574 case LUA_TFUNCTION:
575 case LUA_TTHREAD:
576 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200577 case LUA_TNONE:
578 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200579 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200580 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100581 break;
582 }
583 return 1;
584}
585
586/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800587 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100588 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100589 *
590 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
591 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100593__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100594 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595{
596 int min_arg;
597 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100598 struct proxy *px;
599 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100600
601 idx = 0;
602 min_arg = ARGM(mask);
603 mask >>= ARGM_BITS;
604
605 while (1) {
606
607 /* Check oversize. */
608 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100609 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610 }
611
612 /* Check for mandatory arguments. */
613 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100614 if (idx < min_arg) {
615
616 /* If miss other argument than the first one, we return an error. */
617 if (idx > 0)
618 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
619
620 /* If first argument have a certain type, some default values
621 * may be used. See the function smp_resolve_args().
622 */
623 switch (mask & ARGT_MASK) {
624
625 case ARGT_FE:
626 if (!(p->cap & PR_CAP_FE))
627 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
628 argp[idx].data.prx = p;
629 argp[idx].type = ARGT_FE;
630 argp[idx+1].type = ARGT_STOP;
631 break;
632
633 case ARGT_BE:
634 if (!(p->cap & PR_CAP_BE))
635 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
636 argp[idx].data.prx = p;
637 argp[idx].type = ARGT_BE;
638 argp[idx+1].type = ARGT_STOP;
639 break;
640
641 case ARGT_TAB:
642 argp[idx].data.prx = p;
643 argp[idx].type = ARGT_TAB;
644 argp[idx+1].type = ARGT_STOP;
645 break;
646
647 default:
648 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
649 break;
650 }
651 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100652 return 0;
653 }
654
655 /* Check for exceed the number of requiered argument. */
656 if ((mask & ARGT_MASK) == ARGT_STOP &&
657 argp[idx].type != ARGT_STOP) {
658 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
659 }
660
661 if ((mask & ARGT_MASK) == ARGT_STOP &&
662 argp[idx].type == ARGT_STOP) {
663 return 0;
664 }
665
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100666 /* Convert some argument types. */
667 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100668 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 if (argp[idx].type != ARGT_SINT)
670 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
671 argp[idx].type = ARGT_SINT;
672 break;
673
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100674 case ARGT_TIME:
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_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100678 break;
679
680 case ARGT_SIZE:
681 if (argp[idx].type != ARGT_SINT)
682 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200683 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100684 break;
685
686 case ARGT_FE:
687 if (argp[idx].type != ARGT_STR)
688 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 memcpy(trash.area, argp[idx].data.str.area,
690 argp[idx].data.str.data);
691 trash.area[argp[idx].data.str.data] = 0;
692 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100693 if (!argp[idx].data.prx)
694 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
695 argp[idx].type = ARGT_FE;
696 break;
697
698 case ARGT_BE:
699 if (argp[idx].type != ARGT_STR)
700 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200701 memcpy(trash.area, argp[idx].data.str.area,
702 argp[idx].data.str.data);
703 trash.area[argp[idx].data.str.data] = 0;
704 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100705 if (!argp[idx].data.prx)
706 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
707 argp[idx].type = ARGT_BE;
708 break;
709
710 case ARGT_TAB:
711 if (argp[idx].type != ARGT_STR)
712 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 memcpy(trash.area, argp[idx].data.str.area,
714 argp[idx].data.str.data);
715 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200716 argp[idx].data.t = stktable_find_by_name(trash.area);
717 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100718 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
719 argp[idx].type = ARGT_TAB;
720 break;
721
722 case ARGT_SRV:
723 if (argp[idx].type != ARGT_STR)
724 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 memcpy(trash.area, argp[idx].data.str.area,
726 argp[idx].data.str.data);
727 trash.area[argp[idx].data.str.data] = 0;
728 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100729 if (sname) {
730 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200732 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100733 if (!px)
734 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
735 }
736 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200737 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100738 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100739 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100740 argp[idx].data.srv = findserver(px, sname);
741 if (!argp[idx].data.srv)
742 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
743 argp[idx].type = ARGT_SRV;
744 break;
745
746 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 memcpy(trash.area, argp[idx].data.str.area,
748 argp[idx].data.str.data);
749 trash.area[argp[idx].data.str.data] = 0;
750 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100751 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
752 argp[idx].type = ARGT_IPV4;
753 break;
754
755 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 memcpy(trash.area, argp[idx].data.str.area,
757 argp[idx].data.str.data);
758 trash.area[argp[idx].data.str.data] = 0;
759 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100760 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
761 argp[idx].type = ARGT_MSK4;
762 break;
763
764 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200765 memcpy(trash.area, argp[idx].data.str.area,
766 argp[idx].data.str.data);
767 trash.area[argp[idx].data.str.data] = 0;
768 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100769 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
770 argp[idx].type = ARGT_IPV6;
771 break;
772
773 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200774 memcpy(trash.area, argp[idx].data.str.area,
775 argp[idx].data.str.data);
776 trash.area[argp[idx].data.str.data] = 0;
777 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100778 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
779 argp[idx].type = ARGT_MSK6;
780 break;
781
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100782 case ARGT_MAP:
783 case ARGT_REG:
784 case ARGT_USR:
785 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100786 break;
787 }
788
789 /* Check for type of argument. */
790 if ((mask & ARGT_MASK) != argp[idx].type) {
791 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
792 arg_type_names[(mask & ARGT_MASK)],
793 arg_type_names[argp[idx].type & ARGT_MASK]);
794 WILL_LJMP(luaL_argerror(L, first + idx, msg));
795 }
796
797 /* Next argument. */
798 mask >>= ARGT_BITS;
799 idx++;
800 }
801}
802
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803/*
804 * The following functions are used to make correspondance between the the
805 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806 *
807 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100808 * - hlua_sethlua : create the association between hlua context and lua_state.
809 */
810static inline struct hlua *hlua_gethlua(lua_State *L)
811{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100812 struct hlua **hlua = lua_getextraspace(L);
813 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100814}
815static inline void hlua_sethlua(struct hlua *hlua)
816{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100817 struct hlua **hlua_store = lua_getextraspace(hlua->T);
818 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100819}
820
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100821/* This function is used to send logs. It try to send on screen (stderr)
822 * and on the default syslog server.
823 */
824static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
825{
826 struct tm tm;
827 char *p;
828
829 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200830 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100831 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200832 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200833 /* Break the message if exceed the buffer size. */
834 *(p-4) = ' ';
835 *(p-3) = '.';
836 *(p-2) = '.';
837 *(p-1) = '.';
838 break;
839 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100840 if (isprint(*msg))
841 *p = *msg;
842 else
843 *p = '.';
844 }
845 *p = '\0';
846
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200847 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100848 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200849 get_localtime(date.tv_sec, &tm);
850 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100851 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200852 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100853 fflush(stderr);
854 }
855}
856
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100857/* This function just ensure that the yield will be always
858 * returned with a timeout and permit to set some flags
859 */
860__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100861 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100862{
863 struct hlua *hlua = hlua_gethlua(L);
864
865 /* Set the wake timeout. If timeout is required, we set
866 * the expiration time.
867 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200868 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100869
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100870 hlua->flags |= flags;
871
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100872 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200873 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100874}
875
Willy Tarreau87b09662015-04-03 00:22:06 +0200876/* This function initialises the Lua environment stored in the stream.
877 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100878 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200879 *
880 * This function is particular. it initialises a new Lua thread. If the
881 * initialisation fails (example: out of memory error), the lua function
882 * throws an error (longjmp).
883 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100884 * In some case (at least one), this function can be called from safe
885 * environement, so we must not initialise it. While the support of
886 * threads appear, the safe environment set a lock to ensure only one
887 * Lua execution at a time. If we initialize safe environment in another
888 * safe environmenet, we have a dead lock.
889 *
890 * set "already_safe" true if the context is initialized form safe
891 * Lua fonction.
892 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800893 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200894 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800895 * MUST NOT manipulate the created thread stack state, because it is not
896 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100897 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100898int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100899{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100900 if (!already_safe) {
901 if (!SET_SAFE_LJMP(gL.T)) {
902 lua->Tref = LUA_REFNIL;
903 return 0;
904 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200905 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100906 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100907 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100908 lua->gc_count = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100909 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100910 lua->T = lua_newthread(gL.T);
911 if (!lua->T) {
912 lua->Tref = LUA_REFNIL;
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 0;
916 }
917 hlua_sethlua(lua);
918 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
919 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100920 if (!already_safe)
921 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100922 return 1;
923}
924
Willy Tarreau87b09662015-04-03 00:22:06 +0200925/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100926 * is destroyed. The destroy also the memory context. The struct "lua"
927 * is not freed.
928 */
929void hlua_ctx_destroy(struct hlua *lua)
930{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100931 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100932 return;
933
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100934 if (!lua->T)
935 goto end;
936
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100937 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200938 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100939
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200940 if (!SET_SAFE_LJMP(lua->T))
941 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100942 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200943 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200944
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200945 if (!SET_SAFE_LJMP(gL.T))
946 return;
947 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
948 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200949 /* Forces a garbage collecting process. If the Lua program is finished
950 * without error, we run the GC on the thread pointer. Its freed all
951 * the unused memory.
952 * If the thread is finnish with an error or is currently yielded,
953 * it seems that the GC applied on the thread doesn't clean anything,
954 * so e run the GC on the main thread.
955 * NOTE: maybe this action locks all the Lua threads untiml the en of
956 * the garbage collection.
957 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100958 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200959 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200960 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200961 lua_gc(gL.T, LUA_GCCOLLECT, 0);
962 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200963 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200964
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200965 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100966
967end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100968 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100969}
970
971/* This function is used to restore the Lua context when a coroutine
972 * fails. This function copy the common memory between old coroutine
973 * and the new coroutine. The old coroutine is destroyed, and its
974 * replaced by the new coroutine.
975 * If the flag "keep_msg" is set, the last entry of the old is assumed
976 * as string error message and it is copied in the new stack.
977 */
978static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
979{
980 lua_State *T;
981 int new_ref;
982
983 /* Renew the main LUA stack doesn't have sense. */
984 if (lua == &gL)
985 return 0;
986
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100987 /* New Lua coroutine. */
988 T = lua_newthread(gL.T);
989 if (!T)
990 return 0;
991
992 /* Copy last error message. */
993 if (keep_msg)
994 lua_xmove(lua->T, T, 1);
995
996 /* Copy data between the coroutines. */
997 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
998 lua_xmove(lua->T, T, 1);
999 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1000
1001 /* Destroy old data. */
1002 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1003
1004 /* The thread is garbage collected by Lua. */
1005 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1006
1007 /* Fill the struct with the new coroutine values. */
1008 lua->Mref = new_ref;
1009 lua->T = T;
1010 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1011
1012 /* Set context. */
1013 hlua_sethlua(lua);
1014
1015 return 1;
1016}
1017
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001018void hlua_hook(lua_State *L, lua_Debug *ar)
1019{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001020 struct hlua *hlua = hlua_gethlua(L);
1021
1022 /* Lua cannot yield when its returning from a function,
1023 * so, we can fix the interrupt hook to 1 instruction,
1024 * expecting that the function is finnished.
1025 */
1026 if (lua_gethookmask(L) & LUA_MASKRET) {
1027 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1028 return;
1029 }
1030
1031 /* restore the interrupt condition. */
1032 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1033
1034 /* If we interrupt the Lua processing in yieldable state, we yield.
1035 * If the state is not yieldable, trying yield causes an error.
1036 */
1037 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001038 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001039
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001040 /* If we cannot yield, update the clock and check the timeout. */
1041 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001042 hlua->run_time += now_ms - hlua->start_time;
1043 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001044 lua_pushfstring(L, "execution timeout");
1045 WILL_LJMP(lua_error(L));
1046 }
1047
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001048 /* Update the start time. */
1049 hlua->start_time = now_ms;
1050
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001051 /* Try to interrupt the process at the end of the current
1052 * unyieldable function.
1053 */
1054 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001055}
1056
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057/* This function start or resumes the Lua stack execution. If the flag
1058 * "yield_allowed" if no set and the LUA stack execution returns a yield
1059 * The function return an error.
1060 *
1061 * The function can returns 4 values:
1062 * - HLUA_E_OK : The execution is terminated without any errors.
1063 * - HLUA_E_AGAIN : The execution must continue at the next associated
1064 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001065 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001067 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001069 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070 * LUA code.
1071 */
1072static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1073{
1074 int ret;
1075 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001076 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001077
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001078 /* Initialise run time counter. */
1079 if (!HLUA_IS_RUNNING(lua))
1080 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001081
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001082 /* Lock the whole Lua execution. This lock must be before the
1083 * label "resume_execution".
1084 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001085 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001086
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001087resume_execution:
1088
1089 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1090 * instructions. it is used for preventing infinite loops.
1091 */
1092 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1093
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001094 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001095 HLUA_SET_RUN(lua);
1096 HLUA_CLR_CTRLYIELD(lua);
1097 HLUA_CLR_WAKERESWR(lua);
1098 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001099
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001100 /* Update the start time. */
1101 lua->start_time = now_ms;
1102
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001103 /* Call the function. */
1104 ret = lua_resume(lua->T, gL.T, lua->nargs);
1105 switch (ret) {
1106
1107 case LUA_OK:
1108 ret = HLUA_E_OK;
1109 break;
1110
1111 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001112 /* Check if the execution timeout is expired. It it is the case, we
1113 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001114 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001115 tv_update_date(0, 1);
1116 lua->run_time += now_ms - lua->start_time;
1117 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001118 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001119 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001120 break;
1121 }
1122 /* Process the forced yield. if the general yield is not allowed or
1123 * if no task were associated this the current Lua execution
1124 * coroutine, we resume the execution. Else we want to return in the
1125 * scheduler and we want to be waked up again, to continue the
1126 * current Lua execution. So we schedule our own task.
1127 */
1128 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001129 if (!yield_allowed || !lua->task)
1130 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001131 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001132 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001133 if (!yield_allowed) {
1134 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001135 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001136 break;
1137 }
1138 ret = HLUA_E_AGAIN;
1139 break;
1140
1141 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001142
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001143 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001144 * because the errors ares the only one mean to return immediately
1145 * from and lua execution.
1146 */
1147 if (lua->flags & HLUA_EXIT) {
1148 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001149 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001150 break;
1151 }
1152
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001153 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001154 if (!lua_checkstack(lua->T, 1)) {
1155 ret = HLUA_E_ERR;
1156 break;
1157 }
1158 msg = lua_tostring(lua->T, -1);
1159 lua_settop(lua->T, 0); /* Empty the stack. */
1160 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001161 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001163 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001165 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 ret = HLUA_E_ERRMSG;
1167 break;
1168
1169 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001170 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001172 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 break;
1174
1175 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001176 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001177 if (!lua_checkstack(lua->T, 1)) {
1178 ret = HLUA_E_ERR;
1179 break;
1180 }
1181 msg = lua_tostring(lua->T, -1);
1182 lua_settop(lua->T, 0); /* Empty the stack. */
1183 lua_pop(lua->T, 1);
1184 if (msg)
1185 lua_pushfstring(lua->T, "message handler error: %s", msg);
1186 else
1187 lua_pushfstring(lua->T, "message handler error");
1188 ret = HLUA_E_ERRMSG;
1189 break;
1190
1191 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001192 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001194 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001195 break;
1196 }
1197
1198 switch (ret) {
1199 case HLUA_E_AGAIN:
1200 break;
1201
1202 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001203 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001204 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001205 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 break;
1207
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001208 case HLUA_E_ETMOUT:
1209 case HLUA_E_NOMEM:
1210 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001211 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001212 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001213 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001214 hlua_ctx_renew(lua, 0);
1215 break;
1216
1217 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001218 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001219 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001220 break;
1221 }
1222
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001223 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001224 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001225
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001226 return ret;
1227}
1228
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001229/* This function exit the current code. */
1230__LJMP static int hlua_done(lua_State *L)
1231{
1232 struct hlua *hlua = hlua_gethlua(L);
1233
1234 hlua->flags |= HLUA_EXIT;
1235 WILL_LJMP(lua_error(L));
1236
1237 return 0;
1238}
1239
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001240/* This function is an LUA binding. It provides a function
1241 * for deleting ACL from a referenced ACL file.
1242 */
1243__LJMP static int hlua_del_acl(lua_State *L)
1244{
1245 const char *name;
1246 const char *key;
1247 struct pat_ref *ref;
1248
1249 MAY_LJMP(check_args(L, 2, "del_acl"));
1250
1251 name = MAY_LJMP(luaL_checkstring(L, 1));
1252 key = MAY_LJMP(luaL_checkstring(L, 2));
1253
1254 ref = pat_ref_lookup(name);
1255 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001256 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001257
1258 pat_ref_delete(ref, key);
1259 return 0;
1260}
1261
1262/* This function is an LUA binding. It provides a function
1263 * for deleting map entry from a referenced map file.
1264 */
1265static int hlua_del_map(lua_State *L)
1266{
1267 const char *name;
1268 const char *key;
1269 struct pat_ref *ref;
1270
1271 MAY_LJMP(check_args(L, 2, "del_map"));
1272
1273 name = MAY_LJMP(luaL_checkstring(L, 1));
1274 key = MAY_LJMP(luaL_checkstring(L, 2));
1275
1276 ref = pat_ref_lookup(name);
1277 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001278 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001279
1280 pat_ref_delete(ref, key);
1281 return 0;
1282}
1283
1284/* This function is an LUA binding. It provides a function
1285 * for adding ACL pattern from a referenced ACL file.
1286 */
1287static int hlua_add_acl(lua_State *L)
1288{
1289 const char *name;
1290 const char *key;
1291 struct pat_ref *ref;
1292
1293 MAY_LJMP(check_args(L, 2, "add_acl"));
1294
1295 name = MAY_LJMP(luaL_checkstring(L, 1));
1296 key = MAY_LJMP(luaL_checkstring(L, 2));
1297
1298 ref = pat_ref_lookup(name);
1299 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001300 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001301
1302 if (pat_ref_find_elt(ref, key) == NULL)
1303 pat_ref_add(ref, key, NULL, NULL);
1304 return 0;
1305}
1306
1307/* This function is an LUA binding. It provides a function
1308 * for setting map pattern and sample from a referenced map
1309 * file.
1310 */
1311static int hlua_set_map(lua_State *L)
1312{
1313 const char *name;
1314 const char *key;
1315 const char *value;
1316 struct pat_ref *ref;
1317
1318 MAY_LJMP(check_args(L, 3, "set_map"));
1319
1320 name = MAY_LJMP(luaL_checkstring(L, 1));
1321 key = MAY_LJMP(luaL_checkstring(L, 2));
1322 value = MAY_LJMP(luaL_checkstring(L, 3));
1323
1324 ref = pat_ref_lookup(name);
1325 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001326 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001327
1328 if (pat_ref_find_elt(ref, key) != NULL)
1329 pat_ref_set(ref, key, value, NULL);
1330 else
1331 pat_ref_add(ref, key, value, NULL);
1332 return 0;
1333}
1334
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001335/* A class is a lot of memory that contain data. This data can be a table,
1336 * an integer or user data. This data is associated with a metatable. This
1337 * metatable have an original version registred in the global context with
1338 * the name of the object (_G[<name>] = <metable> ).
1339 *
1340 * A metable is a table that modify the standard behavior of a standard
1341 * access to the associated data. The entries of this new metatable are
1342 * defined as is:
1343 *
1344 * http://lua-users.org/wiki/MetatableEvents
1345 *
1346 * __index
1347 *
1348 * we access an absent field in a table, the result is nil. This is
1349 * true, but it is not the whole truth. Actually, such access triggers
1350 * the interpreter to look for an __index metamethod: If there is no
1351 * such method, as usually happens, then the access results in nil;
1352 * otherwise, the metamethod will provide the result.
1353 *
1354 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1355 * the key does not appear in the table, but the metatable has an __index
1356 * property:
1357 *
1358 * - if the value is a function, the function is called, passing in the
1359 * table and the key; the return value of that function is returned as
1360 * the result.
1361 *
1362 * - if the value is another table, the value of the key in that table is
1363 * asked for and returned (and if it doesn't exist in that table, but that
1364 * table's metatable has an __index property, then it continues on up)
1365 *
1366 * - Use "rawget(myTable,key)" to skip this metamethod.
1367 *
1368 * http://www.lua.org/pil/13.4.1.html
1369 *
1370 * __newindex
1371 *
1372 * Like __index, but control property assignment.
1373 *
1374 * __mode - Control weak references. A string value with one or both
1375 * of the characters 'k' and 'v' which specifies that the the
1376 * keys and/or values in the table are weak references.
1377 *
1378 * __call - Treat a table like a function. When a table is followed by
1379 * parenthesis such as "myTable( 'foo' )" and the metatable has
1380 * a __call key pointing to a function, that function is invoked
1381 * (passing any specified arguments) and the return value is
1382 * returned.
1383 *
1384 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1385 * called, if the metatable for myTable has a __metatable
1386 * key, the value of that key is returned instead of the
1387 * actual metatable.
1388 *
1389 * __tostring - Control string representation. When the builtin
1390 * "tostring( myTable )" function is called, if the metatable
1391 * for myTable has a __tostring property set to a function,
1392 * that function is invoked (passing myTable to it) and the
1393 * return value is used as the string representation.
1394 *
1395 * __len - Control table length. When the table length is requested using
1396 * the length operator ( '#' ), if the metatable for myTable has
1397 * a __len key pointing to a function, that function is invoked
1398 * (passing myTable to it) and the return value used as the value
1399 * of "#myTable".
1400 *
1401 * __gc - Userdata finalizer code. When userdata is set to be garbage
1402 * collected, if the metatable has a __gc field pointing to a
1403 * function, that function is first invoked, passing the userdata
1404 * to it. The __gc metamethod is not called for tables.
1405 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1406 *
1407 * Special metamethods for redefining standard operators:
1408 * http://www.lua.org/pil/13.1.html
1409 *
1410 * __add "+"
1411 * __sub "-"
1412 * __mul "*"
1413 * __div "/"
1414 * __unm "!"
1415 * __pow "^"
1416 * __concat ".."
1417 *
1418 * Special methods for redfining standar relations
1419 * http://www.lua.org/pil/13.2.html
1420 *
1421 * __eq "=="
1422 * __lt "<"
1423 * __le "<="
1424 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001425
1426/*
1427 *
1428 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001429 * Class Map
1430 *
1431 *
1432 */
1433
1434/* Returns a struct hlua_map if the stack entry "ud" is
1435 * a class session, otherwise it throws an error.
1436 */
1437__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1438{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001439 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001440}
1441
1442/* This function is the map constructor. It don't need
1443 * the class Map object. It creates and return a new Map
1444 * object. It must be called only during "body" or "init"
1445 * context because it process some filesystem accesses.
1446 */
1447__LJMP static int hlua_map_new(struct lua_State *L)
1448{
1449 const char *fn;
1450 int match = PAT_MATCH_STR;
1451 struct sample_conv conv;
1452 const char *file = "";
1453 int line = 0;
1454 lua_Debug ar;
1455 char *err = NULL;
1456 struct arg args[2];
1457
1458 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1459 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1460
1461 fn = MAY_LJMP(luaL_checkstring(L, 1));
1462
1463 if (lua_gettop(L) >= 2) {
1464 match = MAY_LJMP(luaL_checkinteger(L, 2));
1465 if (match < 0 || match >= PAT_MATCH_NUM)
1466 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1467 }
1468
1469 /* Get Lua filename and line number. */
1470 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1471 lua_getinfo(L, "Sl", &ar); /* get info about it */
1472 if (ar.currentline > 0) { /* is there info? */
1473 file = ar.short_src;
1474 line = ar.currentline;
1475 }
1476 }
1477
1478 /* fill fake sample_conv struct. */
1479 conv.kw = ""; /* unused. */
1480 conv.process = NULL; /* unused. */
1481 conv.arg_mask = 0; /* unused. */
1482 conv.val_args = NULL; /* unused. */
1483 conv.out_type = SMP_T_STR;
1484 conv.private = (void *)(long)match;
1485 switch (match) {
1486 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1487 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1488 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1489 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1490 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1491 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1492 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001493 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001494 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1495 default:
1496 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1497 }
1498
1499 /* fill fake args. */
1500 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001501 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001502 args[1].type = ARGT_STOP;
1503
1504 /* load the map. */
1505 if (!sample_load_map(args, &conv, file, line, &err)) {
1506 /* error case: we cant use luaL_error because we must
1507 * free the err variable.
1508 */
1509 luaL_where(L, 1);
1510 lua_pushfstring(L, "'new': %s.", err);
1511 lua_concat(L, 2);
1512 free(err);
1513 WILL_LJMP(lua_error(L));
1514 }
1515
1516 /* create the lua object. */
1517 lua_newtable(L);
1518 lua_pushlightuserdata(L, args[0].data.map);
1519 lua_rawseti(L, -2, 0);
1520
1521 /* Pop a class Map metatable and affect it to the userdata. */
1522 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1523 lua_setmetatable(L, -2);
1524
1525
1526 return 1;
1527}
1528
1529__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1530{
1531 struct map_descriptor *desc;
1532 struct pattern *pat;
1533 struct sample smp;
1534
1535 MAY_LJMP(check_args(L, 2, "lookup"));
1536 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001537 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001538 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001539 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001540 }
1541 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001542 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001543 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001544 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 +02001545 }
1546
1547 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001548 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001549 if (str)
1550 lua_pushstring(L, "");
1551 else
1552 lua_pushnil(L);
1553 return 1;
1554 }
1555
1556 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001557 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001558 return 1;
1559}
1560
1561__LJMP static int hlua_map_lookup(struct lua_State *L)
1562{
1563 return _hlua_map_lookup(L, 0);
1564}
1565
1566__LJMP static int hlua_map_slookup(struct lua_State *L)
1567{
1568 return _hlua_map_lookup(L, 1);
1569}
1570
1571/*
1572 *
1573 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001574 * Class Socket
1575 *
1576 *
1577 */
1578
1579__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1580{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001581 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001582}
1583
1584/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001585 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001586 * received.
1587 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001588static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001590 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001591
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001592 if (appctx->ctx.hlua_cosocket.die) {
1593 si_shutw(si);
1594 si_shutr(si);
1595 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001596 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1597 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001598 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1599 }
1600
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001601 /* If we cant write, wakeup the pending write signals. */
1602 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001603 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001604
1605 /* If we cant read, wakeup the pending read signals. */
1606 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001607 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001608
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001609 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001610 * to be notified whenever the connection completes.
1611 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001612 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001613 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001614 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001615 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001616 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001617 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001618
1619 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001620 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001621
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001622 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001623 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001624 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001625
1626 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001627 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001628 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001629
1630 /* Some data were injected in the buffer, notify the stream
1631 * interface.
1632 */
1633 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001634 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001635
1636 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001637 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001638 */
1639 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001640 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001641}
1642
Willy Tarreau87b09662015-04-03 00:22:06 +02001643/* This function is called when the "struct stream" is destroyed.
1644 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001645 * Wake all the pending signals.
1646 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001647static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001648{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001649 struct xref *peer;
1650
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001651 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001652 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1653 if (peer)
1654 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001655
1656 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001657 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1658 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001659}
1660
1661/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001662 * uses this object. If the stream does not exists, just quit.
1663 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664 * pending signal can rest in the read and write lists. destroy
1665 * it.
1666 */
1667__LJMP static int hlua_socket_gc(lua_State *L)
1668{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001669 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001670 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001671 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001672
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001673 MAY_LJMP(check_args(L, 1, "__gc"));
1674
1675 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001676 peer = xref_get_peer_and_lock(&socket->xref);
1677 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001678 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001679 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001681 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001682 appctx->ctx.hlua_cosocket.die = 1;
1683 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001684
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001685 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001686 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687 return 0;
1688}
1689
1690/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001691 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692 */
sada05ed3302018-05-11 11:48:18 -07001693__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001695 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001696 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001697 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001698 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001700 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001701
1702 /* Check if we run on the same thread than the xreator thread.
1703 * We cannot access to the socket if the thread is different.
1704 */
1705 if (socket->tid != tid)
1706 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1707
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001708 peer = xref_get_peer_and_lock(&socket->xref);
1709 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001710 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001711
1712 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001713 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001714
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001715 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001716 appctx->ctx.hlua_cosocket.die = 1;
1717 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001718
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001719 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001720 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001721 return 0;
1722}
1723
sada05ed3302018-05-11 11:48:18 -07001724/* The close function calls close_helper.
1725 */
1726__LJMP static int hlua_socket_close(lua_State *L)
1727{
1728 MAY_LJMP(check_args(L, 1, "close"));
1729 return hlua_socket_close_helper(L);
1730}
1731
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001732/* This Lua function assumes that the stack contain three parameters.
1733 * 1 - USERDATA containing a struct socket
1734 * 2 - INTEGER with values of the macro defined below
1735 * If the integer is -1, we must read at most one line.
1736 * If the integer is -2, we ust read all the data until the
1737 * end of the stream.
1738 * If the integer is positive value, we must read a number of
1739 * bytes corresponding to this value.
1740 */
1741#define HLSR_READ_LINE (-1)
1742#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001743__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001744{
1745 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1746 int wanted = lua_tointeger(L, 2);
1747 struct hlua *hlua = hlua_gethlua(L);
1748 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001749 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001750 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001751 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001752 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001753 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001754 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001755 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001756 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001757 struct stream_interface *si;
1758 struct stream *s;
1759 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001760 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001761
1762 /* Check if this lua stack is schedulable. */
1763 if (!hlua || !hlua->task)
1764 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1765 "'frontend', 'backend' or 'task'"));
1766
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001767 /* Check if we run on the same thread than the xreator thread.
1768 * We cannot access to the socket if the thread is different.
1769 */
1770 if (socket->tid != tid)
1771 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1772
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001773 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001774 peer = xref_get_peer_and_lock(&socket->xref);
1775 if (!peer)
1776 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001777 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1778 si = appctx->owner;
1779 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001780
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001781 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001784 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001785 if (nblk < 0) /* Connection close. */
1786 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001787 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001788 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001789
1790 /* remove final \r\n. */
1791 if (nblk == 1) {
1792 if (blk1[len1-1] == '\n') {
1793 len1--;
1794 skip_at_end++;
1795 if (blk1[len1-1] == '\r') {
1796 len1--;
1797 skip_at_end++;
1798 }
1799 }
1800 }
1801 else {
1802 if (blk2[len2-1] == '\n') {
1803 len2--;
1804 skip_at_end++;
1805 if (blk2[len2-1] == '\r') {
1806 len2--;
1807 skip_at_end++;
1808 }
1809 }
1810 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001811 }
1812
1813 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001814 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001815 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816 if (nblk < 0) /* Connection close. */
1817 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001818 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819 goto connection_empty;
1820 }
1821
1822 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001824 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 if (nblk < 0) /* Connection close. */
1826 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001827 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828 goto connection_empty;
1829
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001830 missing_bytes = wanted - socket->b.n;
1831 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001833 len1 = missing_bytes;
1834 } if (nblk == 2 && len1 + len2 > missing_bytes)
1835 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836 }
1837
1838 len = len1;
1839
1840 luaL_addlstring(&socket->b, blk1, len1);
1841 if (nblk == 2) {
1842 len += len2;
1843 luaL_addlstring(&socket->b, blk2, len2);
1844 }
1845
1846 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001847 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001848
1849 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001850 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001851
1852 /* If the pattern reclaim to read all the data
1853 * in the connection, got out.
1854 */
1855 if (wanted == HLSR_READ_ALL)
1856 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001857 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858 goto connection_empty;
1859
1860 /* Return result. */
1861 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001862 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001863 return 1;
1864
1865connection_closed:
1866
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001867 xref_unlock(&socket->xref, peer);
1868
1869no_peer:
1870
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001871 /* If the buffer containds data. */
1872 if (socket->b.n > 0) {
1873 luaL_pushresult(&socket->b);
1874 return 1;
1875 }
1876 lua_pushnil(L);
1877 lua_pushstring(L, "connection closed.");
1878 return 2;
1879
1880connection_empty:
1881
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001882 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1883 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001885 }
1886 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001887 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888 return 0;
1889}
1890
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001891/* This Lua function gets two parameters. The first one can be string
1892 * or a number. If the string is "*l", the user requires one line. If
1893 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894 * If the value is a number, the user require a number of bytes equal
1895 * to the value. The default value is "*l" (a line).
1896 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001897 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001898 * integer takes this values:
1899 * -1 : read a line
1900 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001901 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001903 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 * concatenated with the read data.
1905 */
1906__LJMP static int hlua_socket_receive(struct lua_State *L)
1907{
1908 int wanted = HLSR_READ_LINE;
1909 const char *pattern;
1910 int type;
1911 char *error;
1912 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001913 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914
1915 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1916 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1917
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001918 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001919
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001920 /* Check if we run on the same thread than the xreator thread.
1921 * We cannot access to the socket if the thread is different.
1922 */
1923 if (socket->tid != tid)
1924 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1925
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001926 /* check for pattern. */
1927 if (lua_gettop(L) >= 2) {
1928 type = lua_type(L, 2);
1929 if (type == LUA_TSTRING) {
1930 pattern = lua_tostring(L, 2);
1931 if (strcmp(pattern, "*a") == 0)
1932 wanted = HLSR_READ_ALL;
1933 else if (strcmp(pattern, "*l") == 0)
1934 wanted = HLSR_READ_LINE;
1935 else {
1936 wanted = strtoll(pattern, &error, 10);
1937 if (*error != '\0')
1938 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1939 }
1940 }
1941 else if (type == LUA_TNUMBER) {
1942 wanted = lua_tointeger(L, 2);
1943 if (wanted < 0)
1944 WILL_LJMP(luaL_error(L, "Unsupported size."));
1945 }
1946 }
1947
1948 /* Set pattern. */
1949 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001950
1951 /* Check if we would replace the top by itself. */
1952 if (lua_gettop(L) != 2)
1953 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001954
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001955 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956 luaL_buffinit(L, &socket->b);
1957
1958 /* Check prefix. */
1959 if (lua_gettop(L) >= 3) {
1960 if (lua_type(L, 3) != LUA_TSTRING)
1961 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1962 pattern = lua_tolstring(L, 3, &len);
1963 luaL_addlstring(&socket->b, pattern, len);
1964 }
1965
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001966 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001967}
1968
1969/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001970 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001972static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973{
1974 struct hlua_socket *socket;
1975 struct hlua *hlua = hlua_gethlua(L);
1976 struct appctx *appctx;
1977 size_t buf_len;
1978 const char *buf;
1979 int len;
1980 int send_len;
1981 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001982 struct xref *peer;
1983 struct stream_interface *si;
1984 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985
1986 /* Check if this lua stack is schedulable. */
1987 if (!hlua || !hlua->task)
1988 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1989 "'frontend', 'backend' or 'task'"));
1990
1991 /* Get object */
1992 socket = MAY_LJMP(hlua_checksocket(L, 1));
1993 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001994 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001995
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001996 /* Check if we run on the same thread than the xreator thread.
1997 * We cannot access to the socket if the thread is different.
1998 */
1999 if (socket->tid != tid)
2000 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2001
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002002 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002003 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002004 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002005 lua_pushinteger(L, -1);
2006 return 1;
2007 }
2008 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2009 si = appctx->owner;
2010 s = si_strm(si);
2011
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002012 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002013 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002014 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002015 lua_pushinteger(L, -1);
2016 return 1;
2017 }
2018
2019 /* Update the input buffer data. */
2020 buf += sent;
2021 send_len = buf_len - sent;
2022
2023 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002024 if (sent >= buf_len) {
2025 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002027 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002029 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002030 * the request buffer if its not required.
2031 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002032 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002033 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002034 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002035 }
2036
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002037 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002038 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002039 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002040 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002041 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042
2043 /* send data */
2044 if (len < send_len)
2045 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002046 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002047
2048 /* "Not enough space" (-1), "Buffer too little to contain
2049 * the data" (-2) are not expected because the available length
2050 * is tested.
2051 * Other unknown error are also not expected.
2052 */
2053 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002054 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002055 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002056
sada05ed3302018-05-11 11:48:18 -07002057 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002058 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002059 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002060 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002061 return 1;
2062 }
2063
2064 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002065 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002066
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002067 s->req.rex = TICK_ETERNITY;
2068 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069
2070 /* Update length sent. */
2071 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002072 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073
2074 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002075 if (sent + len >= buf_len) {
2076 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002078 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079
2080hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002081 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2082 xref_unlock(&socket->xref, peer);
2083 WILL_LJMP(luaL_error(L, "out of memory"));
2084 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002085 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002086 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087 return 0;
2088}
2089
2090/* This function initiate the send of data. It just check the input
2091 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002092 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093 * "hlua_socket_write_yield" that can yield.
2094 *
2095 * The Lua function gets between 3 and 4 parameters. The first one is
2096 * the associated object. The second is a string buffer. The third is
2097 * a facultative integer that represents where is the buffer position
2098 * of the start of the data that can send. The first byte is the
2099 * position "1". The default value is "1". The fourth argument is a
2100 * facultative integer that represents where is the buffer position
2101 * of the end of the data that can send. The default is the last byte.
2102 */
2103static int hlua_socket_send(struct lua_State *L)
2104{
2105 int i;
2106 int j;
2107 const char *buf;
2108 size_t buf_len;
2109
2110 /* Check number of arguments. */
2111 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2112 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2113
2114 /* Get the string. */
2115 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2116
2117 /* Get and check j. */
2118 if (lua_gettop(L) == 4) {
2119 j = MAY_LJMP(luaL_checkinteger(L, 4));
2120 if (j < 0)
2121 j = buf_len + j + 1;
2122 if (j > buf_len)
2123 j = buf_len + 1;
2124 lua_pop(L, 1);
2125 }
2126 else
2127 j = buf_len;
2128
2129 /* Get and check i. */
2130 if (lua_gettop(L) == 3) {
2131 i = MAY_LJMP(luaL_checkinteger(L, 3));
2132 if (i < 0)
2133 i = buf_len + i + 1;
2134 if (i > buf_len)
2135 i = buf_len + 1;
2136 lua_pop(L, 1);
2137 } else
2138 i = 1;
2139
2140 /* Check bth i and j. */
2141 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002142 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002143 return 1;
2144 }
2145 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002146 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002147 return 1;
2148 }
2149 if (i == 0)
2150 i = 1;
2151 if (j == 0)
2152 j = 1;
2153
2154 /* Pop the string. */
2155 lua_pop(L, 1);
2156
2157 /* Update the buffer length. */
2158 buf += i - 1;
2159 buf_len = j - i + 1;
2160 lua_pushlstring(L, buf, buf_len);
2161
2162 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002163 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002164
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002165 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166}
2167
Willy Tarreau22b0a682015-06-17 19:43:49 +02002168#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2170{
2171 static char buffer[SOCKET_INFO_MAX_LEN];
2172 int ret;
2173 int len;
2174 char *p;
2175
2176 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2177 if (ret <= 0) {
2178 lua_pushnil(L);
2179 return 1;
2180 }
2181
2182 if (ret == AF_UNIX) {
2183 lua_pushstring(L, buffer+1);
2184 return 1;
2185 }
2186 else if (ret == AF_INET6) {
2187 buffer[0] = '[';
2188 len = strlen(buffer);
2189 buffer[len] = ']';
2190 len++;
2191 buffer[len] = ':';
2192 len++;
2193 p = buffer;
2194 }
2195 else if (ret == AF_INET) {
2196 p = buffer + 1;
2197 len = strlen(p);
2198 p[len] = ':';
2199 len++;
2200 }
2201 else {
2202 lua_pushnil(L);
2203 return 1;
2204 }
2205
2206 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2207 lua_pushnil(L);
2208 return 1;
2209 }
2210
2211 lua_pushstring(L, p);
2212 return 1;
2213}
2214
2215/* Returns information about the peer of the connection. */
2216__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2217{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002218 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002219 struct xref *peer;
2220 struct appctx *appctx;
2221 struct stream_interface *si;
2222 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002223 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002224
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002225 MAY_LJMP(check_args(L, 1, "getpeername"));
2226
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002227 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002229 /* Check if we run on the same thread than the xreator thread.
2230 * We cannot access to the socket if the thread is different.
2231 */
2232 if (socket->tid != tid)
2233 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2234
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002235 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002236 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002237 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002238 lua_pushnil(L);
2239 return 1;
2240 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002241 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2242 si = appctx->owner;
2243 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002245 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002246 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002247 lua_pushnil(L);
2248 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249 }
2250
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002251 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002252 xref_unlock(&socket->xref, peer);
2253 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002254}
2255
2256/* Returns information about my connection side. */
2257static int hlua_socket_getsockname(struct lua_State *L)
2258{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002259 struct hlua_socket *socket;
2260 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002261 struct appctx *appctx;
2262 struct xref *peer;
2263 struct stream_interface *si;
2264 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002265 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002266
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267 MAY_LJMP(check_args(L, 1, "getsockname"));
2268
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002269 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002270
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002271 /* Check if we run on the same thread than the xreator thread.
2272 * We cannot access to the socket if the thread is different.
2273 */
2274 if (socket->tid != tid)
2275 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2276
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002277 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002278 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002279 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280 lua_pushnil(L);
2281 return 1;
2282 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002283 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2284 si = appctx->owner;
2285 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002286
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002287 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002288 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002289 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290 lua_pushnil(L);
2291 return 1;
2292 }
2293
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002294 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 xref_unlock(&socket->xref, peer);
2296 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297}
2298
2299/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002300static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 .obj_type = OBJ_TYPE_APPLET,
2302 .name = "<LUA_TCP>",
2303 .fct = hlua_socket_handler,
2304 .release = hlua_socket_release,
2305};
2306
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002307__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308{
2309 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2310 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002311 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002313 struct stream_interface *si;
2314 struct stream *s;
2315
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002316 /* Check if we run on the same thread than the xreator thread.
2317 * We cannot access to the socket if the thread is different.
2318 */
2319 if (socket->tid != tid)
2320 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2321
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002322 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002323 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002324 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002325 lua_pushnil(L);
2326 lua_pushstring(L, "Can't connect");
2327 return 2;
2328 }
2329 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2330 si = appctx->owner;
2331 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002332
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002333 /* Check if we run on the same thread than the xreator thread.
2334 * We cannot access to the socket if the thread is different.
2335 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002336 if (socket->tid != tid) {
2337 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002338 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002339 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002340
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002341 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002343 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002344 lua_pushnil(L);
2345 lua_pushstring(L, "Can't connect");
2346 return 2;
2347 }
2348
Willy Tarreaue09101e2018-10-16 17:37:12 +02002349 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
2351 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002352 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002353 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002354 lua_pushinteger(L, 1);
2355 return 1;
2356 }
2357
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002358 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2359 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002361 }
2362 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002363 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002364 return 0;
2365}
2366
2367/* This function fail or initite the connection. */
2368__LJMP static int hlua_socket_connect(struct lua_State *L)
2369{
2370 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002371 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002372 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002373 struct hlua *hlua;
2374 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002375 int low, high;
2376 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002377 struct xref *peer;
2378 struct stream_interface *si;
2379 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002380
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002381 if (lua_gettop(L) < 2)
2382 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002383
2384 /* Get args. */
2385 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002386
2387 /* Check if we run on the same thread than the xreator thread.
2388 * We cannot access to the socket if the thread is different.
2389 */
2390 if (socket->tid != tid)
2391 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2392
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002393 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002394 if (lua_gettop(L) >= 3) {
2395 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002396 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002397
Tim Duesterhus6edab862018-01-06 19:04:45 +01002398 /* Force the ip to end with a colon, to support IPv6 addresses
2399 * that are not enclosed within square brackets.
2400 */
2401 if (port > 0) {
2402 luaL_buffinit(L, &b);
2403 luaL_addstring(&b, ip);
2404 luaL_addchar(&b, ':');
2405 luaL_pushresult(&b);
2406 ip = lua_tolstring(L, lua_gettop(L), NULL);
2407 }
2408 }
2409
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002410 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002411 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002412 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002413 lua_pushnil(L);
2414 return 1;
2415 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416
2417 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002418 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002419 if (!addr) {
2420 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002421 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002422 }
2423 if (low != high) {
2424 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002425 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002426 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002427
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002428 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002429 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002430 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002431 if (port == -1) {
2432 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002433 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002434 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002435 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2436 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002437 if (port == -1) {
2438 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002439 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002440 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002441 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002442 }
2443 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002444
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002445 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2446 si = appctx->owner;
2447 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002448
2449 if (!sockaddr_alloc(&s->target_addr)) {
2450 xref_unlock(&socket->xref, peer);
2451 WILL_LJMP(luaL_error(L, "connect: internal error"));
2452 }
2453 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002454 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002455
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002456 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002457
2458 /* inform the stream that we want to be notified whenever the
2459 * connection completes.
2460 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002461 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002462 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002463 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002464
Willy Tarreauf31af932020-01-14 09:59:38 +01002465 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002466
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002467 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2468 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002469 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002470 }
2471 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002472
2473 task_wakeup(s->task, TASK_WOKEN_INIT);
2474 /* Return yield waiting for connection. */
2475
Willy Tarreau9635e032018-10-16 17:52:55 +02002476 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002477
2478 return 0;
2479}
2480
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002481#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002482__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2483{
2484 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002485 struct xref *peer;
2486 struct appctx *appctx;
2487 struct stream_interface *si;
2488 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002489
2490 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2491 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002492
2493 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002494 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002495 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002496 lua_pushnil(L);
2497 return 1;
2498 }
2499 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2500 si = appctx->owner;
2501 s = si_strm(si);
2502
2503 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002504 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002505 return MAY_LJMP(hlua_socket_connect(L));
2506}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002507#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002508
2509__LJMP static int hlua_socket_setoption(struct lua_State *L)
2510{
2511 return 0;
2512}
2513
2514__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2515{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002516 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002517 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002518 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002519 struct xref *peer;
2520 struct appctx *appctx;
2521 struct stream_interface *si;
2522 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002523
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002524 MAY_LJMP(check_args(L, 2, "settimeout"));
2525
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002526 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002527
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002528 /* convert the timeout to millis */
2529 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002530
Thierry Fournier17a921b2018-03-08 09:59:02 +01002531 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002532 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002533 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2534
Mark Lakes56cc1252018-03-27 09:48:06 +02002535 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002536 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002537
2538 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002539 if (tmout == 0)
2540 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002541
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002542 /* Check if we run on the same thread than the xreator thread.
2543 * We cannot access to the socket if the thread is different.
2544 */
2545 if (socket->tid != tid)
2546 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2547
Mark Lakes56cc1252018-03-27 09:48:06 +02002548 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002549 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002550 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002551 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2552 WILL_LJMP(lua_error(L));
2553 return 0;
2554 }
2555 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2556 si = appctx->owner;
2557 s = si_strm(si);
2558
Cyril Bonté7bb63452018-08-17 23:51:02 +02002559 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 s->req.rto = tmout;
2561 s->req.wto = tmout;
2562 s->res.rto = tmout;
2563 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002564 s->req.rex = tick_add_ifset(now_ms, tmout);
2565 s->req.wex = tick_add_ifset(now_ms, tmout);
2566 s->res.rex = tick_add_ifset(now_ms, tmout);
2567 s->res.wex = tick_add_ifset(now_ms, tmout);
2568
2569 s->task->expire = tick_add_ifset(now_ms, tmout);
2570 task_queue(s->task);
2571
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002572 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002573
Thierry Fourniere9636f12018-03-08 09:54:32 +01002574 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002575 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002576}
2577
2578__LJMP static int hlua_socket_new(lua_State *L)
2579{
2580 struct hlua_socket *socket;
2581 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002582 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002583 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584
2585 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002586 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587 hlua_pusherror(L, "socket: full stack");
2588 goto out_fail_conf;
2589 }
2590
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002591 /* Create the object: obj[0] = userdata. */
2592 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002594 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002595 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002596 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002597
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002598 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002599 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002600 hlua_pusherror(L, "socket: uninitialized pools.");
2601 goto out_fail_conf;
2602 }
2603
Willy Tarreau87b09662015-04-03 00:22:06 +02002604 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2606 lua_setmetatable(L, -2);
2607
Willy Tarreaud420a972015-04-06 00:39:18 +02002608 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002609 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002610 if (!appctx) {
2611 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002612 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002613 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002615 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002616 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002617 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2618 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002619
Willy Tarreaud420a972015-04-06 00:39:18 +02002620 /* Now create a session, task and stream for this applet */
2621 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2622 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002623 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002624 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002625 }
2626
Willy Tarreau87787ac2017-08-28 16:22:54 +02002627 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002628 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002629 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002630 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631 }
2632
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002633 /* Initialise cross reference between stream and Lua socket object. */
2634 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636 /* Configure "right" stream interface. this "si" is used to connect
2637 * and retrieve data from the server. The connection is initialized
2638 * with the "struct server".
2639 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002640 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641
2642 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002643 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002644 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 return 1;
2647
Willy Tarreaud420a972015-04-06 00:39:18 +02002648 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002649 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002650 out_fail_sess:
2651 appctx_free(appctx);
2652 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653 WILL_LJMP(lua_error(L));
2654 return 0;
2655}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002656
2657/*
2658 *
2659 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002660 * Class Channel
2661 *
2662 *
2663 */
2664
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002665/* This function is called before the Lua execution. It stores
2666 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002667 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002668static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002669{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002670 c->mode = stream->be->mode;
2671 switch (c->mode) {
2672 case PR_MODE_HTTP:
2673 c->data.http.dir = opt & SMP_OPT_DIR;
2674 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2675 c->data.http.state = stream->txn->req.msg_state;
2676 else
2677 c->data.http.state = stream->txn->rsp.msg_state;
2678 break;
2679 default:
2680 break;
2681 }
2682}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002683
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002684/* This function is called after the Lua execution. it
2685 * returns true if the parser state is consistent, otherwise,
2686 * it return false.
2687 *
2688 * In HTTP mode, the parser state must be in the same state
2689 * or greater when we exit the function. Even if we do a
2690 * control yield. This prevent to break the HTTP message
2691 * from the Lua code.
2692 */
2693static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2694{
2695 if (c->mode != stream->be->mode)
2696 return 0;
2697
2698 switch (c->mode) {
2699 case PR_MODE_HTTP:
2700 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002701 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002702 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2703 return stream->txn->req.msg_state >= c->data.http.state;
2704 else
2705 return stream->txn->rsp.msg_state >= c->data.http.state;
2706 default:
2707 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002708 }
2709 return 1;
2710}
2711
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002712/* Returns the struct hlua_channel join to the class channel in the
2713 * stack entry "ud" or throws an argument error.
2714 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002715__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002716{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002717 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002718}
2719
Willy Tarreau47860ed2015-03-10 14:07:50 +01002720/* Pushes the channel onto the top of the stack. If the stask does not have a
2721 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002722 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002723static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002724{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002725 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002726 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002727 return 0;
2728
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002729 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002730 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002731 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002732
2733 /* Pop a class sesison metatable and affect it to the userdata. */
2734 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2735 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002736 return 1;
2737}
2738
2739/* Duplicate all the data present in the input channel and put it
2740 * in a string LUA variables. Returns -1 and push a nil value in
2741 * the stack if the channel is closed and all the data are consumed,
2742 * returns 0 if no data are available, otherwise it returns the length
2743 * of the builded string.
2744 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002745static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746{
2747 char *blk1;
2748 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002749 size_t len1;
2750 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002751 int ret;
2752 luaL_Buffer b;
2753
Willy Tarreau06d80a92017-10-19 14:32:15 +02002754 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002755 if (unlikely(ret == 0))
2756 return 0;
2757
2758 if (unlikely(ret < 0)) {
2759 lua_pushnil(L);
2760 return -1;
2761 }
2762
2763 luaL_buffinit(L, &b);
2764 luaL_addlstring(&b, blk1, len1);
2765 if (unlikely(ret == 2))
2766 luaL_addlstring(&b, blk2, len2);
2767 luaL_pushresult(&b);
2768
2769 if (unlikely(ret == 2))
2770 return len1 + len2;
2771 return len1;
2772}
2773
2774/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2775 * a yield. This function keep the data in the buffer.
2776 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002777__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002779 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002780
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002781 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2782
Christopher Faulet3f829a42018-12-13 21:56:45 +01002783 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2784 WILL_LJMP(lua_error(L));
2785
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002786 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002787 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002788 return 1;
2789}
2790
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002791/* Check arguments for the function "hlua_channel_dup_yield". */
2792__LJMP static int hlua_channel_dup(lua_State *L)
2793{
2794 MAY_LJMP(check_args(L, 1, "dup"));
2795 MAY_LJMP(hlua_checkchannel(L, 1));
2796 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2797}
2798
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002799/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2800 * a yield. This function consumes the data in the buffer. It returns
2801 * a string containing the data or a nil pointer if no data are available
2802 * and the channel is closed.
2803 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002804__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002805{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002806 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002807 int ret;
2808
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002809 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002810
Christopher Faulet3f829a42018-12-13 21:56:45 +01002811 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2812 WILL_LJMP(lua_error(L));
2813
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002814 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002815 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002816 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002817
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002818 if (unlikely(ret == -1))
2819 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002820
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002821 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002822 return 1;
2823}
2824
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002825/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002826__LJMP static int hlua_channel_get(lua_State *L)
2827{
2828 MAY_LJMP(check_args(L, 1, "get"));
2829 MAY_LJMP(hlua_checkchannel(L, 1));
2830 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2831}
2832
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002833/* This functions consumes and returns one line. If the channel is closed,
2834 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002835 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002836 * value.
2837 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002838__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002839{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002840 char *blk1;
2841 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002842 size_t len1;
2843 size_t len2;
2844 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002845 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002846 int ret;
2847 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002848
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002849 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2850
Christopher Faulet3f829a42018-12-13 21:56:45 +01002851 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2852 WILL_LJMP(lua_error(L));
2853
Willy Tarreau06d80a92017-10-19 14:32:15 +02002854 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002855 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002856 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002857
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858 if (ret == -1) {
2859 lua_pushnil(L);
2860 return 1;
2861 }
2862
2863 luaL_buffinit(L, &b);
2864 luaL_addlstring(&b, blk1, len1);
2865 len = len1;
2866 if (unlikely(ret == 2)) {
2867 luaL_addlstring(&b, blk2, len2);
2868 len += len2;
2869 }
2870 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002871 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002872 return 1;
2873}
2874
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002875/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002876__LJMP static int hlua_channel_getline(lua_State *L)
2877{
2878 MAY_LJMP(check_args(L, 1, "getline"));
2879 MAY_LJMP(hlua_checkchannel(L, 1));
2880 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2881}
2882
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002883/* This function takes a string as input, and append it at the
2884 * input side of channel. If the data is too big, but a space
2885 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002886 * yields. If the data is bigger than the buffer, or if the
2887 * channel is closed, it returns -1. Otherwise, it returns the
2888 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002889 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002890__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002891{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002892 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893 size_t len;
2894 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2895 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2896 int ret;
2897 int max;
2898
Christopher Faulet3f829a42018-12-13 21:56:45 +01002899 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2900 WILL_LJMP(lua_error(L));
2901
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002902 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002903 * the request buffer if its not required.
2904 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002905 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002906 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002907 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002908 }
2909
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002910 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911 if (max > len - l)
2912 max = len - l;
2913
Willy Tarreau06d80a92017-10-19 14:32:15 +02002914 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915 if (ret == -2 || ret == -3) {
2916 lua_pushinteger(L, -1);
2917 return 1;
2918 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002919 if (ret == -1) {
2920 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002921 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002922 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002923 l += ret;
2924 lua_pop(L, 1);
2925 lua_pushinteger(L, l);
2926
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002927 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002928 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002929 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002930 * in this case, we cannot add more data, so we cannot yield,
2931 * we return the amount of copyied data.
2932 */
2933 return 1;
2934 }
2935 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002936 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 return 1;
2938}
2939
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002940/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2941 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002942 * buffer size is too little for the data.
2943 */
2944__LJMP static int hlua_channel_append(lua_State *L)
2945{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002946 size_t len;
2947
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002949 MAY_LJMP(hlua_checkchannel(L, 1));
2950 MAY_LJMP(luaL_checklstring(L, 2, &len));
2951 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952 lua_pushinteger(L, 0);
2953
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002954 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955}
2956
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002957/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002958 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002959 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002960 * or -1 if the channel is closed or if the buffer size is too
2961 * little for the data.
2962 */
2963__LJMP static int hlua_channel_set(lua_State *L)
2964{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002965 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002966
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002968 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002969 lua_pushinteger(L, 0);
2970
Christopher Faulet3f829a42018-12-13 21:56:45 +01002971 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2972 WILL_LJMP(lua_error(L));
2973
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002974 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002975
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002976 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977}
2978
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002979/* Append data in the output side of the buffer. This data is immediately
2980 * sent. The function returns the amount of data written. If the buffer
2981 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002982 * if the channel is closed.
2983 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002984__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002985{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002986 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002987 size_t len;
2988 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2989 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2990 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002991 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002992
Christopher Faulet3f829a42018-12-13 21:56:45 +01002993 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2994 WILL_LJMP(lua_error(L));
2995
Willy Tarreau47860ed2015-03-10 14:07:50 +01002996 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002997 lua_pushinteger(L, -1);
2998 return 1;
2999 }
3000
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003001 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003002 * the request buffer if its not required.
3003 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003004 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003005 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003006 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003007 }
3008
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003009 /* The written data will be immediately sent, so we can check
3010 * the available space without taking in account the reserve.
3011 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003012 * data, because the buffer will be flushed.
3013 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003014 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003015
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003016 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003017 * in this case, we cannot add more data, so we cannot yield,
3018 * we return the amount of copyied data.
3019 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003020 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003021 return 1;
3022
3023 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 if (max > len - l)
3025 max = len - l;
3026
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003027 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003028 * detects a non contiguous buffer and realign it.
3029 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003030 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003031 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003032
3033 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003034 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003035
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036 /* buffer replace considers that the input part is filled.
3037 * so, I must forward these new data in the output part.
3038 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003039 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003040
3041 l += max;
3042 lua_pop(L, 1);
3043 lua_pushinteger(L, l);
3044
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003045 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003046 * in this case, we cannot add more data, so we cannot yield,
3047 * we return the amount of copyied data.
3048 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003049 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003050 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003052
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003053 if (l < len) {
3054 /* If we are waiting for space in the response buffer, we
3055 * must set the flag WAKERESWR. This flag required the task
3056 * wake up if any activity is detected on the response buffer.
3057 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003058 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003059 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003060 else
3061 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003062 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003063 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064
3065 return 1;
3066}
3067
3068/* Just a wraper of "_hlua_channel_send". This wrapper permits
3069 * yield the LUA process, and resume it without checking the
3070 * input arguments.
3071 */
3072__LJMP static int hlua_channel_send(lua_State *L)
3073{
3074 MAY_LJMP(check_args(L, 2, "send"));
3075 lua_pushinteger(L, 0);
3076
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003077 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003078}
3079
3080/* This function forward and amount of butes. The data pass from
3081 * the input side of the buffer to the output side, and can be
3082 * forwarded. This function never fails.
3083 *
3084 * The Lua function takes an amount of bytes to be forwarded in
3085 * imput. It returns the number of bytes forwarded.
3086 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003087__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003088{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003089 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003090 int len;
3091 int l;
3092 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003093 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003094
3095 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003096
3097 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3098 WILL_LJMP(lua_error(L));
3099
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003100 len = MAY_LJMP(luaL_checkinteger(L, 2));
3101 l = MAY_LJMP(luaL_checkinteger(L, -1));
3102
3103 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003104 if (max > ci_data(chn))
3105 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003106 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107 l += max;
3108
3109 lua_pop(L, 1);
3110 lua_pushinteger(L, l);
3111
3112 /* Check if it miss bytes to forward. */
3113 if (l < len) {
3114 /* The the input channel or the output channel are closed, we
3115 * must return the amount of data forwarded.
3116 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003117 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003118 return 1;
3119
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003120 /* If we are waiting for space data in the response buffer, we
3121 * must set the flag WAKERESWR. This flag required the task
3122 * wake up if any activity is detected on the response buffer.
3123 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003124 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003125 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003126 else
3127 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003128
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003129 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003130 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003131 }
3132
3133 return 1;
3134}
3135
3136/* Just check the input and prepare the stack for the previous
3137 * function "hlua_channel_forward_yield"
3138 */
3139__LJMP static int hlua_channel_forward(lua_State *L)
3140{
3141 MAY_LJMP(check_args(L, 2, "forward"));
3142 MAY_LJMP(hlua_checkchannel(L, 1));
3143 MAY_LJMP(luaL_checkinteger(L, 2));
3144
3145 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003146 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003147}
3148
3149/* Just returns the number of bytes available in the input
3150 * side of the buffer. This function never fails.
3151 */
3152__LJMP static int hlua_channel_get_in_len(lua_State *L)
3153{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003154 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003155
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003156 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003157 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003158 if (IS_HTX_STRM(chn_strm(chn))) {
3159 struct htx *htx = htxbuf(&chn->buf);
3160 lua_pushinteger(L, htx->data - co_data(chn));
3161 }
3162 else
3163 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164 return 1;
3165}
3166
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003167/* Returns true if the channel is full. */
3168__LJMP static int hlua_channel_is_full(lua_State *L)
3169{
3170 struct channel *chn;
3171 int rem;
3172
3173 MAY_LJMP(check_args(L, 1, "is_full"));
3174 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3175
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003176 if (IS_HTX_STRM(chn_strm(chn))) {
3177 struct htx *htx = htxbuf(&chn->buf);
3178
3179 rem = htx_free_data_space(htx);
3180 }
3181 else
3182 rem = b_room(&chn->buf);
3183
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003184 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3185
3186 lua_pushboolean(L, rem <= 0);
3187 return 1;
3188}
3189
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003190/* Just returns the number of bytes available in the output
3191 * side of the buffer. This function never fails.
3192 */
3193__LJMP static int hlua_channel_get_out_len(lua_State *L)
3194{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003195 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003196
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003197 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003198 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003199 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003200 return 1;
3201}
3202
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003203/*
3204 *
3205 *
3206 * Class Fetches
3207 *
3208 *
3209 */
3210
3211/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003212 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003213 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003214__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003215{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003216 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003217}
3218
3219/* This function creates and push in the stack a fetch object according
3220 * with a current TXN.
3221 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003222static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003223{
Willy Tarreau7073c472015-04-06 11:15:40 +02003224 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003225
3226 /* Check stack size. */
3227 if (!lua_checkstack(L, 3))
3228 return 0;
3229
3230 /* Create the object: obj[0] = userdata.
3231 * Note that the base of the Fetches object is the
3232 * transaction object.
3233 */
3234 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003235 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003236 lua_rawseti(L, -2, 0);
3237
Willy Tarreau7073c472015-04-06 11:15:40 +02003238 hsmp->s = txn->s;
3239 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003240 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003241 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003242
3243 /* Pop a class sesison metatable and affect it to the userdata. */
3244 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3245 lua_setmetatable(L, -2);
3246
3247 return 1;
3248}
3249
3250/* This function is an LUA binding. It is called with each sample-fetch.
3251 * It uses closure argument to store the associated sample-fetch. It
3252 * returns only one argument or throws an error. An error is thrown
3253 * only if an error is encountered during the argument parsing. If
3254 * the "sample-fetch" function fails, nil is returned.
3255 */
3256__LJMP static int hlua_run_sample_fetch(lua_State *L)
3257{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003258 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003259 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003260 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003261 int i;
3262 struct sample smp;
3263
3264 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003265 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003266
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003267 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003268 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003269
Thierry FOURNIERca988662015-12-20 18:43:03 +01003270 /* Check execution authorization. */
3271 if (f->use & SMP_USE_HTTP_ANY &&
3272 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3273 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3274 "is not available in Lua services", f->kw);
3275 WILL_LJMP(lua_error(L));
3276 }
3277
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003278 /* Get extra arguments. */
3279 for (i = 0; i < lua_gettop(L) - 1; i++) {
3280 if (i >= ARGM_NBARGS)
3281 break;
3282 hlua_lua2arg(L, i + 2, &args[i]);
3283 }
3284 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003285 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003286
3287 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003288 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003289
3290 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003291 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003292 lua_pushfstring(L, "error in arguments");
3293 WILL_LJMP(lua_error(L));
3294 }
3295
3296 /* Initialise the sample. */
3297 memset(&smp, 0, sizeof(smp));
3298
3299 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003300 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003301 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003302 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003303 lua_pushstring(L, "");
3304 else
3305 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003306 return 1;
3307 }
3308
3309 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003310 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003311 hlua_smp2lua_str(L, &smp);
3312 else
3313 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003314 return 1;
3315}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003316
3317/*
3318 *
3319 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003320 * Class Converters
3321 *
3322 *
3323 */
3324
3325/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003326 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003327 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003328__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003329{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003330 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003331}
3332
3333/* This function creates and push in the stack a Converters object
3334 * according with a current TXN.
3335 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003336static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003337{
Willy Tarreau7073c472015-04-06 11:15:40 +02003338 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003339
3340 /* Check stack size. */
3341 if (!lua_checkstack(L, 3))
3342 return 0;
3343
3344 /* Create the object: obj[0] = userdata.
3345 * Note that the base of the Converters object is the
3346 * same than the TXN object.
3347 */
3348 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003349 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350 lua_rawseti(L, -2, 0);
3351
Willy Tarreau7073c472015-04-06 11:15:40 +02003352 hsmp->s = txn->s;
3353 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003354 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003355 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003356
Willy Tarreau87b09662015-04-03 00:22:06 +02003357 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003358 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3359 lua_setmetatable(L, -2);
3360
3361 return 1;
3362}
3363
3364/* This function is an LUA binding. It is called with each converter.
3365 * It uses closure argument to store the associated converter. It
3366 * returns only one argument or throws an error. An error is thrown
3367 * only if an error is encountered during the argument parsing. If
3368 * the converter function function fails, nil is returned.
3369 */
3370__LJMP static int hlua_run_sample_conv(lua_State *L)
3371{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003372 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003373 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003374 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375 int i;
3376 struct sample smp;
3377
3378 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003379 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003380
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003381 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003382 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003383
3384 /* Get extra arguments. */
3385 for (i = 0; i < lua_gettop(L) - 2; i++) {
3386 if (i >= ARGM_NBARGS)
3387 break;
3388 hlua_lua2arg(L, i + 3, &args[i]);
3389 }
3390 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003391 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003392
3393 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003394 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003395
3396 /* Run the special args checker. */
3397 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3398 hlua_pusherror(L, "error in arguments");
3399 WILL_LJMP(lua_error(L));
3400 }
3401
3402 /* Initialise the sample. */
3403 if (!hlua_lua2smp(L, 2, &smp)) {
3404 hlua_pusherror(L, "error in the input argument");
3405 WILL_LJMP(lua_error(L));
3406 }
3407
Willy Tarreau1777ea62016-03-10 16:15:46 +01003408 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3409
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003410 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003411 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003412 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003413 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003414 WILL_LJMP(lua_error(L));
3415 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003416 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3417 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003418 hlua_pusherror(L, "error during the input argument casting");
3419 WILL_LJMP(lua_error(L));
3420 }
3421
3422 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003423 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003424 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003425 lua_pushstring(L, "");
3426 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003427 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003428 return 1;
3429 }
3430
3431 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003432 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003433 hlua_smp2lua_str(L, &smp);
3434 else
3435 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003436 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003437}
3438
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003439/*
3440 *
3441 *
3442 * Class AppletTCP
3443 *
3444 *
3445 */
3446
3447/* Returns a struct hlua_txn if the stack entry "ud" is
3448 * a class stream, otherwise it throws an error.
3449 */
3450__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3451{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003452 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003453}
3454
3455/* This function creates and push in the stack an Applet object
3456 * according with a current TXN.
3457 */
3458static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3459{
3460 struct hlua_appctx *appctx;
3461 struct stream_interface *si = ctx->owner;
3462 struct stream *s = si_strm(si);
3463 struct proxy *p = s->be;
3464
3465 /* Check stack size. */
3466 if (!lua_checkstack(L, 3))
3467 return 0;
3468
3469 /* Create the object: obj[0] = userdata.
3470 * Note that the base of the Converters object is the
3471 * same than the TXN object.
3472 */
3473 lua_newtable(L);
3474 appctx = lua_newuserdata(L, sizeof(*appctx));
3475 lua_rawseti(L, -2, 0);
3476 appctx->appctx = ctx;
3477 appctx->htxn.s = s;
3478 appctx->htxn.p = p;
3479
3480 /* Create the "f" field that contains a list of fetches. */
3481 lua_pushstring(L, "f");
3482 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3483 return 0;
3484 lua_settable(L, -3);
3485
3486 /* Create the "sf" field that contains a list of stringsafe fetches. */
3487 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003488 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003489 return 0;
3490 lua_settable(L, -3);
3491
3492 /* Create the "c" field that contains a list of converters. */
3493 lua_pushstring(L, "c");
3494 if (!hlua_converters_new(L, &appctx->htxn, 0))
3495 return 0;
3496 lua_settable(L, -3);
3497
3498 /* Create the "sc" field that contains a list of stringsafe converters. */
3499 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003500 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003501 return 0;
3502 lua_settable(L, -3);
3503
3504 /* Pop a class stream metatable and affect it to the table. */
3505 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3506 lua_setmetatable(L, -2);
3507
3508 return 1;
3509}
3510
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003511__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3512{
3513 struct hlua_appctx *appctx;
3514 struct stream *s;
3515 const char *name;
3516 size_t len;
3517 struct sample smp;
3518
3519 MAY_LJMP(check_args(L, 3, "set_var"));
3520
3521 /* It is useles to retrieve the stream, but this function
3522 * runs only in a stream context.
3523 */
3524 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3525 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3526 s = appctx->htxn.s;
3527
3528 /* Converts the third argument in a sample. */
3529 hlua_lua2smp(L, 3, &smp);
3530
3531 /* Store the sample in a variable. */
3532 smp_set_owner(&smp, s->be, s->sess, s, 0);
3533 vars_set_by_name(name, len, &smp);
3534 return 0;
3535}
3536
3537__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3538{
3539 struct hlua_appctx *appctx;
3540 struct stream *s;
3541 const char *name;
3542 size_t len;
3543 struct sample smp;
3544
3545 MAY_LJMP(check_args(L, 2, "unset_var"));
3546
3547 /* It is useles to retrieve the stream, but this function
3548 * runs only in a stream context.
3549 */
3550 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3551 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3552 s = appctx->htxn.s;
3553
3554 /* Unset the variable. */
3555 smp_set_owner(&smp, s->be, s->sess, s, 0);
3556 vars_unset_by_name(name, len, &smp);
3557 return 0;
3558}
3559
3560__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3561{
3562 struct hlua_appctx *appctx;
3563 struct stream *s;
3564 const char *name;
3565 size_t len;
3566 struct sample smp;
3567
3568 MAY_LJMP(check_args(L, 2, "get_var"));
3569
3570 /* It is useles to retrieve the stream, but this function
3571 * runs only in a stream context.
3572 */
3573 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3574 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3575 s = appctx->htxn.s;
3576
3577 smp_set_owner(&smp, s->be, s->sess, s, 0);
3578 if (!vars_get_by_name(name, len, &smp)) {
3579 lua_pushnil(L);
3580 return 1;
3581 }
3582
3583 return hlua_smp2lua(L, &smp);
3584}
3585
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003586__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3587{
3588 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3589 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003590 struct hlua *hlua;
3591
3592 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003593 if (!s->hlua)
3594 return 0;
3595 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003596
3597 MAY_LJMP(check_args(L, 2, "set_priv"));
3598
3599 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003600 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003601
3602 /* Get and store new value. */
3603 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3604 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3605
3606 return 0;
3607}
3608
3609__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3610{
3611 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3612 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003613 struct hlua *hlua;
3614
3615 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003616 if (!s->hlua) {
3617 lua_pushnil(L);
3618 return 1;
3619 }
3620 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003621
3622 /* Push configuration index in the stack. */
3623 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3624
3625 return 1;
3626}
3627
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003628/* If expected data not yet available, it returns a yield. This function
3629 * consumes the data in the buffer. It returns a string containing the
3630 * data. This string can be empty.
3631 */
3632__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3633{
3634 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3635 struct stream_interface *si = appctx->appctx->owner;
3636 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003637 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003638 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003639 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003640 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003641
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003642 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003643 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003644
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003645 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003646 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003647 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003648 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003649 }
3650
3651 /* End of data: commit the total strings and return. */
3652 if (ret < 0) {
3653 luaL_pushresult(&appctx->b);
3654 return 1;
3655 }
3656
3657 /* Ensure that the block 2 length is usable. */
3658 if (ret == 1)
3659 len2 = 0;
3660
3661 /* dont check the max length read and dont check. */
3662 luaL_addlstring(&appctx->b, blk1, len1);
3663 luaL_addlstring(&appctx->b, blk2, len2);
3664
3665 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003666 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003667 luaL_pushresult(&appctx->b);
3668 return 1;
3669}
3670
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003671/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003672__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3673{
3674 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3675
3676 /* Initialise the string catenation. */
3677 luaL_buffinit(L, &appctx->b);
3678
3679 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3680}
3681
3682/* If expected data not yet available, it returns a yield. This function
3683 * consumes the data in the buffer. It returns a string containing the
3684 * data. This string can be empty.
3685 */
3686__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3687{
3688 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3689 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003690 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003691 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003692 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003693 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003694 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003695 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003696
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003697 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003698 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003699
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003700 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003701 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003702 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003703 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003704 }
3705
3706 /* End of data: commit the total strings and return. */
3707 if (ret < 0) {
3708 luaL_pushresult(&appctx->b);
3709 return 1;
3710 }
3711
3712 /* Ensure that the block 2 length is usable. */
3713 if (ret == 1)
3714 len2 = 0;
3715
3716 if (len == -1) {
3717
3718 /* If len == -1, catenate all the data avalaile and
3719 * yield because we want to get all the data until
3720 * the end of data stream.
3721 */
3722 luaL_addlstring(&appctx->b, blk1, len1);
3723 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003724 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003725 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003726 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003727
3728 } else {
3729
3730 /* Copy the fisrt block caping to the length required. */
3731 if (len1 > len)
3732 len1 = len;
3733 luaL_addlstring(&appctx->b, blk1, len1);
3734 len -= len1;
3735
3736 /* Copy the second block. */
3737 if (len2 > len)
3738 len2 = len;
3739 luaL_addlstring(&appctx->b, blk2, len2);
3740 len -= len2;
3741
3742 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003743 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003744
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003745 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003746 if (len > 0) {
3747 lua_pushinteger(L, len);
3748 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003749 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003750 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003751 }
3752
3753 /* return the result. */
3754 luaL_pushresult(&appctx->b);
3755 return 1;
3756 }
3757
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003758 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003759 hlua_pusherror(L, "Lua: internal error");
3760 WILL_LJMP(lua_error(L));
3761 return 0;
3762}
3763
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003764/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003765__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3766{
3767 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3768 int len = -1;
3769
3770 if (lua_gettop(L) > 2)
3771 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3772 if (lua_gettop(L) >= 2) {
3773 len = MAY_LJMP(luaL_checkinteger(L, 2));
3774 lua_pop(L, 1);
3775 }
3776
3777 /* Confirm or set the required length */
3778 lua_pushinteger(L, len);
3779
3780 /* Initialise the string catenation. */
3781 luaL_buffinit(L, &appctx->b);
3782
3783 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3784}
3785
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003786/* Append data in the output side of the buffer. This data is immediately
3787 * sent. The function returns the amount of data written. If the buffer
3788 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003789 * if the channel is closed.
3790 */
3791__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3792{
3793 size_t len;
3794 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3795 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3796 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3797 struct stream_interface *si = appctx->appctx->owner;
3798 struct channel *chn = si_ic(si);
3799 int max;
3800
3801 /* Get the max amount of data which can write as input in the channel. */
3802 max = channel_recv_max(chn);
3803 if (max > (len - l))
3804 max = len - l;
3805
3806 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003807 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003808
3809 /* update counters. */
3810 l += max;
3811 lua_pop(L, 1);
3812 lua_pushinteger(L, l);
3813
3814 /* If some data is not send, declares the situation to the
3815 * applet, and returns a yield.
3816 */
3817 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003818 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003819 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003820 }
3821
3822 return 1;
3823}
3824
3825/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3826 * yield the LUA process, and resume it without checking the
3827 * input arguments.
3828 */
3829__LJMP static int hlua_applet_tcp_send(lua_State *L)
3830{
3831 MAY_LJMP(check_args(L, 2, "send"));
3832 lua_pushinteger(L, 0);
3833
3834 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3835}
3836
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003837/*
3838 *
3839 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003840 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003841 *
3842 *
3843 */
3844
3845/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003846 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003847 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003848__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003849{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003850 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003851}
3852
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003853/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003854 * according with a current TXN.
3855 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003856static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003857{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003858 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003859 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003860 struct stream_interface *si = ctx->owner;
3861 struct stream *s = si_strm(si);
3862 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003863 struct htx *htx;
3864 struct htx_blk *blk;
3865 struct htx_sl *sl;
3866 struct ist path;
3867 unsigned long long len = 0;
3868 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003869
3870 /* Check stack size. */
3871 if (!lua_checkstack(L, 3))
3872 return 0;
3873
3874 /* Create the object: obj[0] = userdata.
3875 * Note that the base of the Converters object is the
3876 * same than the TXN object.
3877 */
3878 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003880 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003881 appctx->appctx = ctx;
3882 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003883 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003884 appctx->htxn.s = s;
3885 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003886
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003887 /* Create the "f" field that contains a list of fetches. */
3888 lua_pushstring(L, "f");
3889 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3890 return 0;
3891 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003892
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003893 /* Create the "sf" field that contains a list of stringsafe fetches. */
3894 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003895 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003896 return 0;
3897 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003898
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003899 /* Create the "c" field that contains a list of converters. */
3900 lua_pushstring(L, "c");
3901 if (!hlua_converters_new(L, &appctx->htxn, 0))
3902 return 0;
3903 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003904
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003905 /* Create the "sc" field that contains a list of stringsafe converters. */
3906 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003907 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003908 return 0;
3909 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003910
Christopher Fauleta2097962019-07-15 16:25:33 +02003911 htx = htxbuf(&s->req.buf);
3912 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003913 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003914 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003915
Christopher Fauleta2097962019-07-15 16:25:33 +02003916 /* Stores the request method. */
3917 lua_pushstring(L, "method");
3918 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3919 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003920
Christopher Fauleta2097962019-07-15 16:25:33 +02003921 /* Stores the http version. */
3922 lua_pushstring(L, "version");
3923 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3924 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003925
Christopher Fauleta2097962019-07-15 16:25:33 +02003926 /* creates an array of headers. hlua_http_get_headers() crates and push
3927 * the array on the top of the stack.
3928 */
3929 lua_pushstring(L, "headers");
3930 htxn.s = s;
3931 htxn.p = px;
3932 htxn.dir = SMP_OPT_DIR_REQ;
3933 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3934 return 0;
3935 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003936
Christopher Fauleta2097962019-07-15 16:25:33 +02003937 path = http_get_path(htx_sl_req_uri(sl));
3938 if (path.ptr) {
3939 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003940
Christopher Fauleta2097962019-07-15 16:25:33 +02003941 p = path.ptr;
3942 end = path.ptr + path.len;
3943 q = p;
3944 while (q < end && *q != '?')
3945 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003946
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003947 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003948 lua_pushstring(L, "path");
3949 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003950 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003951
Christopher Fauleta2097962019-07-15 16:25:33 +02003952 /* Stores the query string. */
3953 lua_pushstring(L, "qs");
3954 if (*q == '?')
3955 q++;
3956 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003957 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003958 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003959
Christopher Fauleta2097962019-07-15 16:25:33 +02003960 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3961 struct htx_blk *blk = htx_get_blk(htx, pos);
3962 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003963
Christopher Fauleta2097962019-07-15 16:25:33 +02003964 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3965 break;
3966 if (type == HTX_BLK_DATA)
3967 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003968 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003969 if (htx->extra != ULLONG_MAX)
3970 len += htx->extra;
3971
3972 /* Stores the request path. */
3973 lua_pushstring(L, "length");
3974 lua_pushinteger(L, len);
3975 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003976
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003977 /* Create an empty array of HTTP request headers. */
3978 lua_pushstring(L, "response");
3979 lua_newtable(L);
3980 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003981
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003982 /* Pop a class stream metatable and affect it to the table. */
3983 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3984 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003985
3986 return 1;
3987}
3988
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003989__LJMP static int hlua_applet_http_set_var(lua_State *L)
3990{
3991 struct hlua_appctx *appctx;
3992 struct stream *s;
3993 const char *name;
3994 size_t len;
3995 struct sample smp;
3996
3997 MAY_LJMP(check_args(L, 3, "set_var"));
3998
3999 /* It is useles to retrieve the stream, but this function
4000 * runs only in a stream context.
4001 */
4002 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4003 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4004 s = appctx->htxn.s;
4005
4006 /* Converts the third argument in a sample. */
4007 hlua_lua2smp(L, 3, &smp);
4008
4009 /* Store the sample in a variable. */
4010 smp_set_owner(&smp, s->be, s->sess, s, 0);
4011 vars_set_by_name(name, len, &smp);
4012 return 0;
4013}
4014
4015__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4016{
4017 struct hlua_appctx *appctx;
4018 struct stream *s;
4019 const char *name;
4020 size_t len;
4021 struct sample smp;
4022
4023 MAY_LJMP(check_args(L, 2, "unset_var"));
4024
4025 /* It is useles to retrieve the stream, but this function
4026 * runs only in a stream context.
4027 */
4028 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4029 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4030 s = appctx->htxn.s;
4031
4032 /* Unset the variable. */
4033 smp_set_owner(&smp, s->be, s->sess, s, 0);
4034 vars_unset_by_name(name, len, &smp);
4035 return 0;
4036}
4037
4038__LJMP static int hlua_applet_http_get_var(lua_State *L)
4039{
4040 struct hlua_appctx *appctx;
4041 struct stream *s;
4042 const char *name;
4043 size_t len;
4044 struct sample smp;
4045
4046 MAY_LJMP(check_args(L, 2, "get_var"));
4047
4048 /* It is useles to retrieve the stream, but this function
4049 * runs only in a stream context.
4050 */
4051 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4052 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4053 s = appctx->htxn.s;
4054
4055 smp_set_owner(&smp, s->be, s->sess, s, 0);
4056 if (!vars_get_by_name(name, len, &smp)) {
4057 lua_pushnil(L);
4058 return 1;
4059 }
4060
4061 return hlua_smp2lua(L, &smp);
4062}
4063
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004064__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4065{
4066 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4067 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004068 struct hlua *hlua;
4069
4070 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004071 if (!s->hlua)
4072 return 0;
4073 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004074
4075 MAY_LJMP(check_args(L, 2, "set_priv"));
4076
4077 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004078 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004079
4080 /* Get and store new value. */
4081 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4082 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4083
4084 return 0;
4085}
4086
4087__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4088{
4089 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4090 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004091 struct hlua *hlua;
4092
4093 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004094 if (!s->hlua) {
4095 lua_pushnil(L);
4096 return 1;
4097 }
4098 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004099
4100 /* Push configuration index in the stack. */
4101 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4102
4103 return 1;
4104}
4105
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004106/* If expected data not yet available, it returns a yield. This function
4107 * consumes the data in the buffer. It returns a string containing the
4108 * data. This string can be empty.
4109 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004110__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004111{
4112 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4113 struct stream_interface *si = appctx->appctx->owner;
4114 struct channel *req = si_oc(si);
4115 struct htx *htx;
4116 struct htx_blk *blk;
4117 size_t count;
4118 int stop = 0;
4119
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004120 htx = htx_from_buf(&req->buf);
4121 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004122 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004123
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004124 while (count && !stop && blk) {
4125 enum htx_blk_type type = htx_get_blk_type(blk);
4126 uint32_t sz = htx_get_blksz(blk);
4127 struct ist v;
4128 uint32_t vlen;
4129 char *nl;
4130
Christopher Faulete461e342018-12-18 16:43:35 +01004131 if (type == HTX_BLK_EOM) {
4132 stop = 1;
4133 break;
4134 }
4135
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004136 vlen = sz;
4137 if (vlen > count) {
4138 if (type != HTX_BLK_DATA)
4139 break;
4140 vlen = count;
4141 }
4142
4143 switch (type) {
4144 case HTX_BLK_UNUSED:
4145 break;
4146
4147 case HTX_BLK_DATA:
4148 v = htx_get_blk_value(htx, blk);
4149 v.len = vlen;
4150 nl = istchr(v, '\n');
4151 if (nl != NULL) {
4152 stop = 1;
4153 vlen = nl - v.ptr + 1;
4154 }
4155 luaL_addlstring(&appctx->b, v.ptr, vlen);
4156 break;
4157
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004158 case HTX_BLK_TLR:
4159 case HTX_BLK_EOM:
4160 stop = 1;
4161 break;
4162
4163 default:
4164 break;
4165 }
4166
4167 co_set_data(req, co_data(req) - vlen);
4168 count -= vlen;
4169 if (sz == vlen)
4170 blk = htx_remove_blk(htx, blk);
4171 else {
4172 htx_cut_data_blk(htx, blk, vlen);
4173 break;
4174 }
4175 }
4176
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004177 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004178 if (!stop) {
4179 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004180 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004181 }
4182
4183 /* return the result. */
4184 luaL_pushresult(&appctx->b);
4185 return 1;
4186}
4187
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004188
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004189/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004190__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004191{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004192 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004193
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004194 /* Initialise the string catenation. */
4195 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004196
Christopher Fauleta2097962019-07-15 16:25:33 +02004197 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004198}
4199
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004200/* If expected data not yet available, it returns a yield. This function
4201 * consumes the data in the buffer. It returns a string containing the
4202 * data. This string can be empty.
4203 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004204__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004205{
4206 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4207 struct stream_interface *si = appctx->appctx->owner;
4208 struct channel *req = si_oc(si);
4209 struct htx *htx;
4210 struct htx_blk *blk;
4211 size_t count;
4212 int len;
4213
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004214 htx = htx_from_buf(&req->buf);
4215 len = MAY_LJMP(luaL_checkinteger(L, 2));
4216 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004217 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004218 while (count && len && blk) {
4219 enum htx_blk_type type = htx_get_blk_type(blk);
4220 uint32_t sz = htx_get_blksz(blk);
4221 struct ist v;
4222 uint32_t vlen;
4223
Christopher Faulete461e342018-12-18 16:43:35 +01004224 if (type == HTX_BLK_EOM) {
4225 len = 0;
4226 break;
4227 }
4228
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004229 vlen = sz;
4230 if (len > 0 && vlen > len)
4231 vlen = len;
4232 if (vlen > count) {
4233 if (type != HTX_BLK_DATA)
4234 break;
4235 vlen = count;
4236 }
4237
4238 switch (type) {
4239 case HTX_BLK_UNUSED:
4240 break;
4241
4242 case HTX_BLK_DATA:
4243 v = htx_get_blk_value(htx, blk);
4244 luaL_addlstring(&appctx->b, v.ptr, vlen);
4245 break;
4246
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004247 case HTX_BLK_TLR:
4248 case HTX_BLK_EOM:
4249 len = 0;
4250 break;
4251
4252 default:
4253 break;
4254 }
4255
4256 co_set_data(req, co_data(req) - vlen);
4257 count -= vlen;
4258 if (len > 0)
4259 len -= vlen;
4260 if (sz == vlen)
4261 blk = htx_remove_blk(htx, blk);
4262 else {
4263 htx_cut_data_blk(htx, blk, vlen);
4264 break;
4265 }
4266 }
4267
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004268 htx_to_buf(htx, &req->buf);
4269
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004270 /* If we are no other data available, yield waiting for new data. */
4271 if (len) {
4272 if (len > 0) {
4273 lua_pushinteger(L, len);
4274 lua_replace(L, 2);
4275 }
4276 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004277 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004278 }
4279
4280 /* return the result. */
4281 luaL_pushresult(&appctx->b);
4282 return 1;
4283}
4284
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004285/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004286__LJMP static int hlua_applet_http_recv(lua_State *L)
4287{
4288 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4289 int len = -1;
4290
4291 /* Check arguments. */
4292 if (lua_gettop(L) > 2)
4293 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4294 if (lua_gettop(L) >= 2) {
4295 len = MAY_LJMP(luaL_checkinteger(L, 2));
4296 lua_pop(L, 1);
4297 }
4298
Christopher Fauleta2097962019-07-15 16:25:33 +02004299 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004300
Christopher Fauleta2097962019-07-15 16:25:33 +02004301 /* Initialise the string catenation. */
4302 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004303
Christopher Fauleta2097962019-07-15 16:25:33 +02004304 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004305}
4306
4307/* Append data in the output side of the buffer. This data is immediately
4308 * sent. The function returns the amount of data written. If the buffer
4309 * cannot contain the data, the function yields. The function returns -1
4310 * if the channel is closed.
4311 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004312__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004313{
4314 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4315 struct stream_interface *si = appctx->appctx->owner;
4316 struct channel *res = si_ic(si);
4317 struct htx *htx = htx_from_buf(&res->buf);
4318 const char *data;
4319 size_t len;
4320 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4321 int max;
4322
Christopher Faulet9060fc02019-07-03 11:39:30 +02004323 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004324 if (!max)
4325 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004326
4327 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4328
4329 /* Get the max amount of data which can write as input in the channel. */
4330 if (max > (len - l))
4331 max = len - l;
4332
4333 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004334 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004335 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004336
4337 /* update counters. */
4338 l += max;
4339 lua_pop(L, 1);
4340 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004341
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004342 /* If some data is not send, declares the situation to the
4343 * applet, and returns a yield.
4344 */
4345 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004346 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004347 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004348 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004349 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004350 }
4351
Christopher Fauleta2097962019-07-15 16:25:33 +02004352 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004353 return 1;
4354}
4355
4356/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4357 * yield the LUA process, and resume it without checking the
4358 * input arguments.
4359 */
4360__LJMP static int hlua_applet_http_send(lua_State *L)
4361{
4362 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004363
4364 /* We want to send some data. Headers must be sent. */
4365 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4366 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4367 WILL_LJMP(lua_error(L));
4368 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004369
Christopher Fauleta2097962019-07-15 16:25:33 +02004370 /* This interger is used for followinf the amount of data sent. */
4371 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004372
Christopher Fauleta2097962019-07-15 16:25:33 +02004373 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004374}
4375
4376__LJMP static int hlua_applet_http_addheader(lua_State *L)
4377{
4378 const char *name;
4379 int ret;
4380
4381 MAY_LJMP(hlua_checkapplet_http(L, 1));
4382 name = MAY_LJMP(luaL_checkstring(L, 2));
4383 MAY_LJMP(luaL_checkstring(L, 3));
4384
4385 /* Push in the stack the "response" entry. */
4386 ret = lua_getfield(L, 1, "response");
4387 if (ret != LUA_TTABLE) {
4388 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4389 "is expected as an array. %s found", lua_typename(L, ret));
4390 WILL_LJMP(lua_error(L));
4391 }
4392
4393 /* check if the header is already registered if it is not
4394 * the case, register it.
4395 */
4396 ret = lua_getfield(L, -1, name);
4397 if (ret == LUA_TNIL) {
4398
4399 /* Entry not found. */
4400 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4401
4402 /* Insert the new header name in the array in the top of the stack.
4403 * It left the new array in the top of the stack.
4404 */
4405 lua_newtable(L);
4406 lua_pushvalue(L, 2);
4407 lua_pushvalue(L, -2);
4408 lua_settable(L, -4);
4409
4410 } else if (ret != LUA_TTABLE) {
4411
4412 /* corruption error. */
4413 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4414 "is expected as an array. %s found", name, lua_typename(L, ret));
4415 WILL_LJMP(lua_error(L));
4416 }
4417
4418 /* Now the top od thestack is an array of values. We push
4419 * the header value as new entry.
4420 */
4421 lua_pushvalue(L, 3);
4422 ret = lua_rawlen(L, -2);
4423 lua_rawseti(L, -2, ret + 1);
4424 lua_pushboolean(L, 1);
4425 return 1;
4426}
4427
4428__LJMP static int hlua_applet_http_status(lua_State *L)
4429{
4430 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4431 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004432 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004433
4434 if (status < 100 || status > 599) {
4435 lua_pushboolean(L, 0);
4436 return 1;
4437 }
4438
4439 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004440 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004441 lua_pushboolean(L, 1);
4442 return 1;
4443}
4444
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004445
Christopher Fauleta2097962019-07-15 16:25:33 +02004446__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004447{
4448 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4449 struct stream_interface *si = appctx->appctx->owner;
4450 struct channel *res = si_ic(si);
4451 struct htx *htx;
4452 struct htx_sl *sl;
4453 struct h1m h1m;
4454 const char *status, *reason;
4455 const char *name, *value;
4456 size_t nlen, vlen;
4457 unsigned int flags;
4458
4459 /* Send the message at once. */
4460 htx = htx_from_buf(&res->buf);
4461 h1m_init_res(&h1m);
4462
4463 /* Use the same http version than the request. */
4464 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4465 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4466 if (reason == NULL)
4467 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4468 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4469 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4470 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4471 }
4472 else {
4473 flags = HTX_SL_F_IS_RESP;
4474 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4475 }
4476 if (!sl) {
4477 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4478 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4479 WILL_LJMP(lua_error(L));
4480 }
4481 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4482
4483 /* Get the array associated to the field "response" in the object AppletHTTP. */
4484 lua_pushvalue(L, 0);
4485 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4486 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4487 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4488 WILL_LJMP(lua_error(L));
4489 }
4490
4491 /* Browse the list of headers. */
4492 lua_pushnil(L);
4493 while(lua_next(L, -2) != 0) {
4494 /* We expect a string as -2. */
4495 if (lua_type(L, -2) != LUA_TSTRING) {
4496 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4497 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4498 lua_typename(L, lua_type(L, -2)));
4499 WILL_LJMP(lua_error(L));
4500 }
4501 name = lua_tolstring(L, -2, &nlen);
4502
4503 /* We expect an array as -1. */
4504 if (lua_type(L, -1) != LUA_TTABLE) {
4505 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4506 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4507 name,
4508 lua_typename(L, lua_type(L, -1)));
4509 WILL_LJMP(lua_error(L));
4510 }
4511
4512 /* Browse the table who is on the top of the stack. */
4513 lua_pushnil(L);
4514 while(lua_next(L, -2) != 0) {
4515 int id;
4516
4517 /* We expect a number as -2. */
4518 if (lua_type(L, -2) != LUA_TNUMBER) {
4519 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4520 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4521 name,
4522 lua_typename(L, lua_type(L, -2)));
4523 WILL_LJMP(lua_error(L));
4524 }
4525 id = lua_tointeger(L, -2);
4526
4527 /* We expect a string as -2. */
4528 if (lua_type(L, -1) != LUA_TSTRING) {
4529 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4530 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4531 name, id,
4532 lua_typename(L, lua_type(L, -1)));
4533 WILL_LJMP(lua_error(L));
4534 }
4535 value = lua_tolstring(L, -1, &vlen);
4536
4537 /* Simple Protocol checks. */
4538 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4539 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4540 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4541 struct ist v = ist2(value, vlen);
4542 int ret;
4543
4544 ret = h1_parse_cont_len_header(&h1m, &v);
4545 if (ret < 0) {
4546 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4547 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4548 name);
4549 WILL_LJMP(lua_error(L));
4550 }
4551 else if (ret == 0)
4552 goto next; /* Skip it */
4553 }
4554
4555 /* Add a new header */
4556 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4557 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4558 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4559 name);
4560 WILL_LJMP(lua_error(L));
4561 }
4562 next:
4563 /* Remove the array from the stack, and get next element with a remaining string. */
4564 lua_pop(L, 1);
4565 }
4566
4567 /* Remove the array from the stack, and get next element with a remaining string. */
4568 lua_pop(L, 1);
4569 }
4570
4571 if (h1m.flags & H1_MF_CHNK)
4572 h1m.flags &= ~H1_MF_CLEN;
4573 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4574 h1m.flags |= H1_MF_XFER_LEN;
4575
4576 /* Uset HTX start-line flags */
4577 if (h1m.flags & H1_MF_XFER_ENC)
4578 flags |= HTX_SL_F_XFER_ENC;
4579 if (h1m.flags & H1_MF_XFER_LEN) {
4580 flags |= HTX_SL_F_XFER_LEN;
4581 if (h1m.flags & H1_MF_CHNK)
4582 flags |= HTX_SL_F_CHNK;
4583 else if (h1m.flags & H1_MF_CLEN)
4584 flags |= HTX_SL_F_CLEN;
4585 if (h1m.body_len == 0)
4586 flags |= HTX_SL_F_BODYLESS;
4587 }
4588 sl->flags |= flags;
4589
4590 /* If we dont have a content-length set, and the HTTP version is 1.1
4591 * and the status code implies the presence of a message body, we must
4592 * announce a transfer encoding chunked. This is required by haproxy
4593 * for the keepalive compliance. If the applet annouces a transfer-encoding
4594 * chunked itslef, don't do anything.
4595 */
4596 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4597 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4598 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4599 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4600 /* Add a new header */
4601 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4602 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4603 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4604 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4605 WILL_LJMP(lua_error(L));
4606 }
4607 }
4608
4609 /* Finalize headers. */
4610 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4611 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4612 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4613 WILL_LJMP(lua_error(L));
4614 }
4615
4616 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4617 b_reset(&res->buf);
4618 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4619 WILL_LJMP(lua_error(L));
4620 }
4621
4622 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004623 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004624
4625 /* Headers sent, set the flag. */
4626 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4627 return 0;
4628
4629}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004630/* We will build the status line and the headers of the HTTP response.
4631 * We will try send at once if its not possible, we give back the hand
4632 * waiting for more room.
4633 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004634__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004635{
4636 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4637 struct stream_interface *si = appctx->appctx->owner;
4638 struct channel *res = si_ic(si);
4639
4640 if (co_data(res)) {
4641 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004642 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004643 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004644 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004645}
4646
4647
Christopher Fauleta2097962019-07-15 16:25:33 +02004648__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004649{
Christopher Fauleta2097962019-07-15 16:25:33 +02004650 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004651}
4652
Christopher Fauleta2097962019-07-15 16:25:33 +02004653/*
4654 *
4655 *
4656 * Class HTTP
4657 *
4658 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004659 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004660
4661/* Returns a struct hlua_txn if the stack entry "ud" is
4662 * a class stream, otherwise it throws an error.
4663 */
4664__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004665{
Christopher Fauleta2097962019-07-15 16:25:33 +02004666 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4667}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004668
Christopher Fauleta2097962019-07-15 16:25:33 +02004669/* This function creates and push in the stack a HTTP object
4670 * according with a current TXN.
4671 */
4672static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4673{
4674 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004675
Christopher Fauleta2097962019-07-15 16:25:33 +02004676 /* Check stack size. */
4677 if (!lua_checkstack(L, 3))
4678 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004679
Christopher Fauleta2097962019-07-15 16:25:33 +02004680 /* Create the object: obj[0] = userdata.
4681 * Note that the base of the Converters object is the
4682 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004683 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004684 lua_newtable(L);
4685 htxn = lua_newuserdata(L, sizeof(*htxn));
4686 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004687
4688 htxn->s = txn->s;
4689 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004690 htxn->dir = txn->dir;
4691 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004692
4693 /* Pop a class stream metatable and affect it to the table. */
4694 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4695 lua_setmetatable(L, -2);
4696
4697 return 1;
4698}
4699
4700/* This function creates ans returns an array of HTTP headers.
4701 * This function does not fails. It is used as wrapper with the
4702 * 2 following functions.
4703 */
4704__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4705{
Christopher Fauleta2097962019-07-15 16:25:33 +02004706 struct htx *htx;
4707 int32_t pos;
4708
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004709 /* Create the table. */
4710 lua_newtable(L);
4711
4712 if (!htxn->s->txn)
4713 return 1;
4714
Christopher Fauleta2097962019-07-15 16:25:33 +02004715 htx = htxbuf(&msg->chn->buf);
4716 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4717 struct htx_blk *blk = htx_get_blk(htx, pos);
4718 enum htx_blk_type type = htx_get_blk_type(blk);
4719 struct ist n, v;
4720 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004721
Christopher Fauleta2097962019-07-15 16:25:33 +02004722 if (type == HTX_BLK_HDR) {
4723 n = htx_get_blk_name(htx,blk);
4724 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004725 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004726 else if (type == HTX_BLK_EOH)
4727 break;
4728 else
4729 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004730
Christopher Fauleta2097962019-07-15 16:25:33 +02004731 /* Check for existing entry:
4732 * assume that the table is on the top of the stack, and
4733 * push the key in the stack, the function lua_gettable()
4734 * perform the lookup.
4735 */
4736 lua_pushlstring(L, n.ptr, n.len);
4737 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004738
Christopher Fauleta2097962019-07-15 16:25:33 +02004739 switch (lua_type(L, -1)) {
4740 case LUA_TNIL:
4741 /* Table not found, create it. */
4742 lua_pop(L, 1); /* remove the nil value. */
4743 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4744 lua_newtable(L); /* create and push empty table. */
4745 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4746 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4747 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004748 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004749
Christopher Fauleta2097962019-07-15 16:25:33 +02004750 case LUA_TTABLE:
4751 /* Entry found: push the value in the table. */
4752 len = lua_rawlen(L, -1);
4753 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4754 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4755 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4756 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004757
Christopher Fauleta2097962019-07-15 16:25:33 +02004758 default:
4759 /* Other cases are errors. */
4760 hlua_pusherror(L, "internal error during the parsing of headers.");
4761 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004762 }
4763 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004764 return 1;
4765}
4766
4767__LJMP static int hlua_http_req_get_headers(lua_State *L)
4768{
4769 struct hlua_txn *htxn;
4770
4771 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4772 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4773
Christopher Faulet301eff82019-07-26 16:31:34 +02004774 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004775 WILL_LJMP(lua_error(L));
4776
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004777 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4778}
4779
4780__LJMP static int hlua_http_res_get_headers(lua_State *L)
4781{
4782 struct hlua_txn *htxn;
4783
4784 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4785 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4786
Christopher Faulet301eff82019-07-26 16:31:34 +02004787 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004788 WILL_LJMP(lua_error(L));
4789
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004790 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4791}
4792
4793/* This function replace full header, or just a value in
4794 * the request or in the response. It is a wrapper fir the
4795 * 4 following functions.
4796 */
4797__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4798 struct http_msg *msg, int action)
4799{
4800 size_t name_len;
4801 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4802 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4803 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004804 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004805 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004806
Dragan Dosen26743032019-04-30 15:54:36 +02004807 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004808 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4809
Christopher Fauleta2097962019-07-15 16:25:33 +02004810 htx = htxbuf(&msg->chn->buf);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004811 http_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02004812 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004813 return 0;
4814}
4815
4816__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4817{
4818 struct hlua_txn *htxn;
4819
4820 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4821 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4822
Christopher Faulet301eff82019-07-26 16:31:34 +02004823 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004824 WILL_LJMP(lua_error(L));
4825
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004826 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4827}
4828
4829__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4830{
4831 struct hlua_txn *htxn;
4832
4833 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4834 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4835
Christopher Faulet301eff82019-07-26 16:31:34 +02004836 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004837 WILL_LJMP(lua_error(L));
4838
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004839 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4840}
4841
4842__LJMP static int hlua_http_req_rep_val(lua_State *L)
4843{
4844 struct hlua_txn *htxn;
4845
4846 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4847 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4848
Christopher Faulet301eff82019-07-26 16:31:34 +02004849 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004850 WILL_LJMP(lua_error(L));
4851
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004852 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4853}
4854
4855__LJMP static int hlua_http_res_rep_val(lua_State *L)
4856{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004857 struct hlua_txn *htxn;
4858
4859 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4860 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4861
Christopher Faulet301eff82019-07-26 16:31:34 +02004862 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004863 WILL_LJMP(lua_error(L));
4864
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004865 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004866}
4867
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004868/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004869 * It is a wrapper for the 2 following functions.
4870 */
4871__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4872{
4873 size_t len;
4874 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004875 struct htx *htx = htxbuf(&msg->chn->buf);
4876 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004877
Christopher Fauleta2097962019-07-15 16:25:33 +02004878 ctx.blk = NULL;
4879 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4880 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004881 return 0;
4882}
4883
4884__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4885{
4886 struct hlua_txn *htxn;
4887
4888 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4889 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4890
Christopher Faulet301eff82019-07-26 16:31:34 +02004891 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004892 WILL_LJMP(lua_error(L));
4893
Willy Tarreaueee5b512015-04-03 23:46:31 +02004894 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004895}
4896
4897__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4898{
4899 struct hlua_txn *htxn;
4900
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004901 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004902 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4903
Christopher Faulet301eff82019-07-26 16:31:34 +02004904 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004905 WILL_LJMP(lua_error(L));
4906
Willy Tarreaueee5b512015-04-03 23:46:31 +02004907 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004908}
4909
4910/* This function adds an header. It is a wrapper used by
4911 * the 2 following functions.
4912 */
4913__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4914{
4915 size_t name_len;
4916 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4917 size_t value_len;
4918 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004919 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004920
Christopher Fauleta2097962019-07-15 16:25:33 +02004921 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4922 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004923 return 0;
4924}
4925
4926__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4927{
4928 struct hlua_txn *htxn;
4929
4930 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4931 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4932
Christopher Faulet301eff82019-07-26 16:31:34 +02004933 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004934 WILL_LJMP(lua_error(L));
4935
Willy Tarreaueee5b512015-04-03 23:46:31 +02004936 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004937}
4938
4939__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4940{
4941 struct hlua_txn *htxn;
4942
4943 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4944 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4945
Christopher Faulet301eff82019-07-26 16:31:34 +02004946 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004947 WILL_LJMP(lua_error(L));
4948
Willy Tarreaueee5b512015-04-03 23:46:31 +02004949 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004950}
4951
4952static int hlua_http_req_set_hdr(lua_State *L)
4953{
4954 struct hlua_txn *htxn;
4955
4956 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4957 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4958
Christopher Faulet301eff82019-07-26 16:31:34 +02004959 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004960 WILL_LJMP(lua_error(L));
4961
Willy Tarreaueee5b512015-04-03 23:46:31 +02004962 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4963 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004964}
4965
4966static int hlua_http_res_set_hdr(lua_State *L)
4967{
4968 struct hlua_txn *htxn;
4969
4970 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4971 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4972
Christopher Faulet301eff82019-07-26 16:31:34 +02004973 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004974 WILL_LJMP(lua_error(L));
4975
Willy Tarreaueee5b512015-04-03 23:46:31 +02004976 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4977 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004978}
4979
4980/* This function set the method. */
4981static int hlua_http_req_set_meth(lua_State *L)
4982{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004983 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004984 size_t name_len;
4985 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004986
Christopher Faulet301eff82019-07-26 16:31:34 +02004987 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004988 WILL_LJMP(lua_error(L));
4989
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004990 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004991 return 1;
4992}
4993
4994/* This function set the method. */
4995static int hlua_http_req_set_path(lua_State *L)
4996{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004997 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004998 size_t name_len;
4999 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005000
Christopher Faulet301eff82019-07-26 16:31:34 +02005001 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005002 WILL_LJMP(lua_error(L));
5003
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005004 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005005 return 1;
5006}
5007
5008/* This function set the query-string. */
5009static int hlua_http_req_set_query(lua_State *L)
5010{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005011 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005012 size_t name_len;
5013 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005014
Christopher Faulet301eff82019-07-26 16:31:34 +02005015 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005016 WILL_LJMP(lua_error(L));
5017
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005018 /* Check length. */
5019 if (name_len > trash.size - 1) {
5020 lua_pushboolean(L, 0);
5021 return 1;
5022 }
5023
5024 /* Add the mark question as prefix. */
5025 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005026 trash.area[trash.data++] = '?';
5027 memcpy(trash.area + trash.data, name, name_len);
5028 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005029
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005030 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005031 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005032 return 1;
5033}
5034
5035/* This function set the uri. */
5036static int hlua_http_req_set_uri(lua_State *L)
5037{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005038 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005039 size_t name_len;
5040 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005041
Christopher Faulet301eff82019-07-26 16:31:34 +02005042 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005043 WILL_LJMP(lua_error(L));
5044
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005045 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005046 return 1;
5047}
5048
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005049/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005050static int hlua_http_res_set_status(lua_State *L)
5051{
5052 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5053 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005054 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005055
Christopher Faulet301eff82019-07-26 16:31:34 +02005056 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005057 WILL_LJMP(lua_error(L));
5058
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005059 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005060 return 0;
5061}
5062
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005063/*
5064 *
5065 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005066 * Class TXN
5067 *
5068 *
5069 */
5070
5071/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005072 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005073 */
5074__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5075{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005076 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005077}
5078
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005079__LJMP static int hlua_set_var(lua_State *L)
5080{
5081 struct hlua_txn *htxn;
5082 const char *name;
5083 size_t len;
5084 struct sample smp;
5085
5086 MAY_LJMP(check_args(L, 3, "set_var"));
5087
5088 /* It is useles to retrieve the stream, but this function
5089 * runs only in a stream context.
5090 */
5091 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5092 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5093
5094 /* Converts the third argument in a sample. */
5095 hlua_lua2smp(L, 3, &smp);
5096
5097 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005098 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005099 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005100 return 0;
5101}
5102
Christopher Faulet85d79c92016-11-09 16:54:56 +01005103__LJMP static int hlua_unset_var(lua_State *L)
5104{
5105 struct hlua_txn *htxn;
5106 const char *name;
5107 size_t len;
5108 struct sample smp;
5109
5110 MAY_LJMP(check_args(L, 2, "unset_var"));
5111
5112 /* It is useles to retrieve the stream, but this function
5113 * runs only in a stream context.
5114 */
5115 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5116 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5117
5118 /* Unset the variable. */
5119 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5120 vars_unset_by_name(name, len, &smp);
5121 return 0;
5122}
5123
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005124__LJMP static int hlua_get_var(lua_State *L)
5125{
5126 struct hlua_txn *htxn;
5127 const char *name;
5128 size_t len;
5129 struct sample smp;
5130
5131 MAY_LJMP(check_args(L, 2, "get_var"));
5132
5133 /* It is useles to retrieve the stream, but this function
5134 * runs only in a stream context.
5135 */
5136 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5137 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5138
Willy Tarreau7560dd42016-03-10 16:28:58 +01005139 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005140 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005141 lua_pushnil(L);
5142 return 1;
5143 }
5144
5145 return hlua_smp2lua(L, &smp);
5146}
5147
Willy Tarreau59551662015-03-10 14:23:13 +01005148__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005149{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005150 struct hlua *hlua;
5151
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005152 MAY_LJMP(check_args(L, 2, "set_priv"));
5153
Willy Tarreau87b09662015-04-03 00:22:06 +02005154 /* It is useles to retrieve the stream, but this function
5155 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005156 */
5157 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005158 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005159
5160 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005161 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005162
5163 /* Get and store new value. */
5164 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5165 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5166
5167 return 0;
5168}
5169
Willy Tarreau59551662015-03-10 14:23:13 +01005170__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005171{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005172 struct hlua *hlua;
5173
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005174 MAY_LJMP(check_args(L, 1, "get_priv"));
5175
Willy Tarreau87b09662015-04-03 00:22:06 +02005176 /* It is useles to retrieve the stream, but this function
5177 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005178 */
5179 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005180 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005181
5182 /* Push configuration index in the stack. */
5183 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5184
5185 return 1;
5186}
5187
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005188/* Create stack entry containing a class TXN. This function
5189 * return 0 if the stack does not contains free slots,
5190 * otherwise it returns 1.
5191 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005192static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005193{
Willy Tarreaude491382015-04-06 11:04:28 +02005194 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005195
5196 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005197 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005198 return 0;
5199
5200 /* NOTE: The allocation never fails. The failure
5201 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005202 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005203 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005204 /* Create the object: obj[0] = userdata. */
5205 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005206 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005207 lua_rawseti(L, -2, 0);
5208
Willy Tarreaude491382015-04-06 11:04:28 +02005209 htxn->s = s;
5210 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005211 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005212 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005213
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005214 /* Create the "f" field that contains a list of fetches. */
5215 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005216 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005217 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005218 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005219
5220 /* Create the "sf" field that contains a list of stringsafe fetches. */
5221 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005222 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005223 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005224 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005225
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005226 /* Create the "c" field that contains a list of converters. */
5227 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005228 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005229 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005230 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005231
5232 /* Create the "sc" field that contains a list of stringsafe converters. */
5233 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005234 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005235 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005236 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005237
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005238 /* Create the "req" field that contains the request channel object. */
5239 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005240 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005241 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005242 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005243
5244 /* Create the "res" field that contains the response channel object. */
5245 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005246 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005247 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005248 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005249
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005250 /* Creates the HTTP object is the current proxy allows http. */
5251 lua_pushstring(L, "http");
5252 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005253 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005254 return 0;
5255 }
5256 else
5257 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005258 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005259
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005260 /* Pop a class sesison metatable and affect it to the userdata. */
5261 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5262 lua_setmetatable(L, -2);
5263
5264 return 1;
5265}
5266
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005267__LJMP static int hlua_txn_deflog(lua_State *L)
5268{
5269 const char *msg;
5270 struct hlua_txn *htxn;
5271
5272 MAY_LJMP(check_args(L, 2, "deflog"));
5273 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5274 msg = MAY_LJMP(luaL_checkstring(L, 2));
5275
5276 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5277 return 0;
5278}
5279
5280__LJMP static int hlua_txn_log(lua_State *L)
5281{
5282 int level;
5283 const char *msg;
5284 struct hlua_txn *htxn;
5285
5286 MAY_LJMP(check_args(L, 3, "log"));
5287 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5288 level = MAY_LJMP(luaL_checkinteger(L, 2));
5289 msg = MAY_LJMP(luaL_checkstring(L, 3));
5290
5291 if (level < 0 || level >= NB_LOG_LEVELS)
5292 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5293
5294 hlua_sendlog(htxn->s->be, level, msg);
5295 return 0;
5296}
5297
5298__LJMP static int hlua_txn_log_debug(lua_State *L)
5299{
5300 const char *msg;
5301 struct hlua_txn *htxn;
5302
5303 MAY_LJMP(check_args(L, 2, "Debug"));
5304 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5305 msg = MAY_LJMP(luaL_checkstring(L, 2));
5306 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5307 return 0;
5308}
5309
5310__LJMP static int hlua_txn_log_info(lua_State *L)
5311{
5312 const char *msg;
5313 struct hlua_txn *htxn;
5314
5315 MAY_LJMP(check_args(L, 2, "Info"));
5316 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5317 msg = MAY_LJMP(luaL_checkstring(L, 2));
5318 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5319 return 0;
5320}
5321
5322__LJMP static int hlua_txn_log_warning(lua_State *L)
5323{
5324 const char *msg;
5325 struct hlua_txn *htxn;
5326
5327 MAY_LJMP(check_args(L, 2, "Warning"));
5328 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5329 msg = MAY_LJMP(luaL_checkstring(L, 2));
5330 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5331 return 0;
5332}
5333
5334__LJMP static int hlua_txn_log_alert(lua_State *L)
5335{
5336 const char *msg;
5337 struct hlua_txn *htxn;
5338
5339 MAY_LJMP(check_args(L, 2, "Alert"));
5340 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5341 msg = MAY_LJMP(luaL_checkstring(L, 2));
5342 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5343 return 0;
5344}
5345
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005346__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5347{
5348 struct hlua_txn *htxn;
5349 int ll;
5350
5351 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5352 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5353 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5354
5355 if (ll < 0 || ll > 7)
5356 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5357
5358 htxn->s->logs.level = ll;
5359 return 0;
5360}
5361
5362__LJMP static int hlua_txn_set_tos(lua_State *L)
5363{
5364 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005365 int tos;
5366
5367 MAY_LJMP(check_args(L, 2, "set_tos"));
5368 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5369 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5370
Willy Tarreau1a18b542018-12-11 16:37:42 +01005371 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005372 return 0;
5373}
5374
5375__LJMP static int hlua_txn_set_mark(lua_State *L)
5376{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005377 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005378 int mark;
5379
5380 MAY_LJMP(check_args(L, 2, "set_mark"));
5381 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5382 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5383
Lukas Tribus579e3e32019-08-11 18:03:45 +02005384 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005385 return 0;
5386}
5387
Patrick Hemmer268a7072018-05-11 12:52:31 -04005388__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5389{
5390 struct hlua_txn *htxn;
5391
5392 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5393 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5394 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5395 return 0;
5396}
5397
5398__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5399{
5400 struct hlua_txn *htxn;
5401
5402 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5403 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5404 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5405 return 0;
5406}
5407
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005408/* This function is an Lua binding that send pending data
5409 * to the client, and close the stream interface.
5410 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005411__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005412{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005413 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005414 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005415 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005416
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005417 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005418 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005419 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005420
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005421 /* If the flags NOTERM is set, we cannot terminate the http
5422 * session, so we just end the execution of the current
5423 * lua code.
5424 */
5425 if (htxn->flags & HLUA_TXN_NOTERM) {
5426 WILL_LJMP(hlua_done(L));
5427 return 0;
5428 }
5429
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005430 ic = &htxn->s->req;
5431 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005432
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005433 if (IS_HTX_STRM(htxn->s)) {
5434 htxn->s->txn->status = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005435 http_reply_and_close(htxn->s, 0, NULL);
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005436 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005437 else {
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005438 channel_auto_read(ic);
5439 channel_abort(ic);
5440 channel_auto_close(ic);
5441 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005442
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005443 oc->wex = tick_add_ifset(now_ms, oc->wto);
5444 channel_auto_read(oc);
5445 channel_auto_close(oc);
5446 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005447
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005448 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005449
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005450 ic->analysers &= AN_REQ_FLT_END;
5451 oc->analysers &= AN_RES_FLT_END;
5452
5453 if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is
5454 htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
5455
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005456 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005457 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005458 return 0;
5459}
5460
5461__LJMP static int hlua_log(lua_State *L)
5462{
5463 int level;
5464 const char *msg;
5465
5466 MAY_LJMP(check_args(L, 2, "log"));
5467 level = MAY_LJMP(luaL_checkinteger(L, 1));
5468 msg = MAY_LJMP(luaL_checkstring(L, 2));
5469
5470 if (level < 0 || level >= NB_LOG_LEVELS)
5471 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5472
5473 hlua_sendlog(NULL, level, msg);
5474 return 0;
5475}
5476
5477__LJMP static int hlua_log_debug(lua_State *L)
5478{
5479 const char *msg;
5480
5481 MAY_LJMP(check_args(L, 1, "debug"));
5482 msg = MAY_LJMP(luaL_checkstring(L, 1));
5483 hlua_sendlog(NULL, LOG_DEBUG, msg);
5484 return 0;
5485}
5486
5487__LJMP static int hlua_log_info(lua_State *L)
5488{
5489 const char *msg;
5490
5491 MAY_LJMP(check_args(L, 1, "info"));
5492 msg = MAY_LJMP(luaL_checkstring(L, 1));
5493 hlua_sendlog(NULL, LOG_INFO, msg);
5494 return 0;
5495}
5496
5497__LJMP static int hlua_log_warning(lua_State *L)
5498{
5499 const char *msg;
5500
5501 MAY_LJMP(check_args(L, 1, "warning"));
5502 msg = MAY_LJMP(luaL_checkstring(L, 1));
5503 hlua_sendlog(NULL, LOG_WARNING, msg);
5504 return 0;
5505}
5506
5507__LJMP static int hlua_log_alert(lua_State *L)
5508{
5509 const char *msg;
5510
5511 MAY_LJMP(check_args(L, 1, "alert"));
5512 msg = MAY_LJMP(luaL_checkstring(L, 1));
5513 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005514 return 0;
5515}
5516
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005517__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005518{
5519 int wakeup_ms = lua_tointeger(L, -1);
5520 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005521 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005522 return 0;
5523}
5524
5525__LJMP static int hlua_sleep(lua_State *L)
5526{
5527 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005528 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005529
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005530 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005531
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005532 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005533 wakeup_ms = tick_add(now_ms, delay);
5534 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005535
Willy Tarreau9635e032018-10-16 17:52:55 +02005536 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005537 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005538}
5539
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005540__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005541{
5542 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005543 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005544
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005545 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005546
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005547 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005548 wakeup_ms = tick_add(now_ms, delay);
5549 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005550
Willy Tarreau9635e032018-10-16 17:52:55 +02005551 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005552 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005553}
5554
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005555/* This functionis an LUA binding. it permits to give back
5556 * the hand at the HAProxy scheduler. It is used when the
5557 * LUA processing consumes a lot of time.
5558 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005559__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005560{
5561 return 0;
5562}
5563
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005564__LJMP static int hlua_yield(lua_State *L)
5565{
Willy Tarreau9635e032018-10-16 17:52:55 +02005566 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005567 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005568}
5569
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005570/* This function change the nice of the currently executed
5571 * task. It is used set low or high priority at the current
5572 * task.
5573 */
Willy Tarreau59551662015-03-10 14:23:13 +01005574__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005575{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005576 struct hlua *hlua;
5577 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005578
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005579 MAY_LJMP(check_args(L, 1, "set_nice"));
5580 hlua = hlua_gethlua(L);
5581 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005582
5583 /* If he task is not set, I'm in a start mode. */
5584 if (!hlua || !hlua->task)
5585 return 0;
5586
5587 if (nice < -1024)
5588 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005589 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005590 nice = 1024;
5591
5592 hlua->task->nice = nice;
5593 return 0;
5594}
5595
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005596/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005597 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5598 * return an E_AGAIN signal, the emmiter of this signal must set a
5599 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005600 *
5601 * Task wrapper are longjmp safe because the only one Lua code
5602 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005603 */
Willy Tarreau60409db2019-08-21 14:14:50 +02005604struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005605{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005606 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005607 enum hlua_exec status;
5608
Christopher Faulet5bc99722018-04-25 10:34:45 +02005609 if (task->thread_mask == MAX_THREADS_MASK)
5610 task_set_affinity(task, tid_bit);
5611
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005612 /* If it is the first call to the task, we must initialize the
5613 * execution timeouts.
5614 */
5615 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005616 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005617
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005618 /* Execute the Lua code. */
5619 status = hlua_ctx_resume(hlua, 1);
5620
5621 switch (status) {
5622 /* finished or yield */
5623 case HLUA_E_OK:
5624 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005625 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005626 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005627 break;
5628
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005629 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005630 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005631 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005632 break;
5633
5634 /* finished with error. */
5635 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005636 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005637 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005638 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005639 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005640 break;
5641
5642 case HLUA_E_ERR:
5643 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005644 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005645 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005646 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005647 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005648 break;
5649 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005650 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005651}
5652
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005653/* This function is an LUA binding that register LUA function to be
5654 * executed after the HAProxy configuration parsing and before the
5655 * HAProxy scheduler starts. This function expect only one LUA
5656 * argument that is a function. This function returns nothing, but
5657 * throws if an error is encountered.
5658 */
5659__LJMP static int hlua_register_init(lua_State *L)
5660{
5661 struct hlua_init_function *init;
5662 int ref;
5663
5664 MAY_LJMP(check_args(L, 1, "register_init"));
5665
5666 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5667
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005668 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005669 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005670 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005671
5672 init->function_ref = ref;
5673 LIST_ADDQ(&hlua_init_functions, &init->l);
5674 return 0;
5675}
5676
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005677/* This functio is an LUA binding. It permits to register a task
5678 * executed in parallel of the main HAroxy activity. The task is
5679 * created and it is set in the HAProxy scheduler. It can be called
5680 * from the "init" section, "post init" or during the runtime.
5681 *
5682 * Lua prototype:
5683 *
5684 * <none> core.register_task(<function>)
5685 */
5686static int hlua_register_task(lua_State *L)
5687{
5688 struct hlua *hlua;
5689 struct task *task;
5690 int ref;
5691
5692 MAY_LJMP(check_args(L, 1, "register_task"));
5693
5694 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5695
Willy Tarreaubafbe012017-11-24 17:34:44 +01005696 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005697 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005698 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005699
Emeric Brunc60def82017-09-27 14:59:38 +02005700 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005701 if (!task)
5702 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5703
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005704 task->context = hlua;
5705 task->process = hlua_process_task;
5706
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005707 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005708 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005709
5710 /* Restore the function in the stack. */
5711 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5712 hlua->nargs = 0;
5713
5714 /* Schedule task. */
5715 task_schedule(task, now_ms);
5716
5717 return 0;
5718}
5719
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005720/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5721 * doesn't allow "yield" functions because the HAProxy engine cannot
5722 * resume converters.
5723 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005724static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005725{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005726 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005727 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005728 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005729
Willy Tarreaube508f12016-03-10 11:47:01 +01005730 if (!stream)
5731 return 0;
5732
Willy Tarreau87b09662015-04-03 00:22:06 +02005733 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005734 * Lua context can be not initialized. This behavior
5735 * permits to save performances because a systematic
5736 * Lua initialization cause 5% performances loss.
5737 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005738 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005739 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005740 if (!stream->hlua) {
5741 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5742 return 0;
5743 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005744 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005745 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5746 return 0;
5747 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005748 }
5749
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005750 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005751 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005752
5753 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005754 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5755 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5756 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005757 else
5758 error = "critical error";
5759 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005760 return 0;
5761 }
5762
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005763 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005764 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005765 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005766 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005767 return 0;
5768 }
5769
5770 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005771 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005772
5773 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005774 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005775 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005776 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005777 return 0;
5778 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005779 hlua_smp2lua(stream->hlua->T, smp);
5780 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005781
5782 /* push keywords in the stack. */
5783 if (arg_p) {
5784 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005785 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005786 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005787 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005788 return 0;
5789 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005790 hlua_arg2lua(stream->hlua->T, arg_p);
5791 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005792 }
5793 }
5794
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005795 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005796 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005797
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005798 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005799 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005800 }
5801
5802 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005803 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005804 /* finished. */
5805 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005806 /* If the stack is empty, the function fails. */
5807 if (lua_gettop(stream->hlua->T) <= 0)
5808 return 0;
5809
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005810 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005811 hlua_lua2smp(stream->hlua->T, -1, smp);
5812 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005813 return 1;
5814
5815 /* yield. */
5816 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005817 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005818 return 0;
5819
5820 /* finished with error. */
5821 case HLUA_E_ERRMSG:
5822 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005823 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005824 fcn->name, lua_tostring(stream->hlua->T, -1));
5825 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005826 return 0;
5827
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005828 case HLUA_E_ETMOUT:
5829 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5830 return 0;
5831
5832 case HLUA_E_NOMEM:
5833 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5834 return 0;
5835
5836 case HLUA_E_YIELD:
5837 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5838 return 0;
5839
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005840 case HLUA_E_ERR:
5841 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005842 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005843
5844 default:
5845 return 0;
5846 }
5847}
5848
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005849/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5850 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005851 * resume sample-fetches. This function will be called by the sample
5852 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005853 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005854static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5855 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005856{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005857 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005858 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005859 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005860 const struct buffer msg = { };
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02005861 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005862
Willy Tarreaube508f12016-03-10 11:47:01 +01005863 if (!stream)
5864 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005865
Willy Tarreau87b09662015-04-03 00:22:06 +02005866 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005867 * Lua context can be not initialized. This behavior
5868 * permits to save performances because a systematic
5869 * Lua initialization cause 5% performances loss.
5870 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005871 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005872 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005873 if (!stream->hlua) {
5874 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5875 return 0;
5876 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005877 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005878 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5879 return 0;
5880 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005881 }
5882
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005883 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005884
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02005885 if (stream->be->mode == PR_MODE_HTTP) {
5886 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
5887 hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
5888 else
5889 hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
5890 }
5891
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005892 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005893 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005894
5895 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005896 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5897 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5898 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005899 else
5900 error = "critical error";
5901 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005902 return 0;
5903 }
5904
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005905 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005906 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005907 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005908 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005909 return 0;
5910 }
5911
5912 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005913 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005914
5915 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02005916 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005917 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005918 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005919 return 0;
5920 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005921 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005922
5923 /* push keywords in the stack. */
5924 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5925 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005926 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005927 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005928 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005929 return 0;
5930 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005931 hlua_arg2lua(stream->hlua->T, arg_p);
5932 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005933 }
5934
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005935 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005936 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005937
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005938 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005939 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005940 }
5941
5942 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005943 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005944 /* finished. */
5945 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005946 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005947 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005948 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005949 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005950 /* If the stack is empty, the function fails. */
5951 if (lua_gettop(stream->hlua->T) <= 0)
5952 return 0;
5953
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005954 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005955 hlua_lua2smp(stream->hlua->T, -1, smp);
5956 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005957
5958 /* Set the end of execution flag. */
5959 smp->flags &= ~SMP_F_MAY_CHANGE;
5960 return 1;
5961
5962 /* yield. */
5963 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005964 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005965 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005966 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005967 return 0;
5968
5969 /* finished with error. */
5970 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005971 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005972 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005973 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005974 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005975 fcn->name, lua_tostring(stream->hlua->T, -1));
5976 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005977 return 0;
5978
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005979 case HLUA_E_ETMOUT:
5980 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005981 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005982 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5983 return 0;
5984
5985 case HLUA_E_NOMEM:
5986 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005987 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005988 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
5989 return 0;
5990
5991 case HLUA_E_YIELD:
5992 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005993 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005994 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
5995 return 0;
5996
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005997 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005998 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005999 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006000 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006001 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006002
6003 default:
6004 return 0;
6005 }
6006}
6007
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006008/* This function is an LUA binding used for registering
6009 * "sample-conv" functions. It expects a converter name used
6010 * in the haproxy configuration file, and an LUA function.
6011 */
6012__LJMP static int hlua_register_converters(lua_State *L)
6013{
6014 struct sample_conv_kw_list *sck;
6015 const char *name;
6016 int ref;
6017 int len;
6018 struct hlua_function *fcn;
6019
6020 MAY_LJMP(check_args(L, 2, "register_converters"));
6021
6022 /* First argument : converter name. */
6023 name = MAY_LJMP(luaL_checkstring(L, 1));
6024
6025 /* Second argument : lua function. */
6026 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6027
6028 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006029 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006030 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006031 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006032 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006033 if (!fcn)
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
6036 /* Fill fcn. */
6037 fcn->name = strdup(name);
6038 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006039 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006040 fcn->function_ref = ref;
6041
6042 /* List head */
6043 sck->list.n = sck->list.p = NULL;
6044
6045 /* converter keyword. */
6046 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006047 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006048 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006049 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006050
6051 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6052 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006053 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 +01006054 sck->kw[0].val_args = NULL;
6055 sck->kw[0].in_type = SMP_T_STR;
6056 sck->kw[0].out_type = SMP_T_STR;
6057 sck->kw[0].private = fcn;
6058
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006059 /* Register this new converter */
6060 sample_register_convs(sck);
6061
6062 return 0;
6063}
6064
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006065/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006066 * "sample-fetch" functions. It expects a converter name used
6067 * in the haproxy configuration file, and an LUA function.
6068 */
6069__LJMP static int hlua_register_fetches(lua_State *L)
6070{
6071 const char *name;
6072 int ref;
6073 int len;
6074 struct sample_fetch_kw_list *sfk;
6075 struct hlua_function *fcn;
6076
6077 MAY_LJMP(check_args(L, 2, "register_fetches"));
6078
6079 /* First argument : sample-fetch name. */
6080 name = MAY_LJMP(luaL_checkstring(L, 1));
6081
6082 /* Second argument : lua function. */
6083 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6084
6085 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006086 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006087 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006088 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006089 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006090 if (!fcn)
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
6093 /* Fill fcn. */
6094 fcn->name = strdup(name);
6095 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006096 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006097 fcn->function_ref = ref;
6098
6099 /* List head */
6100 sfk->list.n = sfk->list.p = NULL;
6101
6102 /* sample-fetch keyword. */
6103 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006104 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006105 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006106 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006107
6108 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6109 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006110 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 +01006111 sfk->kw[0].val_args = NULL;
6112 sfk->kw[0].out_type = SMP_T_STR;
6113 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6114 sfk->kw[0].val = 0;
6115 sfk->kw[0].private = fcn;
6116
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006117 /* Register this new fetch. */
6118 sample_register_fetches(sfk);
6119
6120 return 0;
6121}
6122
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006123/* This function is a wrapper to execute each LUA function declared
6124 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006125 * return ACT_RET_CONT if the processing is finished (with or without
6126 * error) and return ACT_RET_YIELD if the function must be called again
6127 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006128 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006129static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006130 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006131{
6132 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006133 unsigned int hflags = 0;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006134 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006135 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006136 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006137
6138 switch (rule->from) {
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006139 case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break;
6140 case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break;
6141 case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
6142 case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006143 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006144 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006145 return ACT_RET_CONT;
6146 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006147
Willy Tarreau87b09662015-04-03 00:22:06 +02006148 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006149 * Lua context can be not initialized. This behavior
6150 * permits to save performances because a systematic
6151 * Lua initialization cause 5% performances loss.
6152 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006153 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006154 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006155 if (!s->hlua) {
6156 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 FOURNIERbf90ce12019-01-06 19:04:24 +01006160 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006161 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6162 rule->arg.hlua_rule->fcn.name);
6163 return ACT_RET_CONT;
6164 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006165 }
6166
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006167 consistency_set(s, dir, &s->hlua->cons);
6168
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006169 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006170 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006171
6172 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006173 if (!SET_SAFE_LJMP(s->hlua->T)) {
6174 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6175 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006176 else
6177 error = "critical error";
6178 SEND_ERR(px, "Lua function '%s': %s.\n",
6179 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006180 return ACT_RET_CONT;
6181 }
6182
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006183 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006184 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006185 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006186 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006187 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006188 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006189 }
6190
6191 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006192 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006193
Willy Tarreau87b09662015-04-03 00:22:06 +02006194 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006195 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006196 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006197 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006198 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006199 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006200 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006201 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006202
6203 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006204 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006205 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006206 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006207 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006208 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006209 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006210 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006211 lua_pushstring(s->hlua->T, *arg);
6212 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006213 }
6214
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006215 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006216 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006217
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006218 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006219 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006220 }
6221
6222 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006223 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006224 /* finished. */
6225 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006226 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006227 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006228 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006229 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006230 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet8f1aa772019-07-04 11:27:15 +02006231 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006232 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006233
6234 /* yield. */
6235 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006236 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006237 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Faulet81921b12019-08-14 23:19:45 +02006238 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006239 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006240 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006241 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006242 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006243 /* Some actions can be wake up when a "write" event
6244 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006245 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006246 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006247 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006248 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006249 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006250 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006251 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006252 * because HAProxy is not able to manipulate data, it
6253 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006254 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006255
6256 /* finished with error. */
6257 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006258 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006259 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006260 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006261 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006262 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006263 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006264 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6265 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006266 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006267
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006268 case HLUA_E_ETMOUT:
6269 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006270 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006271 return ACT_RET_ERR;
6272 }
6273 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6274 return 0;
6275
6276 case HLUA_E_NOMEM:
6277 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006278 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006279 return ACT_RET_ERR;
6280 }
6281 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6282 return 0;
6283
6284 case HLUA_E_YIELD:
6285 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006286 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006287 return ACT_RET_ERR;
6288 }
6289 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6290 rule->arg.hlua_rule->fcn.name);
6291 return 0;
6292
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006293 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006294 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006295 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006296 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006297 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006298 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006299 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006300 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006301
6302 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006303 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006304 }
6305}
6306
Olivier Houchard9f6af332018-05-25 14:04:04 +02006307struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006308{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006309 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006310
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006311 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006312 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006313 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006314}
6315
6316static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6317{
6318 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006319 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006320 struct task *task;
6321 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006322 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006323
Willy Tarreaubafbe012017-11-24 17:34:44 +01006324 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006325 if (!hlua) {
6326 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6327 ctx->rule->arg.hlua_rule->fcn.name);
6328 return 0;
6329 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006330 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006331 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006332 ctx->ctx.hlua_apptcp.flags = 0;
6333
6334 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006335 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006336 if (!task) {
6337 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6338 ctx->rule->arg.hlua_rule->fcn.name);
6339 return 0;
6340 }
6341 task->nice = 0;
6342 task->context = ctx;
6343 task->process = hlua_applet_wakeup;
6344 ctx->ctx.hlua_apptcp.task = task;
6345
6346 /* In the execution wrappers linked with a stream, the
6347 * Lua context can be not initialized. This behavior
6348 * permits to save performances because a systematic
6349 * Lua initialization cause 5% performances loss.
6350 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006351 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006352 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6353 ctx->rule->arg.hlua_rule->fcn.name);
6354 return 0;
6355 }
6356
6357 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006358 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006359
6360 /* The following Lua calls can fail. */
6361 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006362 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6363 error = lua_tostring(hlua->T, -1);
6364 else
6365 error = "critical error";
6366 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6367 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006368 return 0;
6369 }
6370
6371 /* Check stack available size. */
6372 if (!lua_checkstack(hlua->T, 1)) {
6373 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6374 ctx->rule->arg.hlua_rule->fcn.name);
6375 RESET_SAFE_LJMP(hlua->T);
6376 return 0;
6377 }
6378
6379 /* Restore the function in the stack. */
6380 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6381
6382 /* Create and and push object stream in the stack. */
6383 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6384 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6385 ctx->rule->arg.hlua_rule->fcn.name);
6386 RESET_SAFE_LJMP(hlua->T);
6387 return 0;
6388 }
6389 hlua->nargs = 1;
6390
6391 /* push keywords in the stack. */
6392 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6393 if (!lua_checkstack(hlua->T, 1)) {
6394 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6395 ctx->rule->arg.hlua_rule->fcn.name);
6396 RESET_SAFE_LJMP(hlua->T);
6397 return 0;
6398 }
6399 lua_pushstring(hlua->T, *arg);
6400 hlua->nargs++;
6401 }
6402
6403 RESET_SAFE_LJMP(hlua->T);
6404
6405 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006406 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006407 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006408
6409 return 1;
6410}
6411
Willy Tarreau60409db2019-08-21 14:14:50 +02006412void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006413{
6414 struct stream_interface *si = ctx->owner;
6415 struct stream *strm = si_strm(si);
6416 struct channel *res = si_ic(si);
6417 struct act_rule *rule = ctx->rule;
6418 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006419 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006420
6421 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006422 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6423 /* eat the whole request */
6424 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006425 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006426 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006427
6428 /* If the stream is disconnect or closed, ldo nothing. */
6429 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6430 return;
6431
6432 /* Execute the function. */
6433 switch (hlua_ctx_resume(hlua, 1)) {
6434 /* finished. */
6435 case HLUA_E_OK:
6436 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6437
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006438 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006439 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006440 res->flags |= CF_READ_NULL;
6441 si_shutr(si);
6442 return;
6443
6444 /* yield. */
6445 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006446 if (hlua->wake_time != TICK_ETERNITY)
6447 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006448 return;
6449
6450 /* finished with error. */
6451 case HLUA_E_ERRMSG:
6452 /* Display log. */
6453 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6454 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6455 lua_pop(hlua->T, 1);
6456 goto error;
6457
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006458 case HLUA_E_ETMOUT:
6459 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6460 rule->arg.hlua_rule->fcn.name);
6461 goto error;
6462
6463 case HLUA_E_NOMEM:
6464 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6465 rule->arg.hlua_rule->fcn.name);
6466 goto error;
6467
6468 case HLUA_E_YIELD: /* unexpected */
6469 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6470 rule->arg.hlua_rule->fcn.name);
6471 goto error;
6472
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006473 case HLUA_E_ERR:
6474 /* Display log. */
6475 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6476 rule->arg.hlua_rule->fcn.name);
6477 goto error;
6478
6479 default:
6480 goto error;
6481 }
6482
6483error:
6484
6485 /* For all other cases, just close the stream. */
6486 si_shutw(si);
6487 si_shutr(si);
6488 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6489}
6490
6491static void hlua_applet_tcp_release(struct appctx *ctx)
6492{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006493 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006494 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006495 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006496 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006497}
6498
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006499/* The function returns 1 if the initialisation is complete, 0 if
6500 * an errors occurs and -1 if more data are required for initializing
6501 * the applet.
6502 */
6503static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6504{
6505 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006506 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006507 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006508 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006509 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006510 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006511
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006512 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006513 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006514 if (!hlua) {
6515 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6516 ctx->rule->arg.hlua_rule->fcn.name);
6517 return 0;
6518 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006519 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006520 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006521 ctx->ctx.hlua_apphttp.left_bytes = -1;
6522 ctx->ctx.hlua_apphttp.flags = 0;
6523
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006524 if (txn->req.flags & HTTP_MSGF_VER_11)
6525 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6526
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006527 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006528 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006529 if (!task) {
6530 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6531 ctx->rule->arg.hlua_rule->fcn.name);
6532 return 0;
6533 }
6534 task->nice = 0;
6535 task->context = ctx;
6536 task->process = hlua_applet_wakeup;
6537 ctx->ctx.hlua_apphttp.task = task;
6538
6539 /* In the execution wrappers linked with a stream, the
6540 * Lua context can be not initialized. This behavior
6541 * permits to save performances because a systematic
6542 * Lua initialization cause 5% performances loss.
6543 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006544 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006545 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6546 ctx->rule->arg.hlua_rule->fcn.name);
6547 return 0;
6548 }
6549
6550 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006551 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006552
6553 /* The following Lua calls can fail. */
6554 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006555 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6556 error = lua_tostring(hlua->T, -1);
6557 else
6558 error = "critical error";
6559 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6560 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006561 return 0;
6562 }
6563
6564 /* Check stack available size. */
6565 if (!lua_checkstack(hlua->T, 1)) {
6566 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6567 ctx->rule->arg.hlua_rule->fcn.name);
6568 RESET_SAFE_LJMP(hlua->T);
6569 return 0;
6570 }
6571
6572 /* Restore the function in the stack. */
6573 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6574
6575 /* Create and and push object stream in the stack. */
6576 if (!hlua_applet_http_new(hlua->T, ctx)) {
6577 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6578 ctx->rule->arg.hlua_rule->fcn.name);
6579 RESET_SAFE_LJMP(hlua->T);
6580 return 0;
6581 }
6582 hlua->nargs = 1;
6583
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006584 /* push keywords in the stack. */
6585 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6586 if (!lua_checkstack(hlua->T, 1)) {
6587 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6588 ctx->rule->arg.hlua_rule->fcn.name);
6589 RESET_SAFE_LJMP(hlua->T);
6590 return 0;
6591 }
6592 lua_pushstring(hlua->T, *arg);
6593 hlua->nargs++;
6594 }
6595
6596 RESET_SAFE_LJMP(hlua->T);
6597
6598 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006599 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006600
6601 return 1;
6602}
6603
Willy Tarreau60409db2019-08-21 14:14:50 +02006604void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006605{
6606 struct stream_interface *si = ctx->owner;
6607 struct stream *strm = si_strm(si);
6608 struct channel *req = si_oc(si);
6609 struct channel *res = si_ic(si);
6610 struct act_rule *rule = ctx->rule;
6611 struct proxy *px = strm->be;
6612 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
6613 struct htx *req_htx, *res_htx;
6614
6615 res_htx = htx_from_buf(&res->buf);
6616
6617 /* If the stream is disconnect or closed, ldo nothing. */
6618 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6619 goto out;
6620
6621 /* Check if the input buffer is avalaible. */
6622 if (!b_size(&res->buf)) {
6623 si_rx_room_blk(si);
6624 goto out;
6625 }
6626 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006627 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006628 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6629
6630 /* Set the currently running flag. */
6631 if (!HLUA_IS_RUNNING(hlua) &&
6632 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6633 struct htx_blk *blk;
6634 size_t count = co_data(req);
6635
6636 if (!count) {
6637 si_cant_get(si);
6638 goto out;
6639 }
6640
6641 /* We need to flush the request header. This left the body for
6642 * the Lua.
6643 */
6644 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02006645 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006646 while (count && blk) {
6647 enum htx_blk_type type = htx_get_blk_type(blk);
6648 uint32_t sz = htx_get_blksz(blk);
6649
6650 if (sz > count) {
6651 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006652 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006653 goto out;
6654 }
6655
6656 count -= sz;
6657 co_set_data(req, co_data(req) - sz);
6658 blk = htx_remove_blk(req_htx, blk);
6659
6660 if (type == HTX_BLK_EOH)
6661 break;
6662 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006663 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006664 }
6665
6666 /* Executes The applet if it is not done. */
6667 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6668
6669 /* Execute the function. */
6670 switch (hlua_ctx_resume(hlua, 1)) {
6671 /* finished. */
6672 case HLUA_E_OK:
6673 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6674 break;
6675
6676 /* yield. */
6677 case HLUA_E_AGAIN:
6678 if (hlua->wake_time != TICK_ETERNITY)
6679 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006680 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006681
6682 /* finished with error. */
6683 case HLUA_E_ERRMSG:
6684 /* Display log. */
6685 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6686 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6687 lua_pop(hlua->T, 1);
6688 goto error;
6689
6690 case HLUA_E_ETMOUT:
6691 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6692 rule->arg.hlua_rule->fcn.name);
6693 goto error;
6694
6695 case HLUA_E_NOMEM:
6696 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6697 rule->arg.hlua_rule->fcn.name);
6698 goto error;
6699
6700 case HLUA_E_YIELD: /* unexpected */
6701 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6702 rule->arg.hlua_rule->fcn.name);
6703 goto error;
6704
6705 case HLUA_E_ERR:
6706 /* Display log. */
6707 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6708 rule->arg.hlua_rule->fcn.name);
6709 goto error;
6710
6711 default:
6712 goto error;
6713 }
6714 }
6715
6716 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006717 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
6718 goto done;
6719
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006720 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
6721 goto error;
6722
Christopher Faulet54b5e212019-06-04 10:08:28 +02006723 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006724 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
6725 si_rx_room_blk(si);
6726 goto out;
6727 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006728 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006729 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6730 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006731 }
6732
6733 done:
6734 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006735 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006736 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006737 si_shutr(si);
6738 }
6739
6740 /* eat the whole request */
6741 if (co_data(req)) {
6742 req_htx = htx_from_buf(&req->buf);
6743 co_htx_skip(req, req_htx, co_data(req));
6744 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006745 }
6746 }
6747
6748 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006749 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006750 return;
6751
6752 error:
6753
6754 /* If we are in HTTP mode, and we are not send any
6755 * data, return a 500 server error in best effort:
6756 * if there is no room available in the buffer,
6757 * just close the connection.
6758 */
6759 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02006760 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006761
6762 channel_erase(res);
6763 res->buf.data = b_data(err);
6764 memcpy(res->buf.area, b_head(err), b_data(err));
6765 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006766 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006767 }
6768 if (!(strm->flags & SF_ERR_MASK))
6769 strm->flags |= SF_ERR_RESOURCE;
6770 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6771 goto done;
6772}
6773
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006774static void hlua_applet_http_release(struct appctx *ctx)
6775{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006776 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006777 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006778 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006779 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006780}
6781
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006782/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6783 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006784 *
6785 * This function can fail with an abort() due to an Lua critical error.
6786 * We are in the configuration parsing process of HAProxy, this abort() is
6787 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006788 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006789static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6790 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006791{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006792 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006793 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006794
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006795 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006796 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006797 if (!rule->arg.hlua_rule) {
6798 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006799 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006800 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006801
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006802 /* Memory for arguments. */
6803 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6804 if (!rule->arg.hlua_rule->args) {
6805 memprintf(err, "out of memory error");
6806 return ACT_RET_PRS_ERR;
6807 }
6808
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006809 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006810 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006811
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006812 /* Expect some arguments */
6813 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006814 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006815 memprintf(err, "expect %d arguments", fcn->nargs);
6816 return ACT_RET_PRS_ERR;
6817 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006818 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006819 if (!rule->arg.hlua_rule->args[i]) {
6820 memprintf(err, "out of memory error");
6821 return ACT_RET_PRS_ERR;
6822 }
6823 (*cur_arg)++;
6824 }
6825 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006826
Thierry FOURNIER42148732015-09-02 17:17:33 +02006827 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006828 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006829 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006830}
6831
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006832static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6833 struct act_rule *rule, char **err)
6834{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006835 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006836
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006837 /* HTTP applets are forbidden in tcp-request rules.
6838 * HTTP applet request requires everything initilized by
6839 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6840 * The applet will be immediately initilized, but its before
6841 * the call of this analyzer.
6842 */
6843 if (rule->from != ACT_F_HTTP_REQ) {
6844 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6845 return ACT_RET_PRS_ERR;
6846 }
6847
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006848 /* Memory for the rule. */
6849 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6850 if (!rule->arg.hlua_rule) {
6851 memprintf(err, "out of memory error");
6852 return ACT_RET_PRS_ERR;
6853 }
6854
6855 /* Reference the Lua function and store the reference. */
6856 rule->arg.hlua_rule->fcn = *fcn;
6857
6858 /* TODO: later accept arguments. */
6859 rule->arg.hlua_rule->args = NULL;
6860
6861 /* Add applet pointer in the rule. */
6862 rule->applet.obj_type = OBJ_TYPE_APPLET;
6863 rule->applet.name = fcn->name;
6864 rule->applet.init = hlua_applet_http_init;
6865 rule->applet.fct = hlua_applet_http_fct;
6866 rule->applet.release = hlua_applet_http_release;
6867 rule->applet.timeout = hlua_timeout_applet;
6868
6869 return ACT_RET_PRS_OK;
6870}
6871
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006872/* This function is an LUA binding used for registering
6873 * "sample-conv" functions. It expects a converter name used
6874 * in the haproxy configuration file, and an LUA function.
6875 */
6876__LJMP static int hlua_register_action(lua_State *L)
6877{
6878 struct action_kw_list *akl;
6879 const char *name;
6880 int ref;
6881 int len;
6882 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006883 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006884
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006885 /* Initialise the number of expected arguments at 0. */
6886 nargs = 0;
6887
6888 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6889 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006890
6891 /* First argument : converter name. */
6892 name = MAY_LJMP(luaL_checkstring(L, 1));
6893
6894 /* Second argument : environment. */
6895 if (lua_type(L, 2) != LUA_TTABLE)
6896 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6897
6898 /* Third argument : lua function. */
6899 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6900
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006901 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006902 if (lua_gettop(L) >= 4)
6903 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6904
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006905 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006906 lua_pushnil(L);
6907 while (lua_next(L, 2) != 0) {
6908 if (lua_type(L, -1) != LUA_TSTRING)
6909 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6910
6911 /* Check required environment. Only accepted "http" or "tcp". */
6912 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006913 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006914 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006915 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006916 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006917 if (!fcn)
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
6920 /* Fill fcn. */
6921 fcn->name = strdup(name);
6922 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006923 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006924 fcn->function_ref = ref;
6925
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006926 /* Set the expected number od arguments. */
6927 fcn->nargs = nargs;
6928
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006929 /* List head */
6930 akl->list.n = akl->list.p = NULL;
6931
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006932 /* action keyword. */
6933 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006934 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006935 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006936 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006937
6938 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6939
6940 akl->kw[0].match_pfx = 0;
6941 akl->kw[0].private = fcn;
6942 akl->kw[0].parse = action_register_lua;
6943
6944 /* select the action registering point. */
6945 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6946 tcp_req_cont_keywords_register(akl);
6947 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6948 tcp_res_cont_keywords_register(akl);
6949 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6950 http_req_keywords_register(akl);
6951 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6952 http_res_keywords_register(akl);
6953 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006954 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006955 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6956 "are expected.", lua_tostring(L, -1)));
6957
6958 /* pop the environment string. */
6959 lua_pop(L, 1);
6960 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006961 return ACT_RET_PRS_OK;
6962}
6963
6964static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6965 struct act_rule *rule, char **err)
6966{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006967 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006968
Christopher Faulet280f85b2019-07-15 15:02:04 +02006969 if (px->mode == PR_MODE_HTTP) {
6970 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01006971 return ACT_RET_PRS_ERR;
6972 }
6973
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006974 /* Memory for the rule. */
6975 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6976 if (!rule->arg.hlua_rule) {
6977 memprintf(err, "out of memory error");
6978 return ACT_RET_PRS_ERR;
6979 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006980
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006981 /* Reference the Lua function and store the reference. */
6982 rule->arg.hlua_rule->fcn = *fcn;
6983
6984 /* TODO: later accept arguments. */
6985 rule->arg.hlua_rule->args = NULL;
6986
6987 /* Add applet pointer in the rule. */
6988 rule->applet.obj_type = OBJ_TYPE_APPLET;
6989 rule->applet.name = fcn->name;
6990 rule->applet.init = hlua_applet_tcp_init;
6991 rule->applet.fct = hlua_applet_tcp_fct;
6992 rule->applet.release = hlua_applet_tcp_release;
6993 rule->applet.timeout = hlua_timeout_applet;
6994
6995 return 0;
6996}
6997
6998/* This function is an LUA binding used for registering
6999 * "sample-conv" functions. It expects a converter name used
7000 * in the haproxy configuration file, and an LUA function.
7001 */
7002__LJMP static int hlua_register_service(lua_State *L)
7003{
7004 struct action_kw_list *akl;
7005 const char *name;
7006 const char *env;
7007 int ref;
7008 int len;
7009 struct hlua_function *fcn;
7010
7011 MAY_LJMP(check_args(L, 3, "register_service"));
7012
7013 /* First argument : converter name. */
7014 name = MAY_LJMP(luaL_checkstring(L, 1));
7015
7016 /* Second argument : environment. */
7017 env = MAY_LJMP(luaL_checkstring(L, 2));
7018
7019 /* Third argument : lua function. */
7020 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7021
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007022 /* Allocate and fill the sample fetch keyword struct. */
7023 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7024 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007025 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007026 fcn = calloc(1, sizeof(*fcn));
7027 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007028 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007029
7030 /* Fill fcn. */
7031 len = strlen("<lua.>") + strlen(name) + 1;
7032 fcn->name = calloc(1, len);
7033 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007034 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007035 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7036 fcn->function_ref = ref;
7037
7038 /* List head */
7039 akl->list.n = akl->list.p = NULL;
7040
7041 /* converter keyword. */
7042 len = strlen("lua.") + strlen(name) + 1;
7043 akl->kw[0].kw = calloc(1, len);
7044 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007045 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007046
7047 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7048
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007049 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007050 if (strcmp(env, "tcp") == 0)
7051 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007052 else if (strcmp(env, "http") == 0)
7053 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007054 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007055 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007056 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007057
7058 akl->kw[0].match_pfx = 0;
7059 akl->kw[0].private = fcn;
7060
7061 /* End of array. */
7062 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7063
7064 /* Register this new converter */
7065 service_keywords_register(akl);
7066
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007067 return 0;
7068}
7069
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007070/* This function initialises Lua cli handler. It copies the
7071 * arguments in the Lua stack and create channel IO objects.
7072 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007073static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007074{
7075 struct hlua *hlua;
7076 struct hlua_function *fcn;
7077 int i;
7078 const char *error;
7079
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007080 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007081 appctx->ctx.hlua_cli.fcn = private;
7082
Willy Tarreaubafbe012017-11-24 17:34:44 +01007083 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007084 if (!hlua) {
7085 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007086 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007087 }
7088 HLUA_INIT(hlua);
7089 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007090
7091 /* Create task used by signal to wakeup applets.
7092 * We use the same wakeup fonction than the Lua applet_tcp and
7093 * applet_http. It is absolutely compatible.
7094 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007095 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007096 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007097 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007098 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007099 }
7100 appctx->ctx.hlua_cli.task->nice = 0;
7101 appctx->ctx.hlua_cli.task->context = appctx;
7102 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7103
7104 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007105 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007106 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007107 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007108 }
7109
7110 /* The following Lua calls can fail. */
7111 if (!SET_SAFE_LJMP(hlua->T)) {
7112 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7113 error = lua_tostring(hlua->T, -1);
7114 else
7115 error = "critical error";
7116 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7117 goto error;
7118 }
7119
7120 /* Check stack available size. */
7121 if (!lua_checkstack(hlua->T, 2)) {
7122 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7123 goto error;
7124 }
7125
7126 /* Restore the function in the stack. */
7127 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7128
7129 /* Once the arguments parsed, the CLI is like an AppletTCP,
7130 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007131 */
7132 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7133 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7134 goto error;
7135 }
7136 hlua->nargs = 1;
7137
7138 /* push keywords in the stack. */
7139 for (i = 0; *args[i]; i++) {
7140 /* Check stack available size. */
7141 if (!lua_checkstack(hlua->T, 1)) {
7142 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7143 goto error;
7144 }
7145 lua_pushstring(hlua->T, args[i]);
7146 hlua->nargs++;
7147 }
7148
7149 /* We must initialize the execution timeouts. */
7150 hlua->max_time = hlua_timeout_session;
7151
7152 /* At this point the execution is safe. */
7153 RESET_SAFE_LJMP(hlua->T);
7154
7155 /* It's ok */
7156 return 0;
7157
7158 /* It's not ok. */
7159error:
7160 RESET_SAFE_LJMP(hlua->T);
7161 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007162 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007163 return 1;
7164}
7165
7166static int hlua_cli_io_handler_fct(struct appctx *appctx)
7167{
7168 struct hlua *hlua;
7169 struct stream_interface *si;
7170 struct hlua_function *fcn;
7171
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007172 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007173 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007174 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007175
7176 /* If the stream is disconnect or closed, ldo nothing. */
7177 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7178 return 1;
7179
7180 /* Execute the function. */
7181 switch (hlua_ctx_resume(hlua, 1)) {
7182
7183 /* finished. */
7184 case HLUA_E_OK:
7185 return 1;
7186
7187 /* yield. */
7188 case HLUA_E_AGAIN:
7189 /* We want write. */
7190 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007191 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007192 /* Set the timeout. */
7193 if (hlua->wake_time != TICK_ETERNITY)
7194 task_schedule(hlua->task, hlua->wake_time);
7195 return 0;
7196
7197 /* finished with error. */
7198 case HLUA_E_ERRMSG:
7199 /* Display log. */
7200 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7201 fcn->name, lua_tostring(hlua->T, -1));
7202 lua_pop(hlua->T, 1);
7203 return 1;
7204
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007205 case HLUA_E_ETMOUT:
7206 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7207 fcn->name);
7208 return 1;
7209
7210 case HLUA_E_NOMEM:
7211 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7212 fcn->name);
7213 return 1;
7214
7215 case HLUA_E_YIELD: /* unexpected */
7216 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7217 fcn->name);
7218 return 1;
7219
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007220 case HLUA_E_ERR:
7221 /* Display log. */
7222 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7223 fcn->name);
7224 return 1;
7225
7226 default:
7227 return 1;
7228 }
7229
7230 return 1;
7231}
7232
7233static void hlua_cli_io_release_fct(struct appctx *appctx)
7234{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007235 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007236 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007237}
7238
7239/* This function is an LUA binding used for registering
7240 * new keywords in the cli. It expects a list of keywords
7241 * which are the "path". It is limited to 5 keywords. A
7242 * description of the command, a function to be executed
7243 * for the parsing and a function for io handlers.
7244 */
7245__LJMP static int hlua_register_cli(lua_State *L)
7246{
7247 struct cli_kw_list *cli_kws;
7248 const char *message;
7249 int ref_io;
7250 int len;
7251 struct hlua_function *fcn;
7252 int index;
7253 int i;
7254
7255 MAY_LJMP(check_args(L, 3, "register_cli"));
7256
7257 /* First argument : an array of maximum 5 keywords. */
7258 if (!lua_istable(L, 1))
7259 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7260
7261 /* Second argument : string with contextual message. */
7262 message = MAY_LJMP(luaL_checkstring(L, 2));
7263
7264 /* Third and fourth argument : lua function. */
7265 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7266
7267 /* Allocate and fill the sample fetch keyword struct. */
7268 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7269 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007270 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007271 fcn = calloc(1, sizeof(*fcn));
7272 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007273 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007274
7275 /* Fill path. */
7276 index = 0;
7277 lua_pushnil(L);
7278 while(lua_next(L, 1) != 0) {
7279 if (index >= 5)
7280 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7281 if (lua_type(L, -1) != LUA_TSTRING)
7282 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7283 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7284 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007285 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007286 index++;
7287 lua_pop(L, 1);
7288 }
7289
7290 /* Copy help message. */
7291 cli_kws->kw[0].usage = strdup(message);
7292 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007293 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007294
7295 /* Fill fcn io handler. */
7296 len = strlen("<lua.cli>") + 1;
7297 for (i = 0; i < index; i++)
7298 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7299 fcn->name = calloc(1, len);
7300 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007301 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007302 strncat((char *)fcn->name, "<lua.cli", len);
7303 for (i = 0; i < index; i++) {
7304 strncat((char *)fcn->name, ".", len);
7305 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7306 }
7307 strncat((char *)fcn->name, ">", len);
7308 fcn->function_ref = ref_io;
7309
7310 /* Fill last entries. */
7311 cli_kws->kw[0].private = fcn;
7312 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7313 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7314 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7315
7316 /* Register this new converter */
7317 cli_register_kw(cli_kws);
7318
7319 return 0;
7320}
7321
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007322static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7323 struct proxy *defpx, const char *file, int line,
7324 char **err, unsigned int *timeout)
7325{
7326 const char *error;
7327
7328 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007329 if (error == PARSE_TIME_OVER) {
7330 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7331 args[1], args[0]);
7332 return -1;
7333 }
7334 else if (error == PARSE_TIME_UNDER) {
7335 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7336 args[1], args[0]);
7337 return -1;
7338 }
7339 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007340 memprintf(err, "%s: invalid timeout", args[0]);
7341 return -1;
7342 }
7343 return 0;
7344}
7345
7346static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7347 struct proxy *defpx, const char *file, int line,
7348 char **err)
7349{
7350 return hlua_read_timeout(args, section_type, curpx, defpx,
7351 file, line, err, &hlua_timeout_session);
7352}
7353
7354static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7355 struct proxy *defpx, const char *file, int line,
7356 char **err)
7357{
7358 return hlua_read_timeout(args, section_type, curpx, defpx,
7359 file, line, err, &hlua_timeout_task);
7360}
7361
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007362static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7363 struct proxy *defpx, const char *file, int line,
7364 char **err)
7365{
7366 return hlua_read_timeout(args, section_type, curpx, defpx,
7367 file, line, err, &hlua_timeout_applet);
7368}
7369
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007370static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7371 struct proxy *defpx, const char *file, int line,
7372 char **err)
7373{
7374 char *error;
7375
7376 hlua_nb_instruction = strtoll(args[1], &error, 10);
7377 if (*error != '\0') {
7378 memprintf(err, "%s: invalid number", args[0]);
7379 return -1;
7380 }
7381 return 0;
7382}
7383
Willy Tarreau32f61e22015-03-18 17:54:59 +01007384static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7385 struct proxy *defpx, const char *file, int line,
7386 char **err)
7387{
7388 char *error;
7389
7390 if (*(args[1]) == 0) {
7391 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7392 return -1;
7393 }
7394 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7395 if (*error != '\0') {
7396 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7397 return -1;
7398 }
7399 return 0;
7400}
7401
7402
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007403/* This function is called by the main configuration key "lua-load". It loads and
7404 * execute an lua file during the parsing of the HAProxy configuration file. It is
7405 * the main lua entry point.
7406 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007407 * This function runs with the HAProxy keywords API. It returns -1 if an error
7408 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007409 *
7410 * In some error case, LUA set an error message in top of the stack. This function
7411 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007412 *
7413 * This function can fail with an abort() due to an Lua critical error.
7414 * We are in the configuration parsing process of HAProxy, this abort() is
7415 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007416 */
7417static int hlua_load(char **args, int section_type, struct proxy *curpx,
7418 struct proxy *defpx, const char *file, int line,
7419 char **err)
7420{
7421 int error;
7422
7423 /* Just load and compile the file. */
7424 error = luaL_loadfile(gL.T, args[1]);
7425 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007426 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007427 lua_pop(gL.T, 1);
7428 return -1;
7429 }
7430
7431 /* If no syntax error where detected, execute the code. */
7432 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7433 switch (error) {
7434 case LUA_OK:
7435 break;
7436 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007437 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007438 lua_pop(gL.T, 1);
7439 return -1;
7440 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007441 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007442 return -1;
7443 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007444 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007445 lua_pop(gL.T, 1);
7446 return -1;
7447 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007448 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007449 lua_pop(gL.T, 1);
7450 return -1;
7451 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007452 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007453 lua_pop(gL.T, 1);
7454 return -1;
7455 }
7456
7457 return 0;
7458}
7459
7460/* configuration keywords declaration */
7461static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007462 { CFG_GLOBAL, "lua-load", hlua_load },
7463 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7464 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007465 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007466 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007467 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007468 { 0, NULL, NULL },
7469}};
7470
Willy Tarreau0108d902018-11-25 19:14:37 +01007471INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7472
Christopher Fauletafd8f102018-11-08 11:34:21 +01007473
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007474/* This function can fail with an abort() due to an Lua critical error.
7475 * We are in the initialisation process of HAProxy, this abort() is
7476 * tolerated.
7477 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007478int hlua_post_init()
7479{
7480 struct hlua_init_function *init;
7481 const char *msg;
7482 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007483 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007484
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007485 /* Call post initialisation function in safe environement. */
7486 if (!SET_SAFE_LJMP(gL.T)) {
7487 if (lua_type(gL.T, -1) == LUA_TSTRING)
7488 error = lua_tostring(gL.T, -1);
7489 else
7490 error = "critical error";
7491 fprintf(stderr, "Lua post-init: %s.\n", error);
7492 exit(1);
7493 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007494
7495#if USE_OPENSSL
7496 /* Initialize SSL server. */
7497 if (socket_ssl.xprt->prepare_srv) {
7498 int saved_used_backed = global.ssl_used_backend;
7499 // don't affect maxconn automatic computation
7500 socket_ssl.xprt->prepare_srv(&socket_ssl);
7501 global.ssl_used_backend = saved_used_backed;
7502 }
7503#endif
7504
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007505 hlua_fcn_post_init(gL.T);
7506 RESET_SAFE_LJMP(gL.T);
7507
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007508 list_for_each_entry(init, &hlua_init_functions, l) {
7509 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7510 ret = hlua_ctx_resume(&gL, 0);
7511 switch (ret) {
7512 case HLUA_E_OK:
7513 lua_pop(gL.T, -1);
7514 return 1;
7515 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007516 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007517 return 0;
7518 case HLUA_E_ERRMSG:
7519 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007520 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007521 return 0;
7522 case HLUA_E_ERR:
7523 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007524 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007525 return 0;
7526 }
7527 }
7528 return 1;
7529}
7530
Willy Tarreau32f61e22015-03-18 17:54:59 +01007531/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7532 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7533 * is the previously allocated size or the kind of object in case of a new
7534 * allocation. <nsize> is the requested new size.
7535 */
7536static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7537{
7538 struct hlua_mem_allocator *zone = ud;
7539
7540 if (nsize == 0) {
7541 /* it's a free */
7542 if (ptr)
7543 zone->allocated -= osize;
7544 free(ptr);
7545 return NULL;
7546 }
7547
7548 if (!ptr) {
7549 /* it's a new allocation */
7550 if (zone->limit && zone->allocated + nsize > zone->limit)
7551 return NULL;
7552
7553 ptr = malloc(nsize);
7554 if (ptr)
7555 zone->allocated += nsize;
7556 return ptr;
7557 }
7558
7559 /* it's a realloc */
7560 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7561 return NULL;
7562
7563 ptr = realloc(ptr, nsize);
7564 if (ptr)
7565 zone->allocated += nsize - osize;
7566 return ptr;
7567}
7568
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007569/* Ithis function can fail with an abort() due to an Lua critical error.
7570 * We are in the initialisation process of HAProxy, this abort() is
7571 * tolerated.
7572 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007573void hlua_init(void)
7574{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007575 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007576 int idx;
7577 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007578 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007579 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007580 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007581#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007582 struct srv_kw *kw;
7583 int tmp_error;
7584 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007585 char *args[] = { /* SSL client configuration. */
7586 "ssl",
7587 "verify",
7588 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007589 NULL
7590 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007591#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007592
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007593 /* Init main lua stack. */
7594 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007595 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007596 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007597 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007598 hlua_sethlua(&gL);
7599 gL.Tref = LUA_REFNIL;
7600 gL.task = NULL;
7601
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007602 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007603 * the Lua function can fail with an abort. We are in the initialisation
7604 * process of HAProxy, this abort() is tolerated.
7605 */
7606
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007607 /* Initialise lua. */
7608 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007609
Thierry Fournier75933d42016-01-21 09:30:18 +01007610 /* Set safe environment for the initialisation. */
7611 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007612 if (lua_type(gL.T, -1) == LUA_TSTRING)
7613 error_msg = lua_tostring(gL.T, -1);
7614 else
7615 error_msg = "critical error";
7616 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007617 exit(1);
7618 }
7619
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007620 /*
7621 *
7622 * Create "core" object.
7623 *
7624 */
7625
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007626 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007627 lua_newtable(gL.T);
7628
7629 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007630 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007631 hlua_class_const_int(gL.T, log_levels[i], i);
7632
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007633 /* Register special functions. */
7634 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007635 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007636 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007637 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007638 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007639 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007640 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007641 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007642 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007643 hlua_class_function(gL.T, "sleep", hlua_sleep);
7644 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007645 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7646 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7647 hlua_class_function(gL.T, "set_map", hlua_set_map);
7648 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007649 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007650 hlua_class_function(gL.T, "log", hlua_log);
7651 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7652 hlua_class_function(gL.T, "Info", hlua_log_info);
7653 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7654 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007655 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007656 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007657
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007658 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007659
7660 /*
7661 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007662 * Register class Map
7663 *
7664 */
7665
7666 /* This table entry is the object "Map" base. */
7667 lua_newtable(gL.T);
7668
7669 /* register pattern types. */
7670 for (i=0; i<PAT_MATCH_NUM; i++)
7671 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007672 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007673 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7674 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007675 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007676
7677 /* register constructor. */
7678 hlua_class_function(gL.T, "new", hlua_map_new);
7679
7680 /* Create and fill the metatable. */
7681 lua_newtable(gL.T);
7682
7683 /* Create and fille the __index entry. */
7684 lua_pushstring(gL.T, "__index");
7685 lua_newtable(gL.T);
7686
7687 /* Register . */
7688 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7689 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7690
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007691 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007692
Thierry Fournier45e78d72016-02-19 18:34:46 +01007693 /* Register previous table in the registry with reference and named entry.
7694 * The function hlua_register_metatable() pops the stack, so we
7695 * previously create a copy of the table.
7696 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007697 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007698 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007699
7700 /* Assign the metatable to the mai Map object. */
7701 lua_setmetatable(gL.T, -2);
7702
7703 /* Set a name to the table. */
7704 lua_setglobal(gL.T, "Map");
7705
7706 /*
7707 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007708 * Register class Channel
7709 *
7710 */
7711
7712 /* Create and fill the metatable. */
7713 lua_newtable(gL.T);
7714
7715 /* Create and fille the __index entry. */
7716 lua_pushstring(gL.T, "__index");
7717 lua_newtable(gL.T);
7718
7719 /* Register . */
7720 hlua_class_function(gL.T, "get", hlua_channel_get);
7721 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7722 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7723 hlua_class_function(gL.T, "set", hlua_channel_set);
7724 hlua_class_function(gL.T, "append", hlua_channel_append);
7725 hlua_class_function(gL.T, "send", hlua_channel_send);
7726 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7727 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7728 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007729 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007730
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007731 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007732
7733 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007734 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007735
7736 /*
7737 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007738 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007739 *
7740 */
7741
7742 /* Create and fill the metatable. */
7743 lua_newtable(gL.T);
7744
7745 /* Create and fille the __index entry. */
7746 lua_pushstring(gL.T, "__index");
7747 lua_newtable(gL.T);
7748
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007749 /* Browse existing fetches and create the associated
7750 * object method.
7751 */
7752 sf = NULL;
7753 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7754
7755 /* Dont register the keywork if the arguments check function are
7756 * not safe during the runtime.
7757 */
7758 if ((sf->val_args != NULL) &&
7759 (sf->val_args != val_payload_lv) &&
7760 (sf->val_args != val_hdr))
7761 continue;
7762
7763 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7764 * by an underscore.
7765 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007766 strncpy(trash.area, sf->kw, trash.size);
7767 trash.area[trash.size - 1] = '\0';
7768 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007769 if (*p == '.' || *p == '-' || *p == '+')
7770 *p = '_';
7771
7772 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007773 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007774 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007775 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007776 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007777 }
7778
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007779 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007780
7781 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007782 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007783
7784 /*
7785 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007786 * Register class Converters
7787 *
7788 */
7789
7790 /* Create and fill the metatable. */
7791 lua_newtable(gL.T);
7792
7793 /* Create and fill the __index entry. */
7794 lua_pushstring(gL.T, "__index");
7795 lua_newtable(gL.T);
7796
7797 /* Browse existing converters and create the associated
7798 * object method.
7799 */
7800 sc = NULL;
7801 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7802 /* Dont register the keywork if the arguments check function are
7803 * not safe during the runtime.
7804 */
7805 if (sc->val_args != NULL)
7806 continue;
7807
7808 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7809 * by an underscore.
7810 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007811 strncpy(trash.area, sc->kw, trash.size);
7812 trash.area[trash.size - 1] = '\0';
7813 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007814 if (*p == '.' || *p == '-' || *p == '+')
7815 *p = '_';
7816
7817 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007818 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007819 lua_pushlightuserdata(gL.T, sc);
7820 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007821 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007822 }
7823
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007824 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007825
7826 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007827 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007828
7829 /*
7830 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007831 * Register class HTTP
7832 *
7833 */
7834
7835 /* Create and fill the metatable. */
7836 lua_newtable(gL.T);
7837
7838 /* Create and fille the __index entry. */
7839 lua_pushstring(gL.T, "__index");
7840 lua_newtable(gL.T);
7841
7842 /* Register Lua functions. */
7843 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7844 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7845 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7846 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7847 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7848 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7849 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7850 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7851 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7852 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7853
7854 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7855 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7856 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7857 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7858 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7859 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007860 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007861
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007862 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007863
7864 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007865 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007866
7867 /*
7868 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007869 * Register class AppletTCP
7870 *
7871 */
7872
7873 /* Create and fill the metatable. */
7874 lua_newtable(gL.T);
7875
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007876 /* Create and fille the __index entry. */
7877 lua_pushstring(gL.T, "__index");
7878 lua_newtable(gL.T);
7879
7880 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007881 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7882 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7883 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7884 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7885 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007886 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7887 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7888 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007889
7890 lua_settable(gL.T, -3);
7891
7892 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007893 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007894
7895 /*
7896 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007897 * Register class AppletHTTP
7898 *
7899 */
7900
7901 /* Create and fill the metatable. */
7902 lua_newtable(gL.T);
7903
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007904 /* Create and fille the __index entry. */
7905 lua_pushstring(gL.T, "__index");
7906 lua_newtable(gL.T);
7907
7908 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007909 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7910 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007911 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7912 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7913 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007914 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7915 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7916 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7917 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7918 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7919 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7920
7921 lua_settable(gL.T, -3);
7922
7923 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007924 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007925
7926 /*
7927 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007928 * Register class TXN
7929 *
7930 */
7931
7932 /* Create and fill the metatable. */
7933 lua_newtable(gL.T);
7934
7935 /* Create and fille the __index entry. */
7936 lua_pushstring(gL.T, "__index");
7937 lua_newtable(gL.T);
7938
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007939 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04007940 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7941 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
7942 hlua_class_function(gL.T, "set_var", hlua_set_var);
7943 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
7944 hlua_class_function(gL.T, "get_var", hlua_get_var);
7945 hlua_class_function(gL.T, "done", hlua_txn_done);
7946 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
7947 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7948 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
7949 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
7950 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
7951 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7952 hlua_class_function(gL.T, "log", hlua_txn_log);
7953 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7954 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7955 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7956 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007957
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007958 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007959
7960 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007961 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007962
7963 /*
7964 *
7965 * Register class Socket
7966 *
7967 */
7968
7969 /* Create and fill the metatable. */
7970 lua_newtable(gL.T);
7971
7972 /* Create and fille the __index entry. */
7973 lua_pushstring(gL.T, "__index");
7974 lua_newtable(gL.T);
7975
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007976#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007977 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007978#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007979 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7980 hlua_class_function(gL.T, "send", hlua_socket_send);
7981 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7982 hlua_class_function(gL.T, "close", hlua_socket_close);
7983 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7984 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7985 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7986 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7987
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 the garbage collector entry. */
7991 lua_pushstring(gL.T, "__gc");
7992 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007993 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007994
7995 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007996 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007997
7998 /* Proxy and server configuration initialisation. */
7999 memset(&socket_proxy, 0, sizeof(socket_proxy));
8000 init_new_proxy(&socket_proxy);
8001 socket_proxy.parent = NULL;
8002 socket_proxy.last_change = now.tv_sec;
8003 socket_proxy.id = "LUA-SOCKET";
8004 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8005 socket_proxy.maxconn = 0;
8006 socket_proxy.accept = NULL;
8007 socket_proxy.options2 |= PR_O2_INDEPSTR;
8008 socket_proxy.srv = NULL;
8009 socket_proxy.conn_retries = 0;
8010 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8011
8012 /* Init TCP server: unchanged parameters */
8013 memset(&socket_tcp, 0, sizeof(socket_tcp));
8014 socket_tcp.next = NULL;
8015 socket_tcp.proxy = &socket_proxy;
8016 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8017 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008018 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008019 socket_tcp.priv_conns = NULL;
8020 socket_tcp.idle_conns = NULL;
8021 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008022 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008023 socket_tcp.last_change = 0;
8024 socket_tcp.id = "LUA-TCP-CONN";
8025 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8026 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8027 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8028
8029 /* XXX: Copy default parameter from default server,
8030 * but the default server is not initialized.
8031 */
8032 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8033 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8034 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8035 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8036 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8037 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8038 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8039 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8040 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8041 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8042
8043 socket_tcp.check.status = HCHK_STATUS_INI;
8044 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8045 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8046 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8047 socket_tcp.check.server = &socket_tcp;
8048
8049 socket_tcp.agent.status = HCHK_STATUS_INI;
8050 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8051 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8052 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8053 socket_tcp.agent.server = &socket_tcp;
8054
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008055 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008056
8057#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008058 /* Init TCP server: unchanged parameters */
8059 memset(&socket_ssl, 0, sizeof(socket_ssl));
8060 socket_ssl.next = NULL;
8061 socket_ssl.proxy = &socket_proxy;
8062 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8063 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008064 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008065 socket_ssl.priv_conns = NULL;
8066 socket_ssl.idle_conns = NULL;
8067 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008068 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008069 socket_ssl.last_change = 0;
8070 socket_ssl.id = "LUA-SSL-CONN";
8071 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8072 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8073 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8074
8075 /* XXX: Copy default parameter from default server,
8076 * but the default server is not initialized.
8077 */
8078 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8079 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8080 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8081 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8082 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8083 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8084 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8085 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8086 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8087 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8088
8089 socket_ssl.check.status = HCHK_STATUS_INI;
8090 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8091 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8092 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8093 socket_ssl.check.server = &socket_ssl;
8094
8095 socket_ssl.agent.status = HCHK_STATUS_INI;
8096 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8097 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8098 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8099 socket_ssl.agent.server = &socket_ssl;
8100
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008101 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008102 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008103
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008104 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008105 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8106 /*
8107 *
8108 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008109 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008110 * features like client certificates and ssl_verify.
8111 *
8112 */
8113 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8114 if (tmp_error != 0) {
8115 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8116 abort(); /* This must be never arrives because the command line
8117 not editable by the user. */
8118 }
8119 idx += kw->skip;
8120 }
8121 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008122#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008123
8124 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008125}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008126
Willy Tarreau80713382018-11-26 10:19:54 +01008127static void hlua_register_build_options(void)
8128{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008129 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008130
Willy Tarreaubb57d942016-12-21 19:04:56 +01008131 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8132 hap_register_build_opts(ptr, 1);
8133}
Willy Tarreau80713382018-11-26 10:19:54 +01008134
8135INITCALL0(STG_REGISTER, hlua_register_build_options);