blob: c1cf83c459388c37aec153f3de30096b05dd7af3 [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 FOURNIER9a819e72015-02-16 20:22:55 +010045#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010046#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010047#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020048#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010049#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020050#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020051#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010052#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040053#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010054#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010055#include <proto/payload.h>
56#include <proto/proto_http.h>
57#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010058#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020059#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020060#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010061#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010062#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010063#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020064#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010065
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010066/* Lua uses longjmp to perform yield or throwing errors. This
67 * macro is used only for identifying the function that can
68 * not return because a longjmp is executed.
69 * __LJMP marks a prototype of hlua file that can use longjmp.
70 * WILL_LJMP() marks an lua function that will use longjmp.
71 * MAY_LJMP() marks an lua function that may use longjmp.
72 */
73#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020074#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010075#define MAY_LJMP(func) func
76
Thierry FOURNIERbabae282015-09-17 11:36:37 +020077/* This couple of function executes securely some Lua calls outside of
78 * the lua runtime environment. Each Lua call can return a longjmp
79 * if it encounter a memory error.
80 *
81 * Lua documentation extract:
82 *
83 * If an error happens outside any protected environment, Lua calls
84 * a panic function (see lua_atpanic) and then calls abort, thus
85 * exiting the host application. Your panic function can avoid this
86 * exit by never returning (e.g., doing a long jump to your own
87 * recovery point outside Lua).
88 *
89 * The panic function runs as if it were a message handler (see
90 * §2.3); in particular, the error message is at the top of the
91 * stack. However, there is no guarantee about stack space. To push
92 * anything on the stack, the panic function must first check the
93 * available space (see §4.2).
94 *
95 * We must check all the Lua entry point. This includes:
96 * - The include/proto/hlua.h exported functions
97 * - the task wrapper function
98 * - The action wrapper function
99 * - The converters wrapper function
100 * - The sample-fetch wrapper functions
101 *
102 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800103 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200104 *
105 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
106 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
107 * because they must be exists in the program stack when the longjmp
108 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200109 *
110 * Note that the Lua processing is not really thread safe. It provides
111 * heavy system which consists to add our own lock function in the Lua
112 * code and recompile the library. This system will probably not accepted
113 * by maintainers of various distribs.
114 *
115 * Our main excution point of the Lua is the function lua_resume(). A
116 * quick looking on the Lua sources displays a lua_lock() a the start
117 * of function and a lua_unlock() at the end of the function. So I
118 * conclude that the Lua thread safe mode just perform a mutex around
119 * all execution. So I prefer to do this in the HAProxy code, it will be
120 * easier for distro maintainers.
121 *
122 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
123 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
124 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100126__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200127THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128static int hlua_panic_safe(lua_State *L) { return 0; }
129static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
130
131#define SET_SAFE_LJMP(__L) \
132 ({ \
133 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 if (setjmp(safe_ljmp_env) != 0) { \
136 lua_atpanic(__L, hlua_panic_safe); \
137 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100138 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200139 } else { \
140 lua_atpanic(__L, hlua_panic_ljmp); \
141 ret = 1; \
142 } \
143 ret; \
144 })
145
146/* If we are the last function catching Lua errors, we
147 * must reset the panic function.
148 */
149#define RESET_SAFE_LJMP(__L) \
150 do { \
151 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100152 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200153 } while(0)
154
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200155/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100157/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200159/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100160#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100161#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200162
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100163/* The main Lua execution context. */
164struct hlua gL;
165
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100166/* This is the memory pool containing struct lua for applets
167 * (including cli).
168 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100169DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100170
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100171/* Used for Socket connection. */
172static struct proxy socket_proxy;
173static struct server socket_tcp;
174#ifdef USE_OPENSSL
175static struct server socket_ssl;
176#endif
177
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100178/* List head of the function called at the initialisation time. */
179struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
180
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100181/* The following variables contains the reference of the different
182 * Lua classes. These references are useful for identify metadata
183 * associated with an object.
184 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100185static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100186static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100187static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100188static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100189static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100190static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200191static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200192static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200193static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100194
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100195/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200196 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100197 * short timeout. Lua linked with tasks doesn't have a timeout
198 * because a task may remain alive during all the haproxy execution.
199 */
200static unsigned int hlua_timeout_session = 4000; /* session timeout. */
201static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200202static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100203
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100204/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
205 * it is used for preventing infinite loops.
206 *
207 * I test the scheer with an infinite loop containing one incrementation
208 * and one test. I run this loop between 10 seconds, I raise a ceil of
209 * 710M loops from one interrupt each 9000 instructions, so I fix the value
210 * to one interrupt each 10 000 instructions.
211 *
212 * configured | Number of
213 * instructions | loops executed
214 * between two | in milions
215 * forced yields |
216 * ---------------+---------------
217 * 10 | 160
218 * 500 | 670
219 * 1000 | 680
220 * 5000 | 700
221 * 7000 | 700
222 * 8000 | 700
223 * 9000 | 710 <- ceil
224 * 10000 | 710
225 * 100000 | 710
226 * 1000000 | 710
227 *
228 */
229static unsigned int hlua_nb_instruction = 10000;
230
Willy Tarreau32f61e22015-03-18 17:54:59 +0100231/* Descriptor for the memory allocation state. If limit is not null, it will
232 * be enforced on any memory allocation.
233 */
234struct hlua_mem_allocator {
235 size_t allocated;
236 size_t limit;
237};
238
239static struct hlua_mem_allocator hlua_global_allocator;
240
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100241/* These functions converts types between HAProxy internal args or
242 * sample and LUA types. Another function permits to check if the
243 * LUA stack contains arguments according with an required ARG_T
244 * format.
245 */
246static int hlua_arg2lua(lua_State *L, const struct arg *arg);
247static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100248__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100249 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100250static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100251static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100252static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
253
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200254__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
255
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200256#define SEND_ERR(__be, __fmt, __args...) \
257 do { \
258 send_log(__be, LOG_ERR, __fmt, ## __args); \
259 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100260 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200261 } while (0)
262
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100263/* Used to check an Lua function type in the stack. It creates and
264 * returns a reference of the function. This function throws an
265 * error if the rgument is not a "function".
266 */
267__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
268{
269 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100270 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100271 WILL_LJMP(luaL_argerror(L, argno, msg));
272 }
273 lua_pushvalue(L, argno);
274 return luaL_ref(L, LUA_REGISTRYINDEX);
275}
276
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200277/* Return the string that is of the top of the stack. */
278const char *hlua_get_top_error_string(lua_State *L)
279{
280 if (lua_gettop(L) < 1)
281 return "unknown error";
282 if (lua_type(L, -1) != LUA_TSTRING)
283 return "unknown error";
284 return lua_tostring(L, -1);
285}
286
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200287__LJMP static const char *hlua_traceback(lua_State *L)
288{
289 lua_Debug ar;
290 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200291 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200292 int filled = 0;
293
294 while (lua_getstack(L, level++, &ar)) {
295
296 /* Add separator */
297 if (filled)
298 chunk_appendf(msg, ", ");
299 filled = 1;
300
301 /* Fill fields:
302 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
303 * 'l': fills in the field currentline;
304 * 'n': fills in the field name and namewhat;
305 * 't': fills in the field istailcall;
306 */
307 lua_getinfo(L, "Slnt", &ar);
308
309 /* Append code localisation */
310 if (ar.currentline > 0)
311 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
312 else
313 chunk_appendf(msg, "%s ", ar.short_src);
314
315 /*
316 * Get function name
317 *
318 * if namewhat is no empty, name is defined.
319 * what contains "Lua" for Lua function, "C" for C function,
320 * or "main" for main code.
321 */
322 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
323 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
324
325 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
326 chunk_appendf(msg, "main chunk");
327
328 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
329 chunk_appendf(msg, "C function line %d", ar.linedefined);
330
331 else /* nothing left... */
332 chunk_appendf(msg, "?");
333
334
335 /* Display tailed call */
336 if (ar.istailcall)
337 chunk_appendf(msg, " ...");
338 }
339
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200340 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200341}
342
343
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100344/* This function check the number of arguments available in the
345 * stack. If the number of arguments available is not the same
346 * then <nb> an error is throwed.
347 */
348__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
349{
350 if (lua_gettop(L) == nb)
351 return;
352 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
353}
354
Mark Lakes22154b42018-01-29 14:38:40 -0800355/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100356 * and the line number where the error is encountered.
357 */
358static int hlua_pusherror(lua_State *L, const char *fmt, ...)
359{
360 va_list argp;
361 va_start(argp, fmt);
362 luaL_where(L, 1);
363 lua_pushvfstring(L, fmt, argp);
364 va_end(argp);
365 lua_concat(L, 2);
366 return 1;
367}
368
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100369/* This functions is used with sample fetch and converters. It
370 * converts the HAProxy configuration argument in a lua stack
371 * values.
372 *
373 * It takes an array of "arg", and each entry of the array is
374 * converted and pushed in the LUA stack.
375 */
376static int hlua_arg2lua(lua_State *L, const struct arg *arg)
377{
378 switch (arg->type) {
379 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380 case ARGT_TIME:
381 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100382 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200386 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100387 break;
388
389 case ARGT_IPV4:
390 case ARGT_IPV6:
391 case ARGT_MSK4:
392 case ARGT_MSK6:
393 case ARGT_FE:
394 case ARGT_BE:
395 case ARGT_TAB:
396 case ARGT_SRV:
397 case ARGT_USR:
398 case ARGT_MAP:
399 default:
400 lua_pushnil(L);
401 break;
402 }
403 return 1;
404}
405
406/* This function take one entrie in an LUA stack at the index "ud",
407 * and try to convert it in an HAProxy argument entry. This is useful
408 * with sample fetch wrappers. The input arguments are gived to the
409 * lua wrapper and converted as arg list by thi function.
410 */
411static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
412{
413 switch (lua_type(L, ud)) {
414
415 case LUA_TNUMBER:
416 case LUA_TBOOLEAN:
417 arg->type = ARGT_SINT;
418 arg->data.sint = lua_tointeger(L, ud);
419 break;
420
421 case LUA_TSTRING:
422 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200423 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100424 break;
425
426 case LUA_TUSERDATA:
427 case LUA_TNIL:
428 case LUA_TTABLE:
429 case LUA_TFUNCTION:
430 case LUA_TTHREAD:
431 case LUA_TLIGHTUSERDATA:
432 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200433 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100434 break;
435 }
436 return 1;
437}
438
439/* the following functions are used to convert a struct sample
440 * in Lua type. This useful to convert the return of the
441 * fetchs or converters.
442 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100443static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100444{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200445 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200448 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 break;
450
451 case SMP_T_BIN:
452 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200453 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 break;
455
456 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200457 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
459 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
460 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
461 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
462 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
463 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
464 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
465 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
466 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100468 break;
469 default:
470 lua_pushnil(L);
471 break;
472 }
473 break;
474
475 case SMP_T_IPV4:
476 case SMP_T_IPV6:
477 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200478 if (sample_casts[smp->data.type][SMP_T_STR] &&
479 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100481 else
482 lua_pushnil(L);
483 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100484 default:
485 lua_pushnil(L);
486 break;
487 }
488 return 1;
489}
490
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100491/* the following functions are used to convert a struct sample
492 * in Lua strings. This is useful to convert the return of the
493 * fetchs or converters.
494 */
495static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
496{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200497 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100498
499 case SMP_T_BIN:
500 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100502 break;
503
504 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200505 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100506 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
507 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
508 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
509 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
510 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
511 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
512 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
513 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
514 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100516 break;
517 default:
518 lua_pushstring(L, "");
519 break;
520 }
521 break;
522
523 case SMP_T_SINT:
524 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100525 case SMP_T_IPV4:
526 case SMP_T_IPV6:
527 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200528 if (sample_casts[smp->data.type][SMP_T_STR] &&
529 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100531 else
532 lua_pushstring(L, "");
533 break;
534 default:
535 lua_pushstring(L, "");
536 break;
537 }
538 return 1;
539}
540
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100541/* the following functions are used to convert an Lua type in a
542 * struct sample. This is useful to provide data from a converter
543 * to the LUA code.
544 */
545static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
546{
547 switch (lua_type(L, ud)) {
548
549 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200551 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100552 break;
553
554
555 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200556 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100558 break;
559
560 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200561 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100562 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200563 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566 case LUA_TUSERDATA:
567 case LUA_TNIL:
568 case LUA_TTABLE:
569 case LUA_TFUNCTION:
570 case LUA_TTHREAD:
571 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200572 case LUA_TNONE:
573 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200574 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200575 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100576 break;
577 }
578 return 1;
579}
580
581/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800582 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100583 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100584 *
585 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
586 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100587 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100588__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100589 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590{
591 int min_arg;
592 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100593 struct proxy *px;
594 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595
596 idx = 0;
597 min_arg = ARGM(mask);
598 mask >>= ARGM_BITS;
599
600 while (1) {
601
602 /* Check oversize. */
603 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100604 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100605 }
606
607 /* Check for mandatory arguments. */
608 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100609 if (idx < min_arg) {
610
611 /* If miss other argument than the first one, we return an error. */
612 if (idx > 0)
613 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
614
615 /* If first argument have a certain type, some default values
616 * may be used. See the function smp_resolve_args().
617 */
618 switch (mask & ARGT_MASK) {
619
620 case ARGT_FE:
621 if (!(p->cap & PR_CAP_FE))
622 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
623 argp[idx].data.prx = p;
624 argp[idx].type = ARGT_FE;
625 argp[idx+1].type = ARGT_STOP;
626 break;
627
628 case ARGT_BE:
629 if (!(p->cap & PR_CAP_BE))
630 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
631 argp[idx].data.prx = p;
632 argp[idx].type = ARGT_BE;
633 argp[idx+1].type = ARGT_STOP;
634 break;
635
636 case ARGT_TAB:
637 argp[idx].data.prx = p;
638 argp[idx].type = ARGT_TAB;
639 argp[idx+1].type = ARGT_STOP;
640 break;
641
642 default:
643 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
644 break;
645 }
646 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100647 return 0;
648 }
649
650 /* Check for exceed the number of requiered argument. */
651 if ((mask & ARGT_MASK) == ARGT_STOP &&
652 argp[idx].type != ARGT_STOP) {
653 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
654 }
655
656 if ((mask & ARGT_MASK) == ARGT_STOP &&
657 argp[idx].type == ARGT_STOP) {
658 return 0;
659 }
660
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100661 /* Convert some argument types. */
662 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100663 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100664 if (argp[idx].type != ARGT_SINT)
665 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
666 argp[idx].type = ARGT_SINT;
667 break;
668
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 case ARGT_TIME:
670 if (argp[idx].type != ARGT_SINT)
671 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200672 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100673 break;
674
675 case ARGT_SIZE:
676 if (argp[idx].type != ARGT_SINT)
677 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200678 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 break;
680
681 case ARGT_FE:
682 if (argp[idx].type != ARGT_STR)
683 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 memcpy(trash.area, argp[idx].data.str.area,
685 argp[idx].data.str.data);
686 trash.area[argp[idx].data.str.data] = 0;
687 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100688 if (!argp[idx].data.prx)
689 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
690 argp[idx].type = ARGT_FE;
691 break;
692
693 case ARGT_BE:
694 if (argp[idx].type != ARGT_STR)
695 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 memcpy(trash.area, argp[idx].data.str.area,
697 argp[idx].data.str.data);
698 trash.area[argp[idx].data.str.data] = 0;
699 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100700 if (!argp[idx].data.prx)
701 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
702 argp[idx].type = ARGT_BE;
703 break;
704
705 case ARGT_TAB:
706 if (argp[idx].type != ARGT_STR)
707 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200708 memcpy(trash.area, argp[idx].data.str.area,
709 argp[idx].data.str.data);
710 trash.area[argp[idx].data.str.data] = 0;
711 argp[idx].data.prx = proxy_tbl_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100712 if (!argp[idx].data.prx)
713 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
714 argp[idx].type = ARGT_TAB;
715 break;
716
717 case ARGT_SRV:
718 if (argp[idx].type != ARGT_STR)
719 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200720 memcpy(trash.area, argp[idx].data.str.area,
721 argp[idx].data.str.data);
722 trash.area[argp[idx].data.str.data] = 0;
723 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100724 if (sname) {
725 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200727 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100728 if (!px)
729 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
730 }
731 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100733 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100734 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100735 argp[idx].data.srv = findserver(px, sname);
736 if (!argp[idx].data.srv)
737 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
738 argp[idx].type = ARGT_SRV;
739 break;
740
741 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200742 memcpy(trash.area, argp[idx].data.str.area,
743 argp[idx].data.str.data);
744 trash.area[argp[idx].data.str.data] = 0;
745 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100746 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
747 argp[idx].type = ARGT_IPV4;
748 break;
749
750 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200751 memcpy(trash.area, argp[idx].data.str.area,
752 argp[idx].data.str.data);
753 trash.area[argp[idx].data.str.data] = 0;
754 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100755 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
756 argp[idx].type = ARGT_MSK4;
757 break;
758
759 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200760 memcpy(trash.area, argp[idx].data.str.area,
761 argp[idx].data.str.data);
762 trash.area[argp[idx].data.str.data] = 0;
763 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100764 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
765 argp[idx].type = ARGT_IPV6;
766 break;
767
768 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 memcpy(trash.area, argp[idx].data.str.area,
770 argp[idx].data.str.data);
771 trash.area[argp[idx].data.str.data] = 0;
772 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100773 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
774 argp[idx].type = ARGT_MSK6;
775 break;
776
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100777 case ARGT_MAP:
778 case ARGT_REG:
779 case ARGT_USR:
780 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100781 break;
782 }
783
784 /* Check for type of argument. */
785 if ((mask & ARGT_MASK) != argp[idx].type) {
786 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
787 arg_type_names[(mask & ARGT_MASK)],
788 arg_type_names[argp[idx].type & ARGT_MASK]);
789 WILL_LJMP(luaL_argerror(L, first + idx, msg));
790 }
791
792 /* Next argument. */
793 mask >>= ARGT_BITS;
794 idx++;
795 }
796}
797
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100798/*
799 * The following functions are used to make correspondance between the the
800 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100801 *
802 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803 * - hlua_sethlua : create the association between hlua context and lua_state.
804 */
805static inline struct hlua *hlua_gethlua(lua_State *L)
806{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100807 struct hlua **hlua = lua_getextraspace(L);
808 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809}
810static inline void hlua_sethlua(struct hlua *hlua)
811{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100812 struct hlua **hlua_store = lua_getextraspace(hlua->T);
813 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100814}
815
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100816/* This function is used to send logs. It try to send on screen (stderr)
817 * and on the default syslog server.
818 */
819static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
820{
821 struct tm tm;
822 char *p;
823
824 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200825 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100826 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200827 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200828 /* Break the message if exceed the buffer size. */
829 *(p-4) = ' ';
830 *(p-3) = '.';
831 *(p-2) = '.';
832 *(p-1) = '.';
833 break;
834 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100835 if (isprint(*msg))
836 *p = *msg;
837 else
838 *p = '.';
839 }
840 *p = '\0';
841
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200842 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100843 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200844 get_localtime(date.tv_sec, &tm);
845 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100846 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200847 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100848 fflush(stderr);
849 }
850}
851
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100852/* This function just ensure that the yield will be always
853 * returned with a timeout and permit to set some flags
854 */
855__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100856 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100857{
858 struct hlua *hlua = hlua_gethlua(L);
859
860 /* Set the wake timeout. If timeout is required, we set
861 * the expiration time.
862 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200863 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100864
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100865 hlua->flags |= flags;
866
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100867 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200868 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100869}
870
Willy Tarreau87b09662015-04-03 00:22:06 +0200871/* This function initialises the Lua environment stored in the stream.
872 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100873 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200874 *
875 * This function is particular. it initialises a new Lua thread. If the
876 * initialisation fails (example: out of memory error), the lua function
877 * throws an error (longjmp).
878 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100879 * In some case (at least one), this function can be called from safe
880 * environement, so we must not initialise it. While the support of
881 * threads appear, the safe environment set a lock to ensure only one
882 * Lua execution at a time. If we initialize safe environment in another
883 * safe environmenet, we have a dead lock.
884 *
885 * set "already_safe" true if the context is initialized form safe
886 * Lua fonction.
887 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800888 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200889 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800890 * MUST NOT manipulate the created thread stack state, because it is not
891 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100892 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100893int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100894{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100895 if (!already_safe) {
896 if (!SET_SAFE_LJMP(gL.T)) {
897 lua->Tref = LUA_REFNIL;
898 return 0;
899 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200900 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100901 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100902 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100903 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100904 lua->T = lua_newthread(gL.T);
905 if (!lua->T) {
906 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100907 if (!already_safe)
908 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100909 return 0;
910 }
911 hlua_sethlua(lua);
912 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
913 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100914 if (!already_safe)
915 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100916 return 1;
917}
918
Willy Tarreau87b09662015-04-03 00:22:06 +0200919/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100920 * is destroyed. The destroy also the memory context. The struct "lua"
921 * is not freed.
922 */
923void hlua_ctx_destroy(struct hlua *lua)
924{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100925 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100926 return;
927
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100928 if (!lua->T)
929 goto end;
930
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100931 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200932 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100933
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200934 if (!SET_SAFE_LJMP(lua->T))
935 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100936 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200937 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200938
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200939 if (!SET_SAFE_LJMP(gL.T))
940 return;
941 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
942 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200943 /* Forces a garbage collecting process. If the Lua program is finished
944 * without error, we run the GC on the thread pointer. Its freed all
945 * the unused memory.
946 * If the thread is finnish with an error or is currently yielded,
947 * it seems that the GC applied on the thread doesn't clean anything,
948 * so e run the GC on the main thread.
949 * NOTE: maybe this action locks all the Lua threads untiml the en of
950 * the garbage collection.
951 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200952 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200953 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200954 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200955 lua_gc(gL.T, LUA_GCCOLLECT, 0);
956 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200957 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200958
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200959 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100960
961end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100962 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100963}
964
965/* This function is used to restore the Lua context when a coroutine
966 * fails. This function copy the common memory between old coroutine
967 * and the new coroutine. The old coroutine is destroyed, and its
968 * replaced by the new coroutine.
969 * If the flag "keep_msg" is set, the last entry of the old is assumed
970 * as string error message and it is copied in the new stack.
971 */
972static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
973{
974 lua_State *T;
975 int new_ref;
976
977 /* Renew the main LUA stack doesn't have sense. */
978 if (lua == &gL)
979 return 0;
980
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100981 /* New Lua coroutine. */
982 T = lua_newthread(gL.T);
983 if (!T)
984 return 0;
985
986 /* Copy last error message. */
987 if (keep_msg)
988 lua_xmove(lua->T, T, 1);
989
990 /* Copy data between the coroutines. */
991 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
992 lua_xmove(lua->T, T, 1);
993 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
994
995 /* Destroy old data. */
996 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
997
998 /* The thread is garbage collected by Lua. */
999 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1000
1001 /* Fill the struct with the new coroutine values. */
1002 lua->Mref = new_ref;
1003 lua->T = T;
1004 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1005
1006 /* Set context. */
1007 hlua_sethlua(lua);
1008
1009 return 1;
1010}
1011
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001012void hlua_hook(lua_State *L, lua_Debug *ar)
1013{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001014 struct hlua *hlua = hlua_gethlua(L);
1015
1016 /* Lua cannot yield when its returning from a function,
1017 * so, we can fix the interrupt hook to 1 instruction,
1018 * expecting that the function is finnished.
1019 */
1020 if (lua_gethookmask(L) & LUA_MASKRET) {
1021 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1022 return;
1023 }
1024
1025 /* restore the interrupt condition. */
1026 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1027
1028 /* If we interrupt the Lua processing in yieldable state, we yield.
1029 * If the state is not yieldable, trying yield causes an error.
1030 */
1031 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001032 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001033
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001034 /* If we cannot yield, update the clock and check the timeout. */
1035 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001036 hlua->run_time += now_ms - hlua->start_time;
1037 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001038 lua_pushfstring(L, "execution timeout");
1039 WILL_LJMP(lua_error(L));
1040 }
1041
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001042 /* Update the start time. */
1043 hlua->start_time = now_ms;
1044
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001045 /* Try to interrupt the process at the end of the current
1046 * unyieldable function.
1047 */
1048 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001049}
1050
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001051/* This function start or resumes the Lua stack execution. If the flag
1052 * "yield_allowed" if no set and the LUA stack execution returns a yield
1053 * The function return an error.
1054 *
1055 * The function can returns 4 values:
1056 * - HLUA_E_OK : The execution is terminated without any errors.
1057 * - HLUA_E_AGAIN : The execution must continue at the next associated
1058 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001059 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001060 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001061 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001062 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001063 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001064 * LUA code.
1065 */
1066static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1067{
1068 int ret;
1069 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001070 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001071
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001072 /* Initialise run time counter. */
1073 if (!HLUA_IS_RUNNING(lua))
1074 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001075
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001076 /* Lock the whole Lua execution. This lock must be before the
1077 * label "resume_execution".
1078 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001079 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001080
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001081resume_execution:
1082
1083 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1084 * instructions. it is used for preventing infinite loops.
1085 */
1086 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1087
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001088 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001089 HLUA_SET_RUN(lua);
1090 HLUA_CLR_CTRLYIELD(lua);
1091 HLUA_CLR_WAKERESWR(lua);
1092 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001093
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001094 /* Update the start time. */
1095 lua->start_time = now_ms;
1096
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001097 /* Call the function. */
1098 ret = lua_resume(lua->T, gL.T, lua->nargs);
1099 switch (ret) {
1100
1101 case LUA_OK:
1102 ret = HLUA_E_OK;
1103 break;
1104
1105 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001106 /* Check if the execution timeout is expired. It it is the case, we
1107 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001108 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001109 tv_update_date(0, 1);
1110 lua->run_time += now_ms - lua->start_time;
1111 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001112 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001113 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001114 break;
1115 }
1116 /* Process the forced yield. if the general yield is not allowed or
1117 * if no task were associated this the current Lua execution
1118 * coroutine, we resume the execution. Else we want to return in the
1119 * scheduler and we want to be waked up again, to continue the
1120 * current Lua execution. So we schedule our own task.
1121 */
1122 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001123 if (!yield_allowed || !lua->task)
1124 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001125 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001126 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001127 if (!yield_allowed) {
1128 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001129 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001130 break;
1131 }
1132 ret = HLUA_E_AGAIN;
1133 break;
1134
1135 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001136
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001137 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001138 * because the errors ares the only one mean to return immediately
1139 * from and lua execution.
1140 */
1141 if (lua->flags & HLUA_EXIT) {
1142 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001143 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001144 break;
1145 }
1146
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001147 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001148 if (!lua_checkstack(lua->T, 1)) {
1149 ret = HLUA_E_ERR;
1150 break;
1151 }
1152 msg = lua_tostring(lua->T, -1);
1153 lua_settop(lua->T, 0); /* Empty the stack. */
1154 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001155 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001156 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001157 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001158 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001159 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 ret = HLUA_E_ERRMSG;
1161 break;
1162
1163 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001164 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001165 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001166 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001167 break;
1168
1169 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001170 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 if (!lua_checkstack(lua->T, 1)) {
1172 ret = HLUA_E_ERR;
1173 break;
1174 }
1175 msg = lua_tostring(lua->T, -1);
1176 lua_settop(lua->T, 0); /* Empty the stack. */
1177 lua_pop(lua->T, 1);
1178 if (msg)
1179 lua_pushfstring(lua->T, "message handler error: %s", msg);
1180 else
1181 lua_pushfstring(lua->T, "message handler error");
1182 ret = HLUA_E_ERRMSG;
1183 break;
1184
1185 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001186 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001187 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001188 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001189 break;
1190 }
1191
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001192 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001193 if (lua->flags & HLUA_MUST_GC &&
1194 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001195 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1196
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001197 switch (ret) {
1198 case HLUA_E_AGAIN:
1199 break;
1200
1201 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001202 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001204 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001205 break;
1206
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001207 case HLUA_E_ETMOUT:
1208 case HLUA_E_NOMEM:
1209 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001210 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001211 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001212 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001213 hlua_ctx_renew(lua, 0);
1214 break;
1215
1216 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001217 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001218 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001219 break;
1220 }
1221
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001222 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001223 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001224
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001225 return ret;
1226}
1227
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001228/* This function exit the current code. */
1229__LJMP static int hlua_done(lua_State *L)
1230{
1231 struct hlua *hlua = hlua_gethlua(L);
1232
1233 hlua->flags |= HLUA_EXIT;
1234 WILL_LJMP(lua_error(L));
1235
1236 return 0;
1237}
1238
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001239/* This function is an LUA binding. It provides a function
1240 * for deleting ACL from a referenced ACL file.
1241 */
1242__LJMP static int hlua_del_acl(lua_State *L)
1243{
1244 const char *name;
1245 const char *key;
1246 struct pat_ref *ref;
1247
1248 MAY_LJMP(check_args(L, 2, "del_acl"));
1249
1250 name = MAY_LJMP(luaL_checkstring(L, 1));
1251 key = MAY_LJMP(luaL_checkstring(L, 2));
1252
1253 ref = pat_ref_lookup(name);
1254 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001255 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001256
1257 pat_ref_delete(ref, key);
1258 return 0;
1259}
1260
1261/* This function is an LUA binding. It provides a function
1262 * for deleting map entry from a referenced map file.
1263 */
1264static int hlua_del_map(lua_State *L)
1265{
1266 const char *name;
1267 const char *key;
1268 struct pat_ref *ref;
1269
1270 MAY_LJMP(check_args(L, 2, "del_map"));
1271
1272 name = MAY_LJMP(luaL_checkstring(L, 1));
1273 key = MAY_LJMP(luaL_checkstring(L, 2));
1274
1275 ref = pat_ref_lookup(name);
1276 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001277 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001278
1279 pat_ref_delete(ref, key);
1280 return 0;
1281}
1282
1283/* This function is an LUA binding. It provides a function
1284 * for adding ACL pattern from a referenced ACL file.
1285 */
1286static int hlua_add_acl(lua_State *L)
1287{
1288 const char *name;
1289 const char *key;
1290 struct pat_ref *ref;
1291
1292 MAY_LJMP(check_args(L, 2, "add_acl"));
1293
1294 name = MAY_LJMP(luaL_checkstring(L, 1));
1295 key = MAY_LJMP(luaL_checkstring(L, 2));
1296
1297 ref = pat_ref_lookup(name);
1298 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001299 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001300
1301 if (pat_ref_find_elt(ref, key) == NULL)
1302 pat_ref_add(ref, key, NULL, NULL);
1303 return 0;
1304}
1305
1306/* This function is an LUA binding. It provides a function
1307 * for setting map pattern and sample from a referenced map
1308 * file.
1309 */
1310static int hlua_set_map(lua_State *L)
1311{
1312 const char *name;
1313 const char *key;
1314 const char *value;
1315 struct pat_ref *ref;
1316
1317 MAY_LJMP(check_args(L, 3, "set_map"));
1318
1319 name = MAY_LJMP(luaL_checkstring(L, 1));
1320 key = MAY_LJMP(luaL_checkstring(L, 2));
1321 value = MAY_LJMP(luaL_checkstring(L, 3));
1322
1323 ref = pat_ref_lookup(name);
1324 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001325 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001326
1327 if (pat_ref_find_elt(ref, key) != NULL)
1328 pat_ref_set(ref, key, value, NULL);
1329 else
1330 pat_ref_add(ref, key, value, NULL);
1331 return 0;
1332}
1333
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001334/* A class is a lot of memory that contain data. This data can be a table,
1335 * an integer or user data. This data is associated with a metatable. This
1336 * metatable have an original version registred in the global context with
1337 * the name of the object (_G[<name>] = <metable> ).
1338 *
1339 * A metable is a table that modify the standard behavior of a standard
1340 * access to the associated data. The entries of this new metatable are
1341 * defined as is:
1342 *
1343 * http://lua-users.org/wiki/MetatableEvents
1344 *
1345 * __index
1346 *
1347 * we access an absent field in a table, the result is nil. This is
1348 * true, but it is not the whole truth. Actually, such access triggers
1349 * the interpreter to look for an __index metamethod: If there is no
1350 * such method, as usually happens, then the access results in nil;
1351 * otherwise, the metamethod will provide the result.
1352 *
1353 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1354 * the key does not appear in the table, but the metatable has an __index
1355 * property:
1356 *
1357 * - if the value is a function, the function is called, passing in the
1358 * table and the key; the return value of that function is returned as
1359 * the result.
1360 *
1361 * - if the value is another table, the value of the key in that table is
1362 * asked for and returned (and if it doesn't exist in that table, but that
1363 * table's metatable has an __index property, then it continues on up)
1364 *
1365 * - Use "rawget(myTable,key)" to skip this metamethod.
1366 *
1367 * http://www.lua.org/pil/13.4.1.html
1368 *
1369 * __newindex
1370 *
1371 * Like __index, but control property assignment.
1372 *
1373 * __mode - Control weak references. A string value with one or both
1374 * of the characters 'k' and 'v' which specifies that the the
1375 * keys and/or values in the table are weak references.
1376 *
1377 * __call - Treat a table like a function. When a table is followed by
1378 * parenthesis such as "myTable( 'foo' )" and the metatable has
1379 * a __call key pointing to a function, that function is invoked
1380 * (passing any specified arguments) and the return value is
1381 * returned.
1382 *
1383 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1384 * called, if the metatable for myTable has a __metatable
1385 * key, the value of that key is returned instead of the
1386 * actual metatable.
1387 *
1388 * __tostring - Control string representation. When the builtin
1389 * "tostring( myTable )" function is called, if the metatable
1390 * for myTable has a __tostring property set to a function,
1391 * that function is invoked (passing myTable to it) and the
1392 * return value is used as the string representation.
1393 *
1394 * __len - Control table length. When the table length is requested using
1395 * the length operator ( '#' ), if the metatable for myTable has
1396 * a __len key pointing to a function, that function is invoked
1397 * (passing myTable to it) and the return value used as the value
1398 * of "#myTable".
1399 *
1400 * __gc - Userdata finalizer code. When userdata is set to be garbage
1401 * collected, if the metatable has a __gc field pointing to a
1402 * function, that function is first invoked, passing the userdata
1403 * to it. The __gc metamethod is not called for tables.
1404 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1405 *
1406 * Special metamethods for redefining standard operators:
1407 * http://www.lua.org/pil/13.1.html
1408 *
1409 * __add "+"
1410 * __sub "-"
1411 * __mul "*"
1412 * __div "/"
1413 * __unm "!"
1414 * __pow "^"
1415 * __concat ".."
1416 *
1417 * Special methods for redfining standar relations
1418 * http://www.lua.org/pil/13.2.html
1419 *
1420 * __eq "=="
1421 * __lt "<"
1422 * __le "<="
1423 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001424
1425/*
1426 *
1427 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001428 * Class Map
1429 *
1430 *
1431 */
1432
1433/* Returns a struct hlua_map if the stack entry "ud" is
1434 * a class session, otherwise it throws an error.
1435 */
1436__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1437{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001438 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001439}
1440
1441/* This function is the map constructor. It don't need
1442 * the class Map object. It creates and return a new Map
1443 * object. It must be called only during "body" or "init"
1444 * context because it process some filesystem accesses.
1445 */
1446__LJMP static int hlua_map_new(struct lua_State *L)
1447{
1448 const char *fn;
1449 int match = PAT_MATCH_STR;
1450 struct sample_conv conv;
1451 const char *file = "";
1452 int line = 0;
1453 lua_Debug ar;
1454 char *err = NULL;
1455 struct arg args[2];
1456
1457 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1458 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1459
1460 fn = MAY_LJMP(luaL_checkstring(L, 1));
1461
1462 if (lua_gettop(L) >= 2) {
1463 match = MAY_LJMP(luaL_checkinteger(L, 2));
1464 if (match < 0 || match >= PAT_MATCH_NUM)
1465 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1466 }
1467
1468 /* Get Lua filename and line number. */
1469 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1470 lua_getinfo(L, "Sl", &ar); /* get info about it */
1471 if (ar.currentline > 0) { /* is there info? */
1472 file = ar.short_src;
1473 line = ar.currentline;
1474 }
1475 }
1476
1477 /* fill fake sample_conv struct. */
1478 conv.kw = ""; /* unused. */
1479 conv.process = NULL; /* unused. */
1480 conv.arg_mask = 0; /* unused. */
1481 conv.val_args = NULL; /* unused. */
1482 conv.out_type = SMP_T_STR;
1483 conv.private = (void *)(long)match;
1484 switch (match) {
1485 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1486 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1487 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1488 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1489 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1490 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1491 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001492 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001493 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1494 default:
1495 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1496 }
1497
1498 /* fill fake args. */
1499 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001500 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001501 args[1].type = ARGT_STOP;
1502
1503 /* load the map. */
1504 if (!sample_load_map(args, &conv, file, line, &err)) {
1505 /* error case: we cant use luaL_error because we must
1506 * free the err variable.
1507 */
1508 luaL_where(L, 1);
1509 lua_pushfstring(L, "'new': %s.", err);
1510 lua_concat(L, 2);
1511 free(err);
1512 WILL_LJMP(lua_error(L));
1513 }
1514
1515 /* create the lua object. */
1516 lua_newtable(L);
1517 lua_pushlightuserdata(L, args[0].data.map);
1518 lua_rawseti(L, -2, 0);
1519
1520 /* Pop a class Map metatable and affect it to the userdata. */
1521 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1522 lua_setmetatable(L, -2);
1523
1524
1525 return 1;
1526}
1527
1528__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1529{
1530 struct map_descriptor *desc;
1531 struct pattern *pat;
1532 struct sample smp;
1533
1534 MAY_LJMP(check_args(L, 2, "lookup"));
1535 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001536 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001537 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001538 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001539 }
1540 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001541 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001542 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001543 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 +02001544 }
1545
1546 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001547 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001548 if (str)
1549 lua_pushstring(L, "");
1550 else
1551 lua_pushnil(L);
1552 return 1;
1553 }
1554
1555 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001556 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001557 return 1;
1558}
1559
1560__LJMP static int hlua_map_lookup(struct lua_State *L)
1561{
1562 return _hlua_map_lookup(L, 0);
1563}
1564
1565__LJMP static int hlua_map_slookup(struct lua_State *L)
1566{
1567 return _hlua_map_lookup(L, 1);
1568}
1569
1570/*
1571 *
1572 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001573 * Class Socket
1574 *
1575 *
1576 */
1577
1578__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1579{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001580 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001581}
1582
1583/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001584 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001585 * received.
1586 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001587static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001588{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001589 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001590 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
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
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001601 /* If the connection object is not available, close all the
1602 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001603 */
1604 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001605 si_shutw(si);
1606 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001607 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001608 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1609 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001610 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001611 }
1612
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001613 /* If we cant write, wakeup the pending write signals. */
1614 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001615 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001616
1617 /* If we cant read, wakeup the pending read signals. */
1618 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001619 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001620
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001621 /* if the connection is not estabkished, inform the stream that we want
1622 * to be notified whenever the connection completes.
1623 */
1624 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001625 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001626 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001627 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001628 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001629 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630
1631 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001632 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001633
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001634 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001635 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001636 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001637
1638 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001639 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001640 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001641
1642 /* Some data were injected in the buffer, notify the stream
1643 * interface.
1644 */
1645 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001646 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001647
1648 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001649 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001650 */
1651 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001652 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001653}
1654
Willy Tarreau87b09662015-04-03 00:22:06 +02001655/* This function is called when the "struct stream" is destroyed.
1656 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657 * Wake all the pending signals.
1658 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001659static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001661 struct xref *peer;
1662
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001664 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1665 if (peer)
1666 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001667
1668 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001669 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1670 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001671}
1672
1673/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001674 * uses this object. If the stream does not exists, just quit.
1675 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001676 * pending signal can rest in the read and write lists. destroy
1677 * it.
1678 */
1679__LJMP static int hlua_socket_gc(lua_State *L)
1680{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001681 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001683 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001684
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001685 MAY_LJMP(check_args(L, 1, "__gc"));
1686
1687 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001688 peer = xref_get_peer_and_lock(&socket->xref);
1689 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001690 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001691 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001693 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001694 appctx->ctx.hlua_cosocket.die = 1;
1695 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001696
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001697 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001698 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699 return 0;
1700}
1701
1702/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001703 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704 */
sada05ed3302018-05-11 11:48:18 -07001705__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001707 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001709 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001710
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001711 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001712
1713 /* Check if we run on the same thread than the xreator thread.
1714 * We cannot access to the socket if the thread is different.
1715 */
1716 if (socket->tid != tid)
1717 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1718
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001719 peer = xref_get_peer_and_lock(&socket->xref);
1720 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001721 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001722 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001723
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001724 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001725 appctx->ctx.hlua_cosocket.die = 1;
1726 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001728 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001729 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001730 return 0;
1731}
1732
sada05ed3302018-05-11 11:48:18 -07001733/* The close function calls close_helper.
1734 */
1735__LJMP static int hlua_socket_close(lua_State *L)
1736{
1737 MAY_LJMP(check_args(L, 1, "close"));
1738 return hlua_socket_close_helper(L);
1739}
1740
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001741/* This Lua function assumes that the stack contain three parameters.
1742 * 1 - USERDATA containing a struct socket
1743 * 2 - INTEGER with values of the macro defined below
1744 * If the integer is -1, we must read at most one line.
1745 * If the integer is -2, we ust read all the data until the
1746 * end of the stream.
1747 * If the integer is positive value, we must read a number of
1748 * bytes corresponding to this value.
1749 */
1750#define HLSR_READ_LINE (-1)
1751#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001752__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753{
1754 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1755 int wanted = lua_tointeger(L, 2);
1756 struct hlua *hlua = hlua_gethlua(L);
1757 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001758 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001759 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001760 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001761 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001762 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001763 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001764 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001765 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001766 struct stream_interface *si;
1767 struct stream *s;
1768 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001769 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770
1771 /* Check if this lua stack is schedulable. */
1772 if (!hlua || !hlua->task)
1773 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1774 "'frontend', 'backend' or 'task'"));
1775
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001776 /* Check if we run on the same thread than the xreator thread.
1777 * We cannot access to the socket if the thread is different.
1778 */
1779 if (socket->tid != tid)
1780 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1781
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001782 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001783 peer = xref_get_peer_and_lock(&socket->xref);
1784 if (!peer)
1785 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001786 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1787 si = appctx->owner;
1788 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001790 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001793 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794 if (nblk < 0) /* Connection close. */
1795 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001796 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001797 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001798
1799 /* remove final \r\n. */
1800 if (nblk == 1) {
1801 if (blk1[len1-1] == '\n') {
1802 len1--;
1803 skip_at_end++;
1804 if (blk1[len1-1] == '\r') {
1805 len1--;
1806 skip_at_end++;
1807 }
1808 }
1809 }
1810 else {
1811 if (blk2[len2-1] == '\n') {
1812 len2--;
1813 skip_at_end++;
1814 if (blk2[len2-1] == '\r') {
1815 len2--;
1816 skip_at_end++;
1817 }
1818 }
1819 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 }
1821
1822 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 /* Read all the available 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 }
1830
1831 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001833 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 if (nblk < 0) /* Connection close. */
1835 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001836 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837 goto connection_empty;
1838
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001839 missing_bytes = wanted - socket->b.n;
1840 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001842 len1 = missing_bytes;
1843 } if (nblk == 2 && len1 + len2 > missing_bytes)
1844 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001845 }
1846
1847 len = len1;
1848
1849 luaL_addlstring(&socket->b, blk1, len1);
1850 if (nblk == 2) {
1851 len += len2;
1852 luaL_addlstring(&socket->b, blk2, len2);
1853 }
1854
1855 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001856 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857
1858 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001859 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860
1861 /* If the pattern reclaim to read all the data
1862 * in the connection, got out.
1863 */
1864 if (wanted == HLSR_READ_ALL)
1865 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001866 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001867 goto connection_empty;
1868
1869 /* Return result. */
1870 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001871 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 return 1;
1873
1874connection_closed:
1875
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001876 xref_unlock(&socket->xref, peer);
1877
1878no_peer:
1879
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 /* If the buffer containds data. */
1881 if (socket->b.n > 0) {
1882 luaL_pushresult(&socket->b);
1883 return 1;
1884 }
1885 lua_pushnil(L);
1886 lua_pushstring(L, "connection closed.");
1887 return 2;
1888
1889connection_empty:
1890
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001891 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1892 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001894 }
1895 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001896 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001897 return 0;
1898}
1899
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001900/* This Lua function gets two parameters. The first one can be string
1901 * or a number. If the string is "*l", the user requires one line. If
1902 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903 * If the value is a number, the user require a number of bytes equal
1904 * to the value. The default value is "*l" (a line).
1905 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001906 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907 * integer takes this values:
1908 * -1 : read a line
1909 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001910 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001912 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001913 * concatenated with the read data.
1914 */
1915__LJMP static int hlua_socket_receive(struct lua_State *L)
1916{
1917 int wanted = HLSR_READ_LINE;
1918 const char *pattern;
1919 int type;
1920 char *error;
1921 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001922 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923
1924 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1925 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1926
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001927 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001929 /* Check if we run on the same thread than the xreator thread.
1930 * We cannot access to the socket if the thread is different.
1931 */
1932 if (socket->tid != tid)
1933 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1934
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001935 /* check for pattern. */
1936 if (lua_gettop(L) >= 2) {
1937 type = lua_type(L, 2);
1938 if (type == LUA_TSTRING) {
1939 pattern = lua_tostring(L, 2);
1940 if (strcmp(pattern, "*a") == 0)
1941 wanted = HLSR_READ_ALL;
1942 else if (strcmp(pattern, "*l") == 0)
1943 wanted = HLSR_READ_LINE;
1944 else {
1945 wanted = strtoll(pattern, &error, 10);
1946 if (*error != '\0')
1947 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1948 }
1949 }
1950 else if (type == LUA_TNUMBER) {
1951 wanted = lua_tointeger(L, 2);
1952 if (wanted < 0)
1953 WILL_LJMP(luaL_error(L, "Unsupported size."));
1954 }
1955 }
1956
1957 /* Set pattern. */
1958 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001959
1960 /* Check if we would replace the top by itself. */
1961 if (lua_gettop(L) != 2)
1962 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001964 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 luaL_buffinit(L, &socket->b);
1966
1967 /* Check prefix. */
1968 if (lua_gettop(L) >= 3) {
1969 if (lua_type(L, 3) != LUA_TSTRING)
1970 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1971 pattern = lua_tolstring(L, 3, &len);
1972 luaL_addlstring(&socket->b, pattern, len);
1973 }
1974
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001975 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976}
1977
1978/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001979 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001981static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982{
1983 struct hlua_socket *socket;
1984 struct hlua *hlua = hlua_gethlua(L);
1985 struct appctx *appctx;
1986 size_t buf_len;
1987 const char *buf;
1988 int len;
1989 int send_len;
1990 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001991 struct xref *peer;
1992 struct stream_interface *si;
1993 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001994
1995 /* Check if this lua stack is schedulable. */
1996 if (!hlua || !hlua->task)
1997 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1998 "'frontend', 'backend' or 'task'"));
1999
2000 /* Get object */
2001 socket = MAY_LJMP(hlua_checksocket(L, 1));
2002 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002003 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002004
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002005 /* Check if we run on the same thread than the xreator thread.
2006 * We cannot access to the socket if the thread is different.
2007 */
2008 if (socket->tid != tid)
2009 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2010
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002011 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002012 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002013 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002014 lua_pushinteger(L, -1);
2015 return 1;
2016 }
2017 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2018 si = appctx->owner;
2019 s = si_strm(si);
2020
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002021 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002022 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002023 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002024 lua_pushinteger(L, -1);
2025 return 1;
2026 }
2027
2028 /* Update the input buffer data. */
2029 buf += sent;
2030 send_len = buf_len - sent;
2031
2032 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002033 if (sent >= buf_len) {
2034 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002035 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002036 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002038 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002039 * the request buffer if its not required.
2040 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002041 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002042 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002043 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002044 }
2045
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002046 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002047 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002048 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002050 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051
2052 /* send data */
2053 if (len < send_len)
2054 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002055 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002056
2057 /* "Not enough space" (-1), "Buffer too little to contain
2058 * the data" (-2) are not expected because the available length
2059 * is tested.
2060 * Other unknown error are also not expected.
2061 */
2062 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002063 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002064 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002065
sada05ed3302018-05-11 11:48:18 -07002066 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002068 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002069 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070 return 1;
2071 }
2072
2073 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002074 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002075
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002076 s->req.rex = TICK_ETERNITY;
2077 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078
2079 /* Update length sent. */
2080 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002081 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082
2083 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002084 if (sent + len >= buf_len) {
2085 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002087 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088
2089hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002090 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2091 xref_unlock(&socket->xref, peer);
2092 WILL_LJMP(luaL_error(L, "out of memory"));
2093 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002094 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002095 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002096 return 0;
2097}
2098
2099/* This function initiate the send of data. It just check the input
2100 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002101 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 * "hlua_socket_write_yield" that can yield.
2103 *
2104 * The Lua function gets between 3 and 4 parameters. The first one is
2105 * the associated object. The second is a string buffer. The third is
2106 * a facultative integer that represents where is the buffer position
2107 * of the start of the data that can send. The first byte is the
2108 * position "1". The default value is "1". The fourth argument is a
2109 * facultative integer that represents where is the buffer position
2110 * of the end of the data that can send. The default is the last byte.
2111 */
2112static int hlua_socket_send(struct lua_State *L)
2113{
2114 int i;
2115 int j;
2116 const char *buf;
2117 size_t buf_len;
2118
2119 /* Check number of arguments. */
2120 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2121 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2122
2123 /* Get the string. */
2124 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2125
2126 /* Get and check j. */
2127 if (lua_gettop(L) == 4) {
2128 j = MAY_LJMP(luaL_checkinteger(L, 4));
2129 if (j < 0)
2130 j = buf_len + j + 1;
2131 if (j > buf_len)
2132 j = buf_len + 1;
2133 lua_pop(L, 1);
2134 }
2135 else
2136 j = buf_len;
2137
2138 /* Get and check i. */
2139 if (lua_gettop(L) == 3) {
2140 i = MAY_LJMP(luaL_checkinteger(L, 3));
2141 if (i < 0)
2142 i = buf_len + i + 1;
2143 if (i > buf_len)
2144 i = buf_len + 1;
2145 lua_pop(L, 1);
2146 } else
2147 i = 1;
2148
2149 /* Check bth i and j. */
2150 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002151 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002152 return 1;
2153 }
2154 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002155 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002156 return 1;
2157 }
2158 if (i == 0)
2159 i = 1;
2160 if (j == 0)
2161 j = 1;
2162
2163 /* Pop the string. */
2164 lua_pop(L, 1);
2165
2166 /* Update the buffer length. */
2167 buf += i - 1;
2168 buf_len = j - i + 1;
2169 lua_pushlstring(L, buf, buf_len);
2170
2171 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002172 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002174 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175}
2176
Willy Tarreau22b0a682015-06-17 19:43:49 +02002177#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2179{
2180 static char buffer[SOCKET_INFO_MAX_LEN];
2181 int ret;
2182 int len;
2183 char *p;
2184
2185 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2186 if (ret <= 0) {
2187 lua_pushnil(L);
2188 return 1;
2189 }
2190
2191 if (ret == AF_UNIX) {
2192 lua_pushstring(L, buffer+1);
2193 return 1;
2194 }
2195 else if (ret == AF_INET6) {
2196 buffer[0] = '[';
2197 len = strlen(buffer);
2198 buffer[len] = ']';
2199 len++;
2200 buffer[len] = ':';
2201 len++;
2202 p = buffer;
2203 }
2204 else if (ret == AF_INET) {
2205 p = buffer + 1;
2206 len = strlen(p);
2207 p[len] = ':';
2208 len++;
2209 }
2210 else {
2211 lua_pushnil(L);
2212 return 1;
2213 }
2214
2215 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2216 lua_pushnil(L);
2217 return 1;
2218 }
2219
2220 lua_pushstring(L, p);
2221 return 1;
2222}
2223
2224/* Returns information about the peer of the connection. */
2225__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2226{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002227 struct hlua_socket *socket;
2228 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002229 struct xref *peer;
2230 struct appctx *appctx;
2231 struct stream_interface *si;
2232 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002233 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002234
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002235 MAY_LJMP(check_args(L, 1, "getpeername"));
2236
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002237 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002238
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002239 /* Check if we run on the same thread than the xreator thread.
2240 * We cannot access to the socket if the thread is different.
2241 */
2242 if (socket->tid != tid)
2243 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2244
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002245 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002246 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002247 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002248 lua_pushnil(L);
2249 return 1;
2250 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002251 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2252 si = appctx->owner;
2253 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002254
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002255 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002257 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258 lua_pushnil(L);
2259 return 1;
2260 }
2261
Willy Tarreaua71f6422016-11-16 17:00:14 +01002262 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002264 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002265 lua_pushnil(L);
2266 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267 }
2268
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002269 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2270 xref_unlock(&socket->xref, peer);
2271 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002272}
2273
2274/* Returns information about my connection side. */
2275static int hlua_socket_getsockname(struct lua_State *L)
2276{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002277 struct hlua_socket *socket;
2278 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002279 struct appctx *appctx;
2280 struct xref *peer;
2281 struct stream_interface *si;
2282 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002283 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002284
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285 MAY_LJMP(check_args(L, 1, "getsockname"));
2286
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002287 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002289 /* Check if we run on the same thread than the xreator thread.
2290 * We cannot access to the socket if the thread is different.
2291 */
2292 if (socket->tid != tid)
2293 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2294
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002295 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002296 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002297 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 lua_pushnil(L);
2299 return 1;
2300 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002301 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2302 si = appctx->owner;
2303 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002304
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002305 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002307 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 lua_pushnil(L);
2309 return 1;
2310 }
2311
Willy Tarreaua71f6422016-11-16 17:00:14 +01002312 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002313 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002314 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002315 lua_pushnil(L);
2316 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002317 }
2318
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002319 ret = hlua_socket_info(L, &conn->addr.from);
2320 xref_unlock(&socket->xref, peer);
2321 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322}
2323
2324/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002325static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326 .obj_type = OBJ_TYPE_APPLET,
2327 .name = "<LUA_TCP>",
2328 .fct = hlua_socket_handler,
2329 .release = hlua_socket_release,
2330};
2331
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002332__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002333{
2334 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2335 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002336 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002337 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002338 struct stream_interface *si;
2339 struct stream *s;
2340
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002341 /* Check if we run on the same thread than the xreator thread.
2342 * We cannot access to the socket if the thread is different.
2343 */
2344 if (socket->tid != tid)
2345 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2346
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002347 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002349 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002350 lua_pushnil(L);
2351 lua_pushstring(L, "Can't connect");
2352 return 2;
2353 }
2354 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2355 si = appctx->owner;
2356 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002357
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002358 /* Check if we run on the same thread than the xreator thread.
2359 * We cannot access to the socket if the thread is different.
2360 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002361 if (socket->tid != tid) {
2362 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002363 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002364 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002365
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002367 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002368 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002369 lua_pushnil(L);
2370 lua_pushstring(L, "Can't connect");
2371 return 2;
2372 }
2373
Willy Tarreaue09101e2018-10-16 17:37:12 +02002374 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002375
2376 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002377 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002378 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002379 lua_pushinteger(L, 1);
2380 return 1;
2381 }
2382
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002383 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2384 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002386 }
2387 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002388 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389 return 0;
2390}
2391
2392/* This function fail or initite the connection. */
2393__LJMP static int hlua_socket_connect(struct lua_State *L)
2394{
2395 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002396 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002397 const char *ip;
2398 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002399 struct hlua *hlua;
2400 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002401 int low, high;
2402 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002403 struct xref *peer;
2404 struct stream_interface *si;
2405 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002406
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002407 if (lua_gettop(L) < 2)
2408 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002409
2410 /* Get args. */
2411 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002412
2413 /* Check if we run on the same thread than the xreator thread.
2414 * We cannot access to the socket if the thread is different.
2415 */
2416 if (socket->tid != tid)
2417 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2418
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002419 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002420 if (lua_gettop(L) >= 3) {
2421 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002422 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002423
Tim Duesterhus6edab862018-01-06 19:04:45 +01002424 /* Force the ip to end with a colon, to support IPv6 addresses
2425 * that are not enclosed within square brackets.
2426 */
2427 if (port > 0) {
2428 luaL_buffinit(L, &b);
2429 luaL_addstring(&b, ip);
2430 luaL_addchar(&b, ':');
2431 luaL_pushresult(&b);
2432 ip = lua_tolstring(L, lua_gettop(L), NULL);
2433 }
2434 }
2435
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002436 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002437 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002438 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002439 lua_pushnil(L);
2440 return 1;
2441 }
2442 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2443 si = appctx->owner;
2444 s = si_strm(si);
2445
2446 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002447 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002448 if (!conn) {
2449 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002450 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002451 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002452
Willy Tarreau3adac082015-09-26 17:51:09 +02002453 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002454 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002455
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002456 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002457 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 if (!addr) {
2459 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002460 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002461 }
2462 if (low != high) {
2463 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002464 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002465 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002466 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002467
2468 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002469 if (low == 0) {
2470 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002471 if (port == -1) {
2472 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002473 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002474 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002475 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2476 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002477 if (port == -1) {
2478 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002479 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002480 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002481 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2482 }
2483 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002484
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002485 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002486 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002487
2488 /* inform the stream that we want to be notified whenever the
2489 * connection completes.
2490 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002491 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002492 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002493 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002494
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002495 hlua->flags |= HLUA_MUST_GC;
2496
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002497 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2498 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002499 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002500 }
2501 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002502
2503 task_wakeup(s->task, TASK_WOKEN_INIT);
2504 /* Return yield waiting for connection. */
2505
Willy Tarreau9635e032018-10-16 17:52:55 +02002506 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002507
2508 return 0;
2509}
2510
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002511#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002512__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2513{
2514 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002515 struct xref *peer;
2516 struct appctx *appctx;
2517 struct stream_interface *si;
2518 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519
2520 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2521 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002522
2523 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002524 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002525 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002526 lua_pushnil(L);
2527 return 1;
2528 }
2529 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2530 si = appctx->owner;
2531 s = si_strm(si);
2532
2533 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002534 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002535 return MAY_LJMP(hlua_socket_connect(L));
2536}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002537#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538
2539__LJMP static int hlua_socket_setoption(struct lua_State *L)
2540{
2541 return 0;
2542}
2543
2544__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2545{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002546 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002547 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002548 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002549 struct xref *peer;
2550 struct appctx *appctx;
2551 struct stream_interface *si;
2552 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002553
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002554 MAY_LJMP(check_args(L, 2, "settimeout"));
2555
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002556 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002557
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002558 /* convert the timeout to millis */
2559 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002560
Thierry Fournier17a921b2018-03-08 09:59:02 +01002561 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002562 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002563 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2564
Mark Lakes56cc1252018-03-27 09:48:06 +02002565 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002566 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002567
2568 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002569 if (tmout == 0)
2570 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002571
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002572 /* Check if we run on the same thread than the xreator thread.
2573 * We cannot access to the socket if the thread is different.
2574 */
2575 if (socket->tid != tid)
2576 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2577
Mark Lakes56cc1252018-03-27 09:48:06 +02002578 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002579 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002580 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002581 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2582 WILL_LJMP(lua_error(L));
2583 return 0;
2584 }
2585 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2586 si = appctx->owner;
2587 s = si_strm(si);
2588
Cyril Bonté7bb63452018-08-17 23:51:02 +02002589 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002590 s->req.rto = tmout;
2591 s->req.wto = tmout;
2592 s->res.rto = tmout;
2593 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002594 s->req.rex = tick_add_ifset(now_ms, tmout);
2595 s->req.wex = tick_add_ifset(now_ms, tmout);
2596 s->res.rex = tick_add_ifset(now_ms, tmout);
2597 s->res.wex = tick_add_ifset(now_ms, tmout);
2598
2599 s->task->expire = tick_add_ifset(now_ms, tmout);
2600 task_queue(s->task);
2601
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002602 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002603
Thierry Fourniere9636f12018-03-08 09:54:32 +01002604 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002605 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606}
2607
2608__LJMP static int hlua_socket_new(lua_State *L)
2609{
2610 struct hlua_socket *socket;
2611 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002612 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002613 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614
2615 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002616 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002617 hlua_pusherror(L, "socket: full stack");
2618 goto out_fail_conf;
2619 }
2620
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002621 /* Create the object: obj[0] = userdata. */
2622 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002623 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002624 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002625 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002626 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002627
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002628 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002629 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002630 hlua_pusherror(L, "socket: uninitialized pools.");
2631 goto out_fail_conf;
2632 }
2633
Willy Tarreau87b09662015-04-03 00:22:06 +02002634 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2636 lua_setmetatable(L, -2);
2637
Willy Tarreaud420a972015-04-06 00:39:18 +02002638 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002639 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002640 if (!appctx) {
2641 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002642 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002643 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002644
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002645 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002646 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002647 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2648 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002649
Willy Tarreaud420a972015-04-06 00:39:18 +02002650 /* Now create a session, task and stream for this applet */
2651 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2652 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002654 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002655 }
2656
Willy Tarreau87787ac2017-08-28 16:22:54 +02002657 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002658 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002660 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002661 }
2662
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002663 /* Initialise cross reference between stream and Lua socket object. */
2664 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002665
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002666 /* Configure "right" stream interface. this "si" is used to connect
2667 * and retrieve data from the server. The connection is initialized
2668 * with the "struct server".
2669 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002670 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002671
2672 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002673 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2674 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002675
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676 return 1;
2677
Willy Tarreaud420a972015-04-06 00:39:18 +02002678 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002679 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002680 out_fail_sess:
2681 appctx_free(appctx);
2682 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002683 WILL_LJMP(lua_error(L));
2684 return 0;
2685}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002686
2687/*
2688 *
2689 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002690 * Class Channel
2691 *
2692 *
2693 */
2694
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002695/* This function is called before the Lua execution. It stores
2696 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002697 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002698static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002699{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002700 c->mode = stream->be->mode;
2701 switch (c->mode) {
2702 case PR_MODE_HTTP:
2703 c->data.http.dir = opt & SMP_OPT_DIR;
2704 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2705 c->data.http.state = stream->txn->req.msg_state;
2706 else
2707 c->data.http.state = stream->txn->rsp.msg_state;
2708 break;
2709 default:
2710 break;
2711 }
2712}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002713
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002714/* This function is called after the Lua execution. it
2715 * returns true if the parser state is consistent, otherwise,
2716 * it return false.
2717 *
2718 * In HTTP mode, the parser state must be in the same state
2719 * or greater when we exit the function. Even if we do a
2720 * control yield. This prevent to break the HTTP message
2721 * from the Lua code.
2722 */
2723static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2724{
2725 if (c->mode != stream->be->mode)
2726 return 0;
2727
2728 switch (c->mode) {
2729 case PR_MODE_HTTP:
2730 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002731 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002732 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2733 return stream->txn->req.msg_state >= c->data.http.state;
2734 else
2735 return stream->txn->rsp.msg_state >= c->data.http.state;
2736 default:
2737 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002738 }
2739 return 1;
2740}
2741
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002742/* Returns the struct hlua_channel join to the class channel in the
2743 * stack entry "ud" or throws an argument error.
2744 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002745__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002747 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002748}
2749
Willy Tarreau47860ed2015-03-10 14:07:50 +01002750/* Pushes the channel onto the top of the stack. If the stask does not have a
2751 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002753static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002754{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002755 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002756 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002757 return 0;
2758
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002759 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002760 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002761 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002762
2763 /* Pop a class sesison metatable and affect it to the userdata. */
2764 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2765 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002766 return 1;
2767}
2768
2769/* Duplicate all the data present in the input channel and put it
2770 * in a string LUA variables. Returns -1 and push a nil value in
2771 * the stack if the channel is closed and all the data are consumed,
2772 * returns 0 if no data are available, otherwise it returns the length
2773 * of the builded string.
2774 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002775static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776{
2777 char *blk1;
2778 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002779 size_t len1;
2780 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002781 int ret;
2782 luaL_Buffer b;
2783
Willy Tarreau06d80a92017-10-19 14:32:15 +02002784 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002785 if (unlikely(ret == 0))
2786 return 0;
2787
2788 if (unlikely(ret < 0)) {
2789 lua_pushnil(L);
2790 return -1;
2791 }
2792
2793 luaL_buffinit(L, &b);
2794 luaL_addlstring(&b, blk1, len1);
2795 if (unlikely(ret == 2))
2796 luaL_addlstring(&b, blk2, len2);
2797 luaL_pushresult(&b);
2798
2799 if (unlikely(ret == 2))
2800 return len1 + len2;
2801 return len1;
2802}
2803
2804/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2805 * a yield. This function keep the data in the buffer.
2806 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002807__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002809 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002810
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002811 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2812
Christopher Faulet3f829a42018-12-13 21:56:45 +01002813 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2814 WILL_LJMP(lua_error(L));
2815
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002817 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002818 return 1;
2819}
2820
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002821/* Check arguments for the function "hlua_channel_dup_yield". */
2822__LJMP static int hlua_channel_dup(lua_State *L)
2823{
2824 MAY_LJMP(check_args(L, 1, "dup"));
2825 MAY_LJMP(hlua_checkchannel(L, 1));
2826 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2827}
2828
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002829/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2830 * a yield. This function consumes the data in the buffer. It returns
2831 * a string containing the data or a nil pointer if no data are available
2832 * and the channel is closed.
2833 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002834__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002835{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002836 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002837 int ret;
2838
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002839 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002840
Christopher Faulet3f829a42018-12-13 21:56:45 +01002841 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2842 WILL_LJMP(lua_error(L));
2843
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002844 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002846 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002847
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002848 if (unlikely(ret == -1))
2849 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002850
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002851 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852 return 1;
2853}
2854
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002855/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002856__LJMP static int hlua_channel_get(lua_State *L)
2857{
2858 MAY_LJMP(check_args(L, 1, "get"));
2859 MAY_LJMP(hlua_checkchannel(L, 1));
2860 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2861}
2862
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863/* This functions consumes and returns one line. If the channel is closed,
2864 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002865 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002866 * value.
2867 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002868__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002869{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002870 char *blk1;
2871 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002872 size_t len1;
2873 size_t len2;
2874 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002875 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002876 int ret;
2877 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002878
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002879 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2880
Christopher Faulet3f829a42018-12-13 21:56:45 +01002881 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2882 WILL_LJMP(lua_error(L));
2883
Willy Tarreau06d80a92017-10-19 14:32:15 +02002884 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002886 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002887
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002888 if (ret == -1) {
2889 lua_pushnil(L);
2890 return 1;
2891 }
2892
2893 luaL_buffinit(L, &b);
2894 luaL_addlstring(&b, blk1, len1);
2895 len = len1;
2896 if (unlikely(ret == 2)) {
2897 luaL_addlstring(&b, blk2, len2);
2898 len += len2;
2899 }
2900 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002901 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002902 return 1;
2903}
2904
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002905/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002906__LJMP static int hlua_channel_getline(lua_State *L)
2907{
2908 MAY_LJMP(check_args(L, 1, "getline"));
2909 MAY_LJMP(hlua_checkchannel(L, 1));
2910 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2911}
2912
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002913/* This function takes a string as input, and append it at the
2914 * input side of channel. If the data is too big, but a space
2915 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002916 * yields. If the data is bigger than the buffer, or if the
2917 * channel is closed, it returns -1. Otherwise, it returns the
2918 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002920__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002921{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002922 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002923 size_t len;
2924 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2925 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2926 int ret;
2927 int max;
2928
Christopher Faulet3f829a42018-12-13 21:56:45 +01002929 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2930 WILL_LJMP(lua_error(L));
2931
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002932 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002933 * the request buffer if its not required.
2934 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002935 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002936 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002937 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002938 }
2939
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002940 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941 if (max > len - l)
2942 max = len - l;
2943
Willy Tarreau06d80a92017-10-19 14:32:15 +02002944 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945 if (ret == -2 || ret == -3) {
2946 lua_pushinteger(L, -1);
2947 return 1;
2948 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002949 if (ret == -1) {
2950 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002951 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002952 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002953 l += ret;
2954 lua_pop(L, 1);
2955 lua_pushinteger(L, l);
2956
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002957 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002958 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002959 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002960 * in this case, we cannot add more data, so we cannot yield,
2961 * we return the amount of copyied data.
2962 */
2963 return 1;
2964 }
2965 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002966 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967 return 1;
2968}
2969
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002970/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2971 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002972 * buffer size is too little for the data.
2973 */
2974__LJMP static int hlua_channel_append(lua_State *L)
2975{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002976 size_t len;
2977
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002978 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002979 MAY_LJMP(hlua_checkchannel(L, 1));
2980 MAY_LJMP(luaL_checklstring(L, 2, &len));
2981 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002982 lua_pushinteger(L, 0);
2983
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002984 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002985}
2986
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002987/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002988 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002989 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 * or -1 if the channel is closed or if the buffer size is too
2991 * little for the data.
2992 */
2993__LJMP static int hlua_channel_set(lua_State *L)
2994{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002995 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002996
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002997 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002998 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002999 lua_pushinteger(L, 0);
3000
Christopher Faulet3f829a42018-12-13 21:56:45 +01003001 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3002 WILL_LJMP(lua_error(L));
3003
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003004 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003005
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003006 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003007}
3008
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003009/* Append data in the output side of the buffer. This data is immediately
3010 * sent. The function returns the amount of data written. If the buffer
3011 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003012 * if the channel is closed.
3013 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003014__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003016 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003017 size_t len;
3018 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3019 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3020 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003021 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022
Christopher Faulet3f829a42018-12-13 21:56:45 +01003023 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3024 WILL_LJMP(lua_error(L));
3025
Willy Tarreau47860ed2015-03-10 14:07:50 +01003026 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 lua_pushinteger(L, -1);
3028 return 1;
3029 }
3030
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003031 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003032 * the request buffer if its not required.
3033 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003034 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003035 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003036 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003037 }
3038
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003039 /* The written data will be immediately sent, so we can check
3040 * the available space without taking in account the reserve.
3041 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003042 * data, because the buffer will be flushed.
3043 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003044 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003045
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003046 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003047 * in this case, we cannot add more data, so we cannot yield,
3048 * we return the amount of copyied data.
3049 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003050 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003051 return 1;
3052
3053 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054 if (max > len - l)
3055 max = len - l;
3056
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003057 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003058 * detects a non contiguous buffer and realign it.
3059 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003060 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003061 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003062
3063 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003064 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003065
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003066 /* buffer replace considers that the input part is filled.
3067 * so, I must forward these new data in the output part.
3068 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003069 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003070
3071 l += max;
3072 lua_pop(L, 1);
3073 lua_pushinteger(L, l);
3074
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003075 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003076 * in this case, we cannot add more data, so we cannot yield,
3077 * we return the amount of copyied data.
3078 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003079 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003080 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003081 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003082
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003083 if (l < len) {
3084 /* If we are waiting for space in the response buffer, we
3085 * must set the flag WAKERESWR. This flag required the task
3086 * wake up if any activity is detected on the response buffer.
3087 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003088 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003089 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003090 else
3091 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003092 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003093 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003094
3095 return 1;
3096}
3097
3098/* Just a wraper of "_hlua_channel_send". This wrapper permits
3099 * yield the LUA process, and resume it without checking the
3100 * input arguments.
3101 */
3102__LJMP static int hlua_channel_send(lua_State *L)
3103{
3104 MAY_LJMP(check_args(L, 2, "send"));
3105 lua_pushinteger(L, 0);
3106
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003107 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003108}
3109
3110/* This function forward and amount of butes. The data pass from
3111 * the input side of the buffer to the output side, and can be
3112 * forwarded. This function never fails.
3113 *
3114 * The Lua function takes an amount of bytes to be forwarded in
3115 * imput. It returns the number of bytes forwarded.
3116 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003117__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003118{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003119 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003120 int len;
3121 int l;
3122 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003123 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003124
3125 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003126
3127 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3128 WILL_LJMP(lua_error(L));
3129
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130 len = MAY_LJMP(luaL_checkinteger(L, 2));
3131 l = MAY_LJMP(luaL_checkinteger(L, -1));
3132
3133 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003134 if (max > ci_data(chn))
3135 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003136 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003137 l += max;
3138
3139 lua_pop(L, 1);
3140 lua_pushinteger(L, l);
3141
3142 /* Check if it miss bytes to forward. */
3143 if (l < len) {
3144 /* The the input channel or the output channel are closed, we
3145 * must return the amount of data forwarded.
3146 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003147 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003148 return 1;
3149
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003150 /* If we are waiting for space data in the response buffer, we
3151 * must set the flag WAKERESWR. This flag required the task
3152 * wake up if any activity is detected on the response buffer.
3153 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003154 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003155 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003156 else
3157 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003158
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003159 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003160 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003161 }
3162
3163 return 1;
3164}
3165
3166/* Just check the input and prepare the stack for the previous
3167 * function "hlua_channel_forward_yield"
3168 */
3169__LJMP static int hlua_channel_forward(lua_State *L)
3170{
3171 MAY_LJMP(check_args(L, 2, "forward"));
3172 MAY_LJMP(hlua_checkchannel(L, 1));
3173 MAY_LJMP(luaL_checkinteger(L, 2));
3174
3175 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003176 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003177}
3178
3179/* Just returns the number of bytes available in the input
3180 * side of the buffer. This function never fails.
3181 */
3182__LJMP static int hlua_channel_get_in_len(lua_State *L)
3183{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003184 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003185
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003186 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003187 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003188 if (IS_HTX_STRM(chn_strm(chn))) {
3189 struct htx *htx = htxbuf(&chn->buf);
3190 lua_pushinteger(L, htx->data - co_data(chn));
3191 }
3192 else
3193 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003194 return 1;
3195}
3196
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003197/* Returns true if the channel is full. */
3198__LJMP static int hlua_channel_is_full(lua_State *L)
3199{
3200 struct channel *chn;
3201 int rem;
3202
3203 MAY_LJMP(check_args(L, 1, "is_full"));
3204 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3205
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003206 if (IS_HTX_STRM(chn_strm(chn))) {
3207 struct htx *htx = htxbuf(&chn->buf);
3208
3209 rem = htx_free_data_space(htx);
3210 }
3211 else
3212 rem = b_room(&chn->buf);
3213
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003214 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3215
3216 lua_pushboolean(L, rem <= 0);
3217 return 1;
3218}
3219
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003220/* Just returns the number of bytes available in the output
3221 * side of the buffer. This function never fails.
3222 */
3223__LJMP static int hlua_channel_get_out_len(lua_State *L)
3224{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003225 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003226
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003227 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003228 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003229 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003230 return 1;
3231}
3232
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003233/*
3234 *
3235 *
3236 * Class Fetches
3237 *
3238 *
3239 */
3240
3241/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003242 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003243 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003244__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003245{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003246 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003247}
3248
3249/* This function creates and push in the stack a fetch object according
3250 * with a current TXN.
3251 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003252static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003253{
Willy Tarreau7073c472015-04-06 11:15:40 +02003254 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003255
3256 /* Check stack size. */
3257 if (!lua_checkstack(L, 3))
3258 return 0;
3259
3260 /* Create the object: obj[0] = userdata.
3261 * Note that the base of the Fetches object is the
3262 * transaction object.
3263 */
3264 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003265 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003266 lua_rawseti(L, -2, 0);
3267
Willy Tarreau7073c472015-04-06 11:15:40 +02003268 hsmp->s = txn->s;
3269 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003270 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003271 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003272
3273 /* Pop a class sesison metatable and affect it to the userdata. */
3274 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3275 lua_setmetatable(L, -2);
3276
3277 return 1;
3278}
3279
3280/* This function is an LUA binding. It is called with each sample-fetch.
3281 * It uses closure argument to store the associated sample-fetch. It
3282 * returns only one argument or throws an error. An error is thrown
3283 * only if an error is encountered during the argument parsing. If
3284 * the "sample-fetch" function fails, nil is returned.
3285 */
3286__LJMP static int hlua_run_sample_fetch(lua_State *L)
3287{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003288 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003289 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003290 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003291 int i;
3292 struct sample smp;
3293
3294 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003295 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003296
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003297 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003298 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003299
Thierry FOURNIERca988662015-12-20 18:43:03 +01003300 /* Check execution authorization. */
3301 if (f->use & SMP_USE_HTTP_ANY &&
3302 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3303 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3304 "is not available in Lua services", f->kw);
3305 WILL_LJMP(lua_error(L));
3306 }
3307
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003308 /* Get extra arguments. */
3309 for (i = 0; i < lua_gettop(L) - 1; i++) {
3310 if (i >= ARGM_NBARGS)
3311 break;
3312 hlua_lua2arg(L, i + 2, &args[i]);
3313 }
3314 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003315 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003316
3317 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003318 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003319
3320 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003321 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003322 lua_pushfstring(L, "error in arguments");
3323 WILL_LJMP(lua_error(L));
3324 }
3325
3326 /* Initialise the sample. */
3327 memset(&smp, 0, sizeof(smp));
3328
3329 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003330 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003331 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003332 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003333 lua_pushstring(L, "");
3334 else
3335 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003336 return 1;
3337 }
3338
3339 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003340 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003341 hlua_smp2lua_str(L, &smp);
3342 else
3343 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003344 return 1;
3345}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003346
3347/*
3348 *
3349 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350 * Class Converters
3351 *
3352 *
3353 */
3354
3355/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003356 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003357 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003358__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003359{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003360 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003361}
3362
3363/* This function creates and push in the stack a Converters object
3364 * according with a current TXN.
3365 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003366static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003367{
Willy Tarreau7073c472015-04-06 11:15:40 +02003368 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003369
3370 /* Check stack size. */
3371 if (!lua_checkstack(L, 3))
3372 return 0;
3373
3374 /* Create the object: obj[0] = userdata.
3375 * Note that the base of the Converters object is the
3376 * same than the TXN object.
3377 */
3378 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003379 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003380 lua_rawseti(L, -2, 0);
3381
Willy Tarreau7073c472015-04-06 11:15:40 +02003382 hsmp->s = txn->s;
3383 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003384 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003385 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003386
Willy Tarreau87b09662015-04-03 00:22:06 +02003387 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003388 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3389 lua_setmetatable(L, -2);
3390
3391 return 1;
3392}
3393
3394/* This function is an LUA binding. It is called with each converter.
3395 * It uses closure argument to store the associated converter. It
3396 * returns only one argument or throws an error. An error is thrown
3397 * only if an error is encountered during the argument parsing. If
3398 * the converter function function fails, nil is returned.
3399 */
3400__LJMP static int hlua_run_sample_conv(lua_State *L)
3401{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003402 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003403 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003404 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003405 int i;
3406 struct sample smp;
3407
3408 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003409 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003410
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003411 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003412 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003413
3414 /* Get extra arguments. */
3415 for (i = 0; i < lua_gettop(L) - 2; i++) {
3416 if (i >= ARGM_NBARGS)
3417 break;
3418 hlua_lua2arg(L, i + 3, &args[i]);
3419 }
3420 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003421 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003422
3423 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003424 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003425
3426 /* Run the special args checker. */
3427 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3428 hlua_pusherror(L, "error in arguments");
3429 WILL_LJMP(lua_error(L));
3430 }
3431
3432 /* Initialise the sample. */
3433 if (!hlua_lua2smp(L, 2, &smp)) {
3434 hlua_pusherror(L, "error in the input argument");
3435 WILL_LJMP(lua_error(L));
3436 }
3437
Willy Tarreau1777ea62016-03-10 16:15:46 +01003438 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3439
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003440 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003441 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003442 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003443 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003444 WILL_LJMP(lua_error(L));
3445 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003446 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3447 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003448 hlua_pusherror(L, "error during the input argument casting");
3449 WILL_LJMP(lua_error(L));
3450 }
3451
3452 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003453 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003454 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003455 lua_pushstring(L, "");
3456 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003457 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003458 return 1;
3459 }
3460
3461 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003462 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003463 hlua_smp2lua_str(L, &smp);
3464 else
3465 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003466 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003467}
3468
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003469/*
3470 *
3471 *
3472 * Class AppletTCP
3473 *
3474 *
3475 */
3476
3477/* Returns a struct hlua_txn if the stack entry "ud" is
3478 * a class stream, otherwise it throws an error.
3479 */
3480__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3481{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003482 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003483}
3484
3485/* This function creates and push in the stack an Applet object
3486 * according with a current TXN.
3487 */
3488static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3489{
3490 struct hlua_appctx *appctx;
3491 struct stream_interface *si = ctx->owner;
3492 struct stream *s = si_strm(si);
3493 struct proxy *p = s->be;
3494
3495 /* Check stack size. */
3496 if (!lua_checkstack(L, 3))
3497 return 0;
3498
3499 /* Create the object: obj[0] = userdata.
3500 * Note that the base of the Converters object is the
3501 * same than the TXN object.
3502 */
3503 lua_newtable(L);
3504 appctx = lua_newuserdata(L, sizeof(*appctx));
3505 lua_rawseti(L, -2, 0);
3506 appctx->appctx = ctx;
3507 appctx->htxn.s = s;
3508 appctx->htxn.p = p;
3509
3510 /* Create the "f" field that contains a list of fetches. */
3511 lua_pushstring(L, "f");
3512 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3513 return 0;
3514 lua_settable(L, -3);
3515
3516 /* Create the "sf" field that contains a list of stringsafe fetches. */
3517 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003518 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003519 return 0;
3520 lua_settable(L, -3);
3521
3522 /* Create the "c" field that contains a list of converters. */
3523 lua_pushstring(L, "c");
3524 if (!hlua_converters_new(L, &appctx->htxn, 0))
3525 return 0;
3526 lua_settable(L, -3);
3527
3528 /* Create the "sc" field that contains a list of stringsafe converters. */
3529 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003530 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003531 return 0;
3532 lua_settable(L, -3);
3533
3534 /* Pop a class stream metatable and affect it to the table. */
3535 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3536 lua_setmetatable(L, -2);
3537
3538 return 1;
3539}
3540
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003541__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3542{
3543 struct hlua_appctx *appctx;
3544 struct stream *s;
3545 const char *name;
3546 size_t len;
3547 struct sample smp;
3548
3549 MAY_LJMP(check_args(L, 3, "set_var"));
3550
3551 /* It is useles to retrieve the stream, but this function
3552 * runs only in a stream context.
3553 */
3554 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3555 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3556 s = appctx->htxn.s;
3557
3558 /* Converts the third argument in a sample. */
3559 hlua_lua2smp(L, 3, &smp);
3560
3561 /* Store the sample in a variable. */
3562 smp_set_owner(&smp, s->be, s->sess, s, 0);
3563 vars_set_by_name(name, len, &smp);
3564 return 0;
3565}
3566
3567__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3568{
3569 struct hlua_appctx *appctx;
3570 struct stream *s;
3571 const char *name;
3572 size_t len;
3573 struct sample smp;
3574
3575 MAY_LJMP(check_args(L, 2, "unset_var"));
3576
3577 /* It is useles to retrieve the stream, but this function
3578 * runs only in a stream context.
3579 */
3580 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3581 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3582 s = appctx->htxn.s;
3583
3584 /* Unset the variable. */
3585 smp_set_owner(&smp, s->be, s->sess, s, 0);
3586 vars_unset_by_name(name, len, &smp);
3587 return 0;
3588}
3589
3590__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3591{
3592 struct hlua_appctx *appctx;
3593 struct stream *s;
3594 const char *name;
3595 size_t len;
3596 struct sample smp;
3597
3598 MAY_LJMP(check_args(L, 2, "get_var"));
3599
3600 /* It is useles to retrieve the stream, but this function
3601 * runs only in a stream context.
3602 */
3603 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3604 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3605 s = appctx->htxn.s;
3606
3607 smp_set_owner(&smp, s->be, s->sess, s, 0);
3608 if (!vars_get_by_name(name, len, &smp)) {
3609 lua_pushnil(L);
3610 return 1;
3611 }
3612
3613 return hlua_smp2lua(L, &smp);
3614}
3615
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003616__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3617{
3618 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3619 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003620 struct hlua *hlua;
3621
3622 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003623 if (!s->hlua)
3624 return 0;
3625 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003626
3627 MAY_LJMP(check_args(L, 2, "set_priv"));
3628
3629 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003630 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003631
3632 /* Get and store new value. */
3633 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3634 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3635
3636 return 0;
3637}
3638
3639__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3640{
3641 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3642 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003643 struct hlua *hlua;
3644
3645 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003646 if (!s->hlua) {
3647 lua_pushnil(L);
3648 return 1;
3649 }
3650 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003651
3652 /* Push configuration index in the stack. */
3653 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3654
3655 return 1;
3656}
3657
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003658/* If expected data not yet available, it returns a yield. This function
3659 * consumes the data in the buffer. It returns a string containing the
3660 * data. This string can be empty.
3661 */
3662__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3663{
3664 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3665 struct stream_interface *si = appctx->appctx->owner;
3666 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003667 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003668 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003669 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003670 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003671
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003672 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003673 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003674
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003675 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003676 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003677 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003678 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003679 }
3680
3681 /* End of data: commit the total strings and return. */
3682 if (ret < 0) {
3683 luaL_pushresult(&appctx->b);
3684 return 1;
3685 }
3686
3687 /* Ensure that the block 2 length is usable. */
3688 if (ret == 1)
3689 len2 = 0;
3690
3691 /* dont check the max length read and dont check. */
3692 luaL_addlstring(&appctx->b, blk1, len1);
3693 luaL_addlstring(&appctx->b, blk2, len2);
3694
3695 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003696 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003697 luaL_pushresult(&appctx->b);
3698 return 1;
3699}
3700
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003701/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003702__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3703{
3704 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3705
3706 /* Initialise the string catenation. */
3707 luaL_buffinit(L, &appctx->b);
3708
3709 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3710}
3711
3712/* If expected data not yet available, it returns a yield. This function
3713 * consumes the data in the buffer. It returns a string containing the
3714 * data. This string can be empty.
3715 */
3716__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3717{
3718 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3719 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003720 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003721 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003722 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003723 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003724 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003725 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003726
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003727 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003728 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003729
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003730 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003731 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003732 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003733 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003734 }
3735
3736 /* End of data: commit the total strings and return. */
3737 if (ret < 0) {
3738 luaL_pushresult(&appctx->b);
3739 return 1;
3740 }
3741
3742 /* Ensure that the block 2 length is usable. */
3743 if (ret == 1)
3744 len2 = 0;
3745
3746 if (len == -1) {
3747
3748 /* If len == -1, catenate all the data avalaile and
3749 * yield because we want to get all the data until
3750 * the end of data stream.
3751 */
3752 luaL_addlstring(&appctx->b, blk1, len1);
3753 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003754 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003755 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003756 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003757
3758 } else {
3759
3760 /* Copy the fisrt block caping to the length required. */
3761 if (len1 > len)
3762 len1 = len;
3763 luaL_addlstring(&appctx->b, blk1, len1);
3764 len -= len1;
3765
3766 /* Copy the second block. */
3767 if (len2 > len)
3768 len2 = len;
3769 luaL_addlstring(&appctx->b, blk2, len2);
3770 len -= len2;
3771
3772 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003773 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003774
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003775 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003776 if (len > 0) {
3777 lua_pushinteger(L, len);
3778 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003779 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003780 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003781 }
3782
3783 /* return the result. */
3784 luaL_pushresult(&appctx->b);
3785 return 1;
3786 }
3787
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003788 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003789 hlua_pusherror(L, "Lua: internal error");
3790 WILL_LJMP(lua_error(L));
3791 return 0;
3792}
3793
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003794/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003795__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3796{
3797 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3798 int len = -1;
3799
3800 if (lua_gettop(L) > 2)
3801 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3802 if (lua_gettop(L) >= 2) {
3803 len = MAY_LJMP(luaL_checkinteger(L, 2));
3804 lua_pop(L, 1);
3805 }
3806
3807 /* Confirm or set the required length */
3808 lua_pushinteger(L, len);
3809
3810 /* Initialise the string catenation. */
3811 luaL_buffinit(L, &appctx->b);
3812
3813 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3814}
3815
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003816/* Append data in the output side of the buffer. This data is immediately
3817 * sent. The function returns the amount of data written. If the buffer
3818 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003819 * if the channel is closed.
3820 */
3821__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3822{
3823 size_t len;
3824 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3825 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3826 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3827 struct stream_interface *si = appctx->appctx->owner;
3828 struct channel *chn = si_ic(si);
3829 int max;
3830
3831 /* Get the max amount of data which can write as input in the channel. */
3832 max = channel_recv_max(chn);
3833 if (max > (len - l))
3834 max = len - l;
3835
3836 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003837 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003838
3839 /* update counters. */
3840 l += max;
3841 lua_pop(L, 1);
3842 lua_pushinteger(L, l);
3843
3844 /* If some data is not send, declares the situation to the
3845 * applet, and returns a yield.
3846 */
3847 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003848 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003849 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003850 }
3851
3852 return 1;
3853}
3854
3855/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3856 * yield the LUA process, and resume it without checking the
3857 * input arguments.
3858 */
3859__LJMP static int hlua_applet_tcp_send(lua_State *L)
3860{
3861 MAY_LJMP(check_args(L, 2, "send"));
3862 lua_pushinteger(L, 0);
3863
3864 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3865}
3866
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003867/*
3868 *
3869 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003870 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003871 *
3872 *
3873 */
3874
3875/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003876 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003877 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003879{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003880 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003881}
3882
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003883/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003884 * according with a current TXN.
3885 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003886static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003887{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003888 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003889 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003890 struct stream_interface *si = ctx->owner;
3891 struct stream *s = si_strm(si);
3892 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003893 struct htx *htx;
3894 struct htx_blk *blk;
3895 struct htx_sl *sl;
3896 struct ist path;
3897 unsigned long long len = 0;
3898 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003899
3900 /* Check stack size. */
3901 if (!lua_checkstack(L, 3))
3902 return 0;
3903
3904 /* Create the object: obj[0] = userdata.
3905 * Note that the base of the Converters object is the
3906 * same than the TXN object.
3907 */
3908 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003909 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003910 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003911 appctx->appctx = ctx;
3912 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003913 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003914 appctx->htxn.s = s;
3915 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003916
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003917 /* Create the "f" field that contains a list of fetches. */
3918 lua_pushstring(L, "f");
3919 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3920 return 0;
3921 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003922
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923 /* Create the "sf" field that contains a list of stringsafe fetches. */
3924 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003925 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003926 return 0;
3927 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003928
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003929 /* Create the "c" field that contains a list of converters. */
3930 lua_pushstring(L, "c");
3931 if (!hlua_converters_new(L, &appctx->htxn, 0))
3932 return 0;
3933 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003934
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003935 /* Create the "sc" field that contains a list of stringsafe converters. */
3936 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003937 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003938 return 0;
3939 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003940
Christopher Fauleta2097962019-07-15 16:25:33 +02003941 htx = htxbuf(&s->req.buf);
3942 blk = htx_get_first_blk(htx);
3943 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
3944 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003945
Christopher Fauleta2097962019-07-15 16:25:33 +02003946 /* Stores the request method. */
3947 lua_pushstring(L, "method");
3948 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3949 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003950
Christopher Fauleta2097962019-07-15 16:25:33 +02003951 /* Stores the http version. */
3952 lua_pushstring(L, "version");
3953 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3954 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003955
Christopher Fauleta2097962019-07-15 16:25:33 +02003956 /* creates an array of headers. hlua_http_get_headers() crates and push
3957 * the array on the top of the stack.
3958 */
3959 lua_pushstring(L, "headers");
3960 htxn.s = s;
3961 htxn.p = px;
3962 htxn.dir = SMP_OPT_DIR_REQ;
3963 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3964 return 0;
3965 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003966
Christopher Fauleta2097962019-07-15 16:25:33 +02003967 path = http_get_path(htx_sl_req_uri(sl));
3968 if (path.ptr) {
3969 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003970
Christopher Fauleta2097962019-07-15 16:25:33 +02003971 p = path.ptr;
3972 end = path.ptr + path.len;
3973 q = p;
3974 while (q < end && *q != '?')
3975 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003976
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003977 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003978 lua_pushstring(L, "path");
3979 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003980 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003981
Christopher Fauleta2097962019-07-15 16:25:33 +02003982 /* Stores the query string. */
3983 lua_pushstring(L, "qs");
3984 if (*q == '?')
3985 q++;
3986 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003987 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003988 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003989
Christopher Fauleta2097962019-07-15 16:25:33 +02003990 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3991 struct htx_blk *blk = htx_get_blk(htx, pos);
3992 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003993
Christopher Fauleta2097962019-07-15 16:25:33 +02003994 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3995 break;
3996 if (type == HTX_BLK_DATA)
3997 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003998 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003999 if (htx->extra != ULLONG_MAX)
4000 len += htx->extra;
4001
4002 /* Stores the request path. */
4003 lua_pushstring(L, "length");
4004 lua_pushinteger(L, len);
4005 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004006
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004007 /* Create an empty array of HTTP request headers. */
4008 lua_pushstring(L, "response");
4009 lua_newtable(L);
4010 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004011
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004012 /* Pop a class stream metatable and affect it to the table. */
4013 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4014 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004015
4016 return 1;
4017}
4018
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004019__LJMP static int hlua_applet_http_set_var(lua_State *L)
4020{
4021 struct hlua_appctx *appctx;
4022 struct stream *s;
4023 const char *name;
4024 size_t len;
4025 struct sample smp;
4026
4027 MAY_LJMP(check_args(L, 3, "set_var"));
4028
4029 /* It is useles to retrieve the stream, but this function
4030 * runs only in a stream context.
4031 */
4032 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4033 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4034 s = appctx->htxn.s;
4035
4036 /* Converts the third argument in a sample. */
4037 hlua_lua2smp(L, 3, &smp);
4038
4039 /* Store the sample in a variable. */
4040 smp_set_owner(&smp, s->be, s->sess, s, 0);
4041 vars_set_by_name(name, len, &smp);
4042 return 0;
4043}
4044
4045__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4046{
4047 struct hlua_appctx *appctx;
4048 struct stream *s;
4049 const char *name;
4050 size_t len;
4051 struct sample smp;
4052
4053 MAY_LJMP(check_args(L, 2, "unset_var"));
4054
4055 /* It is useles to retrieve the stream, but this function
4056 * runs only in a stream context.
4057 */
4058 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4059 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4060 s = appctx->htxn.s;
4061
4062 /* Unset the variable. */
4063 smp_set_owner(&smp, s->be, s->sess, s, 0);
4064 vars_unset_by_name(name, len, &smp);
4065 return 0;
4066}
4067
4068__LJMP static int hlua_applet_http_get_var(lua_State *L)
4069{
4070 struct hlua_appctx *appctx;
4071 struct stream *s;
4072 const char *name;
4073 size_t len;
4074 struct sample smp;
4075
4076 MAY_LJMP(check_args(L, 2, "get_var"));
4077
4078 /* It is useles to retrieve the stream, but this function
4079 * runs only in a stream context.
4080 */
4081 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4082 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4083 s = appctx->htxn.s;
4084
4085 smp_set_owner(&smp, s->be, s->sess, s, 0);
4086 if (!vars_get_by_name(name, len, &smp)) {
4087 lua_pushnil(L);
4088 return 1;
4089 }
4090
4091 return hlua_smp2lua(L, &smp);
4092}
4093
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004094__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4095{
4096 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4097 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004098 struct hlua *hlua;
4099
4100 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004101 if (!s->hlua)
4102 return 0;
4103 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004104
4105 MAY_LJMP(check_args(L, 2, "set_priv"));
4106
4107 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004108 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004109
4110 /* Get and store new value. */
4111 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4112 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4113
4114 return 0;
4115}
4116
4117__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4118{
4119 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4120 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004121 struct hlua *hlua;
4122
4123 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004124 if (!s->hlua) {
4125 lua_pushnil(L);
4126 return 1;
4127 }
4128 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004129
4130 /* Push configuration index in the stack. */
4131 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4132
4133 return 1;
4134}
4135
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004136/* If expected data not yet available, it returns a yield. This function
4137 * consumes the data in the buffer. It returns a string containing the
4138 * data. This string can be empty.
4139 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004140__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004141{
4142 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4143 struct stream_interface *si = appctx->appctx->owner;
4144 struct channel *req = si_oc(si);
4145 struct htx *htx;
4146 struct htx_blk *blk;
4147 size_t count;
4148 int stop = 0;
4149
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004150 htx = htx_from_buf(&req->buf);
4151 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004152 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004153
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004154 while (count && !stop && blk) {
4155 enum htx_blk_type type = htx_get_blk_type(blk);
4156 uint32_t sz = htx_get_blksz(blk);
4157 struct ist v;
4158 uint32_t vlen;
4159 char *nl;
4160
Christopher Faulete461e342018-12-18 16:43:35 +01004161 if (type == HTX_BLK_EOM) {
4162 stop = 1;
4163 break;
4164 }
4165
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004166 vlen = sz;
4167 if (vlen > count) {
4168 if (type != HTX_BLK_DATA)
4169 break;
4170 vlen = count;
4171 }
4172
4173 switch (type) {
4174 case HTX_BLK_UNUSED:
4175 break;
4176
4177 case HTX_BLK_DATA:
4178 v = htx_get_blk_value(htx, blk);
4179 v.len = vlen;
4180 nl = istchr(v, '\n');
4181 if (nl != NULL) {
4182 stop = 1;
4183 vlen = nl - v.ptr + 1;
4184 }
4185 luaL_addlstring(&appctx->b, v.ptr, vlen);
4186 break;
4187
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004188 case HTX_BLK_TLR:
4189 case HTX_BLK_EOM:
4190 stop = 1;
4191 break;
4192
4193 default:
4194 break;
4195 }
4196
4197 co_set_data(req, co_data(req) - vlen);
4198 count -= vlen;
4199 if (sz == vlen)
4200 blk = htx_remove_blk(htx, blk);
4201 else {
4202 htx_cut_data_blk(htx, blk, vlen);
4203 break;
4204 }
4205 }
4206
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004207 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004208 if (!stop) {
4209 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004210 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004211 }
4212
4213 /* return the result. */
4214 luaL_pushresult(&appctx->b);
4215 return 1;
4216}
4217
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004218
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004219/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004220__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004221{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004222 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004223
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004224 /* Initialise the string catenation. */
4225 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004226
Christopher Fauleta2097962019-07-15 16:25:33 +02004227 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004228}
4229
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004230/* If expected data not yet available, it returns a yield. This function
4231 * consumes the data in the buffer. It returns a string containing the
4232 * data. This string can be empty.
4233 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004234__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004235{
4236 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4237 struct stream_interface *si = appctx->appctx->owner;
4238 struct channel *req = si_oc(si);
4239 struct htx *htx;
4240 struct htx_blk *blk;
4241 size_t count;
4242 int len;
4243
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004244 htx = htx_from_buf(&req->buf);
4245 len = MAY_LJMP(luaL_checkinteger(L, 2));
4246 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004247 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004248 while (count && len && blk) {
4249 enum htx_blk_type type = htx_get_blk_type(blk);
4250 uint32_t sz = htx_get_blksz(blk);
4251 struct ist v;
4252 uint32_t vlen;
4253
Christopher Faulete461e342018-12-18 16:43:35 +01004254 if (type == HTX_BLK_EOM) {
4255 len = 0;
4256 break;
4257 }
4258
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004259 vlen = sz;
4260 if (len > 0 && vlen > len)
4261 vlen = len;
4262 if (vlen > count) {
4263 if (type != HTX_BLK_DATA)
4264 break;
4265 vlen = count;
4266 }
4267
4268 switch (type) {
4269 case HTX_BLK_UNUSED:
4270 break;
4271
4272 case HTX_BLK_DATA:
4273 v = htx_get_blk_value(htx, blk);
4274 luaL_addlstring(&appctx->b, v.ptr, vlen);
4275 break;
4276
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004277 case HTX_BLK_TLR:
4278 case HTX_BLK_EOM:
4279 len = 0;
4280 break;
4281
4282 default:
4283 break;
4284 }
4285
4286 co_set_data(req, co_data(req) - vlen);
4287 count -= vlen;
4288 if (len > 0)
4289 len -= vlen;
4290 if (sz == vlen)
4291 blk = htx_remove_blk(htx, blk);
4292 else {
4293 htx_cut_data_blk(htx, blk, vlen);
4294 break;
4295 }
4296 }
4297
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004298 htx_to_buf(htx, &req->buf);
4299
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004300 /* If we are no other data available, yield waiting for new data. */
4301 if (len) {
4302 if (len > 0) {
4303 lua_pushinteger(L, len);
4304 lua_replace(L, 2);
4305 }
4306 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004307 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004308 }
4309
4310 /* return the result. */
4311 luaL_pushresult(&appctx->b);
4312 return 1;
4313}
4314
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004315/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004316__LJMP static int hlua_applet_http_recv(lua_State *L)
4317{
4318 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4319 int len = -1;
4320
4321 /* Check arguments. */
4322 if (lua_gettop(L) > 2)
4323 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4324 if (lua_gettop(L) >= 2) {
4325 len = MAY_LJMP(luaL_checkinteger(L, 2));
4326 lua_pop(L, 1);
4327 }
4328
Christopher Fauleta2097962019-07-15 16:25:33 +02004329 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004330
Christopher Fauleta2097962019-07-15 16:25:33 +02004331 /* Initialise the string catenation. */
4332 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004333
Christopher Fauleta2097962019-07-15 16:25:33 +02004334 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004335}
4336
4337/* Append data in the output side of the buffer. This data is immediately
4338 * sent. The function returns the amount of data written. If the buffer
4339 * cannot contain the data, the function yields. The function returns -1
4340 * if the channel is closed.
4341 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004342__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004343{
4344 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4345 struct stream_interface *si = appctx->appctx->owner;
4346 struct channel *res = si_ic(si);
4347 struct htx *htx = htx_from_buf(&res->buf);
4348 const char *data;
4349 size_t len;
4350 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4351 int max;
4352
Christopher Faulet9060fc02019-07-03 11:39:30 +02004353 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004354 if (!max)
4355 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004356
4357 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4358
4359 /* Get the max amount of data which can write as input in the channel. */
4360 if (max > (len - l))
4361 max = len - l;
4362
4363 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004364 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004365 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004366
4367 /* update counters. */
4368 l += max;
4369 lua_pop(L, 1);
4370 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004371
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004372 /* If some data is not send, declares the situation to the
4373 * applet, and returns a yield.
4374 */
4375 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004376 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004377 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004378 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004379 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004380 }
4381
Christopher Fauleta2097962019-07-15 16:25:33 +02004382 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004383 return 1;
4384}
4385
4386/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4387 * yield the LUA process, and resume it without checking the
4388 * input arguments.
4389 */
4390__LJMP static int hlua_applet_http_send(lua_State *L)
4391{
4392 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004393
4394 /* We want to send some data. Headers must be sent. */
4395 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4396 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4397 WILL_LJMP(lua_error(L));
4398 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004399
Christopher Fauleta2097962019-07-15 16:25:33 +02004400 /* This interger is used for followinf the amount of data sent. */
4401 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004402
Christopher Fauleta2097962019-07-15 16:25:33 +02004403 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004404}
4405
4406__LJMP static int hlua_applet_http_addheader(lua_State *L)
4407{
4408 const char *name;
4409 int ret;
4410
4411 MAY_LJMP(hlua_checkapplet_http(L, 1));
4412 name = MAY_LJMP(luaL_checkstring(L, 2));
4413 MAY_LJMP(luaL_checkstring(L, 3));
4414
4415 /* Push in the stack the "response" entry. */
4416 ret = lua_getfield(L, 1, "response");
4417 if (ret != LUA_TTABLE) {
4418 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4419 "is expected as an array. %s found", lua_typename(L, ret));
4420 WILL_LJMP(lua_error(L));
4421 }
4422
4423 /* check if the header is already registered if it is not
4424 * the case, register it.
4425 */
4426 ret = lua_getfield(L, -1, name);
4427 if (ret == LUA_TNIL) {
4428
4429 /* Entry not found. */
4430 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4431
4432 /* Insert the new header name in the array in the top of the stack.
4433 * It left the new array in the top of the stack.
4434 */
4435 lua_newtable(L);
4436 lua_pushvalue(L, 2);
4437 lua_pushvalue(L, -2);
4438 lua_settable(L, -4);
4439
4440 } else if (ret != LUA_TTABLE) {
4441
4442 /* corruption error. */
4443 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4444 "is expected as an array. %s found", name, lua_typename(L, ret));
4445 WILL_LJMP(lua_error(L));
4446 }
4447
4448 /* Now the top od thestack is an array of values. We push
4449 * the header value as new entry.
4450 */
4451 lua_pushvalue(L, 3);
4452 ret = lua_rawlen(L, -2);
4453 lua_rawseti(L, -2, ret + 1);
4454 lua_pushboolean(L, 1);
4455 return 1;
4456}
4457
4458__LJMP static int hlua_applet_http_status(lua_State *L)
4459{
4460 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4461 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004462 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004463
4464 if (status < 100 || status > 599) {
4465 lua_pushboolean(L, 0);
4466 return 1;
4467 }
4468
4469 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004470 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004471 lua_pushboolean(L, 1);
4472 return 1;
4473}
4474
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004475
Christopher Fauleta2097962019-07-15 16:25:33 +02004476__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004477{
4478 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4479 struct stream_interface *si = appctx->appctx->owner;
4480 struct channel *res = si_ic(si);
4481 struct htx *htx;
4482 struct htx_sl *sl;
4483 struct h1m h1m;
4484 const char *status, *reason;
4485 const char *name, *value;
4486 size_t nlen, vlen;
4487 unsigned int flags;
4488
4489 /* Send the message at once. */
4490 htx = htx_from_buf(&res->buf);
4491 h1m_init_res(&h1m);
4492
4493 /* Use the same http version than the request. */
4494 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4495 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4496 if (reason == NULL)
4497 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4498 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4499 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4500 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4501 }
4502 else {
4503 flags = HTX_SL_F_IS_RESP;
4504 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4505 }
4506 if (!sl) {
4507 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4508 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4509 WILL_LJMP(lua_error(L));
4510 }
4511 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4512
4513 /* Get the array associated to the field "response" in the object AppletHTTP. */
4514 lua_pushvalue(L, 0);
4515 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4516 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4517 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4518 WILL_LJMP(lua_error(L));
4519 }
4520
4521 /* Browse the list of headers. */
4522 lua_pushnil(L);
4523 while(lua_next(L, -2) != 0) {
4524 /* We expect a string as -2. */
4525 if (lua_type(L, -2) != LUA_TSTRING) {
4526 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4527 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4528 lua_typename(L, lua_type(L, -2)));
4529 WILL_LJMP(lua_error(L));
4530 }
4531 name = lua_tolstring(L, -2, &nlen);
4532
4533 /* We expect an array as -1. */
4534 if (lua_type(L, -1) != LUA_TTABLE) {
4535 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4536 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4537 name,
4538 lua_typename(L, lua_type(L, -1)));
4539 WILL_LJMP(lua_error(L));
4540 }
4541
4542 /* Browse the table who is on the top of the stack. */
4543 lua_pushnil(L);
4544 while(lua_next(L, -2) != 0) {
4545 int id;
4546
4547 /* We expect a number as -2. */
4548 if (lua_type(L, -2) != LUA_TNUMBER) {
4549 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4550 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4551 name,
4552 lua_typename(L, lua_type(L, -2)));
4553 WILL_LJMP(lua_error(L));
4554 }
4555 id = lua_tointeger(L, -2);
4556
4557 /* We expect a string as -2. */
4558 if (lua_type(L, -1) != LUA_TSTRING) {
4559 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4560 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4561 name, id,
4562 lua_typename(L, lua_type(L, -1)));
4563 WILL_LJMP(lua_error(L));
4564 }
4565 value = lua_tolstring(L, -1, &vlen);
4566
4567 /* Simple Protocol checks. */
4568 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4569 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4570 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4571 struct ist v = ist2(value, vlen);
4572 int ret;
4573
4574 ret = h1_parse_cont_len_header(&h1m, &v);
4575 if (ret < 0) {
4576 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4577 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4578 name);
4579 WILL_LJMP(lua_error(L));
4580 }
4581 else if (ret == 0)
4582 goto next; /* Skip it */
4583 }
4584
4585 /* Add a new header */
4586 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4587 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4588 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4589 name);
4590 WILL_LJMP(lua_error(L));
4591 }
4592 next:
4593 /* Remove the array from the stack, and get next element with a remaining string. */
4594 lua_pop(L, 1);
4595 }
4596
4597 /* Remove the array from the stack, and get next element with a remaining string. */
4598 lua_pop(L, 1);
4599 }
4600
4601 if (h1m.flags & H1_MF_CHNK)
4602 h1m.flags &= ~H1_MF_CLEN;
4603 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4604 h1m.flags |= H1_MF_XFER_LEN;
4605
4606 /* Uset HTX start-line flags */
4607 if (h1m.flags & H1_MF_XFER_ENC)
4608 flags |= HTX_SL_F_XFER_ENC;
4609 if (h1m.flags & H1_MF_XFER_LEN) {
4610 flags |= HTX_SL_F_XFER_LEN;
4611 if (h1m.flags & H1_MF_CHNK)
4612 flags |= HTX_SL_F_CHNK;
4613 else if (h1m.flags & H1_MF_CLEN)
4614 flags |= HTX_SL_F_CLEN;
4615 if (h1m.body_len == 0)
4616 flags |= HTX_SL_F_BODYLESS;
4617 }
4618 sl->flags |= flags;
4619
4620 /* If we dont have a content-length set, and the HTTP version is 1.1
4621 * and the status code implies the presence of a message body, we must
4622 * announce a transfer encoding chunked. This is required by haproxy
4623 * for the keepalive compliance. If the applet annouces a transfer-encoding
4624 * chunked itslef, don't do anything.
4625 */
4626 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4627 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4628 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4629 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4630 /* Add a new header */
4631 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4632 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4633 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4634 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4635 WILL_LJMP(lua_error(L));
4636 }
4637 }
4638
4639 /* Finalize headers. */
4640 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4641 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4642 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4643 WILL_LJMP(lua_error(L));
4644 }
4645
4646 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4647 b_reset(&res->buf);
4648 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4649 WILL_LJMP(lua_error(L));
4650 }
4651
4652 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004653 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004654
4655 /* Headers sent, set the flag. */
4656 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4657 return 0;
4658
4659}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660/* We will build the status line and the headers of the HTTP response.
4661 * We will try send at once if its not possible, we give back the hand
4662 * waiting for more room.
4663 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004664__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004665{
4666 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4667 struct stream_interface *si = appctx->appctx->owner;
4668 struct channel *res = si_ic(si);
4669
4670 if (co_data(res)) {
4671 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004672 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004673 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004674 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004675}
4676
4677
Christopher Fauleta2097962019-07-15 16:25:33 +02004678__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004679{
Christopher Fauleta2097962019-07-15 16:25:33 +02004680 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004681}
4682
Christopher Fauleta2097962019-07-15 16:25:33 +02004683/*
4684 *
4685 *
4686 * Class HTTP
4687 *
4688 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004689 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004690
4691/* Returns a struct hlua_txn if the stack entry "ud" is
4692 * a class stream, otherwise it throws an error.
4693 */
4694__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004695{
Christopher Fauleta2097962019-07-15 16:25:33 +02004696 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4697}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004698
Christopher Fauleta2097962019-07-15 16:25:33 +02004699/* This function creates and push in the stack a HTTP object
4700 * according with a current TXN.
4701 */
4702static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4703{
4704 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004705
Christopher Fauleta2097962019-07-15 16:25:33 +02004706 /* Check stack size. */
4707 if (!lua_checkstack(L, 3))
4708 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004709
Christopher Fauleta2097962019-07-15 16:25:33 +02004710 /* Create the object: obj[0] = userdata.
4711 * Note that the base of the Converters object is the
4712 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004713 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004714 lua_newtable(L);
4715 htxn = lua_newuserdata(L, sizeof(*htxn));
4716 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004717
4718 htxn->s = txn->s;
4719 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004720 htxn->dir = txn->dir;
4721 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004722
4723 /* Pop a class stream metatable and affect it to the table. */
4724 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4725 lua_setmetatable(L, -2);
4726
4727 return 1;
4728}
4729
4730/* This function creates ans returns an array of HTTP headers.
4731 * This function does not fails. It is used as wrapper with the
4732 * 2 following functions.
4733 */
4734__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4735{
Christopher Fauleta2097962019-07-15 16:25:33 +02004736 struct htx *htx;
4737 int32_t pos;
4738
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004739 /* Create the table. */
4740 lua_newtable(L);
4741
4742 if (!htxn->s->txn)
4743 return 1;
4744
Christopher Fauleta2097962019-07-15 16:25:33 +02004745 htx = htxbuf(&msg->chn->buf);
4746 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4747 struct htx_blk *blk = htx_get_blk(htx, pos);
4748 enum htx_blk_type type = htx_get_blk_type(blk);
4749 struct ist n, v;
4750 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004751
Christopher Fauleta2097962019-07-15 16:25:33 +02004752 if (type == HTX_BLK_HDR) {
4753 n = htx_get_blk_name(htx,blk);
4754 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004755 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004756 else if (type == HTX_BLK_EOH)
4757 break;
4758 else
4759 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004760
Christopher Fauleta2097962019-07-15 16:25:33 +02004761 /* Check for existing entry:
4762 * assume that the table is on the top of the stack, and
4763 * push the key in the stack, the function lua_gettable()
4764 * perform the lookup.
4765 */
4766 lua_pushlstring(L, n.ptr, n.len);
4767 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004768
Christopher Fauleta2097962019-07-15 16:25:33 +02004769 switch (lua_type(L, -1)) {
4770 case LUA_TNIL:
4771 /* Table not found, create it. */
4772 lua_pop(L, 1); /* remove the nil value. */
4773 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4774 lua_newtable(L); /* create and push empty table. */
4775 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4776 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4777 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004778 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004779
Christopher Fauleta2097962019-07-15 16:25:33 +02004780 case LUA_TTABLE:
4781 /* Entry found: push the value in the table. */
4782 len = lua_rawlen(L, -1);
4783 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4784 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4785 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4786 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004787
Christopher Fauleta2097962019-07-15 16:25:33 +02004788 default:
4789 /* Other cases are errors. */
4790 hlua_pusherror(L, "internal error during the parsing of headers.");
4791 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004792 }
4793 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004794 return 1;
4795}
4796
4797__LJMP static int hlua_http_req_get_headers(lua_State *L)
4798{
4799 struct hlua_txn *htxn;
4800
4801 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4802 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4803
4804 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4805}
4806
4807__LJMP static int hlua_http_res_get_headers(lua_State *L)
4808{
4809 struct hlua_txn *htxn;
4810
4811 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4812 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4813
4814 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4815}
4816
4817/* This function replace full header, or just a value in
4818 * the request or in the response. It is a wrapper fir the
4819 * 4 following functions.
4820 */
4821__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4822 struct http_msg *msg, int action)
4823{
4824 size_t name_len;
4825 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4826 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4827 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004828 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004829 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004830
Dragan Dosen26743032019-04-30 15:54:36 +02004831 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004832 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4833
Christopher Fauleta2097962019-07-15 16:25:33 +02004834 htx = htxbuf(&msg->chn->buf);
4835 htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02004836 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004837 return 0;
4838}
4839
4840__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4841{
4842 struct hlua_txn *htxn;
4843
4844 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4845 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4846
4847 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4848}
4849
4850__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4851{
4852 struct hlua_txn *htxn;
4853
4854 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4855 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4856
4857 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4858}
4859
4860__LJMP static int hlua_http_req_rep_val(lua_State *L)
4861{
4862 struct hlua_txn *htxn;
4863
4864 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4865 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4866
4867 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4868}
4869
4870__LJMP static int hlua_http_res_rep_val(lua_State *L)
4871{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004872 struct hlua_txn *htxn;
4873
4874 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4875 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4876
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004877 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004878}
4879
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004880/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004881 * It is a wrapper for the 2 following functions.
4882 */
4883__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4884{
4885 size_t len;
4886 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004887 struct htx *htx = htxbuf(&msg->chn->buf);
4888 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004889
Christopher Fauleta2097962019-07-15 16:25:33 +02004890 ctx.blk = NULL;
4891 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4892 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004893 return 0;
4894}
4895
4896__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4897{
4898 struct hlua_txn *htxn;
4899
4900 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4901 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4902
Willy Tarreaueee5b512015-04-03 23:46:31 +02004903 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004904}
4905
4906__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4907{
4908 struct hlua_txn *htxn;
4909
4910 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4911 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4912
Willy Tarreaueee5b512015-04-03 23:46:31 +02004913 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004914}
4915
4916/* This function adds an header. It is a wrapper used by
4917 * the 2 following functions.
4918 */
4919__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4920{
4921 size_t name_len;
4922 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4923 size_t value_len;
4924 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004925 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004926
Christopher Fauleta2097962019-07-15 16:25:33 +02004927 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4928 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004929 return 0;
4930}
4931
4932__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4933{
4934 struct hlua_txn *htxn;
4935
4936 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4937 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4938
Willy Tarreaueee5b512015-04-03 23:46:31 +02004939 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004940}
4941
4942__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4943{
4944 struct hlua_txn *htxn;
4945
4946 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4947 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
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
Willy Tarreaueee5b512015-04-03 23:46:31 +02004959 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4960 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004961}
4962
4963static int hlua_http_res_set_hdr(lua_State *L)
4964{
4965 struct hlua_txn *htxn;
4966
4967 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4968 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4969
Willy Tarreaueee5b512015-04-03 23:46:31 +02004970 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4971 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004972}
4973
4974/* This function set the method. */
4975static int hlua_http_req_set_meth(lua_State *L)
4976{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004977 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004978 size_t name_len;
4979 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004980
Christopher Fauleta2097962019-07-15 16:25:33 +02004981 lua_pushboolean(L, htx_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004982 return 1;
4983}
4984
4985/* This function set the method. */
4986static int hlua_http_req_set_path(lua_State *L)
4987{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004988 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004989 size_t name_len;
4990 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004991
Christopher Fauleta2097962019-07-15 16:25:33 +02004992 lua_pushboolean(L, htx_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004993 return 1;
4994}
4995
4996/* This function set the query-string. */
4997static int hlua_http_req_set_query(lua_State *L)
4998{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004999 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005000 size_t name_len;
5001 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005002
5003 /* Check length. */
5004 if (name_len > trash.size - 1) {
5005 lua_pushboolean(L, 0);
5006 return 1;
5007 }
5008
5009 /* Add the mark question as prefix. */
5010 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005011 trash.area[trash.data++] = '?';
5012 memcpy(trash.area + trash.data, name, name_len);
5013 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005014
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005015 lua_pushboolean(L,
Christopher Fauleta2097962019-07-15 16:25:33 +02005016 htx_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005017 return 1;
5018}
5019
5020/* This function set the uri. */
5021static int hlua_http_req_set_uri(lua_State *L)
5022{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005023 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005024 size_t name_len;
5025 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005026
Christopher Fauleta2097962019-07-15 16:25:33 +02005027 lua_pushboolean(L, htx_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005028 return 1;
5029}
5030
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005031/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005032static int hlua_http_res_set_status(lua_State *L)
5033{
5034 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5035 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005036 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005037
Christopher Fauleta2097962019-07-15 16:25:33 +02005038 htx_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005039 return 0;
5040}
5041
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005042/*
5043 *
5044 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005045 * Class TXN
5046 *
5047 *
5048 */
5049
5050/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005051 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005052 */
5053__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5054{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005055 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005056}
5057
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005058__LJMP static int hlua_set_var(lua_State *L)
5059{
5060 struct hlua_txn *htxn;
5061 const char *name;
5062 size_t len;
5063 struct sample smp;
5064
5065 MAY_LJMP(check_args(L, 3, "set_var"));
5066
5067 /* It is useles to retrieve the stream, but this function
5068 * runs only in a stream context.
5069 */
5070 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5071 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5072
5073 /* Converts the third argument in a sample. */
5074 hlua_lua2smp(L, 3, &smp);
5075
5076 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005077 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005078 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005079 return 0;
5080}
5081
Christopher Faulet85d79c92016-11-09 16:54:56 +01005082__LJMP static int hlua_unset_var(lua_State *L)
5083{
5084 struct hlua_txn *htxn;
5085 const char *name;
5086 size_t len;
5087 struct sample smp;
5088
5089 MAY_LJMP(check_args(L, 2, "unset_var"));
5090
5091 /* It is useles to retrieve the stream, but this function
5092 * runs only in a stream context.
5093 */
5094 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5095 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5096
5097 /* Unset the variable. */
5098 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5099 vars_unset_by_name(name, len, &smp);
5100 return 0;
5101}
5102
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005103__LJMP static int hlua_get_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, "get_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
Willy Tarreau7560dd42016-03-10 16:28:58 +01005118 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005119 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005120 lua_pushnil(L);
5121 return 1;
5122 }
5123
5124 return hlua_smp2lua(L, &smp);
5125}
5126
Willy Tarreau59551662015-03-10 14:23:13 +01005127__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005128{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005129 struct hlua *hlua;
5130
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005131 MAY_LJMP(check_args(L, 2, "set_priv"));
5132
Willy Tarreau87b09662015-04-03 00:22:06 +02005133 /* It is useles to retrieve the stream, but this function
5134 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005135 */
5136 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005137 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005138
5139 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005140 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005141
5142 /* Get and store new value. */
5143 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5144 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5145
5146 return 0;
5147}
5148
Willy Tarreau59551662015-03-10 14:23:13 +01005149__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005150{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005151 struct hlua *hlua;
5152
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005153 MAY_LJMP(check_args(L, 1, "get_priv"));
5154
Willy Tarreau87b09662015-04-03 00:22:06 +02005155 /* It is useles to retrieve the stream, but this function
5156 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005157 */
5158 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005159 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005160
5161 /* Push configuration index in the stack. */
5162 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5163
5164 return 1;
5165}
5166
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005167/* Create stack entry containing a class TXN. This function
5168 * return 0 if the stack does not contains free slots,
5169 * otherwise it returns 1.
5170 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005171static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005172{
Willy Tarreaude491382015-04-06 11:04:28 +02005173 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005174
5175 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005176 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005177 return 0;
5178
5179 /* NOTE: The allocation never fails. The failure
5180 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005181 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005182 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005183 /* Create the object: obj[0] = userdata. */
5184 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005185 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005186 lua_rawseti(L, -2, 0);
5187
Willy Tarreaude491382015-04-06 11:04:28 +02005188 htxn->s = s;
5189 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005190 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005191 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005192
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005193 /* Create the "f" field that contains a list of fetches. */
5194 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005195 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005196 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005197 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005198
5199 /* Create the "sf" field that contains a list of stringsafe fetches. */
5200 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005201 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005202 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005203 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005204
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005205 /* Create the "c" field that contains a list of converters. */
5206 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005207 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005208 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005209 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005210
5211 /* Create the "sc" field that contains a list of stringsafe converters. */
5212 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005213 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005214 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005215 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005216
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005217 /* Create the "req" field that contains the request channel object. */
5218 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005219 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005220 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005221 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005222
5223 /* Create the "res" field that contains the response channel object. */
5224 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005225 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005226 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005227 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005228
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005229 /* Creates the HTTP object is the current proxy allows http. */
5230 lua_pushstring(L, "http");
5231 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005232 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005233 return 0;
5234 }
5235 else
5236 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005237 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005238
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005239 /* Pop a class sesison metatable and affect it to the userdata. */
5240 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5241 lua_setmetatable(L, -2);
5242
5243 return 1;
5244}
5245
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005246__LJMP static int hlua_txn_deflog(lua_State *L)
5247{
5248 const char *msg;
5249 struct hlua_txn *htxn;
5250
5251 MAY_LJMP(check_args(L, 2, "deflog"));
5252 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5253 msg = MAY_LJMP(luaL_checkstring(L, 2));
5254
5255 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5256 return 0;
5257}
5258
5259__LJMP static int hlua_txn_log(lua_State *L)
5260{
5261 int level;
5262 const char *msg;
5263 struct hlua_txn *htxn;
5264
5265 MAY_LJMP(check_args(L, 3, "log"));
5266 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5267 level = MAY_LJMP(luaL_checkinteger(L, 2));
5268 msg = MAY_LJMP(luaL_checkstring(L, 3));
5269
5270 if (level < 0 || level >= NB_LOG_LEVELS)
5271 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5272
5273 hlua_sendlog(htxn->s->be, level, msg);
5274 return 0;
5275}
5276
5277__LJMP static int hlua_txn_log_debug(lua_State *L)
5278{
5279 const char *msg;
5280 struct hlua_txn *htxn;
5281
5282 MAY_LJMP(check_args(L, 2, "Debug"));
5283 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5284 msg = MAY_LJMP(luaL_checkstring(L, 2));
5285 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5286 return 0;
5287}
5288
5289__LJMP static int hlua_txn_log_info(lua_State *L)
5290{
5291 const char *msg;
5292 struct hlua_txn *htxn;
5293
5294 MAY_LJMP(check_args(L, 2, "Info"));
5295 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5296 msg = MAY_LJMP(luaL_checkstring(L, 2));
5297 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5298 return 0;
5299}
5300
5301__LJMP static int hlua_txn_log_warning(lua_State *L)
5302{
5303 const char *msg;
5304 struct hlua_txn *htxn;
5305
5306 MAY_LJMP(check_args(L, 2, "Warning"));
5307 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5308 msg = MAY_LJMP(luaL_checkstring(L, 2));
5309 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5310 return 0;
5311}
5312
5313__LJMP static int hlua_txn_log_alert(lua_State *L)
5314{
5315 const char *msg;
5316 struct hlua_txn *htxn;
5317
5318 MAY_LJMP(check_args(L, 2, "Alert"));
5319 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5320 msg = MAY_LJMP(luaL_checkstring(L, 2));
5321 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5322 return 0;
5323}
5324
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005325__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5326{
5327 struct hlua_txn *htxn;
5328 int ll;
5329
5330 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5331 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5332 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5333
5334 if (ll < 0 || ll > 7)
5335 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5336
5337 htxn->s->logs.level = ll;
5338 return 0;
5339}
5340
5341__LJMP static int hlua_txn_set_tos(lua_State *L)
5342{
5343 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005344 int tos;
5345
5346 MAY_LJMP(check_args(L, 2, "set_tos"));
5347 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5348 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5349
Willy Tarreau1a18b542018-12-11 16:37:42 +01005350 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005351 return 0;
5352}
5353
5354__LJMP static int hlua_txn_set_mark(lua_State *L)
5355{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005356 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005357 int mark;
5358
5359 MAY_LJMP(check_args(L, 2, "set_mark"));
5360 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5361 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5362
Willy Tarreau1a18b542018-12-11 16:37:42 +01005363 conn_set_tos(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005364 return 0;
5365}
5366
Patrick Hemmer268a7072018-05-11 12:52:31 -04005367__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5368{
5369 struct hlua_txn *htxn;
5370
5371 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5372 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5373 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5374 return 0;
5375}
5376
5377__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5378{
5379 struct hlua_txn *htxn;
5380
5381 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5382 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5383 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5384 return 0;
5385}
5386
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005387/* This function is an Lua binding that send pending data
5388 * to the client, and close the stream interface.
5389 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005390__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005391{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005392 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005393 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005394 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005395
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005396 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005397 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005398 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005399
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005400 /* If the flags NOTERM is set, we cannot terminate the http
5401 * session, so we just end the execution of the current
5402 * lua code.
5403 */
5404 if (htxn->flags & HLUA_TXN_NOTERM) {
5405 WILL_LJMP(hlua_done(L));
5406 return 0;
5407 }
5408
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005409 ic = &htxn->s->req;
5410 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005411
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005412 if (IS_HTX_STRM(htxn->s))
5413 htx_reply_and_close(htxn->s, 0, NULL);
5414 else {
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005415 channel_auto_read(ic);
5416 channel_abort(ic);
5417 channel_auto_close(ic);
5418 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005419
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005420 oc->wex = tick_add_ifset(now_ms, oc->wto);
5421 channel_auto_read(oc);
5422 channel_auto_close(oc);
5423 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005424
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005425 ic->analysers = 0;
5426 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005427
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005428 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005429 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005430 return 0;
5431}
5432
5433__LJMP static int hlua_log(lua_State *L)
5434{
5435 int level;
5436 const char *msg;
5437
5438 MAY_LJMP(check_args(L, 2, "log"));
5439 level = MAY_LJMP(luaL_checkinteger(L, 1));
5440 msg = MAY_LJMP(luaL_checkstring(L, 2));
5441
5442 if (level < 0 || level >= NB_LOG_LEVELS)
5443 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5444
5445 hlua_sendlog(NULL, level, msg);
5446 return 0;
5447}
5448
5449__LJMP static int hlua_log_debug(lua_State *L)
5450{
5451 const char *msg;
5452
5453 MAY_LJMP(check_args(L, 1, "debug"));
5454 msg = MAY_LJMP(luaL_checkstring(L, 1));
5455 hlua_sendlog(NULL, LOG_DEBUG, msg);
5456 return 0;
5457}
5458
5459__LJMP static int hlua_log_info(lua_State *L)
5460{
5461 const char *msg;
5462
5463 MAY_LJMP(check_args(L, 1, "info"));
5464 msg = MAY_LJMP(luaL_checkstring(L, 1));
5465 hlua_sendlog(NULL, LOG_INFO, msg);
5466 return 0;
5467}
5468
5469__LJMP static int hlua_log_warning(lua_State *L)
5470{
5471 const char *msg;
5472
5473 MAY_LJMP(check_args(L, 1, "warning"));
5474 msg = MAY_LJMP(luaL_checkstring(L, 1));
5475 hlua_sendlog(NULL, LOG_WARNING, msg);
5476 return 0;
5477}
5478
5479__LJMP static int hlua_log_alert(lua_State *L)
5480{
5481 const char *msg;
5482
5483 MAY_LJMP(check_args(L, 1, "alert"));
5484 msg = MAY_LJMP(luaL_checkstring(L, 1));
5485 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005486 return 0;
5487}
5488
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005489__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005490{
5491 int wakeup_ms = lua_tointeger(L, -1);
5492 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005493 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005494 return 0;
5495}
5496
5497__LJMP static int hlua_sleep(lua_State *L)
5498{
5499 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005500 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005501
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005502 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005503
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005504 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005505 wakeup_ms = tick_add(now_ms, delay);
5506 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005507
Willy Tarreau9635e032018-10-16 17:52:55 +02005508 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005509 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005510}
5511
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005512__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005513{
5514 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005515 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005516
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005517 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005518
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005519 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005520 wakeup_ms = tick_add(now_ms, delay);
5521 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005522
Willy Tarreau9635e032018-10-16 17:52:55 +02005523 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005524 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005525}
5526
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005527/* This functionis an LUA binding. it permits to give back
5528 * the hand at the HAProxy scheduler. It is used when the
5529 * LUA processing consumes a lot of time.
5530 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005531__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005532{
5533 return 0;
5534}
5535
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005536__LJMP static int hlua_yield(lua_State *L)
5537{
Willy Tarreau9635e032018-10-16 17:52:55 +02005538 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005539 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005540}
5541
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005542/* This function change the nice of the currently executed
5543 * task. It is used set low or high priority at the current
5544 * task.
5545 */
Willy Tarreau59551662015-03-10 14:23:13 +01005546__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005547{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005548 struct hlua *hlua;
5549 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005550
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005551 MAY_LJMP(check_args(L, 1, "set_nice"));
5552 hlua = hlua_gethlua(L);
5553 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005554
5555 /* If he task is not set, I'm in a start mode. */
5556 if (!hlua || !hlua->task)
5557 return 0;
5558
5559 if (nice < -1024)
5560 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005561 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005562 nice = 1024;
5563
5564 hlua->task->nice = nice;
5565 return 0;
5566}
5567
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005568/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005569 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5570 * return an E_AGAIN signal, the emmiter of this signal must set a
5571 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005572 *
5573 * Task wrapper are longjmp safe because the only one Lua code
5574 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005575 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005576static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005577{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005578 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005579 enum hlua_exec status;
5580
Christopher Faulet5bc99722018-04-25 10:34:45 +02005581 if (task->thread_mask == MAX_THREADS_MASK)
5582 task_set_affinity(task, tid_bit);
5583
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005584 /* If it is the first call to the task, we must initialize the
5585 * execution timeouts.
5586 */
5587 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005588 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005589
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005590 /* Execute the Lua code. */
5591 status = hlua_ctx_resume(hlua, 1);
5592
5593 switch (status) {
5594 /* finished or yield */
5595 case HLUA_E_OK:
5596 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005597 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005598 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005599 break;
5600
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005601 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005602 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005603 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005604 break;
5605
5606 /* finished with error. */
5607 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005608 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005609 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005610 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005611 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005612 break;
5613
5614 case HLUA_E_ERR:
5615 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005616 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005617 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02005618 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005619 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005620 break;
5621 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005622 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005623}
5624
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005625/* This function is an LUA binding that register LUA function to be
5626 * executed after the HAProxy configuration parsing and before the
5627 * HAProxy scheduler starts. This function expect only one LUA
5628 * argument that is a function. This function returns nothing, but
5629 * throws if an error is encountered.
5630 */
5631__LJMP static int hlua_register_init(lua_State *L)
5632{
5633 struct hlua_init_function *init;
5634 int ref;
5635
5636 MAY_LJMP(check_args(L, 1, "register_init"));
5637
5638 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5639
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005640 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005641 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005642 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005643
5644 init->function_ref = ref;
5645 LIST_ADDQ(&hlua_init_functions, &init->l);
5646 return 0;
5647}
5648
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005649/* This functio is an LUA binding. It permits to register a task
5650 * executed in parallel of the main HAroxy activity. The task is
5651 * created and it is set in the HAProxy scheduler. It can be called
5652 * from the "init" section, "post init" or during the runtime.
5653 *
5654 * Lua prototype:
5655 *
5656 * <none> core.register_task(<function>)
5657 */
5658static int hlua_register_task(lua_State *L)
5659{
5660 struct hlua *hlua;
5661 struct task *task;
5662 int ref;
5663
5664 MAY_LJMP(check_args(L, 1, "register_task"));
5665
5666 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5667
Willy Tarreaubafbe012017-11-24 17:34:44 +01005668 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005669 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005670 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005671
Emeric Brunc60def82017-09-27 14:59:38 +02005672 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02005673 if (!task)
5674 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
5675
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005676 task->context = hlua;
5677 task->process = hlua_process_task;
5678
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005679 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005680 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005681
5682 /* Restore the function in the stack. */
5683 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5684 hlua->nargs = 0;
5685
5686 /* Schedule task. */
5687 task_schedule(task, now_ms);
5688
5689 return 0;
5690}
5691
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005692/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5693 * doesn't allow "yield" functions because the HAProxy engine cannot
5694 * resume converters.
5695 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005696static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005697{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005698 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005699 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005700 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005701
Willy Tarreaube508f12016-03-10 11:47:01 +01005702 if (!stream)
5703 return 0;
5704
Willy Tarreau87b09662015-04-03 00:22:06 +02005705 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005706 * Lua context can be not initialized. This behavior
5707 * permits to save performances because a systematic
5708 * Lua initialization cause 5% performances loss.
5709 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005710 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005711 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005712 if (!stream->hlua) {
5713 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5714 return 0;
5715 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005716 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005717 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5718 return 0;
5719 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005720 }
5721
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005722 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005723 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005724
5725 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005726 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5727 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5728 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005729 else
5730 error = "critical error";
5731 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005732 return 0;
5733 }
5734
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005735 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005736 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005737 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005738 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005739 return 0;
5740 }
5741
5742 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005743 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005744
5745 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005746 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005747 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005748 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005749 return 0;
5750 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005751 hlua_smp2lua(stream->hlua->T, smp);
5752 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005753
5754 /* push keywords in the stack. */
5755 if (arg_p) {
5756 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005757 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005758 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005759 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005760 return 0;
5761 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005762 hlua_arg2lua(stream->hlua->T, arg_p);
5763 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005764 }
5765 }
5766
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005767 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005768 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005769
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005770 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005771 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005772 }
5773
5774 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005775 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005776 /* finished. */
5777 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005778 /* If the stack is empty, the function fails. */
5779 if (lua_gettop(stream->hlua->T) <= 0)
5780 return 0;
5781
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005782 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005783 hlua_lua2smp(stream->hlua->T, -1, smp);
5784 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005785 return 1;
5786
5787 /* yield. */
5788 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005789 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005790 return 0;
5791
5792 /* finished with error. */
5793 case HLUA_E_ERRMSG:
5794 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005795 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005796 fcn->name, lua_tostring(stream->hlua->T, -1));
5797 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005798 return 0;
5799
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005800 case HLUA_E_ETMOUT:
5801 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5802 return 0;
5803
5804 case HLUA_E_NOMEM:
5805 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5806 return 0;
5807
5808 case HLUA_E_YIELD:
5809 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5810 return 0;
5811
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005812 case HLUA_E_ERR:
5813 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005814 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005815
5816 default:
5817 return 0;
5818 }
5819}
5820
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005821/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5822 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005823 * resume sample-fetches. This function will be called by the sample
5824 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005825 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005826static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5827 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005828{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005829 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005830 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005831 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02005832 const struct buffer msg = { };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005833
Willy Tarreaube508f12016-03-10 11:47:01 +01005834 if (!stream)
5835 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01005836
Willy Tarreau87b09662015-04-03 00:22:06 +02005837 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005838 * Lua context can be not initialized. This behavior
5839 * permits to save performances because a systematic
5840 * Lua initialization cause 5% performances loss.
5841 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005842 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005843 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005844 if (!stream->hlua) {
5845 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5846 return 0;
5847 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01005848 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005849 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5850 return 0;
5851 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005852 }
5853
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005854 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005855
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005856 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005857 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005858
5859 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005860 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5861 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5862 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005863 else
5864 error = "critical error";
5865 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005866 return 0;
5867 }
5868
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005869 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005870 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005871 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005872 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005873 return 0;
5874 }
5875
5876 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005877 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005878
5879 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005880 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005881 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005882 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005883 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005884 return 0;
5885 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005886 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005887
5888 /* push keywords in the stack. */
5889 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5890 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005891 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005892 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005893 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005894 return 0;
5895 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005896 hlua_arg2lua(stream->hlua->T, arg_p);
5897 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005898 }
5899
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005900 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005901 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005902
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005903 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005904 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005905 }
5906
5907 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005908 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005909 /* finished. */
5910 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005911 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005912 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005913 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005914 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005915 /* If the stack is empty, the function fails. */
5916 if (lua_gettop(stream->hlua->T) <= 0)
5917 return 0;
5918
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005919 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005920 hlua_lua2smp(stream->hlua->T, -1, smp);
5921 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005922
5923 /* Set the end of execution flag. */
5924 smp->flags &= ~SMP_F_MAY_CHANGE;
5925 return 1;
5926
5927 /* yield. */
5928 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005929 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005930 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005931 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005932 return 0;
5933
5934 /* finished with error. */
5935 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005936 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005937 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005938 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005939 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005940 fcn->name, lua_tostring(stream->hlua->T, -1));
5941 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005942 return 0;
5943
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005944 case HLUA_E_ETMOUT:
5945 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005946 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005947 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5948 return 0;
5949
5950 case HLUA_E_NOMEM:
5951 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005952 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005953 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
5954 return 0;
5955
5956 case HLUA_E_YIELD:
5957 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005958 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005959 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
5960 return 0;
5961
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005962 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005963 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01005964 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005965 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005966 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005967
5968 default:
5969 return 0;
5970 }
5971}
5972
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005973/* This function is an LUA binding used for registering
5974 * "sample-conv" functions. It expects a converter name used
5975 * in the haproxy configuration file, and an LUA function.
5976 */
5977__LJMP static int hlua_register_converters(lua_State *L)
5978{
5979 struct sample_conv_kw_list *sck;
5980 const char *name;
5981 int ref;
5982 int len;
5983 struct hlua_function *fcn;
5984
5985 MAY_LJMP(check_args(L, 2, "register_converters"));
5986
5987 /* First argument : converter name. */
5988 name = MAY_LJMP(luaL_checkstring(L, 1));
5989
5990 /* Second argument : lua function. */
5991 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5992
5993 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005994 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005995 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005996 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005997 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005998 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005999 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006000
6001 /* Fill fcn. */
6002 fcn->name = strdup(name);
6003 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006004 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006005 fcn->function_ref = ref;
6006
6007 /* List head */
6008 sck->list.n = sck->list.p = NULL;
6009
6010 /* converter keyword. */
6011 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006012 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006013 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006014 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006015
6016 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6017 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006018 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 +01006019 sck->kw[0].val_args = NULL;
6020 sck->kw[0].in_type = SMP_T_STR;
6021 sck->kw[0].out_type = SMP_T_STR;
6022 sck->kw[0].private = fcn;
6023
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006024 /* Register this new converter */
6025 sample_register_convs(sck);
6026
6027 return 0;
6028}
6029
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006030/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006031 * "sample-fetch" functions. It expects a converter name used
6032 * in the haproxy configuration file, and an LUA function.
6033 */
6034__LJMP static int hlua_register_fetches(lua_State *L)
6035{
6036 const char *name;
6037 int ref;
6038 int len;
6039 struct sample_fetch_kw_list *sfk;
6040 struct hlua_function *fcn;
6041
6042 MAY_LJMP(check_args(L, 2, "register_fetches"));
6043
6044 /* First argument : sample-fetch name. */
6045 name = MAY_LJMP(luaL_checkstring(L, 1));
6046
6047 /* Second argument : lua function. */
6048 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6049
6050 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006051 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006052 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006053 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006054 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006055 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006056 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006057
6058 /* Fill fcn. */
6059 fcn->name = strdup(name);
6060 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006061 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006062 fcn->function_ref = ref;
6063
6064 /* List head */
6065 sfk->list.n = sfk->list.p = NULL;
6066
6067 /* sample-fetch keyword. */
6068 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006069 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006070 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006071 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006072
6073 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6074 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006075 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 +01006076 sfk->kw[0].val_args = NULL;
6077 sfk->kw[0].out_type = SMP_T_STR;
6078 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6079 sfk->kw[0].val = 0;
6080 sfk->kw[0].private = fcn;
6081
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006082 /* Register this new fetch. */
6083 sample_register_fetches(sfk);
6084
6085 return 0;
6086}
6087
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006088/* This function is a wrapper to execute each LUA function declared
6089 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006090 * return ACT_RET_CONT if the processing is finished (with or without
6091 * error) and return ACT_RET_YIELD if the function must be called again
6092 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006093 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006094static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006095 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006096{
6097 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006098 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006099 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006100 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006101 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006102
6103 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006104 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6105 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6106 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6107 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006108 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006109 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006110 return ACT_RET_CONT;
6111 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006112
Willy Tarreau87b09662015-04-03 00:22:06 +02006113 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006114 * Lua context can be not initialized. This behavior
6115 * permits to save performances because a systematic
6116 * Lua initialization cause 5% performances loss.
6117 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006118 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006119 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006120 if (!s->hlua) {
6121 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6122 rule->arg.hlua_rule->fcn.name);
6123 return ACT_RET_CONT;
6124 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006125 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006126 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6127 rule->arg.hlua_rule->fcn.name);
6128 return ACT_RET_CONT;
6129 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006130 }
6131
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006132 consistency_set(s, dir, &s->hlua->cons);
6133
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006134 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006135 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006136
6137 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006138 if (!SET_SAFE_LJMP(s->hlua->T)) {
6139 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6140 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006141 else
6142 error = "critical error";
6143 SEND_ERR(px, "Lua function '%s': %s.\n",
6144 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006145 return ACT_RET_CONT;
6146 }
6147
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006148 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006149 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006150 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006151 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006152 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006153 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006154 }
6155
6156 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006157 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006158
Willy Tarreau87b09662015-04-03 00:22:06 +02006159 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006160 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006161 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006162 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006163 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006164 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006165 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006166 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006167
6168 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006169 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006170 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006171 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006172 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006173 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006174 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006175 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006176 lua_pushstring(s->hlua->T, *arg);
6177 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006178 }
6179
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006180 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006181 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006182
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006183 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006184 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006185 }
6186
6187 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006188 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006189 /* finished. */
6190 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006191 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006192 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006193 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006194 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006195 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet8f1aa772019-07-04 11:27:15 +02006196 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006197 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006198
6199 /* yield. */
6200 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006201 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006202 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006203 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006204 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006205 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006206 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006207 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006208 /* Some actions can be wake up when a "write" event
6209 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006210 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006211 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006213 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006214 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006215 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006216 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006217 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006218 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006219 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006220 * because HAProxy is not able to manipulate data, it
6221 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006222 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006223
6224 /* finished with error. */
6225 case HLUA_E_ERRMSG:
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 FOURNIER258d8aa2015-02-16 20:23:40 +01006230 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006231 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006232 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6233 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006234 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006235
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006236 case HLUA_E_ETMOUT:
6237 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006238 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006239 return ACT_RET_ERR;
6240 }
6241 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6242 return 0;
6243
6244 case HLUA_E_NOMEM:
6245 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006246 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006247 return ACT_RET_ERR;
6248 }
6249 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6250 return 0;
6251
6252 case HLUA_E_YIELD:
6253 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006254 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006255 return ACT_RET_ERR;
6256 }
6257 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6258 rule->arg.hlua_rule->fcn.name);
6259 return 0;
6260
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006261 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006262 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006263 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006264 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006265 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006266 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006267 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006268 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006269
6270 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006271 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006272 }
6273}
6274
Olivier Houchard9f6af332018-05-25 14:04:04 +02006275struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006276{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006277 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006278
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006279 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006280 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006281 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006282}
6283
6284static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6285{
6286 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006287 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006288 struct task *task;
6289 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006290 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006291
Willy Tarreaubafbe012017-11-24 17:34:44 +01006292 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006293 if (!hlua) {
6294 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6295 ctx->rule->arg.hlua_rule->fcn.name);
6296 return 0;
6297 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006298 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006299 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006300 ctx->ctx.hlua_apptcp.flags = 0;
6301
6302 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006303 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006304 if (!task) {
6305 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6306 ctx->rule->arg.hlua_rule->fcn.name);
6307 return 0;
6308 }
6309 task->nice = 0;
6310 task->context = ctx;
6311 task->process = hlua_applet_wakeup;
6312 ctx->ctx.hlua_apptcp.task = task;
6313
6314 /* In the execution wrappers linked with a stream, the
6315 * Lua context can be not initialized. This behavior
6316 * permits to save performances because a systematic
6317 * Lua initialization cause 5% performances loss.
6318 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006319 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006320 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6321 ctx->rule->arg.hlua_rule->fcn.name);
6322 return 0;
6323 }
6324
6325 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006326 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006327
6328 /* The following Lua calls can fail. */
6329 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006330 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6331 error = lua_tostring(hlua->T, -1);
6332 else
6333 error = "critical error";
6334 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6335 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006336 return 0;
6337 }
6338
6339 /* Check stack available size. */
6340 if (!lua_checkstack(hlua->T, 1)) {
6341 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6342 ctx->rule->arg.hlua_rule->fcn.name);
6343 RESET_SAFE_LJMP(hlua->T);
6344 return 0;
6345 }
6346
6347 /* Restore the function in the stack. */
6348 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6349
6350 /* Create and and push object stream in the stack. */
6351 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6352 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6353 ctx->rule->arg.hlua_rule->fcn.name);
6354 RESET_SAFE_LJMP(hlua->T);
6355 return 0;
6356 }
6357 hlua->nargs = 1;
6358
6359 /* push keywords in the stack. */
6360 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6361 if (!lua_checkstack(hlua->T, 1)) {
6362 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6363 ctx->rule->arg.hlua_rule->fcn.name);
6364 RESET_SAFE_LJMP(hlua->T);
6365 return 0;
6366 }
6367 lua_pushstring(hlua->T, *arg);
6368 hlua->nargs++;
6369 }
6370
6371 RESET_SAFE_LJMP(hlua->T);
6372
6373 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006374 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006375 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006376
6377 return 1;
6378}
6379
6380static void hlua_applet_tcp_fct(struct appctx *ctx)
6381{
6382 struct stream_interface *si = ctx->owner;
6383 struct stream *strm = si_strm(si);
6384 struct channel *res = si_ic(si);
6385 struct act_rule *rule = ctx->rule;
6386 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006387 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006388
6389 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006390 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6391 /* eat the whole request */
6392 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006393 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006394 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006395
6396 /* If the stream is disconnect or closed, ldo nothing. */
6397 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6398 return;
6399
6400 /* Execute the function. */
6401 switch (hlua_ctx_resume(hlua, 1)) {
6402 /* finished. */
6403 case HLUA_E_OK:
6404 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6405
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006406 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006407 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006408 res->flags |= CF_READ_NULL;
6409 si_shutr(si);
6410 return;
6411
6412 /* yield. */
6413 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006414 if (hlua->wake_time != TICK_ETERNITY)
6415 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006416 return;
6417
6418 /* finished with error. */
6419 case HLUA_E_ERRMSG:
6420 /* Display log. */
6421 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6422 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6423 lua_pop(hlua->T, 1);
6424 goto error;
6425
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006426 case HLUA_E_ETMOUT:
6427 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6428 rule->arg.hlua_rule->fcn.name);
6429 goto error;
6430
6431 case HLUA_E_NOMEM:
6432 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6433 rule->arg.hlua_rule->fcn.name);
6434 goto error;
6435
6436 case HLUA_E_YIELD: /* unexpected */
6437 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6438 rule->arg.hlua_rule->fcn.name);
6439 goto error;
6440
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006441 case HLUA_E_ERR:
6442 /* Display log. */
6443 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6444 rule->arg.hlua_rule->fcn.name);
6445 goto error;
6446
6447 default:
6448 goto error;
6449 }
6450
6451error:
6452
6453 /* For all other cases, just close the stream. */
6454 si_shutw(si);
6455 si_shutr(si);
6456 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6457}
6458
6459static void hlua_applet_tcp_release(struct appctx *ctx)
6460{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006461 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006462 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006463 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006464 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006465}
6466
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006467/* The function returns 1 if the initialisation is complete, 0 if
6468 * an errors occurs and -1 if more data are required for initializing
6469 * the applet.
6470 */
6471static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6472{
6473 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006474 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006475 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006476 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006477 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006478 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006479
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006480 txn = strm->txn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006481
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006482 /* We want two things in HTTP mode :
6483 * - enforce server-close mode if we were in keep-alive, so that the
6484 * applet is released after each response ;
6485 * - enable request body transfer to the applet in order to resync
6486 * with the response body.
6487 */
6488 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6489 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006490
Willy Tarreaubafbe012017-11-24 17:34:44 +01006491 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006492 if (!hlua) {
6493 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6494 ctx->rule->arg.hlua_rule->fcn.name);
6495 return 0;
6496 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006497 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006498 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006499 ctx->ctx.hlua_apphttp.left_bytes = -1;
6500 ctx->ctx.hlua_apphttp.flags = 0;
6501
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006502 if (txn->req.flags & HTTP_MSGF_VER_11)
6503 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6504
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006505 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006506 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006507 if (!task) {
6508 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6509 ctx->rule->arg.hlua_rule->fcn.name);
6510 return 0;
6511 }
6512 task->nice = 0;
6513 task->context = ctx;
6514 task->process = hlua_applet_wakeup;
6515 ctx->ctx.hlua_apphttp.task = task;
6516
6517 /* In the execution wrappers linked with a stream, the
6518 * Lua context can be not initialized. This behavior
6519 * permits to save performances because a systematic
6520 * Lua initialization cause 5% performances loss.
6521 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006522 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006523 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6524 ctx->rule->arg.hlua_rule->fcn.name);
6525 return 0;
6526 }
6527
6528 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006529 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006530
6531 /* The following Lua calls can fail. */
6532 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006533 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6534 error = lua_tostring(hlua->T, -1);
6535 else
6536 error = "critical error";
6537 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6538 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006539 return 0;
6540 }
6541
6542 /* Check stack available size. */
6543 if (!lua_checkstack(hlua->T, 1)) {
6544 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6545 ctx->rule->arg.hlua_rule->fcn.name);
6546 RESET_SAFE_LJMP(hlua->T);
6547 return 0;
6548 }
6549
6550 /* Restore the function in the stack. */
6551 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6552
6553 /* Create and and push object stream in the stack. */
6554 if (!hlua_applet_http_new(hlua->T, ctx)) {
6555 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6556 ctx->rule->arg.hlua_rule->fcn.name);
6557 RESET_SAFE_LJMP(hlua->T);
6558 return 0;
6559 }
6560 hlua->nargs = 1;
6561
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006562 /* push keywords in the stack. */
6563 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6564 if (!lua_checkstack(hlua->T, 1)) {
6565 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6566 ctx->rule->arg.hlua_rule->fcn.name);
6567 RESET_SAFE_LJMP(hlua->T);
6568 return 0;
6569 }
6570 lua_pushstring(hlua->T, *arg);
6571 hlua->nargs++;
6572 }
6573
6574 RESET_SAFE_LJMP(hlua->T);
6575
6576 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006577 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006578
6579 return 1;
6580}
6581
Christopher Fauleta2097962019-07-15 16:25:33 +02006582static void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006583{
6584 struct stream_interface *si = ctx->owner;
6585 struct stream *strm = si_strm(si);
6586 struct channel *req = si_oc(si);
6587 struct channel *res = si_ic(si);
6588 struct act_rule *rule = ctx->rule;
6589 struct proxy *px = strm->be;
6590 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
6591 struct htx *req_htx, *res_htx;
6592
6593 res_htx = htx_from_buf(&res->buf);
6594
6595 /* If the stream is disconnect or closed, ldo nothing. */
6596 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6597 goto out;
6598
6599 /* Check if the input buffer is avalaible. */
6600 if (!b_size(&res->buf)) {
6601 si_rx_room_blk(si);
6602 goto out;
6603 }
6604 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006605 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006606 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6607
6608 /* Set the currently running flag. */
6609 if (!HLUA_IS_RUNNING(hlua) &&
6610 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6611 struct htx_blk *blk;
6612 size_t count = co_data(req);
6613
6614 if (!count) {
6615 si_cant_get(si);
6616 goto out;
6617 }
6618
6619 /* We need to flush the request header. This left the body for
6620 * the Lua.
6621 */
6622 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02006623 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006624 while (count && blk) {
6625 enum htx_blk_type type = htx_get_blk_type(blk);
6626 uint32_t sz = htx_get_blksz(blk);
6627
6628 if (sz > count) {
6629 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006630 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006631 goto out;
6632 }
6633
6634 count -= sz;
6635 co_set_data(req, co_data(req) - sz);
6636 blk = htx_remove_blk(req_htx, blk);
6637
6638 if (type == HTX_BLK_EOH)
6639 break;
6640 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01006641 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006642 }
6643
6644 /* Executes The applet if it is not done. */
6645 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6646
6647 /* Execute the function. */
6648 switch (hlua_ctx_resume(hlua, 1)) {
6649 /* finished. */
6650 case HLUA_E_OK:
6651 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6652 break;
6653
6654 /* yield. */
6655 case HLUA_E_AGAIN:
6656 if (hlua->wake_time != TICK_ETERNITY)
6657 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006658 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006659
6660 /* finished with error. */
6661 case HLUA_E_ERRMSG:
6662 /* Display log. */
6663 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6664 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6665 lua_pop(hlua->T, 1);
6666 goto error;
6667
6668 case HLUA_E_ETMOUT:
6669 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6670 rule->arg.hlua_rule->fcn.name);
6671 goto error;
6672
6673 case HLUA_E_NOMEM:
6674 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6675 rule->arg.hlua_rule->fcn.name);
6676 goto error;
6677
6678 case HLUA_E_YIELD: /* unexpected */
6679 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6680 rule->arg.hlua_rule->fcn.name);
6681 goto error;
6682
6683 case HLUA_E_ERR:
6684 /* Display log. */
6685 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6686 rule->arg.hlua_rule->fcn.name);
6687 goto error;
6688
6689 default:
6690 goto error;
6691 }
6692 }
6693
6694 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006695 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
6696 goto done;
6697
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006698 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
6699 goto error;
6700
Christopher Faulet54b5e212019-06-04 10:08:28 +02006701 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006702 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
6703 si_rx_room_blk(si);
6704 goto out;
6705 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006706 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006707 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6708 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006709 }
6710
6711 done:
6712 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006713 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006714 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01006715 si_shutr(si);
6716 }
6717
6718 /* eat the whole request */
6719 if (co_data(req)) {
6720 req_htx = htx_from_buf(&req->buf);
6721 co_htx_skip(req, req_htx, co_data(req));
6722 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006723 }
6724 }
6725
6726 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006727 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006728 return;
6729
6730 error:
6731
6732 /* If we are in HTTP mode, and we are not send any
6733 * data, return a 500 server error in best effort:
6734 * if there is no room available in the buffer,
6735 * just close the connection.
6736 */
6737 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
6738 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
6739
6740 channel_erase(res);
6741 res->buf.data = b_data(err);
6742 memcpy(res->buf.area, b_head(err), b_data(err));
6743 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01006744 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01006745 }
6746 if (!(strm->flags & SF_ERR_MASK))
6747 strm->flags |= SF_ERR_RESOURCE;
6748 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6749 goto done;
6750}
6751
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006752static void hlua_applet_http_release(struct appctx *ctx)
6753{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006754 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006755 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006756 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006757 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006758}
6759
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006760/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6761 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006762 *
6763 * This function can fail with an abort() due to an Lua critical error.
6764 * We are in the configuration parsing process of HAProxy, this abort() is
6765 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006766 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006767static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6768 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006769{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006770 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006771 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006772
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006773 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006774 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006775 if (!rule->arg.hlua_rule) {
6776 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006777 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006778 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006779
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006780 /* Memory for arguments. */
6781 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6782 if (!rule->arg.hlua_rule->args) {
6783 memprintf(err, "out of memory error");
6784 return ACT_RET_PRS_ERR;
6785 }
6786
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006787 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006788 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006789
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006790 /* Expect some arguments */
6791 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006792 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006793 memprintf(err, "expect %d arguments", fcn->nargs);
6794 return ACT_RET_PRS_ERR;
6795 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01006796 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006797 if (!rule->arg.hlua_rule->args[i]) {
6798 memprintf(err, "out of memory error");
6799 return ACT_RET_PRS_ERR;
6800 }
6801 (*cur_arg)++;
6802 }
6803 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006804
Thierry FOURNIER42148732015-09-02 17:17:33 +02006805 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006806 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006807 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006808}
6809
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006810static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6811 struct act_rule *rule, char **err)
6812{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006813 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006814
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006815 /* HTTP applets are forbidden in tcp-request rules.
6816 * HTTP applet request requires everything initilized by
6817 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6818 * The applet will be immediately initilized, but its before
6819 * the call of this analyzer.
6820 */
6821 if (rule->from != ACT_F_HTTP_REQ) {
6822 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6823 return ACT_RET_PRS_ERR;
6824 }
6825
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006826 /* Memory for the rule. */
6827 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6828 if (!rule->arg.hlua_rule) {
6829 memprintf(err, "out of memory error");
6830 return ACT_RET_PRS_ERR;
6831 }
6832
6833 /* Reference the Lua function and store the reference. */
6834 rule->arg.hlua_rule->fcn = *fcn;
6835
6836 /* TODO: later accept arguments. */
6837 rule->arg.hlua_rule->args = NULL;
6838
6839 /* Add applet pointer in the rule. */
6840 rule->applet.obj_type = OBJ_TYPE_APPLET;
6841 rule->applet.name = fcn->name;
6842 rule->applet.init = hlua_applet_http_init;
6843 rule->applet.fct = hlua_applet_http_fct;
6844 rule->applet.release = hlua_applet_http_release;
6845 rule->applet.timeout = hlua_timeout_applet;
6846
6847 return ACT_RET_PRS_OK;
6848}
6849
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006850/* This function is an LUA binding used for registering
6851 * "sample-conv" functions. It expects a converter name used
6852 * in the haproxy configuration file, and an LUA function.
6853 */
6854__LJMP static int hlua_register_action(lua_State *L)
6855{
6856 struct action_kw_list *akl;
6857 const char *name;
6858 int ref;
6859 int len;
6860 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006861 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006862
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006863 /* Initialise the number of expected arguments at 0. */
6864 nargs = 0;
6865
6866 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6867 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006868
6869 /* First argument : converter name. */
6870 name = MAY_LJMP(luaL_checkstring(L, 1));
6871
6872 /* Second argument : environment. */
6873 if (lua_type(L, 2) != LUA_TTABLE)
6874 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6875
6876 /* Third argument : lua function. */
6877 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6878
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006879 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006880 if (lua_gettop(L) >= 4)
6881 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6882
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006883 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006884 lua_pushnil(L);
6885 while (lua_next(L, 2) != 0) {
6886 if (lua_type(L, -1) != LUA_TSTRING)
6887 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6888
6889 /* Check required environment. Only accepted "http" or "tcp". */
6890 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006891 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006892 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006893 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006894 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006895 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006896 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006897
6898 /* Fill fcn. */
6899 fcn->name = strdup(name);
6900 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006901 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006902 fcn->function_ref = ref;
6903
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006904 /* Set the expected number od arguments. */
6905 fcn->nargs = nargs;
6906
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006907 /* List head */
6908 akl->list.n = akl->list.p = NULL;
6909
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006910 /* action keyword. */
6911 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006912 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006913 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006914 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006915
6916 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6917
6918 akl->kw[0].match_pfx = 0;
6919 akl->kw[0].private = fcn;
6920 akl->kw[0].parse = action_register_lua;
6921
6922 /* select the action registering point. */
6923 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6924 tcp_req_cont_keywords_register(akl);
6925 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6926 tcp_res_cont_keywords_register(akl);
6927 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6928 http_req_keywords_register(akl);
6929 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6930 http_res_keywords_register(akl);
6931 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006932 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006933 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6934 "are expected.", lua_tostring(L, -1)));
6935
6936 /* pop the environment string. */
6937 lua_pop(L, 1);
6938 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006939 return ACT_RET_PRS_OK;
6940}
6941
6942static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6943 struct act_rule *rule, char **err)
6944{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006945 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006946
Christopher Faulet280f85b2019-07-15 15:02:04 +02006947 if (px->mode == PR_MODE_HTTP) {
6948 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01006949 return ACT_RET_PRS_ERR;
6950 }
6951
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006952 /* Memory for the rule. */
6953 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6954 if (!rule->arg.hlua_rule) {
6955 memprintf(err, "out of memory error");
6956 return ACT_RET_PRS_ERR;
6957 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006958
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006959 /* Reference the Lua function and store the reference. */
6960 rule->arg.hlua_rule->fcn = *fcn;
6961
6962 /* TODO: later accept arguments. */
6963 rule->arg.hlua_rule->args = NULL;
6964
6965 /* Add applet pointer in the rule. */
6966 rule->applet.obj_type = OBJ_TYPE_APPLET;
6967 rule->applet.name = fcn->name;
6968 rule->applet.init = hlua_applet_tcp_init;
6969 rule->applet.fct = hlua_applet_tcp_fct;
6970 rule->applet.release = hlua_applet_tcp_release;
6971 rule->applet.timeout = hlua_timeout_applet;
6972
6973 return 0;
6974}
6975
6976/* This function is an LUA binding used for registering
6977 * "sample-conv" functions. It expects a converter name used
6978 * in the haproxy configuration file, and an LUA function.
6979 */
6980__LJMP static int hlua_register_service(lua_State *L)
6981{
6982 struct action_kw_list *akl;
6983 const char *name;
6984 const char *env;
6985 int ref;
6986 int len;
6987 struct hlua_function *fcn;
6988
6989 MAY_LJMP(check_args(L, 3, "register_service"));
6990
6991 /* First argument : converter name. */
6992 name = MAY_LJMP(luaL_checkstring(L, 1));
6993
6994 /* Second argument : environment. */
6995 env = MAY_LJMP(luaL_checkstring(L, 2));
6996
6997 /* Third argument : lua function. */
6998 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6999
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007000 /* Allocate and fill the sample fetch keyword struct. */
7001 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7002 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007003 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007004 fcn = calloc(1, sizeof(*fcn));
7005 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007006 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007007
7008 /* Fill fcn. */
7009 len = strlen("<lua.>") + strlen(name) + 1;
7010 fcn->name = calloc(1, len);
7011 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007012 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007013 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7014 fcn->function_ref = ref;
7015
7016 /* List head */
7017 akl->list.n = akl->list.p = NULL;
7018
7019 /* converter keyword. */
7020 len = strlen("lua.") + strlen(name) + 1;
7021 akl->kw[0].kw = calloc(1, len);
7022 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007023 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007024
7025 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7026
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007027 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007028 if (strcmp(env, "tcp") == 0)
7029 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007030 else if (strcmp(env, "http") == 0)
7031 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007032 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007033 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007034 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007035
7036 akl->kw[0].match_pfx = 0;
7037 akl->kw[0].private = fcn;
7038
7039 /* End of array. */
7040 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7041
7042 /* Register this new converter */
7043 service_keywords_register(akl);
7044
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007045 return 0;
7046}
7047
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007048/* This function initialises Lua cli handler. It copies the
7049 * arguments in the Lua stack and create channel IO objects.
7050 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007051static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007052{
7053 struct hlua *hlua;
7054 struct hlua_function *fcn;
7055 int i;
7056 const char *error;
7057
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007058 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007059 appctx->ctx.hlua_cli.fcn = private;
7060
Willy Tarreaubafbe012017-11-24 17:34:44 +01007061 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007062 if (!hlua) {
7063 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007064 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007065 }
7066 HLUA_INIT(hlua);
7067 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007068
7069 /* Create task used by signal to wakeup applets.
7070 * We use the same wakeup fonction than the Lua applet_tcp and
7071 * applet_http. It is absolutely compatible.
7072 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007073 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007074 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007075 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007076 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007077 }
7078 appctx->ctx.hlua_cli.task->nice = 0;
7079 appctx->ctx.hlua_cli.task->context = appctx;
7080 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7081
7082 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007083 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007084 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007085 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007086 }
7087
7088 /* The following Lua calls can fail. */
7089 if (!SET_SAFE_LJMP(hlua->T)) {
7090 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7091 error = lua_tostring(hlua->T, -1);
7092 else
7093 error = "critical error";
7094 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7095 goto error;
7096 }
7097
7098 /* Check stack available size. */
7099 if (!lua_checkstack(hlua->T, 2)) {
7100 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7101 goto error;
7102 }
7103
7104 /* Restore the function in the stack. */
7105 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7106
7107 /* Once the arguments parsed, the CLI is like an AppletTCP,
7108 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007109 */
7110 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7111 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7112 goto error;
7113 }
7114 hlua->nargs = 1;
7115
7116 /* push keywords in the stack. */
7117 for (i = 0; *args[i]; i++) {
7118 /* Check stack available size. */
7119 if (!lua_checkstack(hlua->T, 1)) {
7120 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7121 goto error;
7122 }
7123 lua_pushstring(hlua->T, args[i]);
7124 hlua->nargs++;
7125 }
7126
7127 /* We must initialize the execution timeouts. */
7128 hlua->max_time = hlua_timeout_session;
7129
7130 /* At this point the execution is safe. */
7131 RESET_SAFE_LJMP(hlua->T);
7132
7133 /* It's ok */
7134 return 0;
7135
7136 /* It's not ok. */
7137error:
7138 RESET_SAFE_LJMP(hlua->T);
7139 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007140 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007141 return 1;
7142}
7143
7144static int hlua_cli_io_handler_fct(struct appctx *appctx)
7145{
7146 struct hlua *hlua;
7147 struct stream_interface *si;
7148 struct hlua_function *fcn;
7149
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007150 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007151 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007152 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007153
7154 /* If the stream is disconnect or closed, ldo nothing. */
7155 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7156 return 1;
7157
7158 /* Execute the function. */
7159 switch (hlua_ctx_resume(hlua, 1)) {
7160
7161 /* finished. */
7162 case HLUA_E_OK:
7163 return 1;
7164
7165 /* yield. */
7166 case HLUA_E_AGAIN:
7167 /* We want write. */
7168 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007169 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007170 /* Set the timeout. */
7171 if (hlua->wake_time != TICK_ETERNITY)
7172 task_schedule(hlua->task, hlua->wake_time);
7173 return 0;
7174
7175 /* finished with error. */
7176 case HLUA_E_ERRMSG:
7177 /* Display log. */
7178 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7179 fcn->name, lua_tostring(hlua->T, -1));
7180 lua_pop(hlua->T, 1);
7181 return 1;
7182
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007183 case HLUA_E_ETMOUT:
7184 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7185 fcn->name);
7186 return 1;
7187
7188 case HLUA_E_NOMEM:
7189 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7190 fcn->name);
7191 return 1;
7192
7193 case HLUA_E_YIELD: /* unexpected */
7194 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7195 fcn->name);
7196 return 1;
7197
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007198 case HLUA_E_ERR:
7199 /* Display log. */
7200 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7201 fcn->name);
7202 return 1;
7203
7204 default:
7205 return 1;
7206 }
7207
7208 return 1;
7209}
7210
7211static void hlua_cli_io_release_fct(struct appctx *appctx)
7212{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007213 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007214 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007215}
7216
7217/* This function is an LUA binding used for registering
7218 * new keywords in the cli. It expects a list of keywords
7219 * which are the "path". It is limited to 5 keywords. A
7220 * description of the command, a function to be executed
7221 * for the parsing and a function for io handlers.
7222 */
7223__LJMP static int hlua_register_cli(lua_State *L)
7224{
7225 struct cli_kw_list *cli_kws;
7226 const char *message;
7227 int ref_io;
7228 int len;
7229 struct hlua_function *fcn;
7230 int index;
7231 int i;
7232
7233 MAY_LJMP(check_args(L, 3, "register_cli"));
7234
7235 /* First argument : an array of maximum 5 keywords. */
7236 if (!lua_istable(L, 1))
7237 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7238
7239 /* Second argument : string with contextual message. */
7240 message = MAY_LJMP(luaL_checkstring(L, 2));
7241
7242 /* Third and fourth argument : lua function. */
7243 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7244
7245 /* Allocate and fill the sample fetch keyword struct. */
7246 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7247 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007248 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007249 fcn = calloc(1, sizeof(*fcn));
7250 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007251 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007252
7253 /* Fill path. */
7254 index = 0;
7255 lua_pushnil(L);
7256 while(lua_next(L, 1) != 0) {
7257 if (index >= 5)
7258 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7259 if (lua_type(L, -1) != LUA_TSTRING)
7260 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7261 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7262 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007263 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007264 index++;
7265 lua_pop(L, 1);
7266 }
7267
7268 /* Copy help message. */
7269 cli_kws->kw[0].usage = strdup(message);
7270 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007271 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007272
7273 /* Fill fcn io handler. */
7274 len = strlen("<lua.cli>") + 1;
7275 for (i = 0; i < index; i++)
7276 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7277 fcn->name = calloc(1, len);
7278 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007279 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007280 strncat((char *)fcn->name, "<lua.cli", len);
7281 for (i = 0; i < index; i++) {
7282 strncat((char *)fcn->name, ".", len);
7283 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7284 }
7285 strncat((char *)fcn->name, ">", len);
7286 fcn->function_ref = ref_io;
7287
7288 /* Fill last entries. */
7289 cli_kws->kw[0].private = fcn;
7290 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7291 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7292 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7293
7294 /* Register this new converter */
7295 cli_register_kw(cli_kws);
7296
7297 return 0;
7298}
7299
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007300static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7301 struct proxy *defpx, const char *file, int line,
7302 char **err, unsigned int *timeout)
7303{
7304 const char *error;
7305
7306 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007307 if (error == PARSE_TIME_OVER) {
7308 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7309 args[1], args[0]);
7310 return -1;
7311 }
7312 else if (error == PARSE_TIME_UNDER) {
7313 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7314 args[1], args[0]);
7315 return -1;
7316 }
7317 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007318 memprintf(err, "%s: invalid timeout", args[0]);
7319 return -1;
7320 }
7321 return 0;
7322}
7323
7324static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7325 struct proxy *defpx, const char *file, int line,
7326 char **err)
7327{
7328 return hlua_read_timeout(args, section_type, curpx, defpx,
7329 file, line, err, &hlua_timeout_session);
7330}
7331
7332static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7333 struct proxy *defpx, const char *file, int line,
7334 char **err)
7335{
7336 return hlua_read_timeout(args, section_type, curpx, defpx,
7337 file, line, err, &hlua_timeout_task);
7338}
7339
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007340static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7341 struct proxy *defpx, const char *file, int line,
7342 char **err)
7343{
7344 return hlua_read_timeout(args, section_type, curpx, defpx,
7345 file, line, err, &hlua_timeout_applet);
7346}
7347
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007348static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7349 struct proxy *defpx, const char *file, int line,
7350 char **err)
7351{
7352 char *error;
7353
7354 hlua_nb_instruction = strtoll(args[1], &error, 10);
7355 if (*error != '\0') {
7356 memprintf(err, "%s: invalid number", args[0]);
7357 return -1;
7358 }
7359 return 0;
7360}
7361
Willy Tarreau32f61e22015-03-18 17:54:59 +01007362static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7363 struct proxy *defpx, const char *file, int line,
7364 char **err)
7365{
7366 char *error;
7367
7368 if (*(args[1]) == 0) {
7369 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7370 return -1;
7371 }
7372 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7373 if (*error != '\0') {
7374 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7375 return -1;
7376 }
7377 return 0;
7378}
7379
7380
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007381/* This function is called by the main configuration key "lua-load". It loads and
7382 * execute an lua file during the parsing of the HAProxy configuration file. It is
7383 * the main lua entry point.
7384 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007385 * This function runs with the HAProxy keywords API. It returns -1 if an error
7386 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007387 *
7388 * In some error case, LUA set an error message in top of the stack. This function
7389 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007390 *
7391 * This function can fail with an abort() due to an Lua critical error.
7392 * We are in the configuration parsing process of HAProxy, this abort() is
7393 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007394 */
7395static int hlua_load(char **args, int section_type, struct proxy *curpx,
7396 struct proxy *defpx, const char *file, int line,
7397 char **err)
7398{
7399 int error;
7400
7401 /* Just load and compile the file. */
7402 error = luaL_loadfile(gL.T, args[1]);
7403 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007404 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007405 lua_pop(gL.T, 1);
7406 return -1;
7407 }
7408
7409 /* If no syntax error where detected, execute the code. */
7410 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7411 switch (error) {
7412 case LUA_OK:
7413 break;
7414 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007415 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007416 lua_pop(gL.T, 1);
7417 return -1;
7418 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007419 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007420 return -1;
7421 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007422 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007423 lua_pop(gL.T, 1);
7424 return -1;
7425 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007426 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007427 lua_pop(gL.T, 1);
7428 return -1;
7429 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007430 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007431 lua_pop(gL.T, 1);
7432 return -1;
7433 }
7434
7435 return 0;
7436}
7437
7438/* configuration keywords declaration */
7439static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007440 { CFG_GLOBAL, "lua-load", hlua_load },
7441 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7442 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007443 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007444 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007445 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007446 { 0, NULL, NULL },
7447}};
7448
Willy Tarreau0108d902018-11-25 19:14:37 +01007449INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7450
Christopher Fauletafd8f102018-11-08 11:34:21 +01007451
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007452/* This function can fail with an abort() due to an Lua critical error.
7453 * We are in the initialisation process of HAProxy, this abort() is
7454 * tolerated.
7455 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007456int hlua_post_init()
7457{
7458 struct hlua_init_function *init;
7459 const char *msg;
7460 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007461 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007462
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007463 /* Call post initialisation function in safe environement. */
7464 if (!SET_SAFE_LJMP(gL.T)) {
7465 if (lua_type(gL.T, -1) == LUA_TSTRING)
7466 error = lua_tostring(gL.T, -1);
7467 else
7468 error = "critical error";
7469 fprintf(stderr, "Lua post-init: %s.\n", error);
7470 exit(1);
7471 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007472
7473#if USE_OPENSSL
7474 /* Initialize SSL server. */
7475 if (socket_ssl.xprt->prepare_srv) {
7476 int saved_used_backed = global.ssl_used_backend;
7477 // don't affect maxconn automatic computation
7478 socket_ssl.xprt->prepare_srv(&socket_ssl);
7479 global.ssl_used_backend = saved_used_backed;
7480 }
7481#endif
7482
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007483 hlua_fcn_post_init(gL.T);
7484 RESET_SAFE_LJMP(gL.T);
7485
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007486 list_for_each_entry(init, &hlua_init_functions, l) {
7487 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7488 ret = hlua_ctx_resume(&gL, 0);
7489 switch (ret) {
7490 case HLUA_E_OK:
7491 lua_pop(gL.T, -1);
7492 return 1;
7493 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007494 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007495 return 0;
7496 case HLUA_E_ERRMSG:
7497 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007498 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007499 return 0;
7500 case HLUA_E_ERR:
7501 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007502 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007503 return 0;
7504 }
7505 }
7506 return 1;
7507}
7508
Willy Tarreau32f61e22015-03-18 17:54:59 +01007509/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7510 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7511 * is the previously allocated size or the kind of object in case of a new
7512 * allocation. <nsize> is the requested new size.
7513 */
7514static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7515{
7516 struct hlua_mem_allocator *zone = ud;
7517
7518 if (nsize == 0) {
7519 /* it's a free */
7520 if (ptr)
7521 zone->allocated -= osize;
7522 free(ptr);
7523 return NULL;
7524 }
7525
7526 if (!ptr) {
7527 /* it's a new allocation */
7528 if (zone->limit && zone->allocated + nsize > zone->limit)
7529 return NULL;
7530
7531 ptr = malloc(nsize);
7532 if (ptr)
7533 zone->allocated += nsize;
7534 return ptr;
7535 }
7536
7537 /* it's a realloc */
7538 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7539 return NULL;
7540
7541 ptr = realloc(ptr, nsize);
7542 if (ptr)
7543 zone->allocated += nsize - osize;
7544 return ptr;
7545}
7546
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007547/* Ithis function can fail with an abort() due to an Lua critical error.
7548 * We are in the initialisation process of HAProxy, this abort() is
7549 * tolerated.
7550 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007551void hlua_init(void)
7552{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007553 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007554 int idx;
7555 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007556 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007557 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007558 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007559#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007560 struct srv_kw *kw;
7561 int tmp_error;
7562 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007563 char *args[] = { /* SSL client configuration. */
7564 "ssl",
7565 "verify",
7566 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007567 NULL
7568 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007569#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007570
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007571 /* Init main lua stack. */
7572 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007573 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007574 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007575 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007576 hlua_sethlua(&gL);
7577 gL.Tref = LUA_REFNIL;
7578 gL.task = NULL;
7579
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007580 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007581 * the Lua function can fail with an abort. We are in the initialisation
7582 * process of HAProxy, this abort() is tolerated.
7583 */
7584
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007585 /* Initialise lua. */
7586 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007587
Thierry Fournier75933d42016-01-21 09:30:18 +01007588 /* Set safe environment for the initialisation. */
7589 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007590 if (lua_type(gL.T, -1) == LUA_TSTRING)
7591 error_msg = lua_tostring(gL.T, -1);
7592 else
7593 error_msg = "critical error";
7594 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007595 exit(1);
7596 }
7597
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007598 /*
7599 *
7600 * Create "core" object.
7601 *
7602 */
7603
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007604 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007605 lua_newtable(gL.T);
7606
7607 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007608 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007609 hlua_class_const_int(gL.T, log_levels[i], i);
7610
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007611 /* Register special functions. */
7612 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007613 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007614 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007615 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007616 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007617 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007618 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007619 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007620 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007621 hlua_class_function(gL.T, "sleep", hlua_sleep);
7622 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007623 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7624 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7625 hlua_class_function(gL.T, "set_map", hlua_set_map);
7626 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007627 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007628 hlua_class_function(gL.T, "log", hlua_log);
7629 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7630 hlua_class_function(gL.T, "Info", hlua_log_info);
7631 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7632 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007633 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007634 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007635
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007636 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007637
7638 /*
7639 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007640 * Register class Map
7641 *
7642 */
7643
7644 /* This table entry is the object "Map" base. */
7645 lua_newtable(gL.T);
7646
7647 /* register pattern types. */
7648 for (i=0; i<PAT_MATCH_NUM; i++)
7649 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007650 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007651 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
7652 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007653 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007654
7655 /* register constructor. */
7656 hlua_class_function(gL.T, "new", hlua_map_new);
7657
7658 /* Create and fill the metatable. */
7659 lua_newtable(gL.T);
7660
7661 /* Create and fille the __index entry. */
7662 lua_pushstring(gL.T, "__index");
7663 lua_newtable(gL.T);
7664
7665 /* Register . */
7666 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7667 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7668
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007669 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007670
Thierry Fournier45e78d72016-02-19 18:34:46 +01007671 /* Register previous table in the registry with reference and named entry.
7672 * The function hlua_register_metatable() pops the stack, so we
7673 * previously create a copy of the table.
7674 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007675 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007676 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007677
7678 /* Assign the metatable to the mai Map object. */
7679 lua_setmetatable(gL.T, -2);
7680
7681 /* Set a name to the table. */
7682 lua_setglobal(gL.T, "Map");
7683
7684 /*
7685 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007686 * Register class Channel
7687 *
7688 */
7689
7690 /* Create and fill the metatable. */
7691 lua_newtable(gL.T);
7692
7693 /* Create and fille the __index entry. */
7694 lua_pushstring(gL.T, "__index");
7695 lua_newtable(gL.T);
7696
7697 /* Register . */
7698 hlua_class_function(gL.T, "get", hlua_channel_get);
7699 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7700 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7701 hlua_class_function(gL.T, "set", hlua_channel_set);
7702 hlua_class_function(gL.T, "append", hlua_channel_append);
7703 hlua_class_function(gL.T, "send", hlua_channel_send);
7704 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7705 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7706 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007707 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007708
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007709 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007710
7711 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007712 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007713
7714 /*
7715 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007716 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007717 *
7718 */
7719
7720 /* Create and fill the metatable. */
7721 lua_newtable(gL.T);
7722
7723 /* Create and fille the __index entry. */
7724 lua_pushstring(gL.T, "__index");
7725 lua_newtable(gL.T);
7726
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007727 /* Browse existing fetches and create the associated
7728 * object method.
7729 */
7730 sf = NULL;
7731 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7732
7733 /* Dont register the keywork if the arguments check function are
7734 * not safe during the runtime.
7735 */
7736 if ((sf->val_args != NULL) &&
7737 (sf->val_args != val_payload_lv) &&
7738 (sf->val_args != val_hdr))
7739 continue;
7740
7741 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7742 * by an underscore.
7743 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007744 strncpy(trash.area, sf->kw, trash.size);
7745 trash.area[trash.size - 1] = '\0';
7746 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007747 if (*p == '.' || *p == '-' || *p == '+')
7748 *p = '_';
7749
7750 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007751 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007752 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007753 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007754 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007755 }
7756
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007757 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007758
7759 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007760 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007761
7762 /*
7763 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007764 * Register class Converters
7765 *
7766 */
7767
7768 /* Create and fill the metatable. */
7769 lua_newtable(gL.T);
7770
7771 /* Create and fill the __index entry. */
7772 lua_pushstring(gL.T, "__index");
7773 lua_newtable(gL.T);
7774
7775 /* Browse existing converters and create the associated
7776 * object method.
7777 */
7778 sc = NULL;
7779 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7780 /* Dont register the keywork if the arguments check function are
7781 * not safe during the runtime.
7782 */
7783 if (sc->val_args != NULL)
7784 continue;
7785
7786 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7787 * by an underscore.
7788 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007789 strncpy(trash.area, sc->kw, trash.size);
7790 trash.area[trash.size - 1] = '\0';
7791 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007792 if (*p == '.' || *p == '-' || *p == '+')
7793 *p = '_';
7794
7795 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007796 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007797 lua_pushlightuserdata(gL.T, sc);
7798 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007799 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007800 }
7801
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007802 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007803
7804 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007805 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007806
7807 /*
7808 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007809 * Register class HTTP
7810 *
7811 */
7812
7813 /* Create and fill the metatable. */
7814 lua_newtable(gL.T);
7815
7816 /* Create and fille the __index entry. */
7817 lua_pushstring(gL.T, "__index");
7818 lua_newtable(gL.T);
7819
7820 /* Register Lua functions. */
7821 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7822 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7823 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7824 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7825 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7826 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7827 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7828 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7829 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7830 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7831
7832 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7833 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7834 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7835 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7836 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7837 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007838 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007839
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007840 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007841
7842 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007843 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007844
7845 /*
7846 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007847 * Register class AppletTCP
7848 *
7849 */
7850
7851 /* Create and fill the metatable. */
7852 lua_newtable(gL.T);
7853
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007854 /* Create and fille the __index entry. */
7855 lua_pushstring(gL.T, "__index");
7856 lua_newtable(gL.T);
7857
7858 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007859 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7860 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7861 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7862 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7863 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007864 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7865 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7866 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007867
7868 lua_settable(gL.T, -3);
7869
7870 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007871 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007872
7873 /*
7874 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007875 * Register class AppletHTTP
7876 *
7877 */
7878
7879 /* Create and fill the metatable. */
7880 lua_newtable(gL.T);
7881
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007882 /* Create and fille the __index entry. */
7883 lua_pushstring(gL.T, "__index");
7884 lua_newtable(gL.T);
7885
7886 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007887 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7888 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007889 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7890 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7891 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007892 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7893 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7894 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7895 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7896 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7897 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7898
7899 lua_settable(gL.T, -3);
7900
7901 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007902 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007903
7904 /*
7905 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007906 * Register class TXN
7907 *
7908 */
7909
7910 /* Create and fill the metatable. */
7911 lua_newtable(gL.T);
7912
7913 /* Create and fille the __index entry. */
7914 lua_pushstring(gL.T, "__index");
7915 lua_newtable(gL.T);
7916
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007917 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04007918 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7919 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
7920 hlua_class_function(gL.T, "set_var", hlua_set_var);
7921 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
7922 hlua_class_function(gL.T, "get_var", hlua_get_var);
7923 hlua_class_function(gL.T, "done", hlua_txn_done);
7924 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
7925 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7926 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
7927 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
7928 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
7929 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7930 hlua_class_function(gL.T, "log", hlua_txn_log);
7931 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7932 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7933 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7934 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007935
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007936 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007937
7938 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007939 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007940
7941 /*
7942 *
7943 * Register class Socket
7944 *
7945 */
7946
7947 /* Create and fill the metatable. */
7948 lua_newtable(gL.T);
7949
7950 /* Create and fille the __index entry. */
7951 lua_pushstring(gL.T, "__index");
7952 lua_newtable(gL.T);
7953
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007954#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007955 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007956#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007957 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7958 hlua_class_function(gL.T, "send", hlua_socket_send);
7959 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7960 hlua_class_function(gL.T, "close", hlua_socket_close);
7961 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7962 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7963 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7964 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7965
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007966 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007967
7968 /* Register the garbage collector entry. */
7969 lua_pushstring(gL.T, "__gc");
7970 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007971 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007972
7973 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007974 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007975
7976 /* Proxy and server configuration initialisation. */
7977 memset(&socket_proxy, 0, sizeof(socket_proxy));
7978 init_new_proxy(&socket_proxy);
7979 socket_proxy.parent = NULL;
7980 socket_proxy.last_change = now.tv_sec;
7981 socket_proxy.id = "LUA-SOCKET";
7982 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7983 socket_proxy.maxconn = 0;
7984 socket_proxy.accept = NULL;
7985 socket_proxy.options2 |= PR_O2_INDEPSTR;
7986 socket_proxy.srv = NULL;
7987 socket_proxy.conn_retries = 0;
7988 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7989
7990 /* Init TCP server: unchanged parameters */
7991 memset(&socket_tcp, 0, sizeof(socket_tcp));
7992 socket_tcp.next = NULL;
7993 socket_tcp.proxy = &socket_proxy;
7994 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7995 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04007996 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02007997 socket_tcp.priv_conns = NULL;
7998 socket_tcp.idle_conns = NULL;
7999 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008000 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008001 socket_tcp.last_change = 0;
8002 socket_tcp.id = "LUA-TCP-CONN";
8003 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8004 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8005 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8006
8007 /* XXX: Copy default parameter from default server,
8008 * but the default server is not initialized.
8009 */
8010 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8011 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8012 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8013 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8014 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8015 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8016 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8017 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8018 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8019 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8020
8021 socket_tcp.check.status = HCHK_STATUS_INI;
8022 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8023 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8024 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8025 socket_tcp.check.server = &socket_tcp;
8026
8027 socket_tcp.agent.status = HCHK_STATUS_INI;
8028 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8029 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8030 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8031 socket_tcp.agent.server = &socket_tcp;
8032
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008033 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008034
8035#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008036 /* Init TCP server: unchanged parameters */
8037 memset(&socket_ssl, 0, sizeof(socket_ssl));
8038 socket_ssl.next = NULL;
8039 socket_ssl.proxy = &socket_proxy;
8040 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8041 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008042 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008043 socket_ssl.priv_conns = NULL;
8044 socket_ssl.idle_conns = NULL;
8045 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008046 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008047 socket_ssl.last_change = 0;
8048 socket_ssl.id = "LUA-SSL-CONN";
8049 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8050 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8051 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8052
8053 /* XXX: Copy default parameter from default server,
8054 * but the default server is not initialized.
8055 */
8056 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8057 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8058 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8059 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8060 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8061 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8062 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8063 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8064 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8065 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8066
8067 socket_ssl.check.status = HCHK_STATUS_INI;
8068 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8069 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8070 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8071 socket_ssl.check.server = &socket_ssl;
8072
8073 socket_ssl.agent.status = HCHK_STATUS_INI;
8074 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8075 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8076 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8077 socket_ssl.agent.server = &socket_ssl;
8078
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008079 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008080 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008081
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008082 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008083 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8084 /*
8085 *
8086 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008087 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008088 * features like client certificates and ssl_verify.
8089 *
8090 */
8091 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8092 if (tmp_error != 0) {
8093 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8094 abort(); /* This must be never arrives because the command line
8095 not editable by the user. */
8096 }
8097 idx += kw->skip;
8098 }
8099 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008100#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008101
8102 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008103}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008104
Willy Tarreau80713382018-11-26 10:19:54 +01008105static void hlua_register_build_options(void)
8106{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008107 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008108
Willy Tarreaubb57d942016-12-21 19:04:56 +01008109 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8110 hap_register_build_opts(ptr, 1);
8111}
Willy Tarreau80713382018-11-26 10:19:54 +01008112
8113INITCALL0(STG_REGISTER, hlua_register_build_options);