blob: f31f318a17108531cf2f584aee850d6704e95b3d [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 Tarreau8d2b7772020-05-27 10:58:19 +020024#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
27#include <haproxy/applet.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020029#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020030#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020031#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020032#include <haproxy/connection.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 Tarreauc2b1ff02020-06-04 21:21:03 +020036#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020037#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020039#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020040#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020041#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020042#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020043#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020044#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020045#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020047#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020048#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020049#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020050#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020051#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020052#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020053#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020054#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020056#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020057#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/xref.h>
59
Thierry FOURNIER380d0932015-01-23 14:27:52 +010060
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010061/* Lua uses longjmp to perform yield or throwing errors. This
62 * macro is used only for identifying the function that can
63 * not return because a longjmp is executed.
64 * __LJMP marks a prototype of hlua file that can use longjmp.
65 * WILL_LJMP() marks an lua function that will use longjmp.
66 * MAY_LJMP() marks an lua function that may use longjmp.
67 */
68#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020069#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010070#define MAY_LJMP(func) func
71
Thierry FOURNIERbabae282015-09-17 11:36:37 +020072/* This couple of function executes securely some Lua calls outside of
73 * the lua runtime environment. Each Lua call can return a longjmp
74 * if it encounter a memory error.
75 *
76 * Lua documentation extract:
77 *
78 * If an error happens outside any protected environment, Lua calls
79 * a panic function (see lua_atpanic) and then calls abort, thus
80 * exiting the host application. Your panic function can avoid this
81 * exit by never returning (e.g., doing a long jump to your own
82 * recovery point outside Lua).
83 *
84 * The panic function runs as if it were a message handler (see
85 * §2.3); in particular, the error message is at the top of the
86 * stack. However, there is no guarantee about stack space. To push
87 * anything on the stack, the panic function must first check the
88 * available space (see §4.2).
89 *
90 * We must check all the Lua entry point. This includes:
91 * - The include/proto/hlua.h exported functions
92 * - the task wrapper function
93 * - The action wrapper function
94 * - The converters wrapper function
95 * - The sample-fetch wrapper functions
96 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +050097 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080098 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020099 *
100 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
101 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
102 * because they must be exists in the program stack when the longjmp
103 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200104 *
105 * Note that the Lua processing is not really thread safe. It provides
106 * heavy system which consists to add our own lock function in the Lua
107 * code and recompile the library. This system will probably not accepted
108 * by maintainers of various distribs.
109 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500110 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200111 * quick looking on the Lua sources displays a lua_lock() a the start
112 * of function and a lua_unlock() at the end of the function. So I
113 * conclude that the Lua thread safe mode just perform a mutex around
114 * all execution. So I prefer to do this in the HAProxy code, it will be
115 * easier for distro maintainers.
116 *
117 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
118 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
119 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200120 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100121__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200122THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100124static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125
126#define SET_SAFE_LJMP(__L) \
127 ({ \
128 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100129 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200130 if (setjmp(safe_ljmp_env) != 0) { \
131 lua_atpanic(__L, hlua_panic_safe); \
132 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200134 } else { \
135 lua_atpanic(__L, hlua_panic_ljmp); \
136 ret = 1; \
137 } \
138 ret; \
139 })
140
141/* If we are the last function catching Lua errors, we
142 * must reset the panic function.
143 */
144#define RESET_SAFE_LJMP(__L) \
145 do { \
146 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100147 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200148 } while(0)
149
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200150/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200151#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100152/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200153#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200154/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100155#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100156#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200157
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100158/* The main Lua execution context. */
159struct hlua gL;
160
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100161/* This is the memory pool containing struct lua for applets
162 * (including cli).
163 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100164DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100166/* Used for Socket connection. */
167static struct proxy socket_proxy;
168static struct server socket_tcp;
169#ifdef USE_OPENSSL
170static struct server socket_ssl;
171#endif
172
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100173/* List head of the function called at the initialisation time. */
174struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
175
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100176/* The following variables contains the reference of the different
177 * Lua classes. These references are useful for identify metadata
178 * associated with an object.
179 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100180static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100181static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100182static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100183static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100184static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100185static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200186static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200187static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100189static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100190
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100191/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200192 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193 * short timeout. Lua linked with tasks doesn't have a timeout
194 * because a task may remain alive during all the haproxy execution.
195 */
196static unsigned int hlua_timeout_session = 4000; /* session timeout. */
197static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200198static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100199
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100200/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
201 * it is used for preventing infinite loops.
202 *
203 * I test the scheer with an infinite loop containing one incrementation
204 * and one test. I run this loop between 10 seconds, I raise a ceil of
205 * 710M loops from one interrupt each 9000 instructions, so I fix the value
206 * to one interrupt each 10 000 instructions.
207 *
208 * configured | Number of
209 * instructions | loops executed
210 * between two | in milions
211 * forced yields |
212 * ---------------+---------------
213 * 10 | 160
214 * 500 | 670
215 * 1000 | 680
216 * 5000 | 700
217 * 7000 | 700
218 * 8000 | 700
219 * 9000 | 710 <- ceil
220 * 10000 | 710
221 * 100000 | 710
222 * 1000000 | 710
223 *
224 */
225static unsigned int hlua_nb_instruction = 10000;
226
Willy Tarreau32f61e22015-03-18 17:54:59 +0100227/* Descriptor for the memory allocation state. If limit is not null, it will
228 * be enforced on any memory allocation.
229 */
230struct hlua_mem_allocator {
231 size_t allocated;
232 size_t limit;
233};
234
235static struct hlua_mem_allocator hlua_global_allocator;
236
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100237/* These functions converts types between HAProxy internal args or
238 * sample and LUA types. Another function permits to check if the
239 * LUA stack contains arguments according with an required ARG_T
240 * format.
241 */
242static int hlua_arg2lua(lua_State *L, const struct arg *arg);
243static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100244__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100245 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100246static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100247static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100248static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
249
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100250__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200251
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200252#define SEND_ERR(__be, __fmt, __args...) \
253 do { \
254 send_log(__be, LOG_ERR, __fmt, ## __args); \
255 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100256 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200257 } while (0)
258
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100259/* Used to check an Lua function type in the stack. It creates and
260 * returns a reference of the function. This function throws an
261 * error if the rgument is not a "function".
262 */
263__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
264{
265 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100266 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100267 WILL_LJMP(luaL_argerror(L, argno, msg));
268 }
269 lua_pushvalue(L, argno);
270 return luaL_ref(L, LUA_REGISTRYINDEX);
271}
272
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200273/* Return the string that is of the top of the stack. */
274const char *hlua_get_top_error_string(lua_State *L)
275{
276 if (lua_gettop(L) < 1)
277 return "unknown error";
278 if (lua_type(L, -1) != LUA_TSTRING)
279 return "unknown error";
280 return lua_tostring(L, -1);
281}
282
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200283__LJMP static const char *hlua_traceback(lua_State *L)
284{
285 lua_Debug ar;
286 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200287 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200288 int filled = 0;
289
290 while (lua_getstack(L, level++, &ar)) {
291
292 /* Add separator */
293 if (filled)
294 chunk_appendf(msg, ", ");
295 filled = 1;
296
297 /* Fill fields:
298 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
299 * 'l': fills in the field currentline;
300 * 'n': fills in the field name and namewhat;
301 * 't': fills in the field istailcall;
302 */
303 lua_getinfo(L, "Slnt", &ar);
304
305 /* Append code localisation */
306 if (ar.currentline > 0)
307 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
308 else
309 chunk_appendf(msg, "%s ", ar.short_src);
310
311 /*
312 * Get function name
313 *
314 * if namewhat is no empty, name is defined.
315 * what contains "Lua" for Lua function, "C" for C function,
316 * or "main" for main code.
317 */
318 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
319 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
320
321 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
322 chunk_appendf(msg, "main chunk");
323
324 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
325 chunk_appendf(msg, "C function line %d", ar.linedefined);
326
327 else /* nothing left... */
328 chunk_appendf(msg, "?");
329
330
331 /* Display tailed call */
332 if (ar.istailcall)
333 chunk_appendf(msg, " ...");
334 }
335
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200336 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200337}
338
339
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100340/* This function check the number of arguments available in the
341 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500342 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100343 */
344__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
345{
346 if (lua_gettop(L) == nb)
347 return;
348 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
349}
350
Mark Lakes22154b42018-01-29 14:38:40 -0800351/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100352 * and the line number where the error is encountered.
353 */
354static int hlua_pusherror(lua_State *L, const char *fmt, ...)
355{
356 va_list argp;
357 va_start(argp, fmt);
358 luaL_where(L, 1);
359 lua_pushvfstring(L, fmt, argp);
360 va_end(argp);
361 lua_concat(L, 2);
362 return 1;
363}
364
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100365/* This functions is used with sample fetch and converters. It
366 * converts the HAProxy configuration argument in a lua stack
367 * values.
368 *
369 * It takes an array of "arg", and each entry of the array is
370 * converted and pushed in the LUA stack.
371 */
372static int hlua_arg2lua(lua_State *L, const struct arg *arg)
373{
374 switch (arg->type) {
375 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100376 case ARGT_TIME:
377 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100378 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 break;
380
381 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200382 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_IPV4:
386 case ARGT_IPV6:
387 case ARGT_MSK4:
388 case ARGT_MSK6:
389 case ARGT_FE:
390 case ARGT_BE:
391 case ARGT_TAB:
392 case ARGT_SRV:
393 case ARGT_USR:
394 case ARGT_MAP:
395 default:
396 lua_pushnil(L);
397 break;
398 }
399 return 1;
400}
401
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500402/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100403 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500404 * with sample fetch wrappers. The input arguments are given to the
405 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100406 */
407static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
408{
409 switch (lua_type(L, ud)) {
410
411 case LUA_TNUMBER:
412 case LUA_TBOOLEAN:
413 arg->type = ARGT_SINT;
414 arg->data.sint = lua_tointeger(L, ud);
415 break;
416
417 case LUA_TSTRING:
418 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200419 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200420 /* We don't know the actual size of the underlying allocation, so be conservative. */
421 arg->data.str.size = arg->data.str.data;
422 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100423 break;
424
425 case LUA_TUSERDATA:
426 case LUA_TNIL:
427 case LUA_TTABLE:
428 case LUA_TFUNCTION:
429 case LUA_TTHREAD:
430 case LUA_TLIGHTUSERDATA:
431 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200432 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433 break;
434 }
435 return 1;
436}
437
438/* the following functions are used to convert a struct sample
439 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500440 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100441 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100442static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200444 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449
450 case SMP_T_BIN:
451 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 break;
454
455 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
458 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
459 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
460 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
461 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
462 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
463 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
464 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
465 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100467 break;
468 default:
469 lua_pushnil(L);
470 break;
471 }
472 break;
473
474 case SMP_T_IPV4:
475 case SMP_T_IPV6:
476 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200477 if (sample_casts[smp->data.type][SMP_T_STR] &&
478 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100480 else
481 lua_pushnil(L);
482 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100483 default:
484 lua_pushnil(L);
485 break;
486 }
487 return 1;
488}
489
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100490/* the following functions are used to convert a struct sample
491 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500492 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100493 */
494static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
495{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200496 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497
498 case SMP_T_BIN:
499 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501 break;
502
503 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
506 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
507 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
508 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
509 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
510 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
511 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
512 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
513 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100515 break;
516 default:
517 lua_pushstring(L, "");
518 break;
519 }
520 break;
521
522 case SMP_T_SINT:
523 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 case SMP_T_IPV4:
525 case SMP_T_IPV6:
526 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 if (sample_casts[smp->data.type][SMP_T_STR] &&
528 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 else
531 lua_pushstring(L, "");
532 break;
533 default:
534 lua_pushstring(L, "");
535 break;
536 }
537 return 1;
538}
539
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540/* the following functions are used to convert an Lua type in a
541 * struct sample. This is useful to provide data from a converter
542 * to the LUA code.
543 */
544static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
545{
546 switch (lua_type(L, ud)) {
547
548 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 break;
552
553
554 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200562 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200563 /* We don't know the actual size of the underlying allocation, so be conservative. */
564 smp->data.u.str.size = smp->data.u.str.data;
565 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 break;
567
568 case LUA_TUSERDATA:
569 case LUA_TNIL:
570 case LUA_TTABLE:
571 case LUA_TFUNCTION:
572 case LUA_TTHREAD:
573 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200574 case LUA_TNONE:
575 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200576 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100578 break;
579 }
580 return 1;
581}
582
Ilya Shipitsind4259502020-04-08 01:07:56 +0500583/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800584 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100585 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100586 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500587 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100588 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100590__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100591 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592{
593 int min_arg;
594 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100595 struct proxy *px;
596 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100597
598 idx = 0;
599 min_arg = ARGM(mask);
600 mask >>= ARGM_BITS;
601
602 while (1) {
603
604 /* Check oversize. */
605 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100606 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100607 }
608
609 /* Check for mandatory arguments. */
610 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100611 if (idx < min_arg) {
612
613 /* If miss other argument than the first one, we return an error. */
614 if (idx > 0)
615 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
616
617 /* If first argument have a certain type, some default values
618 * may be used. See the function smp_resolve_args().
619 */
620 switch (mask & ARGT_MASK) {
621
622 case ARGT_FE:
623 if (!(p->cap & PR_CAP_FE))
624 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
625 argp[idx].data.prx = p;
626 argp[idx].type = ARGT_FE;
627 argp[idx+1].type = ARGT_STOP;
628 break;
629
630 case ARGT_BE:
631 if (!(p->cap & PR_CAP_BE))
632 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
633 argp[idx].data.prx = p;
634 argp[idx].type = ARGT_BE;
635 argp[idx+1].type = ARGT_STOP;
636 break;
637
638 case ARGT_TAB:
639 argp[idx].data.prx = p;
640 argp[idx].type = ARGT_TAB;
641 argp[idx+1].type = ARGT_STOP;
642 break;
643
644 default:
645 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
646 break;
647 }
648 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100649 return 0;
650 }
651
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500652 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 if ((mask & ARGT_MASK) == ARGT_STOP &&
654 argp[idx].type != ARGT_STOP) {
655 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
656 }
657
658 if ((mask & ARGT_MASK) == ARGT_STOP &&
659 argp[idx].type == ARGT_STOP) {
660 return 0;
661 }
662
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100663 /* Convert some argument types. */
664 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100665 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100666 if (argp[idx].type != ARGT_SINT)
667 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
668 argp[idx].type = ARGT_SINT;
669 break;
670
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100671 case ARGT_TIME:
672 if (argp[idx].type != ARGT_SINT)
673 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200674 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100675 break;
676
677 case ARGT_SIZE:
678 if (argp[idx].type != ARGT_SINT)
679 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200680 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 break;
682
683 case ARGT_FE:
684 if (argp[idx].type != ARGT_STR)
685 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200686 memcpy(trash.area, argp[idx].data.str.area,
687 argp[idx].data.str.data);
688 trash.area[argp[idx].data.str.data] = 0;
689 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100690 if (!argp[idx].data.prx)
691 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
692 argp[idx].type = ARGT_FE;
693 break;
694
695 case ARGT_BE:
696 if (argp[idx].type != ARGT_STR)
697 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200698 memcpy(trash.area, argp[idx].data.str.area,
699 argp[idx].data.str.data);
700 trash.area[argp[idx].data.str.data] = 0;
701 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100702 if (!argp[idx].data.prx)
703 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
704 argp[idx].type = ARGT_BE;
705 break;
706
707 case ARGT_TAB:
708 if (argp[idx].type != ARGT_STR)
709 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200710 memcpy(trash.area, argp[idx].data.str.area,
711 argp[idx].data.str.data);
712 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200713 argp[idx].data.t = stktable_find_by_name(trash.area);
714 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
716 argp[idx].type = ARGT_TAB;
717 break;
718
719 case ARGT_SRV:
720 if (argp[idx].type != ARGT_STR)
721 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 memcpy(trash.area, argp[idx].data.str.area,
723 argp[idx].data.str.data);
724 trash.area[argp[idx].data.str.data] = 0;
725 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100726 if (sname) {
727 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200728 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200729 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100730 if (!px)
731 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
732 }
733 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200734 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100735 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100736 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100737 argp[idx].data.srv = findserver(px, sname);
738 if (!argp[idx].data.srv)
739 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
740 argp[idx].type = ARGT_SRV;
741 break;
742
743 case ARGT_IPV4:
Christopher Faulet8e09ac82020-08-07 09:07:26 +0200744 if (argp[idx].type != ARGT_STR)
745 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200746 memcpy(trash.area, argp[idx].data.str.area,
747 argp[idx].data.str.data);
748 trash.area[argp[idx].data.str.data] = 0;
749 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
751 argp[idx].type = ARGT_IPV4;
752 break;
753
754 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200755 if (argp[idx].type == ARGT_SINT)
756 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
757 else if (argp[idx].type == ARGT_STR) {
758 memcpy(trash.area, argp[idx].data.str.area,
759 argp[idx].data.str.data);
760 trash.area[argp[idx].data.str.data] = 0;
761 if (!str2mask(trash.area, &argp[idx].data.ipv4))
762 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
763 }
764 else
765 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100766 argp[idx].type = ARGT_MSK4;
767 break;
768
769 case ARGT_IPV6:
Christopher Faulet8e09ac82020-08-07 09:07:26 +0200770 if (argp[idx].type != ARGT_STR)
771 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200772 memcpy(trash.area, argp[idx].data.str.area,
773 argp[idx].data.str.data);
774 trash.area[argp[idx].data.str.data] = 0;
775 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100776 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
777 argp[idx].type = ARGT_IPV6;
778 break;
779
780 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200781 if (argp[idx].type == ARGT_SINT)
782 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
783 else if (argp[idx].type == ARGT_STR) {
784 memcpy(trash.area, argp[idx].data.str.area,
785 argp[idx].data.str.data);
786 trash.area[argp[idx].data.str.data] = 0;
787 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
788 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
789 }
790 else
791 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Tim Duesterhusb814da62018-01-25 16:24:50 +0100792 argp[idx].type = ARGT_MSK6;
793 break;
794
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100795 case ARGT_MAP:
796 case ARGT_REG:
797 case ARGT_USR:
798 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100799 break;
800 }
801
802 /* Check for type of argument. */
803 if ((mask & ARGT_MASK) != argp[idx].type) {
804 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
805 arg_type_names[(mask & ARGT_MASK)],
806 arg_type_names[argp[idx].type & ARGT_MASK]);
807 WILL_LJMP(luaL_argerror(L, first + idx, msg));
808 }
809
810 /* Next argument. */
811 mask >>= ARGT_BITS;
812 idx++;
813 }
814}
815
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100816/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500817 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100818 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100819 *
820 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100821 * - hlua_sethlua : create the association between hlua context and lua_state.
822 */
823static inline struct hlua *hlua_gethlua(lua_State *L)
824{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100825 struct hlua **hlua = lua_getextraspace(L);
826 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100827}
828static inline void hlua_sethlua(struct hlua *hlua)
829{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100830 struct hlua **hlua_store = lua_getextraspace(hlua->T);
831 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100832}
833
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100834/* This function is used to send logs. It try to send on screen (stderr)
835 * and on the default syslog server.
836 */
837static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
838{
839 struct tm tm;
840 char *p;
841
842 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200843 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100844 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200845 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200846 /* Break the message if exceed the buffer size. */
847 *(p-4) = ' ';
848 *(p-3) = '.';
849 *(p-2) = '.';
850 *(p-1) = '.';
851 break;
852 }
Willy Tarreau90807112020-02-25 08:16:33 +0100853 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 *p = *msg;
855 else
856 *p = '.';
857 }
858 *p = '\0';
859
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200860 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100861 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200862 get_localtime(date.tv_sec, &tm);
863 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100864 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200865 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100866 fflush(stderr);
867 }
868}
869
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100870/* This function just ensure that the yield will be always
871 * returned with a timeout and permit to set some flags
872 */
873__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100874 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100875{
876 struct hlua *hlua = hlua_gethlua(L);
877
878 /* Set the wake timeout. If timeout is required, we set
879 * the expiration time.
880 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200881 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100882
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100883 hlua->flags |= flags;
884
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100885 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200886 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100887}
888
Willy Tarreau87b09662015-04-03 00:22:06 +0200889/* This function initialises the Lua environment stored in the stream.
890 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100891 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200892 *
893 * This function is particular. it initialises a new Lua thread. If the
894 * initialisation fails (example: out of memory error), the lua function
895 * throws an error (longjmp).
896 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100897 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500898 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100899 * threads appear, the safe environment set a lock to ensure only one
900 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500901 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100902 *
903 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500904 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100905 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800906 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200907 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800908 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500909 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100910 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100911int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100912{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100913 if (!already_safe) {
914 if (!SET_SAFE_LJMP(gL.T)) {
915 lua->Tref = LUA_REFNIL;
916 return 0;
917 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200918 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100919 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100920 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100921 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100922 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100923 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100924 lua->T = lua_newthread(gL.T);
925 if (!lua->T) {
926 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100927 if (!already_safe)
928 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100929 return 0;
930 }
931 hlua_sethlua(lua);
932 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
933 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100934 if (!already_safe)
935 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100936 return 1;
937}
938
Willy Tarreau87b09662015-04-03 00:22:06 +0200939/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100940 * is destroyed. The destroy also the memory context. The struct "lua"
941 * is not freed.
942 */
943void hlua_ctx_destroy(struct hlua *lua)
944{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100945 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100946 return;
947
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100948 if (!lua->T)
949 goto end;
950
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100951 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200952 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100953
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200954 if (!SET_SAFE_LJMP(lua->T))
955 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100956 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200957 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200958
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200959 if (!SET_SAFE_LJMP(gL.T))
960 return;
961 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
962 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200963 /* Forces a garbage collecting process. If the Lua program is finished
964 * without error, we run the GC on the thread pointer. Its freed all
965 * the unused memory.
966 * If the thread is finnish with an error or is currently yielded,
967 * it seems that the GC applied on the thread doesn't clean anything,
968 * so e run the GC on the main thread.
969 * NOTE: maybe this action locks all the Lua threads untiml the en of
970 * the garbage collection.
971 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100972 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200973 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200974 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200975 lua_gc(gL.T, LUA_GCCOLLECT, 0);
976 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200977 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200978
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200979 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100980
981end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100982 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100983}
984
985/* This function is used to restore the Lua context when a coroutine
986 * fails. This function copy the common memory between old coroutine
987 * and the new coroutine. The old coroutine is destroyed, and its
988 * replaced by the new coroutine.
989 * If the flag "keep_msg" is set, the last entry of the old is assumed
990 * as string error message and it is copied in the new stack.
991 */
992static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
993{
994 lua_State *T;
995 int new_ref;
996
997 /* Renew the main LUA stack doesn't have sense. */
998 if (lua == &gL)
999 return 0;
1000
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001001 /* New Lua coroutine. */
1002 T = lua_newthread(gL.T);
1003 if (!T)
1004 return 0;
1005
1006 /* Copy last error message. */
1007 if (keep_msg)
1008 lua_xmove(lua->T, T, 1);
1009
1010 /* Copy data between the coroutines. */
1011 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1012 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001013 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001014
1015 /* Destroy old data. */
1016 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1017
1018 /* The thread is garbage collected by Lua. */
1019 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1020
1021 /* Fill the struct with the new coroutine values. */
1022 lua->Mref = new_ref;
1023 lua->T = T;
1024 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1025
1026 /* Set context. */
1027 hlua_sethlua(lua);
1028
1029 return 1;
1030}
1031
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001032void hlua_hook(lua_State *L, lua_Debug *ar)
1033{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001034 struct hlua *hlua = hlua_gethlua(L);
1035
1036 /* Lua cannot yield when its returning from a function,
1037 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001038 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001039 */
1040 if (lua_gethookmask(L) & LUA_MASKRET) {
1041 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1042 return;
1043 }
1044
1045 /* restore the interrupt condition. */
1046 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1047
1048 /* If we interrupt the Lua processing in yieldable state, we yield.
1049 * If the state is not yieldable, trying yield causes an error.
1050 */
1051 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001052 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001053
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001054 /* If we cannot yield, update the clock and check the timeout. */
1055 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001056 hlua->run_time += now_ms - hlua->start_time;
1057 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001058 lua_pushfstring(L, "execution timeout");
1059 WILL_LJMP(lua_error(L));
1060 }
1061
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001062 /* Update the start time. */
1063 hlua->start_time = now_ms;
1064
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001065 /* Try to interrupt the process at the end of the current
1066 * unyieldable function.
1067 */
1068 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001069}
1070
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001071/* This function start or resumes the Lua stack execution. If the flag
1072 * "yield_allowed" if no set and the LUA stack execution returns a yield
1073 * The function return an error.
1074 *
1075 * The function can returns 4 values:
1076 * - HLUA_E_OK : The execution is terminated without any errors.
1077 * - HLUA_E_AGAIN : The execution must continue at the next associated
1078 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001079 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001080 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001081 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001082 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001083 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001084 * LUA code.
1085 */
1086static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1087{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001088#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1089 int nres;
1090#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001091 int ret;
1092 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001093 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001094
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001095 /* Initialise run time counter. */
1096 if (!HLUA_IS_RUNNING(lua))
1097 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001098
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001099 /* Lock the whole Lua execution. This lock must be before the
1100 * label "resume_execution".
1101 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001102 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001103
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001104resume_execution:
1105
1106 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1107 * instructions. it is used for preventing infinite loops.
1108 */
1109 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1110
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001111 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001112 HLUA_SET_RUN(lua);
1113 HLUA_CLR_CTRLYIELD(lua);
1114 HLUA_CLR_WAKERESWR(lua);
1115 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001116
Christopher Fauletbc275a92020-02-26 14:55:16 +01001117 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001118 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001119 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001120
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001121 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001122#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1123 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1124#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001125 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001126#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001127 switch (ret) {
1128
1129 case LUA_OK:
1130 ret = HLUA_E_OK;
1131 break;
1132
1133 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001134 /* Check if the execution timeout is expired. It it is the case, we
1135 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001136 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001137 tv_update_date(0, 1);
1138 lua->run_time += now_ms - lua->start_time;
1139 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001140 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001141 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001142 break;
1143 }
1144 /* Process the forced yield. if the general yield is not allowed or
1145 * if no task were associated this the current Lua execution
1146 * coroutine, we resume the execution. Else we want to return in the
1147 * scheduler and we want to be waked up again, to continue the
1148 * current Lua execution. So we schedule our own task.
1149 */
1150 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001151 if (!yield_allowed || !lua->task)
1152 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001153 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001154 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001155 if (!yield_allowed) {
1156 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001157 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001158 break;
1159 }
1160 ret = HLUA_E_AGAIN;
1161 break;
1162
1163 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001164
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001165 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001166 * because the errors ares the only one mean to return immediately
1167 * from and lua execution.
1168 */
1169 if (lua->flags & HLUA_EXIT) {
1170 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001171 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001172 break;
1173 }
1174
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001175 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001176 if (!lua_checkstack(lua->T, 1)) {
1177 ret = HLUA_E_ERR;
1178 break;
1179 }
1180 msg = lua_tostring(lua->T, -1);
1181 lua_settop(lua->T, 0); /* Empty the stack. */
1182 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001183 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001184 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001185 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001186 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001187 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001188 ret = HLUA_E_ERRMSG;
1189 break;
1190
1191 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001192 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001194 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001195 break;
1196
1197 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001198 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001199 if (!lua_checkstack(lua->T, 1)) {
1200 ret = HLUA_E_ERR;
1201 break;
1202 }
1203 msg = lua_tostring(lua->T, -1);
1204 lua_settop(lua->T, 0); /* Empty the stack. */
1205 lua_pop(lua->T, 1);
1206 if (msg)
1207 lua_pushfstring(lua->T, "message handler error: %s", msg);
1208 else
1209 lua_pushfstring(lua->T, "message handler error");
1210 ret = HLUA_E_ERRMSG;
1211 break;
1212
1213 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001214 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001215 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001216 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001217 break;
1218 }
1219
1220 switch (ret) {
1221 case HLUA_E_AGAIN:
1222 break;
1223
1224 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001225 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001226 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001227 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001228 break;
1229
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001230 case HLUA_E_ETMOUT:
1231 case HLUA_E_NOMEM:
1232 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001233 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001234 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001235 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001236 hlua_ctx_renew(lua, 0);
1237 break;
1238
1239 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001240 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001241 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001242 break;
1243 }
1244
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001245 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001246 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001247
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001248 return ret;
1249}
1250
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001251/* This function exit the current code. */
1252__LJMP static int hlua_done(lua_State *L)
1253{
1254 struct hlua *hlua = hlua_gethlua(L);
1255
1256 hlua->flags |= HLUA_EXIT;
1257 WILL_LJMP(lua_error(L));
1258
1259 return 0;
1260}
1261
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001262/* This function is an LUA binding. It provides a function
1263 * for deleting ACL from a referenced ACL file.
1264 */
1265__LJMP static int hlua_del_acl(lua_State *L)
1266{
1267 const char *name;
1268 const char *key;
1269 struct pat_ref *ref;
1270
1271 MAY_LJMP(check_args(L, 2, "del_acl"));
1272
1273 name = MAY_LJMP(luaL_checkstring(L, 1));
1274 key = MAY_LJMP(luaL_checkstring(L, 2));
1275
1276 ref = pat_ref_lookup(name);
1277 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001278 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001279
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001280 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001281 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001282 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001283 return 0;
1284}
1285
1286/* This function is an LUA binding. It provides a function
1287 * for deleting map entry from a referenced map file.
1288 */
1289static int hlua_del_map(lua_State *L)
1290{
1291 const char *name;
1292 const char *key;
1293 struct pat_ref *ref;
1294
1295 MAY_LJMP(check_args(L, 2, "del_map"));
1296
1297 name = MAY_LJMP(luaL_checkstring(L, 1));
1298 key = MAY_LJMP(luaL_checkstring(L, 2));
1299
1300 ref = pat_ref_lookup(name);
1301 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001302 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001303
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001304 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001305 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001306 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001307 return 0;
1308}
1309
1310/* This function is an LUA binding. It provides a function
1311 * for adding ACL pattern from a referenced ACL file.
1312 */
1313static int hlua_add_acl(lua_State *L)
1314{
1315 const char *name;
1316 const char *key;
1317 struct pat_ref *ref;
1318
1319 MAY_LJMP(check_args(L, 2, "add_acl"));
1320
1321 name = MAY_LJMP(luaL_checkstring(L, 1));
1322 key = MAY_LJMP(luaL_checkstring(L, 2));
1323
1324 ref = pat_ref_lookup(name);
1325 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001326 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001327
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001328 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001329 if (pat_ref_find_elt(ref, key) == NULL)
1330 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001331 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001332 return 0;
1333}
1334
1335/* This function is an LUA binding. It provides a function
1336 * for setting map pattern and sample from a referenced map
1337 * file.
1338 */
1339static int hlua_set_map(lua_State *L)
1340{
1341 const char *name;
1342 const char *key;
1343 const char *value;
1344 struct pat_ref *ref;
1345
1346 MAY_LJMP(check_args(L, 3, "set_map"));
1347
1348 name = MAY_LJMP(luaL_checkstring(L, 1));
1349 key = MAY_LJMP(luaL_checkstring(L, 2));
1350 value = MAY_LJMP(luaL_checkstring(L, 3));
1351
1352 ref = pat_ref_lookup(name);
1353 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001354 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001355
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001356 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001357 if (pat_ref_find_elt(ref, key) != NULL)
1358 pat_ref_set(ref, key, value, NULL);
1359 else
1360 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001361 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001362 return 0;
1363}
1364
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001365/* A class is a lot of memory that contain data. This data can be a table,
1366 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001367 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001368 * the name of the object (_G[<name>] = <metable> ).
1369 *
1370 * A metable is a table that modify the standard behavior of a standard
1371 * access to the associated data. The entries of this new metatable are
1372 * defined as is:
1373 *
1374 * http://lua-users.org/wiki/MetatableEvents
1375 *
1376 * __index
1377 *
1378 * we access an absent field in a table, the result is nil. This is
1379 * true, but it is not the whole truth. Actually, such access triggers
1380 * the interpreter to look for an __index metamethod: If there is no
1381 * such method, as usually happens, then the access results in nil;
1382 * otherwise, the metamethod will provide the result.
1383 *
1384 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1385 * the key does not appear in the table, but the metatable has an __index
1386 * property:
1387 *
1388 * - if the value is a function, the function is called, passing in the
1389 * table and the key; the return value of that function is returned as
1390 * the result.
1391 *
1392 * - if the value is another table, the value of the key in that table is
1393 * asked for and returned (and if it doesn't exist in that table, but that
1394 * table's metatable has an __index property, then it continues on up)
1395 *
1396 * - Use "rawget(myTable,key)" to skip this metamethod.
1397 *
1398 * http://www.lua.org/pil/13.4.1.html
1399 *
1400 * __newindex
1401 *
1402 * Like __index, but control property assignment.
1403 *
1404 * __mode - Control weak references. A string value with one or both
1405 * of the characters 'k' and 'v' which specifies that the the
1406 * keys and/or values in the table are weak references.
1407 *
1408 * __call - Treat a table like a function. When a table is followed by
1409 * parenthesis such as "myTable( 'foo' )" and the metatable has
1410 * a __call key pointing to a function, that function is invoked
1411 * (passing any specified arguments) and the return value is
1412 * returned.
1413 *
1414 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1415 * called, if the metatable for myTable has a __metatable
1416 * key, the value of that key is returned instead of the
1417 * actual metatable.
1418 *
1419 * __tostring - Control string representation. When the builtin
1420 * "tostring( myTable )" function is called, if the metatable
1421 * for myTable has a __tostring property set to a function,
1422 * that function is invoked (passing myTable to it) and the
1423 * return value is used as the string representation.
1424 *
1425 * __len - Control table length. When the table length is requested using
1426 * the length operator ( '#' ), if the metatable for myTable has
1427 * a __len key pointing to a function, that function is invoked
1428 * (passing myTable to it) and the return value used as the value
1429 * of "#myTable".
1430 *
1431 * __gc - Userdata finalizer code. When userdata is set to be garbage
1432 * collected, if the metatable has a __gc field pointing to a
1433 * function, that function is first invoked, passing the userdata
1434 * to it. The __gc metamethod is not called for tables.
1435 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1436 *
1437 * Special metamethods for redefining standard operators:
1438 * http://www.lua.org/pil/13.1.html
1439 *
1440 * __add "+"
1441 * __sub "-"
1442 * __mul "*"
1443 * __div "/"
1444 * __unm "!"
1445 * __pow "^"
1446 * __concat ".."
1447 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001448 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001449 * http://www.lua.org/pil/13.2.html
1450 *
1451 * __eq "=="
1452 * __lt "<"
1453 * __le "<="
1454 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001455
1456/*
1457 *
1458 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001459 * Class Map
1460 *
1461 *
1462 */
1463
1464/* Returns a struct hlua_map if the stack entry "ud" is
1465 * a class session, otherwise it throws an error.
1466 */
1467__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1468{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001469 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001470}
1471
1472/* This function is the map constructor. It don't need
1473 * the class Map object. It creates and return a new Map
1474 * object. It must be called only during "body" or "init"
1475 * context because it process some filesystem accesses.
1476 */
1477__LJMP static int hlua_map_new(struct lua_State *L)
1478{
1479 const char *fn;
1480 int match = PAT_MATCH_STR;
1481 struct sample_conv conv;
1482 const char *file = "";
1483 int line = 0;
1484 lua_Debug ar;
1485 char *err = NULL;
1486 struct arg args[2];
1487
1488 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1489 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1490
1491 fn = MAY_LJMP(luaL_checkstring(L, 1));
1492
1493 if (lua_gettop(L) >= 2) {
1494 match = MAY_LJMP(luaL_checkinteger(L, 2));
1495 if (match < 0 || match >= PAT_MATCH_NUM)
1496 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1497 }
1498
1499 /* Get Lua filename and line number. */
1500 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1501 lua_getinfo(L, "Sl", &ar); /* get info about it */
1502 if (ar.currentline > 0) { /* is there info? */
1503 file = ar.short_src;
1504 line = ar.currentline;
1505 }
1506 }
1507
1508 /* fill fake sample_conv struct. */
1509 conv.kw = ""; /* unused. */
1510 conv.process = NULL; /* unused. */
1511 conv.arg_mask = 0; /* unused. */
1512 conv.val_args = NULL; /* unused. */
1513 conv.out_type = SMP_T_STR;
1514 conv.private = (void *)(long)match;
1515 switch (match) {
1516 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1517 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1518 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1519 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1520 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1521 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1522 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001523 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001524 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1525 default:
1526 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1527 }
1528
1529 /* fill fake args. */
1530 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001531 args[0].data.str.area = strdup(fn);
1532 args[0].data.str.data = strlen(fn);
1533 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001534 args[1].type = ARGT_STOP;
1535
1536 /* load the map. */
1537 if (!sample_load_map(args, &conv, file, line, &err)) {
1538 /* error case: we cant use luaL_error because we must
1539 * free the err variable.
1540 */
1541 luaL_where(L, 1);
1542 lua_pushfstring(L, "'new': %s.", err);
1543 lua_concat(L, 2);
1544 free(err);
Christopher Faulet73292e92020-08-06 08:40:09 +02001545 free(args[0].data.str.area);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001546 WILL_LJMP(lua_error(L));
1547 }
1548
1549 /* create the lua object. */
1550 lua_newtable(L);
1551 lua_pushlightuserdata(L, args[0].data.map);
1552 lua_rawseti(L, -2, 0);
1553
1554 /* Pop a class Map metatable and affect it to the userdata. */
1555 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1556 lua_setmetatable(L, -2);
1557
1558
1559 return 1;
1560}
1561
1562__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1563{
1564 struct map_descriptor *desc;
1565 struct pattern *pat;
1566 struct sample smp;
1567
1568 MAY_LJMP(check_args(L, 2, "lookup"));
1569 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001570 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001571 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001572 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001573 }
1574 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001575 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001576 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001577 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 +02001578 }
1579
1580 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001581 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001582 if (str)
1583 lua_pushstring(L, "");
1584 else
1585 lua_pushnil(L);
1586 return 1;
1587 }
1588
1589 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001590 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001591 return 1;
1592}
1593
1594__LJMP static int hlua_map_lookup(struct lua_State *L)
1595{
1596 return _hlua_map_lookup(L, 0);
1597}
1598
1599__LJMP static int hlua_map_slookup(struct lua_State *L)
1600{
1601 return _hlua_map_lookup(L, 1);
1602}
1603
1604/*
1605 *
1606 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001607 * Class Socket
1608 *
1609 *
1610 */
1611
1612__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1613{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001614 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001615}
1616
1617/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001618 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001619 * received.
1620 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001621static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001622{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001623 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001624
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001625 if (appctx->ctx.hlua_cosocket.die) {
1626 si_shutw(si);
1627 si_shutr(si);
1628 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001629 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1630 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001631 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1632 }
1633
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001634 /* If we cant write, wakeup the pending write signals. */
1635 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001636 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001637
1638 /* If we cant read, wakeup the pending read signals. */
1639 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001640 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001641
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001642 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001643 * to be notified whenever the connection completes.
1644 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001645 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001646 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001647 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001648 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001649 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001650 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001651
1652 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001653 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001655 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001656 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001657 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001658
1659 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001660 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001661 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001662
1663 /* Some data were injected in the buffer, notify the stream
1664 * interface.
1665 */
1666 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001667 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001668
1669 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001670 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001671 */
1672 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001673 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001674}
1675
Willy Tarreau87b09662015-04-03 00:22:06 +02001676/* This function is called when the "struct stream" is destroyed.
1677 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001678 * Wake all the pending signals.
1679 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001680static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001681{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001682 struct xref *peer;
1683
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001684 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001685 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1686 if (peer)
1687 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001688
1689 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001690 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1691 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692}
1693
1694/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001695 * uses this object. If the stream does not exists, just quit.
1696 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 * pending signal can rest in the read and write lists. destroy
1698 * it.
1699 */
1700__LJMP static int hlua_socket_gc(lua_State *L)
1701{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001702 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001704 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001706 MAY_LJMP(check_args(L, 1, "__gc"));
1707
1708 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001709 peer = xref_get_peer_and_lock(&socket->xref);
1710 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001711 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001712 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001714 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001715 appctx->ctx.hlua_cosocket.die = 1;
1716 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001717
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001718 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001719 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720 return 0;
1721}
1722
1723/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001724 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725 */
sada05ed3302018-05-11 11:48:18 -07001726__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001728 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001729 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001730 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001731 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001732
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001733 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001734
1735 /* Check if we run on the same thread than the xreator thread.
1736 * We cannot access to the socket if the thread is different.
1737 */
1738 if (socket->tid != tid)
1739 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1740
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001741 peer = xref_get_peer_and_lock(&socket->xref);
1742 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001743 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001744
1745 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001746 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001747
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001748 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001749 appctx->ctx.hlua_cosocket.die = 1;
1750 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001751
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001752 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001753 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001754 return 0;
1755}
1756
sada05ed3302018-05-11 11:48:18 -07001757/* The close function calls close_helper.
1758 */
1759__LJMP static int hlua_socket_close(lua_State *L)
1760{
1761 MAY_LJMP(check_args(L, 1, "close"));
1762 return hlua_socket_close_helper(L);
1763}
1764
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001765/* This Lua function assumes that the stack contain three parameters.
1766 * 1 - USERDATA containing a struct socket
1767 * 2 - INTEGER with values of the macro defined below
1768 * If the integer is -1, we must read at most one line.
1769 * If the integer is -2, we ust read all the data until the
1770 * end of the stream.
1771 * If the integer is positive value, we must read a number of
1772 * bytes corresponding to this value.
1773 */
1774#define HLSR_READ_LINE (-1)
1775#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001776__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001777{
1778 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1779 int wanted = lua_tointeger(L, 2);
1780 struct hlua *hlua = hlua_gethlua(L);
1781 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001782 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001784 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001785 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001786 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001787 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001788 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001789 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001790 struct stream_interface *si;
1791 struct stream *s;
1792 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001793 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794
1795 /* Check if this lua stack is schedulable. */
1796 if (!hlua || !hlua->task)
1797 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1798 "'frontend', 'backend' or 'task'"));
1799
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001800 /* Check if we run on the same thread than the xreator thread.
1801 * We cannot access to the socket if the thread is different.
1802 */
1803 if (socket->tid != tid)
1804 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1805
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001806 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001807 peer = xref_get_peer_and_lock(&socket->xref);
1808 if (!peer)
1809 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001810 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1811 si = appctx->owner;
1812 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001814 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001817 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 if (nblk < 0) /* Connection close. */
1819 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001820 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001821 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001822
1823 /* remove final \r\n. */
1824 if (nblk == 1) {
1825 if (blk1[len1-1] == '\n') {
1826 len1--;
1827 skip_at_end++;
1828 if (blk1[len1-1] == '\r') {
1829 len1--;
1830 skip_at_end++;
1831 }
1832 }
1833 }
1834 else {
1835 if (blk2[len2-1] == '\n') {
1836 len2--;
1837 skip_at_end++;
1838 if (blk2[len2-1] == '\r') {
1839 len2--;
1840 skip_at_end++;
1841 }
1842 }
1843 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001844 }
1845
1846 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001847 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001848 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001849 if (nblk < 0) /* Connection close. */
1850 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001851 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001852 goto connection_empty;
1853 }
1854
1855 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001856 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001857 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858 if (nblk < 0) /* Connection close. */
1859 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001860 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861 goto connection_empty;
1862
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001863 missing_bytes = wanted - socket->b.n;
1864 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001865 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001866 len1 = missing_bytes;
1867 } if (nblk == 2 && len1 + len2 > missing_bytes)
1868 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 }
1870
1871 len = len1;
1872
1873 luaL_addlstring(&socket->b, blk1, len1);
1874 if (nblk == 2) {
1875 len += len2;
1876 luaL_addlstring(&socket->b, blk2, len2);
1877 }
1878
1879 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001880 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001881
1882 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001883 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884
1885 /* If the pattern reclaim to read all the data
1886 * in the connection, got out.
1887 */
1888 if (wanted == HLSR_READ_ALL)
1889 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001890 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891 goto connection_empty;
1892
1893 /* Return result. */
1894 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001895 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896 return 1;
1897
1898connection_closed:
1899
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001900 xref_unlock(&socket->xref, peer);
1901
1902no_peer:
1903
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 /* If the buffer containds data. */
1905 if (socket->b.n > 0) {
1906 luaL_pushresult(&socket->b);
1907 return 1;
1908 }
1909 lua_pushnil(L);
1910 lua_pushstring(L, "connection closed.");
1911 return 2;
1912
1913connection_empty:
1914
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001915 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1916 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001917 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001918 }
1919 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001920 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921 return 0;
1922}
1923
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001924/* This Lua function gets two parameters. The first one can be string
1925 * or a number. If the string is "*l", the user requires one line. If
1926 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927 * If the value is a number, the user require a number of bytes equal
1928 * to the value. The default value is "*l" (a line).
1929 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001930 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931 * integer takes this values:
1932 * -1 : read a line
1933 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001934 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001935 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001936 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001937 * concatenated with the read data.
1938 */
1939__LJMP static int hlua_socket_receive(struct lua_State *L)
1940{
1941 int wanted = HLSR_READ_LINE;
1942 const char *pattern;
1943 int type;
1944 char *error;
1945 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001946 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947
1948 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1949 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1950
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001951 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001952
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001953 /* Check if we run on the same thread than the xreator thread.
1954 * We cannot access to the socket if the thread is different.
1955 */
1956 if (socket->tid != tid)
1957 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1958
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959 /* check for pattern. */
1960 if (lua_gettop(L) >= 2) {
1961 type = lua_type(L, 2);
1962 if (type == LUA_TSTRING) {
1963 pattern = lua_tostring(L, 2);
1964 if (strcmp(pattern, "*a") == 0)
1965 wanted = HLSR_READ_ALL;
1966 else if (strcmp(pattern, "*l") == 0)
1967 wanted = HLSR_READ_LINE;
1968 else {
1969 wanted = strtoll(pattern, &error, 10);
1970 if (*error != '\0')
1971 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1972 }
1973 }
1974 else if (type == LUA_TNUMBER) {
1975 wanted = lua_tointeger(L, 2);
1976 if (wanted < 0)
1977 WILL_LJMP(luaL_error(L, "Unsupported size."));
1978 }
1979 }
1980
1981 /* Set pattern. */
1982 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001983
1984 /* Check if we would replace the top by itself. */
1985 if (lua_gettop(L) != 2)
1986 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001987
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001988 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001989 luaL_buffinit(L, &socket->b);
1990
1991 /* Check prefix. */
1992 if (lua_gettop(L) >= 3) {
1993 if (lua_type(L, 3) != LUA_TSTRING)
1994 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1995 pattern = lua_tolstring(L, 3, &len);
1996 luaL_addlstring(&socket->b, pattern, len);
1997 }
1998
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001999 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000}
2001
2002/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002003 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002004 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002005static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002006{
2007 struct hlua_socket *socket;
2008 struct hlua *hlua = hlua_gethlua(L);
2009 struct appctx *appctx;
2010 size_t buf_len;
2011 const char *buf;
2012 int len;
2013 int send_len;
2014 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002015 struct xref *peer;
2016 struct stream_interface *si;
2017 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002018
2019 /* Check if this lua stack is schedulable. */
2020 if (!hlua || !hlua->task)
2021 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2022 "'frontend', 'backend' or 'task'"));
2023
2024 /* Get object */
2025 socket = MAY_LJMP(hlua_checksocket(L, 1));
2026 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002027 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002029 /* Check if we run on the same thread than the xreator thread.
2030 * We cannot access to the socket if the thread is different.
2031 */
2032 if (socket->tid != tid)
2033 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2034
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002035 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002036 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002037 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002038 lua_pushinteger(L, -1);
2039 return 1;
2040 }
2041 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2042 si = appctx->owner;
2043 s = si_strm(si);
2044
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002046 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002047 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 lua_pushinteger(L, -1);
2049 return 1;
2050 }
2051
2052 /* Update the input buffer data. */
2053 buf += sent;
2054 send_len = buf_len - sent;
2055
2056 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002057 if (sent >= buf_len) {
2058 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002059 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002060 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002061
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002062 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002063 * the request buffer if its not required.
2064 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002065 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002066 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002067 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002068 }
2069
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002070 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002071 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002072 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002074 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075
2076 /* send data */
2077 if (len < send_len)
2078 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002079 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002080
2081 /* "Not enough space" (-1), "Buffer too little to contain
2082 * the data" (-2) are not expected because the available length
2083 * is tested.
2084 * Other unknown error are also not expected.
2085 */
2086 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002087 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002088 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002089
sada05ed3302018-05-11 11:48:18 -07002090 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002091 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002092 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002093 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002094 return 1;
2095 }
2096
2097 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002098 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002099
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002100 s->req.rex = TICK_ETERNITY;
2101 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102
2103 /* Update length sent. */
2104 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002105 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002106
2107 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002108 if (sent + len >= buf_len) {
2109 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002110 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002111 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002112
2113hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002114 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2115 xref_unlock(&socket->xref, peer);
2116 WILL_LJMP(luaL_error(L, "out of memory"));
2117 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002118 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002119 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002120 return 0;
2121}
2122
2123/* This function initiate the send of data. It just check the input
2124 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002125 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002126 * "hlua_socket_write_yield" that can yield.
2127 *
2128 * The Lua function gets between 3 and 4 parameters. The first one is
2129 * the associated object. The second is a string buffer. The third is
2130 * a facultative integer that represents where is the buffer position
2131 * of the start of the data that can send. The first byte is the
2132 * position "1". The default value is "1". The fourth argument is a
2133 * facultative integer that represents where is the buffer position
2134 * of the end of the data that can send. The default is the last byte.
2135 */
2136static int hlua_socket_send(struct lua_State *L)
2137{
2138 int i;
2139 int j;
2140 const char *buf;
2141 size_t buf_len;
2142
2143 /* Check number of arguments. */
2144 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2145 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2146
2147 /* Get the string. */
2148 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2149
2150 /* Get and check j. */
2151 if (lua_gettop(L) == 4) {
2152 j = MAY_LJMP(luaL_checkinteger(L, 4));
2153 if (j < 0)
2154 j = buf_len + j + 1;
2155 if (j > buf_len)
2156 j = buf_len + 1;
2157 lua_pop(L, 1);
2158 }
2159 else
2160 j = buf_len;
2161
2162 /* Get and check i. */
2163 if (lua_gettop(L) == 3) {
2164 i = MAY_LJMP(luaL_checkinteger(L, 3));
2165 if (i < 0)
2166 i = buf_len + i + 1;
2167 if (i > buf_len)
2168 i = buf_len + 1;
2169 lua_pop(L, 1);
2170 } else
2171 i = 1;
2172
2173 /* Check bth i and j. */
2174 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002175 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002176 return 1;
2177 }
2178 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002179 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180 return 1;
2181 }
2182 if (i == 0)
2183 i = 1;
2184 if (j == 0)
2185 j = 1;
2186
2187 /* Pop the string. */
2188 lua_pop(L, 1);
2189
2190 /* Update the buffer length. */
2191 buf += i - 1;
2192 buf_len = j - i + 1;
2193 lua_pushlstring(L, buf, buf_len);
2194
2195 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002196 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002197
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002198 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002199}
2200
Willy Tarreau22b0a682015-06-17 19:43:49 +02002201#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2203{
2204 static char buffer[SOCKET_INFO_MAX_LEN];
2205 int ret;
2206 int len;
2207 char *p;
2208
2209 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2210 if (ret <= 0) {
2211 lua_pushnil(L);
2212 return 1;
2213 }
2214
2215 if (ret == AF_UNIX) {
2216 lua_pushstring(L, buffer+1);
2217 return 1;
2218 }
2219 else if (ret == AF_INET6) {
2220 buffer[0] = '[';
2221 len = strlen(buffer);
2222 buffer[len] = ']';
2223 len++;
2224 buffer[len] = ':';
2225 len++;
2226 p = buffer;
2227 }
2228 else if (ret == AF_INET) {
2229 p = buffer + 1;
2230 len = strlen(p);
2231 p[len] = ':';
2232 len++;
2233 }
2234 else {
2235 lua_pushnil(L);
2236 return 1;
2237 }
2238
2239 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2240 lua_pushnil(L);
2241 return 1;
2242 }
2243
2244 lua_pushstring(L, p);
2245 return 1;
2246}
2247
2248/* Returns information about the peer of the connection. */
2249__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2250{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002251 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002252 struct xref *peer;
2253 struct appctx *appctx;
2254 struct stream_interface *si;
2255 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002256 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002257
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258 MAY_LJMP(check_args(L, 1, "getpeername"));
2259
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002260 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002261
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002262 /* Check if we run on the same thread than the xreator thread.
2263 * We cannot access to the socket if the thread is different.
2264 */
2265 if (socket->tid != tid)
2266 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2267
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002268 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002269 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002270 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271 lua_pushnil(L);
2272 return 1;
2273 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002274 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2275 si = appctx->owner;
2276 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002277
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002278 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002279 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002280 lua_pushnil(L);
2281 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002282 }
2283
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002284 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002285 xref_unlock(&socket->xref, peer);
2286 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287}
2288
2289/* Returns information about my connection side. */
2290static int hlua_socket_getsockname(struct lua_State *L)
2291{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002292 struct hlua_socket *socket;
2293 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 struct appctx *appctx;
2295 struct xref *peer;
2296 struct stream_interface *si;
2297 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002298 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002299
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300 MAY_LJMP(check_args(L, 1, "getsockname"));
2301
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002302 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002304 /* Check if we run on the same thread than the xreator thread.
2305 * We cannot access to the socket if the thread is different.
2306 */
2307 if (socket->tid != tid)
2308 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2309
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002310 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002311 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002312 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002313 lua_pushnil(L);
2314 return 1;
2315 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002316 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2317 si = appctx->owner;
2318 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002320 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002321 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002322 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002323 lua_pushnil(L);
2324 return 1;
2325 }
2326
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002327 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002328 xref_unlock(&socket->xref, peer);
2329 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002330}
2331
2332/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002333static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334 .obj_type = OBJ_TYPE_APPLET,
2335 .name = "<LUA_TCP>",
2336 .fct = hlua_socket_handler,
2337 .release = hlua_socket_release,
2338};
2339
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002340__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002341{
2342 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2343 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002344 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002345 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002346 struct stream_interface *si;
2347 struct stream *s;
2348
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002349 /* Check if we run on the same thread than the xreator thread.
2350 * We cannot access to the socket if the thread is different.
2351 */
2352 if (socket->tid != tid)
2353 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2354
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002355 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002356 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002357 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002358 lua_pushnil(L);
2359 lua_pushstring(L, "Can't connect");
2360 return 2;
2361 }
2362 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2363 si = appctx->owner;
2364 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002365
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002366 /* Check if we run on the same thread than the xreator thread.
2367 * We cannot access to the socket if the thread is different.
2368 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002369 if (socket->tid != tid) {
2370 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002371 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002372 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002373
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002375 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002376 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002377 lua_pushnil(L);
2378 lua_pushstring(L, "Can't connect");
2379 return 2;
2380 }
2381
Willy Tarreaue09101e2018-10-16 17:37:12 +02002382 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002383
2384 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002385 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002386 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387 lua_pushinteger(L, 1);
2388 return 1;
2389 }
2390
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002391 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2392 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002393 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002394 }
2395 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002396 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002397 return 0;
2398}
2399
2400/* This function fail or initite the connection. */
2401__LJMP static int hlua_socket_connect(struct lua_State *L)
2402{
2403 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002404 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002405 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002406 struct hlua *hlua;
2407 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002408 int low, high;
2409 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002410 struct xref *peer;
2411 struct stream_interface *si;
2412 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002413
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002414 if (lua_gettop(L) < 2)
2415 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416
2417 /* Get args. */
2418 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002419
2420 /* Check if we run on the same thread than the xreator thread.
2421 * We cannot access to the socket if the thread is different.
2422 */
2423 if (socket->tid != tid)
2424 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2425
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002426 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002427 if (lua_gettop(L) >= 3) {
2428 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002429 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002430
Tim Duesterhus6edab862018-01-06 19:04:45 +01002431 /* Force the ip to end with a colon, to support IPv6 addresses
2432 * that are not enclosed within square brackets.
2433 */
2434 if (port > 0) {
2435 luaL_buffinit(L, &b);
2436 luaL_addstring(&b, ip);
2437 luaL_addchar(&b, ':');
2438 luaL_pushresult(&b);
2439 ip = lua_tolstring(L, lua_gettop(L), NULL);
2440 }
2441 }
2442
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002443 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002444 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002445 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002446 lua_pushnil(L);
2447 return 1;
2448 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449
2450 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002451 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002452 if (!addr) {
2453 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002454 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002455 }
2456 if (low != high) {
2457 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002458 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002459 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002460
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002461 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002462 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002463 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002464 if (port == -1) {
2465 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002466 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002467 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002468 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2469 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002470 if (port == -1) {
2471 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002472 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002473 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002474 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002475 }
2476 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002477
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002478 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2479 si = appctx->owner;
2480 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002481
2482 if (!sockaddr_alloc(&s->target_addr)) {
2483 xref_unlock(&socket->xref, peer);
2484 WILL_LJMP(luaL_error(L, "connect: internal error"));
2485 }
2486 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002487 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002489 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002490
2491 /* inform the stream that we want to be notified whenever the
2492 * connection completes.
2493 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002494 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002495 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002496 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002497
Willy Tarreauf31af932020-01-14 09:59:38 +01002498 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002499
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002500 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2501 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002502 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002503 }
2504 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002505
2506 task_wakeup(s->task, TASK_WOKEN_INIT);
2507 /* Return yield waiting for connection. */
2508
Willy Tarreau9635e032018-10-16 17:52:55 +02002509 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002510
2511 return 0;
2512}
2513
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002514#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002515__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2516{
2517 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002518 struct xref *peer;
2519 struct appctx *appctx;
2520 struct stream_interface *si;
2521 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002522
2523 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2524 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002525
2526 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002527 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002528 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002529 lua_pushnil(L);
2530 return 1;
2531 }
2532 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2533 si = appctx->owner;
2534 s = si_strm(si);
2535
2536 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002537 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538 return MAY_LJMP(hlua_socket_connect(L));
2539}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002540#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002541
2542__LJMP static int hlua_socket_setoption(struct lua_State *L)
2543{
2544 return 0;
2545}
2546
2547__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2548{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002549 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002550 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002551 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002552 struct xref *peer;
2553 struct appctx *appctx;
2554 struct stream_interface *si;
2555 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002556
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002557 MAY_LJMP(check_args(L, 2, "settimeout"));
2558
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002559 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002560
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002561 /* convert the timeout to millis */
2562 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002563
Thierry Fournier17a921b2018-03-08 09:59:02 +01002564 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002565 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002566 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2567
Mark Lakes56cc1252018-03-27 09:48:06 +02002568 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002569 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002570
2571 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002572 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002573 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002574
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002575 /* Check if we run on the same thread than the xreator thread.
2576 * We cannot access to the socket if the thread is different.
2577 */
2578 if (socket->tid != tid)
2579 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2580
Mark Lakes56cc1252018-03-27 09:48:06 +02002581 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002582 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002583 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002584 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2585 WILL_LJMP(lua_error(L));
2586 return 0;
2587 }
2588 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2589 si = appctx->owner;
2590 s = si_strm(si);
2591
Cyril Bonté7bb63452018-08-17 23:51:02 +02002592 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002593 s->req.rto = tmout;
2594 s->req.wto = tmout;
2595 s->res.rto = tmout;
2596 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002597 s->req.rex = tick_add_ifset(now_ms, tmout);
2598 s->req.wex = tick_add_ifset(now_ms, tmout);
2599 s->res.rex = tick_add_ifset(now_ms, tmout);
2600 s->res.wex = tick_add_ifset(now_ms, tmout);
2601
2602 s->task->expire = tick_add_ifset(now_ms, tmout);
2603 task_queue(s->task);
2604
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002605 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
Thierry Fourniere9636f12018-03-08 09:54:32 +01002607 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002608 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609}
2610
2611__LJMP static int hlua_socket_new(lua_State *L)
2612{
2613 struct hlua_socket *socket;
2614 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002615 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002616 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002617
2618 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002619 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002620 hlua_pusherror(L, "socket: full stack");
2621 goto out_fail_conf;
2622 }
2623
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002624 /* Create the object: obj[0] = userdata. */
2625 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002626 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002627 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002628 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002629 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002630
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002631 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002632 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002633 hlua_pusherror(L, "socket: uninitialized pools.");
2634 goto out_fail_conf;
2635 }
2636
Willy Tarreau87b09662015-04-03 00:22:06 +02002637 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002638 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2639 lua_setmetatable(L, -2);
2640
Willy Tarreaud420a972015-04-06 00:39:18 +02002641 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002642 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002643 if (!appctx) {
2644 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002645 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002646 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002648 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002649 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002650 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2651 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002652
Willy Tarreaud420a972015-04-06 00:39:18 +02002653 /* Now create a session, task and stream for this applet */
2654 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2655 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002656 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002657 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658 }
2659
Willy Tarreau87787ac2017-08-28 16:22:54 +02002660 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002661 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002662 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002663 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002664 }
2665
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002666 /* Initialise cross reference between stream and Lua socket object. */
2667 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002668
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002669 /* Configure "right" stream interface. this "si" is used to connect
2670 * and retrieve data from the server. The connection is initialized
2671 * with the "struct server".
2672 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002673 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002674
2675 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002676 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002677 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002678
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002679 return 1;
2680
Willy Tarreaud420a972015-04-06 00:39:18 +02002681 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002682 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002683 out_fail_sess:
2684 appctx_free(appctx);
2685 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002686 WILL_LJMP(lua_error(L));
2687 return 0;
2688}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002689
2690/*
2691 *
2692 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002693 * Class Channel
2694 *
2695 *
2696 */
2697
2698/* Returns the struct hlua_channel join to the class channel in the
2699 * stack entry "ud" or throws an argument error.
2700 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002701__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002702{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002703 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002704}
2705
Willy Tarreau47860ed2015-03-10 14:07:50 +01002706/* Pushes the channel onto the top of the stack. If the stask does not have a
2707 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002708 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002709static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002710{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002711 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002712 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002713 return 0;
2714
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002715 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002716 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002717 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002718
2719 /* Pop a class sesison metatable and affect it to the userdata. */
2720 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2721 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002722 return 1;
2723}
2724
2725/* Duplicate all the data present in the input channel and put it
2726 * in a string LUA variables. Returns -1 and push a nil value in
2727 * the stack if the channel is closed and all the data are consumed,
2728 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002729 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002730 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002731static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002732{
2733 char *blk1;
2734 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002735 size_t len1;
2736 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002737 int ret;
2738 luaL_Buffer b;
2739
Willy Tarreau06d80a92017-10-19 14:32:15 +02002740 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741 if (unlikely(ret == 0))
2742 return 0;
2743
2744 if (unlikely(ret < 0)) {
2745 lua_pushnil(L);
2746 return -1;
2747 }
2748
2749 luaL_buffinit(L, &b);
2750 luaL_addlstring(&b, blk1, len1);
2751 if (unlikely(ret == 2))
2752 luaL_addlstring(&b, blk2, len2);
2753 luaL_pushresult(&b);
2754
2755 if (unlikely(ret == 2))
2756 return len1 + len2;
2757 return len1;
2758}
2759
2760/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2761 * a yield. This function keep the data in the buffer.
2762 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002763__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002764{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002765 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002766
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002767 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2768
Christopher Faulet3f829a42018-12-13 21:56:45 +01002769 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2770 WILL_LJMP(lua_error(L));
2771
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002772 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002773 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002774 return 1;
2775}
2776
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002777/* Check arguments for the function "hlua_channel_dup_yield". */
2778__LJMP static int hlua_channel_dup(lua_State *L)
2779{
2780 MAY_LJMP(check_args(L, 1, "dup"));
2781 MAY_LJMP(hlua_checkchannel(L, 1));
2782 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2783}
2784
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002785/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2786 * a yield. This function consumes the data in the buffer. It returns
2787 * a string containing the data or a nil pointer if no data are available
2788 * and the channel is closed.
2789 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002790__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002791{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002792 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002793 int ret;
2794
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002795 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002796
Christopher Faulet3f829a42018-12-13 21:56:45 +01002797 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2798 WILL_LJMP(lua_error(L));
2799
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002800 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002802 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002803
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804 if (unlikely(ret == -1))
2805 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002807 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808 return 1;
2809}
2810
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002811/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002812__LJMP static int hlua_channel_get(lua_State *L)
2813{
2814 MAY_LJMP(check_args(L, 1, "get"));
2815 MAY_LJMP(hlua_checkchannel(L, 1));
2816 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2817}
2818
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002819/* This functions consumes and returns one line. If the channel is closed,
2820 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002821 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002822 * value.
2823 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002824__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002826 char *blk1;
2827 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002828 size_t len1;
2829 size_t len2;
2830 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002831 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002832 int ret;
2833 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002834
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002835 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2836
Christopher Faulet3f829a42018-12-13 21:56:45 +01002837 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2838 WILL_LJMP(lua_error(L));
2839
Willy Tarreau06d80a92017-10-19 14:32:15 +02002840 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002841 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002842 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002843
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002844 if (ret == -1) {
2845 lua_pushnil(L);
2846 return 1;
2847 }
2848
2849 luaL_buffinit(L, &b);
2850 luaL_addlstring(&b, blk1, len1);
2851 len = len1;
2852 if (unlikely(ret == 2)) {
2853 luaL_addlstring(&b, blk2, len2);
2854 len += len2;
2855 }
2856 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002857 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858 return 1;
2859}
2860
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002861/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002862__LJMP static int hlua_channel_getline(lua_State *L)
2863{
2864 MAY_LJMP(check_args(L, 1, "getline"));
2865 MAY_LJMP(hlua_checkchannel(L, 1));
2866 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2867}
2868
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002869/* This function takes a string as input, and append it at the
2870 * input side of channel. If the data is too big, but a space
2871 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002872 * yields. If the data is bigger than the buffer, or if the
2873 * channel is closed, it returns -1. Otherwise, it returns the
2874 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002876__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002877{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002878 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879 size_t len;
2880 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2881 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2882 int ret;
2883 int max;
2884
Christopher Faulet3f829a42018-12-13 21:56:45 +01002885 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2886 WILL_LJMP(lua_error(L));
2887
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002888 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002889 * the request buffer if its not required.
2890 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002891 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002892 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002893 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002894 }
2895
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002896 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 if (max > len - l)
2898 max = len - l;
2899
Willy Tarreau06d80a92017-10-19 14:32:15 +02002900 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 if (ret == -2 || ret == -3) {
2902 lua_pushinteger(L, -1);
2903 return 1;
2904 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002905 if (ret == -1) {
2906 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002907 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002908 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002909 l += ret;
2910 lua_pop(L, 1);
2911 lua_pushinteger(L, l);
2912
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002913 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002914 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002915 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002917 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 */
2919 return 1;
2920 }
2921 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002922 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002923 return 1;
2924}
2925
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002926/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2927 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002928 * buffer size is too little for the data.
2929 */
2930__LJMP static int hlua_channel_append(lua_State *L)
2931{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002932 size_t len;
2933
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002935 MAY_LJMP(hlua_checkchannel(L, 1));
2936 MAY_LJMP(luaL_checklstring(L, 2, &len));
2937 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002938 lua_pushinteger(L, 0);
2939
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002940 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941}
2942
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002943/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002945 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002946 * or -1 if the channel is closed or if the buffer size is too
2947 * little for the data.
2948 */
2949__LJMP static int hlua_channel_set(lua_State *L)
2950{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002951 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002952
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002953 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002954 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955 lua_pushinteger(L, 0);
2956
Christopher Faulet3f829a42018-12-13 21:56:45 +01002957 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2958 WILL_LJMP(lua_error(L));
2959
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002960 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002961
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002962 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002963}
2964
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002965/* Append data in the output side of the buffer. This data is immediately
2966 * sent. The function returns the amount of data written. If the buffer
2967 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968 * if the channel is closed.
2969 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002970__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002972 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002973 size_t len;
2974 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2975 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2976 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002977 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002978
Christopher Faulet3f829a42018-12-13 21:56:45 +01002979 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2980 WILL_LJMP(lua_error(L));
2981
Willy Tarreau47860ed2015-03-10 14:07:50 +01002982 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 lua_pushinteger(L, -1);
2984 return 1;
2985 }
2986
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002987 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002988 * the request buffer if its not required.
2989 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002990 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002991 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002992 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002993 }
2994
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002995 /* The written data will be immediately sent, so we can check
2996 * the available space without taking in account the reserve.
2997 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002998 * data, because the buffer will be flushed.
2999 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003000 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003001
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003002 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003003 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003004 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003005 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003006 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003007 return 1;
3008
3009 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 if (max > len - l)
3011 max = len - l;
3012
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003013 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003014 * detects a non contiguous buffer and realign it.
3015 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003016 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003017 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003018
3019 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003020 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003021
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022 /* buffer replace considers that the input part is filled.
3023 * so, I must forward these new data in the output part.
3024 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003025 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026
3027 l += max;
3028 lua_pop(L, 1);
3029 lua_pushinteger(L, l);
3030
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003031 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003032 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003033 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003034 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003035 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003036 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003038
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003039 if (l < len) {
3040 /* If we are waiting for space in the response buffer, we
3041 * must set the flag WAKERESWR. This flag required the task
3042 * wake up if any activity is detected on the response buffer.
3043 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003044 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003045 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003046 else
3047 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003048 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003049 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050
3051 return 1;
3052}
3053
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003054/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003055 * yield the LUA process, and resume it without checking the
3056 * input arguments.
3057 */
3058__LJMP static int hlua_channel_send(lua_State *L)
3059{
3060 MAY_LJMP(check_args(L, 2, "send"));
3061 lua_pushinteger(L, 0);
3062
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003063 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064}
3065
3066/* This function forward and amount of butes. The data pass from
3067 * the input side of the buffer to the output side, and can be
3068 * forwarded. This function never fails.
3069 *
3070 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003071 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003073__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003074{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003075 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076 int len;
3077 int l;
3078 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003079 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003080
3081 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003082
3083 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3084 WILL_LJMP(lua_error(L));
3085
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003086 len = MAY_LJMP(luaL_checkinteger(L, 2));
3087 l = MAY_LJMP(luaL_checkinteger(L, -1));
3088
3089 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003090 if (max > ci_data(chn))
3091 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003092 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093 l += max;
3094
3095 lua_pop(L, 1);
3096 lua_pushinteger(L, l);
3097
3098 /* Check if it miss bytes to forward. */
3099 if (l < len) {
3100 /* The the input channel or the output channel are closed, we
3101 * must return the amount of data forwarded.
3102 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003103 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003104 return 1;
3105
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003106 /* If we are waiting for space data in the response buffer, we
3107 * must set the flag WAKERESWR. This flag required the task
3108 * wake up if any activity is detected on the response buffer.
3109 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003110 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003111 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003112 else
3113 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003114
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003115 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003116 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003117 }
3118
3119 return 1;
3120}
3121
3122/* Just check the input and prepare the stack for the previous
3123 * function "hlua_channel_forward_yield"
3124 */
3125__LJMP static int hlua_channel_forward(lua_State *L)
3126{
3127 MAY_LJMP(check_args(L, 2, "forward"));
3128 MAY_LJMP(hlua_checkchannel(L, 1));
3129 MAY_LJMP(luaL_checkinteger(L, 2));
3130
3131 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003132 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003133}
3134
3135/* Just returns the number of bytes available in the input
3136 * side of the buffer. This function never fails.
3137 */
3138__LJMP static int hlua_channel_get_in_len(lua_State *L)
3139{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003140 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003141
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003142 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003143 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003144 if (IS_HTX_STRM(chn_strm(chn))) {
3145 struct htx *htx = htxbuf(&chn->buf);
3146 lua_pushinteger(L, htx->data - co_data(chn));
3147 }
3148 else
3149 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003150 return 1;
3151}
3152
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003153/* Returns true if the channel is full. */
3154__LJMP static int hlua_channel_is_full(lua_State *L)
3155{
3156 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003157
3158 MAY_LJMP(check_args(L, 1, "is_full"));
3159 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003160 /* ignore the reserve, we are not on a producer side (ie in an
3161 * applet).
3162 */
3163 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003164 return 1;
3165}
3166
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003167/* Returns true if the channel is the response channel. */
3168__LJMP static int hlua_channel_is_resp(lua_State *L)
3169{
3170 struct channel *chn;
3171
3172 MAY_LJMP(check_args(L, 1, "is_resp"));
3173 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3174
3175 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3176 return 1;
3177}
3178
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179/* Just returns the number of bytes available in the output
3180 * side of the buffer. This function never fails.
3181 */
3182__LJMP static int hlua_channel_get_out_len(lua_State *L)
3183{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003184 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003185
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003186 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003187 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003188 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003189 return 1;
3190}
3191
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003192/*
3193 *
3194 *
3195 * Class Fetches
3196 *
3197 *
3198 */
3199
3200/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003201 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003202 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003203__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003204{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003205 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003206}
3207
3208/* This function creates and push in the stack a fetch object according
3209 * with a current TXN.
3210 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003211static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003212{
Willy Tarreau7073c472015-04-06 11:15:40 +02003213 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003214
3215 /* Check stack size. */
3216 if (!lua_checkstack(L, 3))
3217 return 0;
3218
3219 /* Create the object: obj[0] = userdata.
3220 * Note that the base of the Fetches object is the
3221 * transaction object.
3222 */
3223 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003224 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003225 lua_rawseti(L, -2, 0);
3226
Willy Tarreau7073c472015-04-06 11:15:40 +02003227 hsmp->s = txn->s;
3228 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003229 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003230 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003231
3232 /* Pop a class sesison metatable and affect it to the userdata. */
3233 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3234 lua_setmetatable(L, -2);
3235
3236 return 1;
3237}
3238
3239/* This function is an LUA binding. It is called with each sample-fetch.
3240 * It uses closure argument to store the associated sample-fetch. It
3241 * returns only one argument or throws an error. An error is thrown
3242 * only if an error is encountered during the argument parsing. If
3243 * the "sample-fetch" function fails, nil is returned.
3244 */
3245__LJMP static int hlua_run_sample_fetch(lua_State *L)
3246{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003247 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003248 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003249 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003250 int i;
3251 struct sample smp;
3252
3253 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003254 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003255
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003256 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003257 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003258
Thierry FOURNIERca988662015-12-20 18:43:03 +01003259 /* Check execution authorization. */
3260 if (f->use & SMP_USE_HTTP_ANY &&
3261 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3262 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3263 "is not available in Lua services", f->kw);
3264 WILL_LJMP(lua_error(L));
3265 }
3266
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003267 /* Get extra arguments. */
3268 for (i = 0; i < lua_gettop(L) - 1; i++) {
3269 if (i >= ARGM_NBARGS)
3270 break;
3271 hlua_lua2arg(L, i + 2, &args[i]);
3272 }
3273 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003274 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003275
3276 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003277 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003278
3279 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003280 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003281 lua_pushfstring(L, "error in arguments");
3282 WILL_LJMP(lua_error(L));
3283 }
3284
3285 /* Initialise the sample. */
3286 memset(&smp, 0, sizeof(smp));
3287
3288 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003289 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003290 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003291 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003292 lua_pushstring(L, "");
3293 else
3294 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003295 return 1;
3296 }
3297
3298 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003299 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003300 hlua_smp2lua_str(L, &smp);
3301 else
3302 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003303 return 1;
3304}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003305
3306/*
3307 *
3308 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003309 * Class Converters
3310 *
3311 *
3312 */
3313
3314/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003315 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003316 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003317__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003318{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003319 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003320}
3321
3322/* This function creates and push in the stack a Converters object
3323 * according with a current TXN.
3324 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003325static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003326{
Willy Tarreau7073c472015-04-06 11:15:40 +02003327 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003328
3329 /* Check stack size. */
3330 if (!lua_checkstack(L, 3))
3331 return 0;
3332
3333 /* Create the object: obj[0] = userdata.
3334 * Note that the base of the Converters object is the
3335 * same than the TXN object.
3336 */
3337 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003338 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003339 lua_rawseti(L, -2, 0);
3340
Willy Tarreau7073c472015-04-06 11:15:40 +02003341 hsmp->s = txn->s;
3342 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003343 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003344 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003345
Willy Tarreau87b09662015-04-03 00:22:06 +02003346 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003347 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3348 lua_setmetatable(L, -2);
3349
3350 return 1;
3351}
3352
3353/* This function is an LUA binding. It is called with each converter.
3354 * It uses closure argument to store the associated converter. It
3355 * returns only one argument or throws an error. An error is thrown
3356 * only if an error is encountered during the argument parsing. If
3357 * the converter function function fails, nil is returned.
3358 */
3359__LJMP static int hlua_run_sample_conv(lua_State *L)
3360{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003361 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003362 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003363 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003364 int i;
3365 struct sample smp;
3366
3367 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003368 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003369
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003370 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003371 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003372
3373 /* Get extra arguments. */
3374 for (i = 0; i < lua_gettop(L) - 2; i++) {
3375 if (i >= ARGM_NBARGS)
3376 break;
3377 hlua_lua2arg(L, i + 3, &args[i]);
3378 }
3379 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003380 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003381
3382 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003383 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003384
3385 /* Run the special args checker. */
3386 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3387 hlua_pusherror(L, "error in arguments");
3388 WILL_LJMP(lua_error(L));
3389 }
3390
3391 /* Initialise the sample. */
3392 if (!hlua_lua2smp(L, 2, &smp)) {
3393 hlua_pusherror(L, "error in the input argument");
3394 WILL_LJMP(lua_error(L));
3395 }
3396
Willy Tarreau1777ea62016-03-10 16:15:46 +01003397 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3398
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003399 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003400 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003401 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003402 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003403 WILL_LJMP(lua_error(L));
3404 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003405 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3406 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003407 hlua_pusherror(L, "error during the input argument casting");
3408 WILL_LJMP(lua_error(L));
3409 }
3410
3411 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003412 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003413 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003414 lua_pushstring(L, "");
3415 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003416 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003417 return 1;
3418 }
3419
3420 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003421 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003422 hlua_smp2lua_str(L, &smp);
3423 else
3424 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003425 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003426}
3427
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003428/*
3429 *
3430 *
3431 * Class AppletTCP
3432 *
3433 *
3434 */
3435
3436/* Returns a struct hlua_txn if the stack entry "ud" is
3437 * a class stream, otherwise it throws an error.
3438 */
3439__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3440{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003441 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003442}
3443
3444/* This function creates and push in the stack an Applet object
3445 * according with a current TXN.
3446 */
3447static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3448{
3449 struct hlua_appctx *appctx;
3450 struct stream_interface *si = ctx->owner;
3451 struct stream *s = si_strm(si);
3452 struct proxy *p = s->be;
3453
3454 /* Check stack size. */
3455 if (!lua_checkstack(L, 3))
3456 return 0;
3457
3458 /* Create the object: obj[0] = userdata.
3459 * Note that the base of the Converters object is the
3460 * same than the TXN object.
3461 */
3462 lua_newtable(L);
3463 appctx = lua_newuserdata(L, sizeof(*appctx));
3464 lua_rawseti(L, -2, 0);
3465 appctx->appctx = ctx;
3466 appctx->htxn.s = s;
3467 appctx->htxn.p = p;
3468
3469 /* Create the "f" field that contains a list of fetches. */
3470 lua_pushstring(L, "f");
3471 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3472 return 0;
3473 lua_settable(L, -3);
3474
3475 /* Create the "sf" field that contains a list of stringsafe fetches. */
3476 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003477 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003478 return 0;
3479 lua_settable(L, -3);
3480
3481 /* Create the "c" field that contains a list of converters. */
3482 lua_pushstring(L, "c");
3483 if (!hlua_converters_new(L, &appctx->htxn, 0))
3484 return 0;
3485 lua_settable(L, -3);
3486
3487 /* Create the "sc" field that contains a list of stringsafe converters. */
3488 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003489 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003490 return 0;
3491 lua_settable(L, -3);
3492
3493 /* Pop a class stream metatable and affect it to the table. */
3494 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3495 lua_setmetatable(L, -2);
3496
3497 return 1;
3498}
3499
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003500__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3501{
3502 struct hlua_appctx *appctx;
3503 struct stream *s;
3504 const char *name;
3505 size_t len;
3506 struct sample smp;
3507
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003508 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3509 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003510
3511 /* It is useles to retrieve the stream, but this function
3512 * runs only in a stream context.
3513 */
3514 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3515 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3516 s = appctx->htxn.s;
3517
3518 /* Converts the third argument in a sample. */
3519 hlua_lua2smp(L, 3, &smp);
3520
3521 /* Store the sample in a variable. */
3522 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003523
3524 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3525 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3526 else
3527 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3528
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003529 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003530}
3531
3532__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3533{
3534 struct hlua_appctx *appctx;
3535 struct stream *s;
3536 const char *name;
3537 size_t len;
3538 struct sample smp;
3539
3540 MAY_LJMP(check_args(L, 2, "unset_var"));
3541
3542 /* It is useles to retrieve the stream, but this function
3543 * runs only in a stream context.
3544 */
3545 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3546 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3547 s = appctx->htxn.s;
3548
3549 /* Unset the variable. */
3550 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003551 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3552 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003553}
3554
3555__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3556{
3557 struct hlua_appctx *appctx;
3558 struct stream *s;
3559 const char *name;
3560 size_t len;
3561 struct sample smp;
3562
3563 MAY_LJMP(check_args(L, 2, "get_var"));
3564
3565 /* It is useles to retrieve the stream, but this function
3566 * runs only in a stream context.
3567 */
3568 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3569 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3570 s = appctx->htxn.s;
3571
3572 smp_set_owner(&smp, s->be, s->sess, s, 0);
3573 if (!vars_get_by_name(name, len, &smp)) {
3574 lua_pushnil(L);
3575 return 1;
3576 }
3577
3578 return hlua_smp2lua(L, &smp);
3579}
3580
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003581__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3582{
3583 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3584 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003585 struct hlua *hlua;
3586
3587 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003588 if (!s->hlua)
3589 return 0;
3590 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003591
3592 MAY_LJMP(check_args(L, 2, "set_priv"));
3593
3594 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003595 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003596
3597 /* Get and store new value. */
3598 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3599 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3600
3601 return 0;
3602}
3603
3604__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3605{
3606 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3607 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003608 struct hlua *hlua;
3609
3610 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003611 if (!s->hlua) {
3612 lua_pushnil(L);
3613 return 1;
3614 }
3615 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003616
3617 /* Push configuration index in the stack. */
3618 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3619
3620 return 1;
3621}
3622
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003623/* If expected data not yet available, it returns a yield. This function
3624 * consumes the data in the buffer. It returns a string containing the
3625 * data. This string can be empty.
3626 */
3627__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3628{
3629 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3630 struct stream_interface *si = appctx->appctx->owner;
3631 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003632 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003633 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003634 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003635 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003636
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003637 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003638 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003639
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003640 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003641 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003642 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003643 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003644 }
3645
3646 /* End of data: commit the total strings and return. */
3647 if (ret < 0) {
3648 luaL_pushresult(&appctx->b);
3649 return 1;
3650 }
3651
3652 /* Ensure that the block 2 length is usable. */
3653 if (ret == 1)
3654 len2 = 0;
3655
3656 /* dont check the max length read and dont check. */
3657 luaL_addlstring(&appctx->b, blk1, len1);
3658 luaL_addlstring(&appctx->b, blk2, len2);
3659
3660 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003661 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003662 luaL_pushresult(&appctx->b);
3663 return 1;
3664}
3665
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003666/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003667__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3668{
3669 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3670
3671 /* Initialise the string catenation. */
3672 luaL_buffinit(L, &appctx->b);
3673
3674 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3675}
3676
3677/* If expected data not yet available, it returns a yield. This function
3678 * consumes the data in the buffer. It returns a string containing the
3679 * data. This string can be empty.
3680 */
3681__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3682{
3683 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3684 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003685 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003686 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003687 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003688 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003689 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003690 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003691
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003692 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003693 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003694
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003695 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003696 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003697 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003698 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003699 }
3700
3701 /* End of data: commit the total strings and return. */
3702 if (ret < 0) {
3703 luaL_pushresult(&appctx->b);
3704 return 1;
3705 }
3706
3707 /* Ensure that the block 2 length is usable. */
3708 if (ret == 1)
3709 len2 = 0;
3710
3711 if (len == -1) {
3712
3713 /* If len == -1, catenate all the data avalaile and
3714 * yield because we want to get all the data until
3715 * the end of data stream.
3716 */
3717 luaL_addlstring(&appctx->b, blk1, len1);
3718 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003719 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003720 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003721 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003722
3723 } else {
3724
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003725 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003726 if (len1 > len)
3727 len1 = len;
3728 luaL_addlstring(&appctx->b, blk1, len1);
3729 len -= len1;
3730
3731 /* Copy the second block. */
3732 if (len2 > len)
3733 len2 = len;
3734 luaL_addlstring(&appctx->b, blk2, len2);
3735 len -= len2;
3736
3737 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003738 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003739
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003740 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003741 if (len > 0) {
3742 lua_pushinteger(L, len);
3743 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003744 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003745 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003746 }
3747
3748 /* return the result. */
3749 luaL_pushresult(&appctx->b);
3750 return 1;
3751 }
3752
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003753 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003754 hlua_pusherror(L, "Lua: internal error");
3755 WILL_LJMP(lua_error(L));
3756 return 0;
3757}
3758
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003759/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003760__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3761{
3762 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3763 int len = -1;
3764
3765 if (lua_gettop(L) > 2)
3766 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3767 if (lua_gettop(L) >= 2) {
3768 len = MAY_LJMP(luaL_checkinteger(L, 2));
3769 lua_pop(L, 1);
3770 }
3771
3772 /* Confirm or set the required length */
3773 lua_pushinteger(L, len);
3774
3775 /* Initialise the string catenation. */
3776 luaL_buffinit(L, &appctx->b);
3777
3778 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3779}
3780
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003781/* Append data in the output side of the buffer. This data is immediately
3782 * sent. The function returns the amount of data written. If the buffer
3783 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003784 * if the channel is closed.
3785 */
3786__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3787{
3788 size_t len;
3789 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3790 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3791 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3792 struct stream_interface *si = appctx->appctx->owner;
3793 struct channel *chn = si_ic(si);
3794 int max;
3795
3796 /* Get the max amount of data which can write as input in the channel. */
3797 max = channel_recv_max(chn);
3798 if (max > (len - l))
3799 max = len - l;
3800
3801 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003802 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003803
3804 /* update counters. */
3805 l += max;
3806 lua_pop(L, 1);
3807 lua_pushinteger(L, l);
3808
3809 /* If some data is not send, declares the situation to the
3810 * applet, and returns a yield.
3811 */
3812 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003813 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003814 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003815 }
3816
3817 return 1;
3818}
3819
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003820/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003821 * yield the LUA process, and resume it without checking the
3822 * input arguments.
3823 */
3824__LJMP static int hlua_applet_tcp_send(lua_State *L)
3825{
3826 MAY_LJMP(check_args(L, 2, "send"));
3827 lua_pushinteger(L, 0);
3828
3829 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3830}
3831
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003832/*
3833 *
3834 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003835 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003836 *
3837 *
3838 */
3839
3840/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003841 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003842 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003843__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003844{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003845 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003846}
3847
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003848/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003849 * according with a current TXN.
3850 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003851static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003852{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003853 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003854 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003855 struct stream_interface *si = ctx->owner;
3856 struct stream *s = si_strm(si);
3857 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003858 struct htx *htx;
3859 struct htx_blk *blk;
3860 struct htx_sl *sl;
3861 struct ist path;
3862 unsigned long long len = 0;
3863 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003864
3865 /* Check stack size. */
3866 if (!lua_checkstack(L, 3))
3867 return 0;
3868
3869 /* Create the object: obj[0] = userdata.
3870 * Note that the base of the Converters object is the
3871 * same than the TXN object.
3872 */
3873 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003874 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003875 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 appctx->appctx = ctx;
3877 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003878 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 appctx->htxn.s = s;
3880 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003881
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003882 /* Create the "f" field that contains a list of fetches. */
3883 lua_pushstring(L, "f");
3884 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3885 return 0;
3886 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003887
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003888 /* Create the "sf" field that contains a list of stringsafe fetches. */
3889 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003890 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003891 return 0;
3892 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003893
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003894 /* Create the "c" field that contains a list of converters. */
3895 lua_pushstring(L, "c");
3896 if (!hlua_converters_new(L, &appctx->htxn, 0))
3897 return 0;
3898 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003899
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003900 /* Create the "sc" field that contains a list of stringsafe converters. */
3901 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003902 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003903 return 0;
3904 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003905
Christopher Fauleta2097962019-07-15 16:25:33 +02003906 htx = htxbuf(&s->req.buf);
3907 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003908 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003909 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003910
Christopher Fauleta2097962019-07-15 16:25:33 +02003911 /* Stores the request method. */
3912 lua_pushstring(L, "method");
3913 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3914 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003915
Christopher Fauleta2097962019-07-15 16:25:33 +02003916 /* Stores the http version. */
3917 lua_pushstring(L, "version");
3918 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3919 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003920
Christopher Fauleta2097962019-07-15 16:25:33 +02003921 /* creates an array of headers. hlua_http_get_headers() crates and push
3922 * the array on the top of the stack.
3923 */
3924 lua_pushstring(L, "headers");
3925 htxn.s = s;
3926 htxn.p = px;
3927 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003928 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003929 return 0;
3930 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003931
Christopher Fauleta2097962019-07-15 16:25:33 +02003932 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003933 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003934 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003935
Christopher Fauleta2097962019-07-15 16:25:33 +02003936 p = path.ptr;
3937 end = path.ptr + path.len;
3938 q = p;
3939 while (q < end && *q != '?')
3940 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003941
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003942 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003943 lua_pushstring(L, "path");
3944 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003945 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003946
Christopher Fauleta2097962019-07-15 16:25:33 +02003947 /* Stores the query string. */
3948 lua_pushstring(L, "qs");
3949 if (*q == '?')
3950 q++;
3951 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003952 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003953 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003954
Christopher Fauleta2097962019-07-15 16:25:33 +02003955 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3956 struct htx_blk *blk = htx_get_blk(htx, pos);
3957 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003958
Christopher Fauleta2097962019-07-15 16:25:33 +02003959 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3960 break;
3961 if (type == HTX_BLK_DATA)
3962 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003963 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003964 if (htx->extra != ULLONG_MAX)
3965 len += htx->extra;
3966
3967 /* Stores the request path. */
3968 lua_pushstring(L, "length");
3969 lua_pushinteger(L, len);
3970 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003971
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003972 /* Create an empty array of HTTP request headers. */
3973 lua_pushstring(L, "response");
3974 lua_newtable(L);
3975 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003976
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003977 /* Pop a class stream metatable and affect it to the table. */
3978 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3979 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003980
3981 return 1;
3982}
3983
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003984__LJMP static int hlua_applet_http_set_var(lua_State *L)
3985{
3986 struct hlua_appctx *appctx;
3987 struct stream *s;
3988 const char *name;
3989 size_t len;
3990 struct sample smp;
3991
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003992 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3993 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003994
3995 /* It is useles to retrieve the stream, but this function
3996 * runs only in a stream context.
3997 */
3998 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3999 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4000 s = appctx->htxn.s;
4001
4002 /* Converts the third argument in a sample. */
4003 hlua_lua2smp(L, 3, &smp);
4004
4005 /* Store the sample in a variable. */
4006 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004007
4008 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4009 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4010 else
4011 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4012
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004013 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004014}
4015
4016__LJMP static int hlua_applet_http_unset_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, "unset_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 /* Unset the variable. */
4034 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004035 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4036 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004037}
4038
4039__LJMP static int hlua_applet_http_get_var(lua_State *L)
4040{
4041 struct hlua_appctx *appctx;
4042 struct stream *s;
4043 const char *name;
4044 size_t len;
4045 struct sample smp;
4046
4047 MAY_LJMP(check_args(L, 2, "get_var"));
4048
4049 /* It is useles to retrieve the stream, but this function
4050 * runs only in a stream context.
4051 */
4052 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4053 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4054 s = appctx->htxn.s;
4055
4056 smp_set_owner(&smp, s->be, s->sess, s, 0);
4057 if (!vars_get_by_name(name, len, &smp)) {
4058 lua_pushnil(L);
4059 return 1;
4060 }
4061
4062 return hlua_smp2lua(L, &smp);
4063}
4064
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004065__LJMP static int hlua_applet_http_set_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 return 0;
4074 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004075
4076 MAY_LJMP(check_args(L, 2, "set_priv"));
4077
4078 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004079 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004080
4081 /* Get and store new value. */
4082 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4083 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4084
4085 return 0;
4086}
4087
4088__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4089{
4090 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4091 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004092 struct hlua *hlua;
4093
4094 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004095 if (!s->hlua) {
4096 lua_pushnil(L);
4097 return 1;
4098 }
4099 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004100
4101 /* Push configuration index in the stack. */
4102 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4103
4104 return 1;
4105}
4106
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004107/* If expected data not yet available, it returns a yield. This function
4108 * consumes the data in the buffer. It returns a string containing the
4109 * data. This string can be empty.
4110 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004111__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004112{
4113 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4114 struct stream_interface *si = appctx->appctx->owner;
4115 struct channel *req = si_oc(si);
4116 struct htx *htx;
4117 struct htx_blk *blk;
4118 size_t count;
4119 int stop = 0;
4120
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004121 htx = htx_from_buf(&req->buf);
4122 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004123 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004124
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004125 while (count && !stop && blk) {
4126 enum htx_blk_type type = htx_get_blk_type(blk);
4127 uint32_t sz = htx_get_blksz(blk);
4128 struct ist v;
4129 uint32_t vlen;
4130 char *nl;
4131
Christopher Faulete461e342018-12-18 16:43:35 +01004132 if (type == HTX_BLK_EOM) {
4133 stop = 1;
4134 break;
4135 }
4136
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004137 vlen = sz;
4138 if (vlen > count) {
4139 if (type != HTX_BLK_DATA)
4140 break;
4141 vlen = count;
4142 }
4143
4144 switch (type) {
4145 case HTX_BLK_UNUSED:
4146 break;
4147
4148 case HTX_BLK_DATA:
4149 v = htx_get_blk_value(htx, blk);
4150 v.len = vlen;
4151 nl = istchr(v, '\n');
4152 if (nl != NULL) {
4153 stop = 1;
4154 vlen = nl - v.ptr + 1;
4155 }
4156 luaL_addlstring(&appctx->b, v.ptr, vlen);
4157 break;
4158
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004159 case HTX_BLK_TLR:
4160 case HTX_BLK_EOM:
4161 stop = 1;
4162 break;
4163
4164 default:
4165 break;
4166 }
4167
4168 co_set_data(req, co_data(req) - vlen);
4169 count -= vlen;
4170 if (sz == vlen)
4171 blk = htx_remove_blk(htx, blk);
4172 else {
4173 htx_cut_data_blk(htx, blk, vlen);
4174 break;
4175 }
4176 }
4177
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004178 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004179 if (!stop) {
4180 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004181 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004182 }
4183
4184 /* return the result. */
4185 luaL_pushresult(&appctx->b);
4186 return 1;
4187}
4188
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004189
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004190/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004191__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004192{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004193 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004194
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004195 /* Initialise the string catenation. */
4196 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004197
Christopher Fauleta2097962019-07-15 16:25:33 +02004198 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004199}
4200
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004201/* If expected data not yet available, it returns a yield. This function
4202 * consumes the data in the buffer. It returns a string containing the
4203 * data. This string can be empty.
4204 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004205__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004206{
4207 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4208 struct stream_interface *si = appctx->appctx->owner;
4209 struct channel *req = si_oc(si);
4210 struct htx *htx;
4211 struct htx_blk *blk;
4212 size_t count;
4213 int len;
4214
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004215 htx = htx_from_buf(&req->buf);
4216 len = MAY_LJMP(luaL_checkinteger(L, 2));
4217 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004218 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004219 while (count && len && blk) {
4220 enum htx_blk_type type = htx_get_blk_type(blk);
4221 uint32_t sz = htx_get_blksz(blk);
4222 struct ist v;
4223 uint32_t vlen;
4224
Christopher Faulete461e342018-12-18 16:43:35 +01004225 if (type == HTX_BLK_EOM) {
4226 len = 0;
4227 break;
4228 }
4229
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004230 vlen = sz;
4231 if (len > 0 && vlen > len)
4232 vlen = len;
4233 if (vlen > count) {
4234 if (type != HTX_BLK_DATA)
4235 break;
4236 vlen = count;
4237 }
4238
4239 switch (type) {
4240 case HTX_BLK_UNUSED:
4241 break;
4242
4243 case HTX_BLK_DATA:
4244 v = htx_get_blk_value(htx, blk);
4245 luaL_addlstring(&appctx->b, v.ptr, vlen);
4246 break;
4247
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004248 case HTX_BLK_TLR:
4249 case HTX_BLK_EOM:
4250 len = 0;
4251 break;
4252
4253 default:
4254 break;
4255 }
4256
4257 co_set_data(req, co_data(req) - vlen);
4258 count -= vlen;
4259 if (len > 0)
4260 len -= vlen;
4261 if (sz == vlen)
4262 blk = htx_remove_blk(htx, blk);
4263 else {
4264 htx_cut_data_blk(htx, blk, vlen);
4265 break;
4266 }
4267 }
4268
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004269 htx_to_buf(htx, &req->buf);
4270
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004271 /* If we are no other data available, yield waiting for new data. */
4272 if (len) {
4273 if (len > 0) {
4274 lua_pushinteger(L, len);
4275 lua_replace(L, 2);
4276 }
4277 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004278 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004279 }
4280
4281 /* return the result. */
4282 luaL_pushresult(&appctx->b);
4283 return 1;
4284}
4285
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004286/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004287__LJMP static int hlua_applet_http_recv(lua_State *L)
4288{
4289 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4290 int len = -1;
4291
4292 /* Check arguments. */
4293 if (lua_gettop(L) > 2)
4294 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4295 if (lua_gettop(L) >= 2) {
4296 len = MAY_LJMP(luaL_checkinteger(L, 2));
4297 lua_pop(L, 1);
4298 }
4299
Christopher Fauleta2097962019-07-15 16:25:33 +02004300 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004301
Christopher Fauleta2097962019-07-15 16:25:33 +02004302 /* Initialise the string catenation. */
4303 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004304
Christopher Fauleta2097962019-07-15 16:25:33 +02004305 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004306}
4307
4308/* Append data in the output side of the buffer. This data is immediately
4309 * sent. The function returns the amount of data written. If the buffer
4310 * cannot contain the data, the function yields. The function returns -1
4311 * if the channel is closed.
4312 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004313__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004314{
4315 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4316 struct stream_interface *si = appctx->appctx->owner;
4317 struct channel *res = si_ic(si);
4318 struct htx *htx = htx_from_buf(&res->buf);
4319 const char *data;
4320 size_t len;
4321 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4322 int max;
4323
Christopher Faulet9060fc02019-07-03 11:39:30 +02004324 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004325 if (!max)
4326 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004327
4328 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4329
4330 /* Get the max amount of data which can write as input in the channel. */
4331 if (max > (len - l))
4332 max = len - l;
4333
4334 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004335 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004336 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004337
4338 /* update counters. */
4339 l += max;
4340 lua_pop(L, 1);
4341 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004342
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004343 /* If some data is not send, declares the situation to the
4344 * applet, and returns a yield.
4345 */
4346 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004347 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004348 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004349 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004350 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004351 }
4352
Christopher Fauleta2097962019-07-15 16:25:33 +02004353 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004354 return 1;
4355}
4356
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004357/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004358 * yield the LUA process, and resume it without checking the
4359 * input arguments.
4360 */
4361__LJMP static int hlua_applet_http_send(lua_State *L)
4362{
4363 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004364
4365 /* We want to send some data. Headers must be sent. */
4366 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4367 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4368 WILL_LJMP(lua_error(L));
4369 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004370
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004371 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004372 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004373
Christopher Fauleta2097962019-07-15 16:25:33 +02004374 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004375}
4376
4377__LJMP static int hlua_applet_http_addheader(lua_State *L)
4378{
4379 const char *name;
4380 int ret;
4381
4382 MAY_LJMP(hlua_checkapplet_http(L, 1));
4383 name = MAY_LJMP(luaL_checkstring(L, 2));
4384 MAY_LJMP(luaL_checkstring(L, 3));
4385
4386 /* Push in the stack the "response" entry. */
4387 ret = lua_getfield(L, 1, "response");
4388 if (ret != LUA_TTABLE) {
4389 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4390 "is expected as an array. %s found", lua_typename(L, ret));
4391 WILL_LJMP(lua_error(L));
4392 }
4393
4394 /* check if the header is already registered if it is not
4395 * the case, register it.
4396 */
4397 ret = lua_getfield(L, -1, name);
4398 if (ret == LUA_TNIL) {
4399
4400 /* Entry not found. */
4401 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4402
4403 /* Insert the new header name in the array in the top of the stack.
4404 * It left the new array in the top of the stack.
4405 */
4406 lua_newtable(L);
4407 lua_pushvalue(L, 2);
4408 lua_pushvalue(L, -2);
4409 lua_settable(L, -4);
4410
4411 } else if (ret != LUA_TTABLE) {
4412
4413 /* corruption error. */
4414 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4415 "is expected as an array. %s found", name, lua_typename(L, ret));
4416 WILL_LJMP(lua_error(L));
4417 }
4418
4419 /* Now the top od thestack is an array of values. We push
4420 * the header value as new entry.
4421 */
4422 lua_pushvalue(L, 3);
4423 ret = lua_rawlen(L, -2);
4424 lua_rawseti(L, -2, ret + 1);
4425 lua_pushboolean(L, 1);
4426 return 1;
4427}
4428
4429__LJMP static int hlua_applet_http_status(lua_State *L)
4430{
4431 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4432 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004433 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004434
4435 if (status < 100 || status > 599) {
4436 lua_pushboolean(L, 0);
4437 return 1;
4438 }
4439
4440 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004441 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004442 lua_pushboolean(L, 1);
4443 return 1;
4444}
4445
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004446
Christopher Fauleta2097962019-07-15 16:25:33 +02004447__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004448{
4449 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4450 struct stream_interface *si = appctx->appctx->owner;
4451 struct channel *res = si_ic(si);
4452 struct htx *htx;
4453 struct htx_sl *sl;
4454 struct h1m h1m;
4455 const char *status, *reason;
4456 const char *name, *value;
4457 size_t nlen, vlen;
4458 unsigned int flags;
4459
4460 /* Send the message at once. */
4461 htx = htx_from_buf(&res->buf);
4462 h1m_init_res(&h1m);
4463
4464 /* Use the same http version than the request. */
4465 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4466 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4467 if (reason == NULL)
4468 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4469 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4470 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4471 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4472 }
4473 else {
4474 flags = HTX_SL_F_IS_RESP;
4475 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4476 }
4477 if (!sl) {
4478 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4479 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4480 WILL_LJMP(lua_error(L));
4481 }
4482 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4483
4484 /* Get the array associated to the field "response" in the object AppletHTTP. */
4485 lua_pushvalue(L, 0);
4486 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4487 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4488 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4489 WILL_LJMP(lua_error(L));
4490 }
4491
4492 /* Browse the list of headers. */
4493 lua_pushnil(L);
4494 while(lua_next(L, -2) != 0) {
4495 /* We expect a string as -2. */
4496 if (lua_type(L, -2) != LUA_TSTRING) {
4497 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4498 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4499 lua_typename(L, lua_type(L, -2)));
4500 WILL_LJMP(lua_error(L));
4501 }
4502 name = lua_tolstring(L, -2, &nlen);
4503
4504 /* We expect an array as -1. */
4505 if (lua_type(L, -1) != LUA_TTABLE) {
4506 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4507 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4508 name,
4509 lua_typename(L, lua_type(L, -1)));
4510 WILL_LJMP(lua_error(L));
4511 }
4512
4513 /* Browse the table who is on the top of the stack. */
4514 lua_pushnil(L);
4515 while(lua_next(L, -2) != 0) {
4516 int id;
4517
4518 /* We expect a number as -2. */
4519 if (lua_type(L, -2) != LUA_TNUMBER) {
4520 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4521 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4522 name,
4523 lua_typename(L, lua_type(L, -2)));
4524 WILL_LJMP(lua_error(L));
4525 }
4526 id = lua_tointeger(L, -2);
4527
4528 /* We expect a string as -2. */
4529 if (lua_type(L, -1) != LUA_TSTRING) {
4530 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4531 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4532 name, id,
4533 lua_typename(L, lua_type(L, -1)));
4534 WILL_LJMP(lua_error(L));
4535 }
4536 value = lua_tolstring(L, -1, &vlen);
4537
4538 /* Simple Protocol checks. */
4539 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004540 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004541 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4542 struct ist v = ist2(value, vlen);
4543 int ret;
4544
4545 ret = h1_parse_cont_len_header(&h1m, &v);
4546 if (ret < 0) {
4547 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4548 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4549 name);
4550 WILL_LJMP(lua_error(L));
4551 }
4552 else if (ret == 0)
4553 goto next; /* Skip it */
4554 }
4555
4556 /* Add a new header */
4557 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4558 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4559 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4560 name);
4561 WILL_LJMP(lua_error(L));
4562 }
4563 next:
4564 /* Remove the array from the stack, and get next element with a remaining string. */
4565 lua_pop(L, 1);
4566 }
4567
4568 /* Remove the array from the stack, and get next element with a remaining string. */
4569 lua_pop(L, 1);
4570 }
4571
4572 if (h1m.flags & H1_MF_CHNK)
4573 h1m.flags &= ~H1_MF_CLEN;
4574 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4575 h1m.flags |= H1_MF_XFER_LEN;
4576
4577 /* Uset HTX start-line flags */
4578 if (h1m.flags & H1_MF_XFER_ENC)
4579 flags |= HTX_SL_F_XFER_ENC;
4580 if (h1m.flags & H1_MF_XFER_LEN) {
4581 flags |= HTX_SL_F_XFER_LEN;
4582 if (h1m.flags & H1_MF_CHNK)
4583 flags |= HTX_SL_F_CHNK;
4584 else if (h1m.flags & H1_MF_CLEN)
4585 flags |= HTX_SL_F_CLEN;
4586 if (h1m.body_len == 0)
4587 flags |= HTX_SL_F_BODYLESS;
4588 }
4589 sl->flags |= flags;
4590
4591 /* If we dont have a content-length set, and the HTTP version is 1.1
4592 * and the status code implies the presence of a message body, we must
4593 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004594 * for the keepalive compliance. If the applet announces a transfer-encoding
4595 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004596 */
4597 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4598 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4599 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4600 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4601 /* Add a new header */
4602 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4603 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4604 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4605 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4606 WILL_LJMP(lua_error(L));
4607 }
4608 }
4609
4610 /* Finalize headers. */
4611 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4612 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4613 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4614 WILL_LJMP(lua_error(L));
4615 }
4616
4617 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4618 b_reset(&res->buf);
4619 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4620 WILL_LJMP(lua_error(L));
4621 }
4622
4623 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004624 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004625
4626 /* Headers sent, set the flag. */
4627 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4628 return 0;
4629
4630}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004631/* We will build the status line and the headers of the HTTP response.
4632 * We will try send at once if its not possible, we give back the hand
4633 * waiting for more room.
4634 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004635__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004636{
4637 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4638 struct stream_interface *si = appctx->appctx->owner;
4639 struct channel *res = si_ic(si);
4640
4641 if (co_data(res)) {
4642 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004643 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004644 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004645 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004646}
4647
4648
Christopher Fauleta2097962019-07-15 16:25:33 +02004649__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004650{
Christopher Fauleta2097962019-07-15 16:25:33 +02004651 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004652}
4653
Christopher Fauleta2097962019-07-15 16:25:33 +02004654/*
4655 *
4656 *
4657 * Class HTTP
4658 *
4659 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004660 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004661
4662/* Returns a struct hlua_txn if the stack entry "ud" is
4663 * a class stream, otherwise it throws an error.
4664 */
4665__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004666{
Christopher Fauleta2097962019-07-15 16:25:33 +02004667 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4668}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004669
Christopher Fauleta2097962019-07-15 16:25:33 +02004670/* This function creates and push in the stack a HTTP object
4671 * according with a current TXN.
4672 */
4673static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4674{
4675 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004676
Christopher Fauleta2097962019-07-15 16:25:33 +02004677 /* Check stack size. */
4678 if (!lua_checkstack(L, 3))
4679 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004680
Christopher Fauleta2097962019-07-15 16:25:33 +02004681 /* Create the object: obj[0] = userdata.
4682 * Note that the base of the Converters object is the
4683 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004684 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004685 lua_newtable(L);
4686 htxn = lua_newuserdata(L, sizeof(*htxn));
4687 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004688
4689 htxn->s = txn->s;
4690 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004691 htxn->dir = txn->dir;
4692 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004693
4694 /* Pop a class stream metatable and affect it to the table. */
4695 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4696 lua_setmetatable(L, -2);
4697
4698 return 1;
4699}
4700
4701/* This function creates ans returns an array of HTTP headers.
4702 * This function does not fails. It is used as wrapper with the
4703 * 2 following functions.
4704 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004705__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004706{
Christopher Fauleta2097962019-07-15 16:25:33 +02004707 struct htx *htx;
4708 int32_t pos;
4709
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004710 /* Create the table. */
4711 lua_newtable(L);
4712
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004713
Christopher Fauleta2097962019-07-15 16:25:33 +02004714 htx = htxbuf(&msg->chn->buf);
4715 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4716 struct htx_blk *blk = htx_get_blk(htx, pos);
4717 enum htx_blk_type type = htx_get_blk_type(blk);
4718 struct ist n, v;
4719 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004720
Christopher Fauleta2097962019-07-15 16:25:33 +02004721 if (type == HTX_BLK_HDR) {
4722 n = htx_get_blk_name(htx,blk);
4723 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004724 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004725 else if (type == HTX_BLK_EOH)
4726 break;
4727 else
4728 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004729
Christopher Fauleta2097962019-07-15 16:25:33 +02004730 /* Check for existing entry:
4731 * assume that the table is on the top of the stack, and
4732 * push the key in the stack, the function lua_gettable()
4733 * perform the lookup.
4734 */
4735 lua_pushlstring(L, n.ptr, n.len);
4736 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004737
Christopher Fauleta2097962019-07-15 16:25:33 +02004738 switch (lua_type(L, -1)) {
4739 case LUA_TNIL:
4740 /* Table not found, create it. */
4741 lua_pop(L, 1); /* remove the nil value. */
4742 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4743 lua_newtable(L); /* create and push empty table. */
4744 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4745 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4746 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004747 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004748
Christopher Fauleta2097962019-07-15 16:25:33 +02004749 case LUA_TTABLE:
4750 /* Entry found: push the value in the table. */
4751 len = lua_rawlen(L, -1);
4752 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4753 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4754 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4755 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004756
Christopher Fauleta2097962019-07-15 16:25:33 +02004757 default:
4758 /* Other cases are errors. */
4759 hlua_pusherror(L, "internal error during the parsing of headers.");
4760 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004761 }
4762 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004763 return 1;
4764}
4765
4766__LJMP static int hlua_http_req_get_headers(lua_State *L)
4767{
4768 struct hlua_txn *htxn;
4769
4770 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4771 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4772
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004773 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004774 WILL_LJMP(lua_error(L));
4775
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004776 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004777}
4778
4779__LJMP static int hlua_http_res_get_headers(lua_State *L)
4780{
4781 struct hlua_txn *htxn;
4782
4783 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4784 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4785
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004786 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004787 WILL_LJMP(lua_error(L));
4788
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004789 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004790}
4791
4792/* This function replace full header, or just a value in
4793 * the request or in the response. It is a wrapper fir the
4794 * 4 following functions.
4795 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004796__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004797{
4798 size_t name_len;
4799 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4800 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4801 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004802 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004803 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004804
Dragan Dosen26743032019-04-30 15:54:36 +02004805 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004806 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4807
Christopher Fauleta2097962019-07-15 16:25:33 +02004808 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004809 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004810 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004811 return 0;
4812}
4813
4814__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4815{
4816 struct hlua_txn *htxn;
4817
4818 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4819 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4820
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004821 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004822 WILL_LJMP(lua_error(L));
4823
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004824 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004825}
4826
4827__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4828{
4829 struct hlua_txn *htxn;
4830
4831 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4832 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4833
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004834 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004835 WILL_LJMP(lua_error(L));
4836
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004837 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004838}
4839
4840__LJMP static int hlua_http_req_rep_val(lua_State *L)
4841{
4842 struct hlua_txn *htxn;
4843
4844 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4845 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4846
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004847 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004848 WILL_LJMP(lua_error(L));
4849
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004850 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004851}
4852
4853__LJMP static int hlua_http_res_rep_val(lua_State *L)
4854{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004855 struct hlua_txn *htxn;
4856
4857 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4858 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4859
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004860 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004861 WILL_LJMP(lua_error(L));
4862
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004863 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004864}
4865
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004866/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004867 * It is a wrapper for the 2 following functions.
4868 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004869__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004870{
4871 size_t len;
4872 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004873 struct htx *htx = htxbuf(&msg->chn->buf);
4874 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004875
Christopher Fauleta2097962019-07-15 16:25:33 +02004876 ctx.blk = NULL;
4877 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4878 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004879 return 0;
4880}
4881
4882__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4883{
4884 struct hlua_txn *htxn;
4885
4886 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4887 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4888
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004889 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004890 WILL_LJMP(lua_error(L));
4891
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004892 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004893}
4894
4895__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4896{
4897 struct hlua_txn *htxn;
4898
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004899 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004900 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4901
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004902 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004903 WILL_LJMP(lua_error(L));
4904
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004905 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004906}
4907
4908/* This function adds an header. It is a wrapper used by
4909 * the 2 following functions.
4910 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004911__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004912{
4913 size_t name_len;
4914 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4915 size_t value_len;
4916 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004917 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004918
Christopher Fauleta2097962019-07-15 16:25:33 +02004919 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4920 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004921 return 0;
4922}
4923
4924__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4925{
4926 struct hlua_txn *htxn;
4927
4928 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4929 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4930
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004931 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004932 WILL_LJMP(lua_error(L));
4933
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004934 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004935}
4936
4937__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4938{
4939 struct hlua_txn *htxn;
4940
4941 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4942 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4943
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004944 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004945 WILL_LJMP(lua_error(L));
4946
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004947 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004948}
4949
4950static int hlua_http_req_set_hdr(lua_State *L)
4951{
4952 struct hlua_txn *htxn;
4953
4954 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4955 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4956
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004957 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004958 WILL_LJMP(lua_error(L));
4959
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004960 hlua_http_del_hdr(L, &htxn->s->txn->req);
4961 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004962}
4963
4964static int hlua_http_res_set_hdr(lua_State *L)
4965{
4966 struct hlua_txn *htxn;
4967
4968 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4969 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4970
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004971 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004972 WILL_LJMP(lua_error(L));
4973
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004974 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4975 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004976}
4977
4978/* This function set the method. */
4979static int hlua_http_req_set_meth(lua_State *L)
4980{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004981 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004982 size_t name_len;
4983 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004984
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004985 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004986 WILL_LJMP(lua_error(L));
4987
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004988 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004989 return 1;
4990}
4991
4992/* This function set the method. */
4993static int hlua_http_req_set_path(lua_State *L)
4994{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004995 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004996 size_t name_len;
4997 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004998
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004999 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005000 WILL_LJMP(lua_error(L));
5001
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005002 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005003 return 1;
5004}
5005
5006/* This function set the query-string. */
5007static int hlua_http_req_set_query(lua_State *L)
5008{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005009 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005010 size_t name_len;
5011 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005012
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005013 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005014 WILL_LJMP(lua_error(L));
5015
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005016 /* Check length. */
5017 if (name_len > trash.size - 1) {
5018 lua_pushboolean(L, 0);
5019 return 1;
5020 }
5021
5022 /* Add the mark question as prefix. */
5023 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005024 trash.area[trash.data++] = '?';
5025 memcpy(trash.area + trash.data, name, name_len);
5026 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005027
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005028 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005029 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005030 return 1;
5031}
5032
5033/* This function set the uri. */
5034static int hlua_http_req_set_uri(lua_State *L)
5035{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005036 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005037 size_t name_len;
5038 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005039
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005040 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005041 WILL_LJMP(lua_error(L));
5042
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005043 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005044 return 1;
5045}
5046
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005047/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005048static int hlua_http_res_set_status(lua_State *L)
5049{
5050 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5051 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005052 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5053 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005054
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005055 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005056 WILL_LJMP(lua_error(L));
5057
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005058 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005059 return 0;
5060}
5061
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005062/*
5063 *
5064 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005065 * Class TXN
5066 *
5067 *
5068 */
5069
5070/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005071 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005072 */
5073__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5074{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005075 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005076}
5077
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005078__LJMP static int hlua_set_var(lua_State *L)
5079{
5080 struct hlua_txn *htxn;
5081 const char *name;
5082 size_t len;
5083 struct sample smp;
5084
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005085 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5086 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005087
5088 /* It is useles to retrieve the stream, but this function
5089 * runs only in a stream context.
5090 */
5091 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5092 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5093
5094 /* Converts the third argument in a sample. */
5095 hlua_lua2smp(L, 3, &smp);
5096
5097 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005098 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005099
5100 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5101 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5102 else
5103 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5104
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005105 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005106}
5107
Christopher Faulet85d79c92016-11-09 16:54:56 +01005108__LJMP static int hlua_unset_var(lua_State *L)
5109{
5110 struct hlua_txn *htxn;
5111 const char *name;
5112 size_t len;
5113 struct sample smp;
5114
5115 MAY_LJMP(check_args(L, 2, "unset_var"));
5116
5117 /* It is useles to retrieve the stream, but this function
5118 * runs only in a stream context.
5119 */
5120 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5121 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5122
5123 /* Unset the variable. */
5124 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005125 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5126 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005127}
5128
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005129__LJMP static int hlua_get_var(lua_State *L)
5130{
5131 struct hlua_txn *htxn;
5132 const char *name;
5133 size_t len;
5134 struct sample smp;
5135
5136 MAY_LJMP(check_args(L, 2, "get_var"));
5137
5138 /* It is useles to retrieve the stream, but this function
5139 * runs only in a stream context.
5140 */
5141 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5142 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5143
Willy Tarreau7560dd42016-03-10 16:28:58 +01005144 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005145 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005146 lua_pushnil(L);
5147 return 1;
5148 }
5149
5150 return hlua_smp2lua(L, &smp);
5151}
5152
Willy Tarreau59551662015-03-10 14:23:13 +01005153__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005154{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005155 struct hlua *hlua;
5156
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005157 MAY_LJMP(check_args(L, 2, "set_priv"));
5158
Willy Tarreau87b09662015-04-03 00:22:06 +02005159 /* It is useles to retrieve the stream, but this function
5160 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005161 */
5162 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005163 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005164
5165 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005166 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005167
5168 /* Get and store new value. */
5169 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5170 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5171
5172 return 0;
5173}
5174
Willy Tarreau59551662015-03-10 14:23:13 +01005175__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005176{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005177 struct hlua *hlua;
5178
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005179 MAY_LJMP(check_args(L, 1, "get_priv"));
5180
Willy Tarreau87b09662015-04-03 00:22:06 +02005181 /* It is useles to retrieve the stream, but this function
5182 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005183 */
5184 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005185 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005186
5187 /* Push configuration index in the stack. */
5188 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5189
5190 return 1;
5191}
5192
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005193/* Create stack entry containing a class TXN. This function
5194 * return 0 if the stack does not contains free slots,
5195 * otherwise it returns 1.
5196 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005197static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005198{
Willy Tarreaude491382015-04-06 11:04:28 +02005199 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005200
5201 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005202 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005203 return 0;
5204
5205 /* NOTE: The allocation never fails. The failure
5206 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005207 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005208 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005209 /* Create the object: obj[0] = userdata. */
5210 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005211 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005212 lua_rawseti(L, -2, 0);
5213
Willy Tarreaude491382015-04-06 11:04:28 +02005214 htxn->s = s;
5215 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005216 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005217 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005218
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005219 /* Create the "f" field that contains a list of fetches. */
5220 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005221 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005222 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005223 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005224
5225 /* Create the "sf" field that contains a list of stringsafe fetches. */
5226 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005227 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005228 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005229 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005230
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005231 /* Create the "c" field that contains a list of converters. */
5232 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005233 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005234 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005235 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005236
5237 /* Create the "sc" field that contains a list of stringsafe converters. */
5238 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005239 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005240 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005241 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005242
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005243 /* Create the "req" field that contains the request channel object. */
5244 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005245 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005246 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005247 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005248
5249 /* Create the "res" field that contains the response channel object. */
5250 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005251 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005252 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005253 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005254
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005255 /* Creates the HTTP object is the current proxy allows http. */
5256 lua_pushstring(L, "http");
5257 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005258 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005259 return 0;
5260 }
5261 else
5262 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005263 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005264
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005265 /* Pop a class sesison metatable and affect it to the userdata. */
5266 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5267 lua_setmetatable(L, -2);
5268
5269 return 1;
5270}
5271
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005272__LJMP static int hlua_txn_deflog(lua_State *L)
5273{
5274 const char *msg;
5275 struct hlua_txn *htxn;
5276
5277 MAY_LJMP(check_args(L, 2, "deflog"));
5278 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5279 msg = MAY_LJMP(luaL_checkstring(L, 2));
5280
5281 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5282 return 0;
5283}
5284
5285__LJMP static int hlua_txn_log(lua_State *L)
5286{
5287 int level;
5288 const char *msg;
5289 struct hlua_txn *htxn;
5290
5291 MAY_LJMP(check_args(L, 3, "log"));
5292 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5293 level = MAY_LJMP(luaL_checkinteger(L, 2));
5294 msg = MAY_LJMP(luaL_checkstring(L, 3));
5295
5296 if (level < 0 || level >= NB_LOG_LEVELS)
5297 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5298
5299 hlua_sendlog(htxn->s->be, level, msg);
5300 return 0;
5301}
5302
5303__LJMP static int hlua_txn_log_debug(lua_State *L)
5304{
5305 const char *msg;
5306 struct hlua_txn *htxn;
5307
5308 MAY_LJMP(check_args(L, 2, "Debug"));
5309 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5310 msg = MAY_LJMP(luaL_checkstring(L, 2));
5311 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5312 return 0;
5313}
5314
5315__LJMP static int hlua_txn_log_info(lua_State *L)
5316{
5317 const char *msg;
5318 struct hlua_txn *htxn;
5319
5320 MAY_LJMP(check_args(L, 2, "Info"));
5321 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5322 msg = MAY_LJMP(luaL_checkstring(L, 2));
5323 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5324 return 0;
5325}
5326
5327__LJMP static int hlua_txn_log_warning(lua_State *L)
5328{
5329 const char *msg;
5330 struct hlua_txn *htxn;
5331
5332 MAY_LJMP(check_args(L, 2, "Warning"));
5333 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5334 msg = MAY_LJMP(luaL_checkstring(L, 2));
5335 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5336 return 0;
5337}
5338
5339__LJMP static int hlua_txn_log_alert(lua_State *L)
5340{
5341 const char *msg;
5342 struct hlua_txn *htxn;
5343
5344 MAY_LJMP(check_args(L, 2, "Alert"));
5345 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5346 msg = MAY_LJMP(luaL_checkstring(L, 2));
5347 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5348 return 0;
5349}
5350
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005351__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5352{
5353 struct hlua_txn *htxn;
5354 int ll;
5355
5356 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5357 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5358 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5359
5360 if (ll < 0 || ll > 7)
5361 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5362
5363 htxn->s->logs.level = ll;
5364 return 0;
5365}
5366
5367__LJMP static int hlua_txn_set_tos(lua_State *L)
5368{
5369 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005370 int tos;
5371
5372 MAY_LJMP(check_args(L, 2, "set_tos"));
5373 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5374 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5375
Willy Tarreau1a18b542018-12-11 16:37:42 +01005376 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005377 return 0;
5378}
5379
5380__LJMP static int hlua_txn_set_mark(lua_State *L)
5381{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005382 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005383 int mark;
5384
5385 MAY_LJMP(check_args(L, 2, "set_mark"));
5386 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5387 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5388
Lukas Tribus579e3e32019-08-11 18:03:45 +02005389 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005390 return 0;
5391}
5392
Patrick Hemmer268a7072018-05-11 12:52:31 -04005393__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5394{
5395 struct hlua_txn *htxn;
5396
5397 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5398 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5399 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5400 return 0;
5401}
5402
5403__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5404{
5405 struct hlua_txn *htxn;
5406
5407 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5408 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5409 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5410 return 0;
5411}
5412
Christopher Faulet700d9e82020-01-31 12:21:52 +01005413/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005414 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005415 * message and terminate the transaction. It returns 1 on success and 0 on
5416 * error. The Reply must be on top of the stack.
5417 */
5418__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5419{
5420 struct htx *htx;
5421 struct htx_sl *sl;
5422 struct h1m h1m;
5423 const char *status, *reason, *body;
5424 size_t status_len, reason_len, body_len;
5425 int ret, code, flags;
5426
5427 code = 200;
5428 status = "200";
5429 status_len = 3;
5430 ret = lua_getfield(L, -1, "status");
5431 if (ret == LUA_TNUMBER) {
5432 code = lua_tointeger(L, -1);
5433 status = lua_tolstring(L, -1, &status_len);
5434 }
5435 lua_pop(L, 1);
5436
5437 reason = http_get_reason(code);
5438 reason_len = strlen(reason);
5439 ret = lua_getfield(L, -1, "reason");
5440 if (ret == LUA_TSTRING)
5441 reason = lua_tolstring(L, -1, &reason_len);
5442 lua_pop(L, 1);
5443
5444 body = NULL;
5445 body_len = 0;
5446 ret = lua_getfield(L, -1, "body");
5447 if (ret == LUA_TSTRING)
5448 body = lua_tolstring(L, -1, &body_len);
5449 lua_pop(L, 1);
5450
5451 /* Prepare the response before inserting the headers */
5452 h1m_init_res(&h1m);
5453 htx = htx_from_buf(&s->res.buf);
5454 channel_htx_truncate(&s->res, htx);
5455 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5456 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5457 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5458 ist2(status, status_len), ist2(reason, reason_len));
5459 }
5460 else {
5461 flags = HTX_SL_F_IS_RESP;
5462 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5463 ist2(status, status_len), ist2(reason, reason_len));
5464 }
5465 if (!sl)
5466 goto fail;
5467 sl->info.res.status = code;
5468
5469 /* Push in the stack the "headers" entry. */
5470 ret = lua_getfield(L, -1, "headers");
5471 if (ret != LUA_TTABLE)
5472 goto skip_headers;
5473
5474 lua_pushnil(L);
5475 while (lua_next(L, -2) != 0) {
5476 struct ist name, value;
5477 const char *n, *v;
5478 size_t nlen, vlen;
5479
5480 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5481 /* Skip element if the key is not a string or if the value is not a table */
5482 goto next_hdr;
5483 }
5484
5485 n = lua_tolstring(L, -2, &nlen);
5486 name = ist2(n, nlen);
5487 if (isteqi(name, ist("content-length"))) {
5488 /* Always skip content-length header. It will be added
5489 * later with the correct len
5490 */
5491 goto next_hdr;
5492 }
5493
5494 /* Loop on header's values */
5495 lua_pushnil(L);
5496 while (lua_next(L, -2)) {
5497 if (!lua_isstring(L, -1)) {
5498 /* Skip the value if it is not a string */
5499 goto next_value;
5500 }
5501
5502 v = lua_tolstring(L, -1, &vlen);
5503 value = ist2(v, vlen);
5504
5505 if (isteqi(name, ist("transfer-encoding")))
5506 h1_parse_xfer_enc_header(&h1m, value);
5507 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5508 goto fail;
5509
5510 next_value:
5511 lua_pop(L, 1);
5512 }
5513
5514 next_hdr:
5515 lua_pop(L, 1);
5516 }
5517 skip_headers:
5518 lua_pop(L, 1);
5519
5520 /* Update h1m flags: CLEN is set if CHNK is not present */
5521 if (!(h1m.flags & H1_MF_CHNK)) {
5522 const char *clen = ultoa(body_len);
5523
5524 h1m.flags |= H1_MF_CLEN;
5525 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5526 goto fail;
5527 }
5528 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5529 h1m.flags |= H1_MF_XFER_LEN;
5530
5531 /* Update HTX start-line flags */
5532 if (h1m.flags & H1_MF_XFER_ENC)
5533 flags |= HTX_SL_F_XFER_ENC;
5534 if (h1m.flags & H1_MF_XFER_LEN) {
5535 flags |= HTX_SL_F_XFER_LEN;
5536 if (h1m.flags & H1_MF_CHNK)
5537 flags |= HTX_SL_F_CHNK;
5538 else if (h1m.flags & H1_MF_CLEN)
5539 flags |= HTX_SL_F_CLEN;
5540 if (h1m.body_len == 0)
5541 flags |= HTX_SL_F_BODYLESS;
5542 }
5543 sl->flags |= flags;
5544
5545
5546 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5547 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5548 !htx_add_endof(htx, HTX_BLK_EOM))
5549 goto fail;
5550
5551 /* Now, forward the response and terminate the transaction */
5552 s->txn->status = code;
5553 htx_to_buf(htx, &s->res.buf);
5554 if (!http_forward_proxy_resp(s, 1))
5555 goto fail;
5556
5557 return 1;
5558
5559 fail:
5560 channel_htx_truncate(&s->res, htx);
5561 return 0;
5562}
5563
5564/* Terminate a transaction if called from a lua action. For TCP streams,
5565 * processing is just aborted. Nothing is returned to the client and all
5566 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5567 * is forwarded to the client before terminating the transaction. On success,
5568 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5569 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5570 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005571 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005572__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005573{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005574 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005575 struct stream *s;
5576 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005577
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005578 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005579
Christopher Faulet700d9e82020-01-31 12:21:52 +01005580 /* If the flags NOTERM is set, we cannot terminate the session, so we
5581 * just end the execution of the current lua code. */
5582 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005583 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005584
Christopher Faulet700d9e82020-01-31 12:21:52 +01005585 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005586 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005587 struct channel *req = &s->req;
5588 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005589
Christopher Faulet700d9e82020-01-31 12:21:52 +01005590 channel_auto_read(req);
5591 channel_abort(req);
5592 channel_auto_close(req);
5593 channel_erase(req);
5594
5595 res->wex = tick_add_ifset(now_ms, res->wto);
5596 channel_auto_read(res);
5597 channel_auto_close(res);
5598 channel_shutr_now(res);
5599
5600 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5601 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005602 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005603
Christopher Faulet700d9e82020-01-31 12:21:52 +01005604 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5605 /* No reply or invalid reply */
5606 s->txn->status = 0;
5607 http_reply_and_close(s, 0, NULL);
5608 }
5609 else {
5610 /* Remove extra args to have the reply on top of the stack */
5611 if (lua_gettop(L) > 2)
5612 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005613
Christopher Faulet700d9e82020-01-31 12:21:52 +01005614 if (!hlua_txn_forward_reply(L, s)) {
5615 if (!(s->flags & SF_ERR_MASK))
5616 s->flags |= SF_ERR_PRXCOND;
5617 lua_pushinteger(L, ACT_RET_ERR);
5618 WILL_LJMP(hlua_done(L));
5619 return 0; /* Never reached */
5620 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005621 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005622
Christopher Faulet700d9e82020-01-31 12:21:52 +01005623 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5624 if (htxn->dir == SMP_OPT_DIR_REQ) {
5625 /* let's log the request time */
5626 s->logs.tv_request = now;
5627 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5628 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5629 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005630
Christopher Faulet700d9e82020-01-31 12:21:52 +01005631 done:
5632 if (!(s->flags & SF_ERR_MASK))
5633 s->flags |= SF_ERR_LOCAL;
5634 if (!(s->flags & SF_FINST_MASK))
5635 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005636
Christopher Faulet4ad73102020-03-05 11:07:31 +01005637 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005638 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005639 return 0;
5640}
5641
Christopher Faulet700d9e82020-01-31 12:21:52 +01005642/*
5643 *
5644 *
5645 * Class REPLY
5646 *
5647 *
5648 */
5649
5650/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5651 * free slots, the function fails and returns 0;
5652 */
5653static int hlua_txn_reply_new(lua_State *L)
5654{
5655 struct hlua_txn *htxn;
5656 const char *reason, *body = NULL;
5657 int ret, status;
5658
5659 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005660 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005661 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5662 WILL_LJMP(lua_error(L));
5663 }
5664
5665 /* Default value */
5666 status = 200;
5667 reason = http_get_reason(status);
5668
5669 if (lua_istable(L, 2)) {
5670 /* load status and reason from the table argument at index 2 */
5671 ret = lua_getfield(L, 2, "status");
5672 if (ret == LUA_TNIL)
5673 goto reason;
5674 else if (ret != LUA_TNUMBER) {
5675 /* invalid status: ignore the reason */
5676 goto body;
5677 }
5678 status = lua_tointeger(L, -1);
5679
5680 reason:
5681 lua_pop(L, 1); /* restore the stack: remove status */
5682 ret = lua_getfield(L, 2, "reason");
5683 if (ret == LUA_TSTRING)
5684 reason = lua_tostring(L, -1);
5685
5686 body:
5687 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5688 ret = lua_getfield(L, 2, "body");
5689 if (ret == LUA_TSTRING)
5690 body = lua_tostring(L, -1);
5691 lua_pop(L, 1); /* restore the stack: remove body */
5692 }
5693
5694 /* Create the Reply table */
5695 lua_newtable(L);
5696
5697 /* Add status element */
5698 lua_pushstring(L, "status");
5699 lua_pushinteger(L, status);
5700 lua_settable(L, -3);
5701
5702 /* Add reason element */
5703 reason = http_get_reason(status);
5704 lua_pushstring(L, "reason");
5705 lua_pushstring(L, reason);
5706 lua_settable(L, -3);
5707
5708 /* Add body element, nil if undefined */
5709 lua_pushstring(L, "body");
5710 if (body)
5711 lua_pushstring(L, body);
5712 else
5713 lua_pushnil(L);
5714 lua_settable(L, -3);
5715
5716 /* Add headers element */
5717 lua_pushstring(L, "headers");
5718 lua_newtable(L);
5719
5720 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5721 if (lua_istable(L, 2)) {
5722 /* load headers from the table argument at index 2. If it is a table, copy it. */
5723 ret = lua_getfield(L, 2, "headers");
5724 if (ret == LUA_TTABLE) {
5725 /* stack: [ ... <headers:table>, <table> ] */
5726 lua_pushnil(L);
5727 while (lua_next(L, -2) != 0) {
5728 /* stack: [ ... <headers:table>, <table>, k, v] */
5729 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5730 /* invalid value type, skip it */
5731 lua_pop(L, 1);
5732 continue;
5733 }
5734
5735
5736 /* Duplicate the key and swap it with the value. */
5737 lua_pushvalue(L, -2);
5738 lua_insert(L, -2);
5739 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5740
5741 lua_newtable(L);
5742 lua_insert(L, -2);
5743 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5744
5745 if (lua_isstring(L, -1)) {
5746 /* push the value in the inner table */
5747 lua_rawseti(L, -2, 1);
5748 }
5749 else { /* table */
5750 lua_pushnil(L);
5751 while (lua_next(L, -2) != 0) {
5752 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5753 if (!lua_isstring(L, -1)) {
5754 /* invalid value type, skip it*/
5755 lua_pop(L, 1);
5756 continue;
5757 }
5758 /* push the value in the inner table */
5759 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5760 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5761 }
5762 lua_pop(L, 1);
5763 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5764 }
5765
5766 /* push (k,v) on the stack in the headers table:
5767 * stack: [ ... <headers:table>, <table>, k, k, v ]
5768 */
5769 lua_settable(L, -5);
5770 /* stack: [ ... <headers:table>, <table>, k ] */
5771 }
5772 }
5773 lua_pop(L, 1);
5774 }
5775 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5776 lua_settable(L, -3);
5777 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5778
5779 /* Pop a class sesison metatable and affect it to the userdata. */
5780 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5781 lua_setmetatable(L, -2);
5782 return 1;
5783}
5784
5785/* Set the reply status code, and optionally the reason. If no reason is
5786 * provided, the default one corresponding to the status code is used.
5787 */
5788__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5789{
5790 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5791 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5792
5793 /* First argument (self) must be a table */
5794 luaL_checktype(L, 1, LUA_TTABLE);
5795
5796 if (status < 100 || status > 599) {
5797 lua_pushboolean(L, 0);
5798 return 1;
5799 }
5800 if (!reason)
5801 reason = http_get_reason(status);
5802
5803 lua_pushinteger(L, status);
5804 lua_setfield(L, 1, "status");
5805
5806 lua_pushstring(L, reason);
5807 lua_setfield(L, 1, "reason");
5808
5809 lua_pushboolean(L, 1);
5810 return 1;
5811}
5812
5813/* Add a header into the reply object. Each header name is associated to an
5814 * array of values in the "headers" table. If the header name is not found, a
5815 * new entry is created.
5816 */
5817__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5818{
5819 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5820 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5821 int ret;
5822
5823 /* First argument (self) must be a table */
5824 luaL_checktype(L, 1, LUA_TTABLE);
5825
5826 /* Push in the stack the "headers" entry. */
5827 ret = lua_getfield(L, 1, "headers");
5828 if (ret != LUA_TTABLE) {
5829 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5830 WILL_LJMP(lua_error(L));
5831 }
5832
5833 /* check if the header is already registered. If not, register it. */
5834 ret = lua_getfield(L, -1, name);
5835 if (ret == LUA_TNIL) {
5836 /* Entry not found. */
5837 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5838
5839 /* Insert the new header name in the array in the top of the stack.
5840 * It left the new array in the top of the stack.
5841 */
5842 lua_newtable(L);
5843 lua_pushstring(L, name);
5844 lua_pushvalue(L, -2);
5845 lua_settable(L, -4);
5846 }
5847 else if (ret != LUA_TTABLE) {
5848 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5849 WILL_LJMP(lua_error(L));
5850 }
5851
5852 /* Now the top od thestack is an array of values. We push
5853 * the header value as new entry.
5854 */
5855 lua_pushstring(L, value);
5856 ret = lua_rawlen(L, -2);
5857 lua_rawseti(L, -2, ret + 1);
5858
5859 lua_pushboolean(L, 1);
5860 return 1;
5861}
5862
5863/* Remove all occurrences of a given header name. */
5864__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5865{
5866 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5867 int ret;
5868
5869 /* First argument (self) must be a table */
5870 luaL_checktype(L, 1, LUA_TTABLE);
5871
5872 /* Push in the stack the "headers" entry. */
5873 ret = lua_getfield(L, 1, "headers");
5874 if (ret != LUA_TTABLE) {
5875 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5876 WILL_LJMP(lua_error(L));
5877 }
5878
5879 lua_pushstring(L, name);
5880 lua_pushnil(L);
5881 lua_settable(L, -3);
5882
5883 lua_pushboolean(L, 1);
5884 return 1;
5885}
5886
5887/* Set the reply's body. Overwrite any existing entry. */
5888__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5889{
5890 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5891
5892 /* First argument (self) must be a table */
5893 luaL_checktype(L, 1, LUA_TTABLE);
5894
5895 lua_pushstring(L, payload);
5896 lua_setfield(L, 1, "body");
5897
5898 lua_pushboolean(L, 1);
5899 return 1;
5900}
5901
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005902__LJMP static int hlua_log(lua_State *L)
5903{
5904 int level;
5905 const char *msg;
5906
5907 MAY_LJMP(check_args(L, 2, "log"));
5908 level = MAY_LJMP(luaL_checkinteger(L, 1));
5909 msg = MAY_LJMP(luaL_checkstring(L, 2));
5910
5911 if (level < 0 || level >= NB_LOG_LEVELS)
5912 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5913
5914 hlua_sendlog(NULL, level, msg);
5915 return 0;
5916}
5917
5918__LJMP static int hlua_log_debug(lua_State *L)
5919{
5920 const char *msg;
5921
5922 MAY_LJMP(check_args(L, 1, "debug"));
5923 msg = MAY_LJMP(luaL_checkstring(L, 1));
5924 hlua_sendlog(NULL, LOG_DEBUG, msg);
5925 return 0;
5926}
5927
5928__LJMP static int hlua_log_info(lua_State *L)
5929{
5930 const char *msg;
5931
5932 MAY_LJMP(check_args(L, 1, "info"));
5933 msg = MAY_LJMP(luaL_checkstring(L, 1));
5934 hlua_sendlog(NULL, LOG_INFO, msg);
5935 return 0;
5936}
5937
5938__LJMP static int hlua_log_warning(lua_State *L)
5939{
5940 const char *msg;
5941
5942 MAY_LJMP(check_args(L, 1, "warning"));
5943 msg = MAY_LJMP(luaL_checkstring(L, 1));
5944 hlua_sendlog(NULL, LOG_WARNING, msg);
5945 return 0;
5946}
5947
5948__LJMP static int hlua_log_alert(lua_State *L)
5949{
5950 const char *msg;
5951
5952 MAY_LJMP(check_args(L, 1, "alert"));
5953 msg = MAY_LJMP(luaL_checkstring(L, 1));
5954 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005955 return 0;
5956}
5957
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005958__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005959{
5960 int wakeup_ms = lua_tointeger(L, -1);
5961 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005962 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005963 return 0;
5964}
5965
5966__LJMP static int hlua_sleep(lua_State *L)
5967{
5968 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005969 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005970
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005971 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005972
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005973 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005974 wakeup_ms = tick_add(now_ms, delay);
5975 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005976
Willy Tarreau9635e032018-10-16 17:52:55 +02005977 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005978 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005979}
5980
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005981__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005982{
5983 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005984 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005985
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005986 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005987
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005988 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005989 wakeup_ms = tick_add(now_ms, delay);
5990 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005991
Willy Tarreau9635e032018-10-16 17:52:55 +02005992 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005993 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005994}
5995
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005996/* This functionis an LUA binding. it permits to give back
5997 * the hand at the HAProxy scheduler. It is used when the
5998 * LUA processing consumes a lot of time.
5999 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006000__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006001{
6002 return 0;
6003}
6004
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006005__LJMP static int hlua_yield(lua_State *L)
6006{
Willy Tarreau9635e032018-10-16 17:52:55 +02006007 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006008 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006009}
6010
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006011/* This function change the nice of the currently executed
6012 * task. It is used set low or high priority at the current
6013 * task.
6014 */
Willy Tarreau59551662015-03-10 14:23:13 +01006015__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006016{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006017 struct hlua *hlua;
6018 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006019
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006020 MAY_LJMP(check_args(L, 1, "set_nice"));
6021 hlua = hlua_gethlua(L);
6022 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006023
6024 /* If he task is not set, I'm in a start mode. */
6025 if (!hlua || !hlua->task)
6026 return 0;
6027
6028 if (nice < -1024)
6029 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006030 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006031 nice = 1024;
6032
6033 hlua->task->nice = nice;
6034 return 0;
6035}
6036
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006037/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006038 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6039 * return an E_AGAIN signal, the emmiter of this signal must set a
6040 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006041 *
6042 * Task wrapper are longjmp safe because the only one Lua code
6043 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006044 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006045struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006046{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006047 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006048 enum hlua_exec status;
6049
Christopher Faulet5bc99722018-04-25 10:34:45 +02006050 if (task->thread_mask == MAX_THREADS_MASK)
6051 task_set_affinity(task, tid_bit);
6052
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006053 /* If it is the first call to the task, we must initialize the
6054 * execution timeouts.
6055 */
6056 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006057 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006058
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006059 /* Execute the Lua code. */
6060 status = hlua_ctx_resume(hlua, 1);
6061
6062 switch (status) {
6063 /* finished or yield */
6064 case HLUA_E_OK:
6065 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006066 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006067 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006068 break;
6069
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006070 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006071 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006072 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006073 break;
6074
6075 /* finished with error. */
6076 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006077 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006078 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006079 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006080 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006081 break;
6082
6083 case HLUA_E_ERR:
6084 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006085 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006086 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006087 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006088 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006089 break;
6090 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006091 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006092}
6093
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006094/* This function is an LUA binding that register LUA function to be
6095 * executed after the HAProxy configuration parsing and before the
6096 * HAProxy scheduler starts. This function expect only one LUA
6097 * argument that is a function. This function returns nothing, but
6098 * throws if an error is encountered.
6099 */
6100__LJMP static int hlua_register_init(lua_State *L)
6101{
6102 struct hlua_init_function *init;
6103 int ref;
6104
6105 MAY_LJMP(check_args(L, 1, "register_init"));
6106
6107 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6108
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006109 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006110 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006111 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006112
6113 init->function_ref = ref;
6114 LIST_ADDQ(&hlua_init_functions, &init->l);
6115 return 0;
6116}
6117
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006118/* This functio is an LUA binding. It permits to register a task
6119 * executed in parallel of the main HAroxy activity. The task is
6120 * created and it is set in the HAProxy scheduler. It can be called
6121 * from the "init" section, "post init" or during the runtime.
6122 *
6123 * Lua prototype:
6124 *
6125 * <none> core.register_task(<function>)
6126 */
6127static int hlua_register_task(lua_State *L)
6128{
6129 struct hlua *hlua;
6130 struct task *task;
6131 int ref;
6132
6133 MAY_LJMP(check_args(L, 1, "register_task"));
6134
6135 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6136
Willy Tarreaubafbe012017-11-24 17:34:44 +01006137 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006138 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006139 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006140
Emeric Brunc60def82017-09-27 14:59:38 +02006141 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006142 if (!task)
6143 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6144
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006145 task->context = hlua;
6146 task->process = hlua_process_task;
6147
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006148 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006149 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006150
6151 /* Restore the function in the stack. */
6152 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6153 hlua->nargs = 0;
6154
6155 /* Schedule task. */
6156 task_schedule(task, now_ms);
6157
6158 return 0;
6159}
6160
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006161/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6162 * doesn't allow "yield" functions because the HAProxy engine cannot
6163 * resume converters.
6164 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006165static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006166{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006167 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006168 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006169 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006170
Willy Tarreaube508f12016-03-10 11:47:01 +01006171 if (!stream)
6172 return 0;
6173
Willy Tarreau87b09662015-04-03 00:22:06 +02006174 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006175 * Lua context can be not initialized. This behavior
6176 * permits to save performances because a systematic
6177 * Lua initialization cause 5% performances loss.
6178 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006179 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006180 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006181 if (!stream->hlua) {
6182 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6183 return 0;
6184 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006185 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006186 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6187 return 0;
6188 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006189 }
6190
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006191 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006192 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006193
6194 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006195 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6196 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6197 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006198 else
6199 error = "critical error";
6200 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006201 return 0;
6202 }
6203
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006204 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006205 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006206 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006207 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006208 return 0;
6209 }
6210
6211 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006213
6214 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006215 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006216 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006217 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006218 return 0;
6219 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006220 hlua_smp2lua(stream->hlua->T, smp);
6221 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006222
6223 /* push keywords in the stack. */
6224 if (arg_p) {
6225 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006226 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006227 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006228 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006229 return 0;
6230 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006231 hlua_arg2lua(stream->hlua->T, arg_p);
6232 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006233 }
6234 }
6235
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006236 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006237 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006238
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006239 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006240 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006241 }
6242
6243 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006244 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006245 /* finished. */
6246 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006247 /* If the stack is empty, the function fails. */
6248 if (lua_gettop(stream->hlua->T) <= 0)
6249 return 0;
6250
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006251 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006252 hlua_lua2smp(stream->hlua->T, -1, smp);
6253 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006254 return 1;
6255
6256 /* yield. */
6257 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006258 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006259 return 0;
6260
6261 /* finished with error. */
6262 case HLUA_E_ERRMSG:
6263 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006264 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006265 fcn->name, lua_tostring(stream->hlua->T, -1));
6266 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006267 return 0;
6268
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006269 case HLUA_E_ETMOUT:
6270 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6271 return 0;
6272
6273 case HLUA_E_NOMEM:
6274 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6275 return 0;
6276
6277 case HLUA_E_YIELD:
6278 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6279 return 0;
6280
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006281 case HLUA_E_ERR:
6282 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006283 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006284 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006285
6286 default:
6287 return 0;
6288 }
6289}
6290
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006291/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6292 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006293 * resume sample-fetches. This function will be called by the sample
6294 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006295 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006296static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6297 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006298{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006299 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006300 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006301 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006302 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006303
Willy Tarreaube508f12016-03-10 11:47:01 +01006304 if (!stream)
6305 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006306
Willy Tarreau87b09662015-04-03 00:22:06 +02006307 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006308 * Lua context can be not initialized. This behavior
6309 * permits to save performances because a systematic
6310 * Lua initialization cause 5% performances loss.
6311 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006312 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006313 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006314 if (!stream->hlua) {
6315 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6316 return 0;
6317 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006318 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006319 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6320 return 0;
6321 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006322 }
6323
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006324 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006325 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006326
6327 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006328 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6329 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6330 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006331 else
6332 error = "critical error";
6333 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006334 return 0;
6335 }
6336
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006337 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006338 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006339 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006340 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006341 return 0;
6342 }
6343
6344 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006345 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006346
6347 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006348 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006349 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006350 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006351 return 0;
6352 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006353 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006354
6355 /* push keywords in the stack. */
6356 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6357 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006358 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006359 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006360 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006361 return 0;
6362 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006363 hlua_arg2lua(stream->hlua->T, arg_p);
6364 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006365 }
6366
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006367 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006368 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006369
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006370 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006371 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006372 }
6373
6374 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006375 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006376 /* finished. */
6377 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006378 /* If the stack is empty, the function fails. */
6379 if (lua_gettop(stream->hlua->T) <= 0)
6380 return 0;
6381
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006382 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006383 hlua_lua2smp(stream->hlua->T, -1, smp);
6384 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006385
6386 /* Set the end of execution flag. */
6387 smp->flags &= ~SMP_F_MAY_CHANGE;
6388 return 1;
6389
6390 /* yield. */
6391 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006392 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006393 return 0;
6394
6395 /* finished with error. */
6396 case HLUA_E_ERRMSG:
6397 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006398 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006399 fcn->name, lua_tostring(stream->hlua->T, -1));
6400 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006401 return 0;
6402
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006403 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006404 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6405 return 0;
6406
6407 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006408 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6409 return 0;
6410
6411 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006412 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6413 return 0;
6414
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006415 case HLUA_E_ERR:
6416 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006417 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006418 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006419
6420 default:
6421 return 0;
6422 }
6423}
6424
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006425/* This function is an LUA binding used for registering
6426 * "sample-conv" functions. It expects a converter name used
6427 * in the haproxy configuration file, and an LUA function.
6428 */
6429__LJMP static int hlua_register_converters(lua_State *L)
6430{
6431 struct sample_conv_kw_list *sck;
6432 const char *name;
6433 int ref;
6434 int len;
6435 struct hlua_function *fcn;
6436
6437 MAY_LJMP(check_args(L, 2, "register_converters"));
6438
6439 /* First argument : converter name. */
6440 name = MAY_LJMP(luaL_checkstring(L, 1));
6441
6442 /* Second argument : lua function. */
6443 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6444
6445 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006446 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006447 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006448 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006449 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006450 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006451 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006452
6453 /* Fill fcn. */
6454 fcn->name = strdup(name);
6455 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006456 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006457 fcn->function_ref = ref;
6458
6459 /* List head */
6460 sck->list.n = sck->list.p = NULL;
6461
6462 /* converter keyword. */
6463 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006464 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006465 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006466 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006467
6468 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6469 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006470 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 +01006471 sck->kw[0].val_args = NULL;
6472 sck->kw[0].in_type = SMP_T_STR;
6473 sck->kw[0].out_type = SMP_T_STR;
6474 sck->kw[0].private = fcn;
6475
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006476 /* Register this new converter */
6477 sample_register_convs(sck);
6478
6479 return 0;
6480}
6481
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006482/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006483 * "sample-fetch" functions. It expects a converter name used
6484 * in the haproxy configuration file, and an LUA function.
6485 */
6486__LJMP static int hlua_register_fetches(lua_State *L)
6487{
6488 const char *name;
6489 int ref;
6490 int len;
6491 struct sample_fetch_kw_list *sfk;
6492 struct hlua_function *fcn;
6493
6494 MAY_LJMP(check_args(L, 2, "register_fetches"));
6495
6496 /* First argument : sample-fetch name. */
6497 name = MAY_LJMP(luaL_checkstring(L, 1));
6498
6499 /* Second argument : lua function. */
6500 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6501
6502 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006503 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006504 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006505 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006506 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006507 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006508 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006509
6510 /* Fill fcn. */
6511 fcn->name = strdup(name);
6512 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006513 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006514 fcn->function_ref = ref;
6515
6516 /* List head */
6517 sfk->list.n = sfk->list.p = NULL;
6518
6519 /* sample-fetch keyword. */
6520 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006521 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006522 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006523 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006524
6525 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6526 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006527 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 +01006528 sfk->kw[0].val_args = NULL;
6529 sfk->kw[0].out_type = SMP_T_STR;
6530 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6531 sfk->kw[0].val = 0;
6532 sfk->kw[0].private = fcn;
6533
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006534 /* Register this new fetch. */
6535 sample_register_fetches(sfk);
6536
6537 return 0;
6538}
6539
Christopher Faulet501465d2020-02-26 14:54:16 +01006540/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006541 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006542__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006543{
6544 struct hlua *hlua = hlua_gethlua(L);
6545 unsigned int delay;
6546 unsigned int wakeup_ms;
6547
6548 MAY_LJMP(check_args(L, 1, "wake_time"));
6549
6550 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6551 wakeup_ms = tick_add(now_ms, delay);
6552 hlua->wake_time = wakeup_ms;
6553 return 0;
6554}
6555
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006556/* This function is a wrapper to execute each LUA function declared as an action
6557 * wrapper during the initialisation period. This function may return any
6558 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6559 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6560 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006561 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006562static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006563 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006564{
6565 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006566 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006567 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006568 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006569
6570 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006571 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6572 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6573 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6574 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006575 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006576 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006577 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006578 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006579
Willy Tarreau87b09662015-04-03 00:22:06 +02006580 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006581 * Lua context can be not initialized. This behavior
6582 * permits to save performances because a systematic
6583 * Lua initialization cause 5% performances loss.
6584 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006585 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006586 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006587 if (!s->hlua) {
6588 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6589 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006590 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006591 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006592 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006593 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6594 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006595 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006596 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006597 }
6598
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006599 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006600 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006601
6602 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006603 if (!SET_SAFE_LJMP(s->hlua->T)) {
6604 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6605 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006606 else
6607 error = "critical error";
6608 SEND_ERR(px, "Lua function '%s': %s.\n",
6609 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006610 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006611 }
6612
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006613 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006614 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006615 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006616 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006617 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006618 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006619 }
6620
6621 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006622 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006623
Willy Tarreau87b09662015-04-03 00:22:06 +02006624 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006625 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006626 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006627 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006628 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006629 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006630 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006631 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006632
6633 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006634 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006635 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006636 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006637 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006638 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006639 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006640 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006641 lua_pushstring(s->hlua->T, *arg);
6642 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006643 }
6644
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006645 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006646 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006647
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006648 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006649 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006650 }
6651
6652 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006653 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006654 /* finished. */
6655 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006656 /* Catch the return value */
6657 if (lua_gettop(s->hlua->T) > 0)
6658 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006659
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006660 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006661 if (act_ret == ACT_RET_YIELD) {
6662 if (flags & ACT_OPT_FINAL)
6663 goto err_yield;
6664
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006665 if (dir == SMP_OPT_DIR_REQ)
6666 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6667 s->hlua->wake_time);
6668 else
6669 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6670 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006671 }
6672 goto end;
6673
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006674 /* yield. */
6675 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006676 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006677 if (dir == SMP_OPT_DIR_REQ)
6678 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6679 s->hlua->wake_time);
6680 else
6681 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6682 s->hlua->wake_time);
6683
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006684 /* Some actions can be wake up when a "write" event
6685 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006686 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006687 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006688 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006689 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006690 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006691 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006692 act_ret = ACT_RET_YIELD;
6693 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006694
6695 /* finished with error. */
6696 case HLUA_E_ERRMSG:
6697 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006698 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006699 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6700 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006701 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006702
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006703 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006704 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006705 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006706
6707 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006708 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006709 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006710
6711 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006712 err_yield:
6713 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006714 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6715 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006716 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006717
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006718 case HLUA_E_ERR:
6719 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006720 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006721 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006722
6723 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006724 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006725 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006726
6727 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006728 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006729 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006730 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006731}
6732
Olivier Houchard9f6af332018-05-25 14:04:04 +02006733struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006734{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006735 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006736
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006737 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006738 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006739 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006740}
6741
6742static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6743{
6744 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006745 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006746 struct task *task;
6747 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006748 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006749
Willy Tarreaubafbe012017-11-24 17:34:44 +01006750 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006751 if (!hlua) {
6752 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6753 ctx->rule->arg.hlua_rule->fcn.name);
6754 return 0;
6755 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006756 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006757 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006758 ctx->ctx.hlua_apptcp.flags = 0;
6759
6760 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006761 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006762 if (!task) {
6763 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6764 ctx->rule->arg.hlua_rule->fcn.name);
6765 return 0;
6766 }
6767 task->nice = 0;
6768 task->context = ctx;
6769 task->process = hlua_applet_wakeup;
6770 ctx->ctx.hlua_apptcp.task = task;
6771
6772 /* In the execution wrappers linked with a stream, the
6773 * Lua context can be not initialized. This behavior
6774 * permits to save performances because a systematic
6775 * Lua initialization cause 5% performances loss.
6776 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006777 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006778 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6779 ctx->rule->arg.hlua_rule->fcn.name);
6780 return 0;
6781 }
6782
6783 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006784 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006785
6786 /* The following Lua calls can fail. */
6787 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006788 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6789 error = lua_tostring(hlua->T, -1);
6790 else
6791 error = "critical error";
6792 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6793 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006794 return 0;
6795 }
6796
6797 /* Check stack available size. */
6798 if (!lua_checkstack(hlua->T, 1)) {
6799 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6800 ctx->rule->arg.hlua_rule->fcn.name);
6801 RESET_SAFE_LJMP(hlua->T);
6802 return 0;
6803 }
6804
6805 /* Restore the function in the stack. */
6806 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6807
6808 /* Create and and push object stream in the stack. */
6809 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6810 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6811 ctx->rule->arg.hlua_rule->fcn.name);
6812 RESET_SAFE_LJMP(hlua->T);
6813 return 0;
6814 }
6815 hlua->nargs = 1;
6816
6817 /* push keywords in the stack. */
6818 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6819 if (!lua_checkstack(hlua->T, 1)) {
6820 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6821 ctx->rule->arg.hlua_rule->fcn.name);
6822 RESET_SAFE_LJMP(hlua->T);
6823 return 0;
6824 }
6825 lua_pushstring(hlua->T, *arg);
6826 hlua->nargs++;
6827 }
6828
6829 RESET_SAFE_LJMP(hlua->T);
6830
6831 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006832 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006833 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006834
6835 return 1;
6836}
6837
Willy Tarreau60409db2019-08-21 14:14:50 +02006838void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006839{
6840 struct stream_interface *si = ctx->owner;
6841 struct stream *strm = si_strm(si);
6842 struct channel *res = si_ic(si);
6843 struct act_rule *rule = ctx->rule;
6844 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006845 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006846
6847 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006848 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6849 /* eat the whole request */
6850 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006851 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006852 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006853
6854 /* If the stream is disconnect or closed, ldo nothing. */
6855 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6856 return;
6857
6858 /* Execute the function. */
6859 switch (hlua_ctx_resume(hlua, 1)) {
6860 /* finished. */
6861 case HLUA_E_OK:
6862 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6863
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006864 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006865 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006866 res->flags |= CF_READ_NULL;
6867 si_shutr(si);
6868 return;
6869
6870 /* yield. */
6871 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006872 if (hlua->wake_time != TICK_ETERNITY)
6873 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006874 return;
6875
6876 /* finished with error. */
6877 case HLUA_E_ERRMSG:
6878 /* Display log. */
6879 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6880 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6881 lua_pop(hlua->T, 1);
6882 goto error;
6883
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006884 case HLUA_E_ETMOUT:
6885 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6886 rule->arg.hlua_rule->fcn.name);
6887 goto error;
6888
6889 case HLUA_E_NOMEM:
6890 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6891 rule->arg.hlua_rule->fcn.name);
6892 goto error;
6893
6894 case HLUA_E_YIELD: /* unexpected */
6895 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6896 rule->arg.hlua_rule->fcn.name);
6897 goto error;
6898
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006899 case HLUA_E_ERR:
6900 /* Display log. */
6901 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6902 rule->arg.hlua_rule->fcn.name);
6903 goto error;
6904
6905 default:
6906 goto error;
6907 }
6908
6909error:
6910
6911 /* For all other cases, just close the stream. */
6912 si_shutw(si);
6913 si_shutr(si);
6914 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6915}
6916
6917static void hlua_applet_tcp_release(struct appctx *ctx)
6918{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006919 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006920 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006921 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006922 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006923}
6924
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006925/* The function returns 1 if the initialisation is complete, 0 if
6926 * an errors occurs and -1 if more data are required for initializing
6927 * the applet.
6928 */
6929static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6930{
6931 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006932 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006933 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006934 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006935 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006936 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006937
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006938 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006939 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006940 if (!hlua) {
6941 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6942 ctx->rule->arg.hlua_rule->fcn.name);
6943 return 0;
6944 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006945 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006946 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006947 ctx->ctx.hlua_apphttp.left_bytes = -1;
6948 ctx->ctx.hlua_apphttp.flags = 0;
6949
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006950 if (txn->req.flags & HTTP_MSGF_VER_11)
6951 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6952
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006953 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006954 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006955 if (!task) {
6956 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6957 ctx->rule->arg.hlua_rule->fcn.name);
6958 return 0;
6959 }
6960 task->nice = 0;
6961 task->context = ctx;
6962 task->process = hlua_applet_wakeup;
6963 ctx->ctx.hlua_apphttp.task = task;
6964
6965 /* In the execution wrappers linked with a stream, the
6966 * Lua context can be not initialized. This behavior
6967 * permits to save performances because a systematic
6968 * Lua initialization cause 5% performances loss.
6969 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006970 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006971 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6972 ctx->rule->arg.hlua_rule->fcn.name);
6973 return 0;
6974 }
6975
6976 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006977 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006978
6979 /* The following Lua calls can fail. */
6980 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006981 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6982 error = lua_tostring(hlua->T, -1);
6983 else
6984 error = "critical error";
6985 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6986 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006987 return 0;
6988 }
6989
6990 /* Check stack available size. */
6991 if (!lua_checkstack(hlua->T, 1)) {
6992 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6993 ctx->rule->arg.hlua_rule->fcn.name);
6994 RESET_SAFE_LJMP(hlua->T);
6995 return 0;
6996 }
6997
6998 /* Restore the function in the stack. */
6999 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7000
7001 /* Create and and push object stream in the stack. */
7002 if (!hlua_applet_http_new(hlua->T, ctx)) {
7003 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7004 ctx->rule->arg.hlua_rule->fcn.name);
7005 RESET_SAFE_LJMP(hlua->T);
7006 return 0;
7007 }
7008 hlua->nargs = 1;
7009
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007010 /* push keywords in the stack. */
7011 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7012 if (!lua_checkstack(hlua->T, 1)) {
7013 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7014 ctx->rule->arg.hlua_rule->fcn.name);
7015 RESET_SAFE_LJMP(hlua->T);
7016 return 0;
7017 }
7018 lua_pushstring(hlua->T, *arg);
7019 hlua->nargs++;
7020 }
7021
7022 RESET_SAFE_LJMP(hlua->T);
7023
7024 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007025 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007026
7027 return 1;
7028}
7029
Willy Tarreau60409db2019-08-21 14:14:50 +02007030void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007031{
7032 struct stream_interface *si = ctx->owner;
7033 struct stream *strm = si_strm(si);
7034 struct channel *req = si_oc(si);
7035 struct channel *res = si_ic(si);
7036 struct act_rule *rule = ctx->rule;
7037 struct proxy *px = strm->be;
7038 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7039 struct htx *req_htx, *res_htx;
7040
7041 res_htx = htx_from_buf(&res->buf);
7042
7043 /* If the stream is disconnect or closed, ldo nothing. */
7044 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7045 goto out;
7046
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007047 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007048 if (!b_size(&res->buf)) {
7049 si_rx_room_blk(si);
7050 goto out;
7051 }
7052 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007053 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007054 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7055
7056 /* Set the currently running flag. */
7057 if (!HLUA_IS_RUNNING(hlua) &&
7058 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7059 struct htx_blk *blk;
7060 size_t count = co_data(req);
7061
7062 if (!count) {
7063 si_cant_get(si);
7064 goto out;
7065 }
7066
7067 /* We need to flush the request header. This left the body for
7068 * the Lua.
7069 */
7070 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007071 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007072 while (count && blk) {
7073 enum htx_blk_type type = htx_get_blk_type(blk);
7074 uint32_t sz = htx_get_blksz(blk);
7075
7076 if (sz > count) {
7077 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007078 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007079 goto out;
7080 }
7081
7082 count -= sz;
7083 co_set_data(req, co_data(req) - sz);
7084 blk = htx_remove_blk(req_htx, blk);
7085
7086 if (type == HTX_BLK_EOH)
7087 break;
7088 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007089 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007090 }
7091
7092 /* Executes The applet if it is not done. */
7093 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7094
7095 /* Execute the function. */
7096 switch (hlua_ctx_resume(hlua, 1)) {
7097 /* finished. */
7098 case HLUA_E_OK:
7099 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7100 break;
7101
7102 /* yield. */
7103 case HLUA_E_AGAIN:
7104 if (hlua->wake_time != TICK_ETERNITY)
7105 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007106 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007107
7108 /* finished with error. */
7109 case HLUA_E_ERRMSG:
7110 /* Display log. */
7111 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7112 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7113 lua_pop(hlua->T, 1);
7114 goto error;
7115
7116 case HLUA_E_ETMOUT:
7117 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7118 rule->arg.hlua_rule->fcn.name);
7119 goto error;
7120
7121 case HLUA_E_NOMEM:
7122 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7123 rule->arg.hlua_rule->fcn.name);
7124 goto error;
7125
7126 case HLUA_E_YIELD: /* unexpected */
7127 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7128 rule->arg.hlua_rule->fcn.name);
7129 goto error;
7130
7131 case HLUA_E_ERR:
7132 /* Display log. */
7133 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7134 rule->arg.hlua_rule->fcn.name);
7135 goto error;
7136
7137 default:
7138 goto error;
7139 }
7140 }
7141
7142 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007143 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7144 goto done;
7145
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007146 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7147 goto error;
7148
Christopher Faulet54b5e212019-06-04 10:08:28 +02007149 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007150 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007151 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7152 si_rx_room_blk(si);
7153 goto out;
7154 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007155 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007156 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7157 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007158 }
7159
7160 done:
7161 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007162 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007163 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007164 si_shutr(si);
7165 }
7166
7167 /* eat the whole request */
7168 if (co_data(req)) {
7169 req_htx = htx_from_buf(&req->buf);
7170 co_htx_skip(req, req_htx, co_data(req));
7171 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007172 }
7173 }
7174
7175 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007176 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007177 return;
7178
7179 error:
7180
7181 /* If we are in HTTP mode, and we are not send any
7182 * data, return a 500 server error in best effort:
7183 * if there is no room available in the buffer,
7184 * just close the connection.
7185 */
7186 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007187 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007188
7189 channel_erase(res);
7190 res->buf.data = b_data(err);
7191 memcpy(res->buf.area, b_head(err), b_data(err));
7192 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007193 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007194 }
7195 if (!(strm->flags & SF_ERR_MASK))
7196 strm->flags |= SF_ERR_RESOURCE;
7197 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7198 goto done;
7199}
7200
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007201static void hlua_applet_http_release(struct appctx *ctx)
7202{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007203 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007204 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007205 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007206 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007207}
7208
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007209/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007210 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007211 *
7212 * This function can fail with an abort() due to an Lua critical error.
7213 * We are in the configuration parsing process of HAProxy, this abort() is
7214 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007215 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007216static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7217 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007218{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007219 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007220 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007221
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007222 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007223 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007224 if (!rule->arg.hlua_rule) {
7225 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007226 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007227 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007228
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007229 /* Memory for arguments. */
7230 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7231 if (!rule->arg.hlua_rule->args) {
7232 memprintf(err, "out of memory error");
7233 return ACT_RET_PRS_ERR;
7234 }
7235
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007236 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007237 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007238
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007239 /* Expect some arguments */
7240 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007241 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007242 memprintf(err, "expect %d arguments", fcn->nargs);
7243 return ACT_RET_PRS_ERR;
7244 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007245 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007246 if (!rule->arg.hlua_rule->args[i]) {
7247 memprintf(err, "out of memory error");
7248 return ACT_RET_PRS_ERR;
7249 }
7250 (*cur_arg)++;
7251 }
7252 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007253
Thierry FOURNIER42148732015-09-02 17:17:33 +02007254 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007255 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007256 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007257}
7258
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007259static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7260 struct act_rule *rule, char **err)
7261{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007262 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007263
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007264 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007265 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007266 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007267 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007268 * the call of this analyzer.
7269 */
7270 if (rule->from != ACT_F_HTTP_REQ) {
7271 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7272 return ACT_RET_PRS_ERR;
7273 }
7274
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007275 /* Memory for the rule. */
7276 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7277 if (!rule->arg.hlua_rule) {
7278 memprintf(err, "out of memory error");
7279 return ACT_RET_PRS_ERR;
7280 }
7281
7282 /* Reference the Lua function and store the reference. */
7283 rule->arg.hlua_rule->fcn = *fcn;
7284
7285 /* TODO: later accept arguments. */
7286 rule->arg.hlua_rule->args = NULL;
7287
7288 /* Add applet pointer in the rule. */
7289 rule->applet.obj_type = OBJ_TYPE_APPLET;
7290 rule->applet.name = fcn->name;
7291 rule->applet.init = hlua_applet_http_init;
7292 rule->applet.fct = hlua_applet_http_fct;
7293 rule->applet.release = hlua_applet_http_release;
7294 rule->applet.timeout = hlua_timeout_applet;
7295
7296 return ACT_RET_PRS_OK;
7297}
7298
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007299/* This function is an LUA binding used for registering
7300 * "sample-conv" functions. It expects a converter name used
7301 * in the haproxy configuration file, and an LUA function.
7302 */
7303__LJMP static int hlua_register_action(lua_State *L)
7304{
7305 struct action_kw_list *akl;
7306 const char *name;
7307 int ref;
7308 int len;
7309 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007310 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007311
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007312 /* Initialise the number of expected arguments at 0. */
7313 nargs = 0;
7314
7315 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7316 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007317
7318 /* First argument : converter name. */
7319 name = MAY_LJMP(luaL_checkstring(L, 1));
7320
7321 /* Second argument : environment. */
7322 if (lua_type(L, 2) != LUA_TTABLE)
7323 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7324
7325 /* Third argument : lua function. */
7326 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7327
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007328 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007329 if (lua_gettop(L) >= 4)
7330 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7331
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007332 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007333 lua_pushnil(L);
7334 while (lua_next(L, 2) != 0) {
7335 if (lua_type(L, -1) != LUA_TSTRING)
7336 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7337
7338 /* Check required environment. Only accepted "http" or "tcp". */
7339 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007340 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007341 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007342 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007343 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007344 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007345 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007346
7347 /* Fill fcn. */
7348 fcn->name = strdup(name);
7349 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007350 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007351 fcn->function_ref = ref;
7352
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007353 /* Set the expected number od arguments. */
7354 fcn->nargs = nargs;
7355
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007356 /* List head */
7357 akl->list.n = akl->list.p = NULL;
7358
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007359 /* action keyword. */
7360 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007361 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007362 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007363 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007364
7365 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7366
7367 akl->kw[0].match_pfx = 0;
7368 akl->kw[0].private = fcn;
7369 akl->kw[0].parse = action_register_lua;
7370
7371 /* select the action registering point. */
7372 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7373 tcp_req_cont_keywords_register(akl);
7374 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7375 tcp_res_cont_keywords_register(akl);
7376 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7377 http_req_keywords_register(akl);
7378 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7379 http_res_keywords_register(akl);
7380 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007381 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007382 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7383 "are expected.", lua_tostring(L, -1)));
7384
7385 /* pop the environment string. */
7386 lua_pop(L, 1);
7387 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007388 return ACT_RET_PRS_OK;
7389}
7390
7391static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7392 struct act_rule *rule, char **err)
7393{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007394 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007395
Christopher Faulet280f85b2019-07-15 15:02:04 +02007396 if (px->mode == PR_MODE_HTTP) {
7397 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007398 return ACT_RET_PRS_ERR;
7399 }
7400
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007401 /* Memory for the rule. */
7402 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7403 if (!rule->arg.hlua_rule) {
7404 memprintf(err, "out of memory error");
7405 return ACT_RET_PRS_ERR;
7406 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007407
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007408 /* Reference the Lua function and store the reference. */
7409 rule->arg.hlua_rule->fcn = *fcn;
7410
7411 /* TODO: later accept arguments. */
7412 rule->arg.hlua_rule->args = NULL;
7413
7414 /* Add applet pointer in the rule. */
7415 rule->applet.obj_type = OBJ_TYPE_APPLET;
7416 rule->applet.name = fcn->name;
7417 rule->applet.init = hlua_applet_tcp_init;
7418 rule->applet.fct = hlua_applet_tcp_fct;
7419 rule->applet.release = hlua_applet_tcp_release;
7420 rule->applet.timeout = hlua_timeout_applet;
7421
7422 return 0;
7423}
7424
7425/* This function is an LUA binding used for registering
7426 * "sample-conv" functions. It expects a converter name used
7427 * in the haproxy configuration file, and an LUA function.
7428 */
7429__LJMP static int hlua_register_service(lua_State *L)
7430{
7431 struct action_kw_list *akl;
7432 const char *name;
7433 const char *env;
7434 int ref;
7435 int len;
7436 struct hlua_function *fcn;
7437
7438 MAY_LJMP(check_args(L, 3, "register_service"));
7439
7440 /* First argument : converter name. */
7441 name = MAY_LJMP(luaL_checkstring(L, 1));
7442
7443 /* Second argument : environment. */
7444 env = MAY_LJMP(luaL_checkstring(L, 2));
7445
7446 /* Third argument : lua function. */
7447 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7448
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007449 /* Allocate and fill the sample fetch keyword struct. */
7450 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7451 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007452 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007453 fcn = calloc(1, sizeof(*fcn));
7454 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007455 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007456
7457 /* Fill fcn. */
7458 len = strlen("<lua.>") + strlen(name) + 1;
7459 fcn->name = calloc(1, len);
7460 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007461 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007462 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7463 fcn->function_ref = ref;
7464
7465 /* List head */
7466 akl->list.n = akl->list.p = NULL;
7467
7468 /* converter keyword. */
7469 len = strlen("lua.") + strlen(name) + 1;
7470 akl->kw[0].kw = calloc(1, len);
7471 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007472 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007473
7474 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7475
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007476 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007477 if (strcmp(env, "tcp") == 0)
7478 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007479 else if (strcmp(env, "http") == 0)
7480 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007481 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007482 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007483 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007484
7485 akl->kw[0].match_pfx = 0;
7486 akl->kw[0].private = fcn;
7487
7488 /* End of array. */
7489 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7490
7491 /* Register this new converter */
7492 service_keywords_register(akl);
7493
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007494 return 0;
7495}
7496
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007497/* This function initialises Lua cli handler. It copies the
7498 * arguments in the Lua stack and create channel IO objects.
7499 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007500static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007501{
7502 struct hlua *hlua;
7503 struct hlua_function *fcn;
7504 int i;
7505 const char *error;
7506
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007507 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007508 appctx->ctx.hlua_cli.fcn = private;
7509
Willy Tarreaubafbe012017-11-24 17:34:44 +01007510 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007511 if (!hlua) {
7512 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007513 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007514 }
7515 HLUA_INIT(hlua);
7516 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007517
7518 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007519 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007520 * applet_http. It is absolutely compatible.
7521 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007522 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007523 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007524 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007525 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007526 }
7527 appctx->ctx.hlua_cli.task->nice = 0;
7528 appctx->ctx.hlua_cli.task->context = appctx;
7529 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7530
7531 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007532 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007533 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007534 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007535 }
7536
7537 /* The following Lua calls can fail. */
7538 if (!SET_SAFE_LJMP(hlua->T)) {
7539 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7540 error = lua_tostring(hlua->T, -1);
7541 else
7542 error = "critical error";
7543 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7544 goto error;
7545 }
7546
7547 /* Check stack available size. */
7548 if (!lua_checkstack(hlua->T, 2)) {
7549 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7550 goto error;
7551 }
7552
7553 /* Restore the function in the stack. */
7554 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7555
7556 /* Once the arguments parsed, the CLI is like an AppletTCP,
7557 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007558 */
7559 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7560 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7561 goto error;
7562 }
7563 hlua->nargs = 1;
7564
7565 /* push keywords in the stack. */
7566 for (i = 0; *args[i]; i++) {
7567 /* Check stack available size. */
7568 if (!lua_checkstack(hlua->T, 1)) {
7569 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7570 goto error;
7571 }
7572 lua_pushstring(hlua->T, args[i]);
7573 hlua->nargs++;
7574 }
7575
7576 /* We must initialize the execution timeouts. */
7577 hlua->max_time = hlua_timeout_session;
7578
7579 /* At this point the execution is safe. */
7580 RESET_SAFE_LJMP(hlua->T);
7581
7582 /* It's ok */
7583 return 0;
7584
7585 /* It's not ok. */
7586error:
7587 RESET_SAFE_LJMP(hlua->T);
7588 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007589 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007590 return 1;
7591}
7592
7593static int hlua_cli_io_handler_fct(struct appctx *appctx)
7594{
7595 struct hlua *hlua;
7596 struct stream_interface *si;
7597 struct hlua_function *fcn;
7598
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007599 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007600 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007601 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007602
7603 /* If the stream is disconnect or closed, ldo nothing. */
7604 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7605 return 1;
7606
7607 /* Execute the function. */
7608 switch (hlua_ctx_resume(hlua, 1)) {
7609
7610 /* finished. */
7611 case HLUA_E_OK:
7612 return 1;
7613
7614 /* yield. */
7615 case HLUA_E_AGAIN:
7616 /* We want write. */
7617 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007618 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007619 /* Set the timeout. */
7620 if (hlua->wake_time != TICK_ETERNITY)
7621 task_schedule(hlua->task, hlua->wake_time);
7622 return 0;
7623
7624 /* finished with error. */
7625 case HLUA_E_ERRMSG:
7626 /* Display log. */
7627 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7628 fcn->name, lua_tostring(hlua->T, -1));
7629 lua_pop(hlua->T, 1);
7630 return 1;
7631
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007632 case HLUA_E_ETMOUT:
7633 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7634 fcn->name);
7635 return 1;
7636
7637 case HLUA_E_NOMEM:
7638 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7639 fcn->name);
7640 return 1;
7641
7642 case HLUA_E_YIELD: /* unexpected */
7643 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7644 fcn->name);
7645 return 1;
7646
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007647 case HLUA_E_ERR:
7648 /* Display log. */
7649 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7650 fcn->name);
7651 return 1;
7652
7653 default:
7654 return 1;
7655 }
7656
7657 return 1;
7658}
7659
7660static void hlua_cli_io_release_fct(struct appctx *appctx)
7661{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007662 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007663 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007664}
7665
7666/* This function is an LUA binding used for registering
7667 * new keywords in the cli. It expects a list of keywords
7668 * which are the "path". It is limited to 5 keywords. A
7669 * description of the command, a function to be executed
7670 * for the parsing and a function for io handlers.
7671 */
7672__LJMP static int hlua_register_cli(lua_State *L)
7673{
7674 struct cli_kw_list *cli_kws;
7675 const char *message;
7676 int ref_io;
7677 int len;
7678 struct hlua_function *fcn;
7679 int index;
7680 int i;
7681
7682 MAY_LJMP(check_args(L, 3, "register_cli"));
7683
7684 /* First argument : an array of maximum 5 keywords. */
7685 if (!lua_istable(L, 1))
7686 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7687
7688 /* Second argument : string with contextual message. */
7689 message = MAY_LJMP(luaL_checkstring(L, 2));
7690
7691 /* Third and fourth argument : lua function. */
7692 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7693
7694 /* Allocate and fill the sample fetch keyword struct. */
7695 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7696 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007697 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007698 fcn = calloc(1, sizeof(*fcn));
7699 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007700 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007701
7702 /* Fill path. */
7703 index = 0;
7704 lua_pushnil(L);
7705 while(lua_next(L, 1) != 0) {
7706 if (index >= 5)
7707 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7708 if (lua_type(L, -1) != LUA_TSTRING)
7709 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7710 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7711 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007712 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007713 index++;
7714 lua_pop(L, 1);
7715 }
7716
7717 /* Copy help message. */
7718 cli_kws->kw[0].usage = strdup(message);
7719 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007720 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007721
7722 /* Fill fcn io handler. */
7723 len = strlen("<lua.cli>") + 1;
7724 for (i = 0; i < index; i++)
7725 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7726 fcn->name = calloc(1, len);
7727 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007728 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007729 strncat((char *)fcn->name, "<lua.cli", len);
7730 for (i = 0; i < index; i++) {
7731 strncat((char *)fcn->name, ".", len);
7732 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7733 }
7734 strncat((char *)fcn->name, ">", len);
7735 fcn->function_ref = ref_io;
7736
7737 /* Fill last entries. */
7738 cli_kws->kw[0].private = fcn;
7739 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7740 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7741 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7742
7743 /* Register this new converter */
7744 cli_register_kw(cli_kws);
7745
7746 return 0;
7747}
7748
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007749static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7750 struct proxy *defpx, const char *file, int line,
7751 char **err, unsigned int *timeout)
7752{
7753 const char *error;
7754
7755 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007756 if (error == PARSE_TIME_OVER) {
7757 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7758 args[1], args[0]);
7759 return -1;
7760 }
7761 else if (error == PARSE_TIME_UNDER) {
7762 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7763 args[1], args[0]);
7764 return -1;
7765 }
7766 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007767 memprintf(err, "%s: invalid timeout", args[0]);
7768 return -1;
7769 }
7770 return 0;
7771}
7772
7773static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7774 struct proxy *defpx, const char *file, int line,
7775 char **err)
7776{
7777 return hlua_read_timeout(args, section_type, curpx, defpx,
7778 file, line, err, &hlua_timeout_session);
7779}
7780
7781static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7782 struct proxy *defpx, const char *file, int line,
7783 char **err)
7784{
7785 return hlua_read_timeout(args, section_type, curpx, defpx,
7786 file, line, err, &hlua_timeout_task);
7787}
7788
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007789static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7790 struct proxy *defpx, const char *file, int line,
7791 char **err)
7792{
7793 return hlua_read_timeout(args, section_type, curpx, defpx,
7794 file, line, err, &hlua_timeout_applet);
7795}
7796
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007797static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7798 struct proxy *defpx, const char *file, int line,
7799 char **err)
7800{
7801 char *error;
7802
7803 hlua_nb_instruction = strtoll(args[1], &error, 10);
7804 if (*error != '\0') {
7805 memprintf(err, "%s: invalid number", args[0]);
7806 return -1;
7807 }
7808 return 0;
7809}
7810
Willy Tarreau32f61e22015-03-18 17:54:59 +01007811static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7812 struct proxy *defpx, const char *file, int line,
7813 char **err)
7814{
7815 char *error;
7816
7817 if (*(args[1]) == 0) {
7818 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7819 return -1;
7820 }
7821 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7822 if (*error != '\0') {
7823 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7824 return -1;
7825 }
7826 return 0;
7827}
7828
7829
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007830/* This function is called by the main configuration key "lua-load". It loads and
7831 * execute an lua file during the parsing of the HAProxy configuration file. It is
7832 * the main lua entry point.
7833 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007834 * This function runs with the HAProxy keywords API. It returns -1 if an error
7835 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007836 *
7837 * In some error case, LUA set an error message in top of the stack. This function
7838 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007839 *
7840 * This function can fail with an abort() due to an Lua critical error.
7841 * We are in the configuration parsing process of HAProxy, this abort() is
7842 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007843 */
7844static int hlua_load(char **args, int section_type, struct proxy *curpx,
7845 struct proxy *defpx, const char *file, int line,
7846 char **err)
7847{
7848 int error;
7849
7850 /* Just load and compile the file. */
7851 error = luaL_loadfile(gL.T, args[1]);
7852 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007853 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007854 lua_pop(gL.T, 1);
7855 return -1;
7856 }
7857
7858 /* If no syntax error where detected, execute the code. */
7859 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7860 switch (error) {
7861 case LUA_OK:
7862 break;
7863 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007864 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007865 lua_pop(gL.T, 1);
7866 return -1;
7867 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007868 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007869 return -1;
7870 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007871 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007872 lua_pop(gL.T, 1);
7873 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007874#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007875 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007876 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007877 lua_pop(gL.T, 1);
7878 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007879#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007880 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007881 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007882 lua_pop(gL.T, 1);
7883 return -1;
7884 }
7885
7886 return 0;
7887}
7888
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007889/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7890 * in the given <ctx>.
7891 */
7892static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7893{
7894 lua_getglobal(ctx.T, "package"); /* push package variable */
7895 lua_pushstring(ctx.T, path); /* push given path */
7896 lua_pushstring(ctx.T, ";"); /* push semicolon */
7897 lua_getfield(ctx.T, -3, type); /* push old path */
7898 lua_concat(ctx.T, 3); /* concatenate to new path */
7899 lua_setfield(ctx.T, -2, type); /* store new path */
7900 lua_pop(ctx.T, 1); /* pop package variable */
7901
7902 return 0;
7903}
7904
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007905static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7906 struct proxy *defpx, const char *file, int line,
7907 char **err)
7908{
7909 char *path;
7910 char *type = "path";
7911 if (too_many_args(2, args, err, NULL)) {
7912 return -1;
7913 }
7914
7915 if (!(*args[1])) {
7916 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7917 return -1;
7918 }
7919 path = args[1];
7920
7921 if (*args[2]) {
7922 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7923 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7924 return -1;
7925 }
7926 type = args[2];
7927 }
7928
7929 return hlua_prepend_path(gL, type, path);
7930}
7931
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007932/* configuration keywords declaration */
7933static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007934 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007935 { CFG_GLOBAL, "lua-load", hlua_load },
7936 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7937 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007938 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007939 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007940 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007941 { 0, NULL, NULL },
7942}};
7943
Willy Tarreau0108d902018-11-25 19:14:37 +01007944INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7945
Christopher Fauletafd8f102018-11-08 11:34:21 +01007946
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007947/* This function can fail with an abort() due to an Lua critical error.
7948 * We are in the initialisation process of HAProxy, this abort() is
7949 * tolerated.
7950 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007951int hlua_post_init()
7952{
7953 struct hlua_init_function *init;
7954 const char *msg;
7955 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007956 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007957
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007958 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007959 if (!SET_SAFE_LJMP(gL.T)) {
7960 if (lua_type(gL.T, -1) == LUA_TSTRING)
7961 error = lua_tostring(gL.T, -1);
7962 else
7963 error = "critical error";
7964 fprintf(stderr, "Lua post-init: %s.\n", error);
7965 exit(1);
7966 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007967
7968#if USE_OPENSSL
7969 /* Initialize SSL server. */
7970 if (socket_ssl.xprt->prepare_srv) {
7971 int saved_used_backed = global.ssl_used_backend;
7972 // don't affect maxconn automatic computation
7973 socket_ssl.xprt->prepare_srv(&socket_ssl);
7974 global.ssl_used_backend = saved_used_backed;
7975 }
7976#endif
7977
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007978 hlua_fcn_post_init(gL.T);
7979 RESET_SAFE_LJMP(gL.T);
7980
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007981 list_for_each_entry(init, &hlua_init_functions, l) {
7982 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7983 ret = hlua_ctx_resume(&gL, 0);
7984 switch (ret) {
7985 case HLUA_E_OK:
7986 lua_pop(gL.T, -1);
7987 return 1;
7988 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007989 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007990 return 0;
7991 case HLUA_E_ERRMSG:
7992 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007993 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007994 return 0;
7995 case HLUA_E_ERR:
7996 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007997 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007998 return 0;
7999 }
8000 }
8001 return 1;
8002}
8003
Willy Tarreau32f61e22015-03-18 17:54:59 +01008004/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8005 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8006 * is the previously allocated size or the kind of object in case of a new
8007 * allocation. <nsize> is the requested new size.
8008 */
8009static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8010{
8011 struct hlua_mem_allocator *zone = ud;
8012
8013 if (nsize == 0) {
8014 /* it's a free */
8015 if (ptr)
8016 zone->allocated -= osize;
8017 free(ptr);
8018 return NULL;
8019 }
8020
8021 if (!ptr) {
8022 /* it's a new allocation */
8023 if (zone->limit && zone->allocated + nsize > zone->limit)
8024 return NULL;
8025
8026 ptr = malloc(nsize);
8027 if (ptr)
8028 zone->allocated += nsize;
8029 return ptr;
8030 }
8031
8032 /* it's a realloc */
8033 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8034 return NULL;
8035
8036 ptr = realloc(ptr, nsize);
8037 if (ptr)
8038 zone->allocated += nsize - osize;
8039 return ptr;
8040}
8041
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008042/* Ithis function can fail with an abort() due to an Lua critical error.
8043 * We are in the initialisation process of HAProxy, this abort() is
8044 * tolerated.
8045 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008046void hlua_init(void)
8047{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008048 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008049 int idx;
8050 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008051 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008052 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008053 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008054#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008055 struct srv_kw *kw;
8056 int tmp_error;
8057 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008058 char *args[] = { /* SSL client configuration. */
8059 "ssl",
8060 "verify",
8061 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008062 NULL
8063 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008064#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008065
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008066 /* Init main lua stack. */
8067 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008068 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008069 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008070 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008071 hlua_sethlua(&gL);
8072 gL.Tref = LUA_REFNIL;
8073 gL.task = NULL;
8074
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008075 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008076 * the Lua function can fail with an abort. We are in the initialisation
8077 * process of HAProxy, this abort() is tolerated.
8078 */
8079
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008080 /* Initialise lua. */
8081 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008082#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8083#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8084#ifdef HLUA_PREPEND_PATH
8085 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8086#endif
8087#ifdef HLUA_PREPEND_CPATH
8088 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8089#endif
8090#undef HLUA_PREPEND_PATH_TOSTRING
8091#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008092
Thierry Fournier75933d42016-01-21 09:30:18 +01008093 /* Set safe environment for the initialisation. */
8094 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008095 if (lua_type(gL.T, -1) == LUA_TSTRING)
8096 error_msg = lua_tostring(gL.T, -1);
8097 else
8098 error_msg = "critical error";
8099 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008100 exit(1);
8101 }
8102
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008103 /*
8104 *
8105 * Create "core" object.
8106 *
8107 */
8108
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008109 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008110 lua_newtable(gL.T);
8111
8112 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008113 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008114 hlua_class_const_int(gL.T, log_levels[i], i);
8115
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008116 /* Register special functions. */
8117 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008118 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008119 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008120 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008121 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008122 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008123 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008124 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008125 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008126 hlua_class_function(gL.T, "sleep", hlua_sleep);
8127 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008128 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8129 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8130 hlua_class_function(gL.T, "set_map", hlua_set_map);
8131 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008132 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008133 hlua_class_function(gL.T, "log", hlua_log);
8134 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8135 hlua_class_function(gL.T, "Info", hlua_log_info);
8136 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8137 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008138 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008139 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008140
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008141 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008142
8143 /*
8144 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008145 * Create "act" object.
8146 *
8147 */
8148
8149 /* This table entry is the object "act" base. */
8150 lua_newtable(gL.T);
8151
8152 /* push action return constants */
8153 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8154 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8155 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8156 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8157 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8158 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8159 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8160 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8161
Christopher Faulet501465d2020-02-26 14:54:16 +01008162 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008163
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008164 lua_setglobal(gL.T, "act");
8165
8166 /*
8167 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008168 * Register class Map
8169 *
8170 */
8171
8172 /* This table entry is the object "Map" base. */
8173 lua_newtable(gL.T);
8174
8175 /* register pattern types. */
8176 for (i=0; i<PAT_MATCH_NUM; i++)
8177 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008178 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008179 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8180 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008181 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008182
8183 /* register constructor. */
8184 hlua_class_function(gL.T, "new", hlua_map_new);
8185
8186 /* Create and fill the metatable. */
8187 lua_newtable(gL.T);
8188
Ilya Shipitsind4259502020-04-08 01:07:56 +05008189 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008190 lua_pushstring(gL.T, "__index");
8191 lua_newtable(gL.T);
8192
8193 /* Register . */
8194 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8195 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8196
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008197 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008198
Thierry Fournier45e78d72016-02-19 18:34:46 +01008199 /* Register previous table in the registry with reference and named entry.
8200 * The function hlua_register_metatable() pops the stack, so we
8201 * previously create a copy of the table.
8202 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008203 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008204 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008205
8206 /* Assign the metatable to the mai Map object. */
8207 lua_setmetatable(gL.T, -2);
8208
8209 /* Set a name to the table. */
8210 lua_setglobal(gL.T, "Map");
8211
8212 /*
8213 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008214 * Register class Channel
8215 *
8216 */
8217
8218 /* Create and fill the metatable. */
8219 lua_newtable(gL.T);
8220
Ilya Shipitsind4259502020-04-08 01:07:56 +05008221 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008222 lua_pushstring(gL.T, "__index");
8223 lua_newtable(gL.T);
8224
8225 /* Register . */
8226 hlua_class_function(gL.T, "get", hlua_channel_get);
8227 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8228 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8229 hlua_class_function(gL.T, "set", hlua_channel_set);
8230 hlua_class_function(gL.T, "append", hlua_channel_append);
8231 hlua_class_function(gL.T, "send", hlua_channel_send);
8232 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8233 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8234 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008235 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008236 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008237
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008238 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008239
8240 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008241 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008242
8243 /*
8244 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008245 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008246 *
8247 */
8248
8249 /* Create and fill the metatable. */
8250 lua_newtable(gL.T);
8251
Ilya Shipitsind4259502020-04-08 01:07:56 +05008252 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008253 lua_pushstring(gL.T, "__index");
8254 lua_newtable(gL.T);
8255
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008256 /* Browse existing fetches and create the associated
8257 * object method.
8258 */
8259 sf = NULL;
8260 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8261
8262 /* Dont register the keywork if the arguments check function are
8263 * not safe during the runtime.
8264 */
8265 if ((sf->val_args != NULL) &&
8266 (sf->val_args != val_payload_lv) &&
8267 (sf->val_args != val_hdr))
8268 continue;
8269
8270 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8271 * by an underscore.
8272 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008273 strncpy(trash.area, sf->kw, trash.size);
8274 trash.area[trash.size - 1] = '\0';
8275 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008276 if (*p == '.' || *p == '-' || *p == '+')
8277 *p = '_';
8278
8279 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008280 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008281 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008282 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008283 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008284 }
8285
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008286 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008287
8288 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008289 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008290
8291 /*
8292 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008293 * Register class Converters
8294 *
8295 */
8296
8297 /* Create and fill the metatable. */
8298 lua_newtable(gL.T);
8299
8300 /* Create and fill the __index entry. */
8301 lua_pushstring(gL.T, "__index");
8302 lua_newtable(gL.T);
8303
8304 /* Browse existing converters and create the associated
8305 * object method.
8306 */
8307 sc = NULL;
8308 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8309 /* Dont register the keywork if the arguments check function are
8310 * not safe during the runtime.
8311 */
8312 if (sc->val_args != NULL)
8313 continue;
8314
8315 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8316 * by an underscore.
8317 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008318 strncpy(trash.area, sc->kw, trash.size);
8319 trash.area[trash.size - 1] = '\0';
8320 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008321 if (*p == '.' || *p == '-' || *p == '+')
8322 *p = '_';
8323
8324 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008325 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008326 lua_pushlightuserdata(gL.T, sc);
8327 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008328 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008329 }
8330
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008331 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008332
8333 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008334 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008335
8336 /*
8337 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008338 * Register class HTTP
8339 *
8340 */
8341
8342 /* Create and fill the metatable. */
8343 lua_newtable(gL.T);
8344
Ilya Shipitsind4259502020-04-08 01:07:56 +05008345 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008346 lua_pushstring(gL.T, "__index");
8347 lua_newtable(gL.T);
8348
8349 /* Register Lua functions. */
8350 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8351 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8352 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8353 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8354 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8355 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8356 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8357 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8358 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8359 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8360
8361 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8362 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8363 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8364 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8365 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8366 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008367 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008368
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008369 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008370
8371 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008372 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008373
8374 /*
8375 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008376 * Register class AppletTCP
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 FOURNIERf0a64b62015-09-19 12:36:17 +02008384 lua_pushstring(gL.T, "__index");
8385 lua_newtable(gL.T);
8386
8387 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008388 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8389 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8390 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8391 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8392 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008393 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8394 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8395 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008396
8397 lua_settable(gL.T, -3);
8398
8399 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008400 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008401
8402 /*
8403 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008404 * Register class AppletHTTP
8405 *
8406 */
8407
8408 /* Create and fill the metatable. */
8409 lua_newtable(gL.T);
8410
Ilya Shipitsind4259502020-04-08 01:07:56 +05008411 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008412 lua_pushstring(gL.T, "__index");
8413 lua_newtable(gL.T);
8414
8415 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008416 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8417 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008418 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8419 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8420 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008421 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8422 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8423 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8424 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8425 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8426 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8427
8428 lua_settable(gL.T, -3);
8429
8430 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008431 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008432
8433 /*
8434 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008435 * Register class TXN
8436 *
8437 */
8438
8439 /* Create and fill the metatable. */
8440 lua_newtable(gL.T);
8441
Ilya Shipitsind4259502020-04-08 01:07:56 +05008442 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008443 lua_pushstring(gL.T, "__index");
8444 lua_newtable(gL.T);
8445
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008446 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008447 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8448 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8449 hlua_class_function(gL.T, "set_var", hlua_set_var);
8450 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8451 hlua_class_function(gL.T, "get_var", hlua_get_var);
8452 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008453 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008454 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8455 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8456 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8457 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8458 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8459 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8460 hlua_class_function(gL.T, "log", hlua_txn_log);
8461 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8462 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8463 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8464 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008465
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008466 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008467
8468 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008469 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008470
8471 /*
8472 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008473 * Register class reply
8474 *
8475 */
8476 lua_newtable(gL.T);
8477 lua_pushstring(gL.T, "__index");
8478 lua_newtable(gL.T);
8479 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8480 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8481 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8482 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8483 lua_settable(gL.T, -3); /* Sets the __index entry. */
8484 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8485
8486
8487 /*
8488 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008489 * Register class Socket
8490 *
8491 */
8492
8493 /* Create and fill the metatable. */
8494 lua_newtable(gL.T);
8495
Ilya Shipitsind4259502020-04-08 01:07:56 +05008496 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008497 lua_pushstring(gL.T, "__index");
8498 lua_newtable(gL.T);
8499
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008500#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008501 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008502#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008503 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8504 hlua_class_function(gL.T, "send", hlua_socket_send);
8505 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8506 hlua_class_function(gL.T, "close", hlua_socket_close);
8507 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8508 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8509 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8510 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8511
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008512 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008513
8514 /* Register the garbage collector entry. */
8515 lua_pushstring(gL.T, "__gc");
8516 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008517 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008518
8519 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008520 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008521
8522 /* Proxy and server configuration initialisation. */
8523 memset(&socket_proxy, 0, sizeof(socket_proxy));
8524 init_new_proxy(&socket_proxy);
8525 socket_proxy.parent = NULL;
8526 socket_proxy.last_change = now.tv_sec;
8527 socket_proxy.id = "LUA-SOCKET";
8528 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8529 socket_proxy.maxconn = 0;
8530 socket_proxy.accept = NULL;
8531 socket_proxy.options2 |= PR_O2_INDEPSTR;
8532 socket_proxy.srv = NULL;
8533 socket_proxy.conn_retries = 0;
8534 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8535
8536 /* Init TCP server: unchanged parameters */
8537 memset(&socket_tcp, 0, sizeof(socket_tcp));
8538 socket_tcp.next = NULL;
8539 socket_tcp.proxy = &socket_proxy;
8540 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8541 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008542 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008543 socket_tcp.idle_conns = NULL;
8544 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008545 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008546 socket_tcp.last_change = 0;
8547 socket_tcp.id = "LUA-TCP-CONN";
8548 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8549 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8550 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8551
8552 /* XXX: Copy default parameter from default server,
8553 * but the default server is not initialized.
8554 */
8555 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8556 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8557 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8558 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8559 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8560 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8561 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8562 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8563 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8564 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8565
8566 socket_tcp.check.status = HCHK_STATUS_INI;
8567 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8568 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8569 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8570 socket_tcp.check.server = &socket_tcp;
8571
8572 socket_tcp.agent.status = HCHK_STATUS_INI;
8573 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8574 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8575 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8576 socket_tcp.agent.server = &socket_tcp;
8577
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008578 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008579
8580#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008581 /* Init TCP server: unchanged parameters */
8582 memset(&socket_ssl, 0, sizeof(socket_ssl));
8583 socket_ssl.next = NULL;
8584 socket_ssl.proxy = &socket_proxy;
8585 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8586 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008587 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008588 socket_ssl.idle_conns = NULL;
8589 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008590 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008591 socket_ssl.last_change = 0;
8592 socket_ssl.id = "LUA-SSL-CONN";
8593 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8594 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8595 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8596
8597 /* XXX: Copy default parameter from default server,
8598 * but the default server is not initialized.
8599 */
8600 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8601 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8602 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8603 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8604 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8605 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8606 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8607 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8608 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8609 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8610
8611 socket_ssl.check.status = HCHK_STATUS_INI;
8612 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8613 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8614 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8615 socket_ssl.check.server = &socket_ssl;
8616
8617 socket_ssl.agent.status = HCHK_STATUS_INI;
8618 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8619 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8620 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8621 socket_ssl.agent.server = &socket_ssl;
8622
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008623 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008624 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008625
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008626 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008627 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8628 /*
8629 *
8630 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008631 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008632 * features like client certificates and ssl_verify.
8633 *
8634 */
8635 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8636 if (tmp_error != 0) {
8637 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8638 abort(); /* This must be never arrives because the command line
8639 not editable by the user. */
8640 }
8641 idx += kw->skip;
8642 }
8643 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008644#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008645
8646 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008647}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008648
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008649static void hlua_deinit()
8650{
8651 lua_close(gL.T);
8652}
8653
8654REGISTER_POST_DEINIT(hlua_deinit);
8655
Willy Tarreau80713382018-11-26 10:19:54 +01008656static void hlua_register_build_options(void)
8657{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008658 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008659
Willy Tarreaubb57d942016-12-21 19:04:56 +01008660 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8661 hap_register_build_opts(ptr, 1);
8662}
Willy Tarreau80713382018-11-26 10:19:54 +01008663
8664INITCALL0(STG_REGISTER, hlua_register_build_options);