blob: bed059ca8be8cad630b7205879daefac70680581 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020014#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016#include <lauxlib.h>
17#include <lua.h>
18#include <lualib.h>
19
Thierry FOURNIER463119c2015-03-10 00:35:36 +010020#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
21#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010022#endif
23
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020024#include <haproxy/api.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020025#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010026
27#include <common/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020028#include <haproxy/connection.h>
Willy Tarreau87735332020-06-04 09:08:41 +020029#include <haproxy/http_htx.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020030#include <haproxy/thread.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau374b4422020-06-02 17:46:16 +020032#include <haproxy/xref.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020033#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020034#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020035#include <haproxy/hlua_fcn.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020036#include <haproxy/http_fetch.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020037#include <haproxy/http_rules.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020038#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020039#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020040#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020041#include <haproxy/payload.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020042#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020043#include <haproxy/session.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020044#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020045#include <haproxy/tcp_rules.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020046#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020047#include <haproxy/vars.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010048
William Lallemand9ed62032016-11-21 17:49:11 +010049#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010050#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010051#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010052
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020053#include <haproxy/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020054#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010055#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/cli.h>
William Lallemand9ed62032016-11-21 17:49:11 +010057#include <proto/stats.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040058#include <proto/queue.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059#include <proto/http_ana.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010060#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020061#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010062#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010063
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010064/* Lua uses longjmp to perform yield or throwing errors. This
65 * macro is used only for identifying the function that can
66 * not return because a longjmp is executed.
67 * __LJMP marks a prototype of hlua file that can use longjmp.
68 * WILL_LJMP() marks an lua function that will use longjmp.
69 * MAY_LJMP() marks an lua function that may use longjmp.
70 */
71#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020072#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010073#define MAY_LJMP(func) func
74
Thierry FOURNIERbabae282015-09-17 11:36:37 +020075/* This couple of function executes securely some Lua calls outside of
76 * the lua runtime environment. Each Lua call can return a longjmp
77 * if it encounter a memory error.
78 *
79 * Lua documentation extract:
80 *
81 * If an error happens outside any protected environment, Lua calls
82 * a panic function (see lua_atpanic) and then calls abort, thus
83 * exiting the host application. Your panic function can avoid this
84 * exit by never returning (e.g., doing a long jump to your own
85 * recovery point outside Lua).
86 *
87 * The panic function runs as if it were a message handler (see
88 * §2.3); in particular, the error message is at the top of the
89 * stack. However, there is no guarantee about stack space. To push
90 * anything on the stack, the panic function must first check the
91 * available space (see §4.2).
92 *
93 * We must check all the Lua entry point. This includes:
94 * - The include/proto/hlua.h exported functions
95 * - the task wrapper function
96 * - The action wrapper function
97 * - The converters wrapper function
98 * - The sample-fetch wrapper functions
99 *
100 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800101 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200102 *
103 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
104 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
105 * because they must be exists in the program stack when the longjmp
106 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200107 *
108 * Note that the Lua processing is not really thread safe. It provides
109 * heavy system which consists to add our own lock function in the Lua
110 * code and recompile the library. This system will probably not accepted
111 * by maintainers of various distribs.
112 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500113 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200114 * quick looking on the Lua sources displays a lua_lock() a the start
115 * of function and a lua_unlock() at the end of the function. So I
116 * conclude that the Lua thread safe mode just perform a mutex around
117 * all execution. So I prefer to do this in the HAProxy code, it will be
118 * easier for distro maintainers.
119 *
120 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
121 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
122 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100124__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200125THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100127static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128
129#define SET_SAFE_LJMP(__L) \
130 ({ \
131 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200133 if (setjmp(safe_ljmp_env) != 0) { \
134 lua_atpanic(__L, hlua_panic_safe); \
135 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100136 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200137 } else { \
138 lua_atpanic(__L, hlua_panic_ljmp); \
139 ret = 1; \
140 } \
141 ret; \
142 })
143
144/* If we are the last function catching Lua errors, we
145 * must reset the panic function.
146 */
147#define RESET_SAFE_LJMP(__L) \
148 do { \
149 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100150 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 } while(0)
152
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200153/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100155/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200157/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100158#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100159#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200160
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100161/* The main Lua execution context. */
162struct hlua gL;
163
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100164/* This is the memory pool containing struct lua for applets
165 * (including cli).
166 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100167DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100168
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100169/* Used for Socket connection. */
170static struct proxy socket_proxy;
171static struct server socket_tcp;
172#ifdef USE_OPENSSL
173static struct server socket_ssl;
174#endif
175
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100176/* List head of the function called at the initialisation time. */
177struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
178
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100179/* The following variables contains the reference of the different
180 * Lua classes. These references are useful for identify metadata
181 * associated with an object.
182 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100183static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100184static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100185static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100186static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100187static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100188static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200189static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200190static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200191static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100192static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100193
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200195 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100196 * short timeout. Lua linked with tasks doesn't have a timeout
197 * because a task may remain alive during all the haproxy execution.
198 */
199static unsigned int hlua_timeout_session = 4000; /* session timeout. */
200static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200201static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100202
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100203/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
204 * it is used for preventing infinite loops.
205 *
206 * I test the scheer with an infinite loop containing one incrementation
207 * and one test. I run this loop between 10 seconds, I raise a ceil of
208 * 710M loops from one interrupt each 9000 instructions, so I fix the value
209 * to one interrupt each 10 000 instructions.
210 *
211 * configured | Number of
212 * instructions | loops executed
213 * between two | in milions
214 * forced yields |
215 * ---------------+---------------
216 * 10 | 160
217 * 500 | 670
218 * 1000 | 680
219 * 5000 | 700
220 * 7000 | 700
221 * 8000 | 700
222 * 9000 | 710 <- ceil
223 * 10000 | 710
224 * 100000 | 710
225 * 1000000 | 710
226 *
227 */
228static unsigned int hlua_nb_instruction = 10000;
229
Willy Tarreau32f61e22015-03-18 17:54:59 +0100230/* Descriptor for the memory allocation state. If limit is not null, it will
231 * be enforced on any memory allocation.
232 */
233struct hlua_mem_allocator {
234 size_t allocated;
235 size_t limit;
236};
237
238static struct hlua_mem_allocator hlua_global_allocator;
239
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100240/* These functions converts types between HAProxy internal args or
241 * sample and LUA types. Another function permits to check if the
242 * LUA stack contains arguments according with an required ARG_T
243 * format.
244 */
245static int hlua_arg2lua(lua_State *L, const struct arg *arg);
246static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100247__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100248 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100249static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100250static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100251static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
252
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100253__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200254
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200255#define SEND_ERR(__be, __fmt, __args...) \
256 do { \
257 send_log(__be, LOG_ERR, __fmt, ## __args); \
258 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100259 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200260 } while (0)
261
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100262/* Used to check an Lua function type in the stack. It creates and
263 * returns a reference of the function. This function throws an
264 * error if the rgument is not a "function".
265 */
266__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
267{
268 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100269 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100270 WILL_LJMP(luaL_argerror(L, argno, msg));
271 }
272 lua_pushvalue(L, argno);
273 return luaL_ref(L, LUA_REGISTRYINDEX);
274}
275
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200276/* Return the string that is of the top of the stack. */
277const char *hlua_get_top_error_string(lua_State *L)
278{
279 if (lua_gettop(L) < 1)
280 return "unknown error";
281 if (lua_type(L, -1) != LUA_TSTRING)
282 return "unknown error";
283 return lua_tostring(L, -1);
284}
285
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200286__LJMP static const char *hlua_traceback(lua_State *L)
287{
288 lua_Debug ar;
289 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200290 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200291 int filled = 0;
292
293 while (lua_getstack(L, level++, &ar)) {
294
295 /* Add separator */
296 if (filled)
297 chunk_appendf(msg, ", ");
298 filled = 1;
299
300 /* Fill fields:
301 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
302 * 'l': fills in the field currentline;
303 * 'n': fills in the field name and namewhat;
304 * 't': fills in the field istailcall;
305 */
306 lua_getinfo(L, "Slnt", &ar);
307
308 /* Append code localisation */
309 if (ar.currentline > 0)
310 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
311 else
312 chunk_appendf(msg, "%s ", ar.short_src);
313
314 /*
315 * Get function name
316 *
317 * if namewhat is no empty, name is defined.
318 * what contains "Lua" for Lua function, "C" for C function,
319 * or "main" for main code.
320 */
321 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
322 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
323
324 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
325 chunk_appendf(msg, "main chunk");
326
327 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
328 chunk_appendf(msg, "C function line %d", ar.linedefined);
329
330 else /* nothing left... */
331 chunk_appendf(msg, "?");
332
333
334 /* Display tailed call */
335 if (ar.istailcall)
336 chunk_appendf(msg, " ...");
337 }
338
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200339 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200340}
341
342
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100343/* This function check the number of arguments available in the
344 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500345 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100346 */
347__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
348{
349 if (lua_gettop(L) == nb)
350 return;
351 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
352}
353
Mark Lakes22154b42018-01-29 14:38:40 -0800354/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100355 * and the line number where the error is encountered.
356 */
357static int hlua_pusherror(lua_State *L, const char *fmt, ...)
358{
359 va_list argp;
360 va_start(argp, fmt);
361 luaL_where(L, 1);
362 lua_pushvfstring(L, fmt, argp);
363 va_end(argp);
364 lua_concat(L, 2);
365 return 1;
366}
367
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100368/* This functions is used with sample fetch and converters. It
369 * converts the HAProxy configuration argument in a lua stack
370 * values.
371 *
372 * It takes an array of "arg", and each entry of the array is
373 * converted and pushed in the LUA stack.
374 */
375static int hlua_arg2lua(lua_State *L, const struct arg *arg)
376{
377 switch (arg->type) {
378 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 case ARGT_TIME:
380 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100381 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100382 break;
383
384 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200385 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100386 break;
387
388 case ARGT_IPV4:
389 case ARGT_IPV6:
390 case ARGT_MSK4:
391 case ARGT_MSK6:
392 case ARGT_FE:
393 case ARGT_BE:
394 case ARGT_TAB:
395 case ARGT_SRV:
396 case ARGT_USR:
397 case ARGT_MAP:
398 default:
399 lua_pushnil(L);
400 break;
401 }
402 return 1;
403}
404
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500405/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100406 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500407 * with sample fetch wrappers. The input arguments are given to the
408 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100409 */
410static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
411{
412 switch (lua_type(L, ud)) {
413
414 case LUA_TNUMBER:
415 case LUA_TBOOLEAN:
416 arg->type = ARGT_SINT;
417 arg->data.sint = lua_tointeger(L, ud);
418 break;
419
420 case LUA_TSTRING:
421 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200422 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200423 /* We don't know the actual size of the underlying allocation, so be conservative. */
424 arg->data.str.size = arg->data.str.data;
425 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100426 break;
427
428 case LUA_TUSERDATA:
429 case LUA_TNIL:
430 case LUA_TTABLE:
431 case LUA_TFUNCTION:
432 case LUA_TTHREAD:
433 case LUA_TLIGHTUSERDATA:
434 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200435 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100436 break;
437 }
438 return 1;
439}
440
441/* the following functions are used to convert a struct sample
442 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500443 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100444 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100445static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200447 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200450 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100451 break;
452
453 case SMP_T_BIN:
454 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200455 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456 break;
457
458 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200459 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100460 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
461 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
462 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
463 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
464 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
465 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
466 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
467 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
468 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200469 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100470 break;
471 default:
472 lua_pushnil(L);
473 break;
474 }
475 break;
476
477 case SMP_T_IPV4:
478 case SMP_T_IPV6:
479 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200480 if (sample_casts[smp->data.type][SMP_T_STR] &&
481 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200482 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100483 else
484 lua_pushnil(L);
485 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100486 default:
487 lua_pushnil(L);
488 break;
489 }
490 return 1;
491}
492
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100493/* the following functions are used to convert a struct sample
494 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500495 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100496 */
497static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
498{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200499 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100500
501 case SMP_T_BIN:
502 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100504 break;
505
506 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200507 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100508 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
509 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
510 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
511 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
512 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
513 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
514 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
515 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
516 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200517 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100518 break;
519 default:
520 lua_pushstring(L, "");
521 break;
522 }
523 break;
524
525 case SMP_T_SINT:
526 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100527 case SMP_T_IPV4:
528 case SMP_T_IPV6:
529 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200530 if (sample_casts[smp->data.type][SMP_T_STR] &&
531 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200532 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100533 else
534 lua_pushstring(L, "");
535 break;
536 default:
537 lua_pushstring(L, "");
538 break;
539 }
540 return 1;
541}
542
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100543/* the following functions are used to convert an Lua type in a
544 * struct sample. This is useful to provide data from a converter
545 * to the LUA code.
546 */
547static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
548{
549 switch (lua_type(L, ud)) {
550
551 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200552 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200553 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100554 break;
555
556
557 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200558 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100560 break;
561
562 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200563 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200565 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200566 /* We don't know the actual size of the underlying allocation, so be conservative. */
567 smp->data.u.str.size = smp->data.u.str.data;
568 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100569 break;
570
571 case LUA_TUSERDATA:
572 case LUA_TNIL:
573 case LUA_TTABLE:
574 case LUA_TFUNCTION:
575 case LUA_TTHREAD:
576 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200577 case LUA_TNONE:
578 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200579 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200580 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100581 break;
582 }
583 return 1;
584}
585
Ilya Shipitsind4259502020-04-08 01:07:56 +0500586/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800587 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100588 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100589 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500590 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100591 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100593__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100594 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595{
596 int min_arg;
597 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100598 struct proxy *px;
599 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100600
601 idx = 0;
602 min_arg = ARGM(mask);
603 mask >>= ARGM_BITS;
604
605 while (1) {
606
607 /* Check oversize. */
608 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100609 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610 }
611
612 /* Check for mandatory arguments. */
613 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100614 if (idx < min_arg) {
615
616 /* If miss other argument than the first one, we return an error. */
617 if (idx > 0)
618 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
619
620 /* If first argument have a certain type, some default values
621 * may be used. See the function smp_resolve_args().
622 */
623 switch (mask & ARGT_MASK) {
624
625 case ARGT_FE:
626 if (!(p->cap & PR_CAP_FE))
627 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
628 argp[idx].data.prx = p;
629 argp[idx].type = ARGT_FE;
630 argp[idx+1].type = ARGT_STOP;
631 break;
632
633 case ARGT_BE:
634 if (!(p->cap & PR_CAP_BE))
635 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
636 argp[idx].data.prx = p;
637 argp[idx].type = ARGT_BE;
638 argp[idx+1].type = ARGT_STOP;
639 break;
640
641 case ARGT_TAB:
642 argp[idx].data.prx = p;
643 argp[idx].type = ARGT_TAB;
644 argp[idx+1].type = ARGT_STOP;
645 break;
646
647 default:
648 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
649 break;
650 }
651 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100652 return 0;
653 }
654
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500655 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100656 if ((mask & ARGT_MASK) == ARGT_STOP &&
657 argp[idx].type != ARGT_STOP) {
658 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
659 }
660
661 if ((mask & ARGT_MASK) == ARGT_STOP &&
662 argp[idx].type == ARGT_STOP) {
663 return 0;
664 }
665
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100666 /* Convert some argument types. */
667 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100668 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 if (argp[idx].type != ARGT_SINT)
670 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
671 argp[idx].type = ARGT_SINT;
672 break;
673
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100674 case ARGT_TIME:
675 if (argp[idx].type != ARGT_SINT)
676 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200677 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100678 break;
679
680 case ARGT_SIZE:
681 if (argp[idx].type != ARGT_SINT)
682 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200683 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100684 break;
685
686 case ARGT_FE:
687 if (argp[idx].type != ARGT_STR)
688 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 memcpy(trash.area, argp[idx].data.str.area,
690 argp[idx].data.str.data);
691 trash.area[argp[idx].data.str.data] = 0;
692 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100693 if (!argp[idx].data.prx)
694 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
695 argp[idx].type = ARGT_FE;
696 break;
697
698 case ARGT_BE:
699 if (argp[idx].type != ARGT_STR)
700 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200701 memcpy(trash.area, argp[idx].data.str.area,
702 argp[idx].data.str.data);
703 trash.area[argp[idx].data.str.data] = 0;
704 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100705 if (!argp[idx].data.prx)
706 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
707 argp[idx].type = ARGT_BE;
708 break;
709
710 case ARGT_TAB:
711 if (argp[idx].type != ARGT_STR)
712 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 memcpy(trash.area, argp[idx].data.str.area,
714 argp[idx].data.str.data);
715 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200716 argp[idx].data.t = stktable_find_by_name(trash.area);
717 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100718 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
719 argp[idx].type = ARGT_TAB;
720 break;
721
722 case ARGT_SRV:
723 if (argp[idx].type != ARGT_STR)
724 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 memcpy(trash.area, argp[idx].data.str.area,
726 argp[idx].data.str.data);
727 trash.area[argp[idx].data.str.data] = 0;
728 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100729 if (sname) {
730 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200732 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100733 if (!px)
734 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
735 }
736 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200737 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100738 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100739 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100740 argp[idx].data.srv = findserver(px, sname);
741 if (!argp[idx].data.srv)
742 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
743 argp[idx].type = ARGT_SRV;
744 break;
745
746 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 memcpy(trash.area, argp[idx].data.str.area,
748 argp[idx].data.str.data);
749 trash.area[argp[idx].data.str.data] = 0;
750 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100751 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
752 argp[idx].type = ARGT_IPV4;
753 break;
754
755 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 memcpy(trash.area, argp[idx].data.str.area,
757 argp[idx].data.str.data);
758 trash.area[argp[idx].data.str.data] = 0;
759 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100760 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
761 argp[idx].type = ARGT_MSK4;
762 break;
763
764 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200765 memcpy(trash.area, argp[idx].data.str.area,
766 argp[idx].data.str.data);
767 trash.area[argp[idx].data.str.data] = 0;
768 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100769 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
770 argp[idx].type = ARGT_IPV6;
771 break;
772
773 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200774 memcpy(trash.area, argp[idx].data.str.area,
775 argp[idx].data.str.data);
776 trash.area[argp[idx].data.str.data] = 0;
777 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100778 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
779 argp[idx].type = ARGT_MSK6;
780 break;
781
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100782 case ARGT_MAP:
783 case ARGT_REG:
784 case ARGT_USR:
785 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100786 break;
787 }
788
789 /* Check for type of argument. */
790 if ((mask & ARGT_MASK) != argp[idx].type) {
791 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
792 arg_type_names[(mask & ARGT_MASK)],
793 arg_type_names[argp[idx].type & ARGT_MASK]);
794 WILL_LJMP(luaL_argerror(L, first + idx, msg));
795 }
796
797 /* Next argument. */
798 mask >>= ARGT_BITS;
799 idx++;
800 }
801}
802
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500804 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100805 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806 *
807 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100808 * - hlua_sethlua : create the association between hlua context and lua_state.
809 */
810static inline struct hlua *hlua_gethlua(lua_State *L)
811{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100812 struct hlua **hlua = lua_getextraspace(L);
813 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100814}
815static inline void hlua_sethlua(struct hlua *hlua)
816{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100817 struct hlua **hlua_store = lua_getextraspace(hlua->T);
818 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100819}
820
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100821/* This function is used to send logs. It try to send on screen (stderr)
822 * and on the default syslog server.
823 */
824static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
825{
826 struct tm tm;
827 char *p;
828
829 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200830 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100831 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200832 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200833 /* Break the message if exceed the buffer size. */
834 *(p-4) = ' ';
835 *(p-3) = '.';
836 *(p-2) = '.';
837 *(p-1) = '.';
838 break;
839 }
Willy Tarreau90807112020-02-25 08:16:33 +0100840 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100841 *p = *msg;
842 else
843 *p = '.';
844 }
845 *p = '\0';
846
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200847 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100848 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200849 get_localtime(date.tv_sec, &tm);
850 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100851 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200852 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100853 fflush(stderr);
854 }
855}
856
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100857/* This function just ensure that the yield will be always
858 * returned with a timeout and permit to set some flags
859 */
860__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100861 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100862{
863 struct hlua *hlua = hlua_gethlua(L);
864
865 /* Set the wake timeout. If timeout is required, we set
866 * the expiration time.
867 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200868 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100869
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100870 hlua->flags |= flags;
871
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100872 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200873 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100874}
875
Willy Tarreau87b09662015-04-03 00:22:06 +0200876/* This function initialises the Lua environment stored in the stream.
877 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100878 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200879 *
880 * This function is particular. it initialises a new Lua thread. If the
881 * initialisation fails (example: out of memory error), the lua function
882 * throws an error (longjmp).
883 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100884 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500885 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100886 * threads appear, the safe environment set a lock to ensure only one
887 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500888 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100889 *
890 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500891 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100892 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800893 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200894 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800895 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500896 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100897 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100898int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100899{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100900 if (!already_safe) {
901 if (!SET_SAFE_LJMP(gL.T)) {
902 lua->Tref = LUA_REFNIL;
903 return 0;
904 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200905 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100906 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100907 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100908 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100909 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100910 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100911 lua->T = lua_newthread(gL.T);
912 if (!lua->T) {
913 lua->Tref = LUA_REFNIL;
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 0;
917 }
918 hlua_sethlua(lua);
919 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
920 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100921 if (!already_safe)
922 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100923 return 1;
924}
925
Willy Tarreau87b09662015-04-03 00:22:06 +0200926/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100927 * is destroyed. The destroy also the memory context. The struct "lua"
928 * is not freed.
929 */
930void hlua_ctx_destroy(struct hlua *lua)
931{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100932 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100933 return;
934
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100935 if (!lua->T)
936 goto end;
937
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100938 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200939 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100940
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200941 if (!SET_SAFE_LJMP(lua->T))
942 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100943 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200944 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200945
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200946 if (!SET_SAFE_LJMP(gL.T))
947 return;
948 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
949 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200950 /* Forces a garbage collecting process. If the Lua program is finished
951 * without error, we run the GC on the thread pointer. Its freed all
952 * the unused memory.
953 * If the thread is finnish with an error or is currently yielded,
954 * it seems that the GC applied on the thread doesn't clean anything,
955 * so e run the GC on the main thread.
956 * NOTE: maybe this action locks all the Lua threads untiml the en of
957 * the garbage collection.
958 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100959 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200960 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200961 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200962 lua_gc(gL.T, LUA_GCCOLLECT, 0);
963 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200964 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200965
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200966 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100967
968end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100969 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100970}
971
972/* This function is used to restore the Lua context when a coroutine
973 * fails. This function copy the common memory between old coroutine
974 * and the new coroutine. The old coroutine is destroyed, and its
975 * replaced by the new coroutine.
976 * If the flag "keep_msg" is set, the last entry of the old is assumed
977 * as string error message and it is copied in the new stack.
978 */
979static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
980{
981 lua_State *T;
982 int new_ref;
983
984 /* Renew the main LUA stack doesn't have sense. */
985 if (lua == &gL)
986 return 0;
987
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100988 /* New Lua coroutine. */
989 T = lua_newthread(gL.T);
990 if (!T)
991 return 0;
992
993 /* Copy last error message. */
994 if (keep_msg)
995 lua_xmove(lua->T, T, 1);
996
997 /* Copy data between the coroutines. */
998 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
999 lua_xmove(lua->T, T, 1);
1000 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1001
1002 /* Destroy old data. */
1003 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1004
1005 /* The thread is garbage collected by Lua. */
1006 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1007
1008 /* Fill the struct with the new coroutine values. */
1009 lua->Mref = new_ref;
1010 lua->T = T;
1011 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1012
1013 /* Set context. */
1014 hlua_sethlua(lua);
1015
1016 return 1;
1017}
1018
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001019void hlua_hook(lua_State *L, lua_Debug *ar)
1020{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001021 struct hlua *hlua = hlua_gethlua(L);
1022
1023 /* Lua cannot yield when its returning from a function,
1024 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001025 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001026 */
1027 if (lua_gethookmask(L) & LUA_MASKRET) {
1028 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1029 return;
1030 }
1031
1032 /* restore the interrupt condition. */
1033 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1034
1035 /* If we interrupt the Lua processing in yieldable state, we yield.
1036 * If the state is not yieldable, trying yield causes an error.
1037 */
1038 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001039 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001040
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001041 /* If we cannot yield, update the clock and check the timeout. */
1042 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001043 hlua->run_time += now_ms - hlua->start_time;
1044 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001045 lua_pushfstring(L, "execution timeout");
1046 WILL_LJMP(lua_error(L));
1047 }
1048
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001049 /* Update the start time. */
1050 hlua->start_time = now_ms;
1051
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001052 /* Try to interrupt the process at the end of the current
1053 * unyieldable function.
1054 */
1055 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001056}
1057
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001058/* This function start or resumes the Lua stack execution. If the flag
1059 * "yield_allowed" if no set and the LUA stack execution returns a yield
1060 * The function return an error.
1061 *
1062 * The function can returns 4 values:
1063 * - HLUA_E_OK : The execution is terminated without any errors.
1064 * - HLUA_E_AGAIN : The execution must continue at the next associated
1065 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001066 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001067 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001068 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001069 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001070 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001071 * LUA code.
1072 */
1073static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1074{
1075 int ret;
1076 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001077 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001078
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001079 /* Initialise run time counter. */
1080 if (!HLUA_IS_RUNNING(lua))
1081 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001082
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001083 /* Lock the whole Lua execution. This lock must be before the
1084 * label "resume_execution".
1085 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001086 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001087
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001088resume_execution:
1089
1090 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1091 * instructions. it is used for preventing infinite loops.
1092 */
1093 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1094
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001095 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001096 HLUA_SET_RUN(lua);
1097 HLUA_CLR_CTRLYIELD(lua);
1098 HLUA_CLR_WAKERESWR(lua);
1099 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001100
Christopher Fauletbc275a92020-02-26 14:55:16 +01001101 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001102 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001103 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001104
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001105 /* Call the function. */
1106 ret = lua_resume(lua->T, gL.T, lua->nargs);
1107 switch (ret) {
1108
1109 case LUA_OK:
1110 ret = HLUA_E_OK;
1111 break;
1112
1113 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001114 /* Check if the execution timeout is expired. It it is the case, we
1115 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001116 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001117 tv_update_date(0, 1);
1118 lua->run_time += now_ms - lua->start_time;
1119 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001120 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001121 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001122 break;
1123 }
1124 /* Process the forced yield. if the general yield is not allowed or
1125 * if no task were associated this the current Lua execution
1126 * coroutine, we resume the execution. Else we want to return in the
1127 * scheduler and we want to be waked up again, to continue the
1128 * current Lua execution. So we schedule our own task.
1129 */
1130 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001131 if (!yield_allowed || !lua->task)
1132 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001133 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001134 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001135 if (!yield_allowed) {
1136 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001137 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001138 break;
1139 }
1140 ret = HLUA_E_AGAIN;
1141 break;
1142
1143 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001144
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001145 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001146 * because the errors ares the only one mean to return immediately
1147 * from and lua execution.
1148 */
1149 if (lua->flags & HLUA_EXIT) {
1150 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001151 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001152 break;
1153 }
1154
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001155 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001156 if (!lua_checkstack(lua->T, 1)) {
1157 ret = HLUA_E_ERR;
1158 break;
1159 }
1160 msg = lua_tostring(lua->T, -1);
1161 lua_settop(lua->T, 0); /* Empty the stack. */
1162 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001163 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001165 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001167 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001168 ret = HLUA_E_ERRMSG;
1169 break;
1170
1171 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001172 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001174 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001175 break;
1176
1177 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001178 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001179 if (!lua_checkstack(lua->T, 1)) {
1180 ret = HLUA_E_ERR;
1181 break;
1182 }
1183 msg = lua_tostring(lua->T, -1);
1184 lua_settop(lua->T, 0); /* Empty the stack. */
1185 lua_pop(lua->T, 1);
1186 if (msg)
1187 lua_pushfstring(lua->T, "message handler error: %s", msg);
1188 else
1189 lua_pushfstring(lua->T, "message handler error");
1190 ret = HLUA_E_ERRMSG;
1191 break;
1192
1193 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001194 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001195 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001196 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001197 break;
1198 }
1199
1200 switch (ret) {
1201 case HLUA_E_AGAIN:
1202 break;
1203
1204 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001205 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001207 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001208 break;
1209
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001210 case HLUA_E_ETMOUT:
1211 case HLUA_E_NOMEM:
1212 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001213 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001214 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001215 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001216 hlua_ctx_renew(lua, 0);
1217 break;
1218
1219 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001220 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001221 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001222 break;
1223 }
1224
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001225 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001226 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001227
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001228 return ret;
1229}
1230
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001231/* This function exit the current code. */
1232__LJMP static int hlua_done(lua_State *L)
1233{
1234 struct hlua *hlua = hlua_gethlua(L);
1235
1236 hlua->flags |= HLUA_EXIT;
1237 WILL_LJMP(lua_error(L));
1238
1239 return 0;
1240}
1241
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001242/* This function is an LUA binding. It provides a function
1243 * for deleting ACL from a referenced ACL file.
1244 */
1245__LJMP static int hlua_del_acl(lua_State *L)
1246{
1247 const char *name;
1248 const char *key;
1249 struct pat_ref *ref;
1250
1251 MAY_LJMP(check_args(L, 2, "del_acl"));
1252
1253 name = MAY_LJMP(luaL_checkstring(L, 1));
1254 key = MAY_LJMP(luaL_checkstring(L, 2));
1255
1256 ref = pat_ref_lookup(name);
1257 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001258 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001259
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001260 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001261 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001262 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001263 return 0;
1264}
1265
1266/* This function is an LUA binding. It provides a function
1267 * for deleting map entry from a referenced map file.
1268 */
1269static int hlua_del_map(lua_State *L)
1270{
1271 const char *name;
1272 const char *key;
1273 struct pat_ref *ref;
1274
1275 MAY_LJMP(check_args(L, 2, "del_map"));
1276
1277 name = MAY_LJMP(luaL_checkstring(L, 1));
1278 key = MAY_LJMP(luaL_checkstring(L, 2));
1279
1280 ref = pat_ref_lookup(name);
1281 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001282 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001283
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001284 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001285 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001286 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001287 return 0;
1288}
1289
1290/* This function is an LUA binding. It provides a function
1291 * for adding ACL pattern from a referenced ACL file.
1292 */
1293static int hlua_add_acl(lua_State *L)
1294{
1295 const char *name;
1296 const char *key;
1297 struct pat_ref *ref;
1298
1299 MAY_LJMP(check_args(L, 2, "add_acl"));
1300
1301 name = MAY_LJMP(luaL_checkstring(L, 1));
1302 key = MAY_LJMP(luaL_checkstring(L, 2));
1303
1304 ref = pat_ref_lookup(name);
1305 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001306 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001307
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001308 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001309 if (pat_ref_find_elt(ref, key) == NULL)
1310 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001311 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001312 return 0;
1313}
1314
1315/* This function is an LUA binding. It provides a function
1316 * for setting map pattern and sample from a referenced map
1317 * file.
1318 */
1319static int hlua_set_map(lua_State *L)
1320{
1321 const char *name;
1322 const char *key;
1323 const char *value;
1324 struct pat_ref *ref;
1325
1326 MAY_LJMP(check_args(L, 3, "set_map"));
1327
1328 name = MAY_LJMP(luaL_checkstring(L, 1));
1329 key = MAY_LJMP(luaL_checkstring(L, 2));
1330 value = MAY_LJMP(luaL_checkstring(L, 3));
1331
1332 ref = pat_ref_lookup(name);
1333 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001334 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001335
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001336 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001337 if (pat_ref_find_elt(ref, key) != NULL)
1338 pat_ref_set(ref, key, value, NULL);
1339 else
1340 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001341 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001342 return 0;
1343}
1344
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001345/* A class is a lot of memory that contain data. This data can be a table,
1346 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001347 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001348 * the name of the object (_G[<name>] = <metable> ).
1349 *
1350 * A metable is a table that modify the standard behavior of a standard
1351 * access to the associated data. The entries of this new metatable are
1352 * defined as is:
1353 *
1354 * http://lua-users.org/wiki/MetatableEvents
1355 *
1356 * __index
1357 *
1358 * we access an absent field in a table, the result is nil. This is
1359 * true, but it is not the whole truth. Actually, such access triggers
1360 * the interpreter to look for an __index metamethod: If there is no
1361 * such method, as usually happens, then the access results in nil;
1362 * otherwise, the metamethod will provide the result.
1363 *
1364 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1365 * the key does not appear in the table, but the metatable has an __index
1366 * property:
1367 *
1368 * - if the value is a function, the function is called, passing in the
1369 * table and the key; the return value of that function is returned as
1370 * the result.
1371 *
1372 * - if the value is another table, the value of the key in that table is
1373 * asked for and returned (and if it doesn't exist in that table, but that
1374 * table's metatable has an __index property, then it continues on up)
1375 *
1376 * - Use "rawget(myTable,key)" to skip this metamethod.
1377 *
1378 * http://www.lua.org/pil/13.4.1.html
1379 *
1380 * __newindex
1381 *
1382 * Like __index, but control property assignment.
1383 *
1384 * __mode - Control weak references. A string value with one or both
1385 * of the characters 'k' and 'v' which specifies that the the
1386 * keys and/or values in the table are weak references.
1387 *
1388 * __call - Treat a table like a function. When a table is followed by
1389 * parenthesis such as "myTable( 'foo' )" and the metatable has
1390 * a __call key pointing to a function, that function is invoked
1391 * (passing any specified arguments) and the return value is
1392 * returned.
1393 *
1394 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1395 * called, if the metatable for myTable has a __metatable
1396 * key, the value of that key is returned instead of the
1397 * actual metatable.
1398 *
1399 * __tostring - Control string representation. When the builtin
1400 * "tostring( myTable )" function is called, if the metatable
1401 * for myTable has a __tostring property set to a function,
1402 * that function is invoked (passing myTable to it) and the
1403 * return value is used as the string representation.
1404 *
1405 * __len - Control table length. When the table length is requested using
1406 * the length operator ( '#' ), if the metatable for myTable has
1407 * a __len key pointing to a function, that function is invoked
1408 * (passing myTable to it) and the return value used as the value
1409 * of "#myTable".
1410 *
1411 * __gc - Userdata finalizer code. When userdata is set to be garbage
1412 * collected, if the metatable has a __gc field pointing to a
1413 * function, that function is first invoked, passing the userdata
1414 * to it. The __gc metamethod is not called for tables.
1415 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1416 *
1417 * Special metamethods for redefining standard operators:
1418 * http://www.lua.org/pil/13.1.html
1419 *
1420 * __add "+"
1421 * __sub "-"
1422 * __mul "*"
1423 * __div "/"
1424 * __unm "!"
1425 * __pow "^"
1426 * __concat ".."
1427 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001428 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001429 * http://www.lua.org/pil/13.2.html
1430 *
1431 * __eq "=="
1432 * __lt "<"
1433 * __le "<="
1434 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001435
1436/*
1437 *
1438 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001439 * Class Map
1440 *
1441 *
1442 */
1443
1444/* Returns a struct hlua_map if the stack entry "ud" is
1445 * a class session, otherwise it throws an error.
1446 */
1447__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1448{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001449 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001450}
1451
1452/* This function is the map constructor. It don't need
1453 * the class Map object. It creates and return a new Map
1454 * object. It must be called only during "body" or "init"
1455 * context because it process some filesystem accesses.
1456 */
1457__LJMP static int hlua_map_new(struct lua_State *L)
1458{
1459 const char *fn;
1460 int match = PAT_MATCH_STR;
1461 struct sample_conv conv;
1462 const char *file = "";
1463 int line = 0;
1464 lua_Debug ar;
1465 char *err = NULL;
1466 struct arg args[2];
1467
1468 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1469 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1470
1471 fn = MAY_LJMP(luaL_checkstring(L, 1));
1472
1473 if (lua_gettop(L) >= 2) {
1474 match = MAY_LJMP(luaL_checkinteger(L, 2));
1475 if (match < 0 || match >= PAT_MATCH_NUM)
1476 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1477 }
1478
1479 /* Get Lua filename and line number. */
1480 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1481 lua_getinfo(L, "Sl", &ar); /* get info about it */
1482 if (ar.currentline > 0) { /* is there info? */
1483 file = ar.short_src;
1484 line = ar.currentline;
1485 }
1486 }
1487
1488 /* fill fake sample_conv struct. */
1489 conv.kw = ""; /* unused. */
1490 conv.process = NULL; /* unused. */
1491 conv.arg_mask = 0; /* unused. */
1492 conv.val_args = NULL; /* unused. */
1493 conv.out_type = SMP_T_STR;
1494 conv.private = (void *)(long)match;
1495 switch (match) {
1496 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1497 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1498 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1499 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1500 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1501 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1502 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001503 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001504 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1505 default:
1506 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1507 }
1508
1509 /* fill fake args. */
1510 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001511 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001512 args[1].type = ARGT_STOP;
1513
1514 /* load the map. */
1515 if (!sample_load_map(args, &conv, file, line, &err)) {
1516 /* error case: we cant use luaL_error because we must
1517 * free the err variable.
1518 */
1519 luaL_where(L, 1);
1520 lua_pushfstring(L, "'new': %s.", err);
1521 lua_concat(L, 2);
1522 free(err);
1523 WILL_LJMP(lua_error(L));
1524 }
1525
1526 /* create the lua object. */
1527 lua_newtable(L);
1528 lua_pushlightuserdata(L, args[0].data.map);
1529 lua_rawseti(L, -2, 0);
1530
1531 /* Pop a class Map metatable and affect it to the userdata. */
1532 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1533 lua_setmetatable(L, -2);
1534
1535
1536 return 1;
1537}
1538
1539__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1540{
1541 struct map_descriptor *desc;
1542 struct pattern *pat;
1543 struct sample smp;
1544
1545 MAY_LJMP(check_args(L, 2, "lookup"));
1546 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001547 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001548 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001549 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001550 }
1551 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001552 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001553 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001554 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 +02001555 }
1556
1557 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001558 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001559 if (str)
1560 lua_pushstring(L, "");
1561 else
1562 lua_pushnil(L);
1563 return 1;
1564 }
1565
1566 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001568 return 1;
1569}
1570
1571__LJMP static int hlua_map_lookup(struct lua_State *L)
1572{
1573 return _hlua_map_lookup(L, 0);
1574}
1575
1576__LJMP static int hlua_map_slookup(struct lua_State *L)
1577{
1578 return _hlua_map_lookup(L, 1);
1579}
1580
1581/*
1582 *
1583 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001584 * Class Socket
1585 *
1586 *
1587 */
1588
1589__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1590{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001591 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001592}
1593
1594/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001595 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001596 * received.
1597 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001598static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001599{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001600 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001601
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001602 if (appctx->ctx.hlua_cosocket.die) {
1603 si_shutw(si);
1604 si_shutr(si);
1605 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001606 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1607 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001608 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1609 }
1610
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001611 /* If we cant write, wakeup the pending write signals. */
1612 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001613 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001614
1615 /* If we cant read, wakeup the pending read signals. */
1616 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001617 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001618
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001619 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001620 * to be notified whenever the connection completes.
1621 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001622 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001623 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001624 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001625 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001626 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001627 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628
1629 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001630 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001631
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001632 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001633 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001634 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001635
1636 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001637 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001638 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001639
1640 /* Some data were injected in the buffer, notify the stream
1641 * interface.
1642 */
1643 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001644 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001645
1646 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001647 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001648 */
1649 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001650 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001651}
1652
Willy Tarreau87b09662015-04-03 00:22:06 +02001653/* This function is called when the "struct stream" is destroyed.
1654 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001655 * Wake all the pending signals.
1656 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001657static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001658{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001659 struct xref *peer;
1660
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001661 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001662 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1663 if (peer)
1664 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001665
1666 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001667 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1668 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001669}
1670
1671/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001672 * uses this object. If the stream does not exists, just quit.
1673 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001674 * pending signal can rest in the read and write lists. destroy
1675 * it.
1676 */
1677__LJMP static int hlua_socket_gc(lua_State *L)
1678{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001679 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001681 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001683 MAY_LJMP(check_args(L, 1, "__gc"));
1684
1685 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001686 peer = xref_get_peer_and_lock(&socket->xref);
1687 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001688 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001689 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001690
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001691 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001692 appctx->ctx.hlua_cosocket.die = 1;
1693 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001695 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001696 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 return 0;
1698}
1699
1700/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001701 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702 */
sada05ed3302018-05-11 11:48:18 -07001703__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001705 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001707 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001708 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001710 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001711
1712 /* Check if we run on the same thread than the xreator thread.
1713 * We cannot access to the socket if the thread is different.
1714 */
1715 if (socket->tid != tid)
1716 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1717
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001718 peer = xref_get_peer_and_lock(&socket->xref);
1719 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001721
1722 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001723 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001724
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001725 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001726 appctx->ctx.hlua_cosocket.die = 1;
1727 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001728
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001729 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001730 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001731 return 0;
1732}
1733
sada05ed3302018-05-11 11:48:18 -07001734/* The close function calls close_helper.
1735 */
1736__LJMP static int hlua_socket_close(lua_State *L)
1737{
1738 MAY_LJMP(check_args(L, 1, "close"));
1739 return hlua_socket_close_helper(L);
1740}
1741
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001742/* This Lua function assumes that the stack contain three parameters.
1743 * 1 - USERDATA containing a struct socket
1744 * 2 - INTEGER with values of the macro defined below
1745 * If the integer is -1, we must read at most one line.
1746 * If the integer is -2, we ust read all the data until the
1747 * end of the stream.
1748 * If the integer is positive value, we must read a number of
1749 * bytes corresponding to this value.
1750 */
1751#define HLSR_READ_LINE (-1)
1752#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001753__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001754{
1755 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1756 int wanted = lua_tointeger(L, 2);
1757 struct hlua *hlua = hlua_gethlua(L);
1758 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001759 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001760 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001761 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001762 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001763 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001764 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001765 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001766 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001767 struct stream_interface *si;
1768 struct stream *s;
1769 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001770 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001771
1772 /* Check if this lua stack is schedulable. */
1773 if (!hlua || !hlua->task)
1774 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1775 "'frontend', 'backend' or 'task'"));
1776
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001777 /* Check if we run on the same thread than the xreator thread.
1778 * We cannot access to the socket if the thread is different.
1779 */
1780 if (socket->tid != tid)
1781 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1782
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001783 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001784 peer = xref_get_peer_and_lock(&socket->xref);
1785 if (!peer)
1786 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001787 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1788 si = appctx->owner;
1789 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001791 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001794 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795 if (nblk < 0) /* Connection close. */
1796 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001797 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001798 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001799
1800 /* remove final \r\n. */
1801 if (nblk == 1) {
1802 if (blk1[len1-1] == '\n') {
1803 len1--;
1804 skip_at_end++;
1805 if (blk1[len1-1] == '\r') {
1806 len1--;
1807 skip_at_end++;
1808 }
1809 }
1810 }
1811 else {
1812 if (blk2[len2-1] == '\n') {
1813 len2--;
1814 skip_at_end++;
1815 if (blk2[len2-1] == '\r') {
1816 len2--;
1817 skip_at_end++;
1818 }
1819 }
1820 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001821 }
1822
1823 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001824 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001825 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001826 if (nblk < 0) /* Connection close. */
1827 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001828 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829 goto connection_empty;
1830 }
1831
1832 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001834 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001835 if (nblk < 0) /* Connection close. */
1836 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001837 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838 goto connection_empty;
1839
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001840 missing_bytes = wanted - socket->b.n;
1841 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001842 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001843 len1 = missing_bytes;
1844 } if (nblk == 2 && len1 + len2 > missing_bytes)
1845 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846 }
1847
1848 len = len1;
1849
1850 luaL_addlstring(&socket->b, blk1, len1);
1851 if (nblk == 2) {
1852 len += len2;
1853 luaL_addlstring(&socket->b, blk2, len2);
1854 }
1855
1856 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001857 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858
1859 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001860 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861
1862 /* If the pattern reclaim to read all the data
1863 * in the connection, got out.
1864 */
1865 if (wanted == HLSR_READ_ALL)
1866 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001867 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868 goto connection_empty;
1869
1870 /* Return result. */
1871 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001872 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001873 return 1;
1874
1875connection_closed:
1876
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001877 xref_unlock(&socket->xref, peer);
1878
1879no_peer:
1880
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001881 /* If the buffer containds data. */
1882 if (socket->b.n > 0) {
1883 luaL_pushresult(&socket->b);
1884 return 1;
1885 }
1886 lua_pushnil(L);
1887 lua_pushstring(L, "connection closed.");
1888 return 2;
1889
1890connection_empty:
1891
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001892 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1893 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001895 }
1896 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001897 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001898 return 0;
1899}
1900
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001901/* This Lua function gets two parameters. The first one can be string
1902 * or a number. If the string is "*l", the user requires one line. If
1903 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 * If the value is a number, the user require a number of bytes equal
1905 * to the value. The default value is "*l" (a line).
1906 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001907 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 * integer takes this values:
1909 * -1 : read a line
1910 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001911 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001912 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001913 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914 * concatenated with the read data.
1915 */
1916__LJMP static int hlua_socket_receive(struct lua_State *L)
1917{
1918 int wanted = HLSR_READ_LINE;
1919 const char *pattern;
1920 int type;
1921 char *error;
1922 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001923 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001924
1925 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1926 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1927
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001928 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001929
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001930 /* Check if we run on the same thread than the xreator thread.
1931 * We cannot access to the socket if the thread is different.
1932 */
1933 if (socket->tid != tid)
1934 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1935
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936 /* check for pattern. */
1937 if (lua_gettop(L) >= 2) {
1938 type = lua_type(L, 2);
1939 if (type == LUA_TSTRING) {
1940 pattern = lua_tostring(L, 2);
1941 if (strcmp(pattern, "*a") == 0)
1942 wanted = HLSR_READ_ALL;
1943 else if (strcmp(pattern, "*l") == 0)
1944 wanted = HLSR_READ_LINE;
1945 else {
1946 wanted = strtoll(pattern, &error, 10);
1947 if (*error != '\0')
1948 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1949 }
1950 }
1951 else if (type == LUA_TNUMBER) {
1952 wanted = lua_tointeger(L, 2);
1953 if (wanted < 0)
1954 WILL_LJMP(luaL_error(L, "Unsupported size."));
1955 }
1956 }
1957
1958 /* Set pattern. */
1959 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001960
1961 /* Check if we would replace the top by itself. */
1962 if (lua_gettop(L) != 2)
1963 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001964
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001965 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001966 luaL_buffinit(L, &socket->b);
1967
1968 /* Check prefix. */
1969 if (lua_gettop(L) >= 3) {
1970 if (lua_type(L, 3) != LUA_TSTRING)
1971 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1972 pattern = lua_tolstring(L, 3, &len);
1973 luaL_addlstring(&socket->b, pattern, len);
1974 }
1975
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001976 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977}
1978
1979/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001980 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001981 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001982static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001983{
1984 struct hlua_socket *socket;
1985 struct hlua *hlua = hlua_gethlua(L);
1986 struct appctx *appctx;
1987 size_t buf_len;
1988 const char *buf;
1989 int len;
1990 int send_len;
1991 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001992 struct xref *peer;
1993 struct stream_interface *si;
1994 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001995
1996 /* Check if this lua stack is schedulable. */
1997 if (!hlua || !hlua->task)
1998 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1999 "'frontend', 'backend' or 'task'"));
2000
2001 /* Get object */
2002 socket = MAY_LJMP(hlua_checksocket(L, 1));
2003 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002004 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002006 /* Check if we run on the same thread than the xreator thread.
2007 * We cannot access to the socket if the thread is different.
2008 */
2009 if (socket->tid != tid)
2010 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2011
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002012 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002013 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002014 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002015 lua_pushinteger(L, -1);
2016 return 1;
2017 }
2018 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2019 si = appctx->owner;
2020 s = si_strm(si);
2021
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002022 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002023 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002024 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002025 lua_pushinteger(L, -1);
2026 return 1;
2027 }
2028
2029 /* Update the input buffer data. */
2030 buf += sent;
2031 send_len = buf_len - sent;
2032
2033 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002034 if (sent >= buf_len) {
2035 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002037 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002038
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002039 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002040 * the request buffer if its not required.
2041 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002042 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002043 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002044 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002045 }
2046
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002047 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002048 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002049 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002050 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002051 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002052
2053 /* send data */
2054 if (len < send_len)
2055 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002056 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002057
2058 /* "Not enough space" (-1), "Buffer too little to contain
2059 * the data" (-2) are not expected because the available length
2060 * is tested.
2061 * Other unknown error are also not expected.
2062 */
2063 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002064 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002065 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002066
sada05ed3302018-05-11 11:48:18 -07002067 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002068 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002069 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002070 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071 return 1;
2072 }
2073
2074 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002075 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002076
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002077 s->req.rex = TICK_ETERNITY;
2078 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079
2080 /* Update length sent. */
2081 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002082 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083
2084 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002085 if (sent + len >= buf_len) {
2086 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002088 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002089
2090hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002091 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2092 xref_unlock(&socket->xref, peer);
2093 WILL_LJMP(luaL_error(L, "out of memory"));
2094 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002095 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002096 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002097 return 0;
2098}
2099
2100/* This function initiate the send of data. It just check the input
2101 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002102 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002103 * "hlua_socket_write_yield" that can yield.
2104 *
2105 * The Lua function gets between 3 and 4 parameters. The first one is
2106 * the associated object. The second is a string buffer. The third is
2107 * a facultative integer that represents where is the buffer position
2108 * of the start of the data that can send. The first byte is the
2109 * position "1". The default value is "1". The fourth argument is a
2110 * facultative integer that represents where is the buffer position
2111 * of the end of the data that can send. The default is the last byte.
2112 */
2113static int hlua_socket_send(struct lua_State *L)
2114{
2115 int i;
2116 int j;
2117 const char *buf;
2118 size_t buf_len;
2119
2120 /* Check number of arguments. */
2121 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2122 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2123
2124 /* Get the string. */
2125 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2126
2127 /* Get and check j. */
2128 if (lua_gettop(L) == 4) {
2129 j = MAY_LJMP(luaL_checkinteger(L, 4));
2130 if (j < 0)
2131 j = buf_len + j + 1;
2132 if (j > buf_len)
2133 j = buf_len + 1;
2134 lua_pop(L, 1);
2135 }
2136 else
2137 j = buf_len;
2138
2139 /* Get and check i. */
2140 if (lua_gettop(L) == 3) {
2141 i = MAY_LJMP(luaL_checkinteger(L, 3));
2142 if (i < 0)
2143 i = buf_len + i + 1;
2144 if (i > buf_len)
2145 i = buf_len + 1;
2146 lua_pop(L, 1);
2147 } else
2148 i = 1;
2149
2150 /* Check bth i and j. */
2151 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002152 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002153 return 1;
2154 }
2155 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002156 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002157 return 1;
2158 }
2159 if (i == 0)
2160 i = 1;
2161 if (j == 0)
2162 j = 1;
2163
2164 /* Pop the string. */
2165 lua_pop(L, 1);
2166
2167 /* Update the buffer length. */
2168 buf += i - 1;
2169 buf_len = j - i + 1;
2170 lua_pushlstring(L, buf, buf_len);
2171
2172 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002173 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002174
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002175 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002176}
2177
Willy Tarreau22b0a682015-06-17 19:43:49 +02002178#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002179__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2180{
2181 static char buffer[SOCKET_INFO_MAX_LEN];
2182 int ret;
2183 int len;
2184 char *p;
2185
2186 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2187 if (ret <= 0) {
2188 lua_pushnil(L);
2189 return 1;
2190 }
2191
2192 if (ret == AF_UNIX) {
2193 lua_pushstring(L, buffer+1);
2194 return 1;
2195 }
2196 else if (ret == AF_INET6) {
2197 buffer[0] = '[';
2198 len = strlen(buffer);
2199 buffer[len] = ']';
2200 len++;
2201 buffer[len] = ':';
2202 len++;
2203 p = buffer;
2204 }
2205 else if (ret == AF_INET) {
2206 p = buffer + 1;
2207 len = strlen(p);
2208 p[len] = ':';
2209 len++;
2210 }
2211 else {
2212 lua_pushnil(L);
2213 return 1;
2214 }
2215
2216 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2217 lua_pushnil(L);
2218 return 1;
2219 }
2220
2221 lua_pushstring(L, p);
2222 return 1;
2223}
2224
2225/* Returns information about the peer of the connection. */
2226__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2227{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002228 struct hlua_socket *socket;
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
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002255 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002256 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002257 lua_pushnil(L);
2258 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259 }
2260
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002261 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002262 xref_unlock(&socket->xref, peer);
2263 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002264}
2265
2266/* Returns information about my connection side. */
2267static int hlua_socket_getsockname(struct lua_State *L)
2268{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002269 struct hlua_socket *socket;
2270 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002271 struct appctx *appctx;
2272 struct xref *peer;
2273 struct stream_interface *si;
2274 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002275 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002276
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002277 MAY_LJMP(check_args(L, 1, "getsockname"));
2278
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002279 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002280
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002281 /* Check if we run on the same thread than the xreator thread.
2282 * We cannot access to the socket if the thread is different.
2283 */
2284 if (socket->tid != tid)
2285 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2286
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002287 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002288 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002289 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290 lua_pushnil(L);
2291 return 1;
2292 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002293 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2294 si = appctx->owner;
2295 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002296
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002297 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002298 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002299 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300 lua_pushnil(L);
2301 return 1;
2302 }
2303
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002304 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002305 xref_unlock(&socket->xref, peer);
2306 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307}
2308
2309/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002310static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002311 .obj_type = OBJ_TYPE_APPLET,
2312 .name = "<LUA_TCP>",
2313 .fct = hlua_socket_handler,
2314 .release = hlua_socket_release,
2315};
2316
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002317__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318{
2319 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2320 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002321 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002323 struct stream_interface *si;
2324 struct stream *s;
2325
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002326 /* Check if we run on the same thread than the xreator thread.
2327 * We cannot access to the socket if the thread is different.
2328 */
2329 if (socket->tid != tid)
2330 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2331
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002332 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002333 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002334 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002335 lua_pushnil(L);
2336 lua_pushstring(L, "Can't connect");
2337 return 2;
2338 }
2339 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2340 si = appctx->owner;
2341 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002342
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002343 /* Check if we run on the same thread than the xreator thread.
2344 * We cannot access to the socket if the thread is different.
2345 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002346 if (socket->tid != tid) {
2347 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002348 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002349 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002350
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002351 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002352 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002353 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002354 lua_pushnil(L);
2355 lua_pushstring(L, "Can't connect");
2356 return 2;
2357 }
2358
Willy Tarreaue09101e2018-10-16 17:37:12 +02002359 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360
2361 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002362 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002363 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002364 lua_pushinteger(L, 1);
2365 return 1;
2366 }
2367
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002368 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2369 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002370 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002371 }
2372 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002373 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374 return 0;
2375}
2376
2377/* This function fail or initite the connection. */
2378__LJMP static int hlua_socket_connect(struct lua_State *L)
2379{
2380 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002381 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002383 struct hlua *hlua;
2384 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002385 int low, high;
2386 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002387 struct xref *peer;
2388 struct stream_interface *si;
2389 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002391 if (lua_gettop(L) < 2)
2392 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002393
2394 /* Get args. */
2395 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002396
2397 /* Check if we run on the same thread than the xreator thread.
2398 * We cannot access to the socket if the thread is different.
2399 */
2400 if (socket->tid != tid)
2401 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2402
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002404 if (lua_gettop(L) >= 3) {
2405 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002406 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002407
Tim Duesterhus6edab862018-01-06 19:04:45 +01002408 /* Force the ip to end with a colon, to support IPv6 addresses
2409 * that are not enclosed within square brackets.
2410 */
2411 if (port > 0) {
2412 luaL_buffinit(L, &b);
2413 luaL_addstring(&b, ip);
2414 luaL_addchar(&b, ':');
2415 luaL_pushresult(&b);
2416 ip = lua_tolstring(L, lua_gettop(L), NULL);
2417 }
2418 }
2419
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002420 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002421 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002422 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002423 lua_pushnil(L);
2424 return 1;
2425 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002426
2427 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002428 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002429 if (!addr) {
2430 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002431 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 }
2433 if (low != high) {
2434 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002435 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002436 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002437
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002438 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002439 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002440 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002441 if (port == -1) {
2442 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002443 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002444 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002445 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2446 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002447 if (port == -1) {
2448 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002449 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002450 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002451 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002452 }
2453 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002454
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002455 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2456 si = appctx->owner;
2457 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002458
2459 if (!sockaddr_alloc(&s->target_addr)) {
2460 xref_unlock(&socket->xref, peer);
2461 WILL_LJMP(luaL_error(L, "connect: internal error"));
2462 }
2463 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002464 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002465
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002466 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002467
2468 /* inform the stream that we want to be notified whenever the
2469 * connection completes.
2470 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002471 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002472 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002473 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002474
Willy Tarreauf31af932020-01-14 09:59:38 +01002475 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002476
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002477 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2478 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002479 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002480 }
2481 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002482
2483 task_wakeup(s->task, TASK_WOKEN_INIT);
2484 /* Return yield waiting for connection. */
2485
Willy Tarreau9635e032018-10-16 17:52:55 +02002486 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002487
2488 return 0;
2489}
2490
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002491#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002492__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2493{
2494 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002495 struct xref *peer;
2496 struct appctx *appctx;
2497 struct stream_interface *si;
2498 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002499
2500 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2501 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002502
2503 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002504 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002505 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002506 lua_pushnil(L);
2507 return 1;
2508 }
2509 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2510 si = appctx->owner;
2511 s = si_strm(si);
2512
2513 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002514 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002515 return MAY_LJMP(hlua_socket_connect(L));
2516}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002517#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002518
2519__LJMP static int hlua_socket_setoption(struct lua_State *L)
2520{
2521 return 0;
2522}
2523
2524__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2525{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002526 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002527 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002528 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002529 struct xref *peer;
2530 struct appctx *appctx;
2531 struct stream_interface *si;
2532 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002533
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002534 MAY_LJMP(check_args(L, 2, "settimeout"));
2535
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002536 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002537
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002538 /* convert the timeout to millis */
2539 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002540
Thierry Fournier17a921b2018-03-08 09:59:02 +01002541 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002542 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002543 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2544
Mark Lakes56cc1252018-03-27 09:48:06 +02002545 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002546 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002547
2548 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002549 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002550 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002551
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002552 /* Check if we run on the same thread than the xreator thread.
2553 * We cannot access to the socket if the thread is different.
2554 */
2555 if (socket->tid != tid)
2556 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2557
Mark Lakes56cc1252018-03-27 09:48:06 +02002558 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002559 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002561 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2562 WILL_LJMP(lua_error(L));
2563 return 0;
2564 }
2565 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2566 si = appctx->owner;
2567 s = si_strm(si);
2568
Cyril Bonté7bb63452018-08-17 23:51:02 +02002569 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002570 s->req.rto = tmout;
2571 s->req.wto = tmout;
2572 s->res.rto = tmout;
2573 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002574 s->req.rex = tick_add_ifset(now_ms, tmout);
2575 s->req.wex = tick_add_ifset(now_ms, tmout);
2576 s->res.rex = tick_add_ifset(now_ms, tmout);
2577 s->res.wex = tick_add_ifset(now_ms, tmout);
2578
2579 s->task->expire = tick_add_ifset(now_ms, tmout);
2580 task_queue(s->task);
2581
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002582 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002583
Thierry Fourniere9636f12018-03-08 09:54:32 +01002584 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002585 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002586}
2587
2588__LJMP static int hlua_socket_new(lua_State *L)
2589{
2590 struct hlua_socket *socket;
2591 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002592 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002593 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002594
2595 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002596 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002597 hlua_pusherror(L, "socket: full stack");
2598 goto out_fail_conf;
2599 }
2600
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002601 /* Create the object: obj[0] = userdata. */
2602 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002603 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002604 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002606 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002607
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002608 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002609 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002610 hlua_pusherror(L, "socket: uninitialized pools.");
2611 goto out_fail_conf;
2612 }
2613
Willy Tarreau87b09662015-04-03 00:22:06 +02002614 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002615 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2616 lua_setmetatable(L, -2);
2617
Willy Tarreaud420a972015-04-06 00:39:18 +02002618 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002619 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002620 if (!appctx) {
2621 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002622 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002623 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002624
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002625 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002626 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002627 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2628 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002629
Willy Tarreaud420a972015-04-06 00:39:18 +02002630 /* Now create a session, task and stream for this applet */
2631 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2632 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002633 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002634 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635 }
2636
Willy Tarreau87787ac2017-08-28 16:22:54 +02002637 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002638 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002639 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002640 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641 }
2642
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002643 /* Initialise cross reference between stream and Lua socket object. */
2644 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 /* Configure "right" stream interface. this "si" is used to connect
2647 * and retrieve data from the server. The connection is initialized
2648 * with the "struct server".
2649 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002650 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002651
2652 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002653 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002654 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002655
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002656 return 1;
2657
Willy Tarreaud420a972015-04-06 00:39:18 +02002658 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002659 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002660 out_fail_sess:
2661 appctx_free(appctx);
2662 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002663 WILL_LJMP(lua_error(L));
2664 return 0;
2665}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002666
2667/*
2668 *
2669 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002670 * Class Channel
2671 *
2672 *
2673 */
2674
2675/* Returns the struct hlua_channel join to the class channel in the
2676 * stack entry "ud" or throws an argument error.
2677 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002678__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002679{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002680 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002681}
2682
Willy Tarreau47860ed2015-03-10 14:07:50 +01002683/* Pushes the channel onto the top of the stack. If the stask does not have a
2684 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002686static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002688 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002689 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002690 return 0;
2691
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002692 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002693 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002694 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002695
2696 /* Pop a class sesison metatable and affect it to the userdata. */
2697 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2698 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002699 return 1;
2700}
2701
2702/* Duplicate all the data present in the input channel and put it
2703 * in a string LUA variables. Returns -1 and push a nil value in
2704 * the stack if the channel is closed and all the data are consumed,
2705 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002706 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002707 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002708static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002709{
2710 char *blk1;
2711 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002712 size_t len1;
2713 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002714 int ret;
2715 luaL_Buffer b;
2716
Willy Tarreau06d80a92017-10-19 14:32:15 +02002717 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002718 if (unlikely(ret == 0))
2719 return 0;
2720
2721 if (unlikely(ret < 0)) {
2722 lua_pushnil(L);
2723 return -1;
2724 }
2725
2726 luaL_buffinit(L, &b);
2727 luaL_addlstring(&b, blk1, len1);
2728 if (unlikely(ret == 2))
2729 luaL_addlstring(&b, blk2, len2);
2730 luaL_pushresult(&b);
2731
2732 if (unlikely(ret == 2))
2733 return len1 + len2;
2734 return len1;
2735}
2736
2737/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2738 * a yield. This function keep the data in the buffer.
2739 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002740__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002742 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002743
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002744 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2745
Christopher Faulet3f829a42018-12-13 21:56:45 +01002746 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2747 WILL_LJMP(lua_error(L));
2748
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002749 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002750 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002751 return 1;
2752}
2753
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002754/* Check arguments for the function "hlua_channel_dup_yield". */
2755__LJMP static int hlua_channel_dup(lua_State *L)
2756{
2757 MAY_LJMP(check_args(L, 1, "dup"));
2758 MAY_LJMP(hlua_checkchannel(L, 1));
2759 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2760}
2761
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002762/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2763 * a yield. This function consumes the data in the buffer. It returns
2764 * a string containing the data or a nil pointer if no data are available
2765 * and the channel is closed.
2766 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002767__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002768{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002769 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002770 int ret;
2771
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002772 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002773
Christopher Faulet3f829a42018-12-13 21:56:45 +01002774 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2775 WILL_LJMP(lua_error(L));
2776
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002777 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002779 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002780
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002781 if (unlikely(ret == -1))
2782 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002784 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002785 return 1;
2786}
2787
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002788/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002789__LJMP static int hlua_channel_get(lua_State *L)
2790{
2791 MAY_LJMP(check_args(L, 1, "get"));
2792 MAY_LJMP(hlua_checkchannel(L, 1));
2793 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2794}
2795
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002796/* This functions consumes and returns one line. If the channel is closed,
2797 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002798 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002799 * value.
2800 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002801__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002802{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803 char *blk1;
2804 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002805 size_t len1;
2806 size_t len2;
2807 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002808 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002809 int ret;
2810 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002811
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002812 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2813
Christopher Faulet3f829a42018-12-13 21:56:45 +01002814 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2815 WILL_LJMP(lua_error(L));
2816
Willy Tarreau06d80a92017-10-19 14:32:15 +02002817 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002818 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002819 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002820
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002821 if (ret == -1) {
2822 lua_pushnil(L);
2823 return 1;
2824 }
2825
2826 luaL_buffinit(L, &b);
2827 luaL_addlstring(&b, blk1, len1);
2828 len = len1;
2829 if (unlikely(ret == 2)) {
2830 luaL_addlstring(&b, blk2, len2);
2831 len += len2;
2832 }
2833 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002834 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002835 return 1;
2836}
2837
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002838/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002839__LJMP static int hlua_channel_getline(lua_State *L)
2840{
2841 MAY_LJMP(check_args(L, 1, "getline"));
2842 MAY_LJMP(hlua_checkchannel(L, 1));
2843 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2844}
2845
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002846/* This function takes a string as input, and append it at the
2847 * input side of channel. If the data is too big, but a space
2848 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002849 * yields. If the data is bigger than the buffer, or if the
2850 * channel is closed, it returns -1. Otherwise, it returns the
2851 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002853__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002854{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002855 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002856 size_t len;
2857 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2858 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2859 int ret;
2860 int max;
2861
Christopher Faulet3f829a42018-12-13 21:56:45 +01002862 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2863 WILL_LJMP(lua_error(L));
2864
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002865 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002866 * the request buffer if its not required.
2867 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002868 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002869 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002870 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002871 }
2872
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002873 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002874 if (max > len - l)
2875 max = len - l;
2876
Willy Tarreau06d80a92017-10-19 14:32:15 +02002877 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878 if (ret == -2 || ret == -3) {
2879 lua_pushinteger(L, -1);
2880 return 1;
2881 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002882 if (ret == -1) {
2883 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002884 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002885 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002886 l += ret;
2887 lua_pop(L, 1);
2888 lua_pushinteger(L, l);
2889
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002890 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002891 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002892 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002894 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895 */
2896 return 1;
2897 }
2898 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002899 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900 return 1;
2901}
2902
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002903/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2904 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002905 * buffer size is too little for the data.
2906 */
2907__LJMP static int hlua_channel_append(lua_State *L)
2908{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002909 size_t len;
2910
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002912 MAY_LJMP(hlua_checkchannel(L, 1));
2913 MAY_LJMP(luaL_checklstring(L, 2, &len));
2914 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915 lua_pushinteger(L, 0);
2916
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002917 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918}
2919
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002920/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002921 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002922 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002923 * or -1 if the channel is closed or if the buffer size is too
2924 * little for the data.
2925 */
2926__LJMP static int hlua_channel_set(lua_State *L)
2927{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002928 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002929
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002930 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002931 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002932 lua_pushinteger(L, 0);
2933
Christopher Faulet3f829a42018-12-13 21:56:45 +01002934 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2935 WILL_LJMP(lua_error(L));
2936
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002937 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002938
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002939 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940}
2941
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002942/* Append data in the output side of the buffer. This data is immediately
2943 * sent. The function returns the amount of data written. If the buffer
2944 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945 * if the channel is closed.
2946 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002947__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002949 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002950 size_t len;
2951 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2952 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2953 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002954 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955
Christopher Faulet3f829a42018-12-13 21:56:45 +01002956 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2957 WILL_LJMP(lua_error(L));
2958
Willy Tarreau47860ed2015-03-10 14:07:50 +01002959 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002960 lua_pushinteger(L, -1);
2961 return 1;
2962 }
2963
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002964 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002965 * the request buffer if its not required.
2966 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002967 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002968 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002969 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002970 }
2971
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002972 /* The written data will be immediately sent, so we can check
2973 * the available space without taking in account the reserve.
2974 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002975 * data, because the buffer will be flushed.
2976 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002977 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002978
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002979 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002980 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002981 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002982 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002983 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002984 return 1;
2985
2986 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002987 if (max > len - l)
2988 max = len - l;
2989
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002990 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002991 * detects a non contiguous buffer and realign it.
2992 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002993 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002994 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002995
2996 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002997 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002998
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002999 /* buffer replace considers that the input part is filled.
3000 * so, I must forward these new data in the output part.
3001 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003002 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003003
3004 l += max;
3005 lua_pop(L, 1);
3006 lua_pushinteger(L, l);
3007
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003008 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003009 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003010 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003011 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003012 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003013 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003015
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003016 if (l < len) {
3017 /* If we are waiting for space in the response buffer, we
3018 * must set the flag WAKERESWR. This flag required the task
3019 * wake up if any activity is detected on the response buffer.
3020 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003021 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003022 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003023 else
3024 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003025 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003026 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027
3028 return 1;
3029}
3030
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003031/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003032 * yield the LUA process, and resume it without checking the
3033 * input arguments.
3034 */
3035__LJMP static int hlua_channel_send(lua_State *L)
3036{
3037 MAY_LJMP(check_args(L, 2, "send"));
3038 lua_pushinteger(L, 0);
3039
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003040 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003041}
3042
3043/* This function forward and amount of butes. The data pass from
3044 * the input side of the buffer to the output side, and can be
3045 * forwarded. This function never fails.
3046 *
3047 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003048 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003050__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003052 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053 int len;
3054 int l;
3055 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003056 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003057
3058 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003059
3060 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3061 WILL_LJMP(lua_error(L));
3062
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003063 len = MAY_LJMP(luaL_checkinteger(L, 2));
3064 l = MAY_LJMP(luaL_checkinteger(L, -1));
3065
3066 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003067 if (max > ci_data(chn))
3068 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003069 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003070 l += max;
3071
3072 lua_pop(L, 1);
3073 lua_pushinteger(L, l);
3074
3075 /* Check if it miss bytes to forward. */
3076 if (l < len) {
3077 /* The the input channel or the output channel are closed, we
3078 * must return the amount of data forwarded.
3079 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003080 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003081 return 1;
3082
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003083 /* If we are waiting for space data in the response buffer, we
3084 * must set the flag WAKERESWR. This flag required the task
3085 * wake up if any activity is detected on the response buffer.
3086 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003087 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003088 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003089 else
3090 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003091
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003092 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003093 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003094 }
3095
3096 return 1;
3097}
3098
3099/* Just check the input and prepare the stack for the previous
3100 * function "hlua_channel_forward_yield"
3101 */
3102__LJMP static int hlua_channel_forward(lua_State *L)
3103{
3104 MAY_LJMP(check_args(L, 2, "forward"));
3105 MAY_LJMP(hlua_checkchannel(L, 1));
3106 MAY_LJMP(luaL_checkinteger(L, 2));
3107
3108 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003109 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003110}
3111
3112/* Just returns the number of bytes available in the input
3113 * side of the buffer. This function never fails.
3114 */
3115__LJMP static int hlua_channel_get_in_len(lua_State *L)
3116{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003117 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003118
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003119 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003120 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003121 if (IS_HTX_STRM(chn_strm(chn))) {
3122 struct htx *htx = htxbuf(&chn->buf);
3123 lua_pushinteger(L, htx->data - co_data(chn));
3124 }
3125 else
3126 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 return 1;
3128}
3129
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003130/* Returns true if the channel is full. */
3131__LJMP static int hlua_channel_is_full(lua_State *L)
3132{
3133 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003134
3135 MAY_LJMP(check_args(L, 1, "is_full"));
3136 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003137 /* ignore the reserve, we are not on a producer side (ie in an
3138 * applet).
3139 */
3140 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003141 return 1;
3142}
3143
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003144/* Returns true if the channel is the response channel. */
3145__LJMP static int hlua_channel_is_resp(lua_State *L)
3146{
3147 struct channel *chn;
3148
3149 MAY_LJMP(check_args(L, 1, "is_resp"));
3150 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3151
3152 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3153 return 1;
3154}
3155
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003156/* Just returns the number of bytes available in the output
3157 * side of the buffer. This function never fails.
3158 */
3159__LJMP static int hlua_channel_get_out_len(lua_State *L)
3160{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003161 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003162
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003163 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003164 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003165 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003166 return 1;
3167}
3168
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003169/*
3170 *
3171 *
3172 * Class Fetches
3173 *
3174 *
3175 */
3176
3177/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003178 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003179 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003180__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003181{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003182 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003183}
3184
3185/* This function creates and push in the stack a fetch object according
3186 * with a current TXN.
3187 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003188static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003189{
Willy Tarreau7073c472015-04-06 11:15:40 +02003190 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003191
3192 /* Check stack size. */
3193 if (!lua_checkstack(L, 3))
3194 return 0;
3195
3196 /* Create the object: obj[0] = userdata.
3197 * Note that the base of the Fetches object is the
3198 * transaction object.
3199 */
3200 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003201 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003202 lua_rawseti(L, -2, 0);
3203
Willy Tarreau7073c472015-04-06 11:15:40 +02003204 hsmp->s = txn->s;
3205 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003206 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003207 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003208
3209 /* Pop a class sesison metatable and affect it to the userdata. */
3210 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3211 lua_setmetatable(L, -2);
3212
3213 return 1;
3214}
3215
3216/* This function is an LUA binding. It is called with each sample-fetch.
3217 * It uses closure argument to store the associated sample-fetch. It
3218 * returns only one argument or throws an error. An error is thrown
3219 * only if an error is encountered during the argument parsing. If
3220 * the "sample-fetch" function fails, nil is returned.
3221 */
3222__LJMP static int hlua_run_sample_fetch(lua_State *L)
3223{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003224 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003225 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003226 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003227 int i;
3228 struct sample smp;
3229
3230 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003231 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003232
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003233 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003234 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003235
Thierry FOURNIERca988662015-12-20 18:43:03 +01003236 /* Check execution authorization. */
3237 if (f->use & SMP_USE_HTTP_ANY &&
3238 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3239 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3240 "is not available in Lua services", f->kw);
3241 WILL_LJMP(lua_error(L));
3242 }
3243
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003244 /* Get extra arguments. */
3245 for (i = 0; i < lua_gettop(L) - 1; i++) {
3246 if (i >= ARGM_NBARGS)
3247 break;
3248 hlua_lua2arg(L, i + 2, &args[i]);
3249 }
3250 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003251 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003252
3253 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003254 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003255
3256 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003257 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003258 lua_pushfstring(L, "error in arguments");
3259 WILL_LJMP(lua_error(L));
3260 }
3261
3262 /* Initialise the sample. */
3263 memset(&smp, 0, sizeof(smp));
3264
3265 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003266 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003267 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003268 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003269 lua_pushstring(L, "");
3270 else
3271 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003272 return 1;
3273 }
3274
3275 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003276 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003277 hlua_smp2lua_str(L, &smp);
3278 else
3279 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003280 return 1;
3281}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003282
3283/*
3284 *
3285 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003286 * Class Converters
3287 *
3288 *
3289 */
3290
3291/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003292 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003293 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003294__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003295{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003296 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003297}
3298
3299/* This function creates and push in the stack a Converters object
3300 * according with a current TXN.
3301 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003302static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003303{
Willy Tarreau7073c472015-04-06 11:15:40 +02003304 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003305
3306 /* Check stack size. */
3307 if (!lua_checkstack(L, 3))
3308 return 0;
3309
3310 /* Create the object: obj[0] = userdata.
3311 * Note that the base of the Converters object is the
3312 * same than the TXN object.
3313 */
3314 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003315 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003316 lua_rawseti(L, -2, 0);
3317
Willy Tarreau7073c472015-04-06 11:15:40 +02003318 hsmp->s = txn->s;
3319 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003320 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003321 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003322
Willy Tarreau87b09662015-04-03 00:22:06 +02003323 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003324 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3325 lua_setmetatable(L, -2);
3326
3327 return 1;
3328}
3329
3330/* This function is an LUA binding. It is called with each converter.
3331 * It uses closure argument to store the associated converter. It
3332 * returns only one argument or throws an error. An error is thrown
3333 * only if an error is encountered during the argument parsing. If
3334 * the converter function function fails, nil is returned.
3335 */
3336__LJMP static int hlua_run_sample_conv(lua_State *L)
3337{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003338 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003339 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003340 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003341 int i;
3342 struct sample smp;
3343
3344 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003345 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003346
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003347 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003348 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003349
3350 /* Get extra arguments. */
3351 for (i = 0; i < lua_gettop(L) - 2; i++) {
3352 if (i >= ARGM_NBARGS)
3353 break;
3354 hlua_lua2arg(L, i + 3, &args[i]);
3355 }
3356 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003357 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003358
3359 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003360 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003361
3362 /* Run the special args checker. */
3363 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3364 hlua_pusherror(L, "error in arguments");
3365 WILL_LJMP(lua_error(L));
3366 }
3367
3368 /* Initialise the sample. */
3369 if (!hlua_lua2smp(L, 2, &smp)) {
3370 hlua_pusherror(L, "error in the input argument");
3371 WILL_LJMP(lua_error(L));
3372 }
3373
Willy Tarreau1777ea62016-03-10 16:15:46 +01003374 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3375
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003376 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003377 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003378 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003379 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003380 WILL_LJMP(lua_error(L));
3381 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003382 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3383 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003384 hlua_pusherror(L, "error during the input argument casting");
3385 WILL_LJMP(lua_error(L));
3386 }
3387
3388 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003389 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003390 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003391 lua_pushstring(L, "");
3392 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003393 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003394 return 1;
3395 }
3396
3397 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003398 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003399 hlua_smp2lua_str(L, &smp);
3400 else
3401 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003402 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003403}
3404
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003405/*
3406 *
3407 *
3408 * Class AppletTCP
3409 *
3410 *
3411 */
3412
3413/* Returns a struct hlua_txn if the stack entry "ud" is
3414 * a class stream, otherwise it throws an error.
3415 */
3416__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3417{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003418 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003419}
3420
3421/* This function creates and push in the stack an Applet object
3422 * according with a current TXN.
3423 */
3424static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3425{
3426 struct hlua_appctx *appctx;
3427 struct stream_interface *si = ctx->owner;
3428 struct stream *s = si_strm(si);
3429 struct proxy *p = s->be;
3430
3431 /* Check stack size. */
3432 if (!lua_checkstack(L, 3))
3433 return 0;
3434
3435 /* Create the object: obj[0] = userdata.
3436 * Note that the base of the Converters object is the
3437 * same than the TXN object.
3438 */
3439 lua_newtable(L);
3440 appctx = lua_newuserdata(L, sizeof(*appctx));
3441 lua_rawseti(L, -2, 0);
3442 appctx->appctx = ctx;
3443 appctx->htxn.s = s;
3444 appctx->htxn.p = p;
3445
3446 /* Create the "f" field that contains a list of fetches. */
3447 lua_pushstring(L, "f");
3448 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3449 return 0;
3450 lua_settable(L, -3);
3451
3452 /* Create the "sf" field that contains a list of stringsafe fetches. */
3453 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003454 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003455 return 0;
3456 lua_settable(L, -3);
3457
3458 /* Create the "c" field that contains a list of converters. */
3459 lua_pushstring(L, "c");
3460 if (!hlua_converters_new(L, &appctx->htxn, 0))
3461 return 0;
3462 lua_settable(L, -3);
3463
3464 /* Create the "sc" field that contains a list of stringsafe converters. */
3465 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003466 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003467 return 0;
3468 lua_settable(L, -3);
3469
3470 /* Pop a class stream metatable and affect it to the table. */
3471 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3472 lua_setmetatable(L, -2);
3473
3474 return 1;
3475}
3476
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003477__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3478{
3479 struct hlua_appctx *appctx;
3480 struct stream *s;
3481 const char *name;
3482 size_t len;
3483 struct sample smp;
3484
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003485 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3486 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003487
3488 /* It is useles to retrieve the stream, but this function
3489 * runs only in a stream context.
3490 */
3491 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3492 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3493 s = appctx->htxn.s;
3494
3495 /* Converts the third argument in a sample. */
3496 hlua_lua2smp(L, 3, &smp);
3497
3498 /* Store the sample in a variable. */
3499 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003500
3501 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3502 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3503 else
3504 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3505
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003506 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003507}
3508
3509__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3510{
3511 struct hlua_appctx *appctx;
3512 struct stream *s;
3513 const char *name;
3514 size_t len;
3515 struct sample smp;
3516
3517 MAY_LJMP(check_args(L, 2, "unset_var"));
3518
3519 /* It is useles to retrieve the stream, but this function
3520 * runs only in a stream context.
3521 */
3522 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3523 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3524 s = appctx->htxn.s;
3525
3526 /* Unset the variable. */
3527 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003528 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3529 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003530}
3531
3532__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3533{
3534 struct hlua_appctx *appctx;
3535 struct stream *s;
3536 const char *name;
3537 size_t len;
3538 struct sample smp;
3539
3540 MAY_LJMP(check_args(L, 2, "get_var"));
3541
3542 /* It is useles to retrieve the stream, but this function
3543 * runs only in a stream context.
3544 */
3545 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3546 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3547 s = appctx->htxn.s;
3548
3549 smp_set_owner(&smp, s->be, s->sess, s, 0);
3550 if (!vars_get_by_name(name, len, &smp)) {
3551 lua_pushnil(L);
3552 return 1;
3553 }
3554
3555 return hlua_smp2lua(L, &smp);
3556}
3557
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003558__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3559{
3560 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3561 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003562 struct hlua *hlua;
3563
3564 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003565 if (!s->hlua)
3566 return 0;
3567 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003568
3569 MAY_LJMP(check_args(L, 2, "set_priv"));
3570
3571 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003572 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003573
3574 /* Get and store new value. */
3575 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3576 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3577
3578 return 0;
3579}
3580
3581__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3582{
3583 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3584 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003585 struct hlua *hlua;
3586
3587 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003588 if (!s->hlua) {
3589 lua_pushnil(L);
3590 return 1;
3591 }
3592 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003593
3594 /* Push configuration index in the stack. */
3595 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3596
3597 return 1;
3598}
3599
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003600/* If expected data not yet available, it returns a yield. This function
3601 * consumes the data in the buffer. It returns a string containing the
3602 * data. This string can be empty.
3603 */
3604__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3605{
3606 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3607 struct stream_interface *si = appctx->appctx->owner;
3608 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003609 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003610 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003611 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003612 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003613
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003614 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003615 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003616
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003617 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003618 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003619 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003620 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003621 }
3622
3623 /* End of data: commit the total strings and return. */
3624 if (ret < 0) {
3625 luaL_pushresult(&appctx->b);
3626 return 1;
3627 }
3628
3629 /* Ensure that the block 2 length is usable. */
3630 if (ret == 1)
3631 len2 = 0;
3632
3633 /* dont check the max length read and dont check. */
3634 luaL_addlstring(&appctx->b, blk1, len1);
3635 luaL_addlstring(&appctx->b, blk2, len2);
3636
3637 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003638 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003639 luaL_pushresult(&appctx->b);
3640 return 1;
3641}
3642
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003643/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003644__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3645{
3646 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3647
3648 /* Initialise the string catenation. */
3649 luaL_buffinit(L, &appctx->b);
3650
3651 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3652}
3653
3654/* If expected data not yet available, it returns a yield. This function
3655 * consumes the data in the buffer. It returns a string containing the
3656 * data. This string can be empty.
3657 */
3658__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3659{
3660 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3661 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003662 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003663 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003664 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003665 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003666 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003667 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003668
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003669 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003670 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003671
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003672 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003673 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003674 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003675 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003676 }
3677
3678 /* End of data: commit the total strings and return. */
3679 if (ret < 0) {
3680 luaL_pushresult(&appctx->b);
3681 return 1;
3682 }
3683
3684 /* Ensure that the block 2 length is usable. */
3685 if (ret == 1)
3686 len2 = 0;
3687
3688 if (len == -1) {
3689
3690 /* If len == -1, catenate all the data avalaile and
3691 * yield because we want to get all the data until
3692 * the end of data stream.
3693 */
3694 luaL_addlstring(&appctx->b, blk1, len1);
3695 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003696 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003697 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003698 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003699
3700 } else {
3701
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003702 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003703 if (len1 > len)
3704 len1 = len;
3705 luaL_addlstring(&appctx->b, blk1, len1);
3706 len -= len1;
3707
3708 /* Copy the second block. */
3709 if (len2 > len)
3710 len2 = len;
3711 luaL_addlstring(&appctx->b, blk2, len2);
3712 len -= len2;
3713
3714 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003715 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003717 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003718 if (len > 0) {
3719 lua_pushinteger(L, len);
3720 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003721 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003722 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003723 }
3724
3725 /* return the result. */
3726 luaL_pushresult(&appctx->b);
3727 return 1;
3728 }
3729
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003730 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003731 hlua_pusherror(L, "Lua: internal error");
3732 WILL_LJMP(lua_error(L));
3733 return 0;
3734}
3735
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003736/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003737__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3738{
3739 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3740 int len = -1;
3741
3742 if (lua_gettop(L) > 2)
3743 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3744 if (lua_gettop(L) >= 2) {
3745 len = MAY_LJMP(luaL_checkinteger(L, 2));
3746 lua_pop(L, 1);
3747 }
3748
3749 /* Confirm or set the required length */
3750 lua_pushinteger(L, len);
3751
3752 /* Initialise the string catenation. */
3753 luaL_buffinit(L, &appctx->b);
3754
3755 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3756}
3757
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003758/* Append data in the output side of the buffer. This data is immediately
3759 * sent. The function returns the amount of data written. If the buffer
3760 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003761 * if the channel is closed.
3762 */
3763__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3764{
3765 size_t len;
3766 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3767 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3768 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3769 struct stream_interface *si = appctx->appctx->owner;
3770 struct channel *chn = si_ic(si);
3771 int max;
3772
3773 /* Get the max amount of data which can write as input in the channel. */
3774 max = channel_recv_max(chn);
3775 if (max > (len - l))
3776 max = len - l;
3777
3778 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003779 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003780
3781 /* update counters. */
3782 l += max;
3783 lua_pop(L, 1);
3784 lua_pushinteger(L, l);
3785
3786 /* If some data is not send, declares the situation to the
3787 * applet, and returns a yield.
3788 */
3789 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003790 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003791 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003792 }
3793
3794 return 1;
3795}
3796
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003797/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003798 * yield the LUA process, and resume it without checking the
3799 * input arguments.
3800 */
3801__LJMP static int hlua_applet_tcp_send(lua_State *L)
3802{
3803 MAY_LJMP(check_args(L, 2, "send"));
3804 lua_pushinteger(L, 0);
3805
3806 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3807}
3808
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003809/*
3810 *
3811 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003812 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003813 *
3814 *
3815 */
3816
3817/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003818 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003819 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003820__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003821{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003822 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003823}
3824
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003825/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003826 * according with a current TXN.
3827 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003828static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003829{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003830 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003831 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003832 struct stream_interface *si = ctx->owner;
3833 struct stream *s = si_strm(si);
3834 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003835 struct htx *htx;
3836 struct htx_blk *blk;
3837 struct htx_sl *sl;
3838 struct ist path;
3839 unsigned long long len = 0;
3840 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003841
3842 /* Check stack size. */
3843 if (!lua_checkstack(L, 3))
3844 return 0;
3845
3846 /* Create the object: obj[0] = userdata.
3847 * Note that the base of the Converters object is the
3848 * same than the TXN object.
3849 */
3850 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003851 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003852 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003853 appctx->appctx = ctx;
3854 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003855 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003856 appctx->htxn.s = s;
3857 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003858
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003859 /* Create the "f" field that contains a list of fetches. */
3860 lua_pushstring(L, "f");
3861 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3862 return 0;
3863 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003864
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003865 /* Create the "sf" field that contains a list of stringsafe fetches. */
3866 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003867 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003868 return 0;
3869 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003870
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003871 /* Create the "c" field that contains a list of converters. */
3872 lua_pushstring(L, "c");
3873 if (!hlua_converters_new(L, &appctx->htxn, 0))
3874 return 0;
3875 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003876
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003877 /* Create the "sc" field that contains a list of stringsafe converters. */
3878 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003879 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003880 return 0;
3881 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003882
Christopher Fauleta2097962019-07-15 16:25:33 +02003883 htx = htxbuf(&s->req.buf);
3884 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003885 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003886 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003887
Christopher Fauleta2097962019-07-15 16:25:33 +02003888 /* Stores the request method. */
3889 lua_pushstring(L, "method");
3890 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3891 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003892
Christopher Fauleta2097962019-07-15 16:25:33 +02003893 /* Stores the http version. */
3894 lua_pushstring(L, "version");
3895 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3896 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003897
Christopher Fauleta2097962019-07-15 16:25:33 +02003898 /* creates an array of headers. hlua_http_get_headers() crates and push
3899 * the array on the top of the stack.
3900 */
3901 lua_pushstring(L, "headers");
3902 htxn.s = s;
3903 htxn.p = px;
3904 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003905 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003906 return 0;
3907 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003908
Christopher Fauleta2097962019-07-15 16:25:33 +02003909 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003910 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003911 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003912
Christopher Fauleta2097962019-07-15 16:25:33 +02003913 p = path.ptr;
3914 end = path.ptr + path.len;
3915 q = p;
3916 while (q < end && *q != '?')
3917 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003918
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003919 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003920 lua_pushstring(L, "path");
3921 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003922 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003923
Christopher Fauleta2097962019-07-15 16:25:33 +02003924 /* Stores the query string. */
3925 lua_pushstring(L, "qs");
3926 if (*q == '?')
3927 q++;
3928 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003929 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003930 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003931
Christopher Fauleta2097962019-07-15 16:25:33 +02003932 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3933 struct htx_blk *blk = htx_get_blk(htx, pos);
3934 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003935
Christopher Fauleta2097962019-07-15 16:25:33 +02003936 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3937 break;
3938 if (type == HTX_BLK_DATA)
3939 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003940 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003941 if (htx->extra != ULLONG_MAX)
3942 len += htx->extra;
3943
3944 /* Stores the request path. */
3945 lua_pushstring(L, "length");
3946 lua_pushinteger(L, len);
3947 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003948
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003949 /* Create an empty array of HTTP request headers. */
3950 lua_pushstring(L, "response");
3951 lua_newtable(L);
3952 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003953
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003954 /* Pop a class stream metatable and affect it to the table. */
3955 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3956 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003957
3958 return 1;
3959}
3960
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003961__LJMP static int hlua_applet_http_set_var(lua_State *L)
3962{
3963 struct hlua_appctx *appctx;
3964 struct stream *s;
3965 const char *name;
3966 size_t len;
3967 struct sample smp;
3968
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003969 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3970 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003971
3972 /* It is useles to retrieve the stream, but this function
3973 * runs only in a stream context.
3974 */
3975 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3976 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3977 s = appctx->htxn.s;
3978
3979 /* Converts the third argument in a sample. */
3980 hlua_lua2smp(L, 3, &smp);
3981
3982 /* Store the sample in a variable. */
3983 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003984
3985 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3986 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3987 else
3988 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3989
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003990 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003991}
3992
3993__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3994{
3995 struct hlua_appctx *appctx;
3996 struct stream *s;
3997 const char *name;
3998 size_t len;
3999 struct sample smp;
4000
4001 MAY_LJMP(check_args(L, 2, "unset_var"));
4002
4003 /* It is useles to retrieve the stream, but this function
4004 * runs only in a stream context.
4005 */
4006 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4007 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4008 s = appctx->htxn.s;
4009
4010 /* Unset the variable. */
4011 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004012 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4013 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004014}
4015
4016__LJMP static int hlua_applet_http_get_var(lua_State *L)
4017{
4018 struct hlua_appctx *appctx;
4019 struct stream *s;
4020 const char *name;
4021 size_t len;
4022 struct sample smp;
4023
4024 MAY_LJMP(check_args(L, 2, "get_var"));
4025
4026 /* It is useles to retrieve the stream, but this function
4027 * runs only in a stream context.
4028 */
4029 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4030 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4031 s = appctx->htxn.s;
4032
4033 smp_set_owner(&smp, s->be, s->sess, s, 0);
4034 if (!vars_get_by_name(name, len, &smp)) {
4035 lua_pushnil(L);
4036 return 1;
4037 }
4038
4039 return hlua_smp2lua(L, &smp);
4040}
4041
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004042__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4043{
4044 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4045 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004046 struct hlua *hlua;
4047
4048 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004049 if (!s->hlua)
4050 return 0;
4051 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004052
4053 MAY_LJMP(check_args(L, 2, "set_priv"));
4054
4055 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004056 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004057
4058 /* Get and store new value. */
4059 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4060 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4061
4062 return 0;
4063}
4064
4065__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4066{
4067 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4068 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004069 struct hlua *hlua;
4070
4071 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004072 if (!s->hlua) {
4073 lua_pushnil(L);
4074 return 1;
4075 }
4076 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004077
4078 /* Push configuration index in the stack. */
4079 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4080
4081 return 1;
4082}
4083
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004084/* If expected data not yet available, it returns a yield. This function
4085 * consumes the data in the buffer. It returns a string containing the
4086 * data. This string can be empty.
4087 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004088__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004089{
4090 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4091 struct stream_interface *si = appctx->appctx->owner;
4092 struct channel *req = si_oc(si);
4093 struct htx *htx;
4094 struct htx_blk *blk;
4095 size_t count;
4096 int stop = 0;
4097
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004098 htx = htx_from_buf(&req->buf);
4099 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004100 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004101
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004102 while (count && !stop && blk) {
4103 enum htx_blk_type type = htx_get_blk_type(blk);
4104 uint32_t sz = htx_get_blksz(blk);
4105 struct ist v;
4106 uint32_t vlen;
4107 char *nl;
4108
Christopher Faulete461e342018-12-18 16:43:35 +01004109 if (type == HTX_BLK_EOM) {
4110 stop = 1;
4111 break;
4112 }
4113
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004114 vlen = sz;
4115 if (vlen > count) {
4116 if (type != HTX_BLK_DATA)
4117 break;
4118 vlen = count;
4119 }
4120
4121 switch (type) {
4122 case HTX_BLK_UNUSED:
4123 break;
4124
4125 case HTX_BLK_DATA:
4126 v = htx_get_blk_value(htx, blk);
4127 v.len = vlen;
4128 nl = istchr(v, '\n');
4129 if (nl != NULL) {
4130 stop = 1;
4131 vlen = nl - v.ptr + 1;
4132 }
4133 luaL_addlstring(&appctx->b, v.ptr, vlen);
4134 break;
4135
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004136 case HTX_BLK_TLR:
4137 case HTX_BLK_EOM:
4138 stop = 1;
4139 break;
4140
4141 default:
4142 break;
4143 }
4144
4145 co_set_data(req, co_data(req) - vlen);
4146 count -= vlen;
4147 if (sz == vlen)
4148 blk = htx_remove_blk(htx, blk);
4149 else {
4150 htx_cut_data_blk(htx, blk, vlen);
4151 break;
4152 }
4153 }
4154
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004155 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004156 if (!stop) {
4157 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004158 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004159 }
4160
4161 /* return the result. */
4162 luaL_pushresult(&appctx->b);
4163 return 1;
4164}
4165
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004166
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004167/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004168__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004169{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004170 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004171
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004172 /* Initialise the string catenation. */
4173 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004174
Christopher Fauleta2097962019-07-15 16:25:33 +02004175 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004176}
4177
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004178/* If expected data not yet available, it returns a yield. This function
4179 * consumes the data in the buffer. It returns a string containing the
4180 * data. This string can be empty.
4181 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004182__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004183{
4184 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4185 struct stream_interface *si = appctx->appctx->owner;
4186 struct channel *req = si_oc(si);
4187 struct htx *htx;
4188 struct htx_blk *blk;
4189 size_t count;
4190 int len;
4191
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004192 htx = htx_from_buf(&req->buf);
4193 len = MAY_LJMP(luaL_checkinteger(L, 2));
4194 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004195 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004196 while (count && len && blk) {
4197 enum htx_blk_type type = htx_get_blk_type(blk);
4198 uint32_t sz = htx_get_blksz(blk);
4199 struct ist v;
4200 uint32_t vlen;
4201
Christopher Faulete461e342018-12-18 16:43:35 +01004202 if (type == HTX_BLK_EOM) {
4203 len = 0;
4204 break;
4205 }
4206
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004207 vlen = sz;
4208 if (len > 0 && vlen > len)
4209 vlen = len;
4210 if (vlen > count) {
4211 if (type != HTX_BLK_DATA)
4212 break;
4213 vlen = count;
4214 }
4215
4216 switch (type) {
4217 case HTX_BLK_UNUSED:
4218 break;
4219
4220 case HTX_BLK_DATA:
4221 v = htx_get_blk_value(htx, blk);
4222 luaL_addlstring(&appctx->b, v.ptr, vlen);
4223 break;
4224
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004225 case HTX_BLK_TLR:
4226 case HTX_BLK_EOM:
4227 len = 0;
4228 break;
4229
4230 default:
4231 break;
4232 }
4233
4234 co_set_data(req, co_data(req) - vlen);
4235 count -= vlen;
4236 if (len > 0)
4237 len -= vlen;
4238 if (sz == vlen)
4239 blk = htx_remove_blk(htx, blk);
4240 else {
4241 htx_cut_data_blk(htx, blk, vlen);
4242 break;
4243 }
4244 }
4245
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004246 htx_to_buf(htx, &req->buf);
4247
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004248 /* If we are no other data available, yield waiting for new data. */
4249 if (len) {
4250 if (len > 0) {
4251 lua_pushinteger(L, len);
4252 lua_replace(L, 2);
4253 }
4254 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004255 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004256 }
4257
4258 /* return the result. */
4259 luaL_pushresult(&appctx->b);
4260 return 1;
4261}
4262
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004263/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004264__LJMP static int hlua_applet_http_recv(lua_State *L)
4265{
4266 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4267 int len = -1;
4268
4269 /* Check arguments. */
4270 if (lua_gettop(L) > 2)
4271 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4272 if (lua_gettop(L) >= 2) {
4273 len = MAY_LJMP(luaL_checkinteger(L, 2));
4274 lua_pop(L, 1);
4275 }
4276
Christopher Fauleta2097962019-07-15 16:25:33 +02004277 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004278
Christopher Fauleta2097962019-07-15 16:25:33 +02004279 /* Initialise the string catenation. */
4280 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004281
Christopher Fauleta2097962019-07-15 16:25:33 +02004282 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004283}
4284
4285/* Append data in the output side of the buffer. This data is immediately
4286 * sent. The function returns the amount of data written. If the buffer
4287 * cannot contain the data, the function yields. The function returns -1
4288 * if the channel is closed.
4289 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004290__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004291{
4292 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4293 struct stream_interface *si = appctx->appctx->owner;
4294 struct channel *res = si_ic(si);
4295 struct htx *htx = htx_from_buf(&res->buf);
4296 const char *data;
4297 size_t len;
4298 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4299 int max;
4300
Christopher Faulet9060fc02019-07-03 11:39:30 +02004301 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004302 if (!max)
4303 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004304
4305 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4306
4307 /* Get the max amount of data which can write as input in the channel. */
4308 if (max > (len - l))
4309 max = len - l;
4310
4311 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004312 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004313 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004314
4315 /* update counters. */
4316 l += max;
4317 lua_pop(L, 1);
4318 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004319
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004320 /* If some data is not send, declares the situation to the
4321 * applet, and returns a yield.
4322 */
4323 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004324 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004325 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004326 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004327 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004328 }
4329
Christopher Fauleta2097962019-07-15 16:25:33 +02004330 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004331 return 1;
4332}
4333
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004334/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004335 * yield the LUA process, and resume it without checking the
4336 * input arguments.
4337 */
4338__LJMP static int hlua_applet_http_send(lua_State *L)
4339{
4340 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004341
4342 /* We want to send some data. Headers must be sent. */
4343 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4344 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4345 WILL_LJMP(lua_error(L));
4346 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004347
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004348 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004349 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004350
Christopher Fauleta2097962019-07-15 16:25:33 +02004351 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004352}
4353
4354__LJMP static int hlua_applet_http_addheader(lua_State *L)
4355{
4356 const char *name;
4357 int ret;
4358
4359 MAY_LJMP(hlua_checkapplet_http(L, 1));
4360 name = MAY_LJMP(luaL_checkstring(L, 2));
4361 MAY_LJMP(luaL_checkstring(L, 3));
4362
4363 /* Push in the stack the "response" entry. */
4364 ret = lua_getfield(L, 1, "response");
4365 if (ret != LUA_TTABLE) {
4366 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4367 "is expected as an array. %s found", lua_typename(L, ret));
4368 WILL_LJMP(lua_error(L));
4369 }
4370
4371 /* check if the header is already registered if it is not
4372 * the case, register it.
4373 */
4374 ret = lua_getfield(L, -1, name);
4375 if (ret == LUA_TNIL) {
4376
4377 /* Entry not found. */
4378 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4379
4380 /* Insert the new header name in the array in the top of the stack.
4381 * It left the new array in the top of the stack.
4382 */
4383 lua_newtable(L);
4384 lua_pushvalue(L, 2);
4385 lua_pushvalue(L, -2);
4386 lua_settable(L, -4);
4387
4388 } else if (ret != LUA_TTABLE) {
4389
4390 /* corruption error. */
4391 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4392 "is expected as an array. %s found", name, lua_typename(L, ret));
4393 WILL_LJMP(lua_error(L));
4394 }
4395
4396 /* Now the top od thestack is an array of values. We push
4397 * the header value as new entry.
4398 */
4399 lua_pushvalue(L, 3);
4400 ret = lua_rawlen(L, -2);
4401 lua_rawseti(L, -2, ret + 1);
4402 lua_pushboolean(L, 1);
4403 return 1;
4404}
4405
4406__LJMP static int hlua_applet_http_status(lua_State *L)
4407{
4408 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4409 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004410 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004411
4412 if (status < 100 || status > 599) {
4413 lua_pushboolean(L, 0);
4414 return 1;
4415 }
4416
4417 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004418 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004419 lua_pushboolean(L, 1);
4420 return 1;
4421}
4422
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004423
Christopher Fauleta2097962019-07-15 16:25:33 +02004424__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004425{
4426 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4427 struct stream_interface *si = appctx->appctx->owner;
4428 struct channel *res = si_ic(si);
4429 struct htx *htx;
4430 struct htx_sl *sl;
4431 struct h1m h1m;
4432 const char *status, *reason;
4433 const char *name, *value;
4434 size_t nlen, vlen;
4435 unsigned int flags;
4436
4437 /* Send the message at once. */
4438 htx = htx_from_buf(&res->buf);
4439 h1m_init_res(&h1m);
4440
4441 /* Use the same http version than the request. */
4442 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4443 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4444 if (reason == NULL)
4445 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4446 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4447 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4448 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4449 }
4450 else {
4451 flags = HTX_SL_F_IS_RESP;
4452 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4453 }
4454 if (!sl) {
4455 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4456 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4457 WILL_LJMP(lua_error(L));
4458 }
4459 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4460
4461 /* Get the array associated to the field "response" in the object AppletHTTP. */
4462 lua_pushvalue(L, 0);
4463 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4464 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4465 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4466 WILL_LJMP(lua_error(L));
4467 }
4468
4469 /* Browse the list of headers. */
4470 lua_pushnil(L);
4471 while(lua_next(L, -2) != 0) {
4472 /* We expect a string as -2. */
4473 if (lua_type(L, -2) != LUA_TSTRING) {
4474 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4475 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4476 lua_typename(L, lua_type(L, -2)));
4477 WILL_LJMP(lua_error(L));
4478 }
4479 name = lua_tolstring(L, -2, &nlen);
4480
4481 /* We expect an array as -1. */
4482 if (lua_type(L, -1) != LUA_TTABLE) {
4483 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4484 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4485 name,
4486 lua_typename(L, lua_type(L, -1)));
4487 WILL_LJMP(lua_error(L));
4488 }
4489
4490 /* Browse the table who is on the top of the stack. */
4491 lua_pushnil(L);
4492 while(lua_next(L, -2) != 0) {
4493 int id;
4494
4495 /* We expect a number as -2. */
4496 if (lua_type(L, -2) != LUA_TNUMBER) {
4497 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4498 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4499 name,
4500 lua_typename(L, lua_type(L, -2)));
4501 WILL_LJMP(lua_error(L));
4502 }
4503 id = lua_tointeger(L, -2);
4504
4505 /* We expect a string as -2. */
4506 if (lua_type(L, -1) != LUA_TSTRING) {
4507 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4508 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4509 name, id,
4510 lua_typename(L, lua_type(L, -1)));
4511 WILL_LJMP(lua_error(L));
4512 }
4513 value = lua_tolstring(L, -1, &vlen);
4514
4515 /* Simple Protocol checks. */
4516 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004517 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004518 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4519 struct ist v = ist2(value, vlen);
4520 int ret;
4521
4522 ret = h1_parse_cont_len_header(&h1m, &v);
4523 if (ret < 0) {
4524 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4525 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4526 name);
4527 WILL_LJMP(lua_error(L));
4528 }
4529 else if (ret == 0)
4530 goto next; /* Skip it */
4531 }
4532
4533 /* Add a new header */
4534 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4535 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4536 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4537 name);
4538 WILL_LJMP(lua_error(L));
4539 }
4540 next:
4541 /* Remove the array from the stack, and get next element with a remaining string. */
4542 lua_pop(L, 1);
4543 }
4544
4545 /* Remove the array from the stack, and get next element with a remaining string. */
4546 lua_pop(L, 1);
4547 }
4548
4549 if (h1m.flags & H1_MF_CHNK)
4550 h1m.flags &= ~H1_MF_CLEN;
4551 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4552 h1m.flags |= H1_MF_XFER_LEN;
4553
4554 /* Uset HTX start-line flags */
4555 if (h1m.flags & H1_MF_XFER_ENC)
4556 flags |= HTX_SL_F_XFER_ENC;
4557 if (h1m.flags & H1_MF_XFER_LEN) {
4558 flags |= HTX_SL_F_XFER_LEN;
4559 if (h1m.flags & H1_MF_CHNK)
4560 flags |= HTX_SL_F_CHNK;
4561 else if (h1m.flags & H1_MF_CLEN)
4562 flags |= HTX_SL_F_CLEN;
4563 if (h1m.body_len == 0)
4564 flags |= HTX_SL_F_BODYLESS;
4565 }
4566 sl->flags |= flags;
4567
4568 /* If we dont have a content-length set, and the HTTP version is 1.1
4569 * and the status code implies the presence of a message body, we must
4570 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004571 * for the keepalive compliance. If the applet announces a transfer-encoding
4572 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004573 */
4574 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4575 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4576 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4577 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4578 /* Add a new header */
4579 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4580 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4581 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4582 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4583 WILL_LJMP(lua_error(L));
4584 }
4585 }
4586
4587 /* Finalize headers. */
4588 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4589 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4590 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4591 WILL_LJMP(lua_error(L));
4592 }
4593
4594 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4595 b_reset(&res->buf);
4596 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4597 WILL_LJMP(lua_error(L));
4598 }
4599
4600 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004601 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004602
4603 /* Headers sent, set the flag. */
4604 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4605 return 0;
4606
4607}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004608/* We will build the status line and the headers of the HTTP response.
4609 * We will try send at once if its not possible, we give back the hand
4610 * waiting for more room.
4611 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004612__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004613{
4614 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4615 struct stream_interface *si = appctx->appctx->owner;
4616 struct channel *res = si_ic(si);
4617
4618 if (co_data(res)) {
4619 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004620 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004621 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004622 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004623}
4624
4625
Christopher Fauleta2097962019-07-15 16:25:33 +02004626__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004627{
Christopher Fauleta2097962019-07-15 16:25:33 +02004628 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004629}
4630
Christopher Fauleta2097962019-07-15 16:25:33 +02004631/*
4632 *
4633 *
4634 * Class HTTP
4635 *
4636 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004637 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004638
4639/* Returns a struct hlua_txn if the stack entry "ud" is
4640 * a class stream, otherwise it throws an error.
4641 */
4642__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004643{
Christopher Fauleta2097962019-07-15 16:25:33 +02004644 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4645}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004646
Christopher Fauleta2097962019-07-15 16:25:33 +02004647/* This function creates and push in the stack a HTTP object
4648 * according with a current TXN.
4649 */
4650static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4651{
4652 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004653
Christopher Fauleta2097962019-07-15 16:25:33 +02004654 /* Check stack size. */
4655 if (!lua_checkstack(L, 3))
4656 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004657
Christopher Fauleta2097962019-07-15 16:25:33 +02004658 /* Create the object: obj[0] = userdata.
4659 * Note that the base of the Converters object is the
4660 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004661 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004662 lua_newtable(L);
4663 htxn = lua_newuserdata(L, sizeof(*htxn));
4664 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004665
4666 htxn->s = txn->s;
4667 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004668 htxn->dir = txn->dir;
4669 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004670
4671 /* Pop a class stream metatable and affect it to the table. */
4672 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4673 lua_setmetatable(L, -2);
4674
4675 return 1;
4676}
4677
4678/* This function creates ans returns an array of HTTP headers.
4679 * This function does not fails. It is used as wrapper with the
4680 * 2 following functions.
4681 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004682__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004683{
Christopher Fauleta2097962019-07-15 16:25:33 +02004684 struct htx *htx;
4685 int32_t pos;
4686
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004687 /* Create the table. */
4688 lua_newtable(L);
4689
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004690
Christopher Fauleta2097962019-07-15 16:25:33 +02004691 htx = htxbuf(&msg->chn->buf);
4692 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4693 struct htx_blk *blk = htx_get_blk(htx, pos);
4694 enum htx_blk_type type = htx_get_blk_type(blk);
4695 struct ist n, v;
4696 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004697
Christopher Fauleta2097962019-07-15 16:25:33 +02004698 if (type == HTX_BLK_HDR) {
4699 n = htx_get_blk_name(htx,blk);
4700 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004701 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004702 else if (type == HTX_BLK_EOH)
4703 break;
4704 else
4705 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004706
Christopher Fauleta2097962019-07-15 16:25:33 +02004707 /* Check for existing entry:
4708 * assume that the table is on the top of the stack, and
4709 * push the key in the stack, the function lua_gettable()
4710 * perform the lookup.
4711 */
4712 lua_pushlstring(L, n.ptr, n.len);
4713 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004714
Christopher Fauleta2097962019-07-15 16:25:33 +02004715 switch (lua_type(L, -1)) {
4716 case LUA_TNIL:
4717 /* Table not found, create it. */
4718 lua_pop(L, 1); /* remove the nil value. */
4719 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4720 lua_newtable(L); /* create and push empty table. */
4721 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4722 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4723 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004724 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004725
Christopher Fauleta2097962019-07-15 16:25:33 +02004726 case LUA_TTABLE:
4727 /* Entry found: push the value in the table. */
4728 len = lua_rawlen(L, -1);
4729 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4730 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4731 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4732 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004733
Christopher Fauleta2097962019-07-15 16:25:33 +02004734 default:
4735 /* Other cases are errors. */
4736 hlua_pusherror(L, "internal error during the parsing of headers.");
4737 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004738 }
4739 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004740 return 1;
4741}
4742
4743__LJMP static int hlua_http_req_get_headers(lua_State *L)
4744{
4745 struct hlua_txn *htxn;
4746
4747 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4748 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4749
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004750 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004751 WILL_LJMP(lua_error(L));
4752
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004753 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004754}
4755
4756__LJMP static int hlua_http_res_get_headers(lua_State *L)
4757{
4758 struct hlua_txn *htxn;
4759
4760 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4761 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4762
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004763 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004764 WILL_LJMP(lua_error(L));
4765
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004766 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004767}
4768
4769/* This function replace full header, or just a value in
4770 * the request or in the response. It is a wrapper fir the
4771 * 4 following functions.
4772 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004773__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004774{
4775 size_t name_len;
4776 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4777 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4778 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004779 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004780 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004781
Dragan Dosen26743032019-04-30 15:54:36 +02004782 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004783 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4784
Christopher Fauleta2097962019-07-15 16:25:33 +02004785 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004786 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004787 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004788 return 0;
4789}
4790
4791__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4792{
4793 struct hlua_txn *htxn;
4794
4795 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4796 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4797
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004798 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004799 WILL_LJMP(lua_error(L));
4800
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004801 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004802}
4803
4804__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4805{
4806 struct hlua_txn *htxn;
4807
4808 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4809 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4810
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004811 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004812 WILL_LJMP(lua_error(L));
4813
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004814 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004815}
4816
4817__LJMP static int hlua_http_req_rep_val(lua_State *L)
4818{
4819 struct hlua_txn *htxn;
4820
4821 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4822 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4823
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004824 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004825 WILL_LJMP(lua_error(L));
4826
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004827 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004828}
4829
4830__LJMP static int hlua_http_res_rep_val(lua_State *L)
4831{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004832 struct hlua_txn *htxn;
4833
4834 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4835 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4836
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004837 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004838 WILL_LJMP(lua_error(L));
4839
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004840 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004841}
4842
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004843/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004844 * It is a wrapper for the 2 following functions.
4845 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004846__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004847{
4848 size_t len;
4849 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004850 struct htx *htx = htxbuf(&msg->chn->buf);
4851 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852
Christopher Fauleta2097962019-07-15 16:25:33 +02004853 ctx.blk = NULL;
4854 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4855 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004856 return 0;
4857}
4858
4859__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4860{
4861 struct hlua_txn *htxn;
4862
4863 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4864 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4865
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004866 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004867 WILL_LJMP(lua_error(L));
4868
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004869 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004870}
4871
4872__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4873{
4874 struct hlua_txn *htxn;
4875
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004876 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004877 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4878
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004879 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004880 WILL_LJMP(lua_error(L));
4881
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004882 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004883}
4884
4885/* This function adds an header. It is a wrapper used by
4886 * the 2 following functions.
4887 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004888__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004889{
4890 size_t name_len;
4891 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4892 size_t value_len;
4893 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004894 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004895
Christopher Fauleta2097962019-07-15 16:25:33 +02004896 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4897 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004898 return 0;
4899}
4900
4901__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4902{
4903 struct hlua_txn *htxn;
4904
4905 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4906 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4907
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004908 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004909 WILL_LJMP(lua_error(L));
4910
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004911 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004912}
4913
4914__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4915{
4916 struct hlua_txn *htxn;
4917
4918 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4919 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4920
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004921 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004922 WILL_LJMP(lua_error(L));
4923
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004924 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004925}
4926
4927static int hlua_http_req_set_hdr(lua_State *L)
4928{
4929 struct hlua_txn *htxn;
4930
4931 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4932 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4933
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004934 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004935 WILL_LJMP(lua_error(L));
4936
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004937 hlua_http_del_hdr(L, &htxn->s->txn->req);
4938 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004939}
4940
4941static int hlua_http_res_set_hdr(lua_State *L)
4942{
4943 struct hlua_txn *htxn;
4944
4945 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4946 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4947
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004948 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004949 WILL_LJMP(lua_error(L));
4950
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004951 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4952 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004953}
4954
4955/* This function set the method. */
4956static int hlua_http_req_set_meth(lua_State *L)
4957{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004958 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004959 size_t name_len;
4960 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004961
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004962 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004963 WILL_LJMP(lua_error(L));
4964
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004965 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004966 return 1;
4967}
4968
4969/* This function set the method. */
4970static int hlua_http_req_set_path(lua_State *L)
4971{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004972 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004973 size_t name_len;
4974 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004975
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004976 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004977 WILL_LJMP(lua_error(L));
4978
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004979 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004980 return 1;
4981}
4982
4983/* This function set the query-string. */
4984static int hlua_http_req_set_query(lua_State *L)
4985{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004986 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004987 size_t name_len;
4988 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004989
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004990 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004991 WILL_LJMP(lua_error(L));
4992
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004993 /* Check length. */
4994 if (name_len > trash.size - 1) {
4995 lua_pushboolean(L, 0);
4996 return 1;
4997 }
4998
4999 /* Add the mark question as prefix. */
5000 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005001 trash.area[trash.data++] = '?';
5002 memcpy(trash.area + trash.data, name, name_len);
5003 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005004
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005005 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005006 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005007 return 1;
5008}
5009
5010/* This function set the uri. */
5011static int hlua_http_req_set_uri(lua_State *L)
5012{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005013 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005014 size_t name_len;
5015 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005016
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005017 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005018 WILL_LJMP(lua_error(L));
5019
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005020 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005021 return 1;
5022}
5023
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005024/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005025static int hlua_http_res_set_status(lua_State *L)
5026{
5027 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5028 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005029 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5030 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005031
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005032 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005033 WILL_LJMP(lua_error(L));
5034
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005035 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005036 return 0;
5037}
5038
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005039/*
5040 *
5041 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005042 * Class TXN
5043 *
5044 *
5045 */
5046
5047/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005048 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005049 */
5050__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5051{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005052 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005053}
5054
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005055__LJMP static int hlua_set_var(lua_State *L)
5056{
5057 struct hlua_txn *htxn;
5058 const char *name;
5059 size_t len;
5060 struct sample smp;
5061
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005062 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5063 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005064
5065 /* It is useles to retrieve the stream, but this function
5066 * runs only in a stream context.
5067 */
5068 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5069 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5070
5071 /* Converts the third argument in a sample. */
5072 hlua_lua2smp(L, 3, &smp);
5073
5074 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005075 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005076
5077 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5078 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5079 else
5080 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5081
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005082 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005083}
5084
Christopher Faulet85d79c92016-11-09 16:54:56 +01005085__LJMP static int hlua_unset_var(lua_State *L)
5086{
5087 struct hlua_txn *htxn;
5088 const char *name;
5089 size_t len;
5090 struct sample smp;
5091
5092 MAY_LJMP(check_args(L, 2, "unset_var"));
5093
5094 /* It is useles to retrieve the stream, but this function
5095 * runs only in a stream context.
5096 */
5097 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5098 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5099
5100 /* Unset the variable. */
5101 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005102 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5103 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005104}
5105
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005106__LJMP static int hlua_get_var(lua_State *L)
5107{
5108 struct hlua_txn *htxn;
5109 const char *name;
5110 size_t len;
5111 struct sample smp;
5112
5113 MAY_LJMP(check_args(L, 2, "get_var"));
5114
5115 /* It is useles to retrieve the stream, but this function
5116 * runs only in a stream context.
5117 */
5118 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5119 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5120
Willy Tarreau7560dd42016-03-10 16:28:58 +01005121 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005122 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005123 lua_pushnil(L);
5124 return 1;
5125 }
5126
5127 return hlua_smp2lua(L, &smp);
5128}
5129
Willy Tarreau59551662015-03-10 14:23:13 +01005130__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005131{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005132 struct hlua *hlua;
5133
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005134 MAY_LJMP(check_args(L, 2, "set_priv"));
5135
Willy Tarreau87b09662015-04-03 00:22:06 +02005136 /* It is useles to retrieve the stream, but this function
5137 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005138 */
5139 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005140 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005141
5142 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005143 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005144
5145 /* Get and store new value. */
5146 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5147 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5148
5149 return 0;
5150}
5151
Willy Tarreau59551662015-03-10 14:23:13 +01005152__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005153{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005154 struct hlua *hlua;
5155
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005156 MAY_LJMP(check_args(L, 1, "get_priv"));
5157
Willy Tarreau87b09662015-04-03 00:22:06 +02005158 /* It is useles to retrieve the stream, but this function
5159 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005160 */
5161 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005162 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005163
5164 /* Push configuration index in the stack. */
5165 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5166
5167 return 1;
5168}
5169
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005170/* Create stack entry containing a class TXN. This function
5171 * return 0 if the stack does not contains free slots,
5172 * otherwise it returns 1.
5173 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005174static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005175{
Willy Tarreaude491382015-04-06 11:04:28 +02005176 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005177
5178 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005179 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005180 return 0;
5181
5182 /* NOTE: The allocation never fails. The failure
5183 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005184 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005185 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005186 /* Create the object: obj[0] = userdata. */
5187 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005188 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005189 lua_rawseti(L, -2, 0);
5190
Willy Tarreaude491382015-04-06 11:04:28 +02005191 htxn->s = s;
5192 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005193 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005194 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005195
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005196 /* Create the "f" field that contains a list of fetches. */
5197 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005198 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005199 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005200 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005201
5202 /* Create the "sf" field that contains a list of stringsafe fetches. */
5203 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005204 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005205 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005206 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005207
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005208 /* Create the "c" field that contains a list of converters. */
5209 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005210 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005211 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005212 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005213
5214 /* Create the "sc" field that contains a list of stringsafe converters. */
5215 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005216 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005217 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005218 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005219
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005220 /* Create the "req" field that contains the request channel object. */
5221 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005222 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005223 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005224 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005225
5226 /* Create the "res" field that contains the response channel object. */
5227 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005228 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005229 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005230 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005231
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005232 /* Creates the HTTP object is the current proxy allows http. */
5233 lua_pushstring(L, "http");
5234 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005235 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005236 return 0;
5237 }
5238 else
5239 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005240 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005241
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005242 /* Pop a class sesison metatable and affect it to the userdata. */
5243 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5244 lua_setmetatable(L, -2);
5245
5246 return 1;
5247}
5248
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005249__LJMP static int hlua_txn_deflog(lua_State *L)
5250{
5251 const char *msg;
5252 struct hlua_txn *htxn;
5253
5254 MAY_LJMP(check_args(L, 2, "deflog"));
5255 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5256 msg = MAY_LJMP(luaL_checkstring(L, 2));
5257
5258 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5259 return 0;
5260}
5261
5262__LJMP static int hlua_txn_log(lua_State *L)
5263{
5264 int level;
5265 const char *msg;
5266 struct hlua_txn *htxn;
5267
5268 MAY_LJMP(check_args(L, 3, "log"));
5269 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5270 level = MAY_LJMP(luaL_checkinteger(L, 2));
5271 msg = MAY_LJMP(luaL_checkstring(L, 3));
5272
5273 if (level < 0 || level >= NB_LOG_LEVELS)
5274 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5275
5276 hlua_sendlog(htxn->s->be, level, msg);
5277 return 0;
5278}
5279
5280__LJMP static int hlua_txn_log_debug(lua_State *L)
5281{
5282 const char *msg;
5283 struct hlua_txn *htxn;
5284
5285 MAY_LJMP(check_args(L, 2, "Debug"));
5286 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5287 msg = MAY_LJMP(luaL_checkstring(L, 2));
5288 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5289 return 0;
5290}
5291
5292__LJMP static int hlua_txn_log_info(lua_State *L)
5293{
5294 const char *msg;
5295 struct hlua_txn *htxn;
5296
5297 MAY_LJMP(check_args(L, 2, "Info"));
5298 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5299 msg = MAY_LJMP(luaL_checkstring(L, 2));
5300 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5301 return 0;
5302}
5303
5304__LJMP static int hlua_txn_log_warning(lua_State *L)
5305{
5306 const char *msg;
5307 struct hlua_txn *htxn;
5308
5309 MAY_LJMP(check_args(L, 2, "Warning"));
5310 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5311 msg = MAY_LJMP(luaL_checkstring(L, 2));
5312 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5313 return 0;
5314}
5315
5316__LJMP static int hlua_txn_log_alert(lua_State *L)
5317{
5318 const char *msg;
5319 struct hlua_txn *htxn;
5320
5321 MAY_LJMP(check_args(L, 2, "Alert"));
5322 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5323 msg = MAY_LJMP(luaL_checkstring(L, 2));
5324 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5325 return 0;
5326}
5327
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005328__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5329{
5330 struct hlua_txn *htxn;
5331 int ll;
5332
5333 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5334 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5335 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5336
5337 if (ll < 0 || ll > 7)
5338 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5339
5340 htxn->s->logs.level = ll;
5341 return 0;
5342}
5343
5344__LJMP static int hlua_txn_set_tos(lua_State *L)
5345{
5346 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005347 int tos;
5348
5349 MAY_LJMP(check_args(L, 2, "set_tos"));
5350 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5351 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5352
Willy Tarreau1a18b542018-12-11 16:37:42 +01005353 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005354 return 0;
5355}
5356
5357__LJMP static int hlua_txn_set_mark(lua_State *L)
5358{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005359 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005360 int mark;
5361
5362 MAY_LJMP(check_args(L, 2, "set_mark"));
5363 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5364 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5365
Lukas Tribus579e3e32019-08-11 18:03:45 +02005366 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005367 return 0;
5368}
5369
Patrick Hemmer268a7072018-05-11 12:52:31 -04005370__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5371{
5372 struct hlua_txn *htxn;
5373
5374 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5375 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5376 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5377 return 0;
5378}
5379
5380__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5381{
5382 struct hlua_txn *htxn;
5383
5384 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5385 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5386 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5387 return 0;
5388}
5389
Christopher Faulet700d9e82020-01-31 12:21:52 +01005390/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005391 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005392 * message and terminate the transaction. It returns 1 on success and 0 on
5393 * error. The Reply must be on top of the stack.
5394 */
5395__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5396{
5397 struct htx *htx;
5398 struct htx_sl *sl;
5399 struct h1m h1m;
5400 const char *status, *reason, *body;
5401 size_t status_len, reason_len, body_len;
5402 int ret, code, flags;
5403
5404 code = 200;
5405 status = "200";
5406 status_len = 3;
5407 ret = lua_getfield(L, -1, "status");
5408 if (ret == LUA_TNUMBER) {
5409 code = lua_tointeger(L, -1);
5410 status = lua_tolstring(L, -1, &status_len);
5411 }
5412 lua_pop(L, 1);
5413
5414 reason = http_get_reason(code);
5415 reason_len = strlen(reason);
5416 ret = lua_getfield(L, -1, "reason");
5417 if (ret == LUA_TSTRING)
5418 reason = lua_tolstring(L, -1, &reason_len);
5419 lua_pop(L, 1);
5420
5421 body = NULL;
5422 body_len = 0;
5423 ret = lua_getfield(L, -1, "body");
5424 if (ret == LUA_TSTRING)
5425 body = lua_tolstring(L, -1, &body_len);
5426 lua_pop(L, 1);
5427
5428 /* Prepare the response before inserting the headers */
5429 h1m_init_res(&h1m);
5430 htx = htx_from_buf(&s->res.buf);
5431 channel_htx_truncate(&s->res, htx);
5432 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5433 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5434 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5435 ist2(status, status_len), ist2(reason, reason_len));
5436 }
5437 else {
5438 flags = HTX_SL_F_IS_RESP;
5439 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5440 ist2(status, status_len), ist2(reason, reason_len));
5441 }
5442 if (!sl)
5443 goto fail;
5444 sl->info.res.status = code;
5445
5446 /* Push in the stack the "headers" entry. */
5447 ret = lua_getfield(L, -1, "headers");
5448 if (ret != LUA_TTABLE)
5449 goto skip_headers;
5450
5451 lua_pushnil(L);
5452 while (lua_next(L, -2) != 0) {
5453 struct ist name, value;
5454 const char *n, *v;
5455 size_t nlen, vlen;
5456
5457 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5458 /* Skip element if the key is not a string or if the value is not a table */
5459 goto next_hdr;
5460 }
5461
5462 n = lua_tolstring(L, -2, &nlen);
5463 name = ist2(n, nlen);
5464 if (isteqi(name, ist("content-length"))) {
5465 /* Always skip content-length header. It will be added
5466 * later with the correct len
5467 */
5468 goto next_hdr;
5469 }
5470
5471 /* Loop on header's values */
5472 lua_pushnil(L);
5473 while (lua_next(L, -2)) {
5474 if (!lua_isstring(L, -1)) {
5475 /* Skip the value if it is not a string */
5476 goto next_value;
5477 }
5478
5479 v = lua_tolstring(L, -1, &vlen);
5480 value = ist2(v, vlen);
5481
5482 if (isteqi(name, ist("transfer-encoding")))
5483 h1_parse_xfer_enc_header(&h1m, value);
5484 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5485 goto fail;
5486
5487 next_value:
5488 lua_pop(L, 1);
5489 }
5490
5491 next_hdr:
5492 lua_pop(L, 1);
5493 }
5494 skip_headers:
5495 lua_pop(L, 1);
5496
5497 /* Update h1m flags: CLEN is set if CHNK is not present */
5498 if (!(h1m.flags & H1_MF_CHNK)) {
5499 const char *clen = ultoa(body_len);
5500
5501 h1m.flags |= H1_MF_CLEN;
5502 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5503 goto fail;
5504 }
5505 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5506 h1m.flags |= H1_MF_XFER_LEN;
5507
5508 /* Update HTX start-line flags */
5509 if (h1m.flags & H1_MF_XFER_ENC)
5510 flags |= HTX_SL_F_XFER_ENC;
5511 if (h1m.flags & H1_MF_XFER_LEN) {
5512 flags |= HTX_SL_F_XFER_LEN;
5513 if (h1m.flags & H1_MF_CHNK)
5514 flags |= HTX_SL_F_CHNK;
5515 else if (h1m.flags & H1_MF_CLEN)
5516 flags |= HTX_SL_F_CLEN;
5517 if (h1m.body_len == 0)
5518 flags |= HTX_SL_F_BODYLESS;
5519 }
5520 sl->flags |= flags;
5521
5522
5523 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5524 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5525 !htx_add_endof(htx, HTX_BLK_EOM))
5526 goto fail;
5527
5528 /* Now, forward the response and terminate the transaction */
5529 s->txn->status = code;
5530 htx_to_buf(htx, &s->res.buf);
5531 if (!http_forward_proxy_resp(s, 1))
5532 goto fail;
5533
5534 return 1;
5535
5536 fail:
5537 channel_htx_truncate(&s->res, htx);
5538 return 0;
5539}
5540
5541/* Terminate a transaction if called from a lua action. For TCP streams,
5542 * processing is just aborted. Nothing is returned to the client and all
5543 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5544 * is forwarded to the client before terminating the transaction. On success,
5545 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5546 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5547 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005548 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005549__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005550{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005551 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005552 struct stream *s;
5553 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005554
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005555 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005556
Christopher Faulet700d9e82020-01-31 12:21:52 +01005557 /* If the flags NOTERM is set, we cannot terminate the session, so we
5558 * just end the execution of the current lua code. */
5559 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005560 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005561
Christopher Faulet700d9e82020-01-31 12:21:52 +01005562 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005563 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005564 struct channel *req = &s->req;
5565 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005566
Christopher Faulet700d9e82020-01-31 12:21:52 +01005567 channel_auto_read(req);
5568 channel_abort(req);
5569 channel_auto_close(req);
5570 channel_erase(req);
5571
5572 res->wex = tick_add_ifset(now_ms, res->wto);
5573 channel_auto_read(res);
5574 channel_auto_close(res);
5575 channel_shutr_now(res);
5576
5577 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5578 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005579 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005580
Christopher Faulet700d9e82020-01-31 12:21:52 +01005581 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5582 /* No reply or invalid reply */
5583 s->txn->status = 0;
5584 http_reply_and_close(s, 0, NULL);
5585 }
5586 else {
5587 /* Remove extra args to have the reply on top of the stack */
5588 if (lua_gettop(L) > 2)
5589 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005590
Christopher Faulet700d9e82020-01-31 12:21:52 +01005591 if (!hlua_txn_forward_reply(L, s)) {
5592 if (!(s->flags & SF_ERR_MASK))
5593 s->flags |= SF_ERR_PRXCOND;
5594 lua_pushinteger(L, ACT_RET_ERR);
5595 WILL_LJMP(hlua_done(L));
5596 return 0; /* Never reached */
5597 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005598 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005599
Christopher Faulet700d9e82020-01-31 12:21:52 +01005600 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5601 if (htxn->dir == SMP_OPT_DIR_REQ) {
5602 /* let's log the request time */
5603 s->logs.tv_request = now;
5604 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5605 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5606 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005607
Christopher Faulet700d9e82020-01-31 12:21:52 +01005608 done:
5609 if (!(s->flags & SF_ERR_MASK))
5610 s->flags |= SF_ERR_LOCAL;
5611 if (!(s->flags & SF_FINST_MASK))
5612 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005613
Christopher Faulet4ad73102020-03-05 11:07:31 +01005614 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005615 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005616 return 0;
5617}
5618
Christopher Faulet700d9e82020-01-31 12:21:52 +01005619/*
5620 *
5621 *
5622 * Class REPLY
5623 *
5624 *
5625 */
5626
5627/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5628 * free slots, the function fails and returns 0;
5629 */
5630static int hlua_txn_reply_new(lua_State *L)
5631{
5632 struct hlua_txn *htxn;
5633 const char *reason, *body = NULL;
5634 int ret, status;
5635
5636 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005637 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005638 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5639 WILL_LJMP(lua_error(L));
5640 }
5641
5642 /* Default value */
5643 status = 200;
5644 reason = http_get_reason(status);
5645
5646 if (lua_istable(L, 2)) {
5647 /* load status and reason from the table argument at index 2 */
5648 ret = lua_getfield(L, 2, "status");
5649 if (ret == LUA_TNIL)
5650 goto reason;
5651 else if (ret != LUA_TNUMBER) {
5652 /* invalid status: ignore the reason */
5653 goto body;
5654 }
5655 status = lua_tointeger(L, -1);
5656
5657 reason:
5658 lua_pop(L, 1); /* restore the stack: remove status */
5659 ret = lua_getfield(L, 2, "reason");
5660 if (ret == LUA_TSTRING)
5661 reason = lua_tostring(L, -1);
5662
5663 body:
5664 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5665 ret = lua_getfield(L, 2, "body");
5666 if (ret == LUA_TSTRING)
5667 body = lua_tostring(L, -1);
5668 lua_pop(L, 1); /* restore the stack: remove body */
5669 }
5670
5671 /* Create the Reply table */
5672 lua_newtable(L);
5673
5674 /* Add status element */
5675 lua_pushstring(L, "status");
5676 lua_pushinteger(L, status);
5677 lua_settable(L, -3);
5678
5679 /* Add reason element */
5680 reason = http_get_reason(status);
5681 lua_pushstring(L, "reason");
5682 lua_pushstring(L, reason);
5683 lua_settable(L, -3);
5684
5685 /* Add body element, nil if undefined */
5686 lua_pushstring(L, "body");
5687 if (body)
5688 lua_pushstring(L, body);
5689 else
5690 lua_pushnil(L);
5691 lua_settable(L, -3);
5692
5693 /* Add headers element */
5694 lua_pushstring(L, "headers");
5695 lua_newtable(L);
5696
5697 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5698 if (lua_istable(L, 2)) {
5699 /* load headers from the table argument at index 2. If it is a table, copy it. */
5700 ret = lua_getfield(L, 2, "headers");
5701 if (ret == LUA_TTABLE) {
5702 /* stack: [ ... <headers:table>, <table> ] */
5703 lua_pushnil(L);
5704 while (lua_next(L, -2) != 0) {
5705 /* stack: [ ... <headers:table>, <table>, k, v] */
5706 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5707 /* invalid value type, skip it */
5708 lua_pop(L, 1);
5709 continue;
5710 }
5711
5712
5713 /* Duplicate the key and swap it with the value. */
5714 lua_pushvalue(L, -2);
5715 lua_insert(L, -2);
5716 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5717
5718 lua_newtable(L);
5719 lua_insert(L, -2);
5720 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5721
5722 if (lua_isstring(L, -1)) {
5723 /* push the value in the inner table */
5724 lua_rawseti(L, -2, 1);
5725 }
5726 else { /* table */
5727 lua_pushnil(L);
5728 while (lua_next(L, -2) != 0) {
5729 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5730 if (!lua_isstring(L, -1)) {
5731 /* invalid value type, skip it*/
5732 lua_pop(L, 1);
5733 continue;
5734 }
5735 /* push the value in the inner table */
5736 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5737 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5738 }
5739 lua_pop(L, 1);
5740 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5741 }
5742
5743 /* push (k,v) on the stack in the headers table:
5744 * stack: [ ... <headers:table>, <table>, k, k, v ]
5745 */
5746 lua_settable(L, -5);
5747 /* stack: [ ... <headers:table>, <table>, k ] */
5748 }
5749 }
5750 lua_pop(L, 1);
5751 }
5752 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5753 lua_settable(L, -3);
5754 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5755
5756 /* Pop a class sesison metatable and affect it to the userdata. */
5757 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5758 lua_setmetatable(L, -2);
5759 return 1;
5760}
5761
5762/* Set the reply status code, and optionally the reason. If no reason is
5763 * provided, the default one corresponding to the status code is used.
5764 */
5765__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5766{
5767 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5768 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5769
5770 /* First argument (self) must be a table */
5771 luaL_checktype(L, 1, LUA_TTABLE);
5772
5773 if (status < 100 || status > 599) {
5774 lua_pushboolean(L, 0);
5775 return 1;
5776 }
5777 if (!reason)
5778 reason = http_get_reason(status);
5779
5780 lua_pushinteger(L, status);
5781 lua_setfield(L, 1, "status");
5782
5783 lua_pushstring(L, reason);
5784 lua_setfield(L, 1, "reason");
5785
5786 lua_pushboolean(L, 1);
5787 return 1;
5788}
5789
5790/* Add a header into the reply object. Each header name is associated to an
5791 * array of values in the "headers" table. If the header name is not found, a
5792 * new entry is created.
5793 */
5794__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5795{
5796 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5797 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5798 int ret;
5799
5800 /* First argument (self) must be a table */
5801 luaL_checktype(L, 1, LUA_TTABLE);
5802
5803 /* Push in the stack the "headers" entry. */
5804 ret = lua_getfield(L, 1, "headers");
5805 if (ret != LUA_TTABLE) {
5806 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5807 WILL_LJMP(lua_error(L));
5808 }
5809
5810 /* check if the header is already registered. If not, register it. */
5811 ret = lua_getfield(L, -1, name);
5812 if (ret == LUA_TNIL) {
5813 /* Entry not found. */
5814 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5815
5816 /* Insert the new header name in the array in the top of the stack.
5817 * It left the new array in the top of the stack.
5818 */
5819 lua_newtable(L);
5820 lua_pushstring(L, name);
5821 lua_pushvalue(L, -2);
5822 lua_settable(L, -4);
5823 }
5824 else if (ret != LUA_TTABLE) {
5825 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5826 WILL_LJMP(lua_error(L));
5827 }
5828
5829 /* Now the top od thestack is an array of values. We push
5830 * the header value as new entry.
5831 */
5832 lua_pushstring(L, value);
5833 ret = lua_rawlen(L, -2);
5834 lua_rawseti(L, -2, ret + 1);
5835
5836 lua_pushboolean(L, 1);
5837 return 1;
5838}
5839
5840/* Remove all occurrences of a given header name. */
5841__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5842{
5843 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5844 int ret;
5845
5846 /* First argument (self) must be a table */
5847 luaL_checktype(L, 1, LUA_TTABLE);
5848
5849 /* Push in the stack the "headers" entry. */
5850 ret = lua_getfield(L, 1, "headers");
5851 if (ret != LUA_TTABLE) {
5852 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5853 WILL_LJMP(lua_error(L));
5854 }
5855
5856 lua_pushstring(L, name);
5857 lua_pushnil(L);
5858 lua_settable(L, -3);
5859
5860 lua_pushboolean(L, 1);
5861 return 1;
5862}
5863
5864/* Set the reply's body. Overwrite any existing entry. */
5865__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5866{
5867 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5868
5869 /* First argument (self) must be a table */
5870 luaL_checktype(L, 1, LUA_TTABLE);
5871
5872 lua_pushstring(L, payload);
5873 lua_setfield(L, 1, "body");
5874
5875 lua_pushboolean(L, 1);
5876 return 1;
5877}
5878
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005879__LJMP static int hlua_log(lua_State *L)
5880{
5881 int level;
5882 const char *msg;
5883
5884 MAY_LJMP(check_args(L, 2, "log"));
5885 level = MAY_LJMP(luaL_checkinteger(L, 1));
5886 msg = MAY_LJMP(luaL_checkstring(L, 2));
5887
5888 if (level < 0 || level >= NB_LOG_LEVELS)
5889 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5890
5891 hlua_sendlog(NULL, level, msg);
5892 return 0;
5893}
5894
5895__LJMP static int hlua_log_debug(lua_State *L)
5896{
5897 const char *msg;
5898
5899 MAY_LJMP(check_args(L, 1, "debug"));
5900 msg = MAY_LJMP(luaL_checkstring(L, 1));
5901 hlua_sendlog(NULL, LOG_DEBUG, msg);
5902 return 0;
5903}
5904
5905__LJMP static int hlua_log_info(lua_State *L)
5906{
5907 const char *msg;
5908
5909 MAY_LJMP(check_args(L, 1, "info"));
5910 msg = MAY_LJMP(luaL_checkstring(L, 1));
5911 hlua_sendlog(NULL, LOG_INFO, msg);
5912 return 0;
5913}
5914
5915__LJMP static int hlua_log_warning(lua_State *L)
5916{
5917 const char *msg;
5918
5919 MAY_LJMP(check_args(L, 1, "warning"));
5920 msg = MAY_LJMP(luaL_checkstring(L, 1));
5921 hlua_sendlog(NULL, LOG_WARNING, msg);
5922 return 0;
5923}
5924
5925__LJMP static int hlua_log_alert(lua_State *L)
5926{
5927 const char *msg;
5928
5929 MAY_LJMP(check_args(L, 1, "alert"));
5930 msg = MAY_LJMP(luaL_checkstring(L, 1));
5931 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005932 return 0;
5933}
5934
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005935__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005936{
5937 int wakeup_ms = lua_tointeger(L, -1);
5938 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005939 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005940 return 0;
5941}
5942
5943__LJMP static int hlua_sleep(lua_State *L)
5944{
5945 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005946 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005947
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005948 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005949
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005950 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005951 wakeup_ms = tick_add(now_ms, delay);
5952 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005953
Willy Tarreau9635e032018-10-16 17:52:55 +02005954 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005955 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005956}
5957
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005958__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005959{
5960 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005961 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005962
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005963 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005964
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005965 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005966 wakeup_ms = tick_add(now_ms, delay);
5967 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005968
Willy Tarreau9635e032018-10-16 17:52:55 +02005969 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005970 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005971}
5972
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005973/* This functionis an LUA binding. it permits to give back
5974 * the hand at the HAProxy scheduler. It is used when the
5975 * LUA processing consumes a lot of time.
5976 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005977__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005978{
5979 return 0;
5980}
5981
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005982__LJMP static int hlua_yield(lua_State *L)
5983{
Willy Tarreau9635e032018-10-16 17:52:55 +02005984 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005985 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005986}
5987
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005988/* This function change the nice of the currently executed
5989 * task. It is used set low or high priority at the current
5990 * task.
5991 */
Willy Tarreau59551662015-03-10 14:23:13 +01005992__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005993{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005994 struct hlua *hlua;
5995 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005996
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005997 MAY_LJMP(check_args(L, 1, "set_nice"));
5998 hlua = hlua_gethlua(L);
5999 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006000
6001 /* If he task is not set, I'm in a start mode. */
6002 if (!hlua || !hlua->task)
6003 return 0;
6004
6005 if (nice < -1024)
6006 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006007 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006008 nice = 1024;
6009
6010 hlua->task->nice = nice;
6011 return 0;
6012}
6013
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006014/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006015 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6016 * return an E_AGAIN signal, the emmiter of this signal must set a
6017 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006018 *
6019 * Task wrapper are longjmp safe because the only one Lua code
6020 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006021 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006022struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006023{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006024 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006025 enum hlua_exec status;
6026
Christopher Faulet5bc99722018-04-25 10:34:45 +02006027 if (task->thread_mask == MAX_THREADS_MASK)
6028 task_set_affinity(task, tid_bit);
6029
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006030 /* If it is the first call to the task, we must initialize the
6031 * execution timeouts.
6032 */
6033 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006034 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006035
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006036 /* Execute the Lua code. */
6037 status = hlua_ctx_resume(hlua, 1);
6038
6039 switch (status) {
6040 /* finished or yield */
6041 case HLUA_E_OK:
6042 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006043 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006044 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006045 break;
6046
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006047 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006048 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006049 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006050 break;
6051
6052 /* finished with error. */
6053 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006054 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006055 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006056 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006057 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006058 break;
6059
6060 case HLUA_E_ERR:
6061 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006062 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006063 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006064 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006065 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006066 break;
6067 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006068 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006069}
6070
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006071/* This function is an LUA binding that register LUA function to be
6072 * executed after the HAProxy configuration parsing and before the
6073 * HAProxy scheduler starts. This function expect only one LUA
6074 * argument that is a function. This function returns nothing, but
6075 * throws if an error is encountered.
6076 */
6077__LJMP static int hlua_register_init(lua_State *L)
6078{
6079 struct hlua_init_function *init;
6080 int ref;
6081
6082 MAY_LJMP(check_args(L, 1, "register_init"));
6083
6084 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6085
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006086 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006087 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006088 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006089
6090 init->function_ref = ref;
6091 LIST_ADDQ(&hlua_init_functions, &init->l);
6092 return 0;
6093}
6094
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006095/* This functio is an LUA binding. It permits to register a task
6096 * executed in parallel of the main HAroxy activity. The task is
6097 * created and it is set in the HAProxy scheduler. It can be called
6098 * from the "init" section, "post init" or during the runtime.
6099 *
6100 * Lua prototype:
6101 *
6102 * <none> core.register_task(<function>)
6103 */
6104static int hlua_register_task(lua_State *L)
6105{
6106 struct hlua *hlua;
6107 struct task *task;
6108 int ref;
6109
6110 MAY_LJMP(check_args(L, 1, "register_task"));
6111
6112 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6113
Willy Tarreaubafbe012017-11-24 17:34:44 +01006114 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006115 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006116 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006117
Emeric Brunc60def82017-09-27 14:59:38 +02006118 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006119 if (!task)
6120 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6121
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006122 task->context = hlua;
6123 task->process = hlua_process_task;
6124
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006125 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006126 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006127
6128 /* Restore the function in the stack. */
6129 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6130 hlua->nargs = 0;
6131
6132 /* Schedule task. */
6133 task_schedule(task, now_ms);
6134
6135 return 0;
6136}
6137
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006138/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6139 * doesn't allow "yield" functions because the HAProxy engine cannot
6140 * resume converters.
6141 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006142static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006143{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006144 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006145 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006146 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006147
Willy Tarreaube508f12016-03-10 11:47:01 +01006148 if (!stream)
6149 return 0;
6150
Willy Tarreau87b09662015-04-03 00:22:06 +02006151 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006152 * Lua context can be not initialized. This behavior
6153 * permits to save performances because a systematic
6154 * Lua initialization cause 5% performances loss.
6155 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006156 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006157 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006158 if (!stream->hlua) {
6159 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6160 return 0;
6161 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006162 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006163 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6164 return 0;
6165 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006166 }
6167
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006168 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006169 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006170
6171 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006172 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6173 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6174 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006175 else
6176 error = "critical error";
6177 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006178 return 0;
6179 }
6180
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006181 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006182 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006183 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006184 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006185 return 0;
6186 }
6187
6188 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006189 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006190
6191 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006192 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006193 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006194 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006195 return 0;
6196 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006197 hlua_smp2lua(stream->hlua->T, smp);
6198 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006199
6200 /* push keywords in the stack. */
6201 if (arg_p) {
6202 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006203 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006204 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006205 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006206 return 0;
6207 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006208 hlua_arg2lua(stream->hlua->T, arg_p);
6209 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006210 }
6211 }
6212
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006213 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006214 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006215
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006216 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006217 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006218 }
6219
6220 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006221 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006222 /* finished. */
6223 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006224 /* If the stack is empty, the function fails. */
6225 if (lua_gettop(stream->hlua->T) <= 0)
6226 return 0;
6227
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006228 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006229 hlua_lua2smp(stream->hlua->T, -1, smp);
6230 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006231 return 1;
6232
6233 /* yield. */
6234 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006235 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006236 return 0;
6237
6238 /* finished with error. */
6239 case HLUA_E_ERRMSG:
6240 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006241 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006242 fcn->name, lua_tostring(stream->hlua->T, -1));
6243 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006244 return 0;
6245
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006246 case HLUA_E_ETMOUT:
6247 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6248 return 0;
6249
6250 case HLUA_E_NOMEM:
6251 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6252 return 0;
6253
6254 case HLUA_E_YIELD:
6255 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6256 return 0;
6257
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006258 case HLUA_E_ERR:
6259 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006260 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006261
6262 default:
6263 return 0;
6264 }
6265}
6266
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006267/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6268 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006269 * resume sample-fetches. This function will be called by the sample
6270 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006271 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006272static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6273 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006274{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006275 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006276 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006277 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006278 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006279
Willy Tarreaube508f12016-03-10 11:47:01 +01006280 if (!stream)
6281 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006282
Willy Tarreau87b09662015-04-03 00:22:06 +02006283 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006284 * Lua context can be not initialized. This behavior
6285 * permits to save performances because a systematic
6286 * Lua initialization cause 5% performances loss.
6287 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006288 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006289 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006290 if (!stream->hlua) {
6291 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6292 return 0;
6293 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006294 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006295 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6296 return 0;
6297 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006298 }
6299
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006300 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006301 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006302
6303 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006304 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6305 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6306 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006307 else
6308 error = "critical error";
6309 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006310 return 0;
6311 }
6312
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006313 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006314 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006315 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006316 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006317 return 0;
6318 }
6319
6320 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006321 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006322
6323 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006324 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006325 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006326 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006327 return 0;
6328 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006329 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006330
6331 /* push keywords in the stack. */
6332 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6333 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006334 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006335 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006336 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006337 return 0;
6338 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006339 hlua_arg2lua(stream->hlua->T, arg_p);
6340 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006341 }
6342
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006343 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006344 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006345
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006346 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006347 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006348 }
6349
6350 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006351 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006352 /* finished. */
6353 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006354 /* If the stack is empty, the function fails. */
6355 if (lua_gettop(stream->hlua->T) <= 0)
6356 return 0;
6357
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006358 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006359 hlua_lua2smp(stream->hlua->T, -1, smp);
6360 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006361
6362 /* Set the end of execution flag. */
6363 smp->flags &= ~SMP_F_MAY_CHANGE;
6364 return 1;
6365
6366 /* yield. */
6367 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006368 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006369 return 0;
6370
6371 /* finished with error. */
6372 case HLUA_E_ERRMSG:
6373 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006374 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006375 fcn->name, lua_tostring(stream->hlua->T, -1));
6376 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006377 return 0;
6378
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006379 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006380 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6381 return 0;
6382
6383 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006384 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6385 return 0;
6386
6387 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006388 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6389 return 0;
6390
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006391 case HLUA_E_ERR:
6392 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006393 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006394
6395 default:
6396 return 0;
6397 }
6398}
6399
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006400/* This function is an LUA binding used for registering
6401 * "sample-conv" functions. It expects a converter name used
6402 * in the haproxy configuration file, and an LUA function.
6403 */
6404__LJMP static int hlua_register_converters(lua_State *L)
6405{
6406 struct sample_conv_kw_list *sck;
6407 const char *name;
6408 int ref;
6409 int len;
6410 struct hlua_function *fcn;
6411
6412 MAY_LJMP(check_args(L, 2, "register_converters"));
6413
6414 /* First argument : converter name. */
6415 name = MAY_LJMP(luaL_checkstring(L, 1));
6416
6417 /* Second argument : lua function. */
6418 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6419
6420 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006421 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006422 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006423 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006424 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006425 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006426 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006427
6428 /* Fill fcn. */
6429 fcn->name = strdup(name);
6430 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006431 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006432 fcn->function_ref = ref;
6433
6434 /* List head */
6435 sck->list.n = sck->list.p = NULL;
6436
6437 /* converter keyword. */
6438 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006439 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006440 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006441 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006442
6443 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6444 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006445 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 +01006446 sck->kw[0].val_args = NULL;
6447 sck->kw[0].in_type = SMP_T_STR;
6448 sck->kw[0].out_type = SMP_T_STR;
6449 sck->kw[0].private = fcn;
6450
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006451 /* Register this new converter */
6452 sample_register_convs(sck);
6453
6454 return 0;
6455}
6456
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006457/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006458 * "sample-fetch" functions. It expects a converter name used
6459 * in the haproxy configuration file, and an LUA function.
6460 */
6461__LJMP static int hlua_register_fetches(lua_State *L)
6462{
6463 const char *name;
6464 int ref;
6465 int len;
6466 struct sample_fetch_kw_list *sfk;
6467 struct hlua_function *fcn;
6468
6469 MAY_LJMP(check_args(L, 2, "register_fetches"));
6470
6471 /* First argument : sample-fetch name. */
6472 name = MAY_LJMP(luaL_checkstring(L, 1));
6473
6474 /* Second argument : lua function. */
6475 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6476
6477 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006478 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006479 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006480 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006481 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006482 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006483 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006484
6485 /* Fill fcn. */
6486 fcn->name = strdup(name);
6487 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006488 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006489 fcn->function_ref = ref;
6490
6491 /* List head */
6492 sfk->list.n = sfk->list.p = NULL;
6493
6494 /* sample-fetch keyword. */
6495 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006496 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006497 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006498 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006499
6500 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6501 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006502 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 +01006503 sfk->kw[0].val_args = NULL;
6504 sfk->kw[0].out_type = SMP_T_STR;
6505 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6506 sfk->kw[0].val = 0;
6507 sfk->kw[0].private = fcn;
6508
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006509 /* Register this new fetch. */
6510 sample_register_fetches(sfk);
6511
6512 return 0;
6513}
6514
Christopher Faulet501465d2020-02-26 14:54:16 +01006515/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006516 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006517__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006518{
6519 struct hlua *hlua = hlua_gethlua(L);
6520 unsigned int delay;
6521 unsigned int wakeup_ms;
6522
6523 MAY_LJMP(check_args(L, 1, "wake_time"));
6524
6525 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6526 wakeup_ms = tick_add(now_ms, delay);
6527 hlua->wake_time = wakeup_ms;
6528 return 0;
6529}
6530
6531
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006532/* This function is a wrapper to execute each LUA function declared as an action
6533 * wrapper during the initialisation period. This function may return any
6534 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6535 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6536 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006537 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006538static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006539 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006540{
6541 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006542 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006543 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006544 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006545
6546 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006547 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6548 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6549 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6550 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006551 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006552 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006553 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006554 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006555
Willy Tarreau87b09662015-04-03 00:22:06 +02006556 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006557 * Lua context can be not initialized. This behavior
6558 * permits to save performances because a systematic
6559 * Lua initialization cause 5% performances loss.
6560 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006561 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006562 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006563 if (!s->hlua) {
6564 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6565 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006566 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006568 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006569 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6570 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006571 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006572 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006573 }
6574
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006575 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006576 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006577
6578 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006579 if (!SET_SAFE_LJMP(s->hlua->T)) {
6580 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6581 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006582 else
6583 error = "critical error";
6584 SEND_ERR(px, "Lua function '%s': %s.\n",
6585 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006586 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006587 }
6588
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006589 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006590 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006591 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006592 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006593 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006594 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006595 }
6596
6597 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006598 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006599
Willy Tarreau87b09662015-04-03 00:22:06 +02006600 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006601 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006602 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006603 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006604 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006605 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006606 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006607 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006608
6609 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006610 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006611 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006612 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006613 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006614 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006615 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006616 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006617 lua_pushstring(s->hlua->T, *arg);
6618 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006619 }
6620
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006621 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006622 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006623
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006624 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006625 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006626 }
6627
Christopher Faulet23308eb2020-06-02 18:46:07 +02006628 /* Always reset the analyse expiration timeout for the corresponding
6629 * channel in case the lua script yield, to be sure to not keep an
6630 * expired timeout.
6631 */
6632 if (dir == SMP_OPT_DIR_REQ)
6633 s->req.analyse_exp = TICK_ETERNITY;
6634 else
6635 s->res.analyse_exp = TICK_ETERNITY;
6636
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006637 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006638 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006639 /* finished. */
6640 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006641 /* Catch the return value */
6642 if (lua_gettop(s->hlua->T) > 0)
6643 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006644
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006645 /* Set timeout in the required channel. */
6646 if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) {
6647 if (dir == SMP_OPT_DIR_REQ)
6648 s->req.analyse_exp = s->hlua->wake_time;
6649 else
6650 s->res.analyse_exp = s->hlua->wake_time;
6651 }
6652 goto end;
6653
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006654 /* yield. */
6655 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006656 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006657 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Faulet81921b12019-08-14 23:19:45 +02006658 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006659 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006660 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006661 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006662 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006663 /* Some actions can be wake up when a "write" event
6664 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006665 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006666 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006667 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006668 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006669 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006670 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006671 act_ret = ACT_RET_YIELD;
6672 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006673
6674 /* finished with error. */
6675 case HLUA_E_ERRMSG:
6676 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006677 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006678 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6679 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006680 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006681
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006682 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006683 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006684 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006685
6686 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006687 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006688 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006689
6690 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006691 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6692 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006693 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006694
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006695 case HLUA_E_ERR:
6696 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006697 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006698 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006699
6700 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006701 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006702 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006703
6704 end:
6705 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006706}
6707
Olivier Houchard9f6af332018-05-25 14:04:04 +02006708struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006709{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006710 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006711
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006712 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006713 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006714 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006715}
6716
6717static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6718{
6719 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006720 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006721 struct task *task;
6722 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006723 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006724
Willy Tarreaubafbe012017-11-24 17:34:44 +01006725 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006726 if (!hlua) {
6727 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6728 ctx->rule->arg.hlua_rule->fcn.name);
6729 return 0;
6730 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006731 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006732 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006733 ctx->ctx.hlua_apptcp.flags = 0;
6734
6735 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006736 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006737 if (!task) {
6738 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6739 ctx->rule->arg.hlua_rule->fcn.name);
6740 return 0;
6741 }
6742 task->nice = 0;
6743 task->context = ctx;
6744 task->process = hlua_applet_wakeup;
6745 ctx->ctx.hlua_apptcp.task = task;
6746
6747 /* In the execution wrappers linked with a stream, the
6748 * Lua context can be not initialized. This behavior
6749 * permits to save performances because a systematic
6750 * Lua initialization cause 5% performances loss.
6751 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006752 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006753 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6754 ctx->rule->arg.hlua_rule->fcn.name);
6755 return 0;
6756 }
6757
6758 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006759 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006760
6761 /* The following Lua calls can fail. */
6762 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006763 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6764 error = lua_tostring(hlua->T, -1);
6765 else
6766 error = "critical error";
6767 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6768 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006769 return 0;
6770 }
6771
6772 /* Check stack available size. */
6773 if (!lua_checkstack(hlua->T, 1)) {
6774 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6775 ctx->rule->arg.hlua_rule->fcn.name);
6776 RESET_SAFE_LJMP(hlua->T);
6777 return 0;
6778 }
6779
6780 /* Restore the function in the stack. */
6781 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6782
6783 /* Create and and push object stream in the stack. */
6784 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6785 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6786 ctx->rule->arg.hlua_rule->fcn.name);
6787 RESET_SAFE_LJMP(hlua->T);
6788 return 0;
6789 }
6790 hlua->nargs = 1;
6791
6792 /* push keywords in the stack. */
6793 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6794 if (!lua_checkstack(hlua->T, 1)) {
6795 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6796 ctx->rule->arg.hlua_rule->fcn.name);
6797 RESET_SAFE_LJMP(hlua->T);
6798 return 0;
6799 }
6800 lua_pushstring(hlua->T, *arg);
6801 hlua->nargs++;
6802 }
6803
6804 RESET_SAFE_LJMP(hlua->T);
6805
6806 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006807 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006808 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006809
6810 return 1;
6811}
6812
Willy Tarreau60409db2019-08-21 14:14:50 +02006813void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006814{
6815 struct stream_interface *si = ctx->owner;
6816 struct stream *strm = si_strm(si);
6817 struct channel *res = si_ic(si);
6818 struct act_rule *rule = ctx->rule;
6819 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006820 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006821
6822 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006823 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6824 /* eat the whole request */
6825 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006826 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006827 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006828
6829 /* If the stream is disconnect or closed, ldo nothing. */
6830 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6831 return;
6832
6833 /* Execute the function. */
6834 switch (hlua_ctx_resume(hlua, 1)) {
6835 /* finished. */
6836 case HLUA_E_OK:
6837 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6838
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006839 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006840 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006841 res->flags |= CF_READ_NULL;
6842 si_shutr(si);
6843 return;
6844
6845 /* yield. */
6846 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006847 if (hlua->wake_time != TICK_ETERNITY)
6848 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006849 return;
6850
6851 /* finished with error. */
6852 case HLUA_E_ERRMSG:
6853 /* Display log. */
6854 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6855 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6856 lua_pop(hlua->T, 1);
6857 goto error;
6858
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006859 case HLUA_E_ETMOUT:
6860 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6861 rule->arg.hlua_rule->fcn.name);
6862 goto error;
6863
6864 case HLUA_E_NOMEM:
6865 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6866 rule->arg.hlua_rule->fcn.name);
6867 goto error;
6868
6869 case HLUA_E_YIELD: /* unexpected */
6870 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6871 rule->arg.hlua_rule->fcn.name);
6872 goto error;
6873
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006874 case HLUA_E_ERR:
6875 /* Display log. */
6876 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6877 rule->arg.hlua_rule->fcn.name);
6878 goto error;
6879
6880 default:
6881 goto error;
6882 }
6883
6884error:
6885
6886 /* For all other cases, just close the stream. */
6887 si_shutw(si);
6888 si_shutr(si);
6889 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6890}
6891
6892static void hlua_applet_tcp_release(struct appctx *ctx)
6893{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006894 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006895 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006896 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006897 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006898}
6899
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006900/* The function returns 1 if the initialisation is complete, 0 if
6901 * an errors occurs and -1 if more data are required for initializing
6902 * the applet.
6903 */
6904static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6905{
6906 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006907 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006908 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006909 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006910 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006911 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006912
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006913 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006914 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006915 if (!hlua) {
6916 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6917 ctx->rule->arg.hlua_rule->fcn.name);
6918 return 0;
6919 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006920 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006921 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006922 ctx->ctx.hlua_apphttp.left_bytes = -1;
6923 ctx->ctx.hlua_apphttp.flags = 0;
6924
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006925 if (txn->req.flags & HTTP_MSGF_VER_11)
6926 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6927
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006928 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006929 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006930 if (!task) {
6931 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6932 ctx->rule->arg.hlua_rule->fcn.name);
6933 return 0;
6934 }
6935 task->nice = 0;
6936 task->context = ctx;
6937 task->process = hlua_applet_wakeup;
6938 ctx->ctx.hlua_apphttp.task = task;
6939
6940 /* In the execution wrappers linked with a stream, the
6941 * Lua context can be not initialized. This behavior
6942 * permits to save performances because a systematic
6943 * Lua initialization cause 5% performances loss.
6944 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006945 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006946 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6947 ctx->rule->arg.hlua_rule->fcn.name);
6948 return 0;
6949 }
6950
6951 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006952 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006953
6954 /* The following Lua calls can fail. */
6955 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006956 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6957 error = lua_tostring(hlua->T, -1);
6958 else
6959 error = "critical error";
6960 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6961 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006962 return 0;
6963 }
6964
6965 /* Check stack available size. */
6966 if (!lua_checkstack(hlua->T, 1)) {
6967 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6968 ctx->rule->arg.hlua_rule->fcn.name);
6969 RESET_SAFE_LJMP(hlua->T);
6970 return 0;
6971 }
6972
6973 /* Restore the function in the stack. */
6974 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6975
6976 /* Create and and push object stream in the stack. */
6977 if (!hlua_applet_http_new(hlua->T, ctx)) {
6978 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6979 ctx->rule->arg.hlua_rule->fcn.name);
6980 RESET_SAFE_LJMP(hlua->T);
6981 return 0;
6982 }
6983 hlua->nargs = 1;
6984
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006985 /* push keywords in the stack. */
6986 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6987 if (!lua_checkstack(hlua->T, 1)) {
6988 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6989 ctx->rule->arg.hlua_rule->fcn.name);
6990 RESET_SAFE_LJMP(hlua->T);
6991 return 0;
6992 }
6993 lua_pushstring(hlua->T, *arg);
6994 hlua->nargs++;
6995 }
6996
6997 RESET_SAFE_LJMP(hlua->T);
6998
6999 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007000 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007001
7002 return 1;
7003}
7004
Willy Tarreau60409db2019-08-21 14:14:50 +02007005void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007006{
7007 struct stream_interface *si = ctx->owner;
7008 struct stream *strm = si_strm(si);
7009 struct channel *req = si_oc(si);
7010 struct channel *res = si_ic(si);
7011 struct act_rule *rule = ctx->rule;
7012 struct proxy *px = strm->be;
7013 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7014 struct htx *req_htx, *res_htx;
7015
7016 res_htx = htx_from_buf(&res->buf);
7017
7018 /* If the stream is disconnect or closed, ldo nothing. */
7019 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7020 goto out;
7021
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007022 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007023 if (!b_size(&res->buf)) {
7024 si_rx_room_blk(si);
7025 goto out;
7026 }
7027 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007028 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007029 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7030
7031 /* Set the currently running flag. */
7032 if (!HLUA_IS_RUNNING(hlua) &&
7033 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7034 struct htx_blk *blk;
7035 size_t count = co_data(req);
7036
7037 if (!count) {
7038 si_cant_get(si);
7039 goto out;
7040 }
7041
7042 /* We need to flush the request header. This left the body for
7043 * the Lua.
7044 */
7045 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007046 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007047 while (count && blk) {
7048 enum htx_blk_type type = htx_get_blk_type(blk);
7049 uint32_t sz = htx_get_blksz(blk);
7050
7051 if (sz > count) {
7052 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007053 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007054 goto out;
7055 }
7056
7057 count -= sz;
7058 co_set_data(req, co_data(req) - sz);
7059 blk = htx_remove_blk(req_htx, blk);
7060
7061 if (type == HTX_BLK_EOH)
7062 break;
7063 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007064 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007065 }
7066
7067 /* Executes The applet if it is not done. */
7068 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7069
7070 /* Execute the function. */
7071 switch (hlua_ctx_resume(hlua, 1)) {
7072 /* finished. */
7073 case HLUA_E_OK:
7074 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7075 break;
7076
7077 /* yield. */
7078 case HLUA_E_AGAIN:
7079 if (hlua->wake_time != TICK_ETERNITY)
7080 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007081 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007082
7083 /* finished with error. */
7084 case HLUA_E_ERRMSG:
7085 /* Display log. */
7086 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7087 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7088 lua_pop(hlua->T, 1);
7089 goto error;
7090
7091 case HLUA_E_ETMOUT:
7092 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7093 rule->arg.hlua_rule->fcn.name);
7094 goto error;
7095
7096 case HLUA_E_NOMEM:
7097 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7098 rule->arg.hlua_rule->fcn.name);
7099 goto error;
7100
7101 case HLUA_E_YIELD: /* unexpected */
7102 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7103 rule->arg.hlua_rule->fcn.name);
7104 goto error;
7105
7106 case HLUA_E_ERR:
7107 /* Display log. */
7108 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7109 rule->arg.hlua_rule->fcn.name);
7110 goto error;
7111
7112 default:
7113 goto error;
7114 }
7115 }
7116
7117 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007118 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7119 goto done;
7120
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007121 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7122 goto error;
7123
Christopher Faulet54b5e212019-06-04 10:08:28 +02007124 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007125 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7126 si_rx_room_blk(si);
7127 goto out;
7128 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007129 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007130 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7131 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007132 }
7133
7134 done:
7135 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007136 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007137 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007138 si_shutr(si);
7139 }
7140
7141 /* eat the whole request */
7142 if (co_data(req)) {
7143 req_htx = htx_from_buf(&req->buf);
7144 co_htx_skip(req, req_htx, co_data(req));
7145 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007146 }
7147 }
7148
7149 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007150 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007151 return;
7152
7153 error:
7154
7155 /* If we are in HTTP mode, and we are not send any
7156 * data, return a 500 server error in best effort:
7157 * if there is no room available in the buffer,
7158 * just close the connection.
7159 */
7160 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007161 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007162
7163 channel_erase(res);
7164 res->buf.data = b_data(err);
7165 memcpy(res->buf.area, b_head(err), b_data(err));
7166 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007167 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007168 }
7169 if (!(strm->flags & SF_ERR_MASK))
7170 strm->flags |= SF_ERR_RESOURCE;
7171 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7172 goto done;
7173}
7174
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007175static void hlua_applet_http_release(struct appctx *ctx)
7176{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007177 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007178 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007179 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007180 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007181}
7182
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007183/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007184 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007185 *
7186 * This function can fail with an abort() due to an Lua critical error.
7187 * We are in the configuration parsing process of HAProxy, this abort() is
7188 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007189 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007190static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7191 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007192{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007193 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007194 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007195
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007196 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007197 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007198 if (!rule->arg.hlua_rule) {
7199 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007200 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007201 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007202
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007203 /* Memory for arguments. */
7204 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7205 if (!rule->arg.hlua_rule->args) {
7206 memprintf(err, "out of memory error");
7207 return ACT_RET_PRS_ERR;
7208 }
7209
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007210 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007211 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007212
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007213 /* Expect some arguments */
7214 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007215 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007216 memprintf(err, "expect %d arguments", fcn->nargs);
7217 return ACT_RET_PRS_ERR;
7218 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007219 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007220 if (!rule->arg.hlua_rule->args[i]) {
7221 memprintf(err, "out of memory error");
7222 return ACT_RET_PRS_ERR;
7223 }
7224 (*cur_arg)++;
7225 }
7226 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007227
Thierry FOURNIER42148732015-09-02 17:17:33 +02007228 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007229 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007230 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007231}
7232
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007233static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7234 struct act_rule *rule, char **err)
7235{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007236 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007237
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007238 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007239 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007240 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007241 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007242 * the call of this analyzer.
7243 */
7244 if (rule->from != ACT_F_HTTP_REQ) {
7245 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7246 return ACT_RET_PRS_ERR;
7247 }
7248
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007249 /* Memory for the rule. */
7250 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7251 if (!rule->arg.hlua_rule) {
7252 memprintf(err, "out of memory error");
7253 return ACT_RET_PRS_ERR;
7254 }
7255
7256 /* Reference the Lua function and store the reference. */
7257 rule->arg.hlua_rule->fcn = *fcn;
7258
7259 /* TODO: later accept arguments. */
7260 rule->arg.hlua_rule->args = NULL;
7261
7262 /* Add applet pointer in the rule. */
7263 rule->applet.obj_type = OBJ_TYPE_APPLET;
7264 rule->applet.name = fcn->name;
7265 rule->applet.init = hlua_applet_http_init;
7266 rule->applet.fct = hlua_applet_http_fct;
7267 rule->applet.release = hlua_applet_http_release;
7268 rule->applet.timeout = hlua_timeout_applet;
7269
7270 return ACT_RET_PRS_OK;
7271}
7272
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007273/* This function is an LUA binding used for registering
7274 * "sample-conv" functions. It expects a converter name used
7275 * in the haproxy configuration file, and an LUA function.
7276 */
7277__LJMP static int hlua_register_action(lua_State *L)
7278{
7279 struct action_kw_list *akl;
7280 const char *name;
7281 int ref;
7282 int len;
7283 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007284 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007285
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007286 /* Initialise the number of expected arguments at 0. */
7287 nargs = 0;
7288
7289 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7290 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007291
7292 /* First argument : converter name. */
7293 name = MAY_LJMP(luaL_checkstring(L, 1));
7294
7295 /* Second argument : environment. */
7296 if (lua_type(L, 2) != LUA_TTABLE)
7297 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7298
7299 /* Third argument : lua function. */
7300 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7301
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007302 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007303 if (lua_gettop(L) >= 4)
7304 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7305
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007306 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007307 lua_pushnil(L);
7308 while (lua_next(L, 2) != 0) {
7309 if (lua_type(L, -1) != LUA_TSTRING)
7310 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7311
7312 /* Check required environment. Only accepted "http" or "tcp". */
7313 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007314 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007315 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007316 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007317 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007318 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007319 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007320
7321 /* Fill fcn. */
7322 fcn->name = strdup(name);
7323 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007324 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007325 fcn->function_ref = ref;
7326
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007327 /* Set the expected number od arguments. */
7328 fcn->nargs = nargs;
7329
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007330 /* List head */
7331 akl->list.n = akl->list.p = NULL;
7332
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007333 /* action keyword. */
7334 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007335 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007336 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007337 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007338
7339 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7340
7341 akl->kw[0].match_pfx = 0;
7342 akl->kw[0].private = fcn;
7343 akl->kw[0].parse = action_register_lua;
7344
7345 /* select the action registering point. */
7346 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7347 tcp_req_cont_keywords_register(akl);
7348 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7349 tcp_res_cont_keywords_register(akl);
7350 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7351 http_req_keywords_register(akl);
7352 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7353 http_res_keywords_register(akl);
7354 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007355 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007356 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7357 "are expected.", lua_tostring(L, -1)));
7358
7359 /* pop the environment string. */
7360 lua_pop(L, 1);
7361 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007362 return ACT_RET_PRS_OK;
7363}
7364
7365static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7366 struct act_rule *rule, char **err)
7367{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007368 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007369
Christopher Faulet280f85b2019-07-15 15:02:04 +02007370 if (px->mode == PR_MODE_HTTP) {
7371 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007372 return ACT_RET_PRS_ERR;
7373 }
7374
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007375 /* Memory for the rule. */
7376 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7377 if (!rule->arg.hlua_rule) {
7378 memprintf(err, "out of memory error");
7379 return ACT_RET_PRS_ERR;
7380 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007381
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007382 /* Reference the Lua function and store the reference. */
7383 rule->arg.hlua_rule->fcn = *fcn;
7384
7385 /* TODO: later accept arguments. */
7386 rule->arg.hlua_rule->args = NULL;
7387
7388 /* Add applet pointer in the rule. */
7389 rule->applet.obj_type = OBJ_TYPE_APPLET;
7390 rule->applet.name = fcn->name;
7391 rule->applet.init = hlua_applet_tcp_init;
7392 rule->applet.fct = hlua_applet_tcp_fct;
7393 rule->applet.release = hlua_applet_tcp_release;
7394 rule->applet.timeout = hlua_timeout_applet;
7395
7396 return 0;
7397}
7398
7399/* This function is an LUA binding used for registering
7400 * "sample-conv" functions. It expects a converter name used
7401 * in the haproxy configuration file, and an LUA function.
7402 */
7403__LJMP static int hlua_register_service(lua_State *L)
7404{
7405 struct action_kw_list *akl;
7406 const char *name;
7407 const char *env;
7408 int ref;
7409 int len;
7410 struct hlua_function *fcn;
7411
7412 MAY_LJMP(check_args(L, 3, "register_service"));
7413
7414 /* First argument : converter name. */
7415 name = MAY_LJMP(luaL_checkstring(L, 1));
7416
7417 /* Second argument : environment. */
7418 env = MAY_LJMP(luaL_checkstring(L, 2));
7419
7420 /* Third argument : lua function. */
7421 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7422
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007423 /* Allocate and fill the sample fetch keyword struct. */
7424 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7425 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007426 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007427 fcn = calloc(1, sizeof(*fcn));
7428 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007429 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007430
7431 /* Fill fcn. */
7432 len = strlen("<lua.>") + strlen(name) + 1;
7433 fcn->name = calloc(1, len);
7434 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007435 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007436 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7437 fcn->function_ref = ref;
7438
7439 /* List head */
7440 akl->list.n = akl->list.p = NULL;
7441
7442 /* converter keyword. */
7443 len = strlen("lua.") + strlen(name) + 1;
7444 akl->kw[0].kw = calloc(1, len);
7445 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007446 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007447
7448 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7449
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007450 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007451 if (strcmp(env, "tcp") == 0)
7452 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007453 else if (strcmp(env, "http") == 0)
7454 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007455 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007456 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007457 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007458
7459 akl->kw[0].match_pfx = 0;
7460 akl->kw[0].private = fcn;
7461
7462 /* End of array. */
7463 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7464
7465 /* Register this new converter */
7466 service_keywords_register(akl);
7467
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007468 return 0;
7469}
7470
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007471/* This function initialises Lua cli handler. It copies the
7472 * arguments in the Lua stack and create channel IO objects.
7473 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007474static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007475{
7476 struct hlua *hlua;
7477 struct hlua_function *fcn;
7478 int i;
7479 const char *error;
7480
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007481 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007482 appctx->ctx.hlua_cli.fcn = private;
7483
Willy Tarreaubafbe012017-11-24 17:34:44 +01007484 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007485 if (!hlua) {
7486 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007487 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007488 }
7489 HLUA_INIT(hlua);
7490 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007491
7492 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007493 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007494 * applet_http. It is absolutely compatible.
7495 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007496 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007497 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007498 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007499 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007500 }
7501 appctx->ctx.hlua_cli.task->nice = 0;
7502 appctx->ctx.hlua_cli.task->context = appctx;
7503 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7504
7505 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007506 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007507 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007508 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007509 }
7510
7511 /* The following Lua calls can fail. */
7512 if (!SET_SAFE_LJMP(hlua->T)) {
7513 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7514 error = lua_tostring(hlua->T, -1);
7515 else
7516 error = "critical error";
7517 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7518 goto error;
7519 }
7520
7521 /* Check stack available size. */
7522 if (!lua_checkstack(hlua->T, 2)) {
7523 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7524 goto error;
7525 }
7526
7527 /* Restore the function in the stack. */
7528 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7529
7530 /* Once the arguments parsed, the CLI is like an AppletTCP,
7531 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007532 */
7533 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7534 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7535 goto error;
7536 }
7537 hlua->nargs = 1;
7538
7539 /* push keywords in the stack. */
7540 for (i = 0; *args[i]; i++) {
7541 /* Check stack available size. */
7542 if (!lua_checkstack(hlua->T, 1)) {
7543 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7544 goto error;
7545 }
7546 lua_pushstring(hlua->T, args[i]);
7547 hlua->nargs++;
7548 }
7549
7550 /* We must initialize the execution timeouts. */
7551 hlua->max_time = hlua_timeout_session;
7552
7553 /* At this point the execution is safe. */
7554 RESET_SAFE_LJMP(hlua->T);
7555
7556 /* It's ok */
7557 return 0;
7558
7559 /* It's not ok. */
7560error:
7561 RESET_SAFE_LJMP(hlua->T);
7562 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007563 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007564 return 1;
7565}
7566
7567static int hlua_cli_io_handler_fct(struct appctx *appctx)
7568{
7569 struct hlua *hlua;
7570 struct stream_interface *si;
7571 struct hlua_function *fcn;
7572
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007573 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007574 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007575 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007576
7577 /* If the stream is disconnect or closed, ldo nothing. */
7578 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7579 return 1;
7580
7581 /* Execute the function. */
7582 switch (hlua_ctx_resume(hlua, 1)) {
7583
7584 /* finished. */
7585 case HLUA_E_OK:
7586 return 1;
7587
7588 /* yield. */
7589 case HLUA_E_AGAIN:
7590 /* We want write. */
7591 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007592 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007593 /* Set the timeout. */
7594 if (hlua->wake_time != TICK_ETERNITY)
7595 task_schedule(hlua->task, hlua->wake_time);
7596 return 0;
7597
7598 /* finished with error. */
7599 case HLUA_E_ERRMSG:
7600 /* Display log. */
7601 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7602 fcn->name, lua_tostring(hlua->T, -1));
7603 lua_pop(hlua->T, 1);
7604 return 1;
7605
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007606 case HLUA_E_ETMOUT:
7607 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7608 fcn->name);
7609 return 1;
7610
7611 case HLUA_E_NOMEM:
7612 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7613 fcn->name);
7614 return 1;
7615
7616 case HLUA_E_YIELD: /* unexpected */
7617 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7618 fcn->name);
7619 return 1;
7620
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007621 case HLUA_E_ERR:
7622 /* Display log. */
7623 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7624 fcn->name);
7625 return 1;
7626
7627 default:
7628 return 1;
7629 }
7630
7631 return 1;
7632}
7633
7634static void hlua_cli_io_release_fct(struct appctx *appctx)
7635{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007636 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007637 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007638}
7639
7640/* This function is an LUA binding used for registering
7641 * new keywords in the cli. It expects a list of keywords
7642 * which are the "path". It is limited to 5 keywords. A
7643 * description of the command, a function to be executed
7644 * for the parsing and a function for io handlers.
7645 */
7646__LJMP static int hlua_register_cli(lua_State *L)
7647{
7648 struct cli_kw_list *cli_kws;
7649 const char *message;
7650 int ref_io;
7651 int len;
7652 struct hlua_function *fcn;
7653 int index;
7654 int i;
7655
7656 MAY_LJMP(check_args(L, 3, "register_cli"));
7657
7658 /* First argument : an array of maximum 5 keywords. */
7659 if (!lua_istable(L, 1))
7660 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7661
7662 /* Second argument : string with contextual message. */
7663 message = MAY_LJMP(luaL_checkstring(L, 2));
7664
7665 /* Third and fourth argument : lua function. */
7666 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7667
7668 /* Allocate and fill the sample fetch keyword struct. */
7669 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7670 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007671 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007672 fcn = calloc(1, sizeof(*fcn));
7673 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007674 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007675
7676 /* Fill path. */
7677 index = 0;
7678 lua_pushnil(L);
7679 while(lua_next(L, 1) != 0) {
7680 if (index >= 5)
7681 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7682 if (lua_type(L, -1) != LUA_TSTRING)
7683 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7684 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7685 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007686 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007687 index++;
7688 lua_pop(L, 1);
7689 }
7690
7691 /* Copy help message. */
7692 cli_kws->kw[0].usage = strdup(message);
7693 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007694 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007695
7696 /* Fill fcn io handler. */
7697 len = strlen("<lua.cli>") + 1;
7698 for (i = 0; i < index; i++)
7699 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7700 fcn->name = calloc(1, len);
7701 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007702 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007703 strncat((char *)fcn->name, "<lua.cli", len);
7704 for (i = 0; i < index; i++) {
7705 strncat((char *)fcn->name, ".", len);
7706 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7707 }
7708 strncat((char *)fcn->name, ">", len);
7709 fcn->function_ref = ref_io;
7710
7711 /* Fill last entries. */
7712 cli_kws->kw[0].private = fcn;
7713 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7714 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7715 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7716
7717 /* Register this new converter */
7718 cli_register_kw(cli_kws);
7719
7720 return 0;
7721}
7722
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007723static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7724 struct proxy *defpx, const char *file, int line,
7725 char **err, unsigned int *timeout)
7726{
7727 const char *error;
7728
7729 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007730 if (error == PARSE_TIME_OVER) {
7731 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7732 args[1], args[0]);
7733 return -1;
7734 }
7735 else if (error == PARSE_TIME_UNDER) {
7736 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7737 args[1], args[0]);
7738 return -1;
7739 }
7740 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007741 memprintf(err, "%s: invalid timeout", args[0]);
7742 return -1;
7743 }
7744 return 0;
7745}
7746
7747static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7748 struct proxy *defpx, const char *file, int line,
7749 char **err)
7750{
7751 return hlua_read_timeout(args, section_type, curpx, defpx,
7752 file, line, err, &hlua_timeout_session);
7753}
7754
7755static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7756 struct proxy *defpx, const char *file, int line,
7757 char **err)
7758{
7759 return hlua_read_timeout(args, section_type, curpx, defpx,
7760 file, line, err, &hlua_timeout_task);
7761}
7762
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007763static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7764 struct proxy *defpx, const char *file, int line,
7765 char **err)
7766{
7767 return hlua_read_timeout(args, section_type, curpx, defpx,
7768 file, line, err, &hlua_timeout_applet);
7769}
7770
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007771static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7772 struct proxy *defpx, const char *file, int line,
7773 char **err)
7774{
7775 char *error;
7776
7777 hlua_nb_instruction = strtoll(args[1], &error, 10);
7778 if (*error != '\0') {
7779 memprintf(err, "%s: invalid number", args[0]);
7780 return -1;
7781 }
7782 return 0;
7783}
7784
Willy Tarreau32f61e22015-03-18 17:54:59 +01007785static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7786 struct proxy *defpx, const char *file, int line,
7787 char **err)
7788{
7789 char *error;
7790
7791 if (*(args[1]) == 0) {
7792 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7793 return -1;
7794 }
7795 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7796 if (*error != '\0') {
7797 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7798 return -1;
7799 }
7800 return 0;
7801}
7802
7803
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007804/* This function is called by the main configuration key "lua-load". It loads and
7805 * execute an lua file during the parsing of the HAProxy configuration file. It is
7806 * the main lua entry point.
7807 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007808 * This function runs with the HAProxy keywords API. It returns -1 if an error
7809 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007810 *
7811 * In some error case, LUA set an error message in top of the stack. This function
7812 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007813 *
7814 * This function can fail with an abort() due to an Lua critical error.
7815 * We are in the configuration parsing process of HAProxy, this abort() is
7816 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007817 */
7818static int hlua_load(char **args, int section_type, struct proxy *curpx,
7819 struct proxy *defpx, const char *file, int line,
7820 char **err)
7821{
7822 int error;
7823
7824 /* Just load and compile the file. */
7825 error = luaL_loadfile(gL.T, args[1]);
7826 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007827 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007828 lua_pop(gL.T, 1);
7829 return -1;
7830 }
7831
7832 /* If no syntax error where detected, execute the code. */
7833 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7834 switch (error) {
7835 case LUA_OK:
7836 break;
7837 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007838 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007839 lua_pop(gL.T, 1);
7840 return -1;
7841 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007842 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007843 return -1;
7844 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007845 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007846 lua_pop(gL.T, 1);
7847 return -1;
7848 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007849 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007850 lua_pop(gL.T, 1);
7851 return -1;
7852 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007853 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007854 lua_pop(gL.T, 1);
7855 return -1;
7856 }
7857
7858 return 0;
7859}
7860
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007861/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7862 * in the given <ctx>.
7863 */
7864static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7865{
7866 lua_getglobal(ctx.T, "package"); /* push package variable */
7867 lua_pushstring(ctx.T, path); /* push given path */
7868 lua_pushstring(ctx.T, ";"); /* push semicolon */
7869 lua_getfield(ctx.T, -3, type); /* push old path */
7870 lua_concat(ctx.T, 3); /* concatenate to new path */
7871 lua_setfield(ctx.T, -2, type); /* store new path */
7872 lua_pop(ctx.T, 1); /* pop package variable */
7873
7874 return 0;
7875}
7876
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007877static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7878 struct proxy *defpx, const char *file, int line,
7879 char **err)
7880{
7881 char *path;
7882 char *type = "path";
7883 if (too_many_args(2, args, err, NULL)) {
7884 return -1;
7885 }
7886
7887 if (!(*args[1])) {
7888 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7889 return -1;
7890 }
7891 path = args[1];
7892
7893 if (*args[2]) {
7894 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7895 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7896 return -1;
7897 }
7898 type = args[2];
7899 }
7900
7901 return hlua_prepend_path(gL, type, path);
7902}
7903
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007904/* configuration keywords declaration */
7905static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007906 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007907 { CFG_GLOBAL, "lua-load", hlua_load },
7908 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7909 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007910 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007911 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007912 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007913 { 0, NULL, NULL },
7914}};
7915
Willy Tarreau0108d902018-11-25 19:14:37 +01007916INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7917
Christopher Fauletafd8f102018-11-08 11:34:21 +01007918
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007919/* This function can fail with an abort() due to an Lua critical error.
7920 * We are in the initialisation process of HAProxy, this abort() is
7921 * tolerated.
7922 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007923int hlua_post_init()
7924{
7925 struct hlua_init_function *init;
7926 const char *msg;
7927 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007928 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007929
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007930 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007931 if (!SET_SAFE_LJMP(gL.T)) {
7932 if (lua_type(gL.T, -1) == LUA_TSTRING)
7933 error = lua_tostring(gL.T, -1);
7934 else
7935 error = "critical error";
7936 fprintf(stderr, "Lua post-init: %s.\n", error);
7937 exit(1);
7938 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007939
7940#if USE_OPENSSL
7941 /* Initialize SSL server. */
7942 if (socket_ssl.xprt->prepare_srv) {
7943 int saved_used_backed = global.ssl_used_backend;
7944 // don't affect maxconn automatic computation
7945 socket_ssl.xprt->prepare_srv(&socket_ssl);
7946 global.ssl_used_backend = saved_used_backed;
7947 }
7948#endif
7949
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007950 hlua_fcn_post_init(gL.T);
7951 RESET_SAFE_LJMP(gL.T);
7952
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007953 list_for_each_entry(init, &hlua_init_functions, l) {
7954 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7955 ret = hlua_ctx_resume(&gL, 0);
7956 switch (ret) {
7957 case HLUA_E_OK:
7958 lua_pop(gL.T, -1);
7959 return 1;
7960 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007961 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007962 return 0;
7963 case HLUA_E_ERRMSG:
7964 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007965 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007966 return 0;
7967 case HLUA_E_ERR:
7968 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007969 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007970 return 0;
7971 }
7972 }
7973 return 1;
7974}
7975
Willy Tarreau32f61e22015-03-18 17:54:59 +01007976/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7977 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7978 * is the previously allocated size or the kind of object in case of a new
7979 * allocation. <nsize> is the requested new size.
7980 */
7981static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7982{
7983 struct hlua_mem_allocator *zone = ud;
7984
7985 if (nsize == 0) {
7986 /* it's a free */
7987 if (ptr)
7988 zone->allocated -= osize;
7989 free(ptr);
7990 return NULL;
7991 }
7992
7993 if (!ptr) {
7994 /* it's a new allocation */
7995 if (zone->limit && zone->allocated + nsize > zone->limit)
7996 return NULL;
7997
7998 ptr = malloc(nsize);
7999 if (ptr)
8000 zone->allocated += nsize;
8001 return ptr;
8002 }
8003
8004 /* it's a realloc */
8005 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8006 return NULL;
8007
8008 ptr = realloc(ptr, nsize);
8009 if (ptr)
8010 zone->allocated += nsize - osize;
8011 return ptr;
8012}
8013
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008014/* Ithis function can fail with an abort() due to an Lua critical error.
8015 * We are in the initialisation process of HAProxy, this abort() is
8016 * tolerated.
8017 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008018void hlua_init(void)
8019{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008020 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008021 int idx;
8022 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008023 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008024 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008025 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008026#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008027 struct srv_kw *kw;
8028 int tmp_error;
8029 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008030 char *args[] = { /* SSL client configuration. */
8031 "ssl",
8032 "verify",
8033 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008034 NULL
8035 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008036#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008037
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008038 /* Init main lua stack. */
8039 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008040 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008041 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008042 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008043 hlua_sethlua(&gL);
8044 gL.Tref = LUA_REFNIL;
8045 gL.task = NULL;
8046
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008047 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008048 * the Lua function can fail with an abort. We are in the initialisation
8049 * process of HAProxy, this abort() is tolerated.
8050 */
8051
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008052 /* Initialise lua. */
8053 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008054#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8055#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8056#ifdef HLUA_PREPEND_PATH
8057 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8058#endif
8059#ifdef HLUA_PREPEND_CPATH
8060 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8061#endif
8062#undef HLUA_PREPEND_PATH_TOSTRING
8063#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008064
Thierry Fournier75933d42016-01-21 09:30:18 +01008065 /* Set safe environment for the initialisation. */
8066 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008067 if (lua_type(gL.T, -1) == LUA_TSTRING)
8068 error_msg = lua_tostring(gL.T, -1);
8069 else
8070 error_msg = "critical error";
8071 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008072 exit(1);
8073 }
8074
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008075 /*
8076 *
8077 * Create "core" object.
8078 *
8079 */
8080
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008081 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008082 lua_newtable(gL.T);
8083
8084 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008085 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008086 hlua_class_const_int(gL.T, log_levels[i], i);
8087
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008088 /* Register special functions. */
8089 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008090 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008091 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008092 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008093 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008094 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008095 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008096 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008097 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008098 hlua_class_function(gL.T, "sleep", hlua_sleep);
8099 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008100 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8101 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8102 hlua_class_function(gL.T, "set_map", hlua_set_map);
8103 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008104 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008105 hlua_class_function(gL.T, "log", hlua_log);
8106 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8107 hlua_class_function(gL.T, "Info", hlua_log_info);
8108 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8109 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008110 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008111 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008112
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008113 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008114
8115 /*
8116 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008117 * Create "act" object.
8118 *
8119 */
8120
8121 /* This table entry is the object "act" base. */
8122 lua_newtable(gL.T);
8123
8124 /* push action return constants */
8125 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8126 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8127 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8128 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8129 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8130 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8131 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8132 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8133
Christopher Faulet501465d2020-02-26 14:54:16 +01008134 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008135
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008136 lua_setglobal(gL.T, "act");
8137
8138 /*
8139 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008140 * Register class Map
8141 *
8142 */
8143
8144 /* This table entry is the object "Map" base. */
8145 lua_newtable(gL.T);
8146
8147 /* register pattern types. */
8148 for (i=0; i<PAT_MATCH_NUM; i++)
8149 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008150 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008151 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8152 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008153 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008154
8155 /* register constructor. */
8156 hlua_class_function(gL.T, "new", hlua_map_new);
8157
8158 /* Create and fill the metatable. */
8159 lua_newtable(gL.T);
8160
Ilya Shipitsind4259502020-04-08 01:07:56 +05008161 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008162 lua_pushstring(gL.T, "__index");
8163 lua_newtable(gL.T);
8164
8165 /* Register . */
8166 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8167 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8168
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008169 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008170
Thierry Fournier45e78d72016-02-19 18:34:46 +01008171 /* Register previous table in the registry with reference and named entry.
8172 * The function hlua_register_metatable() pops the stack, so we
8173 * previously create a copy of the table.
8174 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008175 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008176 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008177
8178 /* Assign the metatable to the mai Map object. */
8179 lua_setmetatable(gL.T, -2);
8180
8181 /* Set a name to the table. */
8182 lua_setglobal(gL.T, "Map");
8183
8184 /*
8185 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008186 * Register class Channel
8187 *
8188 */
8189
8190 /* Create and fill the metatable. */
8191 lua_newtable(gL.T);
8192
Ilya Shipitsind4259502020-04-08 01:07:56 +05008193 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008194 lua_pushstring(gL.T, "__index");
8195 lua_newtable(gL.T);
8196
8197 /* Register . */
8198 hlua_class_function(gL.T, "get", hlua_channel_get);
8199 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8200 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8201 hlua_class_function(gL.T, "set", hlua_channel_set);
8202 hlua_class_function(gL.T, "append", hlua_channel_append);
8203 hlua_class_function(gL.T, "send", hlua_channel_send);
8204 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8205 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8206 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008207 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008208 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008209
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008210 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008211
8212 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008213 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008214
8215 /*
8216 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008217 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008218 *
8219 */
8220
8221 /* Create and fill the metatable. */
8222 lua_newtable(gL.T);
8223
Ilya Shipitsind4259502020-04-08 01:07:56 +05008224 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008225 lua_pushstring(gL.T, "__index");
8226 lua_newtable(gL.T);
8227
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008228 /* Browse existing fetches and create the associated
8229 * object method.
8230 */
8231 sf = NULL;
8232 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8233
8234 /* Dont register the keywork if the arguments check function are
8235 * not safe during the runtime.
8236 */
8237 if ((sf->val_args != NULL) &&
8238 (sf->val_args != val_payload_lv) &&
8239 (sf->val_args != val_hdr))
8240 continue;
8241
8242 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8243 * by an underscore.
8244 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008245 strncpy(trash.area, sf->kw, trash.size);
8246 trash.area[trash.size - 1] = '\0';
8247 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008248 if (*p == '.' || *p == '-' || *p == '+')
8249 *p = '_';
8250
8251 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008252 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008253 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008254 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008255 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008256 }
8257
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008258 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008259
8260 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008261 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008262
8263 /*
8264 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008265 * Register class Converters
8266 *
8267 */
8268
8269 /* Create and fill the metatable. */
8270 lua_newtable(gL.T);
8271
8272 /* Create and fill the __index entry. */
8273 lua_pushstring(gL.T, "__index");
8274 lua_newtable(gL.T);
8275
8276 /* Browse existing converters and create the associated
8277 * object method.
8278 */
8279 sc = NULL;
8280 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8281 /* Dont register the keywork if the arguments check function are
8282 * not safe during the runtime.
8283 */
8284 if (sc->val_args != NULL)
8285 continue;
8286
8287 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8288 * by an underscore.
8289 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008290 strncpy(trash.area, sc->kw, trash.size);
8291 trash.area[trash.size - 1] = '\0';
8292 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008293 if (*p == '.' || *p == '-' || *p == '+')
8294 *p = '_';
8295
8296 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008297 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008298 lua_pushlightuserdata(gL.T, sc);
8299 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008300 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008301 }
8302
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008303 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008304
8305 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008306 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008307
8308 /*
8309 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008310 * Register class HTTP
8311 *
8312 */
8313
8314 /* Create and fill the metatable. */
8315 lua_newtable(gL.T);
8316
Ilya Shipitsind4259502020-04-08 01:07:56 +05008317 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008318 lua_pushstring(gL.T, "__index");
8319 lua_newtable(gL.T);
8320
8321 /* Register Lua functions. */
8322 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8323 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8324 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8325 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8326 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8327 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8328 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8329 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8330 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8331 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8332
8333 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8334 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8335 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8336 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8337 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8338 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008339 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008340
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008341 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008342
8343 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008344 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008345
8346 /*
8347 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008348 * Register class AppletTCP
8349 *
8350 */
8351
8352 /* Create and fill the metatable. */
8353 lua_newtable(gL.T);
8354
Ilya Shipitsind4259502020-04-08 01:07:56 +05008355 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008356 lua_pushstring(gL.T, "__index");
8357 lua_newtable(gL.T);
8358
8359 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008360 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8361 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8362 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8363 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8364 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008365 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8366 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8367 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008368
8369 lua_settable(gL.T, -3);
8370
8371 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008372 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008373
8374 /*
8375 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008376 * Register class AppletHTTP
8377 *
8378 */
8379
8380 /* Create and fill the metatable. */
8381 lua_newtable(gL.T);
8382
Ilya Shipitsind4259502020-04-08 01:07:56 +05008383 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008384 lua_pushstring(gL.T, "__index");
8385 lua_newtable(gL.T);
8386
8387 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008388 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8389 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008390 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8391 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8392 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008393 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8394 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8395 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8396 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8397 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8398 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8399
8400 lua_settable(gL.T, -3);
8401
8402 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008403 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008404
8405 /*
8406 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008407 * Register class TXN
8408 *
8409 */
8410
8411 /* Create and fill the metatable. */
8412 lua_newtable(gL.T);
8413
Ilya Shipitsind4259502020-04-08 01:07:56 +05008414 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008415 lua_pushstring(gL.T, "__index");
8416 lua_newtable(gL.T);
8417
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008418 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008419 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8420 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8421 hlua_class_function(gL.T, "set_var", hlua_set_var);
8422 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8423 hlua_class_function(gL.T, "get_var", hlua_get_var);
8424 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008425 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008426 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8427 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8428 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8429 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8430 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8431 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8432 hlua_class_function(gL.T, "log", hlua_txn_log);
8433 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8434 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8435 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8436 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008437
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008438 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008439
8440 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008441 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008442
8443 /*
8444 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008445 * Register class reply
8446 *
8447 */
8448 lua_newtable(gL.T);
8449 lua_pushstring(gL.T, "__index");
8450 lua_newtable(gL.T);
8451 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8452 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8453 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8454 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8455 lua_settable(gL.T, -3); /* Sets the __index entry. */
8456 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8457
8458
8459 /*
8460 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008461 * Register class Socket
8462 *
8463 */
8464
8465 /* Create and fill the metatable. */
8466 lua_newtable(gL.T);
8467
Ilya Shipitsind4259502020-04-08 01:07:56 +05008468 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008469 lua_pushstring(gL.T, "__index");
8470 lua_newtable(gL.T);
8471
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008472#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008473 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008474#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008475 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8476 hlua_class_function(gL.T, "send", hlua_socket_send);
8477 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8478 hlua_class_function(gL.T, "close", hlua_socket_close);
8479 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8480 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8481 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8482 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8483
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008484 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008485
8486 /* Register the garbage collector entry. */
8487 lua_pushstring(gL.T, "__gc");
8488 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008489 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008490
8491 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008492 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008493
8494 /* Proxy and server configuration initialisation. */
8495 memset(&socket_proxy, 0, sizeof(socket_proxy));
8496 init_new_proxy(&socket_proxy);
8497 socket_proxy.parent = NULL;
8498 socket_proxy.last_change = now.tv_sec;
8499 socket_proxy.id = "LUA-SOCKET";
8500 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8501 socket_proxy.maxconn = 0;
8502 socket_proxy.accept = NULL;
8503 socket_proxy.options2 |= PR_O2_INDEPSTR;
8504 socket_proxy.srv = NULL;
8505 socket_proxy.conn_retries = 0;
8506 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8507
8508 /* Init TCP server: unchanged parameters */
8509 memset(&socket_tcp, 0, sizeof(socket_tcp));
8510 socket_tcp.next = NULL;
8511 socket_tcp.proxy = &socket_proxy;
8512 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8513 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008514 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008515 socket_tcp.idle_conns = NULL;
8516 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008517 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008518 socket_tcp.last_change = 0;
8519 socket_tcp.id = "LUA-TCP-CONN";
8520 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8521 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8522 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8523
8524 /* XXX: Copy default parameter from default server,
8525 * but the default server is not initialized.
8526 */
8527 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8528 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8529 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8530 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8531 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8532 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8533 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8534 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8535 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8536 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8537
8538 socket_tcp.check.status = HCHK_STATUS_INI;
8539 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8540 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8541 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8542 socket_tcp.check.server = &socket_tcp;
8543
8544 socket_tcp.agent.status = HCHK_STATUS_INI;
8545 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8546 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8547 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8548 socket_tcp.agent.server = &socket_tcp;
8549
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008550 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008551
8552#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008553 /* Init TCP server: unchanged parameters */
8554 memset(&socket_ssl, 0, sizeof(socket_ssl));
8555 socket_ssl.next = NULL;
8556 socket_ssl.proxy = &socket_proxy;
8557 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8558 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008559 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008560 socket_ssl.idle_conns = NULL;
8561 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008562 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008563 socket_ssl.last_change = 0;
8564 socket_ssl.id = "LUA-SSL-CONN";
8565 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8566 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8567 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8568
8569 /* XXX: Copy default parameter from default server,
8570 * but the default server is not initialized.
8571 */
8572 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8573 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8574 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8575 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8576 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8577 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8578 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8579 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8580 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8581 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8582
8583 socket_ssl.check.status = HCHK_STATUS_INI;
8584 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8585 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8586 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8587 socket_ssl.check.server = &socket_ssl;
8588
8589 socket_ssl.agent.status = HCHK_STATUS_INI;
8590 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8591 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8592 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8593 socket_ssl.agent.server = &socket_ssl;
8594
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008595 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008596 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008597
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008598 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008599 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8600 /*
8601 *
8602 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008603 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008604 * features like client certificates and ssl_verify.
8605 *
8606 */
8607 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8608 if (tmp_error != 0) {
8609 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8610 abort(); /* This must be never arrives because the command line
8611 not editable by the user. */
8612 }
8613 idx += kw->skip;
8614 }
8615 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008616#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008617
8618 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008619}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008620
Willy Tarreau80713382018-11-26 10:19:54 +01008621static void hlua_register_build_options(void)
8622{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008623 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008624
Willy Tarreaubb57d942016-12-21 19:04:56 +01008625 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8626 hap_register_build_opts(ptr, 1);
8627}
Willy Tarreau80713382018-11-26 10:19:54 +01008628
8629INITCALL0(STG_REGISTER, hlua_register_build_options);